blob: 5a02db0fb43240c8016bd99207d755c4048874ce [file] [log] [blame]
Alexey Kodanev4bc55ec2013-06-27 18:52:22 +04001/*
2 * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it would 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, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Author: Alexey Kodanev <alexey.kodanev@oracle.com>
19 *
20 */
21
Stanislav kholmanskikhbee0a942013-08-06 16:54:37 +040022#include <errno.h>
Alexey Kodanev4bc55ec2013-06-27 18:52:22 +040023#include <sys/types.h>
Stanislav kholmanskikhbee0a942013-08-06 16:54:37 +040024#include <sys/stat.h>
Alexey Kodanev4bc55ec2013-06-27 18:52:22 +040025#include <sys/wait.h>
Stanislav kholmanskikhbee0a942013-08-06 16:54:37 +040026#include <fcntl.h>
Alexey Kodanev4bc55ec2013-06-27 18:52:22 +040027#include <unistd.h>
Cyril Hrubis989d3062013-08-08 16:39:14 +020028#include <signal.h>
Alexey Kodanev4bc55ec2013-06-27 18:52:22 +040029#include "test.h"
30
Stanislav kholmanskikhbee0a942013-08-06 16:54:37 +040031#define OPEN_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
32#define OPEN_FLAGS (O_WRONLY | O_APPEND | O_CREAT)
33
34void tst_run_cmd_fds(void (cleanup_fn)(void),
Stanislav Kholmanskikhe63337a2013-08-08 11:18:06 +040035 const char *const argv[],
Stanislav kholmanskikhbee0a942013-08-06 16:54:37 +040036 int stdout_fd,
37 int stderr_fd)
Alexey Kodanev4bc55ec2013-06-27 18:52:22 +040038{
39 if (argv == NULL || argv[0] == NULL) {
40 tst_brkm(TBROK, cleanup_fn,
41 "argument list is empty at %s:%d", __FILE__, __LINE__);
42 }
43
Cyril Hrubis989d3062013-08-08 16:39:14 +020044 /*
45 * The tst_sig() install poisoned signal handlers for all signals the
46 * test is not expected to get.
47 *
48 * So we temporarily disable the handler for sigchild we get after our
49 * child exits so that we don't have to disable it in each test that
50 * uses this interface.
51 */
52 void *old_handler = signal(SIGCHLD, SIG_DFL);
53
Alexey Kodanev4bc55ec2013-06-27 18:52:22 +040054 pid_t pid = vfork();
55 if (pid == -1) {
56 tst_brkm(TBROK | TERRNO, cleanup_fn, "vfork failed at %s:%d",
57 __FILE__, __LINE__);
58 }
Stanislav kholmanskikhbee0a942013-08-06 16:54:37 +040059 if (!pid) {
60 /* redirecting stdout and stderr if needed */
61 if (stdout_fd != -1) {
62 close(STDOUT_FILENO);
63 dup2(stdout_fd, STDOUT_FILENO);
64 }
65
66 if (stderr_fd != -1) {
67 close(STDERR_FILENO);
68 dup2(stderr_fd, STDERR_FILENO);
69 }
70
Stanislav Kholmanskikhe63337a2013-08-08 11:18:06 +040071 _exit(execvp(argv[0], (char *const *)argv));
Stanislav kholmanskikhbee0a942013-08-06 16:54:37 +040072 }
Alexey Kodanev4bc55ec2013-06-27 18:52:22 +040073
74 int ret = -1;
75 if (waitpid(pid, &ret, 0) != pid) {
Cyril Hrubis989d3062013-08-08 16:39:14 +020076 tst_brkm(TBROK | TERRNO, cleanup_fn, "waitpid failed at %s:%d",
Alexey Kodanev4bc55ec2013-06-27 18:52:22 +040077 __FILE__, __LINE__);
78 }
79
Cyril Hrubis989d3062013-08-08 16:39:14 +020080 signal(SIGCHLD, old_handler);
81
Alexey Kodanev4bc55ec2013-06-27 18:52:22 +040082 if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
83 tst_brkm(TBROK, cleanup_fn, "failed to exec cmd '%s' at %s:%d",
84 argv[0], __FILE__, __LINE__);
85 }
86}
Stanislav kholmanskikhbee0a942013-08-06 16:54:37 +040087
88void tst_run_cmd(void (cleanup_fn)(void),
Stanislav Kholmanskikhe63337a2013-08-08 11:18:06 +040089 const char *const argv[],
Stanislav kholmanskikhbee0a942013-08-06 16:54:37 +040090 const char *stdout_path,
91 const char *stderr_path)
92{
93 int stdout_fd = -1;
94 int stderr_fd = -1;
95
96 if (stdout_path != NULL) {
97 stdout_fd = open(stdout_path,
98 OPEN_FLAGS, OPEN_MODE);
99
100 if (stdout_fd == -1)
101 tst_resm(TWARN | TERRNO,
102 "open() on %s failed at %s:%d",
103 stdout_path, __FILE__, __LINE__);
104 }
105
106 if (stderr_path != NULL) {
107 stderr_fd = open(stderr_path,
108 OPEN_FLAGS, OPEN_MODE);
109
110 if (stderr_fd == -1)
111 tst_resm(TWARN | TERRNO,
112 "open() on %s failed at %s:%d",
113 stderr_path, __FILE__, __LINE__);
114 }
115
116 tst_run_cmd_fds(cleanup_fn, argv, stdout_fd, stderr_fd);
117
118 if ((stdout_fd != -1) && (close(stdout_fd) == -1))
119 tst_resm(TWARN | TERRNO,
120 "close() on %s failed at %s:%d",
121 stdout_path, __FILE__, __LINE__);
122
123 if ((stderr_fd != -1) && (close(stderr_fd) == -1))
124 tst_resm(TWARN | TERRNO,
125 "close() on %s failed at %s:%d",
126 stderr_path, __FILE__, __LINE__);
127}
George Wanga83cbb82014-12-18 22:02:14 +0800128
129int tst_system(const char *command)
130{
131 int ret = 0;
132
133 /*
134 *Temporarily disable SIGCHLD of user defined handler, so the
135 *system(3) function will not cause unexpected SIGCHLD signal
136 *callback function for test cases.
137 */
138 void *old_handler = signal(SIGCHLD, SIG_DFL);
139
140 ret = system(command);
141
142 signal(SIGCHLD, old_handler);
143 return ret;
144}