github.com/tcnksm/go@v0.0.0-20141208075154-439b32936367/src/lib9/rfork.c (about)

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