1 /*
2  * Check decoding of sigpending syscall.
3  *
4  * Copyright (c) 2016-2017 Dmitry V. Levin <ldv@altlinux.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "tests.h"
31 #include <asm/unistd.h>
32 
33 #ifdef __NR_sigpending
34 
35 # include <signal.h>
36 # include <stdint.h>
37 # include <stdio.h>
38 # include <string.h>
39 # include <unistd.h>
40 
41 static const char *errstr;
42 
43 static long
44 k_sigpending(const kernel_ulong_t set)
45 {
46 	const long rc = syscall(__NR_sigpending, set);
47 	errstr = sprintrc(rc);
48 	return rc;
49 }
50 
51 int
52 main(void)
53 {
54 	TAIL_ALLOC_OBJECT_CONST_PTR(kernel_ulong_t, k_set);
55 	TAIL_ALLOC_OBJECT_CONST_PTR(sigset_t, libc_set);
56 
57 	sigemptyset(libc_set);
58 	if (sigprocmask(SIG_SETMASK, libc_set, NULL))
59 		perror_msg_and_fail("sigprocmask");
60 
61 	if (k_sigpending((uintptr_t) libc_set))
62 		perror_msg_and_skip("sigpending");
63 	else
64 		puts("sigpending([]) = 0");
65 
66 	k_sigpending((uintptr_t) k_set);
67 	puts("sigpending([]) = 0");
68 
69 	k_sigpending((uintptr_t) (k_set + 1));
70 	printf("sigpending(%p) = -1 EFAULT (%m)\n", k_set + 1);
71 
72 	uintptr_t efault = sizeof(*k_set) / 2 + (uintptr_t) k_set;
73 	k_sigpending(efault);
74 	printf("sigpending(%#jx) = -1 EFAULT (%m)\n", (uintmax_t) efault);
75 
76 	sigaddset(libc_set, SIGHUP);
77 	if (sigprocmask(SIG_SETMASK, libc_set, NULL))
78 		perror_msg_and_fail("sigprocmask");
79 	raise(SIGHUP);
80 
81 	k_sigpending((uintptr_t) k_set);
82 	puts("sigpending([HUP]) = 0");
83 
84 	sigaddset(libc_set, SIGINT);
85 	if (sigprocmask(SIG_SETMASK, libc_set, NULL))
86 		perror_msg_and_fail("sigprocmask");
87 	raise(SIGINT);
88 
89 	k_sigpending((uintptr_t) k_set);
90 	puts("sigpending([HUP INT]) = 0");
91 
92 	if (F8ILL_KULONG_SUPPORTED) {
93 		k_sigpending(f8ill_ptr_to_kulong(k_set));
94 		printf("sigpending(%#jx) = %s\n",
95 		       (uintmax_t) f8ill_ptr_to_kulong(k_set), errstr);
96 	}
97 
98 	puts("+++ exited with 0 +++");
99 	return 0;
100 }
101 
102 #else
103 
104 SKIP_MAIN_UNDEFINED("__NR_sigpending")
105 
106 #endif
107