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