blob: fe2befea1e730a5e5cb3ee4d575df76c163ca218 [file] [log] [blame]
Josh Poimboeuf1843b4e2015-12-15 09:39:40 -06001#ifndef __SUBCMD_RUN_COMMAND_H
2#define __SUBCMD_RUN_COMMAND_H
Ingo Molnar07800602009-04-20 15:00:56 +02003
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -06004#include <unistd.h>
5
Ingo Molnar07800602009-04-20 15:00:56 +02006enum {
7 ERR_RUN_COMMAND_FORK = 10000,
8 ERR_RUN_COMMAND_EXEC,
9 ERR_RUN_COMMAND_PIPE,
10 ERR_RUN_COMMAND_WAITPID,
11 ERR_RUN_COMMAND_WAITPID_WRONG_PID,
12 ERR_RUN_COMMAND_WAITPID_SIGNAL,
13 ERR_RUN_COMMAND_WAITPID_NOEXIT,
14};
15#define IS_RUN_COMMAND_ERR(x) (-(x) >= ERR_RUN_COMMAND_FORK)
16
17struct child_process {
18 const char **argv;
19 pid_t pid;
20 /*
21 * Using .in, .out, .err:
22 * - Specify 0 for no redirections (child inherits stdin, stdout,
23 * stderr from parent).
24 * - Specify -1 to have a pipe allocated as follows:
25 * .in: returns the writable pipe end; parent writes to it,
26 * the readable pipe end becomes child's stdin
27 * .out, .err: returns the readable pipe end; parent reads from
28 * it, the writable pipe end becomes child's stdout/stderr
29 * The caller of start_command() must close the returned FDs
30 * after it has completed reading from/writing to it!
31 * - Specify > 0 to set a channel to a particular FD as follows:
32 * .in: a readable FD, becomes child's stdin
33 * .out: a writable FD, becomes child's stdout/stderr
34 * .err > 0 not supported
35 * The specified FD is closed by start_command(), even in case
36 * of errors!
37 */
38 int in;
39 int out;
40 int err;
41 const char *dir;
42 const char *const *env;
43 unsigned no_stdin:1;
44 unsigned no_stdout:1;
45 unsigned no_stderr:1;
Josh Poimboeuf46113a52015-12-15 09:39:37 -060046 unsigned exec_cmd:1; /* if this is to be external sub-command */
Ingo Molnar07800602009-04-20 15:00:56 +020047 unsigned stdout_to_stderr:1;
48 void (*preexec_cb)(void);
49};
50
51int start_command(struct child_process *);
52int finish_command(struct child_process *);
53int run_command(struct child_process *);
54
Ingo Molnar07800602009-04-20 15:00:56 +020055#define RUN_COMMAND_NO_STDIN 1
Josh Poimboeuf46113a52015-12-15 09:39:37 -060056#define RUN_EXEC_CMD 2 /*If this is to be external sub-command */
Ingo Molnar07800602009-04-20 15:00:56 +020057#define RUN_COMMAND_STDOUT_TO_STDERR 4
58int run_command_v_opt(const char **argv, int opt);
59
Josh Poimboeuf1843b4e2015-12-15 09:39:40 -060060#endif /* __SUBCMD_RUN_COMMAND_H */