xref: /original-bsd/usr.bin/window/wwspawn.c (revision 7211505a)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)wwspawn.c	3.13 (Berkeley) 06/29/88";
20 #endif /* not lint */
21 
22 #include "ww.h"
23 #include <sys/signal.h>
24 
25 /*
26  * There is a dead lock with vfork and closing of pseudo-ports.
27  * So we have to be sneaky about error reporting.
28  */
29 wwspawn(wp, file, argv)
30 register struct ww *wp;
31 char *file;
32 char **argv;
33 {
34 	int pid;
35 	int ret;
36 	char erred = 0;
37 	int s;
38 
39 	s = sigblock(sigmask(SIGCHLD));
40 	switch (pid = vfork()) {
41 	case -1:
42 		wwerrno = WWE_SYS;
43 		ret = -1;
44 		break;
45 	case 0:
46 		if (wwenviron(wp) >= 0)
47 			execvp(file, argv);
48 		erred = 1;
49 		_exit(1);
50 	default:
51 		if (erred) {
52 			wwerrno = WWE_SYS;
53 			ret = -1;
54 		} else {
55 			wp->ww_pid = pid;
56 			wp->ww_state = WWS_HASPROC;
57 			ret = pid;
58 		}
59 	}
60 	(void) sigsetmask(s);
61 	if (wp->ww_socket >= 0) {
62 		(void) close(wp->ww_socket);
63 		wp->ww_socket = -1;
64 	}
65 	return ret;
66 }
67