Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sigkill_rain.c 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <stdlib.h>
  2. #include <stddef.h>
  3. #include <unistd.h>
  4. #include <signal.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <stdio.h>
  8. #include <sys/wait.h>
  9. static const struct sockaddr sa;
  10. int main(int argc, char *argv[])
  11. {
  12. int loops;
  13. int pid;
  14. sigset_t set;
  15. printf(
  16. "Please run me under 'strace -f -oLOG', and examine LOG file for incorrect\n"
  17. "decoding of interrupted syscalls: grep for 'sendto', '??" /* anti-trigraph gap */ "?', 'unavailable'.\n"
  18. "Pass number of iterations in argv[1] (default: 999).\n"
  19. );
  20. fflush(NULL);
  21. sigemptyset(&set);
  22. sigaddset(&set, SIGCHLD);
  23. sigprocmask(SIG_BLOCK, &set, NULL);
  24. loops = 999;
  25. if (argv[1])
  26. loops = atoi(argv[1]);
  27. while (--loops >= 0) {
  28. pid = fork();
  29. if (pid < 0)
  30. exit(1);
  31. if (!pid) {
  32. /* child */
  33. int child = getpid();
  34. loops = 99;
  35. while (--loops) {
  36. pid = fork();
  37. if (pid < 0)
  38. exit(1);
  39. if (!pid) {
  40. /* grandchild: kill child */
  41. kill(child, SIGKILL);
  42. exit(0);
  43. }
  44. /* Add various syscalls you want to test here.
  45. * strace will decode them and suddenly find
  46. * process disappearing.
  47. * But leave at least one case "empty", so that
  48. * "kill grandchild" happens quicker.
  49. * This produces cases when strace can't even
  50. * decode syscall number before process dies.
  51. */
  52. switch (loops & 1) {
  53. case 0:
  54. break; /* intentionally empty */
  55. case 1:
  56. sendto(-1, "Hello cruel world", 17, 0, &sa, sizeof(sa));
  57. break;
  58. }
  59. /* kill grandchild */
  60. kill(pid, SIGKILL);
  61. }
  62. exit(0);
  63. }
  64. /* parent */
  65. wait(NULL);
  66. }
  67. return 0;
  68. }