2 * Copyright (c) 1988 Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Copyright (c) 1988 Regents of the University of California. All rights reserved.
37 * @(#)slattach.c 4.6 (Berkeley) 6/1/90
38 * $FreeBSD: src/sbin/slattach/slattach.c,v 1.36 1999/08/28 00:14:25 peter Exp $
39 * $DragonFly: src/sbin/slattach/slattach.c,v 1.4 2003/11/01 17:16:02 drhodus Exp $
42 #include <sys/types.h>
43 #include <sys/ioctl.h>
44 #include <sys/socket.h>
61 #define DEFAULT_BAUD 9600
63 void sighup_handler(); /* SIGHUP handler */
64 void sigint_handler(); /* SIGINT handler */
65 void sigterm_handler(); /* SIGTERM handler */
66 void sigurg_handler(); /* SIGURG handler */
67 void exit_handler(int ret); /* run exit_cmd iff specified upon exit. */
68 void setup_line(int cflag); /* configure terminal settings */
69 void slip_discipline(); /* switch to slip line discipline */
70 void configure_network(); /* configure slip interface */
71 void acquire_line(); /* get tty device as controlling terminal */
72 static void usage(void);
75 char *dev = (char *)0; /* path name of the tty (e.g. /dev/tty01) */
76 char *dvname; /* basename of dev */
77 int locked = 0; /* uucp lock active */
78 int flow_control = 0; /* non-zero to enable hardware flow control. */
79 int modem_control = HUPCL; /* !CLOCAL+HUPCL iff we watch carrier. */
80 int comstate; /* TIOCMGET current state of serial driver */
81 int redial_on_startup = 0; /* iff non-zero execute redial_cmd on startup */
82 speed_t speed = DEFAULT_BAUD; /* baud rate of tty */
83 int slflags = 0; /* compression flags */
84 int unit = -1; /* slip device unit number */
85 int foreground = 0; /* act as demon if zero, else don't fork. */
86 int keepal = 0; /* keepalive timeout */
87 int outfill = 0; /* outfill timeout */
88 int sl_unit = -1; /* unit number */
89 int uucp_lock = 0; /* do uucp locking */
90 int exiting = 0; /* already running exit_handler */
92 struct termios tty; /* tty configuration/state */
94 char tty_path[32]; /* path name of the tty (e.g. /dev/tty01) */
95 char pidfilename[40]; /* e.g. /var/run/slattach.tty01.pid */
96 char *redial_cmd = 0; /* command to exec upon shutdown. */
97 char *config_cmd = 0; /* command to exec if slip unit changes. */
98 char *exit_cmd = 0; /* command to exec before exiting. */
103 fprintf(stderr, "%s\n%s\n",
104 "usage: slattach [-acfhlnz] [-e command] [-r command] [-s speed] [-u command]",
105 " [-L] [-K timeout] [-O timeout] [-S unit] device");
106 /* do not exit here */
110 main(int argc, char **argv)
114 while ((option = getopt(argc, argv, "ace:fhlnr:s:u:zLK:O:S:")) != -1) {
117 slflags |= IFF_LINK2;
118 slflags &= ~IFF_LINK0;
121 slflags |= IFF_LINK0;
122 slflags &= ~IFF_LINK2;
125 exit_cmd = (char*) strdup (optarg);
131 flow_control |= CRTSCTS;
134 modem_control = CLOCAL; /* clear HUPCL too */
137 slflags |= IFF_LINK1;
140 redial_cmd = (char*) strdup (optarg);
143 speed = atoi(optarg);
146 config_cmd = (char*) strdup (optarg);
149 redial_on_startup = 1;
155 keepal = atoi(optarg);
158 outfill = atoi(optarg);
161 sl_unit = atoi(optarg);
170 if (optind == argc - 1)
173 if (optind < (argc - 1))
174 warnx("too many args, first='%s'", argv[optind]);
175 if (optind > (argc - 1))
176 warnx("not enough args");
177 if (dev == (char *)0) {
181 if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
182 strcpy(tty_path, _PATH_DEV);
183 strcat(tty_path, "/");
184 strncat(tty_path, dev, 10);
187 dvname = strrchr(dev, '/'); /* always succeeds */
188 dvname++; /* trailing tty pathname component */
189 snprintf(pidfilename, sizeof(pidfilename),
190 "%sslattach.%s.pid", _PATH_VARRUN, dvname);
191 printf("%s\n",pidfilename);
194 daemon(0,0); /* fork, setsid, chdir /, and close std*. */
195 /* daemon() closed stderr, so log errors from here on. */
196 openlog("slattach",LOG_CONS|LOG_PID,LOG_DAEMON);
198 acquire_line(); /* get tty device as controlling terminal */
199 setup_line(0); /* configure for slip line discipline */
200 slip_discipline(); /* switch to slip line discipline */
202 /* upon INT log a timestamp and exit. */
203 if (signal(SIGINT,sigint_handler) == SIG_ERR)
204 syslog(LOG_NOTICE,"cannot install SIGINT handler: %m");
205 /* upon TERM log a timestamp and exit. */
206 if (signal(SIGTERM,sigterm_handler) == SIG_ERR)
207 syslog(LOG_NOTICE,"cannot install SIGTERM handler: %m");
208 /* upon HUP redial and reconnect. */
209 if (signal(SIGHUP,sighup_handler) == SIG_ERR)
210 syslog(LOG_NOTICE,"cannot install SIGHUP handler: %m");
212 if (redial_on_startup)
214 else if (!(modem_control & CLOCAL)) {
215 if (ioctl(fd, TIOCMGET, &comstate) < 0)
216 syslog(LOG_NOTICE,"cannot get carrier state: %m");
217 if (!(comstate & TIOCM_CD)) { /* check for carrier */
218 /* force a redial if no carrier */
219 kill (getpid(), SIGHUP);
223 configure_network(); /* configure the network if needed. */
232 /* Close all FDs, fork, reopen tty port as 0-2, and make it the
233 controlling terminal for our process group. */
234 void acquire_line(void)
236 int ttydisc = TTYDISC;
240 /* reset to tty discipline */
241 if (fd >= 0 && ioctl(fd, TIOCSETD, &ttydisc) < 0) {
242 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
246 (void)close(STDIN_FILENO); /* close FDs before forking. */
247 (void)close(STDOUT_FILENO);
248 (void)close(STDERR_FILENO);
252 signal(SIGHUP, SIG_IGN); /* ignore HUP signal when parent dies. */
253 if (daemon(0,0)) { /* fork, setsid, chdir /, and close std*. */
254 syslog(LOG_ERR, "daemon(0,0): %m");
258 while (getppid () != 1)
259 sleep (1); /* Wait for parent to die. */
261 /* create PID file */
262 if((pidfile = fopen(pidfilename, "w"))) {
263 fprintf(pidfile, "%ld\n", (long)getpid());
267 if (signal(SIGHUP,sighup_handler) == SIG_ERR) /* Re-enable HUP signal */
268 syslog(LOG_NOTICE,"cannot install SIGHUP handler: %m");
271 /* unlock not needed here, always re-lock with new pid */
273 if ((res = uu_lock(dvname)) != UU_LOCK_OK) {
274 if (res != UU_LOCK_INUSE)
275 syslog(LOG_ERR, "uu_lock: %s", uu_lockerr(res));
276 syslog(LOG_ERR, "can't lock %s", dev);
282 if ((fd = open(dev, O_RDWR | O_NONBLOCK, 0)) < 0) {
283 syslog(LOG_ERR, "open(%s) %m", dev);
286 /* Turn off O_NONBLOCK for dumb redialers, if any. */
287 if ((oflags = fcntl(fd, F_GETFL)) == -1) {
288 syslog(LOG_ERR, "fcntl(F_GETFL) failed: %m");
291 if (fcntl(fd, F_SETFL, oflags & ~O_NONBLOCK) == -1) {
292 syslog(LOG_ERR, "fcntl(F_SETFL) failed: %m");
295 (void)dup2(fd, STDIN_FILENO);
296 (void)dup2(fd, STDOUT_FILENO);
297 (void)dup2(fd, STDERR_FILENO);
302 /* acquire the serial line as a controlling terminal. */
303 if (ioctl(fd, TIOCSCTTY, 0) < 0) {
304 syslog(LOG_ERR,"ioctl(TIOCSCTTY): %m");
307 /* Make us the foreground process group associated with the
308 slip line which is our controlling terminal. */
309 if (tcsetpgrp(fd, getpid()) < 0)
310 syslog(LOG_NOTICE,"tcsetpgrp failed: %m");
313 /* Set the tty flags and set DTR. */
314 /* Call as setup_line(CLOCAL) to force clocal assertion. */
315 void setup_line(int cflag)
317 tty.c_lflag = tty.c_iflag = tty.c_oflag = 0;
318 tty.c_cflag = CREAD | CS8 | flow_control | modem_control | cflag;
319 cfsetispeed(&tty, speed);
320 cfsetospeed(&tty, speed);
321 /* set the line speed and flow control */
322 if (tcsetattr(fd, TCSAFLUSH, &tty) < 0) {
323 syslog(LOG_ERR, "tcsetattr(TCSAFLUSH): %m");
326 /* set data terminal ready */
327 if (ioctl(fd, TIOCSDTR) < 0) {
328 syslog(LOG_ERR, "ioctl(TIOCSDTR): %m");
333 /* Put the line in slip discipline. */
334 void slip_discipline(void)
337 int slipdisc = SLIPDISC;
338 int s, tmp_unit = -1;
340 /* Switch to slip line discipline. */
341 if (ioctl(fd, TIOCSETD, &slipdisc) < 0) {
342 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
346 if (sl_unit >= 0 && ioctl(fd, SLIOCSUNIT, &sl_unit) < 0) {
347 syslog(LOG_ERR, "ioctl(SLIOCSUNIT): %m");
351 /* find out what unit number we were assigned */
352 if (ioctl(fd, SLIOCGUNIT, (caddr_t)&tmp_unit) < 0) {
353 syslog(LOG_ERR, "ioctl(SLIOCGUNIT): %m");
358 syslog(LOG_ERR, "bad unit (%d) from ioctl(SLIOCGUNIT)",tmp_unit);
363 signal(SIGURG, sigurg_handler);
364 if (ioctl(fd, SLIOCSKEEPAL, &keepal) < 0) {
365 syslog(LOG_ERR, "ioctl(SLIOCSKEEPAL): %m");
369 if (outfill > 0 && ioctl(fd, SLIOCSOUTFILL, &outfill) < 0) {
370 syslog(LOG_ERR, "ioctl(SLIOCSOUTFILL): %m");
374 /* open a socket as the handle to the interface */
375 s = socket(AF_INET, SOCK_DGRAM, 0);
377 syslog(LOG_ERR, "socket: %m");
380 sprintf(ifr.ifr_name, "sl%d", tmp_unit);
382 /* get the flags for the interface */
383 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
384 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
388 /* Assert any compression or no-icmp flags. */
389 #define SLMASK (~(IFF_LINK0 | IFF_LINK1 | IFF_LINK2))
390 ifr.ifr_flags &= SLMASK;
391 ifr.ifr_flags |= slflags;
392 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) {
393 syslog(LOG_ERR, "ioctl (SIOCSIFFLAGS): %m");
399 /* configure the interface, e.g. by passing the unit number to a script. */
400 void configure_network(void)
404 /* find out what unit number we were assigned */
405 if (ioctl(fd, SLIOCGUNIT, (caddr_t)&new_unit) < 0) {
406 syslog(LOG_ERR, "ioctl(SLIOCGUNIT): %m");
409 /* iff the unit number changes either invoke config_cmd or punt. */
412 s = (char*) malloc(strlen(config_cmd) + 32);
414 syslog(LOG_ERR, "malloc failed");
417 sprintf (s, "%s %d %d", config_cmd, unit, new_unit);
418 syslog(LOG_NOTICE, "configuring %s (sl%d):", dev, unit);
419 syslog(LOG_NOTICE, " '%s'", s);
424 /* don't compare unit numbers if this is the first time to attach. */
427 if (new_unit != unit) {
429 "slip unit changed from sl%d to sl%d, but no -u CMD was specified!",
433 syslog(LOG_NOTICE,"sl%d connected to %s at %d baud",unit,dev,speed);
437 /* sighup_handler() is invoked when carrier drops, eg. before redial. */
438 void sighup_handler(void)
442 if (redial_cmd == NULL) {
443 syslog(LOG_NOTICE,"SIGHUP on %s (sl%d); exiting", dev, unit);
447 /* invoke a shell for redial_cmd or punt. */
448 if (*redial_cmd) { /* Non-empty redial command */
449 syslog(LOG_NOTICE,"SIGHUP on %s (sl%d); running '%s'",
450 dev, unit, redial_cmd);
451 acquire_line(); /* reopen dead line */
455 uu_unlock(dvname); /* for redial */
458 if (system(redial_cmd))
462 if ((res = uu_lock(dvname)) != UU_LOCK_OK) {
463 if (res != UU_LOCK_INUSE)
464 syslog(LOG_ERR, "uu_lock: %s", uu_lockerr(res));
465 syslog(LOG_ERR, "can't relock %s after %s, aborting",
471 /* Now check again for carrier (dial command is done): */
472 if (!(modem_control & CLOCAL)) {
473 tty.c_cflag &= ~CLOCAL;
474 if (tcsetattr(fd, TCSAFLUSH, &tty) < 0) {
475 syslog(LOG_ERR, "tcsetattr(TCSAFLUSH): %m");
478 ioctl(fd, TIOCMGET, &comstate);
479 if (!(comstate & TIOCM_CD)) { /* check for carrier */
480 /* force a redial if no carrier */
485 } else { /* Empty redial command */
486 syslog(LOG_NOTICE,"SIGHUP on %s (sl%d); reestablish connection",
488 acquire_line(); /* reopen dead line */
489 setup_line(0); /* restore ospeed from hangup (B0) */
490 /* If modem control, just wait for carrier before attaching.
491 If no modem control, just fall through immediately. */
492 if (!(modem_control & CLOCAL)) {
495 syslog(LOG_NOTICE, "waiting for carrier on %s (sl%d)",
497 /* Now wait for carrier before attaching line. */
498 /* We must poll since CLOCAL prevents signal. */
501 ioctl(fd, TIOCMGET, &comstate);
502 if (comstate & TIOCM_CD)
505 syslog(LOG_NOTICE, "carrier now present on %s (sl%d)",
512 /* Signal handler for SIGINT. We just log and exit. */
513 void sigint_handler(void)
516 syslog(LOG_NOTICE,"SIGINT on %s (sl%d); exiting",dev,unit);
519 /* Signal handler for SIGURG. */
520 void sigurg_handler(void)
522 int ttydisc = TTYDISC;
524 signal(SIGURG, SIG_IGN);
526 syslog(LOG_NOTICE,"SIGURG on %s (sl%d); hangup",dev,unit);
527 if (ioctl(fd, TIOCSETD, &ttydisc) < 0) {
528 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
531 cfsetospeed(&tty, B0);
532 if (tcsetattr(fd, TCSANOW, &tty) < 0) {
533 syslog(LOG_ERR, "tcsetattr(TCSANOW): %m");
536 /* Need to go to sighup handler in any case */
537 if (modem_control & CLOCAL)
538 kill (getpid(), SIGHUP);
541 /* Signal handler for SIGTERM. We just log and exit. */
542 void sigterm_handler(void)
545 syslog(LOG_NOTICE,"SIGTERM on %s (sl%d); exiting",dev,unit);
548 /* Run config_cmd if specified before exiting. */
549 void exit_handler(int ret)
554 * First close the slip line in case exit_cmd wants it (like to hang
555 * up a modem or something).
559 if (uucp_lock && locked)
562 /* Remove the PID file */
563 (void)unlink(pidfilename);
567 s = (char*) malloc(strlen(config_cmd) + 32);
569 syslog(LOG_ERR, "malloc failed");
572 sprintf (s, "%s %d -1", config_cmd, unit);
573 syslog(LOG_NOTICE, "deconfiguring %s (sl%d):", dev, unit);
574 syslog(LOG_NOTICE, " '%s'", s);
578 /* invoke a shell for exit_cmd. */
580 syslog(LOG_NOTICE,"exiting after running %s", exit_cmd);
586 /* local variables: */
587 /* c-indent-level: 8 */
588 /* c-argdecl-indent: 0 */
589 /* c-label-offset: -8 */
590 /* c-continued-statement-offset: 8 */
591 /* c-brace-offset: 0 */
592 /* comment-column: 32 */