• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015-2016 Cyril Hrubis <chrubis@suse.cz>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef TST_CMD_H__
19 #define TST_CMD_H__
20 
21 /*
22  * vfork() + execvp() specified program.
23  * @argv: a list of two (at least program name + NULL) or more pointers that
24  * represent the argument list to the new program. The array of pointers
25  * must be terminated by a NULL pointer.
26  * @stdout_fd: file descriptor where to redirect stdout. Set -1 if
27  * redirection is not needed.
28  * @stderr_fd: file descriptor where to redirect stderr. Set -1 if
29  * redirection is not needed.
30  * @pass_exit_val: if it's non-zero, this function will return the program
31  * exit code, otherwise it will call cleanup_fn() if the program
32  * exit code is not zero.
33  */
34 int tst_run_cmd_fds_(void (cleanup_fn)(void),
35 			const char *const argv[],
36 			int stdout_fd,
37 			int stderr_fd,
38 			int pass_exit_val);
39 
40 /* Executes tst_run_cmd_fds() and redirects its output to a file
41  * @stdout_path: path where to redirect stdout. Set NULL if redirection is
42  * not needed.
43  * @stderr_path: path where to redirect stderr. Set NULL if redirection is
44  * not needed.
45  * @pass_exit_val: if it's non-zero, this function will return the program
46  * exit code, otherwise it will call cleanup_fn() if the program
47  * exit code is not zero.
48  */
49 int tst_run_cmd_(void (cleanup_fn)(void),
50 		const char *const argv[],
51 		const char *stdout_path,
52 		const char *stderr_path,
53 		int pass_exit_val);
54 
55 #ifdef TST_TEST_H__
tst_run_cmd_fds(const char * const argv[],int stdout_fd,int stderr_fd,int pass_exit_val)56 static inline int tst_run_cmd_fds(const char *const argv[],
57 				  int stdout_fd,
58 				  int stderr_fd,
59 				  int pass_exit_val)
60 {
61 	return tst_run_cmd_fds_(NULL, argv,
62 	                        stdout_fd, stderr_fd, pass_exit_val);
63 }
64 
tst_run_cmd(const char * const argv[],const char * stdout_path,const char * stderr_path,int pass_exit_val)65 static inline int tst_run_cmd(const char *const argv[],
66 			      const char *stdout_path,
67 			      const char *stderr_path,
68 			      int pass_exit_val)
69 {
70 	return tst_run_cmd_(NULL, argv,
71 	                    stdout_path, stderr_path, pass_exit_val);
72 }
73 #else
tst_run_cmd_fds(void (cleanup_fn)(void),const char * const argv[],int stdout_fd,int stderr_fd,int pass_exit_val)74 static inline int tst_run_cmd_fds(void (cleanup_fn)(void),
75 				  const char *const argv[],
76 				  int stdout_fd,
77 				  int stderr_fd,
78 				  int pass_exit_val)
79 {
80 	return tst_run_cmd_fds_(cleanup_fn, argv,
81 	                        stdout_fd, stderr_fd, pass_exit_val);
82 }
83 
tst_run_cmd(void (cleanup_fn)(void),const char * const argv[],const char * stdout_path,const char * stderr_path,int pass_exit_val)84 static inline int tst_run_cmd(void (cleanup_fn)(void),
85 			      const char *const argv[],
86 			      const char *stdout_path,
87 			      const char *stderr_path,
88 			      int pass_exit_val)
89 {
90 	return tst_run_cmd_(cleanup_fn, argv,
91 	                    stdout_path, stderr_path, pass_exit_val);
92 }
93 #endif
94 
95 /* Wrapper function for system(3), ignorcing SIGCHLD signal.
96  * @command: the command to be run.
97  */
98 int tst_system(const char *command);
99 
100 #endif	/* TST_CMD_H__ */
101