File Index
Files and directories
Sign in
FILE

timer.c

File preview with syntax highlighting.

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