Alexey Kodanev | 4bc55ec | 2013-06-27 18:52:22 +0400 | [diff] [blame] | 1 | /* |
| 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 kholmanskikh | bee0a94 | 2013-08-06 16:54:37 +0400 | [diff] [blame] | 22 | #include <errno.h> |
Alexey Kodanev | 4bc55ec | 2013-06-27 18:52:22 +0400 | [diff] [blame] | 23 | #include <sys/types.h> |
Stanislav kholmanskikh | bee0a94 | 2013-08-06 16:54:37 +0400 | [diff] [blame] | 24 | #include <sys/stat.h> |
Alexey Kodanev | 4bc55ec | 2013-06-27 18:52:22 +0400 | [diff] [blame] | 25 | #include <sys/wait.h> |
Stanislav kholmanskikh | bee0a94 | 2013-08-06 16:54:37 +0400 | [diff] [blame] | 26 | #include <fcntl.h> |
Alexey Kodanev | 4bc55ec | 2013-06-27 18:52:22 +0400 | [diff] [blame] | 27 | #include <unistd.h> |
Cyril Hrubis | 989d306 | 2013-08-08 16:39:14 +0200 | [diff] [blame] | 28 | #include <signal.h> |
Alexey Kodanev | 4bc55ec | 2013-06-27 18:52:22 +0400 | [diff] [blame] | 29 | #include "test.h" |
| 30 | |
Stanislav kholmanskikh | bee0a94 | 2013-08-06 16:54:37 +0400 | [diff] [blame] | 31 | #define OPEN_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) |
| 32 | #define OPEN_FLAGS (O_WRONLY | O_APPEND | O_CREAT) |
| 33 | |
| 34 | void tst_run_cmd_fds(void (cleanup_fn)(void), |
Stanislav Kholmanskikh | e63337a | 2013-08-08 11:18:06 +0400 | [diff] [blame] | 35 | const char *const argv[], |
Stanislav kholmanskikh | bee0a94 | 2013-08-06 16:54:37 +0400 | [diff] [blame] | 36 | int stdout_fd, |
| 37 | int stderr_fd) |
Alexey Kodanev | 4bc55ec | 2013-06-27 18:52:22 +0400 | [diff] [blame] | 38 | { |
| 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 Hrubis | 989d306 | 2013-08-08 16:39:14 +0200 | [diff] [blame] | 44 | /* |
| 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 Kodanev | 4bc55ec | 2013-06-27 18:52:22 +0400 | [diff] [blame] | 54 | 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 kholmanskikh | bee0a94 | 2013-08-06 16:54:37 +0400 | [diff] [blame] | 59 | 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 Kholmanskikh | e63337a | 2013-08-08 11:18:06 +0400 | [diff] [blame] | 71 | _exit(execvp(argv[0], (char *const *)argv)); |
Stanislav kholmanskikh | bee0a94 | 2013-08-06 16:54:37 +0400 | [diff] [blame] | 72 | } |
Alexey Kodanev | 4bc55ec | 2013-06-27 18:52:22 +0400 | [diff] [blame] | 73 | |
| 74 | int ret = -1; |
| 75 | if (waitpid(pid, &ret, 0) != pid) { |
Cyril Hrubis | 989d306 | 2013-08-08 16:39:14 +0200 | [diff] [blame] | 76 | tst_brkm(TBROK | TERRNO, cleanup_fn, "waitpid failed at %s:%d", |
Alexey Kodanev | 4bc55ec | 2013-06-27 18:52:22 +0400 | [diff] [blame] | 77 | __FILE__, __LINE__); |
| 78 | } |
| 79 | |
Cyril Hrubis | 989d306 | 2013-08-08 16:39:14 +0200 | [diff] [blame] | 80 | signal(SIGCHLD, old_handler); |
| 81 | |
Alexey Kodanev | 4bc55ec | 2013-06-27 18:52:22 +0400 | [diff] [blame] | 82 | 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 kholmanskikh | bee0a94 | 2013-08-06 16:54:37 +0400 | [diff] [blame] | 87 | |
| 88 | void tst_run_cmd(void (cleanup_fn)(void), |
Stanislav Kholmanskikh | e63337a | 2013-08-08 11:18:06 +0400 | [diff] [blame] | 89 | const char *const argv[], |
Stanislav kholmanskikh | bee0a94 | 2013-08-06 16:54:37 +0400 | [diff] [blame] | 90 | 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 Wang | a83cbb8 | 2014-12-18 22:02:14 +0800 | [diff] [blame^] | 128 | |
| 129 | int 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 | } |