1 /* SPDX-License-Identifier: GPL-2.0-or-later
2 * Copyright (c) 2015-2016 Cyril Hrubis <chrubis@suse.cz>
3 */
4
5 #ifndef TST_CMD_H__
6 #define TST_CMD_H__
7
8 enum tst_cmd_flags {
9 /*
10 * return the program exit code, otherwise it will call cleanup_fn() if the
11 * program exit code is not zero.
12 */
13 TST_CMD_PASS_RETVAL = 1,
14
15 /* exit with TCONF if program is not in path */
16 TST_CMD_TCONF_ON_MISSING = 2,
17 };
18
19 /*
20 * vfork() + execvp() specified program.
21 * @argv: a list of two (at least program name + NULL) or more pointers that
22 * represent the argument list to the new program. The array of pointers
23 * must be terminated by a NULL pointer.
24 * @stdout_fd: file descriptor where to redirect stdout. Set -1 if
25 * redirection is not needed.
26 * @stderr_fd: file descriptor where to redirect stderr. Set -1 if
27 * redirection is not needed.
28 * @flags: enum tst_cmd_flags
29 */
30 int tst_cmd_fds_(void (cleanup_fn)(void),
31 const char *const argv[],
32 int stdout_fd,
33 int stderr_fd,
34 enum tst_cmd_flags flags);
35
36 /* Executes tst_cmd_fds() and redirects its output to a file
37 * @stdout_path: path where to redirect stdout. Set NULL if redirection is
38 * not needed.
39 * @stderr_path: path where to redirect stderr. Set NULL if redirection is
40 * not needed.
41 * @flags: enum tst_cmd_flags
42 */
43 int tst_cmd_(void (cleanup_fn)(void),
44 const char *const argv[],
45 const char *stdout_path,
46 const char *stderr_path,
47 enum tst_cmd_flags flags);
48
49 #ifdef TST_TEST_H__
tst_cmd_fds(const char * const argv[],int stdout_fd,int stderr_fd,enum tst_cmd_flags flags)50 static inline int tst_cmd_fds(const char *const argv[],
51 int stdout_fd,
52 int stderr_fd,
53 enum tst_cmd_flags flags)
54 {
55 return tst_cmd_fds_(NULL, argv,
56 stdout_fd, stderr_fd, flags);
57 }
58
tst_cmd(const char * const argv[],const char * stdout_path,const char * stderr_path,enum tst_cmd_flags flags)59 static inline int tst_cmd(const char *const argv[],
60 const char *stdout_path,
61 const char *stderr_path,
62 enum tst_cmd_flags flags)
63 {
64 return tst_cmd_(NULL, argv,
65 stdout_path, stderr_path, flags);
66 }
67 #else
tst_cmd_fds(void (cleanup_fn)(void),const char * const argv[],int stdout_fd,int stderr_fd,enum tst_cmd_flags flags)68 static inline int tst_cmd_fds(void (cleanup_fn)(void),
69 const char *const argv[],
70 int stdout_fd,
71 int stderr_fd,
72 enum tst_cmd_flags flags)
73 {
74 return tst_cmd_fds_(cleanup_fn, argv,
75 stdout_fd, stderr_fd, flags);
76 }
77
tst_cmd(void (cleanup_fn)(void),const char * const argv[],const char * stdout_path,const char * stderr_path,enum tst_cmd_flags flags)78 static inline int tst_cmd(void (cleanup_fn)(void),
79 const char *const argv[],
80 const char *stdout_path,
81 const char *stderr_path,
82 enum tst_cmd_flags flags)
83 {
84 return tst_cmd_(cleanup_fn, argv,
85 stdout_path, stderr_path, flags);
86 }
87 #endif
88
89 /* Wrapper function for system(3), ignorcing SIGCHLD signal.
90 * @command: the command to be run.
91 */
92 int tst_system(const char *command);
93
94 #endif /* TST_CMD_H__ */
95