1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) Wipro Technologies Ltd, 2003. All Rights Reserved.
4 *
5 * Author: Aniruddha Marathe <aniruddha.marathe@wipro.com>
6 *
7 * Ported to new library:
8 * 07/2019 Christian Amann <camann@suse.com>
9 */
10 /*
11 *
12 * Basic test for timer_create(2):
13 *
14 * Creates a timer for each available clock using the
15 * following notification types:
16 * 1) SIGEV_NONE
17 * 2) SIGEV_SIGNAL
18 * 3) SIGEV_THREAD
19 * 4) SIGEV_THREAD_ID
20 * 5) NULL
21 *
22 * This is also regression test for commit:
23 * f18ddc13af98 ("alarmtimer: Use EOPNOTSUPP instead of ENOTSUPP")
24 */
25
26 #include <signal.h>
27 #include <time.h>
28 #include "tst_test.h"
29 #include "tst_safe_macros.h"
30 #include "lapi/common_timers.h"
31
32 static struct notif_type {
33 int sigev_signo;
34 int sigev_notify;
35 char *message;
36 } types[] = {
37 {SIGALRM, SIGEV_NONE, "SIGEV_NONE"},
38 {SIGALRM, SIGEV_SIGNAL, "SIGEV_SIGNAL"},
39 {SIGALRM, SIGEV_THREAD, "SIGEV_THREAD"},
40 {SIGALRM, SIGEV_THREAD_ID, "SIGEV_THREAD_ID"},
41 {0, 0, "NULL"},
42 };
43
run(unsigned int n)44 static void run(unsigned int n)
45 {
46 unsigned int i;
47 struct sigevent evp;
48 struct notif_type *nt = &types[n];
49 kernel_timer_t created_timer_id;
50
51 tst_res(TINFO, "Testing notification type: %s", nt->message);
52
53 memset(&evp, 0, sizeof(evp));
54
55 for (i = 0; i < CLOCKS_DEFINED; ++i) {
56 clock_t clock = clock_list[i];
57
58 evp.sigev_value = (union sigval) 0;
59 evp.sigev_signo = nt->sigev_signo;
60 evp.sigev_notify = nt->sigev_notify;
61
62 if (clock == CLOCK_PROCESS_CPUTIME_ID ||
63 clock == CLOCK_THREAD_CPUTIME_ID) {
64 /* (PROCESS_CPUTIME_ID &
65 * THREAD_CPUTIME_ID)
66 * is not supported on kernel versions
67 * lower than 2.6.12
68 */
69 if (!have_cputime_timers())
70 continue;
71 }
72 if (clock == CLOCK_MONOTONIC_RAW)
73 continue;
74
75 if (nt->sigev_notify == SIGEV_THREAD_ID)
76 evp._sigev_un._tid = getpid();
77
78 TEST(tst_syscall(__NR_timer_create, clock,
79 nt->sigev_notify ? &evp : NULL,
80 &created_timer_id));
81
82 if (TST_RET != 0) {
83 if (possibly_unsupported(clock) &&
84 (TST_ERR == EINVAL || TST_ERR == ENOTSUP)) {
85 tst_res(TCONF | TTERRNO, "%s unsupported",
86 get_clock_str(clock));
87 } else {
88 tst_res(TFAIL | TTERRNO,
89 "Failed to create timer for %s",
90 get_clock_str(clock));
91 }
92 continue;
93 }
94
95 tst_res(TPASS, "Timer successfully created for %s",
96 get_clock_str(clock));
97
98 TEST(tst_syscall(__NR_timer_delete, created_timer_id));
99 if (TST_RET != 0) {
100 tst_res(TFAIL | TTERRNO, "Failed to delete timer %s",
101 get_clock_str(clock));
102 }
103 }
104 }
105
106 static struct tst_test test = {
107 .test = run,
108 .tcnt = ARRAY_SIZE(types),
109 .needs_root = 1,
110 .tags = (const struct tst_tag[]) {
111 {"linux-git", "f18ddc13af98"},
112 {}
113 }
114 };
115