Add support for IATTR_WRITE.
[util-vserver.git] / src / vlogin.c
index 56a1df0..dc47f93 100644 (file)
@@ -38,6 +38,7 @@
 #include <signal.h>
 #include <pty.h>
 #include <fcntl.h>
+#include <sys/prctl.h>
 
 #define ENSC_WRAPPERS_PREFIX   "vlogin: "
 #define ENSC_WRAPPERS_IOCTL    1
@@ -124,7 +125,7 @@ terminal_redraw(void)
 }
 
 /* copy terminal activities */
-static void
+static ssize_t
 terminal_copy(int src, int dst)
 {
   char buf[64];
@@ -137,10 +138,12 @@ terminal_copy(int src, int dst)
     terminal_kill(SIGTERM);
     exit(1);
   } else if (len == -1)
-    return;
+    return -1;
 
   /* write activity to user */
   EwriteAll(dst, buf, len);
+
+  return len;
 }
 
 /* shuffle all output, and reset the terminal */
@@ -194,11 +197,11 @@ signal_handler(int sig)
 
 }
 
-void do_vlogin(int argc, char *argv[], int ind)
+void do_vlogin(int UNUSED argc, char *argv[], int ind)
 {
   int slave;
   pid_t pid;
-  int n, i;
+  int n;
   fd_set rfds;
 
   if (!isatty(0) || !isatty(1)) {
@@ -209,6 +212,9 @@ void do_vlogin(int argc, char *argv[], int ind)
   /* set terminal to raw mode */
   terminal_raw();
 
+  /* reset terminal to its original mode */
+  atexit(terminal_reset);
+
   /* fork new pseudo terminal */
   if (openpty(&t.fd, &slave, NULL, NULL, NULL) == -1) {
     perror(ENSC_WRAPPERS_PREFIX "openpty()");
@@ -247,15 +253,7 @@ void do_vlogin(int argc, char *argv[], int ind)
   t.pid = pid;
 
   /* set process title for ps */
-  n = strlen(argv[0]);
-
-  for (i = 0; i < argc; i++)
-    memset(argv[i], '\0', strlen(argv[i]));
-
-  strncpy(argv[0], "login", n);
-
-  /* reset terminal to its original mode */
-  atexit(terminal_reset);
+  prctl(PR_SET_NAME, (unsigned long) "login");
 
   /* we want a redraw */
   terminal_redraw();
@@ -276,11 +274,21 @@ void do_vlogin(int argc, char *argv[], int ind)
       exit(wrapper_exit_code);
     }
 
-    if (FD_ISSET(STDIN_FILENO, &rfds))
-      terminal_copy(STDIN_FILENO, t.fd);
+    if (FD_ISSET(STDIN_FILENO, &rfds)) {
+      /* EOF */
+      if (terminal_copy(STDIN_FILENO, t.fd) == 0) {
+       terminal_kill(SIGHUP);
+       exit(0);
+      }
+    }
 
-    if (FD_ISSET(t.fd, &rfds))
-      terminal_copy(t.fd, STDOUT_FILENO);
+    if (FD_ISSET(t.fd, &rfds)) {
+      /* EOF */
+      if (terminal_copy(t.fd, STDOUT_FILENO) == 0) {
+       terminal_kill(SIGHUP);
+       exit(0);
+      }
+    }
   }
 
   /* never get here, signal handler exits */