2 Unix SMB/CIFS implementation.
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
29 #include "system/filesys.h"
30 #include "system/wait.h"
32 #include "lib/util/samba_util.h"
33 #include "lib/util/debug.h"
34 #include "../lib/util/tevent_unix.h"
35 #include "../lib/util/tfork.h"
36 #include "../lib/util/sys_rw.h"
38 struct samba_runcmd_state
{
41 struct tevent_fd
*fde_stdout
;
42 struct tevent_fd
*fde_stderr
;
43 struct tevent_fd
*fde_status
;
44 int fd_stdin
, fd_stdout
, fd_stderr
, fd_status
;
52 static void samba_runcmd_cleanup_fn(struct tevent_req
*req
,
53 enum tevent_req_state req_state
)
55 struct samba_runcmd_state
*state
= tevent_req_data(
56 req
, struct samba_runcmd_state
);
58 if (state
->tfork
!= NULL
) {
59 tfork_destroy(&state
->tfork
);
63 if (state
->fd_stdin
!= -1) {
64 close(state
->fd_stdin
);
69 int samba_runcmd_export_stdin(struct tevent_req
*req
)
71 struct samba_runcmd_state
*state
= tevent_req_data(req
,
72 struct samba_runcmd_state
);
73 int ret
= state
->fd_stdin
;
80 static void samba_runcmd_io_handler(struct tevent_context
*ev
,
81 struct tevent_fd
*fde
,
86 run a command as a child process, with a timeout.
88 any stdout/stderr from the child will appear in the Samba logs with
89 the specified log levels
91 struct tevent_req
*samba_runcmd_send(TALLOC_CTX
*mem_ctx
,
92 struct tevent_context
*ev
,
93 struct timeval endtime
,
96 const char * const *argv0
, ...)
98 struct tevent_req
*req
;
99 struct samba_runcmd_state
*state
;
100 int p1
[2], p2
[2], p3
[2];
108 req
= tevent_req_create(mem_ctx
, &state
,
109 struct samba_runcmd_state
);
114 state
->stdout_log_level
= stdout_log_level
;
115 state
->stderr_log_level
= stderr_log_level
;
116 state
->fd_stdin
= -1;
118 state
->arg0
= talloc_strdup(state
, argv0
[0]);
119 if (tevent_req_nomem(state
->arg0
, req
)) {
120 return tevent_req_post(req
, ev
);
124 tevent_req_error(req
, errno
);
125 return tevent_req_post(req
, ev
);
130 tevent_req_error(req
, errno
);
131 return tevent_req_post(req
, ev
);
138 tevent_req_error(req
, errno
);
139 return tevent_req_post(req
, ev
);
142 state
->tfork
= tfork_create();
143 if (state
->tfork
== NULL
) {
150 tevent_req_error(req
, errno
);
151 return tevent_req_post(req
, ev
);
153 state
->pid
= tfork_child_pid(state
->tfork
);
154 if (state
->pid
!= 0) {
159 state
->fd_stdout
= p1
[0];
160 state
->fd_stderr
= p2
[0];
161 state
->fd_stdin
= p3
[1];
162 state
->fd_status
= tfork_event_fd(state
->tfork
);
164 set_blocking(state
->fd_stdout
, false);
165 set_blocking(state
->fd_stderr
, false);
166 set_blocking(state
->fd_stdin
, false);
167 set_blocking(state
->fd_status
, false);
169 smb_set_close_on_exec(state
->fd_stdin
);
170 smb_set_close_on_exec(state
->fd_stdout
);
171 smb_set_close_on_exec(state
->fd_stderr
);
172 smb_set_close_on_exec(state
->fd_status
);
174 tevent_req_set_cleanup_fn(req
, samba_runcmd_cleanup_fn
);
176 state
->fde_stdout
= tevent_add_fd(ev
, state
,
179 samba_runcmd_io_handler
,
181 if (tevent_req_nomem(state
->fde_stdout
, req
)) {
182 close(state
->fd_stdout
);
183 close(state
->fd_stderr
);
184 close(state
->fd_status
);
185 return tevent_req_post(req
, ev
);
187 tevent_fd_set_auto_close(state
->fde_stdout
);
189 state
->fde_stderr
= tevent_add_fd(ev
, state
,
192 samba_runcmd_io_handler
,
194 if (tevent_req_nomem(state
->fde_stdout
, req
)) {
195 close(state
->fd_stdout
);
196 close(state
->fd_stderr
);
197 close(state
->fd_status
);
198 return tevent_req_post(req
, ev
);
200 tevent_fd_set_auto_close(state
->fde_stderr
);
202 state
->fde_status
= tevent_add_fd(ev
, state
,
205 samba_runcmd_io_handler
,
207 if (tevent_req_nomem(state
->fde_stdout
, req
)) {
208 close(state
->fd_stdout
);
209 close(state
->fd_stderr
);
210 close(state
->fd_status
);
211 return tevent_req_post(req
, ev
);
213 tevent_fd_set_auto_close(state
->fde_status
);
215 if (!timeval_is_zero(&endtime
)) {
216 tevent_req_set_endtime(req
, ev
, endtime
);
230 /* we want to ensure that all of the network sockets we had
232 tevent_re_initialise(ev
);
234 /* setup for logging to go to the parents debug log */
243 argv
= str_list_copy(state
, discard_const_p(const char *, argv0
));
245 fprintf(stderr
, "Out of memory in child\n");
252 char *arg
= va_arg(ap
, char *);
253 if (arg
== NULL
) break;
254 l
= discard_const_p(const char *, argv
);
255 l
= str_list_add(l
, arg
);
257 fprintf(stderr
, "Out of memory in child\n");
260 argv
= discard_const_p(char *, l
);
264 (void)execvp(state
->arg0
, argv
);
265 fprintf(stderr
, "Failed to exec child - %s\n", strerror(errno
));
271 handle stdout/stderr from the child
273 static void samba_runcmd_io_handler(struct tevent_context
*ev
,
274 struct tevent_fd
*fde
,
278 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
280 struct samba_runcmd_state
*state
= tevent_req_data(req
,
281 struct samba_runcmd_state
);
286 if (!(flags
& TEVENT_FD_READ
)) {
290 if (fde
== state
->fde_stdout
) {
291 level
= state
->stdout_log_level
;
292 fd
= state
->fd_stdout
;
293 } else if (fde
== state
->fde_stderr
) {
294 level
= state
->stderr_log_level
;
295 fd
= state
->fd_stderr
;
299 status
= tfork_status(&state
->tfork
, false);
301 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
304 DBG_ERR("Bad read on status pipe\n");
305 tevent_req_error(req
, errno
);
311 if (WIFEXITED(status
)) {
312 status
= WEXITSTATUS(status
);
313 } else if (WIFSIGNALED(status
)) {
314 status
= WTERMSIG(status
);
319 DBG_NOTICE("Child %s exited %d\n", state
->arg0
, status
);
321 tevent_req_error(req
, status
);
325 tevent_req_done(req
);
329 n
= read(fd
, &state
->buf
[state
->buf_used
],
330 sizeof(state
->buf
) - state
->buf_used
);
332 state
->buf_used
+= n
;
334 if (fde
== state
->fde_stdout
) {
336 state
->fde_stdout
= NULL
;
339 if (fde
== state
->fde_stderr
) {
341 state
->fde_stderr
= NULL
;
347 while (state
->buf_used
> 0 &&
348 (p
= (char *)memchr(state
->buf
, '\n', state
->buf_used
)) != NULL
) {
349 int n1
= (p
- state
->buf
)+1;
351 /* swallow \r from child processes */
352 if (n2
> 0 && state
->buf
[n2
-1] == '\r') {
355 DEBUG(level
,("%s: %*.*s\n", state
->arg0
, n2
, n2
, state
->buf
));
356 memmove(state
->buf
, p
+1, sizeof(state
->buf
) - n1
);
357 state
->buf_used
-= n1
;
360 /* the buffer could have completely filled - unfortunately we have
361 no choice but to dump it out straight away */
362 if (state
->buf_used
== sizeof(state
->buf
)) {
363 DEBUG(level
,("%s: %*.*s\n",
364 state
->arg0
, state
->buf_used
,
365 state
->buf_used
, state
->buf
));
370 int samba_runcmd_recv(struct tevent_req
*req
, int *perrno
)
372 if (tevent_req_is_unix_error(req
, perrno
)) {
373 tevent_req_received(req
);
377 tevent_req_received(req
);