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 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 
32 #include <stdio.h>
33 #include <stdint.h>
34 #include <signal.h>
35 #include <time.h>
36 #include <unistd.h>
37 #include <sys/time.h>
38 #include <sys/syscall.h>
39 
40 static void
handler(int signo)41 handler(int signo)
42 {
43 }
44 
45 int
main(void)46 main(void)
47 {
48 	struct {
49 		struct timespec ts;
50 		uint32_t pad[2];
51 	} req = {
52 		.ts = { .tv_nsec = 0xc0de1 },
53 		.pad = { 0xdeadbeef, 0xbadc0ded }
54 	}, rem = {
55 		.ts = { .tv_sec = 0xc0de2, .tv_nsec = 0xc0de3 },
56 		.pad = { 0xdeadbeef, 0xbadc0ded }
57 	};
58 	const sigset_t set = {};
59 	const struct sigaction act = { .sa_handler = handler };
60 	const struct itimerval itv = {
61 		.it_interval.tv_usec = 222222,
62 		.it_value.tv_usec = 111111
63 	};
64 
65 	if (syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0, &req.ts, NULL))
66 		return 77;
67 	printf("clock_nanosleep(CLOCK_REALTIME, 0, {%jd, %jd}, NULL) = 0\n",
68 	       (intmax_t) req.ts.tv_sec, (intmax_t) req.ts.tv_nsec);
69 
70 	if (!syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0, NULL, &rem.ts))
71 		return 77;
72 	printf("clock_nanosleep(CLOCK_REALTIME, 0, NULL, %p)"
73 	       " = -1 EFAULT (Bad address)\n", &rem.ts);
74 
75 	if (syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0, &req.ts, &rem.ts))
76 		return 77;
77 	printf("clock_nanosleep(CLOCK_REALTIME, 0, {%jd, %jd}, %p) = 0\n",
78 	       (intmax_t) req.ts.tv_sec, (intmax_t) req.ts.tv_nsec, &rem.ts);
79 
80 	req.ts.tv_nsec = 999999999 + 1;
81 	if (!syscall(__NR_clock_nanosleep, CLOCK_MONOTONIC, 0, &req.ts, &rem.ts))
82 		return 77;
83 	printf("clock_nanosleep(CLOCK_MONOTONIC, 0"
84 	       ", {%jd, %jd}, %p) = -1 EINVAL (Invalid argument)\n",
85 	       (intmax_t) req.ts.tv_sec, (intmax_t) req.ts.tv_nsec, &rem.ts);
86 
87 	if (sigaction(SIGALRM, &act, NULL))
88 		return 77;
89 	if (sigprocmask(SIG_SETMASK, &set, NULL))
90 		return 77;
91 
92 	if (setitimer(ITIMER_REAL, &itv, NULL))
93 		return 77;
94 	printf("setitimer(ITIMER_REAL, {it_interval={%jd, %jd}"
95 	       ", it_value={%jd, %jd}}, NULL) = 0\n",
96 	       (intmax_t) itv.it_interval.tv_sec,
97 	       (intmax_t) itv.it_interval.tv_usec,
98 	       (intmax_t) itv.it_value.tv_sec,
99 	       (intmax_t) itv.it_value.tv_usec);
100 
101 	--req.ts.tv_nsec;
102 	if (!syscall(__NR_clock_nanosleep, CLOCK_REALTIME, 0, &req.ts, &rem.ts))
103 		return 77;
104 	printf("clock_nanosleep(CLOCK_REALTIME, 0, {%jd, %jd}, {%jd, %jd})"
105 	       " = ? ERESTART_RESTARTBLOCK (Interrupted by signal)\n",
106 	       (intmax_t) req.ts.tv_sec, (intmax_t) req.ts.tv_nsec,
107 	       (intmax_t) rem.ts.tv_sec, (intmax_t) rem.ts.tv_nsec);
108 	puts("--- SIGALRM {si_signo=SIGALRM, si_code=SI_KERNEL} ---");
109 
110 	if (syscall(__NR_clock_gettime, CLOCK_REALTIME, &req.ts))
111 		return 77;
112 	printf("clock_gettime(CLOCK_REALTIME, {%jd, %jd}) = 0\n",
113 	       (intmax_t) req.ts.tv_sec, (intmax_t) req.ts.tv_nsec);
114 
115 	++req.ts.tv_sec;
116 	rem.ts.tv_sec = 0xc0de4;
117 	rem.ts.tv_nsec = 0xc0de5;
118 	if (!syscall(__NR_clock_nanosleep, CLOCK_REALTIME, TIMER_ABSTIME,
119 		     &req.ts, &rem.ts))
120 		return 77;
121 	printf("clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, {%jd, %jd}, %p)"
122 	       " = ? ERESTARTNOHAND (To be restarted if no handler)\n",
123 	       (intmax_t) req.ts.tv_sec, (intmax_t) req.ts.tv_nsec, &rem.ts);
124 	puts("--- SIGALRM {si_signo=SIGALRM, si_code=SI_KERNEL} ---");
125 
126 	puts("+++ exited with 0 +++");
127 	return 0;
128 }
129