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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
/*
* Copyright © 2019 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
*/
#ifndef IGT_LIB_TESTS_COMMON_H
#define IGT_LIB_TESTS_COMMON_H
#include <assert.h>
#include <sys/wait.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <regex.h>
/*
* We need to hide assert from the cocci igt test refactor spatch.
*
* IMPORTANT: Test infrastructure tests are the only valid places where using
* assert is allowed.
*/
#define internal_assert assert
static inline void internal_assert_wexited(int wstatus, int exitcode)
{
internal_assert(WIFEXITED(wstatus) &&
WEXITSTATUS(wstatus) == exitcode);
}
static inline void internal_assert_wsignaled(int wstatus, int signal)
{
internal_assert(WIFSIGNALED(wstatus) &&
WTERMSIG(wstatus) == signal);
}
static inline void internal_assert_not_wsignaled(int wstatus)
{
internal_assert(!WIFSIGNALED(wstatus));
}
static inline int do_fork(void (*test_to_run)(void))
{
int pid, status;
switch (pid = fork()) {
case -1:
internal_assert(0);
case 0:
test_to_run();
default:
while (waitpid(pid, &status, 0) == -1 &&
errno == EINTR)
;
return status;
}
}
static inline pid_t do_fork_bg_with_pipes(void (*test_to_run)(void), int *out, int *err)
{
int outfd[2], errfd[2];
pid_t pid;
if (out != NULL)
internal_assert(pipe(outfd) != -1);
if (err != NULL)
internal_assert(pipe(errfd) != -1);
pid = fork();
internal_assert(pid != -1);
if (pid == 0) {
/* we'll leak the /dev/null fds, let them die with the forked
* process, also close reading ends if they are any */
if (out == NULL)
outfd[1] = open("/dev/null", O_WRONLY);
else
close(outfd[0]);
if (err == NULL)
errfd[1] = open("/dev/null", O_WRONLY);
else
close(errfd[0]);
while (dup2(outfd[1], STDOUT_FILENO) == -1 && errno == EINTR) {}
while (dup2(errfd[1], STDERR_FILENO) == -1 && errno == EINTR) {}
close(outfd[1]);
close(errfd[1]);
test_to_run();
exit(-1);
} else {
/* close the writing ends */
if (out != NULL) {
close(outfd[1]);
*out = outfd[0];
}
if (err != NULL) {
close(errfd[1]);
*err = errfd[0];
}
return pid;
}
}
static inline int safe_wait(pid_t pid, int *status) {
int ret;
do {
ret = waitpid(pid, status, 0);
} while (ret == -1 && errno == EINTR);
return ret;
}
static inline void assert_pipe_empty(int fd)
{
char buf[5];
internal_assert(0 == read(fd, buf, sizeof(buf)));
}
static inline void read_whole_pipe(int fd, char *buf, size_t buflen)
{
ssize_t readlen;
off_t offset;
offset = 0;
while ((readlen = read(fd, buf+offset, buflen-offset))) {
if (readlen == -1) {
if (errno == EINTR) {
continue;
} else {
printf("read failed with %s\n", strerror(errno));
exit(1);
}
}
internal_assert(readlen != -1);
offset += readlen;
}
}
static inline bool matches(char *str, const char *regex)
{
int ret;
regex_t reg;
ret = regcomp(®, regex, REG_EXTENDED | REG_NEWLINE);
if (ret == 0)
ret = regexec(®, str, 0, NULL, 0);
if (0 != ret && REG_NOMATCH != ret) {
char err[256];
regerror(ret, ®, err, sizeof(err));
printf("%s\n", err);
abort();
}
regfree(®);
return !ret;
}
#endif
|