Workarounds for compiling with -D_FORTIFY_SOURCE=2 (as Ubuntu does), which
[u/mdw/putty] / unix / uxpty.c
index b54ed41..5de43a3 100644 (file)
@@ -260,7 +260,8 @@ static void cleanup_utmp(void)
 
 static void sigchld_handler(int signum)
 {
-    write(pty_signal_pipe[1], "x", 1);
+    if (write(pty_signal_pipe[1], "x", 1) <= 0)
+       /* not much we can do about it */;
 }
 
 #ifndef OMIT_UTMP
@@ -275,8 +276,10 @@ static void fatal_sig_handler(int signum)
 
 static int pty_open_slave(Pty pty)
 {
-    if (pty->slave_fd < 0)
+    if (pty->slave_fd < 0) {
        pty->slave_fd = open(pty->name, O_RDWR);
+        cloexec(pty->slave_fd);
+    }
 
     return pty->slave_fd;
 }
@@ -307,6 +310,8 @@ static void pty_open_master(Pty pty)
                    strcpy(pty->name, master_name);
                    pty->name[5] = 't'; /* /dev/ptyXX -> /dev/ttyXX */
 
+                    cloexec(pty->master_fd);
+
                    if (pty_open_slave(pty) >= 0 &&
                        access(pty->name, R_OK | W_OK) == 0)
                        goto got_one;
@@ -346,6 +351,8 @@ static void pty_open_master(Pty pty)
        exit(1);
     }
 
+    cloexec(pty->master_fd);
+
     pty->name[FILENAME_MAX-1] = '\0';
     strncpy(pty->name, ptsname(pty->master_fd), FILENAME_MAX-1);
 #endif
@@ -354,8 +361,10 @@ static void pty_open_master(Pty pty)
         /*
          * Set the pty master into non-blocking mode.
          */
-        int i = 1;
-        ioctl(pty->master_fd, FIONBIO, &i);
+        int fl;
+       fl = fcntl(pty->master_fd, F_GETFL);
+       if (fl != -1 && !(fl & O_NONBLOCK))
+           fcntl(pty->master_fd, F_SETFL, fl | O_NONBLOCK);
     }
 
     if (!ptys_by_fd)
@@ -408,6 +417,8 @@ void pty_pre_init(void)
        perror("pterm: pipe");
        exit(1);
     }
+    cloexec(pipefd[0]);
+    cloexec(pipefd[1]);
     pid = fork();
     if (pid < 0) {
        perror("pterm: fork");
@@ -623,7 +634,9 @@ int pty_select_result(int fd, int event)
        int status;
        char c[1];
 
-       read(pty_signal_pipe[0], c, 1); /* ignore its value; it'll be `x' */
+       if (read(pty_signal_pipe[0], c, 1) <= 0)
+           /* ignore error */;
+       /* ignore its value; it'll be `x' */
 
        do {
            pid = waitpid(-1, &status, WNOHANG);
@@ -749,7 +762,6 @@ static const char *pty_init(void *frontend, void **backend_handle, Config *cfg,
     }
 
     if (pid == 0) {
-       int i;
        /*
         * We are the child.
         */
@@ -765,18 +777,16 @@ static const char *pty_init(void *frontend, void **backend_handle, Config *cfg,
        dup2(slavefd, 0);
        dup2(slavefd, 1);
        dup2(slavefd, 2);
+       close(slavefd);
        setsid();
 #ifdef TIOCSCTTY
-       ioctl(slavefd, TIOCSCTTY, 1);
+       ioctl(0, TIOCSCTTY, 1);
 #endif
        pgrp = getpid();
-       tcsetpgrp(slavefd, pgrp);
+       tcsetpgrp(0, pgrp);
        setpgid(pgrp, pgrp);
        close(open(pty->name, O_WRONLY, 0));
        setpgid(pgrp, pgrp);
-       /* Close everything _else_, for tidiness. */
-       for (i = 3; i < 1024; i++)
-           close(i);
        {
            char *term_env_var = dupprintf("TERM=%s", cfg->termtype);
            putenv(term_env_var);
@@ -857,14 +867,20 @@ static const char *pty_init(void *frontend, void **backend_handle, Config *cfg,
        add234(ptys_by_pid, pty);
     }
 
-    if (pty_signal_pipe[0] < 0 && pipe(pty_signal_pipe) < 0) {
-       perror("pipe");
-       exit(1);
+    if (pty_signal_pipe[0] < 0) {
+       if (pipe(pty_signal_pipe) < 0) {
+           perror("pipe");
+           exit(1);
+       }
+       cloexec(pty_signal_pipe[0]);
+       cloexec(pty_signal_pipe[1]);
     }
     pty_uxsel_setup(pty);
 
     *backend_handle = pty;
 
+    *realhost = dupprintf("\0");
+
     return NULL;
 }
 
@@ -1006,10 +1022,10 @@ static const struct telnet_special *pty_get_specials(void *handle)
     return NULL;
 }
 
-static Socket pty_socket(void *handle)
+static int pty_connected(void *handle)
 {
     /* Pty pty = (Pty)handle; */
-    return NULL;                      /* shouldn't ever be needed */
+    return TRUE;
 }
 
 static int pty_sendok(void *handle)
@@ -1066,7 +1082,7 @@ Backend pty_backend = {
     pty_size,
     pty_special,
     pty_get_specials,
-    pty_socket,
+    pty_connected,
     pty_exitcode,
     pty_sendok,
     pty_ldisc,
@@ -1074,5 +1090,7 @@ Backend pty_backend = {
     pty_provide_logctx,
     pty_unthrottle,
     pty_cfg_info,
-    1
+    "pty",
+    -1,
+    0
 };