xref: /openbmc/linux/tools/lib/subcmd/run-command.h (revision 8c749ce9)
1 #ifndef __SUBCMD_RUN_COMMAND_H
2 #define __SUBCMD_RUN_COMMAND_H
3 
4 #include <unistd.h>
5 
6 enum {
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 
17 struct 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;
46 	unsigned exec_cmd:1; /* if this is to be external sub-command */
47 	unsigned stdout_to_stderr:1;
48 	void (*preexec_cb)(void);
49 };
50 
51 int start_command(struct child_process *);
52 int finish_command(struct child_process *);
53 int run_command(struct child_process *);
54 
55 #define RUN_COMMAND_NO_STDIN 1
56 #define RUN_EXEC_CMD	     2	/*If this is to be external sub-command */
57 #define RUN_COMMAND_STDOUT_TO_STDERR 4
58 int run_command_v_opt(const char **argv, int opt);
59 
60 #endif /* __SUBCMD_RUN_COMMAND_H */
61