]> git.lizzy.rs Git - rust.git/commit
libstd/sys/unix/process.rs: reap a zombie who didn't get through to exec(2).
authorNODA, Kai <nodakai@gmail.com>
Thu, 4 Dec 2014 18:05:57 +0000 (02:05 +0800)
committerNODA, Kai <nodakai@gmail.com>
Fri, 5 Dec 2014 02:04:06 +0000 (10:04 +0800)
commit74fb798a200dc82cf5b4a18065e3ea565229adc3
tree010d93c42aa13354d6aa4143bb14c67cb7b7d499
parent3c89031e1f213030f0514c8dcb9e6fa19ddbd323
libstd/sys/unix/process.rs: reap a zombie who didn't get through to exec(2).

After the library successfully called fork(2), the child does several
setup works such as setting UID, GID and current directory before it
calls exec(2).  When those setup works failed, the child exits but the
parent didn't call waitpid(2) and left it as a zombie.

This patch also add several sanity checks.  They shouldn't make any
noticeable impact to runtime performance.

The new test case run-pass/wait-forked-but-failed-child.rs calls the ps
command to check if the new code can really reap a zombie.  When
I intentionally create many zombies with my test program
./spawn-failure, The output of "ps -A -o pid,sid,command" should look
like this:

  PID   SID COMMAND
    1     1 /sbin/init
    2     0 [kthreadd]
    3     0 [ksoftirqd/0]
...
12562  9237 ./spawn-failure
12563  9237 [spawn-failure] <defunct>
12564  9237 [spawn-failure] <defunct>
...
12592  9237 [spawn-failure] <defunct>
12593  9237 ps -A -o pid,sid,command
12884 12884 /bin/zsh
12922 12922 /bin/zsh
...

Filtering the output with the "SID" (session ID) column is a quick way
to tell if a process (zombie) was spawned by my own test program.  Then
the number of "defunct" lines is the number of zombie children.

Signed-off-by: NODA, Kai <nodakai@gmail.com>
src/libstd/sys/unix/process.rs
src/test/run-pass/wait-forked-but-failed-child.rs [new file with mode: 0644]