46c455d3d0349084ca332d94ba9de6e54b1f79e4
[nivanova/samba-autobuild/.git] / lib / util / util_runcmd.c
1 /*
2    Unix SMB/CIFS mplementation.
3
4    run a child command
5
6    Copyright (C) Andrew Tridgell 2010
7
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.
12
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.
17
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/>.
20
21 */
22
23 /*
24   this runs a child command with stdout and stderr going to the Samba
25   log
26  */
27
28 #include "includes.h"
29 #include "system/filesys.h"
30 #include <tevent.h>
31 #include "../lib/util/tevent_unix.h"
32
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;
43 };
44
45 static int samba_runcmd_state_destructor(struct samba_runcmd_state *state)
46 {
47         if (state->pid > 0) {
48                 kill(state->pid, SIGKILL);
49                 waitpid(state->pid, NULL, 0);
50                 state->pid = -1;
51         }
52
53         if (state->fd_stdin != -1) {
54                 close(state->fd_stdin);
55         }
56         return 0;
57 }
58
59 static void samba_runcmd_io_handler(struct tevent_context *ev,
60                                     struct tevent_fd *fde,
61                                     uint16_t flags,
62                                     void *private_data);
63
64 /*
65   run a command as a child process, with a timeout.
66
67   any stdout/stderr from the child will appear in the Samba logs with
68   the specified log levels
69  */
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, ...)
76 {
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;
82
83         if (argv0 == NULL) {
84                 return NULL;
85         }
86
87         req = tevent_req_create(mem_ctx, &state,
88                                 struct samba_runcmd_state);
89         if (req == NULL) {
90                 return NULL;
91         }
92
93         state->stdout_log_level = stdout_log_level;
94         state->stderr_log_level = stderr_log_level;
95         state->fd_stdin = -1;
96
97         state->arg0 = talloc_strdup(state, argv0[0]);
98         if (tevent_req_nomem(state->arg0, req)) {
99                 return tevent_req_post(req, ev);
100         }
101
102         if (pipe(p1) != 0) {
103                 tevent_req_error(req, errno);
104                 return tevent_req_post(req, ev);
105         }
106         if (pipe(p2) != 0) {
107                 close(p1[0]);
108                 close(p1[1]);
109                 tevent_req_error(req, errno);
110                 return tevent_req_post(req, ev);
111         }
112         if (pipe(p3) != 0) {
113                 close(p1[0]);
114                 close(p1[1]);
115                 close(p2[0]);
116                 close(p2[1]);
117                 tevent_req_error(req, errno);
118                 return tevent_req_post(req, ev);
119         }
120
121         state->pid = fork();
122         if (state->pid == (pid_t)-1) {
123                 close(p1[0]);
124                 close(p1[1]);
125                 close(p2[0]);
126                 close(p2[1]);
127                 close(p3[0]);
128                 close(p3[1]);
129                 tevent_req_error(req, errno);
130                 return tevent_req_post(req, ev);
131         }
132
133         if (state->pid != 0) {
134                 /* the parent */
135                 close(p1[1]);
136                 close(p2[1]);
137                 close(p3[0]);
138                 state->fd_stdout = p1[0];
139                 state->fd_stderr = p2[0];
140                 state->fd_stdin  = p3[1];
141
142                 set_blocking(state->fd_stdout, false);
143                 set_blocking(state->fd_stderr, false);
144                 set_blocking(state->fd_stdin,  false);
145
146                 smb_set_close_on_exec(state->fd_stdin);
147                 smb_set_close_on_exec(state->fd_stdout);
148                 smb_set_close_on_exec(state->fd_stderr);
149
150                 talloc_set_destructor(state, samba_runcmd_state_destructor);
151
152                 state->fde_stdout = tevent_add_fd(ev, state,
153                                                   state->fd_stdout,
154                                                   TEVENT_FD_READ,
155                                                   samba_runcmd_io_handler,
156                                                   req);
157                 if (tevent_req_nomem(state->fde_stdout, req)) {
158                         close(state->fd_stdout);
159                         close(state->fd_stderr);
160                         return tevent_req_post(req, ev);
161                 }
162                 tevent_fd_set_auto_close(state->fde_stdout);
163
164                 state->fde_stderr = tevent_add_fd(ev, state,
165                                                   state->fd_stderr,
166                                                   TEVENT_FD_READ,
167                                                   samba_runcmd_io_handler,
168                                                   req);
169                 if (tevent_req_nomem(state->fde_stdout, req)) {
170                         close(state->fd_stderr);
171                         return tevent_req_post(req, ev);
172                 }
173                 tevent_fd_set_auto_close(state->fde_stderr);
174
175                 if (!timeval_is_zero(&endtime)) {
176                         tevent_req_set_endtime(req, ev, endtime);
177                 }
178
179                 return req;
180         }
181
182         /* the child */
183         close(p1[0]);
184         close(p2[0]);
185         close(p3[1]);
186         close(0);
187         close(1);
188         close(2);
189
190         /* we want to ensure that all of the network sockets we had
191            open are closed */
192         tevent_re_initialise(ev);
193
194         /* setup for logging to go to the parents debug log */
195         dup2(p3[0], 0);
196         dup2(p1[1], 1);
197         dup2(p2[1], 2);
198
199         close(p1[1]);
200         close(p2[1]);
201         close(p3[0]);
202
203         argv = str_list_copy(state, discard_const_p(const char *, argv0));
204         if (!argv) {
205                 fprintf(stderr, "Out of memory in child\n");
206                 _exit(255);
207         }
208
209         va_start(ap, argv0);
210         while (1) {
211                 const char **l;
212                 char *arg = va_arg(ap, char *);
213                 if (arg == NULL) break;
214                 l = discard_const_p(const char *, argv);
215                 l = str_list_add(l, arg);
216                 if (l == NULL) {
217                         fprintf(stderr, "Out of memory in child\n");
218                         _exit(255);
219                 }
220                 argv = discard_const_p(char *, l);
221         }
222         va_end(ap);
223
224         (void)execvp(state->arg0, argv);
225         fprintf(stderr, "Failed to exec child - %s\n", strerror(errno));
226         _exit(255);
227         return NULL;
228 }
229
230 /*
231   handle stdout/stderr from the child
232  */
233 static void samba_runcmd_io_handler(struct tevent_context *ev,
234                                     struct tevent_fd *fde,
235                                     uint16_t flags,
236                                     void *private_data)
237 {
238         struct tevent_req *req = talloc_get_type_abort(private_data,
239                                  struct tevent_req);
240         struct samba_runcmd_state *state = tevent_req_data(req,
241                                            struct samba_runcmd_state);
242         int level;
243         char *p;
244         int n, fd;
245
246         if (fde == state->fde_stdout) {
247                 level = state->stdout_log_level;
248                 fd = state->fd_stdout;
249         } else if (fde == state->fde_stderr) {
250                 level = state->stderr_log_level;
251                 fd = state->fd_stderr;
252         } else {
253                 return;
254         }
255
256         if (!(flags & TEVENT_FD_READ)) {
257                 return;
258         }
259
260         n = read(fd, &state->buf[state->buf_used],
261                  sizeof(state->buf) - state->buf_used);
262         if (n > 0) {
263                 state->buf_used += n;
264         } else if (n == 0) {
265                 if (fde == state->fde_stdout) {
266                         talloc_free(fde);
267                         state->fde_stdout = NULL;
268                 }
269                 if (fde == state->fde_stderr) {
270                         talloc_free(fde);
271                         state->fde_stderr = NULL;
272                 }
273                 if (state->fde_stdout == NULL &&
274                     state->fde_stderr == NULL) {
275                         int status;
276                         /* the child has closed both stdout and
277                          * stderr, assume its dead */
278                         pid_t pid = waitpid(state->pid, &status, 0);
279                         if (pid != state->pid) {
280                                 if (errno == ECHILD) {
281                                         /* this happens when the
282                                            parent has set SIGCHLD to
283                                            SIG_IGN. In that case we
284                                            can only get error
285                                            information for the child
286                                            via its logging. We should
287                                            stop using SIG_IGN on
288                                            SIGCHLD in the standard
289                                            process model.
290                                         */
291                                         DEBUG(0, ("Error in waitpid() unexpectedly got ECHILD "
292                                                   "for %s child %d - %s, "
293                                                   "someone has set SIGCHLD to SIG_IGN!\n",
294                                           state->arg0, state->pid, strerror(errno)));
295                                         tevent_req_error(req, errno);
296                                         return;
297                                 }
298                                 DEBUG(0,("Error in waitpid() for child %s - %s \n",
299                                          state->arg0, strerror(errno)));
300                                 if (errno == 0) {
301                                         errno = ECHILD;
302                                 }
303                                 tevent_req_error(req, errno);
304                                 return;
305                         }
306                         status = WEXITSTATUS(status);
307                         DEBUG(3,("Child %s exited with status %d - %s\n",
308                                  state->arg0, status, strerror(status)));
309                         if (status != 0) {
310                                 tevent_req_error(req, status);
311                                 return;
312                         }
313
314                         tevent_req_done(req);
315                         return;
316                 }
317                 return;
318         }
319
320         while (state->buf_used > 0 &&
321                (p = (char *)memchr(state->buf, '\n', state->buf_used)) != NULL) {
322                 int n1 = (p - state->buf)+1;
323                 int n2 = n1 - 1;
324                 /* swallow \r from child processes */
325                 if (n2 > 0 && state->buf[n2-1] == '\r') {
326                         n2--;
327                 }
328                 DEBUG(level,("%s: %*.*s\n", state->arg0, n2, n2, state->buf));
329                 memmove(state->buf, p+1, sizeof(state->buf) - n1);
330                 state->buf_used -= n1;
331         }
332
333         /* the buffer could have completely filled - unfortunately we have
334            no choice but to dump it out straight away */
335         if (state->buf_used == sizeof(state->buf)) {
336                 DEBUG(level,("%s: %*.*s\n",
337                              state->arg0, state->buf_used,
338                              state->buf_used, state->buf));
339                 state->buf_used = 0;
340         }
341 }
342
343 int samba_runcmd_recv(struct tevent_req *req, int *perrno)
344 {
345         if (tevent_req_is_unix_error(req, perrno)) {
346                 tevent_req_received(req);
347                 return -1;
348         }
349
350         tevent_req_received(req);
351         return 0;
352 }