File: modem.c

package info (click to toggle)
diald 0.16.5-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 716 kB
  • ctags: 805
  • sloc: ansic: 5,438; tcl: 510; perl: 479; sh: 284; makefile: 166
file content (459 lines) | stat: -rw-r--r-- 11,034 bytes parent folder | download | duplicates (2)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * modem.c - Modem control functions.
 *
 * Copyright (c) 1994, 1995, 1996 Eric Schenk.
 * All rights reserved. Please see the file LICENSE which should be
 * distributed with this software for terms of use.
 *
 * Portions of this code were derived from the code for pppd copyright
 * (c) 1989 Carnegie Mellon University. The copyright notice on this code
 * is reproduced below.
 *
 * Copyright (c) 1989 Carnegie Mellon University.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by Carnegie Mellon University.  The name of the
 * University may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include "diald.h"

static char *current_dev = 0;
static int rotate_offset = 0;

/* local variables */

static struct termios inittermios;      /* Initial TTY termios */
static int restore_term = 0;
int pgrpid;

#if B9600 == 9600
/*
 * XXX assume speed_t values numerically equal bits per second
 * (so we can ask for any speed).
 */
#define translate_speed(bps)	(bps)

#else
/*
 * List of valid speeds.
 */
struct speed {
    int speed_int, speed_val;
} speeds[] = {
#ifdef B50
    { 50, B50 },
#endif
#ifdef B75
    { 75, B75 },
#endif
#ifdef B110
    { 110, B110 },
#endif
#ifdef B134
    { 134, B134 },
#endif
#ifdef B150
    { 150, B150 },
#endif
#ifdef B200
    { 200, B200 },
#endif
#ifdef B300
    { 300, B300 },
#endif
#ifdef B600
    { 600, B600 },
#endif
#ifdef B1200
    { 1200, B1200 },
#endif
#ifdef B1800
    { 1800, B1800 },
#endif
#ifdef B2000
    { 2000, B2000 },
#endif
#ifdef B2400
    { 2400, B2400 },
#endif
#ifdef B3600
    { 3600, B3600 },
#endif
#ifdef B4800
    { 4800, B4800 },
#endif
#ifdef B7200
    { 7200, B7200 },
#endif
#ifdef B9600
    { 9600, B9600 },
#endif
#ifdef B19200
    { 19200, B19200 },
#endif
#ifdef B38400
    { 38400, B38400 },
#endif
#ifdef EXTA
    { 19200, EXTA },
#endif
#ifdef EXTB
    { 38400, EXTB },
#endif
#ifdef B57600
    { 57600, B57600 },
#endif
#ifdef B115200
    { 115200, B115200 },
#endif
#ifdef B230400
    { 230400, B230400 },
#endif
#ifdef B460800
    { 460800, B460800 },
#endif
    { 0, 0 }
};

/*
 * Translate from bits/second to a speed_t.
 */
int translate_speed(int bps)
{
    struct speed *speedp;

    if (bps == 0)
	return 0;
    for (speedp = speeds; speedp->speed_int; speedp++)
	if (bps == speedp->speed_int)
	    return speedp->speed_val;
    syslog(LOG_WARNING, "speed %d not supported", bps);
    return 0;
}
#endif

/*
 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
 * at the requested speed, etc.  If `local' is true, set CLOCAL
 * regardless of whether the modem option was specified.
 */
void set_up_tty(int fd, int local, int spd)
{
    int speed, i;
    struct termios tios;

    if (tcgetattr(fd, &tios) < 0) {
	syslog(LOG_ERR, "could not get initial terminal attributes: %m");
    }

    tios.c_cflag = CS8 | CREAD | HUPCL;
    if (local || !modem) tios.c_cflag |= CLOCAL;
    if (crtscts == 1) tios.c_cflag |= CRTSCTS;
    tios.c_iflag = IGNBRK | IGNPAR;
    tios.c_oflag = 0;
    tios.c_lflag = 0;
    for (i = 0; i < NCCS; i++)
	tios.c_cc[i] = 0;
    tios.c_cc[VMIN] = 1;
    tios.c_cc[VTIME] = 0;

    speed = translate_speed(spd);
    if (speed) {
	cfsetospeed(&tios, speed);
	cfsetispeed(&tios, speed);
    } else {
	speed = cfgetospeed(&tios);
    }

    if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
	syslog(LOG_ERR, "failed to set terminal attributes: %m");
    }
}

