Standardize License information
[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 <afs/param.h>
11 #include <afs/stds.h>
12
13 #include <stddef.h>
14 #include <stdlib.h>
15 #include <errno.h>
16
17 #include "procmgmt.h"
18 #include "pmgtprivate.h"
19
20
21 /* -----------------  Processes  ---------------- */
22
23
24 /*
25  * pmgt_ProcessSpawnVE() -- Spawn a process (Unix fork()/execve() semantics)
26  *
27  *     Returns pid of the child process ((pid_t)-1 on failure with errno set).
28  *
29  *     Notes: A senvp value of NULL results in Unix fork()/execv() semantics.
30  *            Open files are not inherited, except stdin, stdout, and stderr.
31  *            If child fails to exec() spath, its exit code is estatus.
32  *
33  * ASSUMPTIONS: sargv[0] is the same as spath (or its last component).
34  */
35 pid_t
36 pmgt_ProcessSpawnVE(const char *spath,
37                     char *sargv[],
38                     char *senvp[],
39                     int estatus)
40 {
41     pid_t pid;
42
43     /* create child process to exec spath */
44     if ((pid = fork()) == 0) {
45         /* child process */
46         int i;
47
48         /* close random fd's above stderr */
49         for (i = 3; i < 64; i++) {
50             close(i);
51         }
52
53         if (senvp) {
54             execve(spath, sargv, senvp);
55         } else {
56             execv(spath, sargv);
57         }
58
59         /* this point is only reached if exec() failed */
60         exit(estatus);
61     } else {
62         /* parent process */
63         return pid;
64     }
65 }