1 /* 2 * Copyright (c) 1988 Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Adams. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 23 * 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 34 * SUCH DAMAGE. 35 * 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 */ 40 41 #include <sys/types.h> 42 #include <sys/ioctl.h> 43 #include <sys/socket.h> 44 45 #include <err.h> 46 #include <fcntl.h> 47 #include <libutil.h> 48 #include <paths.h> 49 #include <signal.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <syslog.h> 54 #include <termios.h> 55 #include <unistd.h> 56 57 #include <net/if.h> 58 #include <net/slip.h> 59 60 #define DEFAULT_BAUD 9600 61 62 static void sighup_handler(int); /* SIGHUP handler */ 63 static void sigint_handler(int); /* SIGINT handler */ 64 static void sigterm_handler(int); /* SIGTERM handler */ 65 static void sigurg_handler(int); /* SIGURG handler */ 66 static void exit_handler(int); /* run exit_cmd iff specified upon exit. */ 67 static void setup_line(int); /* configure terminal settings */ 68 static void slip_discipline(void); /* switch to slip line discipline */ 69 static void configure_network(void); /* configure slip interface */ 70 static void acquire_line(void); /* get tty device as controlling terminal */ 71 static void usage(void); 72 73 int fd = -1; 74 char *dev = NULL; /* path name of the tty (e.g. /dev/tty01) */ 75 char *dvname; /* basename of dev */ 76 int locked = 0; /* uucp lock active */ 77 int flow_control = 0; /* non-zero to enable hardware flow control. */ 78 int modem_control = HUPCL; /* !CLOCAL+HUPCL iff we watch carrier. */ 79 int comstate; /* TIOCMGET current state of serial driver */ 80 int redial_on_startup = 0; /* iff non-zero execute redial_cmd on startup */ 81 speed_t speed = DEFAULT_BAUD; /* baud rate of tty */ 82 int slflags = 0; /* compression flags */ 83 int unit = -1; /* slip device unit number */ 84 int foreground = 0; /* act as demon if zero, else don't fork. */ 85 int keepal = 0; /* keepalive timeout */ 86 int outfill = 0; /* outfill timeout */ 87 int sl_unit = -1; /* unit number */ 88 int uucp_lock = 0; /* do uucp locking */ 89 int exiting = 0; /* already running exit_handler */ 90 91 struct termios tty; /* tty configuration/state */ 92 93 char tty_path[32]; /* path name of the tty (e.g. /dev/tty01) */ 94 char pidfilename[40]; /* e.g. /var/run/slattach.tty01.pid */ 95 char *redial_cmd = NULL; /* command to exec upon shutdown. */ 96 char *config_cmd = NULL; /* command to exec if slip unit changes. */ 97 char *exit_cmd = NULL; /* command to exec before exiting. */ 98 99 static void 100 usage(void) 101 { 102 fprintf(stderr, "%s\n%s\n", 103 "usage: slattach [-acfhlnz] [-e command] [-r command] [-s speed] [-u command]", 104 " [-L] [-K timeout] [-O timeout] [-S unit] device"); 105 /* do not exit here */ 106 } 107 108 int 109 main(int argc, char **argv) 110 { 111 int option; 112 113 while ((option = getopt(argc, argv, "ace:fhlnr:s:u:zLK:O:S:")) != -1) { 114 switch (option) { 115 case 'a': 116 slflags |= IFF_LINK2; 117 slflags &= ~IFF_LINK0; 118 break; 119 case 'c': 120 slflags |= IFF_LINK0; 121 slflags &= ~IFF_LINK2; 122 break; 123 case 'e': 124 exit_cmd = (char*) strdup (optarg); 125 break; 126 case 'f': 127 foreground = 1; 128 break; 129 case 'h': 130 flow_control |= CRTSCTS; 131 break; 132 case 'l': 133 modem_control = CLOCAL; /* clear HUPCL too */ 134 break; 135 case 'n': 136 slflags |= IFF_LINK1; 137 break; 138 case 'r': 139 redial_cmd = (char*) strdup (optarg); 140 break; 141 case 's': 142 speed = atoi(optarg); 143 break; 144 case 'u': 145 config_cmd = (char*) strdup (optarg); 146 break; 147 case 'z': 148 redial_on_startup = 1; 149 break; 150 case 'L': 151 uucp_lock = 1; 152 break; 153 case 'K': 154 keepal = atoi(optarg); 155 break; 156 case 'O': 157 outfill = atoi(optarg); 158 break; 159 case 'S': 160 sl_unit = atoi(optarg); 161 break; 162 case '?': 163 default: 164 usage(); 165 exit_handler(1); 166 } 167 } 168 169 if (optind == argc - 1) 170 dev = argv[optind]; 171 172 if (optind < (argc - 1)) 173 warnx("too many args, first='%s'", argv[optind]); 174 if (optind > (argc - 1)) 175 warnx("not enough args"); 176 if (dev == NULL) { 177 usage(); 178 exit_handler(2); 179 } 180 if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) { 181 strcpy(tty_path, _PATH_DEV); 182 strcat(tty_path, "/"); 183 strncat(tty_path, dev, 10); 184 dev = tty_path; 185 } 186 dvname = strrchr(dev, '/'); /* always succeeds */ 187 dvname++; /* trailing tty pathname component */ 188 snprintf(pidfilename, sizeof(pidfilename), 189 "%sslattach.%s.pid", _PATH_VARRUN, dvname); 190 printf("%s\n",pidfilename); 191 192 if (!foreground) 193 daemon(0,0); /* fork, setsid, chdir /, and close std*. */ 194 /* daemon() closed stderr, so log errors from here on. */ 195 openlog("slattach",LOG_CONS|LOG_PID,LOG_DAEMON); 196 197 acquire_line(); /* get tty device as controlling terminal */ 198 setup_line(0); /* configure for slip line discipline */ 199 slip_discipline(); /* switch to slip line discipline */ 200 201 /* upon INT log a timestamp and exit. */ 202 if (signal(SIGINT,sigint_handler) == SIG_ERR) 203 syslog(LOG_NOTICE,"cannot install SIGINT handler: %m"); 204 /* upon TERM log a timestamp and exit. */ 205 if (signal(SIGTERM,sigterm_handler) == SIG_ERR) 206 syslog(LOG_NOTICE,"cannot install SIGTERM handler: %m"); 207 /* upon HUP redial and reconnect. */ 208 if (signal(SIGHUP,sighup_handler) == SIG_ERR) 209 syslog(LOG_NOTICE,"cannot install SIGHUP handler: %m"); 210 211 if (redial_on_startup) 212 sighup_handler(0); 213 else if (!(modem_control & CLOCAL)) { 214 if (ioctl(fd, TIOCMGET, &comstate) < 0) 215 syslog(LOG_NOTICE,"cannot get carrier state: %m"); 216 if (!(comstate & TIOCM_CD)) { /* check for carrier */ 217 /* force a redial if no carrier */ 218 kill (getpid(), SIGHUP); 219 } else 220 configure_network(); 221 } else 222 configure_network(); /* configure the network if needed. */ 223 224 for (;;) { 225 sigset_t mask; 226 sigemptyset(&mask); 227 sigsuspend(&mask); 228 } 229 } 230 231 /* Close all FDs, fork, reopen tty port as 0-2, and make it the 232 controlling terminal for our process group. */ 233 static void 234 acquire_line(void) 235 { 236 int ttydisc = TTYDISC; 237 int oflags; 238 FILE *pid_file; 239 240 /* reset to tty discipline */ 241 if (fd >= 0 && ioctl(fd, TIOCSETD, &ttydisc) < 0) { 242 syslog(LOG_ERR, "ioctl(TIOCSETD): %m"); 243 exit_handler(1); 244 } 245 246 close(STDIN_FILENO); /* close FDs before forking. */ 247 close(STDOUT_FILENO); 248 close(STDERR_FILENO); 249 if (fd > 2) 250 close(fd); 251 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"); 255 exit_handler(1); 256 } 257 258 while (getppid () != 1) 259 sleep (1); /* Wait for parent to die. */ 260 261 /* create PID file */ 262 if((pid_file = fopen(pidfilename, "w"))) { 263 fprintf(pid_file, "%ld\n", (long)getpid()); 264 fclose(pid_file); 265 } 266 267 if (signal(SIGHUP,sighup_handler) == SIG_ERR) /* Re-enable HUP signal */ 268 syslog(LOG_NOTICE,"cannot install SIGHUP handler: %m"); 269 270 if (uucp_lock) { 271 /* unlock not needed here, always re-lock with new pid */ 272 int res; 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); 277 exit_handler(1); 278 } 279 locked = 1; 280 } 281 282 if ((fd = open(dev, O_RDWR | O_NONBLOCK, 0)) < 0) { 283 syslog(LOG_ERR, "open(%s) %m", dev); 284 exit_handler(1); 285 } 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"); 289 exit_handler(1); 290 } 291 if (fcntl(fd, F_SETFL, oflags & ~O_NONBLOCK) == -1) { 292 syslog(LOG_ERR, "fcntl(F_SETFL) failed: %m"); 293 exit_handler(1); 294 } 295 dup2(fd, STDIN_FILENO); 296 dup2(fd, STDOUT_FILENO); 297 dup2(fd, STDERR_FILENO); 298 if (fd > 2) 299 close (fd); 300 fd = STDIN_FILENO; 301 302 /* acquire the serial line as a controlling terminal. */ 303 if (ioctl(fd, TIOCSCTTY, 0) < 0) { 304 syslog(LOG_ERR,"ioctl(TIOCSCTTY): %m"); 305 exit_handler(1); 306 } 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"); 311 } 312 313 /* Set the tty flags and set DTR. */ 314 /* Call as setup_line(CLOCAL) to force clocal assertion. */ 315 static void 316 setup_line(int cflag) 317 { 318 tty.c_lflag = tty.c_iflag = tty.c_oflag = 0; 319 tty.c_cflag = CREAD | CS8 | flow_control | modem_control | cflag; 320 cfsetispeed(&tty, speed); 321 cfsetospeed(&tty, speed); 322 /* set the line speed and flow control */ 323 if (tcsetattr(fd, TCSAFLUSH, &tty) < 0) { 324 syslog(LOG_ERR, "tcsetattr(TCSAFLUSH): %m"); 325 exit_handler(1); 326 } 327 /* set data terminal ready */ 328 if (ioctl(fd, TIOCSDTR) < 0) { 329 syslog(LOG_ERR, "ioctl(TIOCSDTR): %m"); 330 exit_handler(1); 331 } 332 } 333 334 /* Put the line in slip discipline. */ 335 static void 336 slip_discipline(void) 337 { 338 struct ifreq ifr; 339 int slipdisc = SLIPDISC; 340 int s, tmp_unit = -1; 341 342 /* Switch to slip line discipline. */ 343 if (ioctl(fd, TIOCSETD, &slipdisc) < 0) { 344 syslog(LOG_ERR, "ioctl(TIOCSETD): %m"); 345 exit_handler(1); 346 } 347 348 if (sl_unit >= 0 && ioctl(fd, SLIOCSUNIT, &sl_unit) < 0) { 349 syslog(LOG_ERR, "ioctl(SLIOCSUNIT): %m"); 350 exit_handler(1); 351 } 352 353 /* find out what unit number we were assigned */ 354 if (ioctl(fd, SLIOCGUNIT, (caddr_t)&tmp_unit) < 0) { 355 syslog(LOG_ERR, "ioctl(SLIOCGUNIT): %m"); 356 exit_handler(1); 357 } 358 359 if (tmp_unit < 0) { 360 syslog(LOG_ERR, "bad unit (%d) from ioctl(SLIOCGUNIT)",tmp_unit); 361 exit_handler(1); 362 } 363 364 if (keepal > 0) { 365 signal(SIGURG, sigurg_handler); 366 if (ioctl(fd, SLIOCSKEEPAL, &keepal) < 0) { 367 syslog(LOG_ERR, "ioctl(SLIOCSKEEPAL): %m"); 368 exit_handler(1); 369 } 370 } 371 if (outfill > 0 && ioctl(fd, SLIOCSOUTFILL, &outfill) < 0) { 372 syslog(LOG_ERR, "ioctl(SLIOCSOUTFILL): %m"); 373 exit_handler(1); 374 } 375 376 /* open a socket as the handle to the interface */ 377 s = socket(AF_INET, SOCK_DGRAM, 0); 378 if (s < 0) { 379 syslog(LOG_ERR, "socket: %m"); 380 exit_handler(1); 381 } 382 sprintf(ifr.ifr_name, "sl%d", tmp_unit); 383 384 /* get the flags for the interface */ 385 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) { 386 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m"); 387 exit_handler(1); 388 } 389 390 /* Assert any compression or no-icmp flags. */ 391 #define SLMASK (~(IFF_LINK0 | IFF_LINK1 | IFF_LINK2)) 392 ifr.ifr_flags &= SLMASK; 393 ifr.ifr_flags |= slflags; 394 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) { 395 syslog(LOG_ERR, "ioctl (SIOCSIFFLAGS): %m"); 396 exit_handler(1); 397 } 398 close(s); 399 } 400 401 /* configure the interface, e.g. by passing the unit number to a script. */ 402 static void 403 configure_network(void) 404 { 405 int new_unit; 406 407 /* find out what unit number we were assigned */ 408 if (ioctl(fd, SLIOCGUNIT, (caddr_t)&new_unit) < 0) { 409 syslog(LOG_ERR, "ioctl(SLIOCGUNIT): %m"); 410 exit_handler(1); 411 } 412 /* iff the unit number changes either invoke config_cmd or punt. */ 413 if (config_cmd) { 414 char *s; 415 s = (char*) malloc(strlen(config_cmd) + 32); 416 if (s == NULL) { 417 syslog(LOG_ERR, "malloc failed"); 418 exit(1); 419 } 420 sprintf (s, "%s %d %d", config_cmd, unit, new_unit); 421 syslog(LOG_NOTICE, "configuring %s (sl%d):", dev, unit); 422 syslog(LOG_NOTICE, " '%s'", s); 423 system(s); 424 free (s); 425 unit = new_unit; 426 } else { 427 /* don't compare unit numbers if this is the first time to attach. */ 428 if (unit < 0) 429 unit = new_unit; 430 if (new_unit != unit) { 431 syslog(LOG_ERR, 432 "slip unit changed from sl%d to sl%d, but no -u CMD was specified!", 433 unit, new_unit); 434 exit_handler(1); 435 } 436 syslog(LOG_NOTICE,"sl%d connected to %s at %d baud",unit,dev,speed); 437 } 438 } 439 440 /* sighup_handler() is invoked when carrier drops, eg. before redial. */ 441 static void 442 sighup_handler(int signo __unused) 443 { 444 if(exiting) return; 445 446 if (redial_cmd == NULL) { 447 syslog(LOG_NOTICE,"SIGHUP on %s (sl%d); exiting", dev, unit); 448 exit_handler(1); 449 } 450 again: 451 /* invoke a shell for redial_cmd or punt. */ 452 if (*redial_cmd) { /* Non-empty redial command */ 453 syslog(LOG_NOTICE,"SIGHUP on %s (sl%d); running '%s'", 454 dev, unit, redial_cmd); 455 acquire_line(); /* reopen dead line */ 456 setup_line(CLOCAL); 457 if (locked) { 458 if (uucp_lock) 459 uu_unlock(dvname); /* for redial */ 460 locked = 0; 461 } 462 if (system(redial_cmd)) 463 goto again; 464 if (uucp_lock) { 465 int res; 466 if ((res = uu_lock(dvname)) != UU_LOCK_OK) { 467 if (res != UU_LOCK_INUSE) 468 syslog(LOG_ERR, "uu_lock: %s", uu_lockerr(res)); 469 syslog(LOG_ERR, "can't relock %s after %s, aborting", 470 dev, redial_cmd); 471 exit_handler(1); 472 } 473 locked = 1; 474 } 475 /* Now check again for carrier (dial command is done): */ 476 if (!(modem_control & CLOCAL)) { 477 tty.c_cflag &= ~CLOCAL; 478 if (tcsetattr(fd, TCSAFLUSH, &tty) < 0) { 479 syslog(LOG_ERR, "tcsetattr(TCSAFLUSH): %m"); 480 exit_handler(1); 481 } 482 ioctl(fd, TIOCMGET, &comstate); 483 if (!(comstate & TIOCM_CD)) { /* check for carrier */ 484 /* force a redial if no carrier */ 485 goto again; 486 } 487 } else 488 setup_line(0); 489 } else { /* Empty redial command */ 490 syslog(LOG_NOTICE,"SIGHUP on %s (sl%d); reestablish connection", 491 dev, unit); 492 acquire_line(); /* reopen dead line */ 493 setup_line(0); /* restore ospeed from hangup (B0) */ 494 /* If modem control, just wait for carrier before attaching. 495 If no modem control, just fall through immediately. */ 496 if (!(modem_control & CLOCAL)) { 497 int carrier = 0; 498 499 syslog(LOG_NOTICE, "waiting for carrier on %s (sl%d)", 500 dev, unit); 501 /* Now wait for carrier before attaching line. */ 502 /* We must poll since CLOCAL prevents signal. */ 503 while (! carrier) { 504 sleep(2); 505 ioctl(fd, TIOCMGET, &comstate); 506 if (comstate & TIOCM_CD) 507 carrier = 1; 508 } 509 syslog(LOG_NOTICE, "carrier now present on %s (sl%d)", 510 dev, unit); 511 } 512 } 513 slip_discipline(); 514 configure_network(); 515 } 516 517 /* Signal handler for SIGINT. We just log and exit. */ 518 static void 519 sigint_handler(int signo __unused) 520 { 521 if(exiting) return; 522 syslog(LOG_NOTICE,"SIGINT on %s (sl%d); exiting",dev,unit); 523 exit_handler(0); 524 } 525 526 /* Signal handler for SIGURG. */ 527 static void 528 sigurg_handler(int signo __unused) 529 { 530 int ttydisc = TTYDISC; 531 532 signal(SIGURG, SIG_IGN); 533 if(exiting) return; 534 syslog(LOG_NOTICE,"SIGURG on %s (sl%d); hangup",dev,unit); 535 if (ioctl(fd, TIOCSETD, &ttydisc) < 0) { 536 syslog(LOG_ERR, "ioctl(TIOCSETD): %m"); 537 exit_handler(1); 538 } 539 cfsetospeed(&tty, B0); 540 if (tcsetattr(fd, TCSANOW, &tty) < 0) { 541 syslog(LOG_ERR, "tcsetattr(TCSANOW): %m"); 542 exit_handler(1); 543 } 544 /* Need to go to sighup handler in any case */ 545 if (modem_control & CLOCAL) 546 kill (getpid(), SIGHUP); 547 548 } 549 550 /* Signal handler for SIGTERM. We just log and exit. */ 551 static void 552 sigterm_handler(int signo __unused) 553 { 554 if(exiting) return; 555 syslog(LOG_NOTICE,"SIGTERM on %s (sl%d); exiting",dev,unit); 556 exit_handler(0); 557 } 558 559 /* Run config_cmd if specified before exiting. */ 560 static void 561 exit_handler(int ret) 562 { 563 if(exiting) return; 564 exiting = 1; 565 /* 566 * First close the slip line in case exit_cmd wants it (like to hang 567 * up a modem or something). 568 */ 569 if (fd != -1) 570 close(fd); 571 if (uucp_lock && locked) 572 uu_unlock(dvname); 573 574 /* Remove the PID file */ 575 unlink(pidfilename); 576 577 if (config_cmd) { 578 char *s; 579 s = (char*) malloc(strlen(config_cmd) + 32); 580 if (s == NULL) { 581 syslog(LOG_ERR, "malloc failed"); 582 exit(1); 583 } 584 sprintf (s, "%s %d -1", config_cmd, unit); 585 syslog(LOG_NOTICE, "deconfiguring %s (sl%d):", dev, unit); 586 syslog(LOG_NOTICE, " '%s'", s); 587 system(s); 588 free (s); 589 } 590 /* invoke a shell for exit_cmd. */ 591 if (exit_cmd) { 592 syslog(LOG_NOTICE,"exiting after running %s", exit_cmd); 593 system(exit_cmd); 594 } 595 exit(ret); 596 } 597 598 /* local variables: */ 599 /* c-indent-level: 8 */ 600 /* c-argdecl-indent: 0 */ 601 /* c-label-offset: -8 */ 602 /* c-continued-statement-offset: 8 */ 603 /* c-brace-offset: 0 */ 604 /* comment-column: 32 */ 605 /* end: */ 606