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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1990, 1991, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)startslip.c 8.1 (Berkeley) 6/5/93
31 * $FreeBSD: src/sbin/startslip/startslip.c,v 1.31.2.1 2000/05/07 18:26:51 joe Exp $
34 #include <sys/types.h>
52 #define DEFAULT_BAUD B9600
53 int speed
= DEFAULT_BAUD
;
54 #define FC_NONE 0 /* flow control: none */
55 #define FC_HW 1 /* flow control: hardware (RTS/CTS) */
56 int flowcontrol
= FC_NONE
;
57 int modem_control
= 1; /* !CLOCAL+HUPCL iff we watch carrier. */
59 int uucp_lock
= 0; /* uucp locking */
64 int locked
= 0; /* uucp lock active */
66 int wait_time
= 60; /* then back off */
67 int script_timeout
= 90; /* connect script default timeout */
68 time_t conn_time
, start_time
;
69 int MAXTRIES
= 6; /* w/60 sec and doubling, takes an hour */
70 #define PIDFILE "%sstartslip.%s.pid"
73 char *dials
[MAXDIALS
];
78 char *dvname
, *devicename
;
85 #define syslog fprintf
86 #define LOG_ERR stderr
87 #define LOG_INFO stderr
91 #define printd if (debug) printf
93 static int carrier(void);
94 static void down(int);
95 static int getline(char *, int, int, time_t);
96 static void usage(void);
97 static void sighup(int);
98 static void sigterm(int);
99 static void sigurg(int);
102 main(int argc
, char **argv
)
107 char *dialerstring
= NULL
, buf
[BUFSIZ
];
108 int unitnum
, keepal
= 0, outfill
= 0;
111 char *upscript
= NULL
, *downscript
= NULL
;
112 int first
= 1, tries
= 0;
119 while ((ch
= getopt(argc
, argv
, "dhlb:s:t:w:A:U:D:W:K:O:S:L")) != -1)
125 speed
= atoi(optarg
);
128 if (diali
>= MAXDIALS
)
129 errx(1, "max dial strings number (%d) exceeded", MAXDIALS
);
130 dials
[diali
++] = strdup(optarg
);
133 script_timeout
= atoi(optarg
);
136 wait_time
= atoi(optarg
);
139 MAXTRIES
= atoi(optarg
);
142 annex
= strdup(optarg
);
145 upscript
= strdup(optarg
);
148 downscript
= strdup(optarg
);
160 keepal
= atoi(optarg
);
163 outfill
= atoi(optarg
);
166 sl_unit
= atoi(optarg
);
179 * Copy these so they exist after we clobber them.
181 devicename
= strdup(argv
[0]);
182 username
= strdup(argv
[1]);
183 password
= strdup(argv
[2]);
186 * Security hack. Do not want private information such as the
187 * password and possible phone number to be left around.
188 * So we clobber the arguments.
190 for (ap
= argv
- optind
+ 1; ap
< argv
+ 3; ap
++)
191 for (cp
= *ap
; *cp
!= 0; cp
++)
194 openlog("startslip", LOG_PID
|LOG_PERROR
, LOG_DAEMON
);
197 setbuf(stdout
, NULL
);
199 signal(SIGTERM
, sigterm
);
200 if ((dvname
= strrchr(devicename
, '/')) == NULL
)
205 result
= snprintf(my_pidfile
, sizeof(my_pidfile
),
206 PIDFILE
, _PATH_VARRUN
, dvname
);
207 if (result
< 0 || (unsigned int)result
>= sizeof(my_pidfile
))
210 if ((pfd
= fopen(my_pidfile
, "r")) != NULL
) {
211 if (fscanf(pfd
, "%ld\n", &lpid
) == 1) {
213 if (pid
== lpid
&& pid
> 0)
217 pfd
= NULL
; /* not remove pidfile yet */
218 sleep(5); /* allow down script to be completed */
221 signal(SIGHUP
, SIG_IGN
);
222 signal(SIGURG
, SIG_IGN
);
227 conn_time
= time(NULL
) - start_time
;
234 } else if (fd
>= 0) {
237 conn_time
= time(NULL
) - start_time
;
245 syslog(LOG_INFO
, "%s: connection time elapsed: %ld secs",
246 username
, (long)conn_time
);
247 sprintf(buf
, "LINE=%d %s %s down",
248 diali
? (dialc
- 1) % diali
: 0,
249 downscript
? downscript
: "/sbin/ifconfig" , unitname
);
256 if (MAXTRIES
> 0 && tries
> MAXTRIES
) {
257 syslog(LOG_ERR
, "%s: exiting login after %d tries", username
, tries
);
264 syslog(LOG_INFO
, "%s: sleeping %d seconds (%d tries)",
265 username
, wait_time
* (tries
- 1), tries
);
266 sleep(wait_time
* (tries
- 1));
271 if (daemon(1, debug
) < 0) {
272 syslog(LOG_ERR
, "%s: daemon: %m", username
);
277 printd("restart: pid %ld: ", (long)pid
);
278 if ((pfd
= fopen(my_pidfile
, "w")) != NULL
) {
279 fprintf(pfd
, "%ld\n", (long)pid
);
285 if ((res
= uu_lock(dvname
)) != UU_LOCK_OK
) {
286 if (res
!= UU_LOCK_INUSE
)
287 syslog(LOG_ERR
, "uu_lock: %s", uu_lockerr(res
));
288 syslog(LOG_ERR
, "%s: can't lock %s", username
, devicename
);
293 if ((fd
= open(devicename
, O_RDWR
| O_NONBLOCK
)) < 0) {
294 syslog(LOG_ERR
, "%s: open %s: %m", username
, devicename
);
305 signal(SIGHUP
, sighup
);
306 if (ioctl(fd
, TIOCSCTTY
, 0) < 0) {
307 syslog(LOG_ERR
, "%s: ioctl (TIOCSCTTY): %m", username
);
310 if (tcsetpgrp(fd
, getpid()) < 0) {
311 syslog(LOG_ERR
, "%s: tcsetpgrp failed: %m", username
);
315 if (tcgetattr(fd
, &t
) < 0) {
316 syslog(LOG_ERR
, "%s: tcgetattr(%s): %m", username
, devicename
);
320 switch (flowcontrol
) {
322 t
.c_cflag
|= (CRTS_IFLOW
|CCTS_OFLOW
);
325 t
.c_cflag
&= ~(CRTS_IFLOW
|CCTS_OFLOW
);
331 t
.c_cflag
&= ~(HUPCL
);
332 t
.c_cflag
|= CLOCAL
; /* until modem commands passes */
333 cfsetispeed(&t
, speed
);
334 cfsetospeed(&t
, speed
);
335 if (tcsetattr(fd
, TCSAFLUSH
, &t
) < 0) {
336 syslog(LOG_ERR
, "%s: tcsetattr(%s): %m", username
, devicename
);
339 sleep(2); /* wait for flakey line to settle */
340 if (hup
|| terminate
)
343 wfd
= fdopen(fd
, "w+");
345 syslog(LOG_ERR
, "%s: can't fdopen %s: %m", username
, devicename
);
351 dialerstring
= dials
[dialc
++ % diali
];
353 syslog(LOG_INFO
, "%s: dialer string: %s\\r", username
, dialerstring
);
354 fprintf(wfd
, "%s\r", dialerstring
);
358 fintimeout
= time(NULL
) + script_timeout
;
360 printd("waiting for carrier\n");
361 while (time(NULL
) < fintimeout
&& !carrier()) {
363 if (hup
|| terminate
)
368 t
.c_cflag
&= ~(CLOCAL
);
369 if (tcsetattr(fd
, TCSANOW
, &t
) < 0) {
370 syslog(LOG_ERR
, "%s: tcsetattr(%s): %m", username
, devicename
);
373 /* Only now we able to receive HUP on carrier drop! */
379 printd("look for login: ");
381 if (getline(buf
, BUFSIZ
, fd
, fintimeout
) == 0 || hup
|| terminate
)
384 if (bcmp(buf
, annex
, strlen(annex
)) == 0) {
385 fprintf(wfd
, "slip\r");
386 printd("Sent \"slip\"\n");
389 if (bcmp(&buf
[1], "sername:", 8) == 0) {
390 fprintf(wfd
, "%s\r", username
);
391 printd("Sent login: %s\n", username
);
394 if (bcmp(&buf
[1], "assword:", 8) == 0) {
395 fprintf(wfd
, "%s\r", password
);
396 printd("Sent password: %s\n", password
);
400 if (strstr(&buf
[1], "ogin:") != NULL
) {
401 fprintf(wfd
, "%s\r", username
);
402 printd("Sent login: %s\n", username
);
405 if (strstr(&buf
[1], "assword:") != NULL
) {
406 fprintf(wfd
, "%s\r", password
);
407 printd("Sent password: %s\n", password
);
413 sleep(5); /* Wait until login completed */
414 if (hup
|| terminate
)
416 start_time
= time(NULL
);
422 if (ioctl(fd
, TIOCSETD
, &disc
) < 0) {
423 syslog(LOG_ERR
, "%s: ioctl (%s, TIOCSETD): %m",
424 username
, devicename
);
427 if (sl_unit
>= 0 && ioctl(fd
, SLIOCSUNIT
, &sl_unit
) < 0) {
428 syslog(LOG_ERR
, "%s: ioctl(SLIOCSUNIT): %m", username
);
431 if (ioctl(fd
, SLIOCGUNIT
, &unitnum
) < 0) {
432 syslog(LOG_ERR
, "%s: ioctl(SLIOCGUNIT): %m", username
);
435 sprintf(unitname
, "sl%d", unitnum
);
438 signal(SIGURG
, sigurg
);
439 if (ioctl(fd
, SLIOCSKEEPAL
, &keepal
) < 0) {
440 syslog(LOG_ERR
, "%s: ioctl(SLIOCSKEEPAL): %m", username
);
444 if (outfill
> 0 && ioctl(fd
, SLIOCSOUTFILL
, &outfill
) < 0) {
445 syslog(LOG_ERR
, "%s: ioctl(SLIOCSOUTFILL): %m", username
);
449 sprintf(buf
, "LINE=%d %s %s up",
450 diali
? (dialc
- 1) % diali
: 0,
451 upscript
? upscript
: "/sbin/ifconfig" , unitname
);
456 syslog(LOG_INFO
, "%s: reconnected on %s (%d tries)", username
, unitname
, tries
);
458 syslog(LOG_INFO
, "%s: connected on %s", username
, unitname
);
462 while (hup
== 0 && terminate
== 0) {
464 printd("sigpause return\n");
467 return(0); /* not reached */
471 sighup(int signo __unused
)
474 if (hup
== 0 && logged_in
)
475 syslog(LOG_INFO
, "%s: got hangup signal", username
);
480 sigurg(int signo __unused
)
483 if (hup
== 0 && logged_in
)
484 syslog(LOG_INFO
, "%s: got dead line signal", username
);
489 sigterm(int signo __unused
)
491 printd("terminate\n");
492 if (terminate
== 0 && logged_in
)
493 syslog(LOG_INFO
, "%s: got terminate signal", username
);
498 getline(char *buf
, int size
, int this_fd
, time_t fintimeout
)
507 for (i
= 0; i
< size
; i
++) {
508 if (hup
|| terminate
)
510 if ((timeout
= fintimeout
- time(NULL
)) <= 0)
513 FD_SET(fd
, &readfds
);
516 if ((ret
= select(this_fd
+ 1, &readfds
, NULL
, NULL
, &tv
)) < 0) {
518 syslog(LOG_ERR
, "%s: getline: select: %m", username
);
522 printd("getline: timed out\n");
525 if ((ret
= read(this_fd
, &buf
[i
], 1)) == 1) {
527 if (buf
[i
] == '\r' || buf
[i
] == '\0') {
531 if (buf
[i
] != '\n' && buf
[i
] != ':')
534 printd("Got %d: %s", i
+ 1, buf
);
539 syslog(LOG_ERR
, "%s: getline: read: %m", username
);
541 syslog(LOG_ERR
, "%s: read returned 0", username
);
543 printd("returning %d after %d: %s\n", ret
, i
, buf
);
556 if (ioctl(fd
, TIOCMGET
, &comstate
) < 0) {
557 syslog(LOG_ERR
, "%s: ioctl (%s, TIOCMGET): %m",
558 username
, devicename
);
561 return !!(comstate
& TIOCM_CD
);
571 if (uucp_lock
&& locked
)
579 fprintf(stderr
, "%s\n%s\n%s\n%s\n",
580 "usage: startslip [-d] [-b speed] [-s string1 [-s string2 [...]]] [-h] [-l]",
581 " [-L] [-A annexname] [-U upscript] [-D downscript]",
582 " [-t script_timeout] [-W maxtries] [-w retry_pause]",
583 " [-K keepalive] [-O outfill] [-S unit] device user passwd");