1*a824f5a1SJean-Baptiste Boric /* $NetBSD: fexec.c,v 1.1.1.3 2009/08/06 16:55:26 joerg Exp $ */
2*a824f5a1SJean-Baptiste Boric
3*a824f5a1SJean-Baptiste Boric /*-
4*a824f5a1SJean-Baptiste Boric * Copyright (c) 2003 The NetBSD Foundation, Inc.
5*a824f5a1SJean-Baptiste Boric * All rights reserved.
6*a824f5a1SJean-Baptiste Boric *
7*a824f5a1SJean-Baptiste Boric * This code is derived from software contributed to The NetBSD Foundation
8*a824f5a1SJean-Baptiste Boric * by Matthias Scheler.
9*a824f5a1SJean-Baptiste Boric *
10*a824f5a1SJean-Baptiste Boric * Redistribution and use in source and binary forms, with or without
11*a824f5a1SJean-Baptiste Boric * modification, are permitted provided that the following conditions
12*a824f5a1SJean-Baptiste Boric * are met:
13*a824f5a1SJean-Baptiste Boric * 1. Redistributions of source code must retain the above copyright
14*a824f5a1SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer.
15*a824f5a1SJean-Baptiste Boric * 2. Redistributions in binary form must reproduce the above copyright
16*a824f5a1SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer in the
17*a824f5a1SJean-Baptiste Boric * documentation and/or other materials provided with the distribution.
18*a824f5a1SJean-Baptiste Boric *
19*a824f5a1SJean-Baptiste Boric * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*a824f5a1SJean-Baptiste Boric * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*a824f5a1SJean-Baptiste Boric * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*a824f5a1SJean-Baptiste Boric * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*a824f5a1SJean-Baptiste Boric * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*a824f5a1SJean-Baptiste Boric * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*a824f5a1SJean-Baptiste Boric * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*a824f5a1SJean-Baptiste Boric * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*a824f5a1SJean-Baptiste Boric * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*a824f5a1SJean-Baptiste Boric * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*a824f5a1SJean-Baptiste Boric * POSSIBILITY OF SUCH DAMAGE.
30*a824f5a1SJean-Baptiste Boric */
31*a824f5a1SJean-Baptiste Boric
32*a824f5a1SJean-Baptiste Boric
33*a824f5a1SJean-Baptiste Boric #if HAVE_CONFIG_H
34*a824f5a1SJean-Baptiste Boric #include "config.h"
35*a824f5a1SJean-Baptiste Boric #endif
36*a824f5a1SJean-Baptiste Boric #include <nbcompat.h>
37*a824f5a1SJean-Baptiste Boric #if HAVE_SYS_CDEFS_H
38*a824f5a1SJean-Baptiste Boric #include <sys/cdefs.h>
39*a824f5a1SJean-Baptiste Boric #endif
40*a824f5a1SJean-Baptiste Boric #if HAVE_SYS_WAIT_H
41*a824f5a1SJean-Baptiste Boric #include <sys/wait.h>
42*a824f5a1SJean-Baptiste Boric #endif
43*a824f5a1SJean-Baptiste Boric
44*a824f5a1SJean-Baptiste Boric #if HAVE_ERR_H
45*a824f5a1SJean-Baptiste Boric #include <err.h>
46*a824f5a1SJean-Baptiste Boric #endif
47*a824f5a1SJean-Baptiste Boric #if HAVE_ERRNO_H
48*a824f5a1SJean-Baptiste Boric #include <errno.h>
49*a824f5a1SJean-Baptiste Boric #endif
50*a824f5a1SJean-Baptiste Boric #if HAVE_STDARG_H
51*a824f5a1SJean-Baptiste Boric #include <stdarg.h>
52*a824f5a1SJean-Baptiste Boric #endif
53*a824f5a1SJean-Baptiste Boric #if HAVE_STDLIB_H
54*a824f5a1SJean-Baptiste Boric #include <stdlib.h>
55*a824f5a1SJean-Baptiste Boric #endif
56*a824f5a1SJean-Baptiste Boric #if HAVE_UNISTD_H
57*a824f5a1SJean-Baptiste Boric #include <unistd.h>
58*a824f5a1SJean-Baptiste Boric #endif
59*a824f5a1SJean-Baptiste Boric
60*a824f5a1SJean-Baptiste Boric #include "lib.h"
61*a824f5a1SJean-Baptiste Boric
62*a824f5a1SJean-Baptiste Boric __RCSID("$NetBSD: fexec.c,v 1.1.1.3 2009/08/06 16:55:26 joerg Exp $");
63*a824f5a1SJean-Baptiste Boric
64*a824f5a1SJean-Baptiste Boric static int vfcexec(const char *, int, const char *, va_list);
65*a824f5a1SJean-Baptiste Boric
66*a824f5a1SJean-Baptiste Boric /*
67*a824f5a1SJean-Baptiste Boric * fork, then change current working directory to path and
68*a824f5a1SJean-Baptiste Boric * execute the command and arguments in the argv array.
69*a824f5a1SJean-Baptiste Boric * wait for the command to finish, then return the exit status.
70*a824f5a1SJean-Baptiste Boric */
71*a824f5a1SJean-Baptiste Boric int
pfcexec(const char * path,const char * file,const char ** argv)72*a824f5a1SJean-Baptiste Boric pfcexec(const char *path, const char *file, const char **argv)
73*a824f5a1SJean-Baptiste Boric {
74*a824f5a1SJean-Baptiste Boric pid_t child;
75*a824f5a1SJean-Baptiste Boric int status;
76*a824f5a1SJean-Baptiste Boric
77*a824f5a1SJean-Baptiste Boric child = vfork();
78*a824f5a1SJean-Baptiste Boric switch (child) {
79*a824f5a1SJean-Baptiste Boric case 0:
80*a824f5a1SJean-Baptiste Boric if ((path != NULL) && (chdir(path) < 0))
81*a824f5a1SJean-Baptiste Boric _exit(127);
82*a824f5a1SJean-Baptiste Boric
83*a824f5a1SJean-Baptiste Boric (void)execvp(file, __UNCONST(argv));
84*a824f5a1SJean-Baptiste Boric _exit(127);
85*a824f5a1SJean-Baptiste Boric /* NOTREACHED */
86*a824f5a1SJean-Baptiste Boric case -1:
87*a824f5a1SJean-Baptiste Boric return -1;
88*a824f5a1SJean-Baptiste Boric }
89*a824f5a1SJean-Baptiste Boric
90*a824f5a1SJean-Baptiste Boric while (waitpid(child, &status, 0) < 0) {
91*a824f5a1SJean-Baptiste Boric if (errno != EINTR)
92*a824f5a1SJean-Baptiste Boric return -1;
93*a824f5a1SJean-Baptiste Boric }
94*a824f5a1SJean-Baptiste Boric
95*a824f5a1SJean-Baptiste Boric if (!WIFEXITED(status))
96*a824f5a1SJean-Baptiste Boric return -1;
97*a824f5a1SJean-Baptiste Boric
98*a824f5a1SJean-Baptiste Boric return WEXITSTATUS(status);
99*a824f5a1SJean-Baptiste Boric }
100*a824f5a1SJean-Baptiste Boric
101*a824f5a1SJean-Baptiste Boric static int
vfcexec(const char * path,int skipempty,const char * arg,va_list ap)102*a824f5a1SJean-Baptiste Boric vfcexec(const char *path, int skipempty, const char *arg, va_list ap)
103*a824f5a1SJean-Baptiste Boric {
104*a824f5a1SJean-Baptiste Boric const char **argv;
105*a824f5a1SJean-Baptiste Boric size_t argv_size, argc;
106*a824f5a1SJean-Baptiste Boric int retval;
107*a824f5a1SJean-Baptiste Boric
108*a824f5a1SJean-Baptiste Boric argv_size = 16;
109*a824f5a1SJean-Baptiste Boric argv = xcalloc(argv_size, sizeof(*argv));
110*a824f5a1SJean-Baptiste Boric
111*a824f5a1SJean-Baptiste Boric argv[0] = arg;
112*a824f5a1SJean-Baptiste Boric argc = 1;
113*a824f5a1SJean-Baptiste Boric
114*a824f5a1SJean-Baptiste Boric do {
115*a824f5a1SJean-Baptiste Boric if (argc == argv_size) {
116*a824f5a1SJean-Baptiste Boric argv_size *= 2;
117*a824f5a1SJean-Baptiste Boric argv = xrealloc(argv, argv_size * sizeof(*argv));
118*a824f5a1SJean-Baptiste Boric }
119*a824f5a1SJean-Baptiste Boric arg = va_arg(ap, const char *);
120*a824f5a1SJean-Baptiste Boric if (skipempty && arg && strlen(arg) == 0)
121*a824f5a1SJean-Baptiste Boric continue;
122*a824f5a1SJean-Baptiste Boric argv[argc++] = arg;
123*a824f5a1SJean-Baptiste Boric } while (arg != NULL);
124*a824f5a1SJean-Baptiste Boric
125*a824f5a1SJean-Baptiste Boric retval = pfcexec(path, argv[0], argv);
126*a824f5a1SJean-Baptiste Boric free(argv);
127*a824f5a1SJean-Baptiste Boric return retval;
128*a824f5a1SJean-Baptiste Boric }
129*a824f5a1SJean-Baptiste Boric
130*a824f5a1SJean-Baptiste Boric int
fexec(const char * arg,...)131*a824f5a1SJean-Baptiste Boric fexec(const char *arg, ...)
132*a824f5a1SJean-Baptiste Boric {
133*a824f5a1SJean-Baptiste Boric va_list ap;
134*a824f5a1SJean-Baptiste Boric int result;
135*a824f5a1SJean-Baptiste Boric
136*a824f5a1SJean-Baptiste Boric va_start(ap, arg);
137*a824f5a1SJean-Baptiste Boric result = vfcexec(NULL, 0, arg, ap);
138*a824f5a1SJean-Baptiste Boric va_end(ap);
139*a824f5a1SJean-Baptiste Boric
140*a824f5a1SJean-Baptiste Boric return result;
141*a824f5a1SJean-Baptiste Boric }
142*a824f5a1SJean-Baptiste Boric
143*a824f5a1SJean-Baptiste Boric int
fexec_skipempty(const char * arg,...)144*a824f5a1SJean-Baptiste Boric fexec_skipempty(const char *arg, ...)
145*a824f5a1SJean-Baptiste Boric {
146*a824f5a1SJean-Baptiste Boric va_list ap;
147*a824f5a1SJean-Baptiste Boric int result;
148*a824f5a1SJean-Baptiste Boric
149*a824f5a1SJean-Baptiste Boric va_start(ap, arg);
150*a824f5a1SJean-Baptiste Boric result = vfcexec(NULL, 1, arg, ap);
151*a824f5a1SJean-Baptiste Boric va_end(ap);
152*a824f5a1SJean-Baptiste Boric
153*a824f5a1SJean-Baptiste Boric return result;
154*a824f5a1SJean-Baptiste Boric }
155*a824f5a1SJean-Baptiste Boric
156*a824f5a1SJean-Baptiste Boric int
fcexec(const char * path,const char * arg,...)157*a824f5a1SJean-Baptiste Boric fcexec(const char *path, const char *arg, ...)
158*a824f5a1SJean-Baptiste Boric {
159*a824f5a1SJean-Baptiste Boric va_list ap;
160*a824f5a1SJean-Baptiste Boric int result;
161*a824f5a1SJean-Baptiste Boric
162*a824f5a1SJean-Baptiste Boric va_start(ap, arg);
163*a824f5a1SJean-Baptiste Boric result = vfcexec(path, 0, arg, ap);
164*a824f5a1SJean-Baptiste Boric va_end(ap);
165*a824f5a1SJean-Baptiste Boric
166*a824f5a1SJean-Baptiste Boric return result;
167*a824f5a1SJean-Baptiste Boric }
168