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