2 * Copyright (c) 1990, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1990, 1991, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)startslip.c 8.1 (Berkeley) 6/5/93
35 * $FreeBSD: src/sbin/startslip/startslip.c,v 1.31.2.1 2000/05/07 18:26:51 joe Exp $
36 * $DragonFly: src/sbin/startslip/startslip.c,v 1.3 2003/09/28 14:39:22 hmp Exp $
39 #include <sys/types.h>
57 #define DEFAULT_BAUD B9600
58 int speed = DEFAULT_BAUD;
59 #define FC_NONE 0 /* flow control: none */
60 #define FC_HW 1 /* flow control: hardware (RTS/CTS) */
61 int flowcontrol = FC_NONE;
62 int modem_control = 1; /* !CLOCAL+HUPCL iff we watch carrier. */
64 int uucp_lock = 0; /* uucp locking */
69 int locked = 0; /* uucp lock active */
71 int wait_time = 60; /* then back off */
72 int script_timeout = 90; /* connect script default timeout */
73 time_t conn_time, start_time;
74 int MAXTRIES = 6; /* w/60 sec and doubling, takes an hour */
75 #define PIDFILE "%sstartslip.%s.pid"
78 char *dials[MAXDIALS];
83 char *dvname, *devicename;
90 #define syslog fprintf
91 #define LOG_ERR stderr
92 #define LOG_INFO stderr
96 #define printd if (debug) printf
98 int carrier __P((void));
100 int getline __P((char *, int, int, time_t));
101 static void usage __P((void));
104 main(int argc, char **argv)
108 void sighup(), sigterm(), sigurg();
110 char *dialerstring = 0, buf[BUFSIZ];
111 int unitnum, keepal = 0, outfill = 0;
114 char *upscript = NULL, *downscript = NULL;
115 int first = 1, tries = 0;
121 while ((ch = getopt(argc, argv, "dhlb:s:t:w:A:U:D:W:K:O:S:L")) != -1)
127 speed = atoi(optarg);
130 if (diali >= MAXDIALS)
131 errx(1, "max dial strings number (%d) exceeded", MAXDIALS);
132 dials[diali++] = strdup(optarg);
135 script_timeout = atoi(optarg);
138 wait_time = atoi(optarg);
141 MAXTRIES = atoi(optarg);
144 annex = strdup(optarg);
147 upscript = strdup(optarg);
150 downscript = strdup(optarg);
162 keepal = atoi(optarg);
165 outfill = atoi(optarg);
168 sl_unit = atoi(optarg);
181 * Copy these so they exist after we clobber them.
183 devicename = strdup(argv[0]);
184 username = strdup(argv[1]);
185 password = strdup(argv[2]);
188 * Security hack. Do not want private information such as the
189 * password and possible phone number to be left around.
190 * So we clobber the arguments.
192 for (ap = argv - optind + 1; ap < argv + 3; ap++)
193 for (cp = *ap; *cp != 0; cp++)
196 openlog("startslip", LOG_PID|LOG_PERROR, LOG_DAEMON);
199 setbuf(stdout, NULL);
201 signal(SIGTERM, sigterm);
202 if ((dvname = strrchr(devicename, '/')) == NULL)
206 if (snprintf(pidfile, sizeof(pidfile), PIDFILE, _PATH_VARRUN, dvname) >= sizeof(pidfile))
209 if ((pfd = fopen(pidfile, "r")) != NULL) {
210 if (fscanf(pfd, "%ld\n", &lpid) == 1) {
212 if (pid == lpid && pid > 0)
216 pfd = NULL; /* not remove pidfile yet */
217 sleep(5); /* allow down script to be completed */
220 signal(SIGHUP, SIG_IGN);
221 signal(SIGURG, SIG_IGN);
226 conn_time = time(NULL) - start_time;
233 } else if (fd >= 0) {
236 conn_time = time(NULL) - start_time;
244 syslog(LOG_INFO, "%s: connection time elapsed: %ld secs",
245 username, (long)conn_time);
246 sprintf(buf, "LINE=%d %s %s down",
247 diali ? (dialc - 1) % diali : 0,
248 downscript ? downscript : "/sbin/ifconfig" , unitname);
255 if (MAXTRIES > 0 && tries > MAXTRIES) {
256 syslog(LOG_ERR, "%s: exiting login after %d tries", username, tries);
263 syslog(LOG_INFO, "%s: sleeping %d seconds (%d tries)",
264 username, wait_time * (tries - 1), tries);
265 sleep(wait_time * (tries - 1));
270 if (daemon(1, debug) < 0) {
271 syslog(LOG_ERR, "%s: daemon: %m", username);
276 printd("restart: pid %ld: ", (long)pid);
277 if ((pfd = fopen(pidfile, "w")) != NULL) {
278 fprintf(pfd, "%ld\n", (long)pid);
284 if ((res = uu_lock(dvname)) != UU_LOCK_OK) {
285 if (res != UU_LOCK_INUSE)
286 syslog(LOG_ERR, "uu_lock: %s", uu_lockerr(res));
287 syslog(LOG_ERR, "%s: can't lock %s", username, devicename);
292 if ((fd = open(devicename, O_RDWR | O_NONBLOCK)) < 0) {
293 syslog(LOG_ERR, "%s: open %s: %m", username, devicename);
304 signal(SIGHUP, sighup);
305 if (ioctl(fd, TIOCSCTTY, 0) < 0) {
306 syslog(LOG_ERR, "%s: ioctl (TIOCSCTTY): %m", username);
309 if (tcsetpgrp(fd, getpid()) < 0) {
310 syslog(LOG_ERR, "%s: tcsetpgrp failed: %m", username);
314 if (tcgetattr(fd, &t) < 0) {
315 syslog(LOG_ERR, "%s: tcgetattr(%s): %m", username, devicename);
319 switch (flowcontrol) {
321 t.c_cflag |= (CRTS_IFLOW|CCTS_OFLOW);
324 t.c_cflag &= ~(CRTS_IFLOW|CCTS_OFLOW);
330 t.c_cflag &= ~(HUPCL);
331 t.c_cflag |= CLOCAL; /* until modem commands passes */
332 cfsetispeed(&t, speed);
333 cfsetospeed(&t, speed);
334 if (tcsetattr(fd, TCSAFLUSH, &t) < 0) {
335 syslog(LOG_ERR, "%s: tcsetattr(%s): %m", username, devicename);
338 sleep(2); /* wait for flakey line to settle */
339 if (hup || terminate)
342 wfd = fdopen(fd, "w+");
344 syslog(LOG_ERR, "%s: can't fdopen %s: %m", username, devicename);
350 dialerstring = dials[dialc++ % diali];
352 syslog(LOG_INFO, "%s: dialer string: %s\\r", username, dialerstring);
353 fprintf(wfd, "%s\r", dialerstring);
357 fintimeout = time(NULL) + script_timeout;
359 printd("waiting for carrier\n");
360 while (time(NULL) < fintimeout && !carrier()) {
362 if (hup || terminate)
367 t.c_cflag &= ~(CLOCAL);
368 if (tcsetattr(fd, TCSANOW, &t) < 0) {
369 syslog(LOG_ERR, "%s: tcsetattr(%s): %m", username, devicename);
372 /* Only now we able to receive HUP on carrier drop! */
378 printd("look for login: ");
380 if (getline(buf, BUFSIZ, fd, fintimeout) == 0 || hup || terminate)
383 if (bcmp(buf, annex, strlen(annex)) == 0) {
384 fprintf(wfd, "slip\r");
385 printd("Sent \"slip\"\n");
388 if (bcmp(&buf[1], "sername:", 8) == 0) {
389 fprintf(wfd, "%s\r", username);
390 printd("Sent login: %s\n", username);
393 if (bcmp(&buf[1], "assword:", 8) == 0) {
394 fprintf(wfd, "%s\r", password);
395 printd("Sent password: %s\n", password);
399 if (strstr(&buf[1], "ogin:") != NULL) {
400 fprintf(wfd, "%s\r", username);
401 printd("Sent login: %s\n", username);
404 if (strstr(&buf[1], "assword:") != NULL) {
405 fprintf(wfd, "%s\r", password);
406 printd("Sent password: %s\n", password);
412 sleep(5); /* Wait until login completed */
413 if (hup || terminate)
415 start_time = time(NULL);
421 if (ioctl(fd, TIOCSETD, &disc) < 0) {
422 syslog(LOG_ERR, "%s: ioctl (%s, TIOCSETD): %m",
423 username, devicename);
426 if (sl_unit >= 0 && ioctl(fd, SLIOCSUNIT, &sl_unit) < 0) {
427 syslog(LOG_ERR, "%s: ioctl(SLIOCSUNIT): %m", username);
430 if (ioctl(fd, SLIOCGUNIT, &unitnum) < 0) {
431 syslog(LOG_ERR, "%s: ioctl(SLIOCGUNIT): %m", username);
434 sprintf(unitname, "sl%d", unitnum);
437 signal(SIGURG, sigurg);
438 if (ioctl(fd, SLIOCSKEEPAL, &keepal) < 0) {
439 syslog(LOG_ERR, "%s: ioctl(SLIOCSKEEPAL): %m", username);
443 if (outfill > 0 && ioctl(fd, SLIOCSOUTFILL, &outfill) < 0) {
444 syslog(LOG_ERR, "%s: ioctl(SLIOCSOUTFILL): %m", username);
448 sprintf(buf, "LINE=%d %s %s up",
449 diali ? (dialc - 1) % diali : 0,
450 upscript ? upscript : "/sbin/ifconfig" , unitname);
455 syslog(LOG_INFO, "%s: reconnected on %s (%d tries)", username, unitname, tries);
457 syslog(LOG_INFO, "%s: connected on %s", username, unitname);
461 while (hup == 0 && terminate == 0) {
463 printd("sigpause return\n");
466 return(0); /* not reached */
474 if (hup == 0 && logged_in)
475 syslog(LOG_INFO, "%s: got hangup signal", username);
484 if (hup == 0 && logged_in)
485 syslog(LOG_INFO, "%s: got dead line signal", username);
493 printd("terminate\n");
494 if (terminate == 0 && logged_in)
495 syslog(LOG_INFO, "%s: got terminate signal", username);
500 getline(char *buf, int size, int fd, time_t fintimeout)
509 for (i = 0; i < size; i++) {
510 if (hup || terminate)
512 if ((timeout = fintimeout - time(NULL)) <= 0)
515 FD_SET(fd, &readfds);
518 if ((ret = select(fd + 1, &readfds, NULL, NULL, &tv)) < 0) {
520 syslog(LOG_ERR, "%s: getline: select: %m", username);
524 printd("getline: timed out\n");
527 if ((ret = read(fd, &buf[i], 1)) == 1) {
529 if (buf[i] == '\r' || buf[i] == '\0') {
533 if (buf[i] != '\n' && buf[i] != ':')
536 printd("Got %d: %s", i + 1, buf);
541 syslog(LOG_ERR, "%s: getline: read: %m", username);
543 syslog(LOG_ERR, "%s: read returned 0", username);
545 printd("returning %d after %d: %s\n", ret, i, buf);
558 if (ioctl(fd, TIOCMGET, &comstate) < 0) {
559 syslog(LOG_ERR, "%s: ioctl (%s, TIOCMGET): %m",
560 username, devicename);
563 return !!(comstate & TIOCM_CD);
573 if (uucp_lock && locked)
581 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
582 "usage: startslip [-d] [-b speed] [-s string1 [-s string2 [...]]] [-h] [-l]",
583 " [-L] [-A annexname] [-U upscript] [-D downscript]",
584 " [-t script_timeout] [-W maxtries] [-w retry_pause]",
585 " [-K keepalive] [-O outfill] [-S unit] device user passwd");