xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/timer_create/timer_create01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) Wipro Technologies Ltd, 2003.  All Rights Reserved.
4  *
5  * Author:	Aniruddha Marathe <[email protected]>
6  *
7  * Ported to new library:
8  * 07/2019	Christian Amann <[email protected]>
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_MONOTONIC_RAW)
63 			continue;
64 
65 		if (nt->sigev_notify == SIGEV_THREAD_ID)
66 			evp._sigev_un._tid = getpid();
67 
68 		TEST(tst_syscall(__NR_timer_create, clock,
69 				nt->sigev_notify ? &evp : NULL,
70 				&created_timer_id));
71 
72 		if (TST_RET != 0) {
73 			if (possibly_unsupported(clock) &&
74 			    (TST_ERR == EINVAL || TST_ERR == ENOTSUP)) {
75 				tst_res(TCONF | TTERRNO, "%s unsupported",
76 					get_clock_str(clock));
77 			} else {
78 				tst_res(TFAIL | TTERRNO,
79 					"Failed to create timer for %s",
80 					get_clock_str(clock));
81 			}
82 			continue;
83 		}
84 
85 		tst_res(TPASS, "Timer successfully created for %s",
86 					get_clock_str(clock));
87 
88 		TEST(tst_syscall(__NR_timer_delete, created_timer_id));
89 		if (TST_RET != 0) {
90 			tst_res(TFAIL | TTERRNO, "Failed to delete timer %s",
91 				get_clock_str(clock));
92 		}
93 	}
94 }
95 
96 static struct tst_test test = {
97 	.test = run,
98 	.tcnt = ARRAY_SIZE(types),
99 	.needs_root = 1,
100 	.tags = (const struct tst_tag[]) {
101 		{"linux-git", "f18ddc13af98"},
102 		{}
103 	}
104 };
105