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.
4 
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.
9 
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.
14 
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/>.  */
18 
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>
28 
29 static void
timerfd_test(void)30 timerfd_test (void)
31 {
32   struct itimerspec settings = { { 0, 0 }, { 2, 0 } };
33   struct itimerspec val;
34   int fd, ret;
35 
36   fd = timerfd_create (CLOCK_REALTIME, 0);
37   if (fd < 0)
38     FAIL_EXIT1 ("*** timerfd_create failed: %m");
39 
40   /* Set the timer.  */
41   ret = timerfd_settime (fd, 0, &settings, NULL);
42   if (ret != 0)
43     FAIL_EXIT1 ("*** timerfd_settime failed: %m\n");
44 
45   /* Sleep for 1 second.  */
46   ret = usleep (1000000);
47   if (ret != 0)
48     FAIL_EXIT1 ("*** usleep failed: %m\n");
49 
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");
54 
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);
58 
59   xclose (fd);
60 }
61 
62 static void
timerfd_large_timeout(void)63 timerfd_large_timeout (void)
64 {
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)
71     {
72       uint64_t buf;
73       TEST_COMPARE (read (fd, &buf, sizeof (buf)), -1);
74       TEST_VERIFY (errno == EINTR);
75     }
76   else
77     TEST_COMPARE (errno, EOVERFLOW);
78 }
79 
80 static int
do_test(void)81 do_test (void)
82 {
83   timerfd_test ();
84   timerfd_large_timeout ();
85   return 0;
86 }
87 
88 #include <support/test-driver.c>
89