1 /*
2  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2015-2017 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "tests.h"
30 #include <fcntl.h>
31 #include <asm/unistd.h>
32 
33 #if defined __NR_timerfd_create \
34  && defined __NR_timerfd_gettime \
35  && defined __NR_timerfd_settime \
36  && defined O_CLOEXEC
37 
38 # include <stdio.h>
39 # include <stdint.h>
40 # include <time.h>
41 # include <unistd.h>
42 
43 int
main(void)44 main(void)
45 {
46 	(void) close(0);
47 	if (syscall(__NR_timerfd_create, CLOCK_MONOTONIC, O_CLOEXEC | O_NONBLOCK))
48 		perror_msg_and_skip("timerfd_create");
49 	puts("timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC|TFD_NONBLOCK) = 0");
50 
51 	TAIL_ALLOC_OBJECT_CONST_PTR(struct itimerspec, its_new);
52 	TAIL_ALLOC_OBJECT_CONST_PTR(struct itimerspec, its_old);
53 
54 	its_new->it_interval.tv_sec = 0xdeadbeefU;
55 	its_new->it_interval.tv_nsec = 0xfacefeedU;
56 	its_new->it_value.tv_sec = (time_t) 0xcafef00ddeadbeefLL;
57 	its_new->it_value.tv_nsec = (long) 0xbadc0dedfacefeedLL;
58 
59 	long rc = syscall(__NR_timerfd_settime, 0, 0, its_new, its_old);
60 	printf("timerfd_settime(0, 0"
61 	       ", {it_interval={tv_sec=%lld, tv_nsec=%llu}"
62 	       ", it_value={tv_sec=%lld, tv_nsec=%llu}}, %p) = %s\n",
63 	       (long long) its_new->it_interval.tv_sec,
64 	       zero_extend_signed_to_ull(its_new->it_interval.tv_nsec),
65 	       (long long) its_new->it_value.tv_sec,
66 	       zero_extend_signed_to_ull(its_new->it_value.tv_nsec),
67 	       its_old, sprintrc(rc));
68 
69 	its_new->it_interval.tv_sec = 0xdeface1;
70 	its_new->it_interval.tv_nsec = 0xdeface2;
71 	its_new->it_value.tv_sec = 0xdeface3;
72 	its_new->it_value.tv_nsec = 0xdeface4;
73 	its_old->it_interval.tv_sec = 0xdeface5;
74 	its_old->it_interval.tv_nsec = 0xdeface6;
75 	its_old->it_value.tv_sec = 0xdeface7;
76 	its_old->it_value.tv_nsec = 0xdeface8;
77 
78 	if (syscall(__NR_timerfd_settime, 0, 0, its_new, its_old))
79 		perror_msg_and_skip("timerfd_settime");
80 	printf("timerfd_settime(0, 0"
81 	       ", {it_interval={tv_sec=%lld, tv_nsec=%llu}"
82 	       ", it_value={tv_sec=%lld, tv_nsec=%llu}}"
83 	       ", {it_interval={tv_sec=%lld, tv_nsec=%llu}"
84 	       ", it_value={tv_sec=%lld, tv_nsec=%llu}}"
85 	       ") = 0\n",
86 	       (long long) its_new->it_interval.tv_sec,
87 	       zero_extend_signed_to_ull(its_new->it_interval.tv_nsec),
88 	       (long long) its_new->it_value.tv_sec,
89 	       zero_extend_signed_to_ull(its_new->it_value.tv_nsec),
90 	       (long long) its_old->it_interval.tv_sec,
91 	       zero_extend_signed_to_ull(its_old->it_interval.tv_nsec),
92 	       (long long) its_old->it_value.tv_sec,
93 	       zero_extend_signed_to_ull(its_old->it_value.tv_nsec));
94 
95 	if (syscall(__NR_timerfd_gettime, 0, its_old))
96 		perror_msg_and_skip("timerfd_gettime");
97 	printf("timerfd_gettime(0"
98 	       ", {it_interval={tv_sec=%lld, tv_nsec=%llu}"
99 	       ", it_value={tv_sec=%lld, tv_nsec=%llu}}) = 0\n",
100 	       (long long) its_old->it_interval.tv_sec,
101 	       zero_extend_signed_to_ull(its_old->it_interval.tv_nsec),
102 	       (long long) its_old->it_value.tv_sec,
103 	       zero_extend_signed_to_ull(its_old->it_value.tv_nsec));
104 
105 	puts("+++ exited with 0 +++");
106 	return 0;
107 }
108 
109 #else
110 
111 SKIP_MAIN_UNDEFINED("__NR_timerfd_create && __NR_timerfd_gettime"
112 		    " && __NR_timerfd_settime && O_CLOEXEC")
113 
114 #endif
115