fix usage of locks with vfork
authorRich Felker <dalias@aerifal.cx>
Fri, 19 Oct 2012 19:02:37 +0000 (15:02 -0400)
committerRich Felker <dalias@aerifal.cx>
Fri, 19 Oct 2012 19:02:37 +0000 (15:02 -0400)
__release_ptc() is only valid in the parent; if it's performed in the
child, the lock will be unlocked early then double-unlocked later,
corrupting the lock state.

src/process/posix_spawn.c
src/process/system.c
src/stdio/popen.c

index c3e800b879553627daef047bae08bea4313d95ea..5eb516b0f4e561e869601427650bad0ad0defe75 100644 (file)
@@ -35,9 +35,9 @@ int __posix_spawnx(pid_t *restrict res, const char *restrict path,
 
        __acquire_ptc();
        pid = __vfork();
-       __release_ptc();
 
        if (pid) {
+               __release_ptc();
                sigprocmask(SIG_SETMASK, &oldmask, 0);
                if (pid < 0) return -pid;
                *res = pid;
index c8f260083ecb827a0c24dcdf5fe693e9485cb4e8..ebe207f597f13edd3bba746115260b6894ce20c4 100644 (file)
@@ -29,7 +29,8 @@ int system(const char *cmd)
 
        __acquire_ptc();
        pid = __vfork();
-       __release_ptc();
+
+       if (pid) __release_ptc();
 
        if (pid > 0) {
                sigset_t new = old;
index 0c9f24e3062da16aab72a9369a750f94b2c19b75..5a47509ed60d6249895df3c80e9d1c4b06ca917d 100644 (file)
@@ -38,9 +38,9 @@ FILE *popen(const char *cmd, const char *mode)
        
        __acquire_ptc();
        pid = __vfork();
-       __release_ptc();
 
        if (pid) {
+               __release_ptc();
                __syscall(SYS_close, p[1-op]);
                sigprocmask(SIG_BLOCK, SIGALL_SET, &old);
                if (pid < 0) {