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.7 2005/02/02 20:27:51 cpressey 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 static int carrier(void);
99 static void down(int);
100 static int getline(char *, int, int, time_t);
101 static void usage(void);
102 static void sighup(int);
103 static void sigterm(int);
104 static void sigurg(int);
107 main(int argc
, char **argv
)
112 char *dialerstring
= 0, buf
[BUFSIZ
];
113 int unitnum
, keepal
= 0, outfill
= 0;
116 char *upscript
= NULL
, *downscript
= NULL
;
117 int first
= 1, tries
= 0;
124 while ((ch
= getopt(argc
, argv
, "dhlb:s:t:w:A:U:D:W:K:O:S:L")) != -1)
130 speed
= atoi(optarg
);
133 if (diali
>= MAXDIALS
)
134 errx(1, "max dial strings number (%d) exceeded", MAXDIALS
);
135 dials
[diali
++] = strdup(optarg
);
138 script_timeout
= atoi(optarg
);
141 wait_time
= atoi(optarg
);
144 MAXTRIES
= atoi(optarg
);
147 annex
= strdup(optarg
);
150 upscript
= strdup(optarg
);
153 downscript
= strdup(optarg
);
165 keepal
= atoi(optarg
);
168 outfill
= atoi(optarg
);
171 sl_unit
= atoi(optarg
);
184 * Copy these so they exist after we clobber them.
186 devicename
= strdup(argv
[0]);
187 username
= strdup(argv
[1]);
188 password
= strdup(argv
[2]);
191 * Security hack. Do not want private information such as the
192 * password and possible phone number to be left around.
193 * So we clobber the arguments.
195 for (ap
= argv
- optind
+ 1; ap
< argv
+ 3; ap
++)
196 for (cp
= *ap
; *cp
!= 0; cp
++)
199 openlog("startslip", LOG_PID
|LOG_PERROR
, LOG_DAEMON
);
202 setbuf(stdout
, NULL
);
204 signal(SIGTERM
, sigterm
);
205 if ((dvname
= strrchr(devicename
, '/')) == NULL
)
210 result
= snprintf(my_pidfile
, sizeof(my_pidfile
),
211 PIDFILE
, _PATH_VARRUN
, dvname
);
212 if (result
< 0 || (unsigned int)result
>= sizeof(my_pidfile
))
215 if ((pfd
= fopen(my_pidfile
, "r")) != NULL
) {
216 if (fscanf(pfd
, "%ld\n", &lpid
) == 1) {
218 if (pid
== lpid
&& pid
> 0)
222 pfd
= NULL
; /* not remove pidfile yet */
223 sleep(5); /* allow down script to be completed */
226 signal(SIGHUP
, SIG_IGN
);
227 signal(SIGURG
, SIG_IGN
);
232 conn_time
= time(NULL
) - start_time
;
239 } else if (fd
>= 0) {
242 conn_time
= time(NULL
) - start_time
;
250 syslog(LOG_INFO
, "%s: connection time elapsed: %ld secs",
251 username
, (long)conn_time
);
252 sprintf(buf
, "LINE=%d %s %s down",
253 diali
? (dialc
- 1) % diali
: 0,
254 downscript
? downscript
: "/sbin/ifconfig" , unitname
);
261 if (MAXTRIES
> 0 && tries
> MAXTRIES
) {
262 syslog(LOG_ERR
, "%s: exiting login after %d tries", username
, tries
);
269 syslog(LOG_INFO
, "%s: sleeping %d seconds (%d tries)",
270 username
, wait_time
* (tries
- 1), tries
);
271 sleep(wait_time
* (tries
- 1));
276 if (daemon(1, debug
) < 0) {
277 syslog(LOG_ERR
, "%s: daemon: %m", username
);
282 printd("restart: pid %ld: ", (long)pid
);
283 if ((pfd
= fopen(my_pidfile
, "w")) != NULL
) {
284 fprintf(pfd
, "%ld\n", (long)pid
);
290 if ((res
= uu_lock(dvname
)) != UU_LOCK_OK
) {
291 if (res
!= UU_LOCK_INUSE
)
292 syslog(LOG_ERR
, "uu_lock: %s", uu_lockerr(res
));
293 syslog(LOG_ERR
, "%s: can't lock %s", username
, devicename
);
298 if ((fd
= open(devicename
, O_RDWR
| O_NONBLOCK
)) < 0) {
299 syslog(LOG_ERR
, "%s: open %s: %m", username
, devicename
);
310 signal(SIGHUP
, sighup
);
311 if (ioctl(fd
, TIOCSCTTY
, 0) < 0) {
312 syslog(LOG_ERR
, "%s: ioctl (TIOCSCTTY): %m", username
);
315 if (tcsetpgrp(fd
, getpid()) < 0) {
316 syslog(LOG_ERR
, "%s: tcsetpgrp failed: %m", username
);
320 if (tcgetattr(fd
, &t
) < 0) {
321 syslog(LOG_ERR
, "%s: tcgetattr(%s): %m", username
, devicename
);
325 switch (flowcontrol
) {
327 t
.c_cflag
|= (CRTS_IFLOW
|CCTS_OFLOW
);
330 t
.c_cflag
&= ~(CRTS_IFLOW
|CCTS_OFLOW
);
336 t
.c_cflag
&= ~(HUPCL
);
337 t
.c_cflag
|= CLOCAL
; /* until modem commands passes */
338 cfsetispeed(&t
, speed
);
339 cfsetospeed(&t
, speed
);
340 if (tcsetattr(fd
, TCSAFLUSH
, &t
) < 0) {
341 syslog(LOG_ERR
, "%s: tcsetattr(%s): %m", username
, devicename
);
344 sleep(2); /* wait for flakey line to settle */
345 if (hup
|| terminate
)
348 wfd
= fdopen(fd
, "w+");
350 syslog(LOG_ERR
, "%s: can't fdopen %s: %m", username
, devicename
);
356 dialerstring
= dials
[dialc
++ % diali
];
358 syslog(LOG_INFO
, "%s: dialer string: %s\\r", username
, dialerstring
);
359 fprintf(wfd
, "%s\r", dialerstring
);
363 fintimeout
= time(NULL
) + script_timeout
;
365 printd("waiting for carrier\n");
366 while (time(NULL
) < fintimeout
&& !carrier()) {
368 if (hup
|| terminate
)
373 t
.c_cflag
&= ~(CLOCAL
);
374 if (tcsetattr(fd
, TCSANOW
, &t
) < 0) {
375 syslog(LOG_ERR
, "%s: tcsetattr(%s): %m", username
, devicename
);
378 /* Only now we able to receive HUP on carrier drop! */
384 printd("look for login: ");
386 if (getline(buf
, BUFSIZ
, fd
, fintimeout
) == 0 || hup
|| terminate
)
389 if (bcmp(buf
, annex
, strlen(annex
)) == 0) {
390 fprintf(wfd
, "slip\r");
391 printd("Sent \"slip\"\n");
394 if (bcmp(&buf
[1], "sername:", 8) == 0) {
395 fprintf(wfd
, "%s\r", username
);
396 printd("Sent login: %s\n", username
);
399 if (bcmp(&buf
[1], "assword:", 8) == 0) {
400 fprintf(wfd
, "%s\r", password
);
401 printd("Sent password: %s\n", password
);
405 if (strstr(&buf
[1], "ogin:") != NULL
) {
406 fprintf(wfd
, "%s\r", username
);
407 printd("Sent login: %s\n", username
);
410 if (strstr(&buf
[1], "assword:") != NULL
) {
411 fprintf(wfd
, "%s\r", password
);
412 printd("Sent password: %s\n", password
);
418 sleep(5); /* Wait until login completed */
419 if (hup
|| terminate
)
421 start_time
= time(NULL
);
427 if (ioctl(fd
, TIOCSETD
, &disc
) < 0) {
428 syslog(LOG_ERR
, "%s: ioctl (%s, TIOCSETD): %m",
429 username
, devicename
);
432 if (sl_unit
>= 0 && ioctl(fd
, SLIOCSUNIT
, &sl_unit
) < 0) {
433 syslog(LOG_ERR
, "%s: ioctl(SLIOCSUNIT): %m", username
);
436 if (ioctl(fd
, SLIOCGUNIT
, &unitnum
) < 0) {
437 syslog(LOG_ERR
, "%s: ioctl(SLIOCGUNIT): %m", username
);
440 sprintf(unitname
, "sl%d", unitnum
);
443 signal(SIGURG
, sigurg
);
444 if (ioctl(fd
, SLIOCSKEEPAL
, &keepal
) < 0) {
445 syslog(LOG_ERR
, "%s: ioctl(SLIOCSKEEPAL): %m", username
);
449 if (outfill
> 0 && ioctl(fd
, SLIOCSOUTFILL
, &outfill
) < 0) {
450 syslog(LOG_ERR
, "%s: ioctl(SLIOCSOUTFILL): %m", username
);
454 sprintf(buf
, "LINE=%d %s %s up",
455 diali
? (dialc
- 1) % diali
: 0,
456 upscript
? upscript
: "/sbin/ifconfig" , unitname
);
461 syslog(LOG_INFO
, "%s: reconnected on %s (%d tries)", username
, unitname
, tries
);
463 syslog(LOG_INFO
, "%s: connected on %s", username
, unitname
);
467 while (hup
== 0 && terminate
== 0) {
469 printd("sigpause return\n");
472 return(0); /* not reached */
476 sighup(int signo __unused
)
479 if (hup
== 0 && logged_in
)
480 syslog(LOG_INFO
, "%s: got hangup signal", username
);
485 sigurg(int signo __unused
)
488 if (hup
== 0 && logged_in
)
489 syslog(LOG_INFO
, "%s: got dead line signal", username
);
494 sigterm(int signo __unused
)
496 printd("terminate\n");
497 if (terminate
== 0 && logged_in
)
498 syslog(LOG_INFO
, "%s: got terminate signal", username
);
503 getline(char *buf
, int size
, int this_fd
, time_t fintimeout
)
512 for (i
= 0; i
< size
; i
++) {
513 if (hup
|| terminate
)
515 if ((timeout
= fintimeout
- time(NULL
)) <= 0)
518 FD_SET(fd
, &readfds
);
521 if ((ret
= select(this_fd
+ 1, &readfds
, NULL
, NULL
, &tv
)) < 0) {
523 syslog(LOG_ERR
, "%s: getline: select: %m", username
);
527 printd("getline: timed out\n");
530 if ((ret
= read(this_fd
, &buf
[i
], 1)) == 1) {
532 if (buf
[i
] == '\r' || buf
[i
] == '\0') {
536 if (buf
[i
] != '\n' && buf
[i
] != ':')
539 printd("Got %d: %s", i
+ 1, buf
);
544 syslog(LOG_ERR
, "%s: getline: read: %m", username
);
546 syslog(LOG_ERR
, "%s: read returned 0", username
);
548 printd("returning %d after %d: %s\n", ret
, i
, buf
);
561 if (ioctl(fd
, TIOCMGET
, &comstate
) < 0) {
562 syslog(LOG_ERR
, "%s: ioctl (%s, TIOCMGET): %m",
563 username
, devicename
);
566 return !!(comstate
& TIOCM_CD
);
576 if (uucp_lock
&& locked
)
584 fprintf(stderr
, "%s\n%s\n%s\n%s\n",
585 "usage: startslip [-d] [-b speed] [-s string1 [-s string2 [...]]] [-h] [-l]",
586 " [-L] [-A annexname] [-U upscript] [-D downscript]",
587 " [-t script_timeout] [-W maxtries] [-w retry_pause]",
588 " [-K keepalive] [-O outfill] [-S unit] device user passwd");