2
* slattach A program for handling dialup IP connecions.
3
* This program forces a TTY line to go into a special
4
* terminal line discipline, so that it can be used for
5
* network traffic instead of the regular terminal I/O.
7
* Usage: slattach [-ehlmnqv] [ -k keepalive ] [ -o outfill ]
8
* [-c cmd] [-s speed] [-p protocol] tty | -
10
* Version: @(#)slattach.c 1.20 1999-05-29
12
* Author: Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
13
* Copyright 1988-1993 MicroWalt Corporation
16
* Alan Cox, <A.Cox@swansea.ac.uk> , July 16 1994
17
* Miquel van Smoorenburg, <miquels@drinkel.ow.org>, October 1994
18
* George Shearer, <gshearer@one.net>, January 3, 1995
19
* Yossi Gottlieb, <yogo@math.tau.ac.il>, February 11, 1995
20
* Peter Tobias, <tobias@et-inf.fho-emden.de>, July 30 1995
21
* Bernd Eckenfels <net-tools@lina.inka.de>, May 29, 1999
22
* added some more printf's for debug and NOBLOCK to open
23
* this should be enough to support 2.2 ttyS-style locks
25
* This program is free software; you can redistribute it
26
* and/or modify it under the terms of the GNU General
27
* Public License as published by the Free Software
28
* Foundation; either version 2 of the License, or (at
29
* your option) any later version.
31
#include <sys/param.h>
32
#include <sys/types.h>
33
#include <sys/socket.h>
34
#include <sys/ioctl.h>
47
#include <linux/if_slip.h>
49
#if defined(__GLIBC__)
50
#if __GLIBC__ == 2 && __GLIBC_MINOR__ == 0
51
# include <termbits.h>
57
#include "pathnames.h"
58
#include "net-support.h"
65
#define _PATH_LOCKD "/var/lock" /* lock files */
68
#define _UID_UUCP "uucp" /* owns locks */
72
#define DEF_PROTO "cslip"
75
const char *Release = RELEASE,
76
*Version = "@(#) slattach 1.21 (1999-11-21)",
77
*Signature = "net-tools, Fred N. van Kempen et al.";
83
} tty_speeds[] = { /* table of usable baud rates */
84
{ "50", B50 }, { "75", B75 },
85
{ "110", B110 }, { "300", B300 },
86
{ "600", B600 }, { "1200", B1200 },
87
{ "2400", B2400 }, { "4800", B4800 },
102
{ "115200", B115200 },
106
struct termios tty_saved, /* saved TTY device state */
107
tty_current; /* current TTY device state */
108
int tty_sdisc, /* saved TTY line discipline */
109
tty_ldisc, /* current TTY line discipline */
110
tty_fd = -1; /* TTY file descriptor */
111
int opt_c = 0; /* "command" to run at exit */
112
int opt_e = 0; /* "activate only" flag */
113
int opt_h = 0; /* "hangup" on carrier loss */
114
#ifdef SIOCSKEEPALIVE
115
int opt_k = 0; /* "keepalive" value */
117
int opt_l = 0; /* "lock it" flag */
118
int opt_L = 0; /* clocal flag */
119
int opt_m = 0; /* "set RAW mode" flag */
120
int opt_n = 0; /* "set No Mesg" flag */
122
int opt_o = 0; /* "outfill" value */
124
int opt_q = 0; /* "quiet" flag */
125
int opt_d = 0; /* debug flag */
126
int opt_v = 0; /* Verbose flag */
128
/* Disable any messages to the input channel of this process. */
132
if (opt_n == 0) return(0);
133
return(fchmod(fd, 0600));
136
/* Check for an existing lock file on our device */
138
tty_already_locked(char *nam)
141
FILE *fd = (FILE *)0;
143
/* Does the lock file on our device exist? */
144
if ((fd = fopen(nam, "r")) == (FILE *)0)
145
return(0); /* No, return perm to continue */
147
/* Yes, the lock is there. Now let's make sure */
148
/* at least there's no active process that owns */
150
i = fscanf(fd, "%d", &pid);
153
if (i != 1) /* Lock file format's wrong! Kill't */
156
/* We got the pid, check if the process's alive */
157
if (kill(pid, 0) == 0) /* it found process */
158
return(1); /* Yup, it's running... */
160
/* Dead, we can proceed locking this device... */
164
/* Lock or unlock a terminal line. */
166
tty_lock(char *path, int mode)
168
static char saved_path[PATH_MAX];
169
static int saved_lock = 0;
174
/* We do not lock standard input. */
175
if ((opt_l == 0) || ((path == NULL) && (saved_lock == 0))) return(0);
177
if (mode == 1) { /* lock */
178
sprintf(saved_path, "%s/LCK..%s", _PATH_LOCKD, path);
179
if (tty_already_locked(saved_path)) {
180
fprintf(stderr, _("slattach: /dev/%s already locked!\n"), path);
183
if ((fd = creat(saved_path, 0644)) < 0) {
185
if (opt_q == 0) fprintf(stderr,
186
_("slattach: tty_lock: (%s): %s\n"),
187
saved_path, strerror(errno));
190
sprintf(apid, "%10d\n", getpid());
191
if (write(fd, apid, strlen(apid)) != strlen(apid)) {
192
fprintf(stderr, _("slattach: cannot write PID file\n"));
200
/* Make sure UUCP owns the lockfile. Required by some packages. */
201
if ((pw = getpwnam(_UID_UUCP)) == NULL) {
202
if (opt_q == 0) fprintf(stderr, _("slattach: tty_lock: UUCP user %s unknown!\n"),
204
return(0); /* keep the lock anyway */
206
(void) chown(saved_path, pw->pw_uid, pw->pw_gid);
208
} else { /* unlock */
209
if (saved_lock != 1) return(0);
210
if (unlink(saved_path) < 0) {
211
if (opt_q == 0) fprintf(stderr,
212
"slattach: tty_unlock: (%s): %s\n", saved_path,
223
/* Find a serial speed code in the table. */
225
tty_find_speed(const char *speed)
230
while (tty_speeds[i].speed != NULL) {
231
if (!strcmp(tty_speeds[i].speed, speed)) return(tty_speeds[i].code);
238
/* Set the number of stop bits. */
240
tty_set_stopbits(struct termios *tty, char *stopbits)
242
if (opt_d) printf("slattach: tty_set_stopbits: %c\n", *stopbits);
245
tty->c_cflag &= ~CSTOPB;
249
tty->c_cflag |= CSTOPB;
259
/* Set the number of data bits. */
261
tty_set_databits(struct termios *tty, char *databits)
263
if (opt_d) printf("slattach: tty_set_databits: %c\n", *databits);
264
tty->c_cflag &= ~CSIZE;
289
/* Set the type of parity encoding. */
291
tty_set_parity(struct termios *tty, char *parity)
293
if (opt_d) printf("slattach: tty_set_parity: %c\n", *parity);
294
switch(toupper(*parity)) {
296
tty->c_cflag &= ~(PARENB | PARODD);
300
tty->c_cflag &= ~(PARENB | PARODD);
301
tty->c_cflag |= (PARENB | PARODD);
305
tty->c_cflag &= ~(PARENB | PARODD);
306
tty->c_cflag |= (PARENB);
316
/* Set the line speed of a terminal line. */
318
tty_set_speed(struct termios *tty, const char *speed)
322
if (opt_d) printf("slattach: tty_set_speed: %s\n", speed);
323
if ((code = tty_find_speed(speed)) < 0) return(code);
324
tty->c_cflag &= ~CBAUD;
325
tty->c_cflag |= code;
330
/* Put a terminal line in a transparent state. */
332
tty_set_raw(struct termios *tty)
337
for(i = 0; i < NCCS; i++)
338
tty->c_cc[i] = '\0'; /* no spec chr */
340
tty->c_cc[VTIME] = 0;
341
tty->c_iflag = (IGNBRK | IGNPAR); /* input flags */
342
tty->c_oflag = (0); /* output flags */
343
tty->c_lflag = (0); /* local flags */
344
speed = (tty->c_cflag & CBAUD); /* save current speed */
345
tty->c_cflag = (CRTSCTS | HUPCL | CREAD); /* UART flags */
347
tty->c_cflag |= CLOCAL;
348
tty->c_cflag |= speed; /* restore speed */
353
/* Fetch the state of a terminal. */
355
tty_get_state(struct termios *tty)
357
if (ioctl(tty_fd, TCGETS, tty) < 0) {
358
if (opt_q == 0) fprintf(stderr,
359
"slattach: tty_get_state: %s\n", strerror(errno));
366
/* Set the state of a terminal. */
368
tty_set_state(struct termios *tty)
370
if (ioctl(tty_fd, TCSETS, tty) < 0) {
371
if (opt_q == 0) fprintf(stderr,
372
"slattach: tty_set_state: %s\n", strerror(errno));
379
/* Get the line discipline of a terminal line. */
381
tty_get_disc(int *disc)
383
if (ioctl(tty_fd, TIOCGETD, disc) < 0) {
384
if (opt_q == 0) fprintf(stderr,
385
"slattach: tty_get_disc: %s\n", strerror(errno));
392
/* Set the line discipline of a terminal line. */
394
tty_set_disc(int disc)
396
if (disc == -1) disc = tty_sdisc;
398
if (ioctl(tty_fd, TIOCSETD, &disc) < 0) {
399
if (opt_q == 0) fprintf(stderr,
400
"slattach: tty_set_disc(%d, %d): %s\n", tty_fd,
401
disc, strerror(errno));
408
/* Fetch the name of the network interface attached to this terminal. */
410
tty_get_name(char *name)
412
if (ioctl(tty_fd, SIOCGIFNAME, name) < 0) {
414
perror("tty_get_name");
421
/* Hangup the line. */
428
(void) tty_set_speed(&tty, "0");
429
if (tty_set_state(&tty) < 0) {
430
if (opt_q == 0) fprintf(stderr, _("slattach: tty_hangup(DROP): %s\n"), strerror(errno));
436
if (tty_set_state(&tty_current) < 0) {
437
if (opt_q == 0) fprintf(stderr, _("slattach: tty_hangup(RAISE): %s\n"), strerror(errno));
444
/* Close down a terminal line. */
448
(void) tty_set_disc(tty_sdisc);
450
(void) tty_lock(NULL, 0);
455
/* Open and initialize a terminal line. */
457
tty_open(char *name, const char *speed)
459
char pathbuf[PATH_MAX];
460
register char *path_open, *path_lock;
463
/* Try opening the TTY device. */
465
if (name[0] != '/') {
466
if (strlen(name + 6) > sizeof(pathbuf)) {
467
if (opt_q == 0) fprintf(stderr,
468
_("slattach: tty name too long\n"));
471
sprintf(pathbuf, "/dev/%s", name);
474
} else if (!strncmp(name, "/dev/", 5)) {
476
path_lock = name + 5;
481
if (opt_d) printf("slattach: tty_open: looking for lock\n");
482
if (tty_lock(path_lock, 1)) return(-1); /* can we lock the device? */
483
if (opt_d) printf("slattach: tty_open: trying to open %s\n", path_open);
484
if ((fd = open(path_open, O_RDWR|O_NDELAY)) < 0) {
485
if (opt_q == 0) fprintf(stderr,
486
"slattach: tty_open(%s, RW): %s\n",
487
path_open, strerror(errno));
491
if (opt_d) printf("slattach: tty_open: %s (fd=%d) ", path_open, fd);
496
/* Fetch the current state of the terminal. */
497
if (tty_get_state(&tty_saved) < 0) {
498
if (opt_q == 0) fprintf(stderr, _("slattach: tty_open: cannot get current state!\n"));
501
tty_current = tty_saved;
503
/* Fetch the current line discipline of this terminal. */
504
if (tty_get_disc(&tty_sdisc) < 0) {
505
if (opt_q == 0) fprintf(stderr, _("slattach: tty_open: cannot get current line disc!\n"));
508
tty_ldisc = tty_sdisc;
510
/* Put this terminal line in a 8-bit transparent mode. */
512
if (tty_set_raw(&tty_current) < 0) {
513
if (opt_q == 0) fprintf(stderr, _("slattach: tty_open: cannot set RAW mode!\n"));
517
/* Set the default speed if we need to. */
519
if (tty_set_speed(&tty_current, speed) != 0) {
520
if (opt_q == 0) fprintf(stderr, _("slattach: tty_open: cannot set %s bps!\n"),
526
/* Set up a completely 8-bit clean line. */
527
if (tty_set_databits(&tty_current, "8") ||
528
tty_set_stopbits(&tty_current, "1") ||
529
tty_set_parity(&tty_current, "N")) {
530
if (opt_q == 0) fprintf(stderr, _("slattach: tty_open: cannot set 8N1 mode!\n"));
534
/* Set the new line mode. */
535
if ((fd = tty_set_state(&tty_current)) < 0) return(fd);
538
/* OK, line is open. Do we need to "silence" it? */
539
(void) tty_nomesg(tty_fd);
545
/* Catch any signals. */
549
/* (void) signal(sig, sig_catch); */
558
char *usage_msg = "Usage: slattach [-ehlLmnqv] "
559
#ifdef SIOCSKEEPALIVE
565
"[-c cmd] [-s speed] [-p protocol] tty | -\n"
566
" slattach -V | --version\n";
568
fprintf(stderr, usage_msg);
576
printf("%s\n%s\n%s\n", Release, Version, Signature);
582
main(int argc, char *argv[])
587
const char *speed = NULL;
588
const char *proto = DEF_PROTO;
589
const char *extcmd = NULL;
591
static struct option longopts[] = {
592
{ "version", 0, NULL, 'V' },
596
strcpy(path_buf, "");
599
/* Scan command line for any arguments. */
601
while ((s = getopt_long(argc, argv, "c:ehlLmnp:qs:vdVk:o:", longopts, NULL)) != EOF) switch(s) {
614
#ifdef SIOCSKEEPALIVE
616
opt_k = atoi(optarg);
638
opt_o = atoi(optarg);
671
if (setvbuf(stdout,0,_IOLBF,0)) {
672
if (opt_q == 0) fprintf(stderr, _("slattach: setvbuf(stdout,0,_IOLBF,0) : %s\n"),
679
if (!strcmp(proto, "tty"))
682
/* Is a terminal given? */
683
if (optind != (argc - 1)) usage();
684
safe_strncpy(path_buf, argv[optind], sizeof(path_buf));
685
if (!strcmp(path_buf, "-")) {
688
if (tty_open(NULL, speed) < 0) { return(3); }
691
if (tty_open(path_dev, speed) < 0) { return(3); }
694
/* Start the correct protocol. */
695
if (!strcmp(proto, "tty")) {
700
if (activate_ld(proto, tty_fd))
702
if ((opt_v == 1) || (opt_d == 1)) {
703
if (tty_get_name(buff)) { return(3); }
704
printf(_("%s started"), proto);
705
if (path_dev != NULL) printf(_(" on %s"), path_dev);
706
printf(_(" interface %s\n"), buff);
709
/* Configure keepalive and outfill. */
710
#ifdef SIOCSKEEPALIVE
711
if (opt_k && (ioctl(tty_fd, SIOCSKEEPALIVE, &opt_k) < 0))
712
fprintf(stderr, "slattach: ioctl(SIOCSKEEPALIVE): %s\n", strerror(errno));
715
if (opt_o && (ioctl(tty_fd, SIOCSOUTFILL, &opt_o) < 0))
716
fprintf(stderr, "slattach: ioctl(SIOCSOUTFILL): %s\n", strerror(errno));
719
(void) signal(SIGHUP, sig_catch);
720
(void) signal(SIGINT, sig_catch);
721
(void) signal(SIGQUIT, sig_catch);
722
(void) signal(SIGTERM, sig_catch);
724
/* Wait until we get killed if hanging on a terminal. */
727
if(opt_h == 1) { /* hangup on carrier loss */
730
ioctl(tty_fd, TIOCMGET, &n);
740
if(extcmd) /* external command on exit */