1994-05-26 06:35:07 +00:00
|
|
|
/*
|
1994-08-23 08:07:17 +00:00
|
|
|
* Copyright (c) 1988 Regents of the University of California.
|
|
|
|
* All rights reserved.
|
1994-05-26 06:35:07 +00:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Rick Adams.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1999-05-29 08:16:34 +00:00
|
|
|
static const char copyright[] =
|
1994-08-23 08:07:17 +00:00
|
|
|
"@(#) Copyright (c) 1988 Regents of the University of California.\n\
|
|
|
|
All rights reserved.\n";
|
1994-05-26 06:35:07 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1999-05-29 08:16:34 +00:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "from: @(#)slattach.c 4.6 (Berkeley) 6/1/90";
|
|
|
|
#endif
|
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1994-05-26 06:35:07 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1996-12-10 21:25:42 +00:00
|
|
|
#include <sys/types.h>
|
1994-08-23 08:07:17 +00:00
|
|
|
#include <sys/ioctl.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <sys/socket.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
|
1997-12-21 13:42:27 +00:00
|
|
|
#include <err.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <fcntl.h>
|
1997-12-21 13:42:27 +00:00
|
|
|
#include <libutil.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <paths.h>
|
1994-08-23 08:07:17 +00:00
|
|
|
#include <signal.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2003-01-08 06:43:27 +00:00
|
|
|
#include <string.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
#include <syslog.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/slip.h>
|
1994-08-23 08:07:17 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
#define DEFAULT_BAUD 9600
|
1994-08-23 08:07:17 +00:00
|
|
|
|
|
|
|
void sighup_handler(); /* SIGHUP handler */
|
|
|
|
void sigint_handler(); /* SIGINT handler */
|
|
|
|
void sigterm_handler(); /* SIGTERM handler */
|
1995-09-18 13:33:30 +00:00
|
|
|
void sigurg_handler(); /* SIGURG handler */
|
1994-08-23 08:07:17 +00:00
|
|
|
void exit_handler(int ret); /* run exit_cmd iff specified upon exit. */
|
1994-08-23 08:28:32 +00:00
|
|
|
void setup_line(int cflag); /* configure terminal settings */
|
|
|
|
void slip_discipline(); /* switch to slip line discipline */
|
|
|
|
void configure_network(); /* configure slip interface */
|
1999-05-29 08:16:34 +00:00
|
|
|
void acquire_line(); /* get tty device as controlling terminal */
|
2002-03-21 13:20:49 +00:00
|
|
|
static void usage(void);
|
1994-08-23 08:07:17 +00:00
|
|
|
|
|
|
|
int fd = -1;
|
1994-08-23 08:28:32 +00:00
|
|
|
char *dev = (char *)0; /* path name of the tty (e.g. /dev/tty01) */
|
1995-09-19 03:27:31 +00:00
|
|
|
char *dvname; /* basename of dev */
|
1995-09-20 04:53:08 +00:00
|
|
|
int locked = 0; /* uucp lock active */
|
1994-08-23 08:07:17 +00:00
|
|
|
int flow_control = 0; /* non-zero to enable hardware flow control. */
|
1994-11-14 20:17:36 +00:00
|
|
|
int modem_control = HUPCL; /* !CLOCAL+HUPCL iff we watch carrier. */
|
1994-08-23 08:07:17 +00:00
|
|
|
int comstate; /* TIOCMGET current state of serial driver */
|
|
|
|
int redial_on_startup = 0; /* iff non-zero execute redial_cmd on startup */
|
1995-09-18 13:33:30 +00:00
|
|
|
speed_t speed = DEFAULT_BAUD; /* baud rate of tty */
|
1994-08-23 08:07:17 +00:00
|
|
|
int slflags = 0; /* compression flags */
|
|
|
|
int unit = -1; /* slip device unit number */
|
2002-05-12 00:22:38 +00:00
|
|
|
int foreground = 0; /* act as daemon if zero, else don't fork. */
|
1995-09-18 13:33:30 +00:00
|
|
|
int keepal = 0; /* keepalive timeout */
|
|
|
|
int outfill = 0; /* outfill timeout */
|
|
|
|
int sl_unit = -1; /* unit number */
|
1995-09-20 04:53:08 +00:00
|
|
|
int uucp_lock = 0; /* do uucp locking */
|
1999-05-29 08:16:34 +00:00
|
|
|
int exiting = 0; /* already running exit_handler */
|
1994-08-23 08:07:17 +00:00
|
|
|
|
1994-08-23 08:28:32 +00:00
|
|
|
struct termios tty; /* tty configuration/state */
|
1994-05-26 06:35:07 +00:00
|
|
|
|
1994-08-23 08:28:32 +00:00
|
|
|
char tty_path[32]; /* path name of the tty (e.g. /dev/tty01) */
|
1995-03-12 15:04:18 +00:00
|
|
|
char pidfilename[40]; /* e.g. /var/run/slattach.tty01.pid */
|
1994-08-23 08:07:17 +00:00
|
|
|
char *redial_cmd = 0; /* command to exec upon shutdown. */
|
|
|
|
char *config_cmd = 0; /* command to exec if slip unit changes. */
|
|
|
|
char *exit_cmd = 0; /* command to exec before exiting. */
|
|
|
|
|
1997-06-19 14:33:42 +00:00
|
|
|
static void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s\n%s\n",
|
|
|
|
"usage: slattach [-acfhlnz] [-e command] [-r command] [-s speed] [-u command]",
|
|
|
|
" [-L] [-K timeout] [-O timeout] [-S unit] device");
|
|
|
|
/* do not exit here */
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
1994-08-23 08:07:17 +00:00
|
|
|
int option;
|
|
|
|
|
1997-03-29 03:33:12 +00:00
|
|
|
while ((option = getopt(argc, argv, "ace:fhlnr:s:u:zLK:O:S:")) != -1) {
|
1994-08-23 08:07:17 +00:00
|
|
|
switch (option) {
|
|
|
|
case 'a':
|
1994-08-23 08:28:32 +00:00
|
|
|
slflags |= IFF_LINK2;
|
|
|
|
slflags &= ~IFF_LINK0;
|
1994-08-23 08:07:17 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
1994-08-23 08:28:32 +00:00
|
|
|
slflags |= IFF_LINK0;
|
|
|
|
slflags &= ~IFF_LINK2;
|
1994-08-23 08:07:17 +00:00
|
|
|
break;
|
|
|
|
case 'e':
|
2003-01-08 06:43:27 +00:00
|
|
|
exit_cmd = strdup (optarg);
|
1994-08-23 08:07:17 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
foreground = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
flow_control |= CRTSCTS;
|
|
|
|
break;
|
|
|
|
case 'l':
|
1994-11-14 20:17:36 +00:00
|
|
|
modem_control = CLOCAL; /* clear HUPCL too */
|
1994-08-23 08:07:17 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
1994-08-23 08:28:32 +00:00
|
|
|
slflags |= IFF_LINK1;
|
1994-08-23 08:07:17 +00:00
|
|
|
break;
|
|
|
|
case 'r':
|
2003-01-08 06:43:27 +00:00
|
|
|
redial_cmd = strdup (optarg);
|
1994-08-23 08:07:17 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
speed = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'u':
|
2003-01-08 06:43:27 +00:00
|
|
|
config_cmd = strdup (optarg);
|
1994-08-23 08:07:17 +00:00
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
redial_on_startup = 1;
|
|
|
|
break;
|
1995-09-20 04:53:08 +00:00
|
|
|
case 'L':
|
|
|
|
uucp_lock = 1;
|
|
|
|
break;
|
1995-09-18 13:33:30 +00:00
|
|
|
case 'K':
|
|
|
|
keepal = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
outfill = atoi(optarg);
|
|
|
|
break;
|
1995-09-18 14:06:38 +00:00
|
|
|
case 'S':
|
1995-09-18 13:33:30 +00:00
|
|
|
sl_unit = atoi(optarg);
|
|
|
|
break;
|
1994-08-23 08:07:17 +00:00
|
|
|
case '?':
|
1997-06-19 14:33:42 +00:00
|
|
|
default:
|
|
|
|
usage();
|
1994-08-23 08:07:17 +00:00
|
|
|
exit_handler(1);
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1994-08-23 08:07:17 +00:00
|
|
|
|
|
|
|
if (optind == argc - 1)
|
|
|
|
dev = argv[optind];
|
|
|
|
|
1997-06-19 14:33:42 +00:00
|
|
|
if (optind < (argc - 1))
|
|
|
|
warnx("too many args, first='%s'", argv[optind]);
|
|
|
|
if (optind > (argc - 1))
|
|
|
|
warnx("not enough args");
|
1994-08-23 08:07:17 +00:00
|
|
|
if (dev == (char *)0) {
|
1997-06-19 14:33:42 +00:00
|
|
|
usage();
|
1994-08-23 08:07:17 +00:00
|
|
|
exit_handler(2);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
|
1994-08-23 08:28:32 +00:00
|
|
|
strcpy(tty_path, _PATH_DEV);
|
|
|
|
strcat(tty_path, "/");
|
|
|
|
strncat(tty_path, dev, 10);
|
|
|
|
dev = tty_path;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1995-09-19 03:27:31 +00:00
|
|
|
dvname = strrchr(dev, '/'); /* always succeeds */
|
|
|
|
dvname++; /* trailing tty pathname component */
|
1999-06-06 07:18:49 +00:00
|
|
|
snprintf(pidfilename, sizeof(pidfilename),
|
|
|
|
"%sslattach.%s.pid", _PATH_VARRUN, dvname);
|
1995-03-12 15:04:18 +00:00
|
|
|
printf("%s\n",pidfilename);
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1994-08-23 08:07:17 +00:00
|
|
|
if (!foreground)
|
|
|
|
daemon(0,0); /* fork, setsid, chdir /, and close std*. */
|
1994-08-23 08:28:32 +00:00
|
|
|
/* daemon() closed stderr, so log errors from here on. */
|
|
|
|
openlog("slattach",LOG_CONS|LOG_PID,LOG_DAEMON);
|
1994-08-23 08:07:17 +00:00
|
|
|
|
1999-05-29 08:16:34 +00:00
|
|
|
acquire_line(); /* get tty device as controlling terminal */
|
1994-08-23 08:28:32 +00:00
|
|
|
setup_line(0); /* configure for slip line discipline */
|
|
|
|
slip_discipline(); /* switch to slip line discipline */
|
1994-08-23 08:07:17 +00:00
|
|
|
|
|
|
|
/* upon INT log a timestamp and exit. */
|
1998-02-20 07:28:50 +00:00
|
|
|
if (signal(SIGINT,sigint_handler) == SIG_ERR)
|
1994-11-01 08:34:32 +00:00
|
|
|
syslog(LOG_NOTICE,"cannot install SIGINT handler: %m");
|
1994-08-23 08:07:17 +00:00
|
|
|
/* upon TERM log a timestamp and exit. */
|
1998-02-20 07:28:50 +00:00
|
|
|
if (signal(SIGTERM,sigterm_handler) == SIG_ERR)
|
1994-11-01 08:34:32 +00:00
|
|
|
syslog(LOG_NOTICE,"cannot install SIGTERM handler: %m");
|
1994-08-23 08:07:17 +00:00
|
|
|
/* upon HUP redial and reconnect. */
|
1998-02-20 07:28:50 +00:00
|
|
|
if (signal(SIGHUP,sighup_handler) == SIG_ERR)
|
1994-11-01 08:34:32 +00:00
|
|
|
syslog(LOG_NOTICE,"cannot install SIGHUP handler: %m");
|
1994-08-23 08:07:17 +00:00
|
|
|
|
|
|
|
if (redial_on_startup)
|
|
|
|
sighup_handler();
|
1994-08-23 08:28:32 +00:00
|
|
|
else if (!(modem_control & CLOCAL)) {
|
1995-02-25 18:06:23 +00:00
|
|
|
if (ioctl(fd, TIOCMGET, &comstate) < 0)
|
|
|
|
syslog(LOG_NOTICE,"cannot get carrier state: %m");
|
1994-08-23 08:07:17 +00:00
|
|
|
if (!(comstate & TIOCM_CD)) { /* check for carrier */
|
|
|
|
/* force a redial if no carrier */
|
|
|
|
kill (getpid(), SIGHUP);
|
1995-02-25 18:06:23 +00:00
|
|
|
} else
|
|
|
|
configure_network();
|
1996-03-12 23:14:45 +00:00
|
|
|
} else
|
1994-08-23 08:28:32 +00:00
|
|
|
configure_network(); /* configure the network if needed. */
|
|
|
|
|
1994-08-23 08:07:17 +00:00
|
|
|
for (;;) {
|
1994-08-23 08:28:32 +00:00
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
1994-08-23 08:07:17 +00:00
|
|
|
sigsuspend(&mask);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1994-08-23 08:07:17 +00:00
|
|
|
}
|
|
|
|
|
1994-08-23 08:28:32 +00:00
|
|
|
/* Close all FDs, fork, reopen tty port as 0-2, and make it the
|
|
|
|
controlling terminal for our process group. */
|
|
|
|
void acquire_line()
|
|
|
|
{
|
|
|
|
int ttydisc = TTYDISC;
|
1995-09-20 12:56:25 +00:00
|
|
|
int oflags;
|
1995-03-12 15:04:18 +00:00
|
|
|
FILE *pidfile;
|
1994-08-23 08:28:32 +00:00
|
|
|
|
1995-09-20 12:56:25 +00:00
|
|
|
/* reset to tty discipline */
|
|
|
|
if (fd >= 0 && ioctl(fd, TIOCSETD, &ttydisc) < 0) {
|
1995-09-19 03:27:31 +00:00
|
|
|
syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
1994-08-23 08:28:32 +00:00
|
|
|
|
|
|
|
(void)close(STDIN_FILENO); /* close FDs before forking. */
|
|
|
|
(void)close(STDOUT_FILENO);
|
|
|
|
(void)close(STDERR_FILENO);
|
|
|
|
if (fd > 2)
|
|
|
|
(void)close(fd);
|
|
|
|
|
|
|
|
signal(SIGHUP, SIG_IGN); /* ignore HUP signal when parent dies. */
|
1995-09-19 03:27:31 +00:00
|
|
|
if (daemon(0,0)) { /* fork, setsid, chdir /, and close std*. */
|
|
|
|
syslog(LOG_ERR, "daemon(0,0): %m");
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
1994-08-23 08:28:32 +00:00
|
|
|
|
|
|
|
while (getppid () != 1)
|
|
|
|
sleep (1); /* Wait for parent to die. */
|
|
|
|
|
1995-03-12 15:04:18 +00:00
|
|
|
/* create PID file */
|
1995-03-12 23:37:28 +00:00
|
|
|
if((pidfile = fopen(pidfilename, "w"))) {
|
1998-06-28 20:33:36 +00:00
|
|
|
fprintf(pidfile, "%ld\n", (long)getpid());
|
1995-03-12 15:04:18 +00:00
|
|
|
fclose(pidfile);
|
|
|
|
}
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1998-02-20 07:28:50 +00:00
|
|
|
if (signal(SIGHUP,sighup_handler) == SIG_ERR) /* Re-enable HUP signal */
|
1994-11-01 08:34:32 +00:00
|
|
|
syslog(LOG_NOTICE,"cannot install SIGHUP handler: %m");
|
1994-08-23 08:28:32 +00:00
|
|
|
|
1995-09-20 04:53:08 +00:00
|
|
|
if (uucp_lock) {
|
|
|
|
/* unlock not needed here, always re-lock with new pid */
|
1997-03-31 22:51:00 +00:00
|
|
|
int res;
|
|
|
|
if ((res = uu_lock(dvname)) != UU_LOCK_OK) {
|
|
|
|
if (res != UU_LOCK_INUSE)
|
|
|
|
syslog(LOG_ERR, "uu_lock: %s", uu_lockerr(res));
|
1995-09-20 04:53:08 +00:00
|
|
|
syslog(LOG_ERR, "can't lock %s", dev);
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
|
|
|
locked = 1;
|
1995-09-19 03:27:31 +00:00
|
|
|
}
|
|
|
|
|
1994-08-23 08:28:32 +00:00
|
|
|
if ((fd = open(dev, O_RDWR | O_NONBLOCK, 0)) < 0) {
|
1995-09-19 03:27:31 +00:00
|
|
|
syslog(LOG_ERR, "open(%s) %m", dev);
|
1994-08-23 08:28:32 +00:00
|
|
|
exit_handler(1);
|
|
|
|
}
|
1995-09-20 12:56:25 +00:00
|
|
|
/* Turn off O_NONBLOCK for dumb redialers, if any. */
|
|
|
|
if ((oflags = fcntl(fd, F_GETFL)) == -1) {
|
|
|
|
syslog(LOG_ERR, "fcntl(F_GETFL) failed: %m");
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
|
|
|
if (fcntl(fd, F_SETFL, oflags & ~O_NONBLOCK) == -1) {
|
|
|
|
syslog(LOG_ERR, "fcntl(F_SETFL) failed: %m");
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
1994-08-23 08:28:32 +00:00
|
|
|
(void)dup2(fd, STDIN_FILENO);
|
|
|
|
(void)dup2(fd, STDOUT_FILENO);
|
|
|
|
(void)dup2(fd, STDERR_FILENO);
|
|
|
|
if (fd > 2)
|
|
|
|
(void)close (fd);
|
|
|
|
fd = STDIN_FILENO;
|
|
|
|
|
1999-05-29 08:16:34 +00:00
|
|
|
/* acquire the serial line as a controlling terminal. */
|
1995-09-19 03:27:31 +00:00
|
|
|
if (ioctl(fd, TIOCSCTTY, 0) < 0) {
|
|
|
|
syslog(LOG_ERR,"ioctl(TIOCSCTTY): %m");
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
1994-08-23 08:28:32 +00:00
|
|
|
/* Make us the foreground process group associated with the
|
|
|
|
slip line which is our controlling terminal. */
|
|
|
|
if (tcsetpgrp(fd, getpid()) < 0)
|
1994-11-01 08:34:32 +00:00
|
|
|
syslog(LOG_NOTICE,"tcsetpgrp failed: %m");
|
1994-08-23 08:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the tty flags and set DTR. */
|
|
|
|
/* Call as setup_line(CLOCAL) to force clocal assertion. */
|
|
|
|
void setup_line(int cflag)
|
1994-08-23 08:07:17 +00:00
|
|
|
{
|
|
|
|
tty.c_lflag = tty.c_iflag = tty.c_oflag = 0;
|
1994-08-23 08:28:32 +00:00
|
|
|
tty.c_cflag = CREAD | CS8 | flow_control | modem_control | cflag;
|
1995-09-20 12:56:25 +00:00
|
|
|
cfsetispeed(&tty, speed);
|
|
|
|
cfsetospeed(&tty, speed);
|
1994-08-23 08:07:17 +00:00
|
|
|
/* set the line speed and flow control */
|
|
|
|
if (tcsetattr(fd, TCSAFLUSH, &tty) < 0) {
|
1994-08-23 08:28:32 +00:00
|
|
|
syslog(LOG_ERR, "tcsetattr(TCSAFLUSH): %m");
|
1994-08-23 08:07:17 +00:00
|
|
|
exit_handler(1);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1994-08-23 08:07:17 +00:00
|
|
|
/* set data terminal ready */
|
|
|
|
if (ioctl(fd, TIOCSDTR) < 0) {
|
1994-08-23 08:28:32 +00:00
|
|
|
syslog(LOG_ERR, "ioctl(TIOCSDTR): %m");
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put the line in slip discipline. */
|
|
|
|
void slip_discipline()
|
|
|
|
{
|
1994-09-14 05:40:56 +00:00
|
|
|
struct ifreq ifr;
|
1994-08-23 08:28:32 +00:00
|
|
|
int slipdisc = SLIPDISC;
|
1994-09-14 05:40:56 +00:00
|
|
|
int s, tmp_unit = -1;
|
1994-08-23 08:28:32 +00:00
|
|
|
|
1994-08-23 08:07:17 +00:00
|
|
|
/* Switch to slip line discipline. */
|
1994-05-26 06:35:07 +00:00
|
|
|
if (ioctl(fd, TIOCSETD, &slipdisc) < 0) {
|
1994-08-23 08:28:32 +00:00
|
|
|
syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
|
1994-08-23 08:07:17 +00:00
|
|
|
exit_handler(1);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1994-09-14 05:40:56 +00:00
|
|
|
|
1995-09-18 13:33:30 +00:00
|
|
|
if (sl_unit >= 0 && ioctl(fd, SLIOCSUNIT, &sl_unit) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(SLIOCSUNIT): %m");
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
|
|
|
|
1994-09-14 05:40:56 +00:00
|
|
|
/* find out what unit number we were assigned */
|
|
|
|
if (ioctl(fd, SLIOCGUNIT, (caddr_t)&tmp_unit) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(SLIOCGUNIT): %m");
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp_unit < 0) {
|
|
|
|
syslog(LOG_ERR, "bad unit (%d) from ioctl(SLIOCGUNIT)",tmp_unit);
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
|
|
|
|
1995-09-18 13:33:30 +00:00
|
|
|
if (keepal > 0) {
|
|
|
|
signal(SIGURG, sigurg_handler);
|
|
|
|
if (ioctl(fd, SLIOCSKEEPAL, &keepal) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(SLIOCSKEEPAL): %m");
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (outfill > 0 && ioctl(fd, SLIOCSOUTFILL, &outfill) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(SLIOCSOUTFILL): %m");
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
|
|
|
|
1994-09-14 05:40:56 +00:00
|
|
|
/* open a socket as the handle to the interface */
|
|
|
|
s = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0) {
|
|
|
|
syslog(LOG_ERR, "socket: %m");
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
|
|
|
sprintf(ifr.ifr_name, "sl%d", tmp_unit);
|
|
|
|
|
|
|
|
/* get the flags for the interface */
|
|
|
|
if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
|
|
|
|
1994-08-23 08:07:17 +00:00
|
|
|
/* Assert any compression or no-icmp flags. */
|
1994-09-14 05:40:56 +00:00
|
|
|
#define SLMASK (~(IFF_LINK0 | IFF_LINK1 | IFF_LINK2))
|
|
|
|
ifr.ifr_flags &= SLMASK;
|
|
|
|
ifr.ifr_flags |= slflags;
|
|
|
|
if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl (SIOCSIFFLAGS): %m");
|
1994-08-23 08:07:17 +00:00
|
|
|
exit_handler(1);
|
|
|
|
}
|
1994-09-14 05:40:56 +00:00
|
|
|
close(s);
|
1994-08-23 08:07:17 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
|
1999-05-29 08:16:34 +00:00
|
|
|
/* configure the interface, e.g. by passing the unit number to a script. */
|
1994-08-23 08:28:32 +00:00
|
|
|
void configure_network()
|
1994-08-23 08:07:17 +00:00
|
|
|
{
|
|
|
|
int new_unit;
|
|
|
|
|
|
|
|
/* find out what unit number we were assigned */
|
|
|
|
if (ioctl(fd, SLIOCGUNIT, (caddr_t)&new_unit) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(SLIOCGUNIT): %m");
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
|
|
|
/* iff the unit number changes either invoke config_cmd or punt. */
|
|
|
|
if (config_cmd) {
|
|
|
|
char *s;
|
|
|
|
s = (char*) malloc(strlen(config_cmd) + 32);
|
1999-05-29 08:16:34 +00:00
|
|
|
if (s == NULL) {
|
|
|
|
syslog(LOG_ERR, "malloc failed");
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-08-23 08:07:17 +00:00
|
|
|
sprintf (s, "%s %d %d", config_cmd, unit, new_unit);
|
1999-05-29 08:16:34 +00:00
|
|
|
syslog(LOG_NOTICE, "configuring %s (sl%d):", dev, unit);
|
1994-08-23 08:07:17 +00:00
|
|
|
syslog(LOG_NOTICE, " '%s'", s);
|
|
|
|
system(s);
|
|
|
|
free (s);
|
|
|
|
unit = new_unit;
|
|
|
|
} else {
|
1996-02-17 19:21:41 +00:00
|
|
|
/* don't compare unit numbers if this is the first time to attach. */
|
|
|
|
if (unit < 0)
|
|
|
|
unit = new_unit;
|
1994-08-23 08:07:17 +00:00
|
|
|
if (new_unit != unit) {
|
1998-08-02 16:06:35 +00:00
|
|
|
syslog(LOG_ERR,
|
|
|
|
"slip unit changed from sl%d to sl%d, but no -u CMD was specified!",
|
|
|
|
unit, new_unit);
|
1994-08-23 08:07:17 +00:00
|
|
|
exit_handler(1);
|
|
|
|
}
|
|
|
|
syslog(LOG_NOTICE,"sl%d connected to %s at %d baud",unit,dev,speed);
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
1995-09-20 12:56:25 +00:00
|
|
|
/* sighup_handler() is invoked when carrier drops, eg. before redial. */
|
1994-08-23 08:07:17 +00:00
|
|
|
void sighup_handler()
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
1994-08-23 08:07:17 +00:00
|
|
|
if(exiting) return;
|
1996-03-12 12:56:27 +00:00
|
|
|
|
1996-03-12 23:14:45 +00:00
|
|
|
if (redial_cmd == NULL) {
|
|
|
|
syslog(LOG_NOTICE,"SIGHUP on %s (sl%d); exiting", dev, unit);
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
|
|
|
again:
|
1994-08-23 08:07:17 +00:00
|
|
|
/* invoke a shell for redial_cmd or punt. */
|
1999-05-29 08:16:34 +00:00
|
|
|
if (*redial_cmd) { /* Non-empty redial command */
|
1996-03-12 23:14:45 +00:00
|
|
|
syslog(LOG_NOTICE,"SIGHUP on %s (sl%d); running '%s'",
|
|
|
|
dev, unit, redial_cmd);
|
|
|
|
acquire_line(); /* reopen dead line */
|
1994-08-23 08:28:32 +00:00
|
|
|
setup_line(CLOCAL);
|
1995-09-20 04:53:08 +00:00
|
|
|
if (locked) {
|
|
|
|
if (uucp_lock)
|
|
|
|
uu_unlock(dvname); /* for redial */
|
|
|
|
locked = 0;
|
|
|
|
}
|
1994-11-14 20:17:36 +00:00
|
|
|
if (system(redial_cmd))
|
|
|
|
goto again;
|
1995-09-20 04:53:08 +00:00
|
|
|
if (uucp_lock) {
|
1997-03-31 22:51:00 +00:00
|
|
|
int res;
|
|
|
|
if ((res = uu_lock(dvname)) != UU_LOCK_OK) {
|
|
|
|
if (res != UU_LOCK_INUSE)
|
|
|
|
syslog(LOG_ERR, "uu_lock: %s", uu_lockerr(res));
|
1995-09-20 04:53:08 +00:00
|
|
|
syslog(LOG_ERR, "can't relock %s after %s, aborting",
|
|
|
|
dev, redial_cmd);
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
|
|
|
locked = 1;
|
1995-09-19 09:07:51 +00:00
|
|
|
}
|
1994-08-23 08:07:17 +00:00
|
|
|
/* Now check again for carrier (dial command is done): */
|
|
|
|
if (!(modem_control & CLOCAL)) {
|
|
|
|
tty.c_cflag &= ~CLOCAL;
|
|
|
|
if (tcsetattr(fd, TCSAFLUSH, &tty) < 0) {
|
|
|
|
syslog(LOG_ERR, "tcsetattr(TCSAFLUSH): %m");
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
|
|
|
ioctl(fd, TIOCMGET, &comstate);
|
|
|
|
if (!(comstate & TIOCM_CD)) { /* check for carrier */
|
|
|
|
/* force a redial if no carrier */
|
|
|
|
goto again;
|
|
|
|
}
|
1995-09-18 13:33:30 +00:00
|
|
|
} else
|
|
|
|
setup_line(0);
|
1996-03-12 23:14:45 +00:00
|
|
|
} else { /* Empty redial command */
|
|
|
|
syslog(LOG_NOTICE,"SIGHUP on %s (sl%d); reestablish connection",
|
|
|
|
dev, unit);
|
|
|
|
acquire_line(); /* reopen dead line */
|
1995-09-18 13:33:30 +00:00
|
|
|
setup_line(0); /* restore ospeed from hangup (B0) */
|
1994-08-23 08:28:32 +00:00
|
|
|
/* If modem control, just wait for carrier before attaching.
|
|
|
|
If no modem control, just fall through immediately. */
|
1994-08-23 08:07:17 +00:00
|
|
|
if (!(modem_control & CLOCAL)) {
|
1994-08-23 08:28:32 +00:00
|
|
|
int carrier = 0;
|
1996-03-12 23:14:45 +00:00
|
|
|
|
1999-05-29 08:16:34 +00:00
|
|
|
syslog(LOG_NOTICE, "waiting for carrier on %s (sl%d)",
|
1994-08-23 08:28:32 +00:00
|
|
|
dev, unit);
|
|
|
|
/* Now wait for carrier before attaching line. */
|
|
|
|
/* We must poll since CLOCAL prevents signal. */
|
|
|
|
while (! carrier) {
|
|
|
|
sleep(2);
|
|
|
|
ioctl(fd, TIOCMGET, &comstate);
|
|
|
|
if (comstate & TIOCM_CD)
|
|
|
|
carrier = 1;
|
1994-08-23 08:07:17 +00:00
|
|
|
}
|
1999-05-29 08:16:34 +00:00
|
|
|
syslog(LOG_NOTICE, "carrier now present on %s (sl%d)",
|
1994-08-23 08:28:32 +00:00
|
|
|
dev, unit);
|
1994-08-23 08:07:17 +00:00
|
|
|
}
|
|
|
|
}
|
1994-08-23 08:28:32 +00:00
|
|
|
slip_discipline();
|
|
|
|
configure_network();
|
1994-08-23 08:07:17 +00:00
|
|
|
}
|
|
|
|
/* Signal handler for SIGINT. We just log and exit. */
|
|
|
|
void sigint_handler()
|
|
|
|
{
|
|
|
|
if(exiting) return;
|
1994-08-23 08:28:32 +00:00
|
|
|
syslog(LOG_NOTICE,"SIGINT on %s (sl%d); exiting",dev,unit);
|
1994-08-23 08:07:17 +00:00
|
|
|
exit_handler(0);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1995-09-18 13:33:30 +00:00
|
|
|
/* Signal handler for SIGURG. */
|
|
|
|
void sigurg_handler()
|
|
|
|
{
|
|
|
|
int ttydisc = TTYDISC;
|
|
|
|
|
|
|
|
signal(SIGURG, SIG_IGN);
|
|
|
|
if(exiting) return;
|
|
|
|
syslog(LOG_NOTICE,"SIGURG on %s (sl%d); hangup",dev,unit);
|
|
|
|
if (ioctl(fd, TIOCSETD, &ttydisc) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
|
|
|
cfsetospeed(&tty, B0);
|
|
|
|
if (tcsetattr(fd, TCSANOW, &tty) < 0) {
|
|
|
|
syslog(LOG_ERR, "tcsetattr(TCSANOW): %m");
|
|
|
|
exit_handler(1);
|
|
|
|
}
|
|
|
|
/* Need to go to sighup handler in any case */
|
|
|
|
if (modem_control & CLOCAL)
|
|
|
|
kill (getpid(), SIGHUP);
|
|
|
|
|
|
|
|
}
|
1994-08-23 08:07:17 +00:00
|
|
|
/* Signal handler for SIGTERM. We just log and exit. */
|
|
|
|
void sigterm_handler()
|
|
|
|
{
|
|
|
|
if(exiting) return;
|
|
|
|
syslog(LOG_NOTICE,"SIGTERM on %s (sl%d); exiting",dev,unit);
|
|
|
|
exit_handler(0);
|
|
|
|
}
|
|
|
|
/* Run config_cmd if specified before exiting. */
|
|
|
|
void exit_handler(int ret)
|
|
|
|
{
|
|
|
|
if(exiting) return;
|
|
|
|
exiting = 1;
|
|
|
|
/*
|
|
|
|
* First close the slip line in case exit_cmd wants it (like to hang
|
|
|
|
* up a modem or something).
|
|
|
|
*/
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
1995-09-20 04:53:08 +00:00
|
|
|
if (uucp_lock && locked)
|
1995-09-19 03:27:31 +00:00
|
|
|
uu_unlock(dvname);
|
1995-03-12 15:04:18 +00:00
|
|
|
|
|
|
|
/* Remove the PID file */
|
|
|
|
(void)unlink(pidfilename);
|
|
|
|
|
1995-09-29 22:03:13 +00:00
|
|
|
if (config_cmd) {
|
|
|
|
char *s;
|
|
|
|
s = (char*) malloc(strlen(config_cmd) + 32);
|
1999-05-29 08:16:34 +00:00
|
|
|
if (s == NULL) {
|
|
|
|
syslog(LOG_ERR, "malloc failed");
|
|
|
|
exit(1);
|
|
|
|
}
|
1995-09-29 22:03:13 +00:00
|
|
|
sprintf (s, "%s %d -1", config_cmd, unit);
|
1999-05-29 08:16:34 +00:00
|
|
|
syslog(LOG_NOTICE, "deconfiguring %s (sl%d):", dev, unit);
|
1995-09-29 22:03:13 +00:00
|
|
|
syslog(LOG_NOTICE, " '%s'", s);
|
|
|
|
system(s);
|
|
|
|
free (s);
|
|
|
|
}
|
1994-08-23 08:07:17 +00:00
|
|
|
/* invoke a shell for exit_cmd. */
|
|
|
|
if (exit_cmd) {
|
|
|
|
syslog(LOG_NOTICE,"exiting after running %s", exit_cmd);
|
|
|
|
system(exit_cmd);
|
|
|
|
}
|
|
|
|
exit(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* local variables: */
|
|
|
|
/* c-indent-level: 8 */
|
|
|
|
/* c-argdecl-indent: 0 */
|
|
|
|
/* c-label-offset: -8 */
|
|
|
|
/* c-continued-statement-offset: 8 */
|
|
|
|
/* c-brace-offset: 0 */
|
|
|
|
/* comment-column: 32 */
|
|
|
|
/* end: */
|