Russ Cox | b53ce1e | 2012-02-01 18:25:40 -0500 | [diff] [blame] | 1 | // +build !windows |
| 2 | |
Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 3 | /* |
| 4 | Plan 9 from User Space src/lib9/rfork.c |
| 5 | http://code.swtch.com/plan9port/src/tip/src/lib9/rfork.c |
| 6 | |
| 7 | Copyright 2001-2007 Russ Cox. All Rights Reserved. |
| 8 | |
| 9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | of this software and associated documentation files (the "Software"), to deal |
| 11 | in the Software without restriction, including without limitation the rights |
| 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | copies of the Software, and to permit persons to whom the Software is |
| 14 | furnished to do so, subject to the following conditions: |
| 15 | |
| 16 | The above copyright notice and this permission notice shall be included in |
| 17 | all copies or substantial portions of the Software. |
| 18 | |
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | THE SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | #include <u.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include <signal.h> |
| 31 | #include <libc.h> |
| 32 | #undef rfork |
| 33 | |
| 34 | static void |
| 35 | nop(int x) |
| 36 | { |
| 37 | USED(x); |
| 38 | } |
| 39 | |
| 40 | int |
| 41 | p9rfork(int flags) |
| 42 | { |
| 43 | int pid, status; |
| 44 | int p[2]; |
| 45 | int n; |
| 46 | char buf[128], *q; |
| 47 | extern char **environ; |
| 48 | |
| 49 | if((flags&(RFPROC|RFFDG|RFMEM)) == (RFPROC|RFFDG)){ |
| 50 | /* check other flags before we commit */ |
| 51 | flags &= ~(RFPROC|RFFDG|RFENVG); |
| 52 | n = (flags & ~(RFNOTEG|RFNAMEG|RFNOWAIT|RFCENVG)); |
| 53 | if(n){ |
| 54 | werrstr("unknown flags %08ux in rfork", n); |
| 55 | return -1; |
| 56 | } |
| 57 | if(flags&RFNOWAIT){ |
| 58 | /* |
| 59 | * BUG - should put the signal handler back after we |
| 60 | * finish, but I just don't care. If a program calls with |
| 61 | * NOWAIT once, they're not likely to want child notes |
| 62 | * after that. |
| 63 | */ |
| 64 | signal(SIGCHLD, nop); |
| 65 | if(pipe(p) < 0) |
| 66 | return -1; |
| 67 | } |
| 68 | pid = fork(); |
| 69 | if(pid == -1) |
| 70 | return -1; |
| 71 | if(flags&RFNOWAIT){ |
| 72 | flags &= ~RFNOWAIT; |
| 73 | if(pid){ |
| 74 | /* |
| 75 | * Parent - wait for child to fork wait-free child. |
| 76 | * Then read pid from pipe. Assume pipe buffer can absorb the write. |
| 77 | */ |
| 78 | close(p[1]); |
| 79 | status = 0; |
| 80 | if(wait4(pid, &status, 0, 0) < 0){ |
| 81 | werrstr("pipe dance - wait4 - %r"); |
| 82 | close(p[0]); |
| 83 | return -1; |
| 84 | } |
Ian Lance Taylor | 7590e28 | 2013-06-25 10:44:25 -0700 | [diff] [blame] | 85 | n = (int)readn(p[0], buf, sizeof buf-1); |
Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 86 | close(p[0]); |
| 87 | if(!WIFEXITED(status) || WEXITSTATUS(status)!=0 || n <= 0){ |
| 88 | if(!WIFEXITED(status)) |
| 89 | werrstr("pipe dance - !exited 0x%ux", status); |
| 90 | else if(WEXITSTATUS(status) != 0) |
| 91 | werrstr("pipe dance - non-zero status 0x%ux", status); |
| 92 | else if(n < 0) |
| 93 | werrstr("pipe dance - pipe read error - %r"); |
| 94 | else if(n == 0) |
| 95 | werrstr("pipe dance - pipe read eof"); |
| 96 | else |
| 97 | werrstr("pipe dance - unknown failure"); |
| 98 | return -1; |
| 99 | } |
| 100 | buf[n] = 0; |
| 101 | if(buf[0] == 'x'){ |
| 102 | werrstr("%s", buf+2); |
| 103 | return -1; |
| 104 | } |
Ian Lance Taylor | 7590e28 | 2013-06-25 10:44:25 -0700 | [diff] [blame] | 105 | pid = (int)strtol(buf, &q, 0); |
Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 106 | }else{ |
| 107 | /* |
| 108 | * Child - fork a new child whose wait message can't |
| 109 | * get back to the parent because we're going to exit! |
| 110 | */ |
| 111 | signal(SIGCHLD, SIG_IGN); |
| 112 | close(p[0]); |
| 113 | pid = fork(); |
| 114 | if(pid){ |
| 115 | /* Child parent - send status over pipe and exit. */ |
| 116 | if(pid > 0) |
| 117 | fprint(p[1], "%d", pid); |
| 118 | else |
| 119 | fprint(p[1], "x %r"); |
| 120 | close(p[1]); |
| 121 | _exit(0); |
| 122 | }else{ |
| 123 | /* Child child - close pipe. */ |
| 124 | close(p[1]); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | if(pid != 0) |
| 129 | return pid; |
| 130 | if(flags&RFCENVG) |
| 131 | if(environ) |
| 132 | *environ = nil; |
| 133 | } |
| 134 | if(flags&RFPROC){ |
| 135 | werrstr("cannot use rfork for shared memory -- use libthread"); |
| 136 | return -1; |
| 137 | } |
| 138 | if(flags&RFNAMEG){ |
| 139 | /* XXX set $NAMESPACE to a new directory */ |
| 140 | flags &= ~RFNAMEG; |
| 141 | } |
| 142 | if(flags&RFNOTEG){ |
| 143 | setpgid(0, getpid()); |
| 144 | flags &= ~RFNOTEG; |
| 145 | } |
| 146 | if(flags&RFNOWAIT){ |
| 147 | werrstr("cannot use RFNOWAIT without RFPROC"); |
| 148 | return -1; |
| 149 | } |
| 150 | if(flags){ |
| 151 | werrstr("unknown flags %08ux in rfork", flags); |
| 152 | return -1; |
| 153 | } |
| 154 | return 0; |
| 155 | } |