• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <signal.h>
34 #include <time.h>
35 #include <unistd.h>
36 #include <sys/syscall.h>
37 
38 #ifdef __NR_timer_create
39 
40 int
main(void)41 main(void)
42 {
43 	int tid[4] = {};
44 	struct sigevent sev = {
45 		.sigev_notify = SIGEV_NONE,
46 		.sigev_signo = 0xfacefeed,
47 		.sigev_value.sival_ptr =
48 			(void *) (unsigned long) 0xdeadbeefbadc0ded
49 	};
50 
51 	if (syscall(__NR_timer_create, CLOCK_REALTIME, &sev, &tid[0]))
52 		return 77;
53 	printf("timer_create(CLOCK_REALTIME, {sigev_value={int=%d, ptr=%p}"
54 	       ", sigev_signo=%u, sigev_notify=SIGEV_NONE}"
55 	       ", [%d]) = 0\n",
56 	       sev.sigev_value.sival_int,
57 	       sev.sigev_value.sival_ptr,
58 	       sev.sigev_signo, tid[0]);
59 
60 	sev.sigev_notify = SIGEV_SIGNAL;
61 	sev.sigev_signo = SIGALRM;
62 	if (syscall(__NR_timer_create, CLOCK_MONOTONIC, &sev, &tid[1]))
63 		return 77;
64 	printf("timer_create(CLOCK_MONOTONIC, {sigev_value={int=%d, ptr=%p}"
65 	       ", sigev_signo=SIGALRM, sigev_notify=SIGEV_SIGNAL}"
66 	       ", [%d]) = 0\n",
67 	       sev.sigev_value.sival_int,
68 	       sev.sigev_value.sival_ptr, tid[1]);
69 
70 	sev.sigev_notify = SIGEV_THREAD;
71 	sev.sigev_notify_function =
72 		(void *) (unsigned long) 0xdeadbeefbadc0ded;
73 	sev.sigev_notify_attributes =
74 		(void *) (unsigned long) 0xcafef00dfacefeed;
75 	if (syscall(__NR_timer_create, CLOCK_REALTIME, &sev, &tid[2]))
76 		return 77;
77 	printf("timer_create(CLOCK_REALTIME, {sigev_value={int=%d, ptr=%p}"
78 	       ", sigev_signo=SIGALRM, sigev_notify=SIGEV_THREAD"
79 	       ", sigev_notify_function=%p, sigev_notify_attributes=%p}"
80 	       ", [%d]) = 0\n",
81 	       sev.sigev_value.sival_int,
82 	       sev.sigev_value.sival_ptr,
83 	       sev.sigev_notify_function,
84 	       sev.sigev_notify_attributes,
85 	       tid[2]);
86 
87 #ifndef sigev_notify_thread_id
88 # if defined HAVE_STRUCT_SIGEVENT__SIGEV_UN__PAD
89 #  define sigev_notify_thread_id _sigev_un._pad[0]
90 # elif defined HAVE_STRUCT_SIGEVENT___PAD
91 #  define sigev_notify_thread_id __pad[0]
92 # endif
93 #endif /* !sigev_notify_thread_id */
94 
95 #ifdef sigev_notify_thread_id
96 # ifndef SIGEV_THREAD_ID
97 #  define SIGEV_THREAD_ID 4
98 # endif
99 	sev.sigev_notify = SIGEV_THREAD_ID;
100 	sev.sigev_notify_thread_id = getpid();
101 	if (syscall(__NR_timer_create, CLOCK_MONOTONIC, &sev, &tid[3]))
102 		return 77;
103 	printf("timer_create(CLOCK_MONOTONIC, {sigev_value={int=%d, ptr=%p}"
104 	       ", sigev_signo=SIGALRM, sigev_notify=SIGEV_THREAD_ID"
105 	       ", sigev_notify_thread_id=%d}"
106 	       ", [%d]) = 0\n",
107 	       sev.sigev_value.sival_int,
108 	       sev.sigev_value.sival_ptr,
109 	       sev.sigev_notify_thread_id,
110 	       tid[3]);
111 #endif /* sigev_notify_thread_id */
112 
113 	puts("+++ exited with 0 +++");
114 	return 0;
115 }
116 
117 #else
118 
119 int
main(void)120 main(void)
121 {
122 	return 77;
123 }
124 
125 #endif
126