xref: /original-bsd/usr.bin/window/wwspawn.c (revision b4971bb3)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Wang at The University of California, Berkeley.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)wwspawn.c	8.1 (Berkeley) 06/06/93";
13 #endif /* not lint */
14 
15 #include "ww.h"
16 #include <sys/signal.h>
17 
18 /*
19  * There is a dead lock with vfork and closing of pseudo-ports.
20  * So we have to be sneaky about error reporting.
21  */
22 wwspawn(wp, file, argv)
23 register struct ww *wp;
24 char *file;
25 char **argv;
26 {
27 	int pid;
28 	int ret;
29 	char erred = 0;
30 	int s;
31 
32 	s = sigblock(sigmask(SIGCHLD));
33 	switch (pid = vfork()) {
34 	case -1:
35 		wwerrno = WWE_SYS;
36 		ret = -1;
37 		break;
38 	case 0:
39 		if (wwenviron(wp) >= 0)
40 			execvp(file, argv);
41 		erred = 1;
42 		_exit(1);
43 	default:
44 		if (erred) {
45 			wwerrno = WWE_SYS;
46 			ret = -1;
47 		} else {
48 			wp->ww_pid = pid;
49 			wp->ww_state = WWS_HASPROC;
50 			ret = pid;
51 		}
52 	}
53 	(void) sigsetmask(s);
54 	if (wp->ww_socket >= 0) {
55 		(void) close(wp->ww_socket);
56 		wp->ww_socket = -1;
57 	}
58 	return ret;
59 }
60