Blame rt/tst-cputimer3.c

Packit 6c4009
/* Tests for POSIX timer implementation using another process's CPU clock.  */
Packit 6c4009
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#if _POSIX_THREADS && defined _POSIX_CPUTIME
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
Packit 6c4009
static clockid_t child_clock;
Packit 6c4009
Packit 6c4009
#define TEST_CLOCK child_clock
Packit 6c4009
#define TEST_CLOCK_MISSING(clock) \
Packit 6c4009
  (setup_test () ? "other-process CPU clock timer support" : NULL)
Packit 6c4009
Packit 6c4009
/* This function is intended to rack up both user and system time.  */
Packit 6c4009
static void
Packit 6c4009
chew_cpu (void)
Packit 6c4009
{
Packit 6c4009
  while (1)
Packit 6c4009
    {
Packit 6c4009
      static volatile char buf[4096];
Packit 6c4009
      for (int i = 0; i < 100; ++i)
Packit 6c4009
	for (size_t j = 0; j < sizeof buf; ++j)
Packit 6c4009
	  buf[j] = 0xaa;
Packit 6c4009
      int nullfd = open ("/dev/null", O_WRONLY);
Packit 6c4009
      for (int i = 0; i < 100; ++i)
Packit 6c4009
	for (size_t j = 0; j < sizeof buf; ++j)
Packit 6c4009
	  buf[j] = 0xbb;
Packit 6c4009
      write (nullfd, (char *) buf, sizeof buf);
Packit 6c4009
      close (nullfd);
Packit 6c4009
      if (getppid () == 1)
Packit 6c4009
	_exit (2);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static pid_t child;
Packit 6c4009
static void
Packit 6c4009
cleanup_child (void)
Packit 6c4009
{
Packit 6c4009
  if (child <= 0)
Packit 6c4009
    return;
Packit 6c4009
  if (kill (child, SIGKILL) < 0 && errno != ESRCH)
Packit 6c4009
    printf ("cannot kill child %d: %m\n", child);
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      int status;
Packit 6c4009
      errno = 0;
Packit 6c4009
      if (waitpid (child, &status, 0) != child)
Packit 6c4009
	printf ("waitpid %d: %m\n", child);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
#define CLEANUP_HANDLER cleanup_child ()
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
setup_test (void)
Packit 6c4009
{
Packit 6c4009
  /* Test timers on a process CPU clock by having a child process eating
Packit 6c4009
     CPU.  First make sure we can make such timers at all.  */
Packit 6c4009
Packit 6c4009
  int pipefd[2];
Packit 6c4009
  if (pipe (pipefd) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("pipe: %m\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  child = fork ();
Packit 6c4009
Packit 6c4009
  if (child == 0)
Packit 6c4009
    {
Packit 6c4009
      char c;
Packit 6c4009
      close (pipefd[1]);
Packit 6c4009
      if (read (pipefd[0], &c, 1) == 1)
Packit 6c4009
	chew_cpu ();
Packit 6c4009
      _exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (child < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("fork: %m\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  atexit (&cleanup_child);
Packit 6c4009
Packit 6c4009
  close (pipefd[0]);
Packit 6c4009
Packit 6c4009
  int e = clock_getcpuclockid (child, &child_clock);
Packit 6c4009
  if (e == EPERM)
Packit 6c4009
    {
Packit 6c4009
      puts ("clock_getcpuclockid does not support other processes");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (e != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("clock_getcpuclockid: %s\n", strerror (e));
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  timer_t t;
Packit 6c4009
  if (timer_create (TEST_CLOCK, NULL, &t) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("timer_create: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  timer_delete (t);
Packit 6c4009
Packit 6c4009
  /* Get the child started chewing.  */
Packit 6c4009
  if (write (pipefd[1], "x", 1) != 1)
Packit 6c4009
    {
Packit 6c4009
      printf ("write to pipe: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  close (pipefd[1]);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#else
Packit 6c4009
# define TEST_CLOCK_MISSING(clock) "process clocks"
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include "tst-timer4.c"