2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; version 2 of the License.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <process/process.h>
23 #include <talloc/talloc.h>
24 #include <waiter/waiter.h>
28 struct waitset *waitset;
29 struct list async_list;
31 struct waiter *sigchld_waiter;
35 /* Internal data type for process handling
37 * Allocation: these structures may have multiple references:
38 * - from the original ctx pointer
39 * - due to inclusion in async_list
40 * - due to a currently-registered waiter
45 /* prevent talloc_free(process) from working */
48 struct process process;
49 struct list_item async_list;
51 struct waiter *stdout_waiter;
56 static struct procset *procset;
58 static struct process_info *get_info(struct process *process)
60 return container_of(process, struct process_info, process);
63 /* Read as much as possible into the currently-allocated stdout buffer, and
64 * possibly realloc it for the next read
67 * > 0 on success (even though no bytes may have been read)
68 * 0 on EOF (no error, but no more reads can be performed)
71 static int process_read_stdout_once(struct process_info *procinfo)
73 struct process *process = &procinfo->process;
76 assert(process->keep_stdout);
78 fd = procinfo->stdout_pipe[0];
80 max_len = procinfo->stdout_buf_len - process->stdout_len - 1;
82 rc = read(fd, process->stdout_buf + process->stdout_len, max_len);
88 pb_log("%s: read failed: %s\n", __func__, strerror(errno));
92 process->stdout_len += rc;
93 if (process->stdout_len == procinfo->stdout_buf_len - 1) {
94 procinfo->stdout_buf_len *= 2;
95 process->stdout_buf = talloc_realloc(procinfo,
96 process->stdout_buf, char,
97 procinfo->stdout_buf_len);
103 static int process_setup_stdout_pipe(struct process_info *procinfo)
107 if (!procinfo->process.keep_stdout)
110 procinfo->stdout_buf_len = 4096;
111 procinfo->process.stdout_len = 0;
112 procinfo->process.stdout_buf = talloc_array(procinfo, char,
113 procinfo->stdout_buf_len);
115 rc = pipe(procinfo->stdout_pipe);
117 pb_log("pipe failed");
123 static void process_setup_stdout_parent(struct process_info *procinfo)
125 if (!procinfo->process.keep_stdout)
128 close(procinfo->stdout_pipe[1]);
131 static void process_setup_stdout_child(struct process_info *procinfo)
133 int log = fileno(pb_log_get_stream());
135 if (procinfo->process.keep_stdout)
136 dup2(procinfo->stdout_pipe[1], STDOUT_FILENO);
138 dup2(log, STDOUT_FILENO);
140 if (procinfo->process.keep_stdout && procinfo->process.add_stderr)
141 dup2(procinfo->stdout_pipe[1], STDERR_FILENO);
143 dup2(log, STDERR_FILENO);
146 static void process_finish_stdout(struct process_info *procinfo)
148 close(procinfo->stdout_pipe[0]);
149 procinfo->process.stdout_buf[procinfo->process.stdout_len] = '\0';
152 static int process_read_stdout(struct process_info *procinfo)
156 if (!procinfo->process.keep_stdout)
160 rc = process_read_stdout_once(procinfo);
163 process_finish_stdout(procinfo);
165 return rc < 0 ? rc : 0;
168 static int process_stdout_cb(void *arg)
170 struct process_info *procinfo = arg;
173 rc = process_read_stdout_once(procinfo);
175 /* if we're going to signal to the waitset that we're done (ie, non-zero
176 * return value), then the waiters will remove us, so we drop the
179 talloc_unlink(procset, procinfo);
180 procinfo->stdout_waiter = NULL;
189 static void sigchld_sigaction(int signo, siginfo_t *info,
190 void *arg __attribute__((unused)))
195 if (signo != SIGCHLD)
200 rc = write(procset->sigchld_pipe[1], &pid, sizeof(pid));
201 if (rc != sizeof(pid))
202 pb_log("%s: write failed: %s\n", __func__, strerror(errno));
205 static int sigchld_pipe_event(void *arg)
207 struct process_info *procinfo;
208 struct procset *procset = arg;
209 struct process *process;
212 rc = read(procset->sigchld_pipe[0], &pid, sizeof(pid));
213 if (rc != sizeof(pid))
217 list_for_each_entry(&procset->async_list, procinfo, async_list) {
218 if (procinfo->process.pid == pid) {
219 process = &procinfo->process;
224 /* We'll receive SIGCHLD for synchronous processes too; just ignore */
228 rc = waitpid(process->pid, &process->exit_status, WNOHANG);
230 /* if the process is still running, ignore the event. We leave
231 * the process in async_list so we can manage the final signal */
235 /* ensure we have all of the child's stdout */
236 process_read_stdout(procinfo);
238 if (process->exit_cb)
239 process->exit_cb(process);
241 list_remove(&procinfo->async_list);
242 talloc_unlink(procset, procinfo);
247 static int process_fini(void *p)
249 struct procset *procset = p;
252 memset(&sa, 0, sizeof(sa));
253 sa.sa_handler = SIG_DFL;
255 sigaction(SIGCHLD, &sa, NULL);
257 waiter_remove(procset->sigchld_waiter);
259 close(procset->sigchld_pipe[0]);
260 close(procset->sigchld_pipe[1]);
264 struct procset *process_init(void *ctx, struct waitset *set, bool dry_run)
269 procset = talloc(ctx, struct procset);
270 procset->waitset = set;
271 procset->dry_run = dry_run;
272 list_init(&procset->async_list);
274 rc = pipe(procset->sigchld_pipe);
276 pb_log("%s: pipe() failed: %s\n", __func__, strerror(errno));
280 procset->sigchld_waiter = waiter_register_io(set,
281 procset->sigchld_pipe[0], WAIT_IN,
282 sigchld_pipe_event, procset);
283 if (!procset->sigchld_waiter)
286 memset(&sa, 0, sizeof(sa));
287 sa.sa_sigaction = sigchld_sigaction;
288 sa.sa_flags = SA_SIGINFO | SA_NOCLDSTOP;
290 rc = sigaction(SIGCHLD, &sa, NULL);
292 pb_log("%s: sigaction() failed: %s\n", __func__,
297 talloc_set_destructor(procset, process_fini);
302 waiter_remove(procset->sigchld_waiter);
304 close(procset->sigchld_pipe[0]);
305 close(procset->sigchld_pipe[1]);
307 talloc_free(procset);
311 struct process *process_create(void *ctx)
313 struct process_info *info = talloc_zero(ctx, struct process_info);
314 info->orig_ctx = ctx;
315 return &info->process;
318 void process_release(struct process *process)
320 struct process_info *info = get_info(process);
321 talloc_unlink(info->orig_ctx, info);
324 static int process_run_common(struct process_info *procinfo)
326 struct process *process = &procinfo->process;
332 logmsg = talloc_asprintf(procinfo, " exe: %s\n argv:", process->path);
333 for (i = 0, arg = process->argv[i]; arg; i++, arg = process->argv[i])
334 logmsg = talloc_asprintf_append(logmsg, " '%s'", arg);
336 pb_log("Running command:\n%s\n", logmsg);
338 rc = process_setup_stdout_pipe(procinfo);
344 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
349 process_setup_stdout_child(procinfo);
350 if (procset->dry_run)
352 execvp(process->path, (char * const *)process->argv);
356 process_setup_stdout_parent(procinfo);
362 int process_run_sync(struct process *process)
364 struct process_info *procinfo = get_info(process);
367 rc = process_run_common(procinfo);
371 process_read_stdout(procinfo);
374 rc = waitpid(process->pid, &process->exit_status, 0);
380 pb_log("%s: waitpid failed: %s\n", __func__, strerror(errno));
387 int process_run_async(struct process *process)
389 struct process_info *procinfo = get_info(process);
392 rc = process_run_common(procinfo);
396 if (process->keep_stdout) {
397 procinfo->stdout_waiter = waiter_register_io(procset->waitset,
398 procinfo->stdout_pipe[0],
399 WAIT_IN, process_stdout_cb,
401 talloc_reference(procset, procinfo);
404 list_add(&procset->async_list, &procinfo->async_list);
405 talloc_reference(procset, procinfo);
410 void process_stop_async(struct process *process)
412 pb_debug("process: sending SIGTERM to pid %d\n", process->pid);
413 kill(process->pid, SIGTERM);
416 int process_run_simple_argv(void *ctx, const char *argv[])
418 struct process *process;
421 process = process_create(ctx);
423 process->path = argv[0];
424 process->argv = argv;
426 rc = process_run_sync(process);
429 rc = process->exit_status;
431 process_release(process);
436 int process_run_simple(void *ctx, const char *name, ...)
438 int rc, i, n_argv = 1;
443 while (va_arg(ap, char *))
447 argv = talloc_array(ctx, const char *, n_argv + 1);
451 for (i = 1; i < n_argv; i++)
452 argv[i] = va_arg(ap, const char *);
457 rc = process_run_simple_argv(ctx, argv);
464 bool process_exit_ok(struct process *process)
466 return WIFEXITED(process->exit_status) &&
467 WEXITSTATUS(process->exit_status) == 0;