]>
git.ipfire.org Git - thirdparty/glibc.git/blob - time/tst-itimer.c
1 /* Basic tests for getitimer and setitimer.
2 Copyright (C) 2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <array_length.h>
23 #include <support/check.h>
24 #include <support/xsignal.h>
28 static sig_atomic_t cnt
;
31 alrm_handler (int sig
)
40 struct timespec ts
= { .tv_sec
= sec
, .tv_nsec
= 0 };
41 while (nanosleep (&ts
, &ts
) == -1 && errno
== EINTR
)
48 struct itimerval it
, it_old
;
49 const int timers
[] = { ITIMER_REAL
, ITIMER_VIRTUAL
, ITIMER_PROF
};
50 for (int i
= 0; i
< array_length (timers
); i
++)
52 TEST_COMPARE (getitimer (timers
[i
], &it
), 0);
54 /* No timer set, all value should be 0. */
55 TEST_COMPARE (it
.it_interval
.tv_sec
, 0);
56 TEST_COMPARE (it
.it_interval
.tv_usec
, 0);
57 TEST_COMPARE (it
.it_value
.tv_sec
, 0);
58 TEST_COMPARE (it
.it_value
.tv_usec
, 0);
60 it
.it_interval
.tv_sec
= 10;
61 it
.it_interval
.tv_usec
= 20;
62 TEST_COMPARE (setitimer (timers
[i
], &it
, NULL
), 0);
64 TEST_COMPARE (setitimer (timers
[i
], &(struct itimerval
) { 0 }, &it_old
),
66 /* ITIMER_REAL returns { 0, 0 } for single-shot timers, while
67 other timers returns setitimer value. */
68 if (timers
[i
] == ITIMER_REAL
)
70 TEST_COMPARE (it_old
.it_interval
.tv_sec
, 0);
71 TEST_COMPARE (it_old
.it_interval
.tv_usec
, 0);
75 TEST_COMPARE (it_old
.it_interval
.tv_sec
, 10);
76 TEST_COMPARE (it_old
.it_interval
.tv_usec
, 20);
79 /* Create a periodic timer and check if the return value is the one
81 it
.it_interval
.tv_sec
= 10;
82 it
.it_interval
.tv_usec
= 20;
83 it
.it_value
.tv_sec
= 30;
84 it
.it_value
.tv_usec
= 40;
85 TEST_COMPARE (setitimer (timers
[i
], &it
, NULL
), 0);
87 TEST_COMPARE (setitimer (timers
[i
], &(struct itimerval
) { 0 }, &it_old
),
89 TEST_COMPARE (it
.it_interval
.tv_sec
, it_old
.it_interval
.tv_sec
);
90 TEST_COMPARE (it
.it_interval
.tv_usec
, it_old
.it_interval
.tv_usec
);
92 if (sizeof (time_t) == 4)
95 /* Same as before, but with a 64 bit time_t value. */
96 it
.it_interval
.tv_sec
= (time_t) 0x1ffffffffull
;
97 it
.it_interval
.tv_usec
= 20;
98 it
.it_value
.tv_sec
= 0;
99 it
.it_value
.tv_usec
= 0;
101 /* Linux does not provide 64 bit time_t support for getitimer and
102 setitimer on architectures with 32 bit time_t support. */
103 if (sizeof (__time_t
) == 8)
105 TEST_COMPARE (setitimer (timers
[i
], &it
, NULL
), 0);
106 TEST_COMPARE (setitimer (timers
[i
], &(struct itimerval
) { 0 },
109 /* ITIMER_REAL returns { 0, 0 } for single-sort timers, while other
110 timers returns setitimer value. */
111 if (timers
[i
] == ITIMER_REAL
)
113 TEST_COMPARE (it_old
.it_interval
.tv_sec
, 0ull);
114 TEST_COMPARE (it_old
.it_interval
.tv_usec
, 0);
118 TEST_COMPARE (it_old
.it_interval
.tv_sec
, 0x1ffffffffull
);
119 TEST_COMPARE (it_old
.it_interval
.tv_usec
, 20);
124 TEST_COMPARE (setitimer (timers
[i
], &it
, NULL
), -1);
125 TEST_COMPARE (errno
, EOVERFLOW
);
128 /* Create a periodic timer and check if the return value is the one
130 it
.it_interval
.tv_sec
= (time_t) 0x1ffffffffull
;
131 it
.it_interval
.tv_usec
= 20;
132 it
.it_value
.tv_sec
= 30;
133 it
.it_value
.tv_usec
= 40;
134 if (sizeof (__time_t
) == 8)
136 TEST_COMPARE (setitimer (timers
[i
], &it
, NULL
), 0);
138 TEST_COMPARE (setitimer (timers
[i
], &(struct itimerval
) { 0 },
141 TEST_COMPARE (it
.it_interval
.tv_sec
, it_old
.it_interval
.tv_sec
);
142 TEST_COMPARE (it
.it_interval
.tv_usec
, it_old
.it_interval
.tv_usec
);
146 TEST_COMPARE (setitimer (timers
[i
], &it
, NULL
), -1);
147 TEST_COMPARE (errno
, EOVERFLOW
);
152 struct sigaction sa
= { .sa_handler
= alrm_handler
, .sa_flags
= 0 };
153 sigemptyset (&sa
.sa_mask
);
154 xsigaction (SIGALRM
, &sa
, NULL
);
157 /* Setup a timer to 0.1s and sleep for 1s and check to 3 signal handler
159 it
.it_interval
.tv_sec
= 0;
160 it
.it_interval
.tv_usec
= 100000;
161 it
.it_value
.tv_sec
= 0;
162 it
.it_value
.tv_usec
= 100000;
164 /* Check ITIMER_VIRTUAL and ITIMER_PROF would require to generate load
165 and be subject to system load. */
167 TEST_COMPARE (setitimer (ITIMER_REAL
, &it
, NULL
), 0);
169 TEST_COMPARE (cnt
, 3);
170 TEST_COMPARE (setitimer (ITIMER_REAL
, &(struct itimerval
) { 0 }, NULL
), 0);
175 #include <support/test-driver.c>