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