Consistently include Makeconfig after defining subdir.
[glibc.git] / rt / tst-cputimer3.c
blob056766a3774ce36a3a7029f200bde76bb96acb72
1 /* Tests for POSIX timer implementation using another process's CPU clock. */
3 #include <unistd.h>
5 #if _POSIX_THREADS && defined _POSIX_CPUTIME
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <time.h>
13 #include <signal.h>
14 #include <sys/wait.h>
16 static clockid_t child_clock;
18 #define TEST_CLOCK child_clock
19 #define TEST_CLOCK_MISSING(clock) \
20 (setup_test () ? "other-process CPU clock timer support" : NULL)
22 /* This function is intended to rack up both user and system time. */
23 static void
24 chew_cpu (void)
26 while (1)
28 static volatile char buf[4096];
29 for (int i = 0; i < 100; ++i)
30 for (size_t j = 0; j < sizeof buf; ++j)
31 buf[j] = 0xaa;
32 int nullfd = open ("/dev/null", O_WRONLY);
33 for (int i = 0; i < 100; ++i)
34 for (size_t j = 0; j < sizeof buf; ++j)
35 buf[j] = 0xbb;
36 write (nullfd, (char *) buf, sizeof buf);
37 close (nullfd);
38 if (getppid () == 1)
39 _exit (2);
43 static pid_t child;
44 static void
45 cleanup_child (void)
47 if (child <= 0)
48 return;
49 if (kill (child, SIGKILL) < 0 && errno != ESRCH)
50 printf ("cannot kill child %d: %m\n", child);
51 else
53 int status;
54 errno = 0;
55 if (waitpid (child, &status, 0) != child)
56 printf ("waitpid %d: %m\n", child);
59 #define CLEANUP_HANDLER cleanup_child ()
61 static int
62 setup_test (void)
64 /* Test timers on a process CPU clock by having a child process eating
65 CPU. First make sure we can make such timers at all. */
67 int pipefd[2];
68 if (pipe (pipefd) < 0)
70 printf ("pipe: %m\n");
71 exit (1);
74 child = fork ();
76 if (child == 0)
78 char c;
79 close (pipefd[1]);
80 if (read (pipefd[0], &c, 1) == 1)
81 chew_cpu ();
82 _exit (1);
85 if (child < 0)
87 printf ("fork: %m\n");
88 exit (1);
91 atexit (&cleanup_child);
93 close (pipefd[0]);
95 int e = clock_getcpuclockid (child, &child_clock);
96 if (e == EPERM)
98 puts ("clock_getcpuclockid does not support other processes");
99 return 1;
101 if (e != 0)
103 printf ("clock_getcpuclockid: %s\n", strerror (e));
104 exit (1);
107 timer_t t;
108 if (timer_create (TEST_CLOCK, NULL, &t) != 0)
110 printf ("timer_create: %m\n");
111 return 1;
113 timer_delete (t);
115 /* Get the child started chewing. */
116 if (write (pipefd[1], "x", 1) != 1)
118 printf ("write to pipe: %m\n");
119 return 1;
121 close (pipefd[1]);
123 return 0;
126 #else
127 # define TEST_CLOCK_MISSING(clock) "process clocks"
128 #endif
130 #include "tst-timer4.c"