[01579ad] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @file
|
---|
| 30 | *
|
---|
| 31 | * Unix-specific functions for test execution via the fork() system call.
|
---|
| 32 | */
|
---|
| 33 |
|
---|
[9b20126] | 34 | /** We need _POSIX_SOURCE because of kill(). */
|
---|
[134ac5d] | 35 | #define _POSIX_SOURCE
|
---|
[9b20126] | 36 | /** We need _BSD_SOURCE because of snprintf() when compiling under C89. */
|
---|
| 37 | #define _BSD_SOURCE
|
---|
[9eb1ff5] | 38 |
|
---|
| 39 | /** Newer versions of features.h needs _DEFAULT_SOURCE. */
|
---|
| 40 | #define _DEFAULT_SOURCE
|
---|
| 41 |
|
---|
[01579ad] | 42 | #include <stdlib.h>
|
---|
| 43 | #include <unistd.h>
|
---|
[9eb1ff5] | 44 | #include <sys/types.h>
|
---|
[134ac5d] | 45 | #include <signal.h>
|
---|
[01579ad] | 46 | #include <errno.h>
|
---|
| 47 | #include <assert.h>
|
---|
| 48 | #include <sys/wait.h>
|
---|
| 49 | #include <stdio.h>
|
---|
| 50 | #include <string.h>
|
---|
| 51 | #include "../internal.h"
|
---|
| 52 |
|
---|
| 53 | /** Maximum size of stdout we are able to capture. */
|
---|
| 54 | #define OUTPUT_BUFFER_SIZE 8192
|
---|
| 55 |
|
---|
| 56 | /** Buffer for assertion and other error messages. */
|
---|
| 57 | static char error_message_buffer[OUTPUT_BUFFER_SIZE];
|
---|
| 58 |
|
---|
| 59 | /** Buffer for stdout from the test. */
|
---|
| 60 | static char extra_output_buffer[OUTPUT_BUFFER_SIZE];
|
---|
| 61 |
|
---|
[134ac5d] | 62 | /** Prepare for a new test.
|
---|
| 63 | *
|
---|
| 64 | * @param test Test that is about to be run.
|
---|
| 65 | */
|
---|
[4b54bd9] | 66 | static void before_test_start(pcut_item_t *test) {
|
---|
[01579ad] | 67 | pcut_report_test_start(test);
|
---|
| 68 |
|
---|
| 69 | memset(error_message_buffer, 0, OUTPUT_BUFFER_SIZE);
|
---|
| 70 | memset(extra_output_buffer, 0, OUTPUT_BUFFER_SIZE);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[134ac5d] | 73 | /** PID of the forked process running the actual test. */
|
---|
| 74 | static pid_t child_pid;
|
---|
| 75 |
|
---|
| 76 | /** Signal handler that kills the child.
|
---|
| 77 | *
|
---|
| 78 | * @param sig Signal number.
|
---|
| 79 | */
|
---|
[4b54bd9] | 80 | static void kill_child_on_alarm(int sig) {
|
---|
[134ac5d] | 81 | PCUT_UNUSED(sig);
|
---|
| 82 | kill(child_pid, SIGKILL);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[01579ad] | 85 | /** Read full buffer from given file descriptor.
|
---|
| 86 | *
|
---|
| 87 | * This function exists to overcome the possibility that read() may
|
---|
| 88 | * not fill the full length of the provided buffer even when EOF is
|
---|
| 89 | * not reached.
|
---|
| 90 | *
|
---|
| 91 | * @param fd Opened file descriptor.
|
---|
| 92 | * @param buffer Buffer to store data into.
|
---|
| 93 | * @param buffer_size Size of the @p buffer in bytes.
|
---|
| 94 | * @return Number of actually read bytes.
|
---|
| 95 | */
|
---|
[4b54bd9] | 96 | static size_t read_all(int fd, char *buffer, size_t buffer_size) {
|
---|
[01579ad] | 97 | ssize_t actually_read;
|
---|
| 98 | char *buffer_start = buffer;
|
---|
| 99 | do {
|
---|
| 100 | actually_read = read(fd, buffer, buffer_size);
|
---|
| 101 | if (actually_read > 0) {
|
---|
| 102 | buffer += actually_read;
|
---|
| 103 | buffer_size -= actually_read;
|
---|
| 104 | if (buffer_size == 0) {
|
---|
| 105 | break;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | } while (actually_read > 0);
|
---|
| 109 | if (buffer_start != buffer) {
|
---|
| 110 | if (*(buffer - 1) == 10) {
|
---|
| 111 | *(buffer - 1) = 0;
|
---|
| 112 | buffer--;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | return buffer - buffer_start;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | /** Convert program exit code to test outcome.
|
---|
| 119 | *
|
---|
| 120 | * @param status Status value from the wait() function.
|
---|
| 121 | * @return Test outcome code.
|
---|
| 122 | */
|
---|
[4b54bd9] | 123 | static int convert_wait_status_to_outcome(int status) {
|
---|
[01579ad] | 124 | if (WIFEXITED(status)) {
|
---|
| 125 | if (WEXITSTATUS(status) != 0) {
|
---|
[9eb1ff5] | 126 | return PCUT_OUTCOME_FAIL;
|
---|
[01579ad] | 127 | } else {
|
---|
[9eb1ff5] | 128 | return PCUT_OUTCOME_PASS;
|
---|
[01579ad] | 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | if (WIFSIGNALED(status)) {
|
---|
[9eb1ff5] | 133 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
[01579ad] | 134 | }
|
---|
| 135 |
|
---|
| 136 | return status;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | /** Run the test in a forked environment and report the result.
|
---|
| 140 | *
|
---|
| 141 | * @param self_path Ignored.
|
---|
| 142 | * @param test Test to be run.
|
---|
| 143 | */
|
---|
[4b54bd9] | 144 | int pcut_run_test_forking(const char *self_path, pcut_item_t *test) {
|
---|
[9b20126] | 145 | int link_stdout[2], link_stderr[2];
|
---|
[9eb1ff5] | 146 | int rc, status, outcome;
|
---|
[9b20126] | 147 | size_t stderr_size;
|
---|
| 148 |
|
---|
[01579ad] | 149 | PCUT_UNUSED(self_path);
|
---|
| 150 |
|
---|
| 151 | before_test_start(test);
|
---|
| 152 |
|
---|
| 153 |
|
---|
[9b20126] | 154 | rc = pipe(link_stdout);
|
---|
[01579ad] | 155 | if (rc == -1) {
|
---|
[4b54bd9] | 156 | pcut_snprintf(error_message_buffer, OUTPUT_BUFFER_SIZE - 1,
|
---|
| 157 | "pipe() failed: %s.", strerror(rc));
|
---|
[9eb1ff5] | 158 | pcut_report_test_done(test, PCUT_OUTCOME_INTERNAL_ERROR, error_message_buffer, NULL, NULL);
|
---|
| 159 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
[01579ad] | 160 | }
|
---|
| 161 | rc = pipe(link_stderr);
|
---|
| 162 | if (rc == -1) {
|
---|
[4b54bd9] | 163 | pcut_snprintf(error_message_buffer, OUTPUT_BUFFER_SIZE - 1,
|
---|
| 164 | "pipe() failed: %s.", strerror(rc));
|
---|
[9eb1ff5] | 165 | pcut_report_test_done(test, PCUT_OUTCOME_INTERNAL_ERROR, error_message_buffer, NULL, NULL);
|
---|
| 166 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
[01579ad] | 167 | }
|
---|
| 168 |
|
---|
[134ac5d] | 169 | child_pid = fork();
|
---|
| 170 | if (child_pid == (pid_t)-1) {
|
---|
[4b54bd9] | 171 | pcut_snprintf(error_message_buffer, OUTPUT_BUFFER_SIZE - 1,
|
---|
| 172 | "fork() failed: %s.", strerror(rc));
|
---|
[9eb1ff5] | 173 | outcome = PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
[01579ad] | 174 | goto leave_close_pipes;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[134ac5d] | 177 | if (child_pid == 0) {
|
---|
[01579ad] | 178 | /* We are the child. */
|
---|
| 179 | dup2(link_stdout[1], STDOUT_FILENO);
|
---|
| 180 | close(link_stdout[0]);
|
---|
| 181 | dup2(link_stderr[1], STDERR_FILENO);
|
---|
| 182 | close(link_stderr[0]);
|
---|
| 183 |
|
---|
[9eb1ff5] | 184 | outcome = pcut_run_test_forked(test);
|
---|
[01579ad] | 185 |
|
---|
[9eb1ff5] | 186 | exit(outcome);
|
---|
[01579ad] | 187 | }
|
---|
| 188 |
|
---|
| 189 | close(link_stdout[1]);
|
---|
| 190 | close(link_stderr[1]);
|
---|
| 191 |
|
---|
[134ac5d] | 192 | signal(SIGALRM, kill_child_on_alarm);
|
---|
| 193 | alarm(pcut_get_test_timeout(test));
|
---|
| 194 |
|
---|
[9b20126] | 195 | stderr_size = read_all(link_stderr[0], extra_output_buffer, OUTPUT_BUFFER_SIZE - 1);
|
---|
[01579ad] | 196 | read_all(link_stdout[0], extra_output_buffer, OUTPUT_BUFFER_SIZE - 1 - stderr_size);
|
---|
| 197 |
|
---|
| 198 | wait(&status);
|
---|
[134ac5d] | 199 | alarm(0);
|
---|
[01579ad] | 200 |
|
---|
[9eb1ff5] | 201 | outcome = convert_wait_status_to_outcome(status);
|
---|
[01579ad] | 202 |
|
---|
| 203 | goto leave_close_parent_pipe;
|
---|
| 204 |
|
---|
| 205 | leave_close_pipes:
|
---|
| 206 | close(link_stdout[1]);
|
---|
| 207 | close(link_stderr[1]);
|
---|
| 208 | leave_close_parent_pipe:
|
---|
| 209 | close(link_stdout[0]);
|
---|
| 210 | close(link_stderr[0]);
|
---|
| 211 |
|
---|
[9eb1ff5] | 212 | pcut_report_test_done_unparsed(test, outcome, extra_output_buffer, OUTPUT_BUFFER_SIZE);
|
---|
| 213 |
|
---|
| 214 | return outcome;
|
---|
[01579ad] | 215 | }
|
---|
[9b20126] | 216 |
|
---|
[4b54bd9] | 217 | void pcut_hook_before_test(pcut_item_t *test) {
|
---|
[9b20126] | 218 | PCUT_UNUSED(test);
|
---|
| 219 |
|
---|
| 220 | /* Do nothing. */
|
---|
| 221 | }
|
---|
| 222 |
|
---|