Alien-uv

 view release on metacpan or  search on metacpan

libuv/test/runner-unix.c  view on Meta::CPAN

/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
 *
 * 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 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;

libuv/test/runner-unix.c  view on Meta::CPAN

    return 0;
  }

  /* Hard case. Do the wait with a timeout.
   *
   * Assumption: we are the only ones making this call right now. Otherwise
   * we'd need to lock vec.
   */

  r = pipe((int*)&(args.pipe));
  if (r) {
    perror("pipe()");
    return -1;
  }

  if (pthread_attr_init(&attr))
    abort();

#if defined(__MVS__)
  if (pthread_attr_setstacksize(&attr, 1024 * 1024))
#else
  if (pthread_attr_setstacksize(&attr, 256 * 1024))
#endif
    abort();

  r = pthread_create(&tid, &attr, dowait, &args);

  if (pthread_attr_destroy(&attr))
    abort();

  if (r) {
    perror("pthread_create()");
    retval = -1;
    goto terminate;
  }

  if (gettimeofday(&timebase, NULL))
    abort();

  tv = timebase;
  for (;;) {
    /* Check that gettimeofday() doesn't jump back in time. */
    assert(tv.tv_sec > timebase.tv_sec ||
           (tv.tv_sec == timebase.tv_sec && tv.tv_usec >= timebase.tv_usec));

    elapsed_ms =
        (tv.tv_sec - timebase.tv_sec) * 1000 +
        (tv.tv_usec / 1000) -
        (timebase.tv_usec / 1000);

    r = 0;  /* Timeout. */
    if (elapsed_ms >= (unsigned) timeout)
      break;

    tv.tv_sec = (timeout - elapsed_ms) / 1000;
    tv.tv_usec = (timeout - elapsed_ms) % 1000 * 1000;

    FD_ZERO(&fds);
    FD_SET(args.pipe[0], &fds);

    r = select(args.pipe[0] + 1, &fds, NULL, NULL, &tv);
    if (!(r == -1 && errno == EINTR))
      break;

    if (gettimeofday(&tv, NULL))
      abort();
  }

  if (r == -1) {
    perror("select()");
    retval = -1;

  } else if (r) {
    /* The thread completed successfully. */
    retval = 0;

  } else {
    /* Timeout. Kill all the children. */
    for (i = 0; i < n; i++) {
      p = (process_info_t*)(vec + i * sizeof(process_info_t));
      kill(p->pid, SIGTERM);
    }
    retval = -2;
  }

  if (pthread_join(tid, NULL))
    abort();

terminate:
  close(args.pipe[0]);
  close(args.pipe[1]);
  return retval;
}


/* Returns the number of bytes in the stdio output buffer for process `p`. */
long int process_output_size(process_info_t *p) {
  /* Size of the p->stdout_file */
  struct stat buf;

  int r = fstat(fileno(p->stdout_file), &buf);
  if (r < 0) {
    return -1;
  }

  return (long)buf.st_size;
}


/* Copy the contents of the stdio output buffer to `fd`. */
int process_copy_output(process_info_t* p, FILE* stream) {
  char buf[1024];
  int r;

  r = fseek(p->stdout_file, 0, SEEK_SET);
  if (r < 0) {
    perror("fseek");
    return -1;
  }

  /* TODO: what if the line is longer than buf */
  while (fgets(buf, sizeof(buf), p->stdout_file) != NULL)
    print_lines(buf, strlen(buf), stream);

  if (ferror(p->stdout_file)) {
    perror("read");
    return -1;
  }

  return 0;



( run in 1.938 second using v1.01-cache-2.11-cpan-385001e3568 )