1 /*
2 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@strace.io>
3 * Copyright (c) 2015-2023 The strace developers.
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "tests.h"
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <signal.h>
13 #include <time.h>
14 #include <sys/time.h>
15
16 #define NANOSLEEP_NAME_RE "(nanosleep|clock_nanosleep(_time64)?)"
17 #define NANOSLEEP_CALL_RE "(nanosleep\\(|clock_nanosleep(_time64)?\\(CLOCK_REALTIME, 0, )"
18
19 int
20 main(void)
21 {
22 #if defined __x86_64__ && defined __ILP32__
23 /*
24 * x32 is broken from the beginning:
25 * https://lkml.org/lkml/2015/11/30/790
26 */
27 error_msg_and_skip("x32 is broken");
28 #else
29 const sigset_t set = {};
30 const struct sigaction act = { .sa_handler = SIG_IGN };
31 const struct itimerval itv = { .it_interval.tv_usec = 22222,
32 .it_value.tv_usec = 11111 };
33 const struct timespec req = { .tv_nsec = 222222222 };
34 struct timespec rem = { 0xdefaced, 0xdefaced };
35
36 if (sigaction(SIGALRM, &act, NULL))
37 perror_msg_and_fail("sigaction");
38 if (sigprocmask(SIG_SETMASK, &set, NULL))
39 perror_msg_and_fail("sigprocmask");
40 if (setitimer(ITIMER_REAL, &itv, NULL))
41 perror_msg_and_skip("setitimer");
42 if (nanosleep(&req, &rem))
43 perror_msg_and_fail("nanosleep");
44
45 printf("%s\\{tv_sec=0, tv_nsec=[0-9]+\\}"
46 ", \\{tv_sec=[0-9]+, tv_nsec=[0-9]+\\}\\)"
47 " = \\? ERESTART_RESTARTBLOCK \\(Interrupted by signal\\)\n",
48 NANOSLEEP_CALL_RE);
49 puts("--- SIGALRM \\{si_signo=SIGALRM, si_code=SI_KERNEL\\} ---");
50 # ifdef __arm__
51 /* old kernels used to overwrite ARM_r0 with -EINTR */
52 # define ALTERNATIVE_NANOSLEEP_REQ "0xfffffffc|"
53 # else
54 # define ALTERNATIVE_NANOSLEEP_REQ ""
55 # endif
56 printf("(%s(%s\\{tv_sec=0, tv_nsec=[0-9]+\\})"
57 ", 0x[[:xdigit:]]+|restart_syscall\\(<\\.\\.\\."
58 " resuming interrupted %s \\.\\.\\.>)\\) = 0\n",
59 NANOSLEEP_CALL_RE,
60 ALTERNATIVE_NANOSLEEP_REQ,
61 NANOSLEEP_NAME_RE);
62
63 puts("!restart_syscall\\(<\\.\\.\\."
64 " resuming interrupted restart_syscall \\.\\.\\.>\\) = .*");
65
66 puts("\\+\\+\\+ exited with 0 \\+\\+\\+");
67 return 0;
68 #endif
69 }