* Foundation; either version 2 of the License, or (at
* your option) any later version.
*/
+#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include "version.h"
#include "config.h"
#include "intl.h"
+#include "util.h"
#ifndef _PATH_LOCKD
-#define _PATH_LOCKD "/var/lock" /* lock files */
+#define _PATH_LOCKD "/var/lock" /* lock files */
#endif
#ifndef _UID_UUCP
-#define _UID_UUCP "uucp" /* owns locks */
+#define _UID_UUCP "uucp" /* owns locks */
+#endif
+#ifndef _PATH_DEVPTMX
+#define _PATH_DEVPTMX "/dev/ptmx" /* pseudo-terminal master */
#endif
#define DEF_PROTO "cslip"
-char *Release = RELEASE,
- *Version = "@(#) slattach 1.20 (1999-05-29)",
- *Signature = "net-tools, Fred N. van Kempen et al.";
+const char *Release = RELEASE,
+ *Version = "$Id: slattach.c,v 1.12 2009/09/06 22:59:43 vapier Exp $",
+ *Signature = "net-tools, Fred N. van Kempen et al.";
struct {
- char *speed;
+ const char *speed;
int code;
} tty_speeds[] = { /* table of usable baud rates */
{ "50", B50 }, { "75", B75 },
int tty_sdisc, /* saved TTY line discipline */
tty_ldisc, /* current TTY line discipline */
tty_fd = -1; /* TTY file descriptor */
+char * path_pts = NULL; /* slave pseudo-terminal device */
int opt_c = 0; /* "command" to run at exit */
int opt_e = 0; /* "activate only" flag */
int opt_h = 0; /* "hangup" on carrier loss */
int opt_k = 0; /* "keepalive" value */
#endif
int opt_l = 0; /* "lock it" flag */
-int opt_L = 0; /* clocal flag */
+int opt_L = 0; /* 3-wire mode flag */
int opt_m = 0; /* "set RAW mode" flag */
int opt_n = 0; /* "set No Mesg" flag */
#ifdef SIOCSOUTFILL
return(-1);
}
- (void) close(fd);
-
/* Make sure UUCP owns the lockfile. Required by some packages. */
if ((pw = getpwnam(_UID_UUCP)) == NULL) {
if (opt_q == 0) fprintf(stderr, _("slattach: tty_lock: UUCP user %s unknown!\n"),
_UID_UUCP);
+ (void) close(fd);
return(0); /* keep the lock anyway */
}
- (void) chown(saved_path, pw->pw_uid, pw->pw_gid);
+ (void) fchown(fd, pw->pw_uid, pw->pw_gid);
+
+ (void) close(fd);
+
saved_lock = 1;
} else { /* unlock */
if (saved_lock != 1) return(0);
/* Find a serial speed code in the table. */
static int
-tty_find_speed(char *speed)
+tty_find_speed(const char *speed)
{
int i;
/* Set the line speed of a terminal line. */
static int
-tty_set_speed(struct termios *tty, char *speed)
+tty_set_speed(struct termios *tty, const char *speed)
{
int code;
tty->c_oflag = (0); /* output flags */
tty->c_lflag = (0); /* local flags */
speed = (tty->c_cflag & CBAUD); /* save current speed */
- tty->c_cflag = (CRTSCTS | HUPCL | CREAD); /* UART flags */
+ tty->c_cflag = (HUPCL | CREAD); /* UART flags */
if (opt_L)
tty->c_cflag |= CLOCAL;
+ else
+ tty->c_cflag |= CRTSCTS;
tty->c_cflag |= speed; /* restore speed */
return(0);
}
tty_get_name(char *name)
{
if (ioctl(tty_fd, SIOCGIFNAME, name) < 0) {
- if (opt_q == 0) fprintf(stderr,
- "slattach: tty_get_name: %s\n", strerror(errno));
+ if (opt_q == 0)
+ perror("tty_get_name");
return(-errno);
}
return(0);
/* Open and initialize a terminal line. */
static int
-tty_open(char *name, char *speed)
+tty_open(char *name, const char *speed)
{
- char path[PATH_MAX];
- register char *sp;
+ char pathbuf[PATH_MAX];
+ register char *path_open, *path_lock;
int fd;
/* Try opening the TTY device. */
if (name != NULL) {
- if ((sp = strrchr(name, '/')) != (char *)NULL) *sp++ = '\0';
- else sp = name;
- sprintf(path, "/dev/%s", sp);
+ if (name[0] != '/') {
+ if (strlen(name + 6) > sizeof(pathbuf)) {
+ if (opt_q == 0) fprintf(stderr,
+ _("slattach: tty name too long\n"));
+ return (-1);
+ }
+ sprintf(pathbuf, "/dev/%s", name);
+ path_open = pathbuf;
+ path_lock = name;
+ } else if (!strncmp(name, "/dev/", 5)) {
+ path_open = name;
+ path_lock = name + 5;
+ } else {
+ path_open = name;
+ path_lock = name;
+ }
if (opt_d) printf("slattach: tty_open: looking for lock\n");
- if (tty_lock(sp, 1)) return(-1); /* can we lock the device? */
- if (opt_d) printf("slattach: tty_open: trying to open %s\n", path);
- if ((fd = open(path, O_RDWR|O_NDELAY)) < 0) {
+ if (tty_lock(path_lock, 1)) return(-1); /* can we lock the device? */
+ if (opt_d) printf("slattach: tty_open: trying to open %s\n", path_open);
+ if ((fd = open(path_open, O_RDWR|O_NDELAY)) < 0) {
if (opt_q == 0) fprintf(stderr,
"slattach: tty_open(%s, RW): %s\n",
- path, strerror(errno));
+ path_open, strerror(errno));
return(-errno);
}
tty_fd = fd;
- if (opt_d) printf("slattach: tty_open: %s (fd=%d) ", path, fd);
+ if (opt_d) printf("slattach: tty_open: %s (fd=%d)\n", path_open, fd);
+ if (!strcmp(path_open, _PATH_DEVPTMX)) {
+ if (opt_d) printf("slattach: tty_open: trying to grantpt and unlockpt\n");
+ if (grantpt(fd) < 0) {
+ if (opt_q == 0) fprintf(stderr,
+ "slattach: tty_open: grantpt: %s\n", strerror(errno));
+ return(-errno);
+ }
+ if (unlockpt(fd) < 0) {
+ if (opt_q == 0) fprintf(stderr,
+ "slattach: tty_open: unlockpt: %s\n", strerror(errno));
+ return(-errno);
+ }
+ path_pts = ptsname(fd);
+ if (path_pts == NULL) {
+ if (opt_q == 0) fprintf(stderr,
+ "slattach: tty_open: ptsname: %s\n", strerror(errno));
+ return(-errno);
+ }
+ if (opt_d) printf("slattach: tty_open: %s: slave pseudo-terminal is %s\n",
+ path_open, path_pts);
+ }
} else {
tty_fd = 0;
- sp = (char *)NULL;
}
/* Fetch the current state of the terminal. */
"[-c cmd] [-s speed] [-p protocol] tty | -\n"
" slattach -V | --version\n";
- fprintf(stderr, usage_msg);
+ fputs(usage_msg, stderr);
exit(1);
}
int
main(int argc, char *argv[])
{
- char path[128];
+ char path_buf[128];
+ char *path_dev;
char buff[128];
- char *speed = NULL;
- char *proto = DEF_PROTO;
- char *extcmd = (char *)0;
- struct hwtype *ht;
- char *sp;
+ const char *speed = NULL;
+ const char *proto = DEF_PROTO;
+ const char *extcmd = NULL;
int s;
static struct option longopts[] = {
{ "version", 0, NULL, 'V' },
{ NULL, 0, NULL, 0 }
};
- strcpy(path, "");
+ strcpy(path_buf, "");
+ path_dev = path_buf;
/* Scan command line for any arguments. */
opterr = 0;
/*NOTREACHED*/
}
+ if (setvbuf(stdout,0,_IOLBF,0)) {
+ if (opt_q == 0) fprintf(stderr, _("slattach: setvbuf(stdout,0,_IOLBF,0) : %s\n"),
+ strerror(errno));
+ exit(1);
+ }
+
activate_init();
- /* Check the protocol. */
- if ((ht = get_hwtype(proto)) == NULL && strcmp(proto, "tty")) {
- if (opt_q == 0) fprintf(stderr, _("slattach: unsupported protocol %s\n"), proto);
- return(2);
- }
- if (ht == NULL) opt_m++;
+ if (!strcmp(proto, "tty"))
+ opt_m++;
/* Is a terminal given? */
if (optind != (argc - 1)) usage();
- strncpy(path, argv[optind], 128);
- if (!strcmp(path, "-")) {
+ safe_strncpy(path_buf, argv[optind], sizeof(path_buf));
+ if (!strcmp(path_buf, "-")) {
opt_e = 1;
- sp = NULL;
+ path_dev = NULL;
if (tty_open(NULL, speed) < 0) { return(3); }
} else {
- if ((sp = strrchr(path, '/')) != NULL) *sp++ = '\0';
- else sp = path;
- if (tty_open(sp, speed) < 0) { return(3); }
+ path_dev = path_buf;
+ if (tty_open(path_dev, speed) < 0) { return(3); }
}
/* Start the correct protocol. */
- if (ht == NULL) {
+ if (!strcmp(proto, "tty")) {
tty_sdisc = N_TTY;
tty_close();
return(0);
}
- (*ht->activate)(tty_fd);
+ if (activate_ld(proto, tty_fd))
+ return(1);
if ((opt_v == 1) || (opt_d == 1)) {
- tty_get_name(buff);
+ if (tty_get_name(buff)) { return(3); }
printf(_("%s started"), proto);
- if (sp != NULL) printf(_(" on %s"), sp);
+ if (path_dev != NULL) printf(_(" on %s"), path_dev);
+ if (path_pts != NULL) printf(_(" ptsname %s"), path_pts);
printf(_(" interface %s\n"), buff);
}
};
tty_close();
- if(extcmd!=(char *)0) /* external command on exit */
+ if(extcmd) /* external command on exit */
system(extcmd);
}
exit(0);