Revert "tty: hvc: Fix data abort due to race in hvc_open"
[linux/fpc-iii.git] / tools / testing / selftests / timens / clock_nanosleep.c
blob72d41b955fb2263ae74c34b446fb322d1bd50c6a
1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 #include <sched.h>
5 #include <sys/timerfd.h>
6 #include <sys/syscall.h>
7 #include <time.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <pthread.h>
13 #include <signal.h>
14 #include <string.h>
16 #include "log.h"
17 #include "timens.h"
19 void test_sig(int sig)
21 if (sig == SIGUSR2)
22 pthread_exit(NULL);
25 struct thread_args {
26 struct timespec *now, *rem;
27 pthread_mutex_t *lock;
28 int clockid;
29 int abs;
32 void *call_nanosleep(void *_args)
34 struct thread_args *args = _args;
36 clock_nanosleep(args->clockid, args->abs ? TIMER_ABSTIME : 0, args->now, args->rem);
37 pthread_mutex_unlock(args->lock);
38 return NULL;
41 int run_test(int clockid, int abs)
43 struct timespec now = {}, rem;
44 struct thread_args args = { .now = &now, .rem = &rem, .clockid = clockid};
45 struct timespec start;
46 pthread_mutex_t lock;
47 pthread_t thread;
48 int j, ok, ret;
50 signal(SIGUSR1, test_sig);
51 signal(SIGUSR2, test_sig);
53 pthread_mutex_init(&lock, NULL);
54 pthread_mutex_lock(&lock);
56 if (clock_gettime(clockid, &start) == -1) {
57 if (errno == EINVAL && check_skip(clockid))
58 return 0;
59 return pr_perror("clock_gettime");
63 if (abs) {
64 now.tv_sec = start.tv_sec;
65 now.tv_nsec = start.tv_nsec;
68 now.tv_sec += 3600;
69 args.abs = abs;
70 args.lock = &lock;
71 ret = pthread_create(&thread, NULL, call_nanosleep, &args);
72 if (ret != 0) {
73 pr_err("Unable to create a thread: %s", strerror(ret));
74 return 1;
77 /* Wait when the thread will call clock_nanosleep(). */
78 ok = 0;
79 for (j = 0; j < 8; j++) {
80 /* The maximum timeout is about 5 seconds. */
81 usleep(10000 << j);
83 /* Try to interrupt clock_nanosleep(). */
84 pthread_kill(thread, SIGUSR1);
86 usleep(10000 << j);
87 /* Check whether clock_nanosleep() has been interrupted or not. */
88 if (pthread_mutex_trylock(&lock) == 0) {
89 /**/
90 ok = 1;
91 break;
94 if (!ok)
95 pthread_kill(thread, SIGUSR2);
96 pthread_join(thread, NULL);
97 pthread_mutex_destroy(&lock);
99 if (!ok) {
100 ksft_test_result_pass("clockid: %d abs:%d timeout\n", clockid, abs);
101 return 1;
104 if (rem.tv_sec < 3300 || rem.tv_sec > 3900) {
105 pr_fail("clockid: %d abs: %d remain: %ld\n",
106 clockid, abs, rem.tv_sec);
107 return 1;
109 ksft_test_result_pass("clockid: %d abs:%d\n", clockid, abs);
111 return 0;
114 int main(int argc, char *argv[])
116 int ret, nsfd;
118 nscheck();
120 ksft_set_plan(4);
122 check_supported_timers();
124 if (unshare_timens())
125 return 1;
127 if (_settime(CLOCK_MONOTONIC, 7 * 24 * 3600))
128 return 1;
129 if (_settime(CLOCK_BOOTTIME, 9 * 24 * 3600))
130 return 1;
132 nsfd = open("/proc/self/ns/time_for_children", O_RDONLY);
133 if (nsfd < 0)
134 return pr_perror("Unable to open timens_for_children");
136 if (setns(nsfd, CLONE_NEWTIME))
137 return pr_perror("Unable to set timens");
139 ret = 0;
140 ret |= run_test(CLOCK_MONOTONIC, 0);
141 ret |= run_test(CLOCK_MONOTONIC, 1);
142 ret |= run_test(CLOCK_BOOTTIME_ALARM, 0);
143 ret |= run_test(CLOCK_BOOTTIME_ALARM, 1);
145 if (ret)
146 ksft_exit_fail();
147 ksft_exit_pass();
148 return ret;