]> code.delx.au - pulseaudio/commitdiff
daemon: make sure pa has its own session and process group, but is not its leader...
authorLennart Poettering <lennart@poettering.net>
Sat, 31 Oct 2009 01:43:47 +0000 (02:43 +0100)
committerLennart Poettering <lennart@poettering.net>
Sat, 31 Oct 2009 01:43:47 +0000 (02:43 +0100)
src/daemon/main.c
src/pulsecore/core-util.c
src/pulsecore/core-util.h

index c73e9afcf96f169a79709fe1f139714476daf8a0..cc6f24bdcd0a9a8ae9d11654928a5d7fecd2304f 100644 (file)
@@ -657,7 +657,7 @@ int main(int argc, char *argv[]) {
 
 #ifdef HAVE_FORK
         if (pipe(daemon_pipe) < 0) {
-            pa_log(_("pipe failed: %s"), pa_cstrerror(errno));
+            pa_log(_("pipe() failed: %s"), pa_cstrerror(errno));
             goto finish;
         }
 
@@ -708,22 +708,27 @@ int main(int argc, char *argv[]) {
             pa_log_set_target(PA_LOG_SYSLOG);
 
 #ifdef HAVE_SETSID
-        setsid();
-#endif
-#ifdef HAVE_SETPGID
-        setpgid(0,0);
+        if (setsid() < 0) {
+            pa_log(_("setsid() failed: %s"), pa_cstrerror(errno));
+            goto finish;
+        }
 #endif
 
-#ifndef OS_IS_WIN32
-        pa_close(0);
-        pa_close(1);
-        pa_close(2);
+        /* We now are a session and process group leader. Let's fork
+         * again and let the father die, so that we'll become a
+         * process that can never acquire a TTY again, in a session and
+         * process group without leader */
 
-        pa_assert_se(open("/dev/null", O_RDONLY) == 0);
-        pa_assert_se(open("/dev/null", O_WRONLY) == 1);
-        pa_assert_se(open("/dev/null", O_WRONLY) == 2);
-#else
-        FreeConsole();
+#ifdef HAVE_FORK
+        if ((child = fork()) < 0) {
+            pa_log(_("fork() failed: %s"), pa_cstrerror(errno));
+            goto finish;
+        }
+
+        if (child != 0) {
+            retval = 0;
+            goto finish;
+        }
 #endif
 
 #ifdef SIGTTOU
@@ -736,12 +741,7 @@ int main(int argc, char *argv[]) {
         signal(SIGTSTP, SIG_IGN);
 #endif
 
-#ifdef TIOCNOTTY
-        if ((tty_fd = open("/dev/tty", O_RDWR)) >= 0) {
-            ioctl(tty_fd, TIOCNOTTY, (char*) 0);
-            pa_assert_se(pa_close(tty_fd) == 0);
-        }
-#endif
+        pa_nullify_stdfds();
     }
 
     pa_set_env_and_record("PULSE_INTERNAL", "1");
index 738bf06592ed2b6cf3be74f3ac73bb1fc6579743..2b0a60a802c6ee4f430b24d0da0e24a78542a556 100644 (file)
@@ -2999,3 +2999,19 @@ finish:
     pa_make_fd_cloexec(fileno(f));
     return f;
 }
+
+void pa_nullify_stdfds(void) {
+
+#ifndef OS_IS_WIN32
+        pa_close(STDIN_FILENO);
+        pa_close(STDOUT_FILENO);
+        pa_close(STDERR_FILENO);
+
+        pa_assert_se(open("/dev/null", O_RDONLY) == STDIN_FILENO);
+        pa_assert_se(open("/dev/null", O_WRONLY) == STDOUT_FILENO);
+        pa_assert_se(open("/dev/null", O_WRONLY) == STDERR_FILENO);
+#else
+        FreeConsole();
+#endif
+
+}
index d50f79a2cdcc6941227d6dd8a304a8214f75f493..9c9cf78ab97ebf30456eebf059050c09ff9bccc3 100644 (file)
@@ -265,4 +265,6 @@ int pa_pipe_cloexec(int pipefd[2]);
 int pa_accept_cloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
 FILE* pa_fopen_cloexec(const char *path, const char *mode);
 
+void pa_nullify_stdfds(void);
+
 #endif