This source file includes following definitions.
- platform_init
- process_start
- dowait
- process_wait
- process_output_size
- process_copy_output
- process_read_last_line
- process_get_name
- process_terminate
- process_reap
- process_cleanup
- rewind_cursor
- uv_sleep
#include "runner-unix.h"
#include "runner.h"
#include <stdint.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#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 <pthread.h>
void platform_init(int argc, char **argv) {
const char* tap;
tap = getenv("UV_TAP_OUTPUT");
tap_output = (tap != NULL && atoi(tap) > 0);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
strncpy(executable_path, argv[0], sizeof(executable_path) - 1);
signal(SIGPIPE, SIG_IGN);
}
int process_start(char* name, char* part, process_info_t* p, int is_helper) {
FILE* stdout_file;
const char* arg;
char* args[16];
int n;
stdout_file = tmpfile();
if (!stdout_file) {
perror("tmpfile");
return -1;
}
p->terminated = 0;
p->status = 0;
pid_t pid = fork();
if (pid < 0) {
perror("fork");
return -1;
}
if (pid == 0) {
arg = getenv("UV_USE_VALGRIND");
n = 0;
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;
dup2(fileno(stdout_file), STDOUT_FILENO);
dup2(fileno(stdout_file), STDERR_FILENO);
execvp(args[0], args);
perror("execvp()");
_exit(127);
}
p->pid = pid;
p->name = strdup(name);
p->stdout_file = stdout_file;
return 0;
}
typedef struct {
int pipe[2];
process_info_t* vec;
int n;
} dowait_args;
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) {
ssize_t r;
do
r = write(args->pipe[1], "", 1);
while (r == -1 && errno == EINTR);
}
return NULL;
}
int process_wait(process_info_t* vec, int n, int timeout) {
int i;
process_info_t* p;
dowait_args args;
args.vec = vec;
args.n = n;
args.pipe[0] = -1;
args.pipe[1] = -1;
if (timeout == -1) {
dowait(&args);
return 0;
}
pthread_t tid;
int retval;
int r = pipe((int*)&(args.pipe));
if (r) {
perror("pipe()");
return -1;
}
r = pthread_create(&tid, NULL, dowait, &args);
if (r) {
perror("pthread_create()");
retval = -1;
goto terminate;
}
struct timeval tv;
tv.tv_sec = timeout / 1000;
tv.tv_usec = 0;
fd_set fds;
FD_ZERO(&fds);
FD_SET(args.pipe[0], &fds);
r = select(args.pipe[0] + 1, &fds, NULL, NULL, &tv);
if (r == -1) {
perror("select()");
retval = -1;
} else if (r) {
retval = 0;
} else {
for (i = 0; i < n; i++) {
p = (process_info_t*)(vec + i * sizeof(process_info_t));
kill(p->pid, SIGTERM);
}
retval = -2;
r = pthread_join(tid, NULL);
if (r) {
perror("pthread_join");
retval = -1;
}
}
terminate:
close(args.pipe[0]);
close(args.pipe[1]);
return retval;
}
long int process_output_size(process_info_t *p) {
struct stat buf;
int r = fstat(fileno(p->stdout_file), &buf);
if (r < 0) {
return -1;
}
return (long)buf.st_size;
}
int process_copy_output(process_info_t *p, int fd) {
int r = fseek(p->stdout_file, 0, SEEK_SET);
if (r < 0) {
perror("fseek");
return -1;
}
ssize_t nwritten;
char buf[1024];
while (fgets(buf, sizeof(buf), p->stdout_file) != NULL) {
nwritten = 0;
if (tap_output)
nwritten += write(fd, "#", 1);
nwritten += write(fd, buf, strlen(buf));
if (nwritten < 0) {
perror("write");
return -1;
}
}
if (ferror(p->stdout_file)) {
perror("read");
return -1;
}
return 0;
}
int process_read_last_line(process_info_t *p,
char* buffer,
size_t buffer_len) {
char* ptr;
int r = fseek(p->stdout_file, 0, SEEK_SET);
if (r < 0) {
perror("fseek");
return -1;
}
buffer[0] = '\0';
while (fgets(buffer, buffer_len, p->stdout_file) != NULL) {
for (ptr = buffer; *ptr && *ptr != '\r' && *ptr != '\n'; ptr++);
*ptr = '\0';
}
if (ferror(p->stdout_file)) {
perror("read");
buffer[0] = '\0';
return -1;
}
return 0;
}
char* process_get_name(process_info_t *p) {
return p->name;
}
int process_terminate(process_info_t *p) {
return kill(p->pid, SIGTERM);
}
int process_reap(process_info_t *p) {
if (WIFEXITED(p->status)) {
return WEXITSTATUS(p->status);
} else {
return p->status;
}
}
void process_cleanup(process_info_t *p) {
fclose(p->stdout_file);
free(p->name);
}
void rewind_cursor(void) {
fprintf(stderr, "\033[2K\r");
}
void uv_sleep(int msec) {
usleep(msec * 1000);
}