1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Linux Test Project, 2002-2022
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
5*49cdfc7eSAndroid Build Coastguard Worker * 03/2001 - Written by Wayne Boyer
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * Spawn a child, verify that setitimer() syscall passes and it ends up
12*49cdfc7eSAndroid Build Coastguard Worker * counting inside expected boundaries. Then verify from the parent that
13*49cdfc7eSAndroid Build Coastguard Worker * the syscall sent the correct signal to the child.
14*49cdfc7eSAndroid Build Coastguard Worker */
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker #include <time.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_clocks.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include "tst_timer.h"
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker static struct timeval tv;
26*49cdfc7eSAndroid Build Coastguard Worker static struct __kernel_old_itimerval *value, *ovalue;
27*49cdfc7eSAndroid Build Coastguard Worker static volatile unsigned long sigcnt;
28*49cdfc7eSAndroid Build Coastguard Worker static long time_step;
29*49cdfc7eSAndroid Build Coastguard Worker static long time_sec;
30*49cdfc7eSAndroid Build Coastguard Worker static long time_usec;
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
33*49cdfc7eSAndroid Build Coastguard Worker int which;
34*49cdfc7eSAndroid Build Coastguard Worker char *des;
35*49cdfc7eSAndroid Build Coastguard Worker int signo;
36*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
37*49cdfc7eSAndroid Build Coastguard Worker {ITIMER_REAL, "ITIMER_REAL", SIGALRM},
38*49cdfc7eSAndroid Build Coastguard Worker {ITIMER_VIRTUAL, "ITIMER_VIRTUAL", SIGVTALRM},
39*49cdfc7eSAndroid Build Coastguard Worker {ITIMER_PROF, "ITIMER_PROF", SIGPROF},
40*49cdfc7eSAndroid Build Coastguard Worker };
41*49cdfc7eSAndroid Build Coastguard Worker
sig_routine(int signo LTP_ATTRIBUTE_UNUSED)42*49cdfc7eSAndroid Build Coastguard Worker static void sig_routine(int signo LTP_ATTRIBUTE_UNUSED)
43*49cdfc7eSAndroid Build Coastguard Worker {
44*49cdfc7eSAndroid Build Coastguard Worker sigcnt++;
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker
set_setitimer_value(int sec,int usec)47*49cdfc7eSAndroid Build Coastguard Worker static void set_setitimer_value(int sec, int usec)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker value->it_value.tv_sec = sec;
50*49cdfc7eSAndroid Build Coastguard Worker value->it_value.tv_usec = usec;
51*49cdfc7eSAndroid Build Coastguard Worker value->it_interval.tv_sec = sec;
52*49cdfc7eSAndroid Build Coastguard Worker value->it_interval.tv_usec = usec;
53*49cdfc7eSAndroid Build Coastguard Worker }
54*49cdfc7eSAndroid Build Coastguard Worker
verify_setitimer(unsigned int i)55*49cdfc7eSAndroid Build Coastguard Worker static void verify_setitimer(unsigned int i)
56*49cdfc7eSAndroid Build Coastguard Worker {
57*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
58*49cdfc7eSAndroid Build Coastguard Worker int status;
59*49cdfc7eSAndroid Build Coastguard Worker long margin;
60*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[i];
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "tc->which = %s", tc->des);
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker if (tc->which == ITIMER_REAL) {
65*49cdfc7eSAndroid Build Coastguard Worker if (gettimeofday(&tv, NULL) == -1)
66*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "gettimeofday(&tv1, NULL) failed");
67*49cdfc7eSAndroid Build Coastguard Worker else
68*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Test begin time: %ld.%lds", tv.tv_sec, tv.tv_usec);
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker
71*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0) {
74*49cdfc7eSAndroid Build Coastguard Worker tst_no_corefile(0);
75*49cdfc7eSAndroid Build Coastguard Worker
76*49cdfc7eSAndroid Build Coastguard Worker set_setitimer_value(time_sec, time_usec);
77*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_PASS(sys_setitimer(tc->which, value, NULL));
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker set_setitimer_value(5 * time_sec, 7 * time_usec);
80*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_PASS(sys_setitimer(tc->which, value, ovalue));
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_EQ_LI(ovalue->it_interval.tv_sec, time_sec);
83*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_EQ_LI(ovalue->it_interval.tv_usec, time_usec);
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "ovalue->it_value.tv_sec=%ld, ovalue->it_value.tv_usec=%ld",
86*49cdfc7eSAndroid Build Coastguard Worker ovalue->it_value.tv_sec, ovalue->it_value.tv_usec);
87*49cdfc7eSAndroid Build Coastguard Worker
88*49cdfc7eSAndroid Build Coastguard Worker /*
89*49cdfc7eSAndroid Build Coastguard Worker * ITIMER_VIRTUAL and ITIMER_PROF timers always expire a
90*49cdfc7eSAndroid Build Coastguard Worker * time_step afterward the elapsed time to make sure that
91*49cdfc7eSAndroid Build Coastguard Worker * at least counters take effect.
92*49cdfc7eSAndroid Build Coastguard Worker */
93*49cdfc7eSAndroid Build Coastguard Worker margin = tc->which == ITIMER_REAL ? 0 : time_step;
94*49cdfc7eSAndroid Build Coastguard Worker
95*49cdfc7eSAndroid Build Coastguard Worker if (ovalue->it_value.tv_sec == time_sec) {
96*49cdfc7eSAndroid Build Coastguard Worker if (ovalue->it_value.tv_usec < 0 ||
97*49cdfc7eSAndroid Build Coastguard Worker ovalue->it_value.tv_usec > time_usec + margin)
98*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "ovalue->it_value.tv_usec is out of range: %ld",
99*49cdfc7eSAndroid Build Coastguard Worker ovalue->it_value.tv_usec);
100*49cdfc7eSAndroid Build Coastguard Worker } else {
101*49cdfc7eSAndroid Build Coastguard Worker if (ovalue->it_value.tv_sec < 0 ||
102*49cdfc7eSAndroid Build Coastguard Worker ovalue->it_value.tv_sec > time_sec)
103*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "ovalue->it_value.tv_sec is out of range: %ld",
104*49cdfc7eSAndroid Build Coastguard Worker ovalue->it_value.tv_sec);
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker SAFE_SIGNAL(tc->signo, sig_routine);
108*49cdfc7eSAndroid Build Coastguard Worker
109*49cdfc7eSAndroid Build Coastguard Worker set_setitimer_value(0, time_usec);
110*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_PASS(sys_setitimer(tc->which, value, NULL));
111*49cdfc7eSAndroid Build Coastguard Worker
112*49cdfc7eSAndroid Build Coastguard Worker while (sigcnt <= 10UL)
113*49cdfc7eSAndroid Build Coastguard Worker ;
114*49cdfc7eSAndroid Build Coastguard Worker
115*49cdfc7eSAndroid Build Coastguard Worker SAFE_SIGNAL(tc->signo, SIG_DFL);
116*49cdfc7eSAndroid Build Coastguard Worker
117*49cdfc7eSAndroid Build Coastguard Worker while (1)
118*49cdfc7eSAndroid Build Coastguard Worker ;
119*49cdfc7eSAndroid Build Coastguard Worker }
120*49cdfc7eSAndroid Build Coastguard Worker
121*49cdfc7eSAndroid Build Coastguard Worker SAFE_WAITPID(pid, &status, 0);
122*49cdfc7eSAndroid Build Coastguard Worker
123*49cdfc7eSAndroid Build Coastguard Worker if (WIFSIGNALED(status) && WTERMSIG(status) == tc->signo)
124*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Child received signal: %s", tst_strsig(tc->signo));
125*49cdfc7eSAndroid Build Coastguard Worker else
126*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Child: %s", tst_strstatus(status));
127*49cdfc7eSAndroid Build Coastguard Worker
128*49cdfc7eSAndroid Build Coastguard Worker if (tc->which == ITIMER_REAL) {
129*49cdfc7eSAndroid Build Coastguard Worker if (gettimeofday(&tv, NULL) == -1)
130*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "gettimeofday(&tv1, NULL) failed");
131*49cdfc7eSAndroid Build Coastguard Worker else
132*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Test end time: %ld.%lds", tv.tv_sec, tv.tv_usec);
133*49cdfc7eSAndroid Build Coastguard Worker }
134*49cdfc7eSAndroid Build Coastguard Worker }
135*49cdfc7eSAndroid Build Coastguard Worker
setup(void)136*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
137*49cdfc7eSAndroid Build Coastguard Worker {
138*49cdfc7eSAndroid Build Coastguard Worker struct timespec time_res;
139*49cdfc7eSAndroid Build Coastguard Worker
140*49cdfc7eSAndroid Build Coastguard Worker /*
141*49cdfc7eSAndroid Build Coastguard Worker * Use CLOCK_MONOTONIC_COARSE resolution for all timers, since its value is
142*49cdfc7eSAndroid Build Coastguard Worker * bigger than CLOCK_MONOTONIC and therefore can used for both realtime and
143*49cdfc7eSAndroid Build Coastguard Worker * virtual/prof timers resolutions.
144*49cdfc7eSAndroid Build Coastguard Worker */
145*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOCK_GETRES(CLOCK_MONOTONIC_COARSE, &time_res);
146*49cdfc7eSAndroid Build Coastguard Worker
147*49cdfc7eSAndroid Build Coastguard Worker time_step = time_res.tv_nsec / 1000;
148*49cdfc7eSAndroid Build Coastguard Worker if (time_step <= 0)
149*49cdfc7eSAndroid Build Coastguard Worker time_step = 1000;
150*49cdfc7eSAndroid Build Coastguard Worker
151*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "clock low-resolution: %luns, time step: %luus",
152*49cdfc7eSAndroid Build Coastguard Worker time_res.tv_nsec, time_step);
153*49cdfc7eSAndroid Build Coastguard Worker
154*49cdfc7eSAndroid Build Coastguard Worker time_sec = 9 + time_step / 1000;
155*49cdfc7eSAndroid Build Coastguard Worker time_usec = 3 * time_step;
156*49cdfc7eSAndroid Build Coastguard Worker }
157*49cdfc7eSAndroid Build Coastguard Worker
158*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
159*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
160*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
161*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
162*49cdfc7eSAndroid Build Coastguard Worker .test = verify_setitimer,
163*49cdfc7eSAndroid Build Coastguard Worker .bufs = (struct tst_buffers[]) {
164*49cdfc7eSAndroid Build Coastguard Worker {&value, .size = sizeof(struct itimerval)},
165*49cdfc7eSAndroid Build Coastguard Worker {&ovalue, .size = sizeof(struct itimerval)},
166*49cdfc7eSAndroid Build Coastguard Worker {}
167*49cdfc7eSAndroid Build Coastguard Worker }
168*49cdfc7eSAndroid Build Coastguard Worker };
169