diff options
author | Jeremy Huddleston <jeremyhu@freedesktop.org> | 2009-06-27 15:28:34 -0700 |
---|---|---|
committer | Jeremy Huddleston <jeremyhu@freedesktop.org> | 2009-06-27 18:26:08 -0700 |
commit | b8050bb6deebdb1ee60731f63884ffca575c09ce (patch) | |
tree | 3854e8896c794ccd0b343308db4f0ef4c3f24357 /hw/xquartz/mach-startup | |
parent | aaff92c8c22a47804a21010d023ef76d82e7ec7e (diff) |
XQuartz: Don't leave zombied processes at startup
(cherry picked from commit 40c1406830588fa85d880e9f4e9ca570db1db306)
Diffstat (limited to 'hw/xquartz/mach-startup')
-rw-r--r-- | hw/xquartz/mach-startup/bundle-main.c | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/hw/xquartz/mach-startup/bundle-main.c b/hw/xquartz/mach-startup/bundle-main.c index fd70f26ed..ef5d75700 100644 --- a/hw/xquartz/mach-startup/bundle-main.c +++ b/hw/xquartz/mach-startup/bundle-main.c @@ -44,6 +44,7 @@ #include <sys/un.h> #include <sys/time.h> +#include <fcntl.h> #include <mach/mach.h> #include <mach/mach_error.h> @@ -514,8 +515,43 @@ int main(int argc, char **argv, char **envp) { * thread handle it. */ if(!listenOnly) { - if(fork() == 0) { - return startup_trigger(argc, argv, envp); + pid_t child1, child2; + int status; + + /* Do the fork-twice trick to avoid having to reap zombies */ + child1 = fork(); + switch (child1) { + case -1: /* error */ + break; + + case 0: /* child1 */ + child2 = fork(); + + switch (child2) { + int max_files, i; + + case -1: /* error */ + break; + + case 0: /* child2 */ + /* close all open files except for standard streams */ + max_files = sysconf(_SC_OPEN_MAX); + for(i = 3; i < max_files; i++) + close(i); + + /* ensure stdin is on /dev/null */ + close(0); + open("/dev/null", O_RDONLY); + + return startup_trigger(argc, argv, envp); + + default: /* parent (child1) */ + _exit(0); + } + break; + + default: /* parent */ + waitpid(child1, &status, 0); } } |