Repository navigation
Expand file tree
/
Copy pathmodem.c
More file actions
executable file
·253 lines (228 loc) · 5.78 KB
/
Copy pathmodem.c
File metadata and controls
executable file
·253 lines (228 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* modem - Put modem into DIALIN or DIALOUT mode. Author: F. van Kempen */
/* Exit: 0 OK, suspended/restarted GETTY
* 1 UNIX error
* 2 Process busy
* Version: 1.3 12/30/89
*
* Author: F. van Kempen, MicroWalt Corporation
*
* All fancy stuff removed, see getty.c. Kees J. Bot.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <utmp.h>
#include <errno.h>
#include <minix/paths.h>
char PATH_UTMP[] = _PATH_UTMP; /* current logins */
_PROTOTYPE(void usage , (void));
_PROTOTYPE(int main , (int argc , char *argv []));
_PROTOTYPE(void sendcodes , (char *tty, char *codes));
void usage()
{
fprintf(stderr,
"Usage: modem [-sio] [-I in-codes] [-O out-codes] line [command args ...]\n");
exit(1);
}
main(argc, argv)
int argc;
char *argv[];
{
struct utmp entry;
char devtty[1024], *tty;
char **command;
int ex_code = 0;
int fd, i, slot, getty;
struct stat st;
enum { TOGGLE, DIALIN, DIALOUT } mode= TOGGLE;
int silent = 0;
_PROTOTYPE(void (*hsig), (int));
_PROTOTYPE(void (*isig), (int));
_PROTOTYPE(void (*qsig), (int));
_PROTOTYPE(void (*tsig), (int));
pid_t pid;
int r, status;
uid_t uid = getuid();
gid_t gid = getgid();
char *in_codes, *out_codes;
i = 1;
while (i < argc && argv[i][0] == '-') {
char *opt = argv[i++] + 1;
if (opt[0] == '-' && opt[1] == 0) break;
while (*opt != 0) {
switch (*opt++) {
case 's': /* silent mode */
silent = 1;
break;
case 'i': /* DIAL-IN mode: suspend GETTY */
mode = DIALIN;
break;
case 'o': /* DIAL-OUT mode: restart GETTY */
mode = DIALOUT;
break;
case 'I': /* code to switch modem to dial-in */
if (*opt == 0) {
if (i == argc) usage();
opt = argv[i++];
}
in_codes = opt;
opt = "";
break;
case 'O': /* code to switch modem to dial-out */
if (*opt == 0) {
if (i == argc) usage();
opt = argv[i++];
}
out_codes = opt;
opt = "";
break;
default:
usage();
}
}
}
if (i == argc) usage();
tty = argv[i++]; /* Modem line */
if (mode != TOGGLE && i != argc) usage();
command = argv + i; /* Command to execute (if any). */
if (strchr(tty, '/') == NULL) {
strcpy(devtty, "/dev/");
strncat(devtty, tty, 1024 - 6);
tty = devtty;
}
if (stat(tty, &st) < 0) {
fprintf(stderr, "modem: %s: %s\n", tty, strerror(errno));
exit(1);
}
if (!S_ISCHR(st.st_mode)) {
fprintf(stderr, "%s is not a tty\n", tty);
exit(1);
}
/* Find the utmp slot number for the line. */
if ((fd= open(tty, O_RDONLY)) < 0 || (slot= fttyslot(fd)) == 0) {
fprintf(stderr, "modem: %s: %s\n", tty, strerror(errno));
exit(1);
}
close(fd);
/* Read the UTMP file to find out the PID and STATUS of the GETTY. */
entry.ut_type= 0;
if ((fd = open(PATH_UTMP, O_RDONLY)) < 0
|| lseek(fd, (off_t) slot * sizeof(entry), SEEK_SET) < 0
|| read(fd, &entry, sizeof(entry)) < 0
) {
fprintf(stderr, "modem: cannot read UTMP !\n");
exit(1);
}
close(fd);
hsig= signal(SIGHUP, SIG_IGN);
isig= signal(SIGINT, SIG_IGN);
qsig= signal(SIGQUIT, SIG_IGN);
tsig= signal(SIGTERM, SIG_IGN);
/* Process the terminal entry if we got one. */
switch (entry.ut_type) {
case LOGIN_PROCESS: /* getty waiting for a call */
getty = 1;
break;
case USER_PROCESS: /* login or user-shell */
if (!silent) fprintf(stderr, "modem: line is busy.\n");
exit(2);
break;
default:
getty = 0;
}
for (i = (mode == TOGGLE) ? 0 : 1; i < 2; i++) {
/* Now perform the desired action (DIALIN or DIALOUT). */
switch (mode) {
case DIALOUT:
case TOGGLE:
if (getty) kill(entry.ut_pid, SIGUSR1); /* suspend getty */
chown(tty, uid, st.st_gid); /* give line to user */
chmod(tty, 0600);
if (out_codes != NULL) sendcodes(tty, out_codes);
if (!silent) printf("modem on %s set for dialout.\n", tty);
break;
case DIALIN:
if (in_codes != NULL) sendcodes(tty, in_codes);
chown(tty, 0, st.st_gid); /* revoke access */
chmod(tty, 0600);
if (getty) kill(entry.ut_pid, SIGUSR2); /* restart getty */
if (!silent) printf("modem on %s set for dialin.\n", tty);
}
if (mode == TOGGLE) {
/* Start the command to run */
pid_t pid;
int status;
switch ((pid = fork())) {
case -1:
fprintf(stderr, "modem: fork(): %s\n", strerror(errno));
ex_code= 1;
break;
case 0:
setgid(gid);
setuid(uid);
(void) signal(SIGHUP, hsig);
(void) signal(SIGINT, isig);
(void) signal(SIGQUIT, qsig);
(void) signal(SIGTERM, tsig);
execvp(command[0], command);
fprintf(stderr, "modem: %s: %s\n",
command[0], strerror(errno));
_exit(127);
default:
while ((r= wait(&status)) != pid) {
if (r == -1 && errno != EINTR) break;
}
if (r == -1 || status != 0) ex_code = 1;
}
mode = DIALIN;
}
}
exit(ex_code);
}
void sendcodes(tty, codes)
char *tty, *codes;
{
int fd;
int c;
char buf[1024], *bp = buf;
if ((fd = open(tty, O_RDWR|O_NONBLOCK)) < 0) {
fprintf(stderr, "modem: can't send codes to %s: %s\n",
tty, strerror(errno));
return;
}
while ((c = *codes++) != 0) {
fprintf(stderr, "%d\n", __LINE__);
if (c == '\\') {
if ((c = *codes++) == 0) break;
if (c == 'r') c= '\r';
if (c == 'n') c= '\n';
}
*bp++ = c;
if (bp == buf + sizeof(buf) || c == '\r' || c == '\n') {
fprintf(stderr, "%d\n", __LINE__);
write(fd, buf, bp - buf);
fprintf(stderr, "%d\n", __LINE__);
do {sleep(1);
fprintf(stderr, "%d\n", __LINE__);
fprintf(stderr, "%d\n", read(fd, buf, sizeof(buf)));
}while (read(fd, buf, sizeof(buf)) > 0);
fprintf(stderr, "%d\n", __LINE__);
bp = buf;
}
}
if (bp > buf) {
fprintf(stderr, "%d\n", __LINE__);
write(fd, buf, bp - buf);
fprintf(stderr, "%d\n", __LINE__);
do sleep(1); while (read(fd, buf, sizeof(buf)) > 0);
fprintf(stderr, "%d\n", __LINE__);
}
close(fd);
}