winbindd: as DC we should try to get the target_domain from @SOMETHING part of the...
[Samba.git] / lib / util / util_runcmd.c
blobac74371e76de808e3d10ace3a978152bba71cec7
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->pid > 0) {
38 kill(state->pid, SIGKILL);
39 waitpid(state->pid, NULL, 0);
40 state->pid = -1;
43 if (state->fd_stdin != -1) {
44 close(state->fd_stdin);
46 return 0;
49 static void samba_runcmd_io_handler(struct tevent_context *ev,
50 struct tevent_fd *fde,
51 uint16_t flags,
52 void *private_data);
55 run a command as a child process, with a timeout.
57 any stdout/stderr from the child will appear in the Samba logs with
58 the specified log levels
60 struct tevent_req *samba_runcmd_send(TALLOC_CTX *mem_ctx,
61 struct tevent_context *ev,
62 struct timeval endtime,
63 int stdout_log_level,
64 int stderr_log_level,
65 const char * const *argv0, ...)
67 struct tevent_req *req;
68 struct samba_runcmd_state *state;
69 int p1[2], p2[2], p3[2];
70 char **argv;
71 va_list ap;
73 if (argv0 == NULL) {
74 return NULL;
77 req = tevent_req_create(mem_ctx, &state,
78 struct samba_runcmd_state);
79 if (req == NULL) {
80 return NULL;
83 state->stdout_log_level = stdout_log_level;
84 state->stderr_log_level = stderr_log_level;
85 state->fd_stdin = -1;
87 state->arg0 = talloc_strdup(state, argv0[0]);
88 if (tevent_req_nomem(state->arg0, req)) {
89 return tevent_req_post(req, ev);
92 if (pipe(p1) != 0) {
93 tevent_req_error(req, errno);
94 return tevent_req_post(req, ev);
96 if (pipe(p2) != 0) {
97 close(p1[0]);
98 close(p1[1]);
99 tevent_req_error(req, errno);
100 return tevent_req_post(req, ev);
102 if (pipe(p3) != 0) {
103 close(p1[0]);
104 close(p1[1]);
105 close(p2[0]);
106 close(p2[1]);
107 tevent_req_error(req, errno);
108 return tevent_req_post(req, ev);
111 state->tfork = tfork_create();
112 if (state->tfork == NULL) {
113 printf("state->tfork == NULL\n");
114 close(p1[0]);
115 close(p1[1]);
116 close(p2[0]);
117 close(p2[1]);
118 close(p3[0]);
119 close(p3[1]);
120 tevent_req_error(req, errno);
121 return tevent_req_post(req, ev);
123 state->pid = tfork_child_pid(state->tfork);
124 if (state->pid != 0) {
125 /* the parent */
126 close(p1[1]);
127 close(p2[1]);
128 close(p3[0]);
129 state->fd_stdout = p1[0];
130 state->fd_stderr = p2[0];
131 state->fd_stdin = p3[1];
132 state->fd_status = tfork_event_fd(state->tfork);
134 set_blocking(state->fd_stdout, false);
135 set_blocking(state->fd_stderr, false);
136 set_blocking(state->fd_stdin, false);
137 set_blocking(state->fd_status, false);
139 smb_set_close_on_exec(state->fd_stdin);
140 smb_set_close_on_exec(state->fd_stdout);
141 smb_set_close_on_exec(state->fd_stderr);
142 smb_set_close_on_exec(state->fd_status);
144 talloc_set_destructor(state, samba_runcmd_state_destructor);
146 state->fde_stdout = tevent_add_fd(ev, state,
147 state->fd_stdout,
148 TEVENT_FD_READ,
149 samba_runcmd_io_handler,
150 req);
151 if (tevent_req_nomem(state->fde_stdout, req)) {
152 close(state->fd_stdout);
153 close(state->fd_stderr);
154 close(state->fd_status);
155 return tevent_req_post(req, ev);
157 tevent_fd_set_auto_close(state->fde_stdout);
159 state->fde_stderr = tevent_add_fd(ev, state,
160 state->fd_stderr,
161 TEVENT_FD_READ,
162 samba_runcmd_io_handler,
163 req);
164 if (tevent_req_nomem(state->fde_stdout, req)) {
165 close(state->fd_stdout);
166 close(state->fd_stderr);
167 close(state->fd_status);
168 return tevent_req_post(req, ev);
170 tevent_fd_set_auto_close(state->fde_stderr);
172 state->fde_status = tevent_add_fd(ev, state,
173 state->fd_status,
174 TEVENT_FD_READ,
175 samba_runcmd_io_handler,
176 req);
177 if (tevent_req_nomem(state->fde_stdout, req)) {
178 close(state->fd_stdout);
179 close(state->fd_stderr);
180 close(state->fd_status);
181 return tevent_req_post(req, ev);
183 tevent_fd_set_auto_close(state->fde_status);
185 if (!timeval_is_zero(&endtime)) {
186 tevent_req_set_endtime(req, ev, endtime);
189 return req;
192 /* the child */
193 close(p1[0]);
194 close(p2[0]);
195 close(p3[1]);
196 close(0);
197 close(1);
198 close(2);
200 /* we want to ensure that all of the network sockets we had
201 open are closed */
202 tevent_re_initialise(ev);
204 /* setup for logging to go to the parents debug log */
205 dup2(p3[0], 0);
206 dup2(p1[1], 1);
207 dup2(p2[1], 2);
209 close(p1[1]);
210 close(p2[1]);
211 close(p3[0]);
213 argv = str_list_copy(state, discard_const_p(const char *, argv0));
214 if (!argv) {
215 fprintf(stderr, "Out of memory in child\n");
216 _exit(255);
219 va_start(ap, argv0);
220 while (1) {
221 const char **l;
222 char *arg = va_arg(ap, char *);
223 if (arg == NULL) break;
224 l = discard_const_p(const char *, argv);
225 l = str_list_add(l, arg);
226 if (l == NULL) {
227 fprintf(stderr, "Out of memory in child\n");
228 _exit(255);
230 argv = discard_const_p(char *, l);
232 va_end(ap);
234 (void)execvp(state->arg0, argv);
235 fprintf(stderr, "Failed to exec child - %s\n", strerror(errno));
236 _exit(255);
237 return NULL;
241 handle stdout/stderr from the child
243 static void samba_runcmd_io_handler(struct tevent_context *ev,
244 struct tevent_fd *fde,
245 uint16_t flags,
246 void *private_data)
248 struct tevent_req *req = talloc_get_type_abort(private_data,
249 struct tevent_req);
250 struct samba_runcmd_state *state = tevent_req_data(req,
251 struct samba_runcmd_state);
252 int level;
253 char *p;
254 int n, fd;
256 if (!(flags & TEVENT_FD_READ)) {
257 return;
260 if (fde == state->fde_stdout) {
261 level = state->stdout_log_level;
262 fd = state->fd_stdout;
263 } else if (fde == state->fde_stderr) {
264 level = state->stderr_log_level;
265 fd = state->fd_stderr;
266 } else {
267 int status;
269 status = tfork_status(&state->tfork, false);
270 if (status == -1) {
271 if (errno == EAGAIN || errno == EWOULDBLOCK) {
272 return;
274 DBG_ERR("Bad read on status pipe\n");
275 tevent_req_error(req, errno);
276 return;
279 if (WIFEXITED(status)) {
280 status = WEXITSTATUS(status);
281 } else if (WIFSIGNALED(status)) {
282 status = WTERMSIG(status);
283 } else {
284 status = ECHILD;
287 DBG_NOTICE("Child %s exited %d\n", state->arg0, status);
288 if (status != 0) {
289 tevent_req_error(req, status);
290 return;
293 tevent_req_done(req);
294 return;
297 n = read(fd, &state->buf[state->buf_used],
298 sizeof(state->buf) - state->buf_used);
299 if (n > 0) {
300 state->buf_used += n;
301 } else if (n == 0) {
302 if (fde == state->fde_stdout) {
303 talloc_free(fde);
304 state->fde_stdout = NULL;
305 return;
307 if (fde == state->fde_stderr) {
308 talloc_free(fde);
309 state->fde_stderr = NULL;
310 return;
312 return;
315 while (state->buf_used > 0 &&
316 (p = (char *)memchr(state->buf, '\n', state->buf_used)) != NULL) {
317 int n1 = (p - state->buf)+1;
318 int n2 = n1 - 1;
319 /* swallow \r from child processes */
320 if (n2 > 0 && state->buf[n2-1] == '\r') {
321 n2--;
323 DEBUG(level,("%s: %*.*s\n", state->arg0, n2, n2, state->buf));
324 memmove(state->buf, p+1, sizeof(state->buf) - n1);
325 state->buf_used -= n1;
328 /* the buffer could have completely filled - unfortunately we have
329 no choice but to dump it out straight away */
330 if (state->buf_used == sizeof(state->buf)) {
331 DEBUG(level,("%s: %*.*s\n",
332 state->arg0, state->buf_used,
333 state->buf_used, state->buf));
334 state->buf_used = 0;
338 int samba_runcmd_recv(struct tevent_req *req, int *perrno)
340 if (tevent_req_is_unix_error(req, perrno)) {
341 tevent_req_received(req);
342 return -1;
345 tevent_req_received(req);
346 return 0;