File Index
Files and directories
Sign in
FILE

timerfd.c

File preview with syntax highlighting.

linux-6.19.14/tools/testing/selftests/timens/timerfd.c C
  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
 18static int tclock_gettime(clockid_t clockid, struct timespec *now)
 19{
 20	if (clockid == CLOCK_BOOTTIME_ALARM)
 21		clockid = CLOCK_BOOTTIME;
 22	return clock_gettime(clockid, now);
 23}
 24
 25static 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
 78int 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	ksft_print_header();
 86
 87	nscheck();
 88
 89	check_supported_timers();
 90
 91	ksft_set_plan(3);
 92
 93	clock_gettime(CLOCK_MONOTONIC, &mtime_now);
 94	clock_gettime(CLOCK_BOOTTIME, &btime_now);
 95
 96	if (unshare_timens())
 97		return 1;
 98
 99	len = snprintf(buf, sizeof(buf), "%d %d 0\n%d %d 0",
100			CLOCK_MONOTONIC, 70 * 24 * 3600,
101			CLOCK_BOOTTIME, 9 * 24 * 3600);
102	fd = open("/proc/self/timens_offsets", O_WRONLY);
103	if (fd < 0)
104		return pr_perror("/proc/self/timens_offsets");
105
106	if (write(fd, buf, len) != len)
107		return pr_perror("/proc/self/timens_offsets");
108
109	close(fd);
110	mtime_now.tv_sec += 70 * 24 * 3600;
111	btime_now.tv_sec += 9 * 24 * 3600;
112
113	pid = fork();
114	if (pid < 0)
115		return pr_perror("Unable to fork");
116	if (pid == 0) {
117		ret = 0;
118		ret |= run_test(CLOCK_BOOTTIME, btime_now);
119		ret |= run_test(CLOCK_MONOTONIC, mtime_now);
120		ret |= run_test(CLOCK_BOOTTIME_ALARM, btime_now);
121
122		if (ret)
123			ksft_exit_fail();
124		ksft_exit_pass();
125		return ret;
126	}
127
128	if (waitpid(pid, &status, 0) != pid)
129		return pr_perror("Unable to wait the child process");
130
131	if (WIFEXITED(status))
132		return WEXITSTATUS(status);
133
134	return 1;
135}
~/ Read-only file preview.