tst-getpid1.c 2.5 KB

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