xref: /aosp_15_r20/external/ltp/include/tst_cmd.h (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright (c) 2015-2016 Cyril Hrubis <[email protected]>
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  *
22  * @param argv A list of two (at least program name + NULL) or more pointers that
23  * represent the argument list to the new program. The array of pointers
24  * must be terminated by a NULL pointer.
25  * @param stdout_fd File descriptor where to redirect stdout. Set -1 if
26  * redirection is not needed.
27  * @param stderr_fd File descriptor where to redirect stderr. Set -1 if
28  * redirection is not needed.
29  * @param flags enum tst_cmd_flags.
30  * @return The exit status of the program.
31  */
32 int tst_cmd_fds_(void (cleanup_fn)(void),
33 			const char *const argv[],
34 			int stdout_fd,
35 			int stderr_fd,
36 			enum tst_cmd_flags flags);
37 
38 /*
39  * Executes tst_cmd_fds() and redirects its output to a file.
40  *
41  * @param stdout_path Path where to redirect stdout. Set NULL if redirection is
42  * not needed.
43  * @param stderr_path Path where to redirect stderr. Set NULL if redirection is
44  * not needed.
45  * @param flags enum tst_cmd_flags.
46  * @return The exit status of the program.
47  */
48 int tst_cmd_(void (cleanup_fn)(void),
49 		const char *const argv[],
50 		const char *stdout_path,
51 		const char *stderr_path,
52 		enum tst_cmd_flags flags);
53 
54 #ifdef TST_TEST_H__
tst_cmd_fds(const char * const argv[],int stdout_fd,int stderr_fd,enum tst_cmd_flags flags)55 static inline int tst_cmd_fds(const char *const argv[],
56 				  int stdout_fd,
57 				  int stderr_fd,
58 				  enum tst_cmd_flags flags)
59 {
60 	return tst_cmd_fds_(NULL, argv,
61 	                        stdout_fd, stderr_fd, flags);
62 }
63 
tst_cmd(const char * const argv[],const char * stdout_path,const char * stderr_path,enum tst_cmd_flags flags)64 static inline int tst_cmd(const char *const argv[],
65 			      const char *stdout_path,
66 			      const char *stderr_path,
67 			      enum tst_cmd_flags flags)
68 {
69 	return tst_cmd_(NULL, argv,
70 	                    stdout_path, stderr_path, flags);
71 }
72 #else
tst_cmd_fds(void (cleanup_fn)(void),const char * const argv[],int stdout_fd,int stderr_fd,enum tst_cmd_flags flags)73 static inline int tst_cmd_fds(void (cleanup_fn)(void),
74 				  const char *const argv[],
75 				  int stdout_fd,
76 				  int stderr_fd,
77 				  enum tst_cmd_flags flags)
78 {
79 	return tst_cmd_fds_(cleanup_fn, argv,
80 	                        stdout_fd, stderr_fd, flags);
81 }
82 
tst_cmd(void (cleanup_fn)(void),const char * const argv[],const char * stdout_path,const char * stderr_path,enum tst_cmd_flags flags)83 static inline int tst_cmd(void (cleanup_fn)(void),
84 			      const char *const argv[],
85 			      const char *stdout_path,
86 			      const char *stderr_path,
87 			      enum tst_cmd_flags flags)
88 {
89 	return tst_cmd_(cleanup_fn, argv,
90 	                    stdout_path, stderr_path, flags);
91 }
92 #endif
93 
94 /* Wrapper function for system(3), ignorcing SIGCHLD signal.
95  * @param command The command to be run.
96  */
97 int tst_system(const char *command);
98 
99 #endif	/* TST_CMD_H__ */
100