Files
linux/tools/testing/selftests/timens/timens.h
Bala-Vignesh-Reddy e6fbd1759c selftests: complete kselftest include centralization
This follow-up patch completes centralization of kselftest.h and
ksefltest_harness.h includes in remaining seltests files, replacing all
relative paths with a non-relative paths using shared -I include path in
lib.mk

Tested with gcc-13.3 and clang-18.1, and cross-compiled successfully on
riscv, arm64, x86_64 and powerpc arch.

[reddybalavignesh9979@gmail.com: add selftests include path for kselftest.h]
  Link: https://lkml.kernel.org/r/20251017090201.317521-1-reddybalavignesh9979@gmail.com
Link: https://lkml.kernel.org/r/20251016104409.68985-1-reddybalavignesh9979@gmail.com
Signed-off-by: Bala-Vignesh-Reddy <reddybalavignesh9979@gmail.com>
Suggested-by: Andrew Morton <akpm@linux-foundation.org>
Link: https://lore.kernel.org/lkml/20250820143954.33d95635e504e94df01930d0@linux-foundation.org/
Reviewed-by: Wei Yang <richard.weiyang@gmail.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Günther Noack <gnoack@google.com>
Cc: Jakub Kacinski <kuba@kernel.org>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mickael Salaun <mic@digikod.net>
Cc: Ming Lei <ming.lei@redhat.com>
Cc: Paolo Abeni <pabeni@redhat.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Simon Horman <horms@kernel.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2025-11-27 14:24:31 -08:00

112 lines
2.3 KiB
C

/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __TIMENS_H__
#define __TIMENS_H__
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include "kselftest.h"
#ifndef CLONE_NEWTIME
# define CLONE_NEWTIME 0x00000080
#endif
static int config_posix_timers = true;
static int config_alarm_timers = true;
static inline void check_supported_timers(void)
{
struct timespec ts;
if (timer_create(-1, 0, 0) == -1 && errno == ENOSYS)
config_posix_timers = false;
if (clock_gettime(CLOCK_BOOTTIME_ALARM, &ts) == -1 && errno == EINVAL)
config_alarm_timers = false;
}
static inline bool check_skip(int clockid)
{
if (!config_alarm_timers && clockid == CLOCK_BOOTTIME_ALARM) {
ksft_test_result_skip("CLOCK_BOOTTIME_ALARM isn't supported\n");
return true;
}
if (config_posix_timers)
return false;
switch (clockid) {
/* Only these clocks are supported without CONFIG_POSIX_TIMERS. */
case CLOCK_BOOTTIME:
case CLOCK_MONOTONIC:
case CLOCK_REALTIME:
return false;
default:
ksft_test_result_skip("Posix Clocks & timers are not supported\n");
return true;
}
return false;
}
static inline int unshare_timens(void)
{
if (unshare(CLONE_NEWTIME)) {
if (errno == EPERM)
ksft_exit_skip("need to run as root\n");
return pr_perror("Can't unshare() timens");
}
return 0;
}
static inline int _settime(clockid_t clk_id, time_t offset)
{
int fd, len;
char buf[4096];
if (clk_id == CLOCK_MONOTONIC_COARSE || clk_id == CLOCK_MONOTONIC_RAW)
clk_id = CLOCK_MONOTONIC;
len = snprintf(buf, sizeof(buf), "%d %ld 0", clk_id, offset);
fd = open("/proc/self/timens_offsets", O_WRONLY);
if (fd < 0)
return pr_perror("/proc/self/timens_offsets");
if (write(fd, buf, len) != len)
return pr_perror("/proc/self/timens_offsets");
close(fd);
return 0;
}
static inline int _gettime(clockid_t clk_id, struct timespec *res, bool raw_syscall)
{
int err;
if (!raw_syscall) {
if (clock_gettime(clk_id, res)) {
pr_perror("clock_gettime(%d)", (int)clk_id);
return -1;
}
return 0;
}
err = syscall(SYS_clock_gettime, clk_id, res);
if (err)
pr_perror("syscall(SYS_clock_gettime(%d))", (int)clk_id);
return err;
}
static inline void nscheck(void)
{
if (access("/proc/self/ns/time", F_OK) < 0)
ksft_exit_skip("Time namespaces are not supported\n");
}
#endif