/*
 * setdtr - control the DTR line on the serial port.
 * This is called from die(), so it shouldn't call die().
 */
void setdtr(int fd, int on)
{
    int modembits = TIOCM_DTR;

    ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
}


/*
 * fork_dialer - run a program to connect the serial device.
 */
void fork_dialer(char *program, int fd)
{
    block_signals();

    dial_pid = fork();

    if (dial_pid < 0) {
	unblock_signals();
        syslog(LOG_ERR, "failed to fork dialer: %m");
	/* FIXME: Probably this should not be fatal */
        die(1);
    }

    if (dial_pid == 0) {
        /* change the signal actions back to the defaults, then unblock them. */
        default_sigacts();
	unblock_signals();

        /* make sure the child doesn't inherit any extra file descriptors */
	close(proxy_mfd);      /* close the master pty endpoint */
	close(proxy_sfd);      /* close the slave pty endpoint */
	if (fifo_fd != -1) close(fifo_fd);
	if (monitors) {
	    MONITORS *c = monitors;
	    while (c) {
	    	close(c->fd);
		c = c->next;
	    }
	}

	/* make sure the stdin and stdout get directed to the modem */
        if (fd != 0) { dup2(fd, 0); close(fd); }
        dup2(0, 1);
	/* FIXME: direct the stderr to the console? */
        dup2(0, 2);

	setenv("MODEM",current_dev,1);	/* set the current device */
	if (fifoname)		/* set the current command FIFO (if any) */
	    setenv("FIFO",fifoname,1);
        execl("/bin/sh", "sh", "-c", program, (char *)0);
        syslog(LOG_ERR, "could not exec /bin/sh: %m");
        _exit(127);
        /* NOTREACHED */
    }

    unblock_signals();
    syslog(LOG_INFO,"Running connect (pid = %d).",dial_pid);
}

/*
 * Open up a modem and set up the desired parameters.
 */
int open_modem()
{
    int npgrpid;
    int i;
    /*
     * Open the serial device and set it up.
     */

    modem_hup = 0;
    modem_fd = -1;
    dial_status = 0;

    /* JPD changed mode_dev so that we can use a connect script to get */
    /* ipppd to dial. This allows us to use a connector program to get */
    /* ipppd to dial and make sure that ipppd successfully connects to */
    /* the ISP. Otherwise, we can't be sure that ipppd successfully */
    /* connected and diald would show that we we're connected */
    if (mode == MODE_DEV) 
    {
        fork_connect(connector);
        return 0;
    }

    if (req_pid) {
	/* The user has specified a device. Use it, no search or lock needed. */
	if ((modem_fd = open(req_dev, O_RDWR | O_NDELAY)) < 0) {
	    syslog(LOG_ERR,"Can't open requested device '%s'",req_dev);
	    killpg(req_pid,SIGKILL);
	    kill(req_pid,SIGKILL);
	    req_pid = 0;
	    dial_status = -1;
	    return 1;
	}
	current_dev = req_dev;
	use_req=1;
    } else {
	for (i = 0; i < device_count; i++) {
	    current_dev = devices[(i+rotate_offset)%device_count];
	    /*
	     * March down the device list till we manage to open one up or
	     * we run out of devices to try.
	     */

	    if (lock_dev && lock(current_dev) < 0)
		continue;

	    /* OK. Locked one, try to open it */
	    if ((modem_fd = open(current_dev, O_RDWR | O_NDELAY)) >= 0)
		break;
	    else {
	       syslog(LOG_ERR,"Error opening device %s: %m",current_dev);
	    }
	    current_dev = 0;

	    /* That didn't work, get rid of the lock */
	    if (lock_dev) unlock();
	}
	if (modem_fd < 0) {
	    syslog(LOG_INFO,"Couldn't find a free device to call out on.");
	    dial_status = -1;
	    return 2;
	}
    }

    if (rotate_devices)
    	rotate_offset = (rotate_offset+1)%device_count;

    /* Make sure we are the session leader */
    if ((npgrpid = setsid()) >= 0)
	pgrpid = npgrpid;

    /* set device to be controlling tty */
    /* This should only happen in SLIP mode */
    if (mode == MODE_SLIP) {
	if (ioctl(modem_fd, TIOCSCTTY, 1) < 0) {
	    syslog(LOG_ERR, "failed to set modem to controlling tty: %m");
	    die(1);
	}

        if (tcsetpgrp(modem_fd, pgrpid) < 0) {
	    syslog(LOG_ERR, "failed to set process group: %m");
	    die(1);
	}
    }

    /* Get rid of any initial line noise */
    tcflush(modem_fd, TCIOFLUSH);

    if (tcgetattr(modem_fd, &inittermios) < 0) {
	syslog(LOG_ERR, "failed to get initial modem terminal attributes: %m");
    }

    /* So we don't try to restore if we die before this */
    restore_term = 1;

    /* Clear the NDELAY flag now */
    if (fcntl(modem_fd,F_SETFL,fcntl(modem_fd,F_GETFL)&~(O_NDELAY)) < 0)
	syslog(LOG_ERR, "failed to clear O_NDELAY flag: %m"), die(1);

    if (!req_pid) {
	/* hang up and then start again */
	set_up_tty(modem_fd, 1, 0);
	sleep(1);
	set_up_tty(modem_fd, 1, inspeed);

	/* Get rid of any initial line noise after the hangup */
	tcflush(modem_fd, TCIOFLUSH);
	fork_dialer(connector, modem_fd);
    } else {
	/* someone else opened the line, we just set the mode */
	set_up_tty(modem_fd, 0, inspeed);
    }
    return 0;
}

/*
 * Reopen up a modem that closed on a sighup and set up the desired parameters.
 */
void reopen_modem()
{
    int npgrpid;

    if(debug&DEBUG_VERBOSE)
	syslog(LOG_INFO,"Reopening modem device");

    close(modem_fd);
    sleep(1);
    if ((modem_fd = open(current_dev, O_RDWR | O_NDELAY)) < 0) {
	syslog(LOG_ERR,"Can't reopen device '%s'",current_dev);
    } else {
	/* Make sure we are the session leader */
	if ((npgrpid = setsid()) >= 0)
	    pgrpid = npgrpid;

	/* set device to be controlling tty */
	/* This should only happen in SLIP mode */
	if (mode == MODE_SLIP) {
	    if (ioctl(modem_fd, TIOCSCTTY, 1) < 0) {
		syslog(LOG_ERR, "failed to set modem to controlling tty: %m");
		die(1);
	    }

	    if (tcsetpgrp(modem_fd, pgrpid) < 0) {
		syslog(LOG_ERR, "failed to set process group: %m");
		die(1);
	    }
	}

	set_up_tty(modem_fd, 1, inspeed);
	/* Clear the NDELAY flag now */
	if (fcntl(modem_fd,F_SETFL,fcntl(modem_fd,F_GETFL)&~(O_NDELAY)) < 0)
	    syslog(LOG_ERR, "failed to clear O_NDELAY flag: %m"), die(1);
    }
}

void finish_dial()
{
    if (!req_pid)
        set_up_tty(modem_fd, 0, inspeed);
}

/*
 * Close the modem, making sure it hangs up properly!
 */
void close_modem()
{
    if (mode == MODE_DEV) {
	req_pid = 0;
	modem_fd = -1 ;
	return ;
    }

    if (debug&DEBUG_VERBOSE)
        syslog(LOG_INFO,"Closing modem line.");

    if (modem_fd < 0) {
 	return;
    }

    /* Get rid of what ever might be waiting to go out still */
    tcflush(modem_fd, TCIOFLUSH);

    /*
     * Restore the initial termio settings.
     */

    if (restore_term) {
	tcsetattr(modem_fd, TCSANOW, &inittermios);
    }

    /*
     * Hang up the modem up by dropping the DTR.
     * We do this because the initial termio settings
     * may not have set HUPCL. This forces the issue.
     * We need the sleep to give the modem a chance to hang
     * up before we get another program asserting the DTR.
     */
    setdtr(modem_fd, 0);
    sleep(1);

    close(modem_fd);
    if (use_req) {
	if (debug&DEBUG_VERBOSE)
	    syslog(LOG_INFO, "Killing requesting shell pid %d",req_pid);
	killpg(req_pid, SIGKILL);
	kill(req_pid, SIGKILL);
	req_pid = 0;
    } else if (lock_dev) unlock();
    modem_fd = -1;
}