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 $
38 #include <sys/types.h>
56 #define DEFAULT_BAUD B9600
57 int speed = DEFAULT_BAUD;
58 #define FC_NONE 0 /* flow control: none */
59 #define FC_HW 1 /* flow control: hardware (RTS/CTS) */
60 int flowcontrol = FC_NONE;
61 int modem_control = 1; /* !CLOCAL+HUPCL iff we watch carrier. */
63 int uucp_lock = 0; /* uucp locking */
68 int locked = 0; /* uucp lock active */
70 int wait_time = 60; /* then back off */
71 int script_timeout = 90; /* connect script default timeout */
72 time_t conn_time, start_time;
73 int MAXTRIES = 6; /* w/60 sec and doubling, takes an hour */
74 #define PIDFILE "%sstartslip.%s.pid"
77 char *dials[MAXDIALS];
82 char *dvname, *devicename;
89 #define syslog fprintf
90 #define LOG_ERR stderr
91 #define LOG_INFO stderr
95 #define printd if (debug) printf
97 static int carrier(void);
98 static void down(int);
99 static int getline(char *, int, int, time_t);
100 static void usage(void);
101 static void sighup(int);
102 static void sigterm(int);
103 static void sigurg(int);
106 main(int argc, char **argv)
111 char *dialerstring = NULL, buf[BUFSIZ];
112 int unitnum, keepal = 0, outfill = 0;
115 char *upscript = NULL, *downscript = NULL;
116 int first = 1, tries = 0;
123 while ((ch = getopt(argc, argv, "dhlb:s:t:w:A:U:D:W:K:O:S:L")) != -1)
129 speed = atoi(optarg);
132 if (diali >= MAXDIALS)
133 errx(1, "max dial strings number (%d) exceeded", MAXDIALS);
134 dials[diali++] = strdup(optarg);
137 script_timeout = atoi(optarg);
140 wait_time = atoi(optarg);
143 MAXTRIES = atoi(optarg);
146 annex = strdup(optarg);
149 upscript = strdup(optarg);
152 downscript = strdup(optarg);
164 keepal = atoi(optarg);
167 outfill = atoi(optarg);
170 sl_unit = atoi(optarg);
183 * Copy these so they exist after we clobber them.
185 devicename = strdup(argv[0]);
186 username = strdup(argv[1]);
187 password = strdup(argv[2]);
190 * Security hack. Do not want private information such as the
191 * password and possible phone number to be left around.
192 * So we clobber the arguments.
194 for (ap = argv - optind + 1; ap < argv + 3; ap++)
195 for (cp = *ap; *cp != 0; cp++)
198 openlog("startslip", LOG_PID|LOG_PERROR, LOG_DAEMON);
201 setbuf(stdout, NULL);
203 signal(SIGTERM, sigterm);
204 if ((dvname = strrchr(devicename, '/')) == NULL)
209 result = snprintf(my_pidfile, sizeof(my_pidfile),
210 PIDFILE, _PATH_VARRUN, dvname);
211 if (result < 0 || (unsigned int)result >= sizeof(my_pidfile))
214 if ((pfd = fopen(my_pidfile, "r")) != NULL) {
215 if (fscanf(pfd, "%ld\n", &lpid) == 1) {
217 if (pid == lpid && pid > 0)
221 pfd = NULL; /* not remove pidfile yet */
222 sleep(5); /* allow down script to be completed */
225 signal(SIGHUP, SIG_IGN);
226 signal(SIGURG, SIG_IGN);
231 conn_time = time(NULL) - start_time;
238 } else if (fd >= 0) {
241 conn_time = time(NULL) - start_time;
249 syslog(LOG_INFO, "%s: connection time elapsed: %ld secs",
250 username, (long)conn_time);
251 sprintf(buf, "LINE=%d %s %s down",
252 diali ? (dialc - 1) % diali : 0,
253 downscript ? downscript : "/sbin/ifconfig" , unitname);
260 if (MAXTRIES > 0 && tries > MAXTRIES) {
261 syslog(LOG_ERR, "%s: exiting login after %d tries", username, tries);
268 syslog(LOG_INFO, "%s: sleeping %d seconds (%d tries)",
269 username, wait_time * (tries - 1), tries);
270 sleep(wait_time * (tries - 1));
275 if (daemon(1, debug) < 0) {
276 syslog(LOG_ERR, "%s: daemon: %m", username);
281 printd("restart: pid %ld: ", (long)pid);
282 if ((pfd = fopen(my_pidfile, "w")) != NULL) {
283 fprintf(pfd, "%ld\n", (long)pid);
289 if ((res = uu_lock(dvname)) != UU_LOCK_OK) {
290 if (res != UU_LOCK_INUSE)
291 syslog(LOG_ERR, "uu_lock: %s", uu_lockerr(res));
292 syslog(LOG_ERR, "%s: can't lock %s", username, devicename);
297 if ((fd = open(devicename, O_RDWR | O_NONBLOCK)) < 0) {
298 syslog(LOG_ERR, "%s: open %s: %m", username, devicename);
309 signal(SIGHUP, sighup);
310 if (ioctl(fd, TIOCSCTTY, 0) < 0) {
311 syslog(LOG_ERR, "%s: ioctl (TIOCSCTTY): %m", username);
314 if (tcsetpgrp(fd, getpid()) < 0) {
315 syslog(LOG_ERR, "%s: tcsetpgrp failed: %m", username);
319 if (tcgetattr(fd, &t) < 0) {
320 syslog(LOG_ERR, "%s: tcgetattr(%s): %m", username, devicename);
324 switch (flowcontrol) {
326 t.c_cflag |= (CRTS_IFLOW|CCTS_OFLOW);
329 t.c_cflag &= ~(CRTS_IFLOW|CCTS_OFLOW);
335 t.c_cflag &= ~(HUPCL);
336 t.c_cflag |= CLOCAL; /* until modem commands passes */
337 cfsetispeed(&t, speed);
338 cfsetospeed(&t, speed);
339 if (tcsetattr(fd, TCSAFLUSH, &t) < 0) {
340 syslog(LOG_ERR, "%s: tcsetattr(%s): %m", username, devicename);
343 sleep(2); /* wait for flakey line to settle */
344 if (hup || terminate)
347 wfd = fdopen(fd, "w+");
349 syslog(LOG_ERR, "%s: can't fdopen %s: %m", username, devicename);
355 dialerstring = dials[dialc++ % diali];
357 syslog(LOG_INFO, "%s: dialer string: %s\\r", username, dialerstring);
358 fprintf(wfd, "%s\r", dialerstring);
362 fintimeout = time(NULL) + script_timeout;
364 printd("waiting for carrier\n");
365 while (time(NULL) < fintimeout && !carrier()) {
367 if (hup || terminate)
372 t.c_cflag &= ~(CLOCAL);
373 if (tcsetattr(fd, TCSANOW, &t) < 0) {
374 syslog(LOG_ERR, "%s: tcsetattr(%s): %m", username, devicename);
377 /* Only now we able to receive HUP on carrier drop! */
383 printd("look for login: ");
385 if (getline(buf, BUFSIZ, fd, fintimeout) == 0 || hup || terminate)
388 if (bcmp(buf, annex, strlen(annex)) == 0) {
389 fprintf(wfd, "slip\r");
390 printd("Sent \"slip\"\n");
393 if (bcmp(&buf[1], "sername:", 8) == 0) {
394 fprintf(wfd, "%s\r", username);
395 printd("Sent login: %s\n", username);
398 if (bcmp(&buf[1], "assword:", 8) == 0) {
399 fprintf(wfd, "%s\r", password);
400 printd("Sent password: %s\n", password);
404 if (strstr(&buf[1], "ogin:") != NULL) {
405 fprintf(wfd, "%s\r", username);
406 printd("Sent login: %s\n", username);
409 if (strstr(&buf[1], "assword:") != NULL) {
410 fprintf(wfd, "%s\r", password);
411 printd("Sent password: %s\n", password);
417 sleep(5); /* Wait until login completed */
418 if (hup || terminate)
420 start_time = time(NULL);
426 if (ioctl(fd, TIOCSETD, &disc) < 0) {
427 syslog(LOG_ERR, "%s: ioctl (%s, TIOCSETD): %m",
428 username, devicename);
431 if (sl_unit >= 0 && ioctl(fd, SLIOCSUNIT, &sl_unit) < 0) {
432 syslog(LOG_ERR, "%s: ioctl(SLIOCSUNIT): %m", username);
435 if (ioctl(fd, SLIOCGUNIT, &unitnum) < 0) {
436 syslog(LOG_ERR, "%s: ioctl(SLIOCGUNIT): %m", username);
439 sprintf(unitname, "sl%d", unitnum);
442 signal(SIGURG, sigurg);
443 if (ioctl(fd, SLIOCSKEEPAL, &keepal) < 0) {
444 syslog(LOG_ERR, "%s: ioctl(SLIOCSKEEPAL): %m", username);
448 if (outfill > 0 && ioctl(fd, SLIOCSOUTFILL, &outfill) < 0) {
449 syslog(LOG_ERR, "%s: ioctl(SLIOCSOUTFILL): %m", username);
453 sprintf(buf, "LINE=%d %s %s up",
454 diali ? (dialc - 1) % diali : 0,
455 upscript ? upscript : "/sbin/ifconfig" , unitname);
460 syslog(LOG_INFO, "%s: reconnected on %s (%d tries)", username, unitname, tries);
462 syslog(LOG_INFO, "%s: connected on %s", username, unitname);
466 while (hup == 0 && terminate == 0) {
468 printd("sigpause return\n");
471 return(0); /* not reached */
475 sighup(int signo __unused)
478 if (hup == 0 && logged_in)
479 syslog(LOG_INFO, "%s: got hangup signal", username);
484 sigurg(int signo __unused)
487 if (hup == 0 && logged_in)
488 syslog(LOG_INFO, "%s: got dead line signal", username);
493 sigterm(int signo __unused)
495 printd("terminate\n");
496 if (terminate == 0 && logged_in)
497 syslog(LOG_INFO, "%s: got terminate signal", username);
502 getline(char *buf, int size, int this_fd, time_t fintimeout)
511 for (i = 0; i < size; i++) {
512 if (hup || terminate)
514 if ((timeout = fintimeout - time(NULL)) <= 0)
517 FD_SET(fd, &readfds);
520 if ((ret = select(this_fd + 1, &readfds, NULL, NULL, &tv)) < 0) {
522 syslog(LOG_ERR, "%s: getline: select: %m", username);
526 printd("getline: timed out\n");
529 if ((ret = read(this_fd, &buf[i], 1)) == 1) {
531 if (buf[i] == '\r' || buf[i] == '\0') {
535 if (buf[i] != '\n' && buf[i] != ':')
538 printd("Got %d: %s", i + 1, buf);
543 syslog(LOG_ERR, "%s: getline: read: %m", username);
545 syslog(LOG_ERR, "%s: read returned 0", username);
547 printd("returning %d after %d: %s\n", ret, i, buf);
560 if (ioctl(fd, TIOCMGET, &comstate) < 0) {
561 syslog(LOG_ERR, "%s: ioctl (%s, TIOCMGET): %m",
562 username, devicename);
565 return !!(comstate & TIOCM_CD);
575 if (uucp_lock && locked)
583 fprintf(stderr, "%s\n%s\n%s\n%s\n",
584 "usage: startslip [-d] [-b speed] [-s string1 [-s string2 [...]]] [-h] [-l]",
585 " [-L] [-A annexname] [-U upscript] [-D downscript]",
586 " [-t script_timeout] [-W maxtries] [-w retry_pause]",
587 " [-K keepalive] [-O outfill] [-S unit] device user passwd");