1 /*
2  * Copyright (C) 1997-2009, Michael Jennings
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies of the Software, its documentation and marketing & publicity
13  * materials, and acknowledgment shall be given in the documentation, materials
14  * and software packages that this Software was used.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 static const char cvs_ident[] = "$Id: system.c 38480 2009-01-06 09:08:48Z mej $";
25 
26 #include "../config.h"
27 #include "feature.h"
28 
29 #include <unistd.h>
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <signal.h>
35 #ifdef HAVE_SYS_WAIT_H
36 #  include <sys/wait.h>
37 #endif
38 
39 #include "command.h"
40 #include "misc.h"
41 #include "system.h"
42 
43 /*static eterm_sighandler_t old_handler = (eterm_sighandler_t) NULL;*/
44 
45 int
wait_for_chld(int system_pid)46 wait_for_chld(int system_pid)
47 {
48     int pid, status = 0, save_errno = errno, code;
49 
50     D_OPTIONS(("wait_for_chld(%ld) called.\n", system_pid));
51 
52     while (1) {
53         do {
54             errno = 0;
55             usleep(10);
56         } while ((((pid = waitpid(system_pid, &status, WNOHANG)) == -1) && (errno == EINTR)) || !pid);
57         /* If the child that exited is the command we spawned, or if the
58            child exited before fork() returned in the parent, it must be
59            our immediate child that exited.  We exit gracefully. */
60         if ((pid == -1) && (errno == ECHILD)) { /* No children exist.  Punt. */
61             errno = save_errno;
62             break;
63         }
64         D_OPTIONS(("%ld exited.\n", pid));
65         if (pid == system_pid || system_pid == -1) {
66             if (WIFEXITED(status)) {
67                 code = WEXITSTATUS(status);
68                 D_OPTIONS(("Child process exited with return code %lu\n", code));
69             } else if (WIFSIGNALED(status)) {
70                 code = WTERMSIG(status);
71                 D_OPTIONS(("Child process was terminated by unhandled signal %lu\n", code));
72             } else {
73                 code = 0;
74             }
75             return (code);
76         }
77         errno = save_errno;
78     }
79     return 0;
80 }
81 
82 /* Replace the system() call with a fork-and-exec that unprivs the child process */
83 int
system_wait(char * command)84 system_wait(char *command)
85 {
86     pid_t pid;
87 
88     D_OPTIONS(("system_wait(%s) called.\n", command));
89 
90     pid = system_no_wait(command);
91     return (wait_for_chld(pid));
92 }
93 
94 pid_t
system_no_wait(char * command)95 system_no_wait(char *command)
96 {
97     pid_t pid;
98 
99     D_OPTIONS(("system_no_wait(%s) called.\n", command));
100 
101     if (!(pid = fork())) {
102         setreuid(my_ruid, my_ruid);
103         setregid(my_rgid, my_rgid);
104         execl("/bin/sh", "sh", "-c", command, (char *) NULL);
105         libast_print_error("execl(%s) failed -- %s\n", command, strerror(errno));
106         exit(EXIT_FAILURE);
107     }
108     D_OPTIONS(("%d:  fork() returned %d\n", getpid(), pid));
109     return (pid);
110 }
111