1*ebfedea0SLionel Sambuc /*	$NetBSD: simple_exec.c,v 1.1.1.1 2011/04/13 18:15:43 elric Exp $	*/
2*ebfedea0SLionel Sambuc 
3*ebfedea0SLionel Sambuc /*
4*ebfedea0SLionel Sambuc  * Copyright (c) 1998 - 2001, 2004 Kungliga Tekniska Högskolan
5*ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6*ebfedea0SLionel Sambuc  * All rights reserved.
7*ebfedea0SLionel Sambuc  *
8*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10*ebfedea0SLionel Sambuc  * are met:
11*ebfedea0SLionel Sambuc  *
12*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14*ebfedea0SLionel Sambuc  *
15*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*ebfedea0SLionel Sambuc  *
19*ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20*ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21*ebfedea0SLionel Sambuc  *    without specific prior written permission.
22*ebfedea0SLionel Sambuc  *
23*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24*ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27*ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34*ebfedea0SLionel Sambuc  */
35*ebfedea0SLionel Sambuc 
36*ebfedea0SLionel Sambuc #include <config.h>
37*ebfedea0SLionel Sambuc 
38*ebfedea0SLionel Sambuc #include <stdarg.h>
39*ebfedea0SLionel Sambuc #include <stdlib.h>
40*ebfedea0SLionel Sambuc #ifdef HAVE_SYS_TYPES_H
41*ebfedea0SLionel Sambuc #include <sys/types.h>
42*ebfedea0SLionel Sambuc #endif
43*ebfedea0SLionel Sambuc #ifdef HAVE_SYS_WAIT_H
44*ebfedea0SLionel Sambuc #include <sys/wait.h>
45*ebfedea0SLionel Sambuc #endif
46*ebfedea0SLionel Sambuc #ifdef HAVE_UNISTD_H
47*ebfedea0SLionel Sambuc #include <unistd.h>
48*ebfedea0SLionel Sambuc #endif
49*ebfedea0SLionel Sambuc #include <errno.h>
50*ebfedea0SLionel Sambuc 
51*ebfedea0SLionel Sambuc #include <krb5/roken.h>
52*ebfedea0SLionel Sambuc 
53*ebfedea0SLionel Sambuc #define EX_NOEXEC	126
54*ebfedea0SLionel Sambuc #define EX_NOTFOUND	127
55*ebfedea0SLionel Sambuc 
56*ebfedea0SLionel Sambuc /* return values:
57*ebfedea0SLionel Sambuc    SE_E_UNSPECIFIED   on `unspecified' system errors
58*ebfedea0SLionel Sambuc    SE_E_FORKFAILED    on fork failures
59*ebfedea0SLionel Sambuc    SE_E_WAITPIDFAILED on waitpid errors
60*ebfedea0SLionel Sambuc    SE_E_EXECTIMEOUT   exec timeout
61*ebfedea0SLionel Sambuc    0-   is return value from subprocess
62*ebfedea0SLionel Sambuc    SE_E_NOEXEC        if the program couldn't be executed
63*ebfedea0SLionel Sambuc    SE_E_NOTFOUND      if the program couldn't be found
64*ebfedea0SLionel Sambuc    128- is 128 + signal that killed subprocess
65*ebfedea0SLionel Sambuc 
66*ebfedea0SLionel Sambuc    possible values `func' can return:
67*ebfedea0SLionel Sambuc    ((time_t)-2)		exit loop w/o killing child and return
68*ebfedea0SLionel Sambuc    			`exec timeout'/-4 from simple_exec
69*ebfedea0SLionel Sambuc    ((time_t)-1)		kill child with SIGTERM and wait for child to exit
70*ebfedea0SLionel Sambuc    0			don't timeout again
71*ebfedea0SLionel Sambuc    n			seconds to next timeout
72*ebfedea0SLionel Sambuc    */
73*ebfedea0SLionel Sambuc 
74*ebfedea0SLionel Sambuc static int sig_alarm;
75*ebfedea0SLionel Sambuc 
76*ebfedea0SLionel Sambuc static RETSIGTYPE
sigtimeout(int sig)77*ebfedea0SLionel Sambuc sigtimeout(int sig)
78*ebfedea0SLionel Sambuc {
79*ebfedea0SLionel Sambuc     sig_alarm = 1;
80*ebfedea0SLionel Sambuc     SIGRETURN(0);
81*ebfedea0SLionel Sambuc }
82*ebfedea0SLionel Sambuc 
83*ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
wait_for_process_timed(pid_t pid,time_t (* func)(void *),void * ptr,time_t timeout)84*ebfedea0SLionel Sambuc wait_for_process_timed(pid_t pid, time_t (*func)(void *),
85*ebfedea0SLionel Sambuc 		       void *ptr, time_t timeout)
86*ebfedea0SLionel Sambuc {
87*ebfedea0SLionel Sambuc     RETSIGTYPE (*old_func)(int sig) = NULL;
88*ebfedea0SLionel Sambuc     unsigned int oldtime = 0;
89*ebfedea0SLionel Sambuc     int ret;
90*ebfedea0SLionel Sambuc 
91*ebfedea0SLionel Sambuc     sig_alarm = 0;
92*ebfedea0SLionel Sambuc 
93*ebfedea0SLionel Sambuc     if (func) {
94*ebfedea0SLionel Sambuc 	old_func = signal(SIGALRM, sigtimeout);
95*ebfedea0SLionel Sambuc 	oldtime = alarm(timeout);
96*ebfedea0SLionel Sambuc     }
97*ebfedea0SLionel Sambuc 
98*ebfedea0SLionel Sambuc     while(1) {
99*ebfedea0SLionel Sambuc 	int status;
100*ebfedea0SLionel Sambuc 
101*ebfedea0SLionel Sambuc 	while(waitpid(pid, &status, 0) < 0) {
102*ebfedea0SLionel Sambuc 	    if (errno != EINTR) {
103*ebfedea0SLionel Sambuc 		ret = SE_E_WAITPIDFAILED;
104*ebfedea0SLionel Sambuc 		goto out;
105*ebfedea0SLionel Sambuc 	    }
106*ebfedea0SLionel Sambuc 	    if (func == NULL)
107*ebfedea0SLionel Sambuc 		continue;
108*ebfedea0SLionel Sambuc 	    if (sig_alarm == 0)
109*ebfedea0SLionel Sambuc 		continue;
110*ebfedea0SLionel Sambuc 	    timeout = (*func)(ptr);
111*ebfedea0SLionel Sambuc 	    if (timeout == (time_t)-1) {
112*ebfedea0SLionel Sambuc 		kill(pid, SIGTERM);
113*ebfedea0SLionel Sambuc 		continue;
114*ebfedea0SLionel Sambuc 	    } else if (timeout == (time_t)-2) {
115*ebfedea0SLionel Sambuc 		ret = SE_E_EXECTIMEOUT;
116*ebfedea0SLionel Sambuc 		goto out;
117*ebfedea0SLionel Sambuc 	    }
118*ebfedea0SLionel Sambuc 	    alarm(timeout);
119*ebfedea0SLionel Sambuc 	}
120*ebfedea0SLionel Sambuc 	if(WIFSTOPPED(status))
121*ebfedea0SLionel Sambuc 	    continue;
122*ebfedea0SLionel Sambuc 	if(WIFEXITED(status)) {
123*ebfedea0SLionel Sambuc 	    ret = WEXITSTATUS(status);
124*ebfedea0SLionel Sambuc 	    break;
125*ebfedea0SLionel Sambuc 	}
126*ebfedea0SLionel Sambuc 	if(WIFSIGNALED(status)) {
127*ebfedea0SLionel Sambuc 	    ret = WTERMSIG(status) + 128;
128*ebfedea0SLionel Sambuc 	    break;
129*ebfedea0SLionel Sambuc 	}
130*ebfedea0SLionel Sambuc     }
131*ebfedea0SLionel Sambuc  out:
132*ebfedea0SLionel Sambuc     if (func) {
133*ebfedea0SLionel Sambuc 	signal(SIGALRM, old_func);
134*ebfedea0SLionel Sambuc 	alarm(oldtime);
135*ebfedea0SLionel Sambuc     }
136*ebfedea0SLionel Sambuc     return ret;
137*ebfedea0SLionel Sambuc }
138*ebfedea0SLionel Sambuc 
139*ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
wait_for_process(pid_t pid)140*ebfedea0SLionel Sambuc wait_for_process(pid_t pid)
141*ebfedea0SLionel Sambuc {
142*ebfedea0SLionel Sambuc     return wait_for_process_timed(pid, NULL, NULL, 0);
143*ebfedea0SLionel Sambuc }
144*ebfedea0SLionel Sambuc 
145*ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
pipe_execv(FILE ** stdin_fd,FILE ** stdout_fd,FILE ** stderr_fd,const char * file,...)146*ebfedea0SLionel Sambuc pipe_execv(FILE **stdin_fd, FILE **stdout_fd, FILE **stderr_fd,
147*ebfedea0SLionel Sambuc 	   const char *file, ...)
148*ebfedea0SLionel Sambuc {
149*ebfedea0SLionel Sambuc     int in_fd[2], out_fd[2], err_fd[2];
150*ebfedea0SLionel Sambuc     pid_t pid;
151*ebfedea0SLionel Sambuc     va_list ap;
152*ebfedea0SLionel Sambuc     char **argv;
153*ebfedea0SLionel Sambuc 
154*ebfedea0SLionel Sambuc     if(stdin_fd != NULL)
155*ebfedea0SLionel Sambuc 	pipe(in_fd);
156*ebfedea0SLionel Sambuc     if(stdout_fd != NULL)
157*ebfedea0SLionel Sambuc 	pipe(out_fd);
158*ebfedea0SLionel Sambuc     if(stderr_fd != NULL)
159*ebfedea0SLionel Sambuc 	pipe(err_fd);
160*ebfedea0SLionel Sambuc     pid = fork();
161*ebfedea0SLionel Sambuc     switch(pid) {
162*ebfedea0SLionel Sambuc     case 0:
163*ebfedea0SLionel Sambuc 	va_start(ap, file);
164*ebfedea0SLionel Sambuc 	argv = vstrcollect(&ap);
165*ebfedea0SLionel Sambuc 	va_end(ap);
166*ebfedea0SLionel Sambuc 	if(argv == NULL)
167*ebfedea0SLionel Sambuc 	    exit(-1);
168*ebfedea0SLionel Sambuc 
169*ebfedea0SLionel Sambuc 	/* close pipes we're not interested in */
170*ebfedea0SLionel Sambuc 	if(stdin_fd != NULL)
171*ebfedea0SLionel Sambuc 	    close(in_fd[1]);
172*ebfedea0SLionel Sambuc 	if(stdout_fd != NULL)
173*ebfedea0SLionel Sambuc 	    close(out_fd[0]);
174*ebfedea0SLionel Sambuc 	if(stderr_fd != NULL)
175*ebfedea0SLionel Sambuc 	    close(err_fd[0]);
176*ebfedea0SLionel Sambuc 
177*ebfedea0SLionel Sambuc 	/* pipe everything caller doesn't care about to /dev/null */
178*ebfedea0SLionel Sambuc 	if(stdin_fd == NULL)
179*ebfedea0SLionel Sambuc 	    in_fd[0] = open(_PATH_DEVNULL, O_RDONLY);
180*ebfedea0SLionel Sambuc 	if(stdout_fd == NULL)
181*ebfedea0SLionel Sambuc 	    out_fd[1] = open(_PATH_DEVNULL, O_WRONLY);
182*ebfedea0SLionel Sambuc 	if(stderr_fd == NULL)
183*ebfedea0SLionel Sambuc 	    err_fd[1] = open(_PATH_DEVNULL, O_WRONLY);
184*ebfedea0SLionel Sambuc 
185*ebfedea0SLionel Sambuc 	/* move to proper descriptors */
186*ebfedea0SLionel Sambuc 	if(in_fd[0] != STDIN_FILENO) {
187*ebfedea0SLionel Sambuc 	    dup2(in_fd[0], STDIN_FILENO);
188*ebfedea0SLionel Sambuc 	    close(in_fd[0]);
189*ebfedea0SLionel Sambuc 	}
190*ebfedea0SLionel Sambuc 	if(out_fd[1] != STDOUT_FILENO) {
191*ebfedea0SLionel Sambuc 	    dup2(out_fd[1], STDOUT_FILENO);
192*ebfedea0SLionel Sambuc 	    close(out_fd[1]);
193*ebfedea0SLionel Sambuc 	}
194*ebfedea0SLionel Sambuc 	if(err_fd[1] != STDERR_FILENO) {
195*ebfedea0SLionel Sambuc 	    dup2(err_fd[1], STDERR_FILENO);
196*ebfedea0SLionel Sambuc 	    close(err_fd[1]);
197*ebfedea0SLionel Sambuc 	}
198*ebfedea0SLionel Sambuc 
199*ebfedea0SLionel Sambuc 	closefrom(3);
200*ebfedea0SLionel Sambuc 
201*ebfedea0SLionel Sambuc 	execv(file, argv);
202*ebfedea0SLionel Sambuc 	exit((errno == ENOENT) ? EX_NOTFOUND : EX_NOEXEC);
203*ebfedea0SLionel Sambuc     case -1:
204*ebfedea0SLionel Sambuc 	if(stdin_fd != NULL) {
205*ebfedea0SLionel Sambuc 	    close(in_fd[0]);
206*ebfedea0SLionel Sambuc 	    close(in_fd[1]);
207*ebfedea0SLionel Sambuc 	}
208*ebfedea0SLionel Sambuc 	if(stdout_fd != NULL) {
209*ebfedea0SLionel Sambuc 	    close(out_fd[0]);
210*ebfedea0SLionel Sambuc 	    close(out_fd[1]);
211*ebfedea0SLionel Sambuc 	}
212*ebfedea0SLionel Sambuc 	if(stderr_fd != NULL) {
213*ebfedea0SLionel Sambuc 	    close(err_fd[0]);
214*ebfedea0SLionel Sambuc 	    close(err_fd[1]);
215*ebfedea0SLionel Sambuc 	}
216*ebfedea0SLionel Sambuc 	return SE_E_FORKFAILED;
217*ebfedea0SLionel Sambuc     default:
218*ebfedea0SLionel Sambuc 	if(stdin_fd != NULL) {
219*ebfedea0SLionel Sambuc 	    close(in_fd[0]);
220*ebfedea0SLionel Sambuc 	    *stdin_fd = fdopen(in_fd[1], "w");
221*ebfedea0SLionel Sambuc 	}
222*ebfedea0SLionel Sambuc 	if(stdout_fd != NULL) {
223*ebfedea0SLionel Sambuc 	    close(out_fd[1]);
224*ebfedea0SLionel Sambuc 	    *stdout_fd = fdopen(out_fd[0], "r");
225*ebfedea0SLionel Sambuc 	}
226*ebfedea0SLionel Sambuc 	if(stderr_fd != NULL) {
227*ebfedea0SLionel Sambuc 	    close(err_fd[1]);
228*ebfedea0SLionel Sambuc 	    *stderr_fd = fdopen(err_fd[0], "r");
229*ebfedea0SLionel Sambuc 	}
230*ebfedea0SLionel Sambuc     }
231*ebfedea0SLionel Sambuc     return pid;
232*ebfedea0SLionel Sambuc }
233*ebfedea0SLionel Sambuc 
234*ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execvp_timed(const char * file,char * const args[],time_t (* func)(void *),void * ptr,time_t timeout)235*ebfedea0SLionel Sambuc simple_execvp_timed(const char *file, char *const args[],
236*ebfedea0SLionel Sambuc 		    time_t (*func)(void *), void *ptr, time_t timeout)
237*ebfedea0SLionel Sambuc {
238*ebfedea0SLionel Sambuc     pid_t pid = fork();
239*ebfedea0SLionel Sambuc     switch(pid){
240*ebfedea0SLionel Sambuc     case -1:
241*ebfedea0SLionel Sambuc 	return SE_E_FORKFAILED;
242*ebfedea0SLionel Sambuc     case 0:
243*ebfedea0SLionel Sambuc 	execvp(file, args);
244*ebfedea0SLionel Sambuc 	exit((errno == ENOENT) ? EX_NOTFOUND : EX_NOEXEC);
245*ebfedea0SLionel Sambuc     default:
246*ebfedea0SLionel Sambuc 	return wait_for_process_timed(pid, func, ptr, timeout);
247*ebfedea0SLionel Sambuc     }
248*ebfedea0SLionel Sambuc }
249*ebfedea0SLionel Sambuc 
250*ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execvp(const char * file,char * const args[])251*ebfedea0SLionel Sambuc simple_execvp(const char *file, char *const args[])
252*ebfedea0SLionel Sambuc {
253*ebfedea0SLionel Sambuc     return simple_execvp_timed(file, args, NULL, NULL, 0);
254*ebfedea0SLionel Sambuc }
255*ebfedea0SLionel Sambuc 
256*ebfedea0SLionel Sambuc /* gee, I'd like a execvpe */
257*ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execve_timed(const char * file,char * const args[],char * const envp[],time_t (* func)(void *),void * ptr,time_t timeout)258*ebfedea0SLionel Sambuc simple_execve_timed(const char *file, char *const args[], char *const envp[],
259*ebfedea0SLionel Sambuc 		    time_t (*func)(void *), void *ptr, time_t timeout)
260*ebfedea0SLionel Sambuc {
261*ebfedea0SLionel Sambuc     pid_t pid = fork();
262*ebfedea0SLionel Sambuc     switch(pid){
263*ebfedea0SLionel Sambuc     case -1:
264*ebfedea0SLionel Sambuc 	return SE_E_FORKFAILED;
265*ebfedea0SLionel Sambuc     case 0:
266*ebfedea0SLionel Sambuc 	execve(file, args, envp);
267*ebfedea0SLionel Sambuc 	exit((errno == ENOENT) ? EX_NOTFOUND : EX_NOEXEC);
268*ebfedea0SLionel Sambuc     default:
269*ebfedea0SLionel Sambuc 	return wait_for_process_timed(pid, func, ptr, timeout);
270*ebfedea0SLionel Sambuc     }
271*ebfedea0SLionel Sambuc }
272*ebfedea0SLionel Sambuc 
273*ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execve(const char * file,char * const args[],char * const envp[])274*ebfedea0SLionel Sambuc simple_execve(const char *file, char *const args[], char *const envp[])
275*ebfedea0SLionel Sambuc {
276*ebfedea0SLionel Sambuc     return simple_execve_timed(file, args, envp, NULL, NULL, 0);
277*ebfedea0SLionel Sambuc }
278*ebfedea0SLionel Sambuc 
279*ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execlp(const char * file,...)280*ebfedea0SLionel Sambuc simple_execlp(const char *file, ...)
281*ebfedea0SLionel Sambuc {
282*ebfedea0SLionel Sambuc     va_list ap;
283*ebfedea0SLionel Sambuc     char **argv;
284*ebfedea0SLionel Sambuc     int ret;
285*ebfedea0SLionel Sambuc 
286*ebfedea0SLionel Sambuc     va_start(ap, file);
287*ebfedea0SLionel Sambuc     argv = vstrcollect(&ap);
288*ebfedea0SLionel Sambuc     va_end(ap);
289*ebfedea0SLionel Sambuc     if(argv == NULL)
290*ebfedea0SLionel Sambuc 	return SE_E_UNSPECIFIED;
291*ebfedea0SLionel Sambuc     ret = simple_execvp(file, argv);
292*ebfedea0SLionel Sambuc     free(argv);
293*ebfedea0SLionel Sambuc     return ret;
294*ebfedea0SLionel Sambuc }
295*ebfedea0SLionel Sambuc 
296*ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execle(const char * file,...)297*ebfedea0SLionel Sambuc simple_execle(const char *file, ... /* ,char *const envp[] */)
298*ebfedea0SLionel Sambuc {
299*ebfedea0SLionel Sambuc     va_list ap;
300*ebfedea0SLionel Sambuc     char **argv;
301*ebfedea0SLionel Sambuc     char *const* envp;
302*ebfedea0SLionel Sambuc     int ret;
303*ebfedea0SLionel Sambuc 
304*ebfedea0SLionel Sambuc     va_start(ap, file);
305*ebfedea0SLionel Sambuc     argv = vstrcollect(&ap);
306*ebfedea0SLionel Sambuc     envp = va_arg(ap, char **);
307*ebfedea0SLionel Sambuc     va_end(ap);
308*ebfedea0SLionel Sambuc     if(argv == NULL)
309*ebfedea0SLionel Sambuc 	return SE_E_UNSPECIFIED;
310*ebfedea0SLionel Sambuc     ret = simple_execve(file, argv, envp);
311*ebfedea0SLionel Sambuc     free(argv);
312*ebfedea0SLionel Sambuc     return ret;
313*ebfedea0SLionel Sambuc }
314