wmii

git clone git://oldgit.suckless.org/wmii/
Log | Files | Refs | README | LICENSE

spawn3.c (797B)


      1 /* Copyright ©2008-2010 Kris Maglione <maglione.k at Gmail>
      2  * See LICENSE file for license details.
      3  */
      4 #include <errno.h>
      5 #include <unistd.h>
      6 #include "util.h"
      7 
      8 int
      9 spawn3(int fd[3], const char *file, char *argv[]) {
     10 	/* Some ideas from Russ Cox's libthread port. */
     11 	int p[2];
     12 	int pid;
     13 
     14 	if(pipe(p) < 0)
     15 		return -1;
     16 	closeexec(p[1]);
     17 
     18 	switch(pid = doublefork()) {
     19 	case 0:
     20 		dup2(fd[0], 0);
     21 		dup2(fd[1], 1);
     22 		dup2(fd[2], 2);
     23 
     24 		execvp(file, argv);
     25 		write(p[1], &errno, sizeof errno);
     26 		exit(1);
     27 		break;
     28 	default:
     29 		close(p[1]);
     30 		if(read(p[0], &errno, sizeof errno) == sizeof errno)
     31 			pid = -1;
     32 		close(p[0]);
     33 		break;
     34 	case -1: /* can't happen */
     35 		break;
     36 	}
     37 
     38 	close(fd[0]);
     39 	/* These could fail if any of these was also a previous fd. */
     40 	close(fd[1]);
     41 	close(fd[2]);
     42 	return pid;
     43 }