• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2015-2017 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "tests.h"
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <signal.h>
34 #include <time.h>
35 #include <sys/time.h>
36 
37 int
main(void)38 main(void)
39 {
40 #if defined __x86_64__ && defined __ILP32__
41 	/*
42 	 * x32 is broken from the beginning:
43 	 * https://lkml.org/lkml/2015/11/30/790
44 	 */
45 	error_msg_and_skip("x32 is broken");
46 #else
47 	const sigset_t set = {};
48 	const struct sigaction act = { .sa_handler = SIG_IGN };
49 	const struct itimerval itv = { .it_value.tv_usec = 111111 };
50 	struct timespec req = { .tv_nsec = 222222222 }, rem;
51 
52 	assert(sigaction(SIGALRM, &act, NULL) == 0);
53 	assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
54 	if (setitimer(ITIMER_REAL, &itv, NULL))
55 		perror_msg_and_skip("setitimer");
56 	if (nanosleep(&req, &rem))
57 		perror_msg_and_fail("nanosleep");
58 
59 	printf("nanosleep\\(\\{tv_sec=%lld, tv_nsec=%llu\\}"
60 	       ", \\{tv_sec=%lld, tv_nsec=%llu\\}\\)"
61 	       " = \\? ERESTART_RESTARTBLOCK \\(Interrupted by signal\\)\n",
62 	       (long long) req.tv_sec, zero_extend_signed_to_ull(req.tv_nsec),
63 	       (long long) rem.tv_sec, zero_extend_signed_to_ull(rem.tv_nsec));
64 	puts("--- SIGALRM \\{si_signo=SIGALRM, si_code=SI_KERNEL\\} ---");
65 #ifdef __arm__
66 /* old kernels used to overwrite ARM_r0 with -EINTR */
67 # define ALTERNATIVE_NANOSLEEP_REQ "0xfffffffc|"
68 #else
69 # define ALTERNATIVE_NANOSLEEP_REQ ""
70 #endif
71 	printf("(nanosleep\\((%s\\{tv_sec=%lld, tv_nsec=%llu\\})"
72 	       ", %p|restart_syscall\\(<\\.\\.\\."
73 	       " resuming interrupted nanosleep \\.\\.\\.>)\\) = 0\n",
74 	       ALTERNATIVE_NANOSLEEP_REQ,
75 	       (long long) req.tv_sec, zero_extend_signed_to_ull(req.tv_nsec),
76 	       &rem);
77 
78 	puts("\\+\\+\\+ exited with 0 \\+\\+\\+");
79 	return 0;
80 #endif
81 }
82