Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / procmgmt / procmgmt_unix.c
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
4 *
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12 #include <afs/stds.h>
13
14 #include <roken.h>
15
16 #include "procmgmt.h"
17 #include "pmgtprivate.h"
18
19
20 /* ----------------- Processes ---------------- */
21
22
23 /*
24 * pmgt_ProcessSpawnVE() -- Spawn a process (Unix fork()/execve() semantics)
25 *
26 * Returns pid of the child process ((pid_t)-1 on failure with errno set).
27 *
28 * Notes: A senvp value of NULL results in Unix fork()/execv() semantics.
29 * Open files are not inherited, except stdin, stdout, and stderr.
30 * If child fails to exec() spath, its exit code is estatus.
31 *
32 * If provided, a signal mask will be set for the spawned process.
33 *
34 * ASSUMPTIONS: sargv[0] is the same as spath (or its last component).
35 */
36 pid_t
37 pmgt_ProcessSpawnVE(const char *spath, char *sargv[], char *senvp[],
38 int estatus, sigset_t *mask)
39 {
40 pid_t pid;
41
42 /* create child process to exec spath */
43 if ((pid = fork()) == 0) {
44 /* child process */
45 int i;
46
47 /* close random fd's above stderr */
48 for (i = 3; i < 64; i++) {
49 close(i);
50 }
51
52 sigprocmask(SIG_SETMASK, mask, NULL);
53
54 if (senvp) {
55 execve(spath, sargv, senvp);
56 } else {
57 execv(spath, sargv);
58 }
59
60 /* this point is only reached if exec() failed */
61 exit(estatus);
62 } else {
63 /* parent process */
64 return pid;
65 }
66 }