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 */
65 static int process_read_stdout_once(struct process_info *procinfo)
67 struct process *process = &procinfo->process;
70 assert(process->keep_stdout);
72 fd = procinfo->stdout_pipe[0];
74 max_len = procinfo->stdout_buf_len - process->stdout_len - 1;
76 rc = read(fd, process->stdout_buf + process->stdout_len, max_len);
80 process->stdout_len += rc;
81 if (process->stdout_len == procinfo->stdout_buf_len - 1) {
82 procinfo->stdout_buf_len *= 2;
83 process->stdout_buf = talloc_realloc(procinfo,
84 process->stdout_buf, char,
85 procinfo->stdout_buf_len);
91 static int process_setup_stdout_pipe(struct process_info *procinfo)
95 if (!procinfo->process.keep_stdout)
98 procinfo->stdout_buf_len = 4096;
99 procinfo->process.stdout_len = 0;
100 procinfo->process.stdout_buf = talloc_array(procinfo, char,
101 procinfo->stdout_buf_len);
103 rc = pipe(procinfo->stdout_pipe);
105 pb_log("pipe failed");
111 static void process_setup_stdout_parent(struct process_info *procinfo)
113 if (!procinfo->process.keep_stdout)
116 close(procinfo->stdout_pipe[1]);
119 static void process_setup_stdout_child(struct process_info *procinfo)
121 int log = fileno(pb_log_get_stream());
123 if (procinfo->process.keep_stdout)
124 dup2(procinfo->stdout_pipe[1], STDOUT_FILENO);
126 dup2(log, STDOUT_FILENO);
128 if (procinfo->process.keep_stdout && procinfo->process.add_stderr)
129 dup2(procinfo->stdout_pipe[1], STDERR_FILENO);
131 dup2(log, STDERR_FILENO);
134 static void process_finish_stdout(struct process_info *procinfo)
136 close(procinfo->stdout_pipe[0]);
137 procinfo->process.stdout_buf[procinfo->process.stdout_len] = '\0';
140 static int process_read_stdout(struct process_info *procinfo)
144 if (!procinfo->process.keep_stdout)
148 rc = process_read_stdout_once(procinfo);
151 process_finish_stdout(procinfo);
153 return rc < 0 ? rc : 0;
156 static int process_stdout_cb(void *arg)
158 struct process_info *procinfo = arg;
161 rc = process_read_stdout_once(procinfo);
163 /* if we're going to signal to the waitset that we're done (ie, non-zero
164 * return value), then the waiters will remove us, so we drop the
167 talloc_unlink(procset, procinfo);
168 procinfo->stdout_waiter = NULL;
177 static void sigchld_sigaction(int signo, siginfo_t *info,
178 void *arg __attribute__((unused)))
183 if (signo != SIGCHLD)
188 rc = write(procset->sigchld_pipe[1], &pid, sizeof(pid));
189 if (rc != sizeof(pid))
190 pb_log("%s: write failed: %s\n", __func__, strerror(errno));
193 static int sigchld_pipe_event(void *arg)
195 struct process_info *procinfo;
196 struct procset *procset = arg;
197 struct process *process;
200 rc = read(procset->sigchld_pipe[0], &pid, sizeof(pid));
201 if (rc != sizeof(pid))
205 list_for_each_entry(&procset->async_list, procinfo, async_list) {
206 if (procinfo->process.pid == pid) {
207 process = &procinfo->process;
212 /* We'll receive SIGCHLD for synchronous processes too; just ignore */
216 rc = waitpid(process->pid, &process->exit_status, WNOHANG);
218 /* if the process is still running, ignore the event. We leave
219 * the process in async_list so we can manage the final signal */
223 /* ensure we have all of the child's stdout */
224 process_read_stdout(procinfo);
226 if (process->exit_cb)
227 process->exit_cb(process);
229 list_remove(&procinfo->async_list);
230 talloc_unlink(procset, procinfo);
235 static int process_fini(void *p)
237 struct procset *procset = p;
240 memset(&sa, 0, sizeof(sa));
241 sa.sa_handler = SIG_DFL;
243 sigaction(SIGCHLD, &sa, NULL);
245 waiter_remove(procset->sigchld_waiter);
247 close(procset->sigchld_pipe[0]);
248 close(procset->sigchld_pipe[1]);
252 struct procset *process_init(void *ctx, struct waitset *set, bool dry_run)
257 procset = talloc(ctx, struct procset);
258 procset->waitset = set;
259 procset->dry_run = dry_run;
260 list_init(&procset->async_list);
262 rc = pipe(procset->sigchld_pipe);
264 pb_log("%s: pipe() failed: %s\n", __func__, strerror(errno));
268 procset->sigchld_waiter = waiter_register_io(set,
269 procset->sigchld_pipe[0], WAIT_IN,
270 sigchld_pipe_event, procset);
271 if (!procset->sigchld_waiter)
274 memset(&sa, 0, sizeof(sa));
275 sa.sa_sigaction = sigchld_sigaction;
276 sa.sa_flags = SA_SIGINFO | SA_NOCLDSTOP;
278 rc = sigaction(SIGCHLD, &sa, NULL);
280 pb_log("%s: sigaction() failed: %s\n", __func__,
285 talloc_set_destructor(procset, process_fini);
290 waiter_remove(procset->sigchld_waiter);
292 close(procset->sigchld_pipe[0]);
293 close(procset->sigchld_pipe[1]);
295 talloc_free(procset);
299 struct process *process_create(void *ctx)
301 struct process_info *info = talloc_zero(ctx, struct process_info);
302 info->orig_ctx = ctx;
303 return &info->process;
306 void process_release(struct process *process)
308 struct process_info *info = get_info(process);
309 talloc_unlink(info->orig_ctx, info);
312 static int process_run_common(struct process_info *procinfo)
314 struct process *process = &procinfo->process;
320 logmsg = talloc_asprintf(procinfo, " exe: %s\n argv:", process->path);
321 for (i = 0, arg = process->argv[i]; arg; i++, arg = process->argv[i])
322 logmsg = talloc_asprintf_append(logmsg, " '%s'", arg);
324 pb_log("Running command:\n%s\n", logmsg);
326 rc = process_setup_stdout_pipe(procinfo);
332 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
337 process_setup_stdout_child(procinfo);
338 if (procset->dry_run)
340 execvp(process->path, (char * const *)process->argv);
344 process_setup_stdout_parent(procinfo);
350 int process_run_sync(struct process *process)
352 struct process_info *procinfo = get_info(process);
355 rc = process_run_common(procinfo);
359 process_read_stdout(procinfo);
362 rc = waitpid(process->pid, &process->exit_status, 0);
368 pb_log("%s: waitpid failed: %s\n", __func__, strerror(errno));
375 int process_run_async(struct process *process)
377 struct process_info *procinfo = get_info(process);
380 rc = process_run_common(procinfo);
384 if (process->keep_stdout) {
385 procinfo->stdout_waiter = waiter_register_io(procset->waitset,
386 procinfo->stdout_pipe[0],
387 WAIT_IN, process_stdout_cb,
389 talloc_reference(procset, procinfo);
392 list_add(&procset->async_list, &procinfo->async_list);
393 talloc_reference(procset, procinfo);
398 void process_stop_async(struct process *process)
400 kill(process->pid, SIGTERM);
403 int process_run_simple_argv(void *ctx, const char *argv[])
405 struct process *process;
408 process = process_create(ctx);
410 process->path = argv[0];
411 process->argv = argv;
413 rc = process_run_sync(process);
416 rc = process->exit_status;
418 process_release(process);
423 int process_run_simple(void *ctx, const char *name, ...)
425 int rc, i, n_argv = 1;
430 while (va_arg(ap, char *))
434 argv = talloc_array(ctx, const char *, n_argv + 1);
438 for (i = 1; i < n_argv; i++)
439 argv[i] = va_arg(ap, const char *);
444 rc = process_run_simple_argv(ctx, argv);
451 bool process_exit_ok(struct process *process)
453 return WIFEXITED(process->exit_status) &&
454 WEXITSTATUS(process->exit_status) == 0;