nis: Fix leak on realloc failure in nis_getnames [BZ #28150]
[glibc.git] / time / tst-itimer.c
blob929c2b74c7027b3d0919a7c8f14c47c653b0193a
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>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <sys/time.h>
23 #include <support/check.h>
24 #include <support/xsignal.h>
25 #include <unistd.h>
26 #include <time.h>
28 static sig_atomic_t cnt;
30 static void
31 alrm_handler (int sig)
33 if (++cnt > 3)
34 cnt = 3;
37 static void
38 intr_sleep (int sec)
40 struct timespec ts = { .tv_sec = sec, .tv_nsec = 0 };
41 while (nanosleep (&ts, &ts) == -1 && errno == EINTR)
45 static int
46 do_test (void)
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),
65 0);
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);
73 else
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
80 previously set. */
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),
88 0);
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)
93 continue;
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 },
107 &it_old),
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);
116 else
118 TEST_COMPARE (it_old.it_interval.tv_sec, 0x1ffffffffull);
119 TEST_COMPARE (it_old.it_interval.tv_usec, 20);
122 else
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
129 previously set. */
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 },
139 &it_old),
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);
144 else
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
158 execution. */
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. */
166 cnt = 0;
167 TEST_COMPARE (setitimer (ITIMER_REAL, &it, NULL), 0);
168 intr_sleep (1);
169 TEST_COMPARE (cnt, 3);
170 TEST_COMPARE (setitimer (ITIMER_REAL, &(struct itimerval) { 0 }, NULL), 0);
172 return 0;
175 #include <support/test-driver.c>