peeksiginfo.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <signal.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include <linux/types.h>
  7. #include <sys/wait.h>
  8. #include <sys/syscall.h>
  9. #include <sys/user.h>
  10. #include <sys/mman.h>
  11. #include "linux/ptrace.h"
  12. static int sys_rt_sigqueueinfo(pid_t tgid, int sig, siginfo_t *uinfo)
  13. {
  14. return syscall(SYS_rt_sigqueueinfo, tgid, sig, uinfo);
  15. }
  16. static int sys_rt_tgsigqueueinfo(pid_t tgid, pid_t tid,
  17. int sig, siginfo_t *uinfo)
  18. {
  19. return syscall(SYS_rt_tgsigqueueinfo, tgid, tid, sig, uinfo);
  20. }
  21. static int sys_ptrace(int request, pid_t pid, void *addr, void *data)
  22. {
  23. return syscall(SYS_ptrace, request, pid, addr, data);
  24. }
  25. #define SIGNR 10
  26. #define TEST_SICODE_PRIV -1
  27. #define TEST_SICODE_SHARE -2
  28. #ifndef PAGE_SIZE
  29. #define PAGE_SIZE sysconf(_SC_PAGESIZE)
  30. #endif
  31. #define err(fmt, ...) \
  32. fprintf(stderr, \
  33. "Error (%s:%d): " fmt, \
  34. __FILE__, __LINE__, ##__VA_ARGS__)
  35. static int check_error_paths(pid_t child)
  36. {
  37. struct ptrace_peeksiginfo_args arg;
  38. int ret, exit_code = -1;
  39. void *addr_rw, *addr_ro;
  40. /*
  41. * Allocate two contiguous pages. The first one is for read-write,
  42. * another is for read-only.
  43. */
  44. addr_rw = mmap(NULL, 2 * PAGE_SIZE, PROT_READ | PROT_WRITE,
  45. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  46. if (addr_rw == MAP_FAILED) {
  47. err("mmap() failed: %m\n");
  48. return 1;
  49. }
  50. addr_ro = mmap(addr_rw + PAGE_SIZE, PAGE_SIZE, PROT_READ,
  51. MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  52. if (addr_ro == MAP_FAILED) {
  53. err("mmap() failed: %m\n");
  54. goto out;
  55. }
  56. arg.nr = SIGNR;
  57. arg.off = 0;
  58. /* Unsupported flags */
  59. arg.flags = ~0;
  60. ret = sys_ptrace(PTRACE_PEEKSIGINFO, child, &arg, addr_rw);
  61. if (ret != -1 || errno != EINVAL) {
  62. err("sys_ptrace() returns %d (expected -1),"
  63. " errno %d (expected %d): %m\n",
  64. ret, errno, EINVAL);
  65. goto out;
  66. }
  67. arg.flags = 0;
  68. /* A part of the buffer is read-only */
  69. ret = sys_ptrace(PTRACE_PEEKSIGINFO, child, &arg,
  70. addr_ro - sizeof(siginfo_t) * 2);
  71. if (ret != 2) {
  72. err("sys_ptrace() returns %d (expected 2): %m\n", ret);
  73. goto out;
  74. }
  75. /* Read-only buffer */
  76. ret = sys_ptrace(PTRACE_PEEKSIGINFO, child, &arg, addr_ro);
  77. if (ret != -1 && errno != EFAULT) {
  78. err("sys_ptrace() returns %d (expected -1),"
  79. " errno %d (expected %d): %m\n",
  80. ret, errno, EFAULT);
  81. goto out;
  82. }
  83. exit_code = 0;
  84. out:
  85. munmap(addr_rw, 2 * PAGE_SIZE);
  86. return exit_code;
  87. }
  88. int check_direct_path(pid_t child, int shared, int nr)
  89. {
  90. struct ptrace_peeksiginfo_args arg = {.flags = 0, .nr = nr, .off = 0};
  91. int i, j, ret, exit_code = -1;
  92. siginfo_t siginfo[SIGNR];
  93. int si_code;
  94. if (shared == 1) {
  95. arg.flags = PTRACE_PEEKSIGINFO_SHARED;
  96. si_code = TEST_SICODE_SHARE;
  97. } else {
  98. arg.flags = 0;
  99. si_code = TEST_SICODE_PRIV;
  100. }
  101. for (i = 0; i < SIGNR; ) {
  102. arg.off = i;
  103. ret = sys_ptrace(PTRACE_PEEKSIGINFO, child, &arg, siginfo);
  104. if (ret == -1) {
  105. err("ptrace() failed: %m\n");
  106. goto out;
  107. }
  108. if (ret == 0)
  109. break;
  110. for (j = 0; j < ret; j++, i++) {
  111. if (siginfo[j].si_code == si_code &&
  112. siginfo[j].si_int == i)
  113. continue;
  114. err("%d: Wrong siginfo i=%d si_code=%d si_int=%d\n",
  115. shared, i, siginfo[j].si_code, siginfo[j].si_int);
  116. goto out;
  117. }
  118. }
  119. if (i != SIGNR) {
  120. err("Only %d signals were read\n", i);
  121. goto out;
  122. }
  123. exit_code = 0;
  124. out:
  125. return exit_code;
  126. }
  127. int main(int argc, char *argv[])
  128. {
  129. siginfo_t siginfo[SIGNR];
  130. int i, exit_code = 1;
  131. sigset_t blockmask;
  132. pid_t child;
  133. sigemptyset(&blockmask);
  134. sigaddset(&blockmask, SIGRTMIN);
  135. sigprocmask(SIG_BLOCK, &blockmask, NULL);
  136. child = fork();
  137. if (child == -1) {
  138. err("fork() failed: %m");
  139. return 1;
  140. } else if (child == 0) {
  141. pid_t ppid = getppid();
  142. while (1) {
  143. if (ppid != getppid())
  144. break;
  145. sleep(1);
  146. }
  147. return 1;
  148. }
  149. /* Send signals in process-wide and per-thread queues */
  150. for (i = 0; i < SIGNR; i++) {
  151. siginfo->si_code = TEST_SICODE_SHARE;
  152. siginfo->si_int = i;
  153. sys_rt_sigqueueinfo(child, SIGRTMIN, siginfo);
  154. siginfo->si_code = TEST_SICODE_PRIV;
  155. siginfo->si_int = i;
  156. sys_rt_tgsigqueueinfo(child, child, SIGRTMIN, siginfo);
  157. }
  158. if (sys_ptrace(PTRACE_ATTACH, child, NULL, NULL) == -1)
  159. return 1;
  160. waitpid(child, NULL, 0);
  161. /* Dump signals one by one*/
  162. if (check_direct_path(child, 0, 1))
  163. goto out;
  164. /* Dump all signals for one call */
  165. if (check_direct_path(child, 0, SIGNR))
  166. goto out;
  167. /*
  168. * Dump signal from the process-wide queue.
  169. * The number of signals is not multible to the buffer size
  170. */
  171. if (check_direct_path(child, 1, 3))
  172. goto out;
  173. if (check_error_paths(child))
  174. goto out;
  175. printf("PASS\n");
  176. exit_code = 0;
  177. out:
  178. if (sys_ptrace(PTRACE_KILL, child, NULL, NULL) == -1)
  179. return 1;
  180. waitpid(child, NULL, 0);
  181. return exit_code;
  182. }