tst-getpid1.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <sched.h>
  2. #include <signal.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8. #include <bits/stackinfo.h>
  9. #ifndef TEST_CLONE_FLAGS
  10. #define TEST_CLONE_FLAGS 0
  11. #endif
  12. static int sig;
  13. static int
  14. f (void *a)
  15. {
  16. puts ("in f");
  17. union sigval sival;
  18. sival.sival_int = getpid ();
  19. printf ("pid = %d\n", sival.sival_int);
  20. if (sigqueue (getppid (), sig, sival) != 0)
  21. return 1;
  22. return 0;
  23. }
  24. static int
  25. do_test (void)
  26. {
  27. int mypid = getpid ();
  28. sig = SIGRTMIN;
  29. sigset_t ss;
  30. sigemptyset (&ss);
  31. sigaddset (&ss, sig);
  32. if (sigprocmask (SIG_BLOCK, &ss, NULL) != 0)
  33. {
  34. printf ("sigprocmask failed: %m\n");
  35. return 1;
  36. }
  37. #ifdef __ia64__
  38. extern int __clone2 (int (*__fn) (void *__arg), void *__child_stack_base,
  39. size_t __child_stack_size, int __flags,
  40. void *__arg, ...);
  41. char st[256 * 1024] __attribute__ ((aligned));
  42. pid_t p = __clone2 (f, st, sizeof (st), TEST_CLONE_FLAGS, 0);
  43. #else
  44. char st[128 * 1024] __attribute__ ((aligned));
  45. # if _STACK_GROWS_DOWN
  46. pid_t p = clone (f, st + sizeof (st), TEST_CLONE_FLAGS, 0);
  47. # elif _STACK_GROWS_UP
  48. pid_t p = clone (f, st, TEST_CLONE_FLAGS, 0);
  49. # else
  50. # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
  51. # endif
  52. #endif
  53. if (p == -1)
  54. {
  55. printf("clone failed: %m\n");
  56. return 1;
  57. }
  58. printf ("new thread: %d\n", (int) p);
  59. siginfo_t si;
  60. do
  61. if (sigwaitinfo (&ss, &si) < 0)
  62. {
  63. printf("sigwaitinfo failed: %m\n");
  64. kill (p, SIGKILL);
  65. return 1;
  66. }
  67. while (si.si_signo != sig || si.si_code != SI_QUEUE);
  68. int e;
  69. if (waitpid (p, &e, __WCLONE) != p)
  70. {
  71. puts ("waitpid failed");
  72. kill (p, SIGKILL);
  73. return 1;
  74. }
  75. if (!WIFEXITED (e))
  76. {
  77. if (WIFSIGNALED (e))
  78. printf ("died from signal %s\n", strsignal (WTERMSIG (e)));
  79. else
  80. puts ("did not terminate correctly");
  81. return 1;
  82. }
  83. if (WEXITSTATUS (e) != 0)
  84. {
  85. printf ("exit code %d\n", WEXITSTATUS (e));
  86. return 1;
  87. }
  88. if (si.si_int != (int) p)
  89. {
  90. printf ("expected PID %d, got si_int %d\n", (int) p, si.si_int);
  91. kill (p, SIGKILL);
  92. return 1;
  93. }
  94. if (si.si_pid != p)
  95. {
  96. printf ("expected PID %d, got si_pid %d\n", (int) p, (int) si.si_pid);
  97. kill (p, SIGKILL);
  98. return 1;
  99. }
  100. if (getpid () != mypid)
  101. {
  102. puts ("my PID changed");
  103. return 1;
  104. }
  105. return 0;
  106. }
  107. #define TEST_FUNCTION do_test ()
  108. #include "../test-skeleton.c"