]> granicus.if.org Git - sudo/commitdiff
Improved background mode support. When not allocating a pty, the
authorTodd C. Miller <Todd.Miller@courtesan.com>
Sun, 14 Aug 2011 15:45:09 +0000 (11:45 -0400)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Sun, 14 Aug 2011 15:45:09 +0000 (11:45 -0400)
command is run in its own process group. This prevents write access
to the tty. When running in a pty, stdin is not hooked up and we
never read from /dev/tty, which results in similar behavior.

--HG--
branch : 1.7

exec.c
exec_pty.c
sudo_exec.h

diff --git a/exec.c b/exec.c
index 19dbad2e4dca52f116c0a32406a9b37e11505af6..fd4c8a61e220a1f2366e436497291a3041cf56b5 100644 (file)
--- a/exec.c
+++ b/exec.c
@@ -231,19 +231,19 @@ sudo_execve(path, argv, envp, uid, cstat, dowait, bgmode)
                cstat->val = errno;
                return -1;
            case 0:
-               /* child continues */   
+               /* child continues without controlling terminal */
+               (void)setpgid(0, 0);
                break;
            default:
-               /* parent exits */
-               exit(0);
+               /* parent exits (but does not flush buffers) */
+               _exit(0);
        }
     }
 
 #ifdef _PATH_SUDO_IO_LOGDIR
     log_io = def_log_output || def_log_input || def_use_pty;
     if (log_io) {
-       if (!bgmode)
-           pty_setup(uid);
+       pty_setup(uid);
        io_log_open();
        dowait = TRUE;
     }
@@ -309,7 +309,7 @@ sudo_execve(path, argv, envp, uid, cstat, dowait, bgmode)
      */
 #ifdef _PATH_SUDO_IO_LOGDIR
     if (log_io)
-       child = fork_pty(path, argv, envp, sv, rbac_enabled, &maxfd);
+       child = fork_pty(path, argv, envp, sv, rbac_enabled, bgmode, &maxfd);
     else
 #endif
        child = fork_cmnd(path, argv, envp, sv, rbac_enabled);
index ec54a1f45deba16c5b157376df90d95bdbc1409c..0788a136f2ab912819ae4892373e312fb0a59cbc 100644 (file)
@@ -347,12 +347,13 @@ perform_io(fdsr, fdsw, cstat)
  * Returns the child pid.
  */
 int
-fork_pty(path, argv, envp, sv, rbac_enabled, maxfd)
+fork_pty(path, argv, envp, sv, rbac_enabled, bgmode, maxfd)
     const char *path;
     char *argv[];
     char *envp[];
     int sv[2];
     int rbac_enabled;
+    int bgmode;
     int *maxfd;
 {
     struct command_status cstat;
@@ -373,17 +374,21 @@ fork_pty(path, argv, envp, sv, rbac_enabled, maxfd)
 
     /*
      * Setup stdin/stdout/stderr for child, to be duped after forking.
+     * In background mode there is no stdin.
      */
-    io_fds[SFD_STDIN] = io_fds[SFD_SLAVE];
+    if (!bgmode)
+       io_fds[SFD_STDIN] = io_fds[SFD_SLAVE];
     io_fds[SFD_STDOUT] = io_fds[SFD_SLAVE];
     io_fds[SFD_STDERR] = io_fds[SFD_SLAVE];
 
-    /* Copy /dev/tty -> pty master */
     if (io_fds[SFD_USERTTY] != -1) {
-       iobufs = io_buf_new(io_fds[SFD_USERTTY], io_fds[SFD_MASTER],
-           log_ttyin, iobufs);
+       /* Read from /dev/tty, write to pty master */
+       if (!bgmode) {
+           iobufs = io_buf_new(io_fds[SFD_USERTTY], io_fds[SFD_MASTER],
+               log_ttyin, iobufs);
+       }
 
-       /* Copy pty master -> /dev/tty */
+       /* Read from pty master, write to /dev/tty */
        iobufs = io_buf_new(io_fds[SFD_MASTER], io_fds[SFD_USERTTY],
            log_ttyout, iobufs);
 
index ea43d55beace8aff26bcfcd3fc606c706ab6ee48..bda6f24a385a5ea7bcede17c49e6ccc00f7de949 100644 (file)
@@ -33,7 +33,7 @@ int pipe_nonblock __P((int fds[2]));
 
 /* exec_pty.c */
 int fork_pty __P((const char *path, char *argv[], char *envp[], int sv[],
-    int rbac_enabled, int *maxfd));
+    int rbac_enabled, int bgmode, int *maxfd));
 int perform_io __P((fd_set *fdsr, fd_set *fdsw, struct command_status *cstat));
 int suspend_parent __P((int signo));
 void fd_set_iobs __P((fd_set *fdsr, fd_set *fdsw));