2 Unix SMB/CIFS mplementation.
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"
31 #include "../lib/util/tevent_unix.h"
33 struct samba_runcmd_state
{
36 struct tevent_fd
*fde_stdout
;
37 struct tevent_fd
*fde_stderr
;
38 int fd_stdout
, fd_stderr
;
45 static int samba_runcmd_state_destructor(struct samba_runcmd_state
*state
)
48 kill(state
->pid
, SIGKILL
);
49 waitpid(state
->pid
, NULL
, 0);
55 static void samba_runcmd_io_handler(struct tevent_context
*ev
,
56 struct tevent_fd
*fde
,
61 run a command as a child process, with a timeout.
63 any stdout/stderr from the child will appear in the Samba logs with
64 the specified log levels
66 struct tevent_req
*samba_runcmd_send(TALLOC_CTX
*mem_ctx
,
67 struct tevent_context
*ev
,
68 struct timeval endtime
,
71 const char * const *argv0
, ...)
73 struct tevent_req
*req
;
74 struct samba_runcmd_state
*state
;
80 req
= tevent_req_create(mem_ctx
, &state
,
81 struct samba_runcmd_state
);
86 state
->stdout_log_level
= stdout_log_level
;
87 state
->stderr_log_level
= stderr_log_level
;
89 state
->arg0
= talloc_strdup(state
, argv0
[0]);
90 if (tevent_req_nomem(state
->arg0
, req
)) {
91 return tevent_req_post(req
, ev
);
95 tevent_req_error(req
, errno
);
96 return tevent_req_post(req
, ev
);
101 tevent_req_error(req
, errno
);
102 return tevent_req_post(req
, ev
);
106 if (state
->pid
== (pid_t
)-1) {
111 tevent_req_error(req
, errno
);
112 return tevent_req_post(req
, ev
);
115 if (state
->pid
!= 0) {
119 state
->fd_stdout
= p1
[0];
120 state
->fd_stderr
= p2
[0];
121 set_blocking(state
->fd_stdout
, false);
122 set_blocking(state
->fd_stderr
, false);
124 talloc_set_destructor(state
, samba_runcmd_state_destructor
);
126 state
->fde_stdout
= tevent_add_fd(ev
, state
,
129 samba_runcmd_io_handler
,
131 if (tevent_req_nomem(state
->fde_stdout
, req
)) {
134 return tevent_req_post(req
, ev
);
136 tevent_fd_set_auto_close(state
->fde_stdout
);
138 state
->fde_stderr
= tevent_add_fd(ev
, state
,
141 samba_runcmd_io_handler
,
143 if (tevent_req_nomem(state
->fde_stdout
, req
)) {
145 return tevent_req_post(req
, ev
);
147 tevent_fd_set_auto_close(state
->fde_stderr
);
149 if (!timeval_is_zero(&endtime
)) {
150 tevent_req_set_endtime(req
, ev
, endtime
);
163 /* we want to ensure that all of the network sockets we had
165 tevent_re_initialise(ev
);
167 /* setup for logging to go to the parents debug log */
168 open("/dev/null", O_RDONLY
); /* for stdin */
172 argv
= str_list_copy(state
, discard_const_p(const char *, argv0
));
174 fprintf(stderr
, "Out of memory in child\n");
180 char *arg
= va_arg(ap
, char *);
181 if (arg
== NULL
) break;
182 argv
= discard_const_p(char *, str_list_add((const char **)argv
, arg
));
184 fprintf(stderr
, "Out of memory in child\n");
190 ret
= execvp(state
->arg0
, argv
);
191 fprintf(stderr
, "Failed to exec child - %s\n", strerror(errno
));
197 handle stdout/stderr from the child
199 static void samba_runcmd_io_handler(struct tevent_context
*ev
,
200 struct tevent_fd
*fde
,
204 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
206 struct samba_runcmd_state
*state
= tevent_req_data(req
,
207 struct samba_runcmd_state
);
212 if (fde
== state
->fde_stdout
) {
213 level
= state
->stdout_log_level
;
214 fd
= state
->fd_stdout
;
216 level
= state
->stderr_log_level
;
217 fd
= state
->fd_stderr
;
220 if (!(flags
& TEVENT_FD_READ
)) {
224 n
= read(fd
, &state
->buf
[state
->buf_used
],
225 sizeof(state
->buf
) - state
->buf_used
);
227 state
->buf_used
+= n
;
229 if (fde
== state
->fde_stdout
) {
231 state
->fde_stdout
= NULL
;
233 if (fde
== state
->fde_stderr
) {
235 state
->fde_stderr
= NULL
;
237 if (state
->fde_stdout
== NULL
&&
238 state
->fde_stderr
== NULL
) {
240 /* the child has closed both stdout and
241 * stderr, assume its dead */
242 pid_t pid
= waitpid(state
->pid
, &status
, 0);
243 if (pid
!= state
->pid
) {
244 if (errno
== ECHILD
) {
245 /* this happens when the
246 parent has set SIGCHLD to
247 SIG_IGN. In that case we
249 information for the child
250 via its logging. We should
251 stop using SIG_IGN on
252 SIGCHLD in the standard
255 tevent_req_done(req
);
258 DEBUG(0,("Error in waitpid() for child %s - %s \n",
259 state
->arg0
, strerror(errno
)));
263 tevent_req_error(req
, errno
);
266 status
= WEXITSTATUS(status
);
267 DEBUG(3,("Child %s exited with status %d - %s\n",
268 state
->arg0
, status
, strerror(status
)));
270 tevent_req_error(req
, status
);
274 tevent_req_done(req
);
280 while (state
->buf_used
> 0 &&
281 (p
= (char *)memchr(state
->buf
, '\n', state
->buf_used
)) != NULL
) {
282 int n1
= (p
- state
->buf
)+1;
284 /* swallow \r from child processes */
285 if (n2
> 0 && state
->buf
[n2
-1] == '\r') {
288 DEBUG(level
,("%s: %*.*s\n", state
->arg0
, n2
, n2
, state
->buf
));
289 memmove(state
->buf
, p
+1, sizeof(state
->buf
) - n1
);
290 state
->buf_used
-= n1
;
293 /* the buffer could have completely filled - unfortunately we have
294 no choice but to dump it out straight away */
295 if (state
->buf_used
== sizeof(state
->buf
)) {
296 DEBUG(level
,("%s: %*.*s\n",
297 state
->arg0
, state
->buf_used
,
298 state
->buf_used
, state
->buf
));
303 int samba_runcmd_recv(struct tevent_req
*req
, int *perrno
)
305 if (tevent_req_is_unix_error(req
, perrno
)) {
306 tevent_req_received(req
);
310 tevent_req_received(req
);