hresult: add new HRESULT_FROM_WERROR macro
[Samba.git] / lib / util / util_runcmd.c
blob9264cbba11b60c586f9a06be7f946aa4f55c0a9a
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"
33 static int samba_runcmd_state_destructor(struct samba_runcmd_state *state)
35 if (state->pid > 0) {
36 kill(state->pid, SIGKILL);
37 waitpid(state->pid, NULL, 0);
38 state->pid = -1;
41 if (state->fd_stdin != -1) {
42 close(state->fd_stdin);
44 return 0;
47 static void samba_runcmd_io_handler(struct tevent_context *ev,
48 struct tevent_fd *fde,
49 uint16_t flags,
50 void *private_data);
53 run a command as a child process, with a timeout.
55 any stdout/stderr from the child will appear in the Samba logs with
56 the specified log levels
58 struct tevent_req *samba_runcmd_send(TALLOC_CTX *mem_ctx,
59 struct tevent_context *ev,
60 struct timeval endtime,
61 int stdout_log_level,
62 int stderr_log_level,
63 const char * const *argv0, ...)
65 struct tevent_req *req;
66 struct samba_runcmd_state *state;
67 int p1[2], p2[2], p3[2];
68 char **argv;
69 va_list ap;
71 if (argv0 == NULL) {
72 return NULL;
75 req = tevent_req_create(mem_ctx, &state,
76 struct samba_runcmd_state);
77 if (req == NULL) {
78 return NULL;
81 state->stdout_log_level = stdout_log_level;
82 state->stderr_log_level = stderr_log_level;
83 state->fd_stdin = -1;
85 state->arg0 = talloc_strdup(state, argv0[0]);
86 if (tevent_req_nomem(state->arg0, req)) {
87 return tevent_req_post(req, ev);
90 if (pipe(p1) != 0) {
91 tevent_req_error(req, errno);
92 return tevent_req_post(req, ev);
94 if (pipe(p2) != 0) {
95 close(p1[0]);
96 close(p1[1]);
97 tevent_req_error(req, errno);
98 return tevent_req_post(req, ev);
100 if (pipe(p3) != 0) {
101 close(p1[0]);
102 close(p1[1]);
103 close(p2[0]);
104 close(p2[1]);
105 tevent_req_error(req, errno);
106 return tevent_req_post(req, ev);
109 state->pid = fork();
110 if (state->pid == (pid_t)-1) {
111 close(p1[0]);
112 close(p1[1]);
113 close(p2[0]);
114 close(p2[1]);
115 close(p3[0]);
116 close(p3[1]);
117 tevent_req_error(req, errno);
118 return tevent_req_post(req, ev);
121 if (state->pid != 0) {
122 /* the parent */
123 close(p1[1]);
124 close(p2[1]);
125 close(p3[0]);
126 state->fd_stdout = p1[0];
127 state->fd_stderr = p2[0];
128 state->fd_stdin = p3[1];
130 set_blocking(state->fd_stdout, false);
131 set_blocking(state->fd_stderr, false);
132 set_blocking(state->fd_stdin, false);
134 smb_set_close_on_exec(state->fd_stdin);
135 smb_set_close_on_exec(state->fd_stdout);
136 smb_set_close_on_exec(state->fd_stderr);
138 talloc_set_destructor(state, samba_runcmd_state_destructor);
140 state->fde_stdout = tevent_add_fd(ev, state,
141 state->fd_stdout,
142 TEVENT_FD_READ,
143 samba_runcmd_io_handler,
144 req);
145 if (tevent_req_nomem(state->fde_stdout, req)) {
146 close(state->fd_stdout);
147 close(state->fd_stderr);
148 return tevent_req_post(req, ev);
150 tevent_fd_set_auto_close(state->fde_stdout);
152 state->fde_stderr = tevent_add_fd(ev, state,
153 state->fd_stderr,
154 TEVENT_FD_READ,
155 samba_runcmd_io_handler,
156 req);
157 if (tevent_req_nomem(state->fde_stdout, req)) {
158 close(state->fd_stderr);
159 return tevent_req_post(req, ev);
161 tevent_fd_set_auto_close(state->fde_stderr);
163 if (!timeval_is_zero(&endtime)) {
164 tevent_req_set_endtime(req, ev, endtime);
167 return req;
170 /* the child */
171 close(p1[0]);
172 close(p2[0]);
173 close(p3[1]);
174 close(0);
175 close(1);
176 close(2);
178 /* we want to ensure that all of the network sockets we had
179 open are closed */
180 tevent_re_initialise(ev);
182 /* setup for logging to go to the parents debug log */
183 dup2(p3[0], 0);
184 dup2(p1[1], 1);
185 dup2(p2[1], 2);
187 close(p1[1]);
188 close(p2[1]);
189 close(p3[0]);
191 argv = str_list_copy(state, discard_const_p(const char *, argv0));
192 if (!argv) {
193 fprintf(stderr, "Out of memory in child\n");
194 _exit(255);
197 va_start(ap, argv0);
198 while (1) {
199 const char **l;
200 char *arg = va_arg(ap, char *);
201 if (arg == NULL) break;
202 l = discard_const_p(const char *, argv);
203 l = str_list_add(l, arg);
204 if (l == NULL) {
205 fprintf(stderr, "Out of memory in child\n");
206 _exit(255);
208 argv = discard_const_p(char *, l);
210 va_end(ap);
212 (void)execvp(state->arg0, argv);
213 fprintf(stderr, "Failed to exec child - %s\n", strerror(errno));
214 _exit(255);
215 return NULL;
219 handle stdout/stderr from the child
221 static void samba_runcmd_io_handler(struct tevent_context *ev,
222 struct tevent_fd *fde,
223 uint16_t flags,
224 void *private_data)
226 struct tevent_req *req = talloc_get_type_abort(private_data,
227 struct tevent_req);
228 struct samba_runcmd_state *state = tevent_req_data(req,
229 struct samba_runcmd_state);
230 int level;
231 char *p;
232 int n, fd;
234 if (fde == state->fde_stdout) {
235 level = state->stdout_log_level;
236 fd = state->fd_stdout;
237 } else if (fde == state->fde_stderr) {
238 level = state->stderr_log_level;
239 fd = state->fd_stderr;
240 } else {
241 return;
244 if (!(flags & TEVENT_FD_READ)) {
245 return;
248 n = read(fd, &state->buf[state->buf_used],
249 sizeof(state->buf) - state->buf_used);
250 if (n > 0) {
251 state->buf_used += n;
252 } else if (n == 0) {
253 if (fde == state->fde_stdout) {
254 talloc_free(fde);
255 state->fde_stdout = NULL;
257 if (fde == state->fde_stderr) {
258 talloc_free(fde);
259 state->fde_stderr = NULL;
261 if (state->fde_stdout == NULL &&
262 state->fde_stderr == NULL) {
263 int status;
264 /* the child has closed both stdout and
265 * stderr, assume its dead */
266 pid_t pid = waitpid(state->pid, &status, 0);
267 if (pid != state->pid) {
268 if (errno == ECHILD) {
269 /* this happens when the
270 parent has set SIGCHLD to
271 SIG_IGN. In that case we
272 can only get error
273 information for the child
274 via its logging. We should
275 stop using SIG_IGN on
276 SIGCHLD in the standard
277 process model.
279 DEBUG(0, ("Error in waitpid() unexpectedly got ECHILD "
280 "for %s child %d - %s, "
281 "someone has set SIGCHLD to SIG_IGN!\n",
282 state->arg0, (int)state->pid, strerror(errno)));
283 tevent_req_error(req, errno);
284 return;
286 DEBUG(0,("Error in waitpid() for child %s - %s \n",
287 state->arg0, strerror(errno)));
288 if (errno == 0) {
289 errno = ECHILD;
291 tevent_req_error(req, errno);
292 return;
294 status = WEXITSTATUS(status);
295 DEBUG(3,("Child %s exited with status %d\n",
296 state->arg0, status));
297 if (status != 0) {
298 tevent_req_error(req, status);
299 return;
302 tevent_req_done(req);
303 return;
305 return;
308 while (state->buf_used > 0 &&
309 (p = (char *)memchr(state->buf, '\n', state->buf_used)) != NULL) {
310 int n1 = (p - state->buf)+1;
311 int n2 = n1 - 1;
312 /* swallow \r from child processes */
313 if (n2 > 0 && state->buf[n2-1] == '\r') {
314 n2--;
316 DEBUG(level,("%s: %*.*s\n", state->arg0, n2, n2, state->buf));
317 memmove(state->buf, p+1, sizeof(state->buf) - n1);
318 state->buf_used -= n1;
321 /* the buffer could have completely filled - unfortunately we have
322 no choice but to dump it out straight away */
323 if (state->buf_used == sizeof(state->buf)) {
324 DEBUG(level,("%s: %*.*s\n",
325 state->arg0, state->buf_used,
326 state->buf_used, state->buf));
327 state->buf_used = 0;
331 int samba_runcmd_recv(struct tevent_req *req, int *perrno)
333 if (tevent_req_is_unix_error(req, perrno)) {
334 tevent_req_received(req);
335 return -1;
338 tevent_req_received(req);
339 return 0;