~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/timens/timerfd.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #define _GNU_SOURCE
  3 #include <sched.h>
  4 
  5 #include <sys/timerfd.h>
  6 #include <sys/syscall.h>
  7 #include <sys/types.h>
  8 #include <sys/wait.h>
  9 #include <time.h>
 10 #include <unistd.h>
 11 #include <stdlib.h>
 12 #include <stdio.h>
 13 #include <stdint.h>
 14 
 15 #include "log.h"
 16 #include "timens.h"
 17 
 18 static int tclock_gettime(clock_t clockid, struct timespec *now)
 19 {
 20         if (clockid == CLOCK_BOOTTIME_ALARM)
 21                 clockid = CLOCK_BOOTTIME;
 22         return clock_gettime(clockid, now);
 23 }
 24 
 25 int run_test(int clockid, struct timespec now)
 26 {
 27         struct itimerspec new_value;
 28         long long elapsed;
 29         int fd, i;
 30 
 31         if (check_skip(clockid))
 32                 return 0;
 33 
 34         if (tclock_gettime(clockid, &now))
 35                 return pr_perror("clock_gettime(%d)", clockid);
 36 
 37         for (i = 0; i < 2; i++) {
 38                 int flags = 0;
 39 
 40                 new_value.it_value.tv_sec = 3600;
 41                 new_value.it_value.tv_nsec = 0;
 42                 new_value.it_interval.tv_sec = 1;
 43                 new_value.it_interval.tv_nsec = 0;
 44 
 45                 if (i == 1) {
 46                         new_value.it_value.tv_sec += now.tv_sec;
 47                         new_value.it_value.tv_nsec += now.tv_nsec;
 48                 }
 49 
 50                 fd = timerfd_create(clockid, 0);
 51                 if (fd == -1)
 52                         return pr_perror("timerfd_create(%d)", clockid);
 53 
 54                 if (i == 1)
 55                         flags |= TFD_TIMER_ABSTIME;
 56 
 57                 if (timerfd_settime(fd, flags, &new_value, NULL))
 58                         return pr_perror("timerfd_settime(%d)", clockid);
 59 
 60                 if (timerfd_gettime(fd, &new_value))
 61                         return pr_perror("timerfd_gettime(%d)", clockid);
 62 
 63                 elapsed = new_value.it_value.tv_sec;
 64                 if (llabs(elapsed - 3600) > 60) {
 65                         ksft_test_result_fail("clockid: %d elapsed: %lld\n",
 66                                               clockid, elapsed);
 67                         return 1;
 68                 }
 69 
 70                 close(fd);
 71         }
 72 
 73         ksft_test_result_pass("clockid=%d\n", clockid);
 74 
 75         return 0;
 76 }
 77 
 78 int main(int argc, char *argv[])
 79 {
 80         int ret, status, len, fd;
 81         char buf[4096];
 82         pid_t pid;
 83         struct timespec btime_now, mtime_now;
 84 
 85         nscheck();
 86 
 87         check_supported_timers();
 88 
 89         ksft_set_plan(3);
 90 
 91         clock_gettime(CLOCK_MONOTONIC, &mtime_now);
 92         clock_gettime(CLOCK_BOOTTIME, &btime_now);
 93 
 94         if (unshare_timens())
 95                 return 1;
 96 
 97         len = snprintf(buf, sizeof(buf), "%d %d 0\n%d %d 0",
 98                         CLOCK_MONOTONIC, 70 * 24 * 3600,
 99                         CLOCK_BOOTTIME, 9 * 24 * 3600);
100         fd = open("/proc/self/timens_offsets", O_WRONLY);
101         if (fd < 0)
102                 return pr_perror("/proc/self/timens_offsets");
103 
104         if (write(fd, buf, len) != len)
105                 return pr_perror("/proc/self/timens_offsets");
106 
107         close(fd);
108         mtime_now.tv_sec += 70 * 24 * 3600;
109         btime_now.tv_sec += 9 * 24 * 3600;
110 
111         pid = fork();
112         if (pid < 0)
113                 return pr_perror("Unable to fork");
114         if (pid == 0) {
115                 ret = 0;
116                 ret |= run_test(CLOCK_BOOTTIME, btime_now);
117                 ret |= run_test(CLOCK_MONOTONIC, mtime_now);
118                 ret |= run_test(CLOCK_BOOTTIME_ALARM, btime_now);
119 
120                 if (ret)
121                         ksft_exit_fail();
122                 ksft_exit_pass();
123                 return ret;
124         }
125 
126         if (waitpid(pid, &status, 0) != pid)
127                 return pr_perror("Unable to wait the child process");
128 
129         if (WIFEXITED(status))
130                 return WEXITSTATUS(status);
131 
132         return 1;
133 }
134 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php