lib/util/run_cmd: prevent zombies in samba_runcmd_send on timeout
[Samba.git] / lib / util / util_runcmd.c
blobef3402ad33fccd305393a16b5cb1b4355ccb4ede
1 /*
2 Unix SMB/CIFS implementation.
4 run a child command
6 Copyright (C) Andrew Tridgell 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 this runs a child command with stdout and stderr going to the Samba
25 log
28 #include "includes.h"
29 #include "system/filesys.h"
30 #include "../lib/util/tevent_unix.h"
31 #include "../lib/util/util_runcmd.h"
32 #include "../lib/util/tfork.h"
33 #include "../lib/util/sys_rw.h"
35 static int samba_runcmd_state_destructor(struct samba_runcmd_state *state)
37 if (state->tfork != NULL) {
38 tfork_destroy(&state->tfork);
40 state->pid = -1;
42 if (state->fd_stdin != -1) {
43 close(state->fd_stdin);
45 return 0;
48 static void samba_runcmd_io_handler(struct tevent_context *ev,
49 struct tevent_fd *fde,
50 uint16_t flags,
51 void *private_data);
54 run a command as a child process, with a timeout.
56 any stdout/stderr from the child will appear in the Samba logs with
57 the specified log levels
59 struct tevent_req *samba_runcmd_send(TALLOC_CTX *mem_ctx,
60 struct tevent_context *ev,
61 struct timeval endtime,
62 int stdout_log_level,
63 int stderr_log_level,
64 const char * const *argv0, ...)
66 struct tevent_req *req;
67 struct samba_runcmd_state *state;
68 int p1[2], p2[2], p3[2];
69 char **argv;
70 va_list ap;
72 if (argv0 == NULL) {
73 return NULL;
76 req = tevent_req_create(mem_ctx, &state,
77 struct samba_runcmd_state);
78 if (req == NULL) {
79 return NULL;
82 state->stdout_log_level = stdout_log_level;
83 state->stderr_log_level = stderr_log_level;
84 state->fd_stdin = -1;
86 state->arg0 = talloc_strdup(state, argv0[0]);
87 if (tevent_req_nomem(state->arg0, req)) {
88 return tevent_req_post(req, ev);
91 if (pipe(p1) != 0) {
92 tevent_req_error(req, errno);
93 return tevent_req_post(req, ev);
95 if (pipe(p2) != 0) {
96 close(p1[0]);
97 close(p1[1]);
98 tevent_req_error(req, errno);
99 return tevent_req_post(req, ev);
101 if (pipe(p3) != 0) {
102 close(p1[0]);
103 close(p1[1]);
104 close(p2[0]);
105 close(p2[1]);
106 tevent_req_error(req, errno);
107 return tevent_req_post(req, ev);
110 state->tfork = tfork_create();
111 if (state->tfork == NULL) {
112 printf("state->tfork == NULL\n");
113 close(p1[0]);
114 close(p1[1]);
115 close(p2[0]);
116 close(p2[1]);
117 close(p3[0]);
118 close(p3[1]);
119 tevent_req_error(req, errno);
120 return tevent_req_post(req, ev);
122 state->pid = tfork_child_pid(state->tfork);
123 if (state->pid != 0) {
124 /* the parent */
125 close(p1[1]);
126 close(p2[1]);
127 close(p3[0]);
128 state->fd_stdout = p1[0];
129 state->fd_stderr = p2[0];
130 state->fd_stdin = p3[1];
131 state->fd_status = tfork_event_fd(state->tfork);
133 set_blocking(state->fd_stdout, false);
134 set_blocking(state->fd_stderr, false);
135 set_blocking(state->fd_stdin, false);
136 set_blocking(state->fd_status, false);
138 smb_set_close_on_exec(state->fd_stdin);
139 smb_set_close_on_exec(state->fd_stdout);
140 smb_set_close_on_exec(state->fd_stderr);
141 smb_set_close_on_exec(state->fd_status);
143 talloc_set_destructor(state, samba_runcmd_state_destructor);
145 state->fde_stdout = tevent_add_fd(ev, state,
146 state->fd_stdout,
147 TEVENT_FD_READ,
148 samba_runcmd_io_handler,
149 req);
150 if (tevent_req_nomem(state->fde_stdout, req)) {
151 close(state->fd_stdout);
152 close(state->fd_stderr);
153 close(state->fd_status);
154 return tevent_req_post(req, ev);
156 tevent_fd_set_auto_close(state->fde_stdout);
158 state->fde_stderr = tevent_add_fd(ev, state,
159 state->fd_stderr,
160 TEVENT_FD_READ,
161 samba_runcmd_io_handler,
162 req);
163 if (tevent_req_nomem(state->fde_stdout, req)) {
164 close(state->fd_stdout);
165 close(state->fd_stderr);
166 close(state->fd_status);
167 return tevent_req_post(req, ev);
169 tevent_fd_set_auto_close(state->fde_stderr);
171 state->fde_status = tevent_add_fd(ev, state,
172 state->fd_status,
173 TEVENT_FD_READ,
174 samba_runcmd_io_handler,
175 req);
176 if (tevent_req_nomem(state->fde_stdout, req)) {
177 close(state->fd_stdout);
178 close(state->fd_stderr);
179 close(state->fd_status);
180 return tevent_req_post(req, ev);
182 tevent_fd_set_auto_close(state->fde_status);
184 if (!timeval_is_zero(&endtime)) {
185 tevent_req_set_endtime(req, ev, endtime);
188 return req;
191 /* the child */
192 close(p1[0]);
193 close(p2[0]);
194 close(p3[1]);
195 close(0);
196 close(1);
197 close(2);
199 /* we want to ensure that all of the network sockets we had
200 open are closed */
201 tevent_re_initialise(ev);
203 /* setup for logging to go to the parents debug log */
204 dup2(p3[0], 0);
205 dup2(p1[1], 1);
206 dup2(p2[1], 2);
208 close(p1[1]);
209 close(p2[1]);
210 close(p3[0]);
212 argv = str_list_copy(state, discard_const_p(const char *, argv0));
213 if (!argv) {
214 fprintf(stderr, "Out of memory in child\n");
215 _exit(255);
218 va_start(ap, argv0);
219 while (1) {
220 const char **l;
221 char *arg = va_arg(ap, char *);
222 if (arg == NULL) break;
223 l = discard_const_p(const char *, argv);
224 l = str_list_add(l, arg);
225 if (l == NULL) {
226 fprintf(stderr, "Out of memory in child\n");
227 _exit(255);
229 argv = discard_const_p(char *, l);
231 va_end(ap);
233 (void)execvp(state->arg0, argv);
234 fprintf(stderr, "Failed to exec child - %s\n", strerror(errno));
235 _exit(255);
236 return NULL;
240 handle stdout/stderr from the child
242 static void samba_runcmd_io_handler(struct tevent_context *ev,
243 struct tevent_fd *fde,
244 uint16_t flags,
245 void *private_data)
247 struct tevent_req *req = talloc_get_type_abort(private_data,
248 struct tevent_req);
249 struct samba_runcmd_state *state = tevent_req_data(req,
250 struct samba_runcmd_state);
251 int level;
252 char *p;
253 int n, fd;
255 if (!(flags & TEVENT_FD_READ)) {
256 return;
259 if (fde == state->fde_stdout) {
260 level = state->stdout_log_level;
261 fd = state->fd_stdout;
262 } else if (fde == state->fde_stderr) {
263 level = state->stderr_log_level;
264 fd = state->fd_stderr;
265 } else {
266 int status;
268 status = tfork_status(&state->tfork, false);
269 if (status == -1) {
270 if (errno == EAGAIN || errno == EWOULDBLOCK) {
271 return;
273 DBG_ERR("Bad read on status pipe\n");
274 tevent_req_error(req, errno);
275 return;
277 state->pid = -1;
278 TALLOC_FREE(fde);
280 if (WIFEXITED(status)) {
281 status = WEXITSTATUS(status);
282 } else if (WIFSIGNALED(status)) {
283 status = WTERMSIG(status);
284 } else {
285 status = ECHILD;
288 DBG_NOTICE("Child %s exited %d\n", state->arg0, status);
289 if (status != 0) {
290 tevent_req_error(req, status);
291 return;
294 tevent_req_done(req);
295 return;
298 n = read(fd, &state->buf[state->buf_used],
299 sizeof(state->buf) - state->buf_used);
300 if (n > 0) {
301 state->buf_used += n;
302 } else if (n == 0) {
303 if (fde == state->fde_stdout) {
304 talloc_free(fde);
305 state->fde_stdout = NULL;
306 return;
308 if (fde == state->fde_stderr) {
309 talloc_free(fde);
310 state->fde_stderr = NULL;
311 return;
313 return;
316 while (state->buf_used > 0 &&
317 (p = (char *)memchr(state->buf, '\n', state->buf_used)) != NULL) {
318 int n1 = (p - state->buf)+1;
319 int n2 = n1 - 1;
320 /* swallow \r from child processes */
321 if (n2 > 0 && state->buf[n2-1] == '\r') {
322 n2--;
324 DEBUG(level,("%s: %*.*s\n", state->arg0, n2, n2, state->buf));
325 memmove(state->buf, p+1, sizeof(state->buf) - n1);
326 state->buf_used -= n1;
329 /* the buffer could have completely filled - unfortunately we have
330 no choice but to dump it out straight away */
331 if (state->buf_used == sizeof(state->buf)) {
332 DEBUG(level,("%s: %*.*s\n",
333 state->arg0, state->buf_used,
334 state->buf_used, state->buf));
335 state->buf_used = 0;
339 int samba_runcmd_recv(struct tevent_req *req, int *perrno)
341 if (tevent_req_is_unix_error(req, perrno)) {
342 tevent_req_received(req);
343 return -1;
346 tevent_req_received(req);
347 return 0;