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 struct process *procinfo_get_process(struct process_info *procinfo)
65 return &procinfo->process;
68 /* Read as much as possible into the currently-allocated stdout buffer, and
69 * possibly realloc it for the next read
70 * If the line pointer is not NULL, it is set to the start of the latest
74 * > 0 on success (even though no bytes may have been read)
75 * 0 on EOF (no error, but no more reads can be performed)
78 static int process_read_stdout_once(struct process_info *procinfo, char **line)
80 struct process *process = &procinfo->process;
83 assert(process->keep_stdout);
85 fd = procinfo->stdout_pipe[0];
87 max_len = procinfo->stdout_buf_len - process->stdout_len - 1;
89 rc = read(fd, process->stdout_buf + process->stdout_len, max_len);
95 pb_log("%s: read failed: %s\n", __func__, strerror(errno));
100 *line = process->stdout_buf + process->stdout_len;
102 process->stdout_len += rc;
103 if (process->stdout_len == procinfo->stdout_buf_len - 1) {
104 procinfo->stdout_buf_len *= 2;
105 process->stdout_buf = talloc_realloc(procinfo,
106 process->stdout_buf, char,
107 procinfo->stdout_buf_len);
113 static int process_setup_stdout_pipe(struct process_info *procinfo)
117 if (!procinfo->process.keep_stdout || procinfo->process.raw_stdout)
120 procinfo->stdout_buf_len = 4096;
121 procinfo->process.stdout_len = 0;
122 procinfo->process.stdout_buf = talloc_array(procinfo, char,
123 procinfo->stdout_buf_len);
125 rc = pipe(procinfo->stdout_pipe);
127 pb_log("pipe failed");
133 static void process_setup_stdout_parent(struct process_info *procinfo)
135 if (!procinfo->process.keep_stdout || procinfo->process.raw_stdout)
138 close(procinfo->stdout_pipe[1]);
141 static void process_setup_stdout_child(struct process_info *procinfo)
143 int log = fileno(pb_log_get_stream());
145 if (procinfo->process.raw_stdout)
148 if (procinfo->process.keep_stdout)
149 dup2(procinfo->stdout_pipe[1], STDOUT_FILENO);
151 dup2(log, STDOUT_FILENO);
153 if (procinfo->process.keep_stdout && procinfo->process.add_stderr)
154 dup2(procinfo->stdout_pipe[1], STDERR_FILENO);
156 dup2(log, STDERR_FILENO);
159 static void process_finish_stdout(struct process_info *procinfo)
161 close(procinfo->stdout_pipe[0]);
162 procinfo->process.stdout_buf[procinfo->process.stdout_len] = '\0';
165 static int process_read_stdout(struct process_info *procinfo)
169 if (!procinfo->process.keep_stdout)
173 rc = process_read_stdout_once(procinfo, NULL);
176 process_finish_stdout(procinfo);
178 return rc < 0 ? rc : 0;
181 static int process_stdout_cb(void *arg)
183 struct process_info *procinfo = arg;
186 rc = process_read_stdout_once(procinfo, NULL);
188 /* if we're going to signal to the waitset that we're done (ie, non-zero
189 * return value), then the waiters will remove us, so we drop the
192 talloc_unlink(procset, procinfo);
193 procinfo->stdout_waiter = NULL;
202 int process_stdout_custom(struct process_info *procinfo, char **line)
206 rc = process_read_stdout_once(procinfo, line);
208 /* if we're going to signal to the waitset that we're done (ie, non-zero
209 * return value), then the waiters will remove us, so we drop the
212 talloc_unlink(procset, procinfo);
213 procinfo->stdout_waiter = NULL;
222 static void sigchld_sigaction(int signo, siginfo_t *info,
223 void *arg __attribute__((unused)))
228 if (signo != SIGCHLD)
233 rc = write(procset->sigchld_pipe[1], &pid, sizeof(pid));
234 if (rc != sizeof(pid))
235 pb_log("%s: write failed: %s\n", __func__, strerror(errno));
238 static int sigchld_pipe_event(void *arg)
240 struct process_info *procinfo;
241 struct procset *procset = arg;
242 struct process *process;
245 rc = read(procset->sigchld_pipe[0], &pid, sizeof(pid));
246 if (rc != sizeof(pid))
250 list_for_each_entry(&procset->async_list, procinfo, async_list) {
251 if (procinfo->process.pid == pid) {
252 process = &procinfo->process;
257 /* We'll receive SIGCHLD for synchronous processes too; just ignore */
261 rc = waitpid(process->pid, &process->exit_status, WNOHANG);
263 /* if the process is still running, ignore the event. We leave
264 * the process in async_list so we can manage the final signal */
268 /* ensure we have all of the child's stdout */
269 process_read_stdout(procinfo);
271 if (process->exit_cb)
272 process->exit_cb(process);
274 list_remove(&procinfo->async_list);
275 talloc_unlink(procset, procinfo);
280 static int process_fini(void *p)
282 struct procset *procset = p;
285 memset(&sa, 0, sizeof(sa));
286 sa.sa_handler = SIG_DFL;
288 sigaction(SIGCHLD, &sa, NULL);
290 waiter_remove(procset->sigchld_waiter);
292 close(procset->sigchld_pipe[0]);
293 close(procset->sigchld_pipe[1]);
297 struct procset *process_init(void *ctx, struct waitset *set, bool dry_run)
302 procset = talloc(ctx, struct procset);
303 procset->waitset = set;
304 procset->dry_run = dry_run;
305 list_init(&procset->async_list);
307 rc = pipe(procset->sigchld_pipe);
309 pb_log("%s: pipe() failed: %s\n", __func__, strerror(errno));
313 procset->sigchld_waiter = waiter_register_io(set,
314 procset->sigchld_pipe[0], WAIT_IN,
315 sigchld_pipe_event, procset);
316 if (!procset->sigchld_waiter)
319 memset(&sa, 0, sizeof(sa));
320 sa.sa_sigaction = sigchld_sigaction;
321 sa.sa_flags = SA_SIGINFO | SA_NOCLDSTOP;
323 rc = sigaction(SIGCHLD, &sa, NULL);
325 pb_log("%s: sigaction() failed: %s\n", __func__,
330 talloc_set_destructor(procset, process_fini);
335 waiter_remove(procset->sigchld_waiter);
337 close(procset->sigchld_pipe[0]);
338 close(procset->sigchld_pipe[1]);
340 talloc_free(procset);
344 struct process *process_create(void *ctx)
346 struct process_info *info = talloc_zero(ctx, struct process_info);
347 info->orig_ctx = ctx;
348 return &info->process;
351 void process_release(struct process *process)
353 struct process_info *info = get_info(process);
354 talloc_unlink(info->orig_ctx, info);
357 static int process_run_common(struct process_info *procinfo)
359 struct process *process = &procinfo->process;
365 logmsg = talloc_asprintf(procinfo, " exe: %s\n argv:", process->path);
366 for (i = 0, arg = process->argv[i]; arg; i++, arg = process->argv[i])
367 logmsg = talloc_asprintf_append(logmsg, " '%s'", arg);
369 pb_log("Running command:\n%s\n", logmsg);
371 rc = process_setup_stdout_pipe(procinfo);
377 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
382 process_setup_stdout_child(procinfo);
383 if (procset->dry_run)
385 execvp(process->path, (char * const *)process->argv);
389 process_setup_stdout_parent(procinfo);
395 int process_run_sync(struct process *process)
397 struct process_info *procinfo = get_info(process);
400 rc = process_run_common(procinfo);
404 process_read_stdout(procinfo);
407 rc = waitpid(process->pid, &process->exit_status, 0);
413 pb_log("%s: waitpid failed: %s\n", __func__, strerror(errno));
420 int process_run_async(struct process *process)
422 struct process_info *procinfo = get_info(process);
426 rc = process_run_common(procinfo);
430 if (process->keep_stdout) {
431 stdout_cb = process->stdout_cb ?: process_stdout_cb;
432 procinfo->stdout_waiter = waiter_register_io(procset->waitset,
433 procinfo->stdout_pipe[0],
434 WAIT_IN, stdout_cb, procinfo);
435 talloc_reference(procset, procinfo);
438 list_add(&procset->async_list, &procinfo->async_list);
439 talloc_reference(procset, procinfo);
444 void process_stop_async(struct process *process)
446 /* Avoid signalling an old pid */
447 if (process->cancelled)
450 pb_debug("process: sending SIGTERM to pid %d\n", process->pid);
451 kill(process->pid, SIGTERM);
452 process->cancelled = true;
455 void process_stop_async_all(void)
457 struct process_info *procinfo;
458 struct process *process = NULL;
460 pb_debug("process: cancelling all async jobs\n");
462 list_for_each_entry(&procset->async_list, procinfo, async_list) {
463 process = &procinfo->process;
464 /* Ignore the process completion - callbacks may use stale data */
465 process->exit_cb = NULL;
466 process->stdout_cb = NULL;
467 process_stop_async(process);
471 int process_run_simple_argv(void *ctx, const char *argv[])
473 struct process *process;
476 process = process_create(ctx);
478 process->path = argv[0];
479 process->argv = argv;
481 rc = process_run_sync(process);
484 rc = process->exit_status;
486 process_release(process);
491 int process_run_simple(void *ctx, const char *name, ...)
493 int rc, i, n_argv = 1;
498 while (va_arg(ap, char *))
502 argv = talloc_array(ctx, const char *, n_argv + 1);
506 for (i = 1; i < n_argv; i++)
507 argv[i] = va_arg(ap, const char *);
512 rc = process_run_simple_argv(ctx, argv);
519 bool process_exit_ok(struct process *process)
521 return WIFEXITED(process->exit_status) &&
522 WEXITSTATUS(process->exit_status) == 0;