xref: /linux/tools/lib/subcmd/run-command.h (revision 6e7fd890f1d6ac83805409e9c346240de2705584)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __SUBCMD_RUN_COMMAND_H
3 #define __SUBCMD_RUN_COMMAND_H
4 
5 #include <unistd.h>
6 
7 enum {
8 	ERR_RUN_COMMAND_FORK = 10000,
9 	ERR_RUN_COMMAND_EXEC,
10 	ERR_RUN_COMMAND_PIPE,
11 	ERR_RUN_COMMAND_WAITPID,
12 	ERR_RUN_COMMAND_WAITPID_WRONG_PID,
13 	ERR_RUN_COMMAND_WAITPID_SIGNAL,
14 	ERR_RUN_COMMAND_WAITPID_NOEXIT,
15 };
16 #define IS_RUN_COMMAND_ERR(x) (-(x) >= ERR_RUN_COMMAND_FORK)
17 
18 struct child_process {
19 	const char **argv;
20 	pid_t pid;
21 	/*
22 	 * Using .in, .out, .err:
23 	 * - Specify 0 for no redirections (child inherits stdin, stdout,
24 	 *   stderr from parent).
25 	 * - Specify -1 to have a pipe allocated as follows:
26 	 *     .in: returns the writable pipe end; parent writes to it,
27 	 *          the readable pipe end becomes child's stdin
28 	 *     .out, .err: returns the readable pipe end; parent reads from
29 	 *          it, the writable pipe end becomes child's stdout/stderr
30 	 *   The caller of start_command() must close the returned FDs
31 	 *   after it has completed reading from/writing to it!
32 	 * - Specify > 0 to set a channel to a particular FD as follows:
33 	 *     .in: a readable FD, becomes child's stdin
34 	 *     .out: a writable FD, becomes child's stdout/stderr
35 	 *     .err > 0 not supported
36 	 *   The specified FD is closed by start_command(), even in case
37 	 *   of errors!
38 	 */
39 	int in;
40 	int out;
41 	int err;
42 	const char *dir;
43 	const char *const *env;
44 	int finish_result;
45 	unsigned no_stdin:1;
46 	unsigned no_stdout:1;
47 	unsigned no_stderr:1;
48 	unsigned exec_cmd:1; /* if this is to be external sub-command */
49 	unsigned stdout_to_stderr:1;
50 	unsigned finished:1;
51 	void (*preexec_cb)(void);
52 	 /* If set, call function in child rather than doing an exec. */
53 	int (*no_exec_cmd)(struct child_process *process);
54 };
55 
56 int start_command(struct child_process *);
57 int check_if_command_finished(struct child_process *);
58 int finish_command(struct child_process *);
59 int run_command(struct child_process *);
60 
61 #define RUN_COMMAND_NO_STDIN 1
62 #define RUN_EXEC_CMD	     2	/*If this is to be external sub-command */
63 #define RUN_COMMAND_STDOUT_TO_STDERR 4
64 int run_command_v_opt(const char **argv, int opt);
65 
66 #endif /* __SUBCMD_RUN_COMMAND_H */
67