#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <err.h>
#include <fcntl.h>
#include <libutil.h>
#include <paths.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <termios.h>
#include <unistd.h>
#include <net/if.h>
#include <net/slip.h>
#define DEFAULT_BAUD 115200
static void sighup_handler(int);
static void sigint_handler(int);
static void sigterm_handler(int);
static void sigurg_handler(int);
static void exit_handler(int);
static void setup_line(int);
static void slip_discipline(void);
static void configure_network(void);
static void acquire_line(void);
static void usage(void);
int fd = -1;
char *dev = NULL;
char *dvname;
int locked = 0;
int flow_control = 0;
int modem_control = HUPCL;
int comstate;
int redial_on_startup = 0;
speed_t speed = DEFAULT_BAUD;
int slflags = 0;
int unit = -1;
int foreground = 0;
int keepal = 0;
int outfill = 0;
int sl_unit = -1;
int uucp_lock = 0;
int exiting = 0;
struct termios tty;
char tty_path[32];
char pidfilename[40];
char *redial_cmd = NULL;
char *config_cmd = NULL;
char *exit_cmd = NULL;
static void
usage(void)
{
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");
}
int
main(int argc, char **argv)
{
int option;
while ((option = getopt(argc, argv, "ace:fhlnr:s:u:zLK:O:S:")) != -1) {
switch (option) {
case 'a':
slflags |= IFF_LINK2;
slflags &= ~IFF_LINK0;
break;
case 'c':
slflags |= IFF_LINK0;
slflags &= ~IFF_LINK2;
break;
case 'e':
exit_cmd = (char*) strdup (optarg);
break;
case 'f':
foreground = 1;
break;
case 'h':
flow_control |= CRTSCTS;
break;
case 'l':
modem_control = CLOCAL;
break;
case 'n':
slflags |= IFF_LINK1;
break;
case 'r':
redial_cmd = (char*) strdup (optarg);
break;
case 's':
speed = atoi(optarg);
break;
case 'u':
config_cmd = (char*) strdup (optarg);
break;
case 'z':
redial_on_startup = 1;
break;
case 'L':
uucp_lock = 1;
break;
case 'K':
keepal = atoi(optarg);
break;
case 'O':
outfill = atoi(optarg);
break;
case 'S':
sl_unit = atoi(optarg);
break;
case '?':
default:
usage();
exit_handler(1);
}
}
if (optind == argc - 1)
dev = argv[optind];
if (optind < (argc - 1))
warnx("too many args, first='%s'", argv[optind]);
if (optind > (argc - 1))
warnx("not enough args");
if (dev == NULL) {
usage();
exit_handler(2);
}
if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
strcpy(tty_path, _PATH_DEV);
strcat(tty_path, "/");
strncat(tty_path, dev, 10);
dev = tty_path;
}
dvname = strrchr(dev, '/');
dvname++;
snprintf(pidfilename, sizeof(pidfilename),
"%sslattach.%s.pid", _PATH_VARRUN, dvname);
printf("%s\n",pidfilename);
if (!foreground)
daemon(0,0);
openlog("slattach",LOG_CONS|LOG_PID,LOG_DAEMON);
acquire_line();
setup_line(0);
slip_discipline();
if (signal(SIGINT,sigint_handler) == SIG_ERR)
syslog(LOG_NOTICE,"cannot install SIGINT handler: %m");
if (signal(SIGTERM,sigterm_handler) == SIG_ERR)
syslog(LOG_NOTICE,"cannot install SIGTERM handler: %m");
if (signal(SIGHUP,sighup_handler) == SIG_ERR)
syslog(LOG_NOTICE,"cannot install SIGHUP handler: %m");
if (redial_on_startup)
sighup_handler(0);
else if (!(modem_control & CLOCAL)) {
if (ioctl(fd, TIOCMGET, &comstate) < 0)
syslog(LOG_NOTICE,"cannot get carrier state: %m");
if (!(comstate & TIOCM_CD)) {
kill (getpid(), SIGHUP);
} else
configure_network();
} else
configure_network();
for (;;) {
sigset_t mask;
sigemptyset(&mask);
sigsuspend(&mask);
}
}
static void
acquire_line(void)
{
int ttydisc = TTYDISC;
int oflags;
FILE *pid_file;
if (fd >= 0 && ioctl(fd, TIOCSETD, &ttydisc) < 0) {
syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
exit_handler(1);
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
if (fd > 2)
close(fd);
signal(SIGHUP, SIG_IGN);
if (daemon(0,0)) {
syslog(LOG_ERR, "daemon(0,0): %m");
exit_handler(1);
}
while (getppid () != 1)
sleep (1);
if((pid_file = fopen(pidfilename, "w"))) {
fprintf(pid_file, "%ld\n", (long)getpid());
fclose(pid_file);
}
if (signal(SIGHUP,sighup_handler) == SIG_ERR)
syslog(LOG_NOTICE,"cannot install SIGHUP handler: %m");
if (uucp_lock) {
int res;
if ((res = uu_lock(dvname)) != UU_LOCK_OK) {
if (res != UU_LOCK_INUSE)
syslog(LOG_ERR, "uu_lock: %s", uu_lockerr(res));
syslog(LOG_ERR, "can't lock %s", dev);
exit_handler(1);
}
locked = 1;
}
if ((fd = open(dev, O_RDWR | O_NONBLOCK, 0)) < 0) {
syslog(LOG_ERR, "open(%s) %m", dev);
exit_handler(1);
}
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);
}
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if (fd > 2)
close (fd);
fd = STDIN_FILENO;
if (ioctl(fd, TIOCSCTTY, 0) < 0) {
syslog(LOG_ERR,"ioctl(TIOCSCTTY): %m");
exit_handler(1);
}
if (tcsetpgrp(fd, getpid()) < 0)
syslog(LOG_NOTICE,"tcsetpgrp failed: %m");
}
static void
setup_line(int cflag)
{
tty.c_lflag = tty.c_iflag = tty.c_oflag = 0;
tty.c_cflag = CREAD | CS8 | flow_control | modem_control | cflag;
cfsetispeed(&tty, speed);
cfsetospeed(&tty, speed);
if (tcsetattr(fd, TCSAFLUSH, &tty) < 0) {
syslog(LOG_ERR, "tcsetattr(TCSAFLUSH): %m");
exit_handler(1);
}
if (ioctl(fd, TIOCSDTR) < 0) {
syslog(LOG_ERR, "ioctl(TIOCSDTR): %m");
exit_handler(1);
}
}
static void
slip_discipline(void)
{
struct ifreq ifr;
int slipdisc = SLIPDISC;
int s, tmp_unit = -1;
if (ioctl(fd, TIOCSETD, &slipdisc) < 0) {
syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
exit_handler(1);
}
if (sl_unit >= 0 && ioctl(fd, SLIOCSUNIT, &sl_unit) < 0) {
syslog(LOG_ERR, "ioctl(SLIOCSUNIT): %m");
exit_handler(1);
}
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);
}
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);
}
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);
if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
exit_handler(1);
}
#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");
exit_handler(1);
}
close(s);
}
static void
configure_network(void)
{
int new_unit;
if (ioctl(fd, SLIOCGUNIT, (caddr_t)&new_unit) < 0) {
syslog(LOG_ERR, "ioctl(SLIOCGUNIT): %m");
exit_handler(1);
}
if (config_cmd) {
char *s;
s = (char*) malloc(strlen(config_cmd) + 32);
if (s == NULL) {
syslog(LOG_ERR, "malloc failed");
exit(1);
}
sprintf (s, "%s %d %d", config_cmd, unit, new_unit);
syslog(LOG_NOTICE, "configuring %s (sl%d):", dev, unit);
syslog(LOG_NOTICE, " '%s'", s);
system(s);
free (s);
unit = new_unit;
} else {
if (unit < 0)
unit = new_unit;
if (new_unit != unit) {
syslog(LOG_ERR,
"slip unit changed from sl%d to sl%d, but no -u CMD was specified!",
unit, new_unit);
exit_handler(1);
}
syslog(LOG_NOTICE,"sl%d connected to %s at %d baud",unit,dev,speed);
}
}
static void
sighup_handler(int signo __unused)
{
if(exiting) return;
if (redial_cmd == NULL) {
syslog(LOG_NOTICE,"SIGHUP on %s (sl%d); exiting", dev, unit);
exit_handler(1);
}
again:
if (*redial_cmd) {
syslog(LOG_NOTICE,"SIGHUP on %s (sl%d); running '%s'",
dev, unit, redial_cmd);
acquire_line();
setup_line(CLOCAL);
if (locked) {
if (uucp_lock)
uu_unlock(dvname);
locked = 0;
}
if (system(redial_cmd))
goto again;
if (uucp_lock) {
int res;
if ((res = uu_lock(dvname)) != UU_LOCK_OK) {
if (res != UU_LOCK_INUSE)
syslog(LOG_ERR, "uu_lock: %s", uu_lockerr(res));
syslog(LOG_ERR, "can't relock %s after %s, aborting",
dev, redial_cmd);
exit_handler(1);
}
locked = 1;
}
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)) {
goto again;
}
} else
setup_line(0);
} else {
syslog(LOG_NOTICE,"SIGHUP on %s (sl%d); reestablish connection",
dev, unit);
acquire_line();
setup_line(0);
if (!(modem_control & CLOCAL)) {
int carrier = 0;
syslog(LOG_NOTICE, "waiting for carrier on %s (sl%d)",
dev, unit);
while (! carrier) {
sleep(2);
ioctl(fd, TIOCMGET, &comstate);
if (comstate & TIOCM_CD)
carrier = 1;
}
syslog(LOG_NOTICE, "carrier now present on %s (sl%d)",
dev, unit);
}
}
slip_discipline();
configure_network();
}
static void
sigint_handler(int signo __unused)
{
if(exiting) return;
syslog(LOG_NOTICE,"SIGINT on %s (sl%d); exiting",dev,unit);
exit_handler(0);
}
static void
sigurg_handler(int signo __unused)
{
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);
}
if (modem_control & CLOCAL)
kill (getpid(), SIGHUP);
}
static void
sigterm_handler(int signo __unused)
{
if(exiting) return;
syslog(LOG_NOTICE,"SIGTERM on %s (sl%d); exiting",dev,unit);
exit_handler(0);
}
static void
exit_handler(int ret)
{
if(exiting) return;
exiting = 1;
if (fd != -1)
close(fd);
if (uucp_lock && locked)
uu_unlock(dvname);
unlink(pidfilename);
if (config_cmd) {
char *s;
s = (char*) malloc(strlen(config_cmd) + 32);
if (s == NULL) {
syslog(LOG_ERR, "malloc failed");
exit(1);
}
sprintf (s, "%s %d -1", config_cmd, unit);
syslog(LOG_NOTICE, "deconfiguring %s (sl%d):", dev, unit);
syslog(LOG_NOTICE, " '%s'", s);
system(s);
free (s);
}
if (exit_cmd) {
syslog(LOG_NOTICE,"exiting after running %s", exit_cmd);
system(exit_cmd);
}
exit(ret);
}