]> granicus.if.org Git - strace/blob - tests/clock_nanosleep.c
tests: include tests.h instead of config.h
[strace] / tests / clock_nanosleep.c
1 /*
2  * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "tests.h"
29 #include <stdio.h>
30 #include <stdint.h>
31 #include <signal.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <sys/time.h>
35 #include <sys/syscall.h>
36
37 static void
38 handler(int signo)
39 {
40 }
41
42 int
43 main(void)
44 {
45         struct {
46                 struct timespec ts;
47                 uint32_t pad[2];
48         } req = {
49                 .ts = { .tv_nsec = 0xc0de1 },
50                 .pad = { 0xdeadbeef, 0xbadc0ded }
51         }, rem = {
52                 .ts = { .tv_sec = 0xc0de2, .tv_nsec = 0xc0de3 },
53                 .pad = { 0xdeadbeef, 0xbadc0ded }
54         };
55         const sigset_t set = {};
56         const struct sigaction act = { .sa_handler = handler };
57         const struct itimerval itv = {
58                 .it_interval.tv_usec = 222222,
59                 .it_value.tv_usec = 111111
60         };
61
62         if (syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0, &req.ts, NULL))
63                 return 77;
64         printf("clock_nanosleep(CLOCK_REALTIME, 0, {%jd, %jd}, NULL) = 0\n",
65                (intmax_t) req.ts.tv_sec, (intmax_t) req.ts.tv_nsec);
66
67         if (!syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0, NULL, &rem.ts))
68                 return 77;
69         printf("clock_nanosleep(CLOCK_REALTIME, 0, NULL, %p)"
70                " = -1 EFAULT (Bad address)\n", &rem.ts);
71
72         if (syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0, &req.ts, &rem.ts))
73                 return 77;
74         printf("clock_nanosleep(CLOCK_REALTIME, 0, {%jd, %jd}, %p) = 0\n",
75                (intmax_t) req.ts.tv_sec, (intmax_t) req.ts.tv_nsec, &rem.ts);
76
77         req.ts.tv_nsec = 999999999 + 1;
78         if (!syscall(__NR_clock_nanosleep, CLOCK_MONOTONIC, 0, &req.ts, &rem.ts))
79                 return 77;
80         printf("clock_nanosleep(CLOCK_MONOTONIC, 0"
81                ", {%jd, %jd}, %p) = -1 EINVAL (Invalid argument)\n",
82                (intmax_t) req.ts.tv_sec, (intmax_t) req.ts.tv_nsec, &rem.ts);
83
84         if (sigaction(SIGALRM, &act, NULL))
85                 return 77;
86         if (sigprocmask(SIG_SETMASK, &set, NULL))
87                 return 77;
88
89         if (setitimer(ITIMER_REAL, &itv, NULL))
90                 return 77;
91         printf("setitimer(ITIMER_REAL, {it_interval={%jd, %jd}"
92                ", it_value={%jd, %jd}}, NULL) = 0\n",
93                (intmax_t) itv.it_interval.tv_sec,
94                (intmax_t) itv.it_interval.tv_usec,
95                (intmax_t) itv.it_value.tv_sec,
96                (intmax_t) itv.it_value.tv_usec);
97
98         --req.ts.tv_nsec;
99         if (!syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0, &req.ts, &rem.ts))
100                 return 77;
101         printf("clock_nanosleep(CLOCK_REALTIME, 0, {%jd, %jd}, {%jd, %jd})"
102                " = ? ERESTART_RESTARTBLOCK (Interrupted by signal)\n",
103                (intmax_t) req.ts.tv_sec, (intmax_t) req.ts.tv_nsec,
104                (intmax_t) rem.ts.tv_sec, (intmax_t) rem.ts.tv_nsec);
105         puts("--- SIGALRM {si_signo=SIGALRM, si_code=SI_KERNEL} ---");
106
107         if (syscall(__NR_clock_gettime, CLOCK_REALTIME, &req.ts))
108                 return 77;
109         printf("clock_gettime(CLOCK_REALTIME, {%jd, %jd}) = 0\n",
110                (intmax_t) req.ts.tv_sec, (intmax_t) req.ts.tv_nsec);
111
112         ++req.ts.tv_sec;
113         rem.ts.tv_sec = 0xc0de4;
114         rem.ts.tv_nsec = 0xc0de5;
115         if (!syscall(__NR_clock_nanosleep, CLOCK_REALTIME, TIMER_ABSTIME,
116                      &req.ts, &rem.ts))
117                 return 77;
118         printf("clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, {%jd, %jd}, %p)"
119                " = ? ERESTARTNOHAND (To be restarted if no handler)\n",
120                (intmax_t) req.ts.tv_sec, (intmax_t) req.ts.tv_nsec, &rem.ts);
121         puts("--- SIGALRM {si_signo=SIGALRM, si_code=SI_KERNEL} ---");
122
123         puts("+++ exited with 0 +++");
124         return 0;
125 }