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