Linux: Consolidate typesizes.h
[glibc.git] / sysdeps / unix / sysv / linux / tst-timerfd.c
blob672acd0f32ca2adfe95b25753aeaf1dd2c459c8e
1 /* Test for timerfd related functions
2 Copyright (C) 2021-2022 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 <errno.h>
20 #include <intprops.h>
21 #include <time.h>
22 #include <support/check.h>
23 #include <support/support.h>
24 #include <support/xunistd.h>
25 #include <support/timespec.h>
26 #include <sys/time.h>
27 #include <sys/timerfd.h>
29 static void
30 timerfd_test (void)
32 struct itimerspec settings = { { 0, 0 }, { 2, 0 } };
33 struct itimerspec val;
34 int fd, ret;
36 fd = timerfd_create (CLOCK_REALTIME, 0);
37 if (fd < 0)
38 FAIL_EXIT1 ("*** timerfd_create failed: %m");
40 /* Set the timer. */
41 ret = timerfd_settime (fd, 0, &settings, NULL);
42 if (ret != 0)
43 FAIL_EXIT1 ("*** timerfd_settime failed: %m\n");
45 /* Sleep for 1 second. */
46 ret = usleep (1000000);
47 if (ret != 0)
48 FAIL_EXIT1 ("*** usleep failed: %m\n");
50 /* Read the timer just after sleep. */
51 ret = timerfd_gettime (fd, &val);
52 if (ret != 0)
53 FAIL_EXIT1 ("*** timerfd_gettime failed: %m\n");
55 /* Check difference between timerfd_gettime calls. */
56 TEST_COMPARE (support_timespec_check_in_range
57 ((struct timespec) { 1, 0 }, val.it_value, 0.9, 1.0), 1);
59 xclose (fd);
62 static void
63 timerfd_large_timeout (void)
65 int fd = timerfd_create (CLOCK_REALTIME, 0);
66 TEST_VERIFY (fd != -1);
67 support_create_timer (0, 100000000, false, NULL);
68 struct itimerspec it = { { 0, 0 }, { TYPE_MAXIMUM (time_t), 0 } };
69 int r = timerfd_settime (fd, 0, &it, NULL);
70 if (r == 0)
72 uint64_t buf;
73 TEST_COMPARE (read (fd, &buf, sizeof (buf)), -1);
74 TEST_VERIFY (errno == EINTR);
76 else
77 TEST_COMPARE (errno, EOVERFLOW);
80 static int
81 do_test (void)
83 timerfd_test ();
84 timerfd_large_timeout ();
85 return 0;
88 #include <support/test-driver.c>