6 #include <linux/types.h>
8 #include <sys/syscall.h>
12 #include "linux/ptrace.h"
14 static int sys_rt_sigqueueinfo(pid_t tgid, int sig, siginfo_t *uinfo)
16 return syscall(SYS_rt_sigqueueinfo, tgid, sig, uinfo);
19 static int sys_rt_tgsigqueueinfo(pid_t tgid, pid_t tid,
20 int sig, siginfo_t *uinfo)
22 return syscall(SYS_rt_tgsigqueueinfo, tgid, tid, sig, uinfo);
25 static int sys_ptrace(int request, pid_t pid, void *addr, void *data)
27 return syscall(SYS_ptrace, request, pid, addr, data);
31 #define TEST_SICODE_PRIV -1
32 #define TEST_SICODE_SHARE -2
34 #define err(fmt, ...) \
36 "Error (%s:%d): " fmt, \
37 __FILE__, __LINE__, ##__VA_ARGS__)
39 static int check_error_paths(pid_t child)
41 struct ptrace_peeksiginfo_args arg;
42 int ret, exit_code = -1;
43 void *addr_rw, *addr_ro;
46 * Allocate two contiguous pages. The first one is for read-write,
47 * another is for read-only.
49 addr_rw = mmap(NULL, 2 * PAGE_SIZE, PROT_READ | PROT_WRITE,
50 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
51 if (addr_rw == MAP_FAILED) {
52 err("mmap() failed: %m\n");
56 addr_ro = mmap(addr_rw + PAGE_SIZE, PAGE_SIZE, PROT_READ,
57 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
58 if (addr_ro == MAP_FAILED) {
59 err("mmap() failed: %m\n");
66 /* Unsupported flags */
68 ret = sys_ptrace(PTRACE_PEEKSIGINFO, child, &arg, addr_rw);
69 if (ret != -1 || errno != EINVAL) {
70 err("sys_ptrace() returns %d (expected -1),"
71 " errno %d (expected %d): %m\n",
77 /* A part of the buffer is read-only */
78 ret = sys_ptrace(PTRACE_PEEKSIGINFO, child, &arg,
79 addr_ro - sizeof(siginfo_t) * 2);
81 err("sys_ptrace() returns %d (expected 2): %m\n", ret);
85 /* Read-only buffer */
86 ret = sys_ptrace(PTRACE_PEEKSIGINFO, child, &arg, addr_ro);
87 if (ret != -1 && errno != EFAULT) {
88 err("sys_ptrace() returns %d (expected -1),"
89 " errno %d (expected %d): %m\n",
96 munmap(addr_rw, 2 * PAGE_SIZE);
100 int check_direct_path(pid_t child, int shared, int nr)
102 struct ptrace_peeksiginfo_args arg = {.flags = 0, .nr = nr, .off = 0};
103 int i, j, ret, exit_code = -1;
104 siginfo_t siginfo[SIGNR];
108 arg.flags = PTRACE_PEEKSIGINFO_SHARED;
109 si_code = TEST_SICODE_SHARE;
112 si_code = TEST_SICODE_PRIV;
115 for (i = 0; i < SIGNR; ) {
117 ret = sys_ptrace(PTRACE_PEEKSIGINFO, child, &arg, siginfo);
119 err("ptrace() failed: %m\n");
126 for (j = 0; j < ret; j++, i++) {
127 if (siginfo[j].si_code == si_code &&
128 siginfo[j].si_int == i)
131 err("%d: Wrong siginfo i=%d si_code=%d si_int=%d\n",
132 shared, i, siginfo[j].si_code, siginfo[j].si_int);
138 err("Only %d signals were read\n", i);
147 int main(int argc, char *argv[])
149 siginfo_t siginfo[SIGNR];
150 int i, exit_code = 1;
154 sigemptyset(&blockmask);
155 sigaddset(&blockmask, SIGRTMIN);
156 sigprocmask(SIG_BLOCK, &blockmask, NULL);
160 err("fork() failed: %m");
162 } else if (child == 0) {
163 pid_t ppid = getppid();
165 if (ppid != getppid())
172 /* Send signals in process-wide and per-thread queues */
173 for (i = 0; i < SIGNR; i++) {
174 siginfo->si_code = TEST_SICODE_SHARE;
176 sys_rt_sigqueueinfo(child, SIGRTMIN, siginfo);
178 siginfo->si_code = TEST_SICODE_PRIV;
180 sys_rt_tgsigqueueinfo(child, child, SIGRTMIN, siginfo);
183 if (sys_ptrace(PTRACE_ATTACH, child, NULL, NULL) == -1)
186 waitpid(child, NULL, 0);
188 /* Dump signals one by one*/
189 if (check_direct_path(child, 0, 1))
191 /* Dump all signals for one call */
192 if (check_direct_path(child, 0, SIGNR))
196 * Dump signal from the process-wide queue.
197 * The number of signals is not multible to the buffer size
199 if (check_direct_path(child, 1, 3))
202 if (check_error_paths(child))
208 if (sys_ptrace(PTRACE_KILL, child, NULL, NULL) == -1)
211 waitpid(child, NULL, 0);