libcli: Avoid a talloc/free
[Samba.git] / lib / util / util_runcmd.c
blobc8547de694ab975d5792be090d2929d8cd4eba00
1 /*
2 Unix SMB/CIFS mplementation.
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 <tevent.h>
31 #include "../lib/util/tevent_unix.h"
33 struct samba_runcmd_state {
34 int stdout_log_level;
35 int stderr_log_level;
36 struct tevent_fd *fde_stdout;
37 struct tevent_fd *fde_stderr;
38 int fd_stdin, fd_stdout, fd_stderr;
39 char *arg0;
40 pid_t pid;
41 char buf[1024];
42 uint16_t buf_used;
45 static int samba_runcmd_state_destructor(struct samba_runcmd_state *state)
47 if (state->pid > 0) {
48 kill(state->pid, SIGKILL);
49 waitpid(state->pid, NULL, 0);
50 state->pid = -1;
53 if (state->fd_stdin != -1) {
54 close(state->fd_stdin);
56 return 0;
59 static void samba_runcmd_io_handler(struct tevent_context *ev,
60 struct tevent_fd *fde,
61 uint16_t flags,
62 void *private_data);
65 run a command as a child process, with a timeout.
67 any stdout/stderr from the child will appear in the Samba logs with
68 the specified log levels
70 struct tevent_req *samba_runcmd_send(TALLOC_CTX *mem_ctx,
71 struct tevent_context *ev,
72 struct timeval endtime,
73 int stdout_log_level,
74 int stderr_log_level,
75 const char * const *argv0, ...)
77 struct tevent_req *req;
78 struct samba_runcmd_state *state;
79 int p1[2], p2[2], p3[2];
80 char **argv;
81 va_list ap;
83 req = tevent_req_create(mem_ctx, &state,
84 struct samba_runcmd_state);
85 if (req == NULL) {
86 return NULL;
89 state->stdout_log_level = stdout_log_level;
90 state->stderr_log_level = stderr_log_level;
91 state->fd_stdin = -1;
93 state->arg0 = talloc_strdup(state, argv0[0]);
94 if (tevent_req_nomem(state->arg0, req)) {
95 return tevent_req_post(req, ev);
98 if (pipe(p1) != 0) {
99 tevent_req_error(req, errno);
100 return tevent_req_post(req, ev);
102 if (pipe(p2) != 0) {
103 close(p1[0]);
104 close(p1[1]);
105 tevent_req_error(req, errno);
106 return tevent_req_post(req, ev);
108 if (pipe(p3) != 0) {
109 close(p1[0]);
110 close(p1[1]);
111 close(p2[0]);
112 close(p2[1]);
113 tevent_req_error(req, errno);
114 return tevent_req_post(req, ev);
117 state->pid = fork();
118 if (state->pid == (pid_t)-1) {
119 close(p1[0]);
120 close(p1[1]);
121 close(p2[0]);
122 close(p2[1]);
123 close(p3[0]);
124 close(p3[1]);
125 tevent_req_error(req, errno);
126 return tevent_req_post(req, ev);
129 if (state->pid != 0) {
130 /* the parent */
131 close(p1[1]);
132 close(p2[1]);
133 close(p3[0]);
134 state->fd_stdout = p1[0];
135 state->fd_stderr = p2[0];
136 state->fd_stdin = p3[1];
138 set_blocking(state->fd_stdout, false);
139 set_blocking(state->fd_stderr, false);
140 set_blocking(state->fd_stdin, false);
142 smb_set_close_on_exec(state->fd_stdin);
143 smb_set_close_on_exec(state->fd_stdout);
144 smb_set_close_on_exec(state->fd_stderr);
146 talloc_set_destructor(state, samba_runcmd_state_destructor);
148 state->fde_stdout = tevent_add_fd(ev, state,
149 state->fd_stdout,
150 TEVENT_FD_READ,
151 samba_runcmd_io_handler,
152 req);
153 if (tevent_req_nomem(state->fde_stdout, req)) {
154 close(state->fd_stdout);
155 close(state->fd_stderr);
156 return tevent_req_post(req, ev);
158 tevent_fd_set_auto_close(state->fde_stdout);
160 state->fde_stderr = tevent_add_fd(ev, state,
161 state->fd_stderr,
162 TEVENT_FD_READ,
163 samba_runcmd_io_handler,
164 req);
165 if (tevent_req_nomem(state->fde_stdout, req)) {
166 close(state->fd_stderr);
167 return tevent_req_post(req, ev);
169 tevent_fd_set_auto_close(state->fde_stderr);
171 if (!timeval_is_zero(&endtime)) {
172 tevent_req_set_endtime(req, ev, endtime);
175 return req;
178 /* the child */
179 close(p1[0]);
180 close(p2[0]);
181 close(p3[1]);
182 close(0);
183 close(1);
184 close(2);
186 /* we want to ensure that all of the network sockets we had
187 open are closed */
188 tevent_re_initialise(ev);
190 /* setup for logging to go to the parents debug log */
191 dup2(p3[0], 0);
192 dup2(p1[1], 1);
193 dup2(p2[1], 2);
195 close(p1[1]);
196 close(p2[1]);
197 close(p3[0]);
199 argv = str_list_copy(state, discard_const_p(const char *, argv0));
200 if (!argv) {
201 fprintf(stderr, "Out of memory in child\n");
202 _exit(255);
205 va_start(ap, argv0);
206 while (1) {
207 const char **l;
208 char *arg = va_arg(ap, char *);
209 if (arg == NULL) break;
210 l = discard_const_p(const char *, argv);
211 l = str_list_add(l, arg);
212 if (l == NULL) {
213 fprintf(stderr, "Out of memory in child\n");
214 _exit(255);
216 argv = discard_const_p(char *, l);
218 va_end(ap);
220 (void)execvp(state->arg0, argv);
221 fprintf(stderr, "Failed to exec child - %s\n", strerror(errno));
222 _exit(255);
223 return NULL;
227 handle stdout/stderr from the child
229 static void samba_runcmd_io_handler(struct tevent_context *ev,
230 struct tevent_fd *fde,
231 uint16_t flags,
232 void *private_data)
234 struct tevent_req *req = talloc_get_type_abort(private_data,
235 struct tevent_req);
236 struct samba_runcmd_state *state = tevent_req_data(req,
237 struct samba_runcmd_state);
238 int level;
239 char *p;
240 int n, fd;
242 if (fde == state->fde_stdout) {
243 level = state->stdout_log_level;
244 fd = state->fd_stdout;
245 } else if (fde == state->fde_stderr) {
246 level = state->stderr_log_level;
247 fd = state->fd_stderr;
248 } else {
249 return;
252 if (!(flags & TEVENT_FD_READ)) {
253 return;
256 n = read(fd, &state->buf[state->buf_used],
257 sizeof(state->buf) - state->buf_used);
258 if (n > 0) {
259 state->buf_used += n;
260 } else if (n == 0) {
261 if (fde == state->fde_stdout) {
262 talloc_free(fde);
263 state->fde_stdout = NULL;
265 if (fde == state->fde_stderr) {
266 talloc_free(fde);
267 state->fde_stderr = NULL;
269 if (state->fde_stdout == NULL &&
270 state->fde_stderr == NULL) {
271 int status;
272 /* the child has closed both stdout and
273 * stderr, assume its dead */
274 pid_t pid = waitpid(state->pid, &status, 0);
275 if (pid != state->pid) {
276 if (errno == ECHILD) {
277 /* this happens when the
278 parent has set SIGCHLD to
279 SIG_IGN. In that case we
280 can only get error
281 information for the child
282 via its logging. We should
283 stop using SIG_IGN on
284 SIGCHLD in the standard
285 process model.
287 tevent_req_done(req);
288 return;
290 DEBUG(0,("Error in waitpid() for child %s - %s \n",
291 state->arg0, strerror(errno)));
292 if (errno == 0) {
293 errno = ECHILD;
295 tevent_req_error(req, errno);
296 return;
298 status = WEXITSTATUS(status);
299 DEBUG(3,("Child %s exited with status %d - %s\n",
300 state->arg0, status, strerror(status)));
301 if (status != 0) {
302 tevent_req_error(req, status);
303 return;
306 tevent_req_done(req);
307 return;
309 return;
312 while (state->buf_used > 0 &&
313 (p = (char *)memchr(state->buf, '\n', state->buf_used)) != NULL) {
314 int n1 = (p - state->buf)+1;
315 int n2 = n1 - 1;
316 /* swallow \r from child processes */
317 if (n2 > 0 && state->buf[n2-1] == '\r') {
318 n2--;
320 DEBUG(level,("%s: %*.*s\n", state->arg0, n2, n2, state->buf));
321 memmove(state->buf, p+1, sizeof(state->buf) - n1);
322 state->buf_used -= n1;
325 /* the buffer could have completely filled - unfortunately we have
326 no choice but to dump it out straight away */
327 if (state->buf_used == sizeof(state->buf)) {
328 DEBUG(level,("%s: %*.*s\n",
329 state->arg0, state->buf_used,
330 state->buf_used, state->buf));
331 state->buf_used = 0;
335 int samba_runcmd_recv(struct tevent_req *req, int *perrno)
337 if (tevent_req_is_unix_error(req, perrno)) {
338 tevent_req_received(req);
339 return -1;
342 tevent_req_received(req);
343 return 0;