infiniband: fix up new warning message
[platform/upstream/net-tools.git] / slattach.c
index 75dc566..2b7dde2 100644 (file)
 #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
 
 
@@ -73,7 +76,7 @@
 
 
 const char *Release = RELEASE,
-          *Version = "@(#) slattach 1.21 (1999-11-21)",
+          *Version = "$Id: slattach.c,v 1.12 2009/09/06 22:59:43 vapier Exp $",
           *Signature = "net-tools, Fred N. van Kempen et al.";
 
 
@@ -108,6 +111,7 @@ struct termios      tty_saved,              /* saved TTY device state       */
 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     */
@@ -115,7 +119,7 @@ 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
@@ -195,15 +199,17 @@ tty_lock(char *path, int mode)
                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);
@@ -342,9 +348,11 @@ tty_set_raw(struct termios *tty)
   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);
 }
@@ -456,29 +464,62 @@ tty_close(void)
 static int
 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. */
@@ -553,7 +594,7 @@ usage(void)
          "[-c cmd] [-s speed] [-p protocol] tty | -\n"
          "       slattach -V | --version\n";
 
-  fprintf(stderr, usage_msg);
+  fputs(usage_msg, stderr);
   exit(1);
 }
 
@@ -569,19 +610,20 @@ version(void)
 int
 main(int argc, char *argv[])
 {
-  char path[128];
+  char path_buf[128];
+  char *path_dev;
   char buff[128];
   const char *speed = NULL;
   const char *proto = DEF_PROTO;
   const char *extcmd = NULL;
-  char *sp;
   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;
@@ -655,6 +697,12 @@ main(int argc, char *argv[])
                /*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();
 
   if (!strcmp(proto, "tty"))
@@ -662,15 +710,14 @@ main(int argc, char *argv[])
 
   /* Is a terminal given? */
   if (optind != (argc - 1)) usage();
-  safe_strncpy(path, argv[optind], sizeof(path));
-  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. */
@@ -684,7 +731,8 @@ main(int argc, char *argv[])
   if ((opt_v == 1) || (opt_d == 1)) {
         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);
   }