Alien-uv
view release on metacpan or search on metacpan
libuv/test/runner-unix.c view on Meta::CPAN
* 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 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.
*/
#include "runner-unix.h"
#include "runner.h"
#include <limits.h>
#include <stdint.h> /* uintptr_t */
#include <errno.h>
#include <unistd.h> /* readlink, usleep */
#include <string.h> /* strdup */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <assert.h>
#include <sys/select.h>
#include <sys/time.h>
#include <pthread.h>
extern char** environ;
static void closefd(int fd) {
if (close(fd) == 0 || errno == EINTR || errno == EINPROGRESS)
return;
perror("close");
abort();
}
void notify_parent_process(void) {
char* arg;
int fd;
arg = getenv("UV_TEST_RUNNER_FD");
if (arg == NULL)
return;
fd = atoi(arg);
assert(fd > STDERR_FILENO);
unsetenv("UV_TEST_RUNNER_FD");
closefd(fd);
}
/* Do platform-specific initialization. */
int platform_init(int argc, char **argv) {
/* Disable stdio output buffering. */
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
signal(SIGPIPE, SIG_IGN);
if (realpath(argv[0], executable_path) == NULL) {
perror("realpath");
return -1;
}
return 0;
}
/* Invoke "argv[0] test-name [test-part]". Store process info in *p. Make sure
* that all stdio output of the processes is buffered up. */
int process_start(char* name, char* part, process_info_t* p, int is_helper) {
FILE* stdout_file;
int stdout_fd;
const char* arg;
char* args[16];
int pipefd[2];
char fdstr[8];
ssize_t rc;
int n;
pid_t pid;
arg = getenv("UV_USE_VALGRIND");
n = 0;
/* Disable valgrind for helpers, it complains about helpers leaking memory.
* They're killed after the test and as such never get a chance to clean up.
*/
if (is_helper == 0 && arg != NULL && atoi(arg) != 0) {
args[n++] = "valgrind";
args[n++] = "--quiet";
args[n++] = "--leak-check=full";
args[n++] = "--show-reachable=yes";
args[n++] = "--error-exitcode=125";
}
args[n++] = executable_path;
args[n++] = name;
args[n++] = part;
args[n++] = NULL;
stdout_file = tmpfile();
stdout_fd = fileno(stdout_file);
if (!stdout_file) {
perror("tmpfile");
return -1;
}
if (is_helper) {
if (pipe(pipefd)) {
perror("pipe");
return -1;
}
snprintf(fdstr, sizeof(fdstr), "%d", pipefd[1]);
if (setenv("UV_TEST_RUNNER_FD", fdstr, /* overwrite */ 1)) {
perror("setenv");
return -1;
}
}
p->terminated = 0;
p->status = 0;
pid = fork();
if (pid < 0) {
perror("fork");
return -1;
}
if (pid == 0) {
/* child */
if (is_helper)
closefd(pipefd[0]);
dup2(stdout_fd, STDOUT_FILENO);
dup2(stdout_fd, STDERR_FILENO);
execve(args[0], args, environ);
perror("execve()");
_exit(127);
}
/* parent */
p->pid = pid;
p->name = strdup(name);
p->stdout_file = stdout_file;
if (!is_helper)
return 0;
closefd(pipefd[1]);
unsetenv("UV_TEST_RUNNER_FD");
do
rc = read(pipefd[0], &n, 1);
while (rc == -1 && errno == EINTR);
closefd(pipefd[0]);
if (rc == -1) {
perror("read");
return -1;
}
if (rc > 0) {
fprintf(stderr, "EOF expected but got data.\n");
return -1;
}
return 0;
}
typedef struct {
int pipe[2];
process_info_t* vec;
int n;
} dowait_args;
/* This function is run inside a pthread. We do this so that we can possibly
* timeout.
*/
static void* dowait(void* data) {
dowait_args* args = data;
int i, r;
process_info_t* p;
for (i = 0; i < args->n; i++) {
p = (process_info_t*)(args->vec + i * sizeof(process_info_t));
if (p->terminated) continue;
r = waitpid(p->pid, &p->status, 0);
if (r < 0) {
perror("waitpid");
return NULL;
}
p->terminated = 1;
}
if (args->pipe[1] >= 0) {
/* Write a character to the main thread to notify it about this. */
ssize_t r;
do
r = write(args->pipe[1], "", 1);
while (r == -1 && errno == EINTR);
}
return NULL;
}
( run in 0.342 second using v1.01-cache-2.11-cpan-f0fbb3f571b )