tst-getpid3.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <errno.h>
  2. #include <pthread.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sys/wait.h>
  8. static pid_t pid;
  9. static void *
  10. pid_thread (void *arg)
  11. {
  12. if (pid != getpid ())
  13. {
  14. printf ("pid wrong in thread: should be %d, is %d\n",
  15. (int) pid, (int) getpid ());
  16. return (void *) 1L;
  17. }
  18. return NULL;
  19. }
  20. static int
  21. do_test (void)
  22. {
  23. pid = getpid ();
  24. pthread_t thr;
  25. int ret = pthread_create (&thr, NULL, pid_thread, NULL);
  26. if (ret)
  27. {
  28. printf ("pthread_create failed: %d\n", ret);
  29. return 1;
  30. }
  31. void *thr_ret;
  32. ret = pthread_join (thr, &thr_ret);
  33. if (ret)
  34. {
  35. printf ("pthread_create failed: %d\n", ret);
  36. return 1;
  37. }
  38. else if (thr_ret)
  39. {
  40. printf ("thread getpid failed\n");
  41. return 1;
  42. }
  43. pid_t child = fork ();
  44. if (child == -1)
  45. {
  46. printf ("fork failed: %m\n");
  47. return 1;
  48. }
  49. else if (child == 0)
  50. {
  51. if (pid == getpid ())
  52. {
  53. puts ("pid did not change after fork");
  54. exit (1);
  55. }
  56. pid = getpid ();
  57. ret = pthread_create (&thr, NULL, pid_thread, NULL);
  58. if (ret)
  59. {
  60. printf ("pthread_create failed: %d\n", ret);
  61. return 1;
  62. }
  63. ret = pthread_join (thr, &thr_ret);
  64. if (ret)
  65. {
  66. printf ("pthread_create failed: %d\n", ret);
  67. return 1;
  68. }
  69. else if (thr_ret)
  70. {
  71. printf ("thread getpid failed\n");
  72. return 1;
  73. }
  74. return 0;
  75. }
  76. int status;
  77. if (TEMP_FAILURE_RETRY (waitpid (child, &status, 0)) != child)
  78. {
  79. puts ("waitpid failed");
  80. kill (child, SIGKILL);
  81. return 1;
  82. }
  83. if (!WIFEXITED (status))
  84. {
  85. if (WIFSIGNALED (status))
  86. printf ("died from signal %s\n", strsignal (WTERMSIG (status)));
  87. else
  88. puts ("did not terminate correctly");
  89. return 1;
  90. }
  91. if (WEXITSTATUS (status) != 0)
  92. {
  93. printf ("exit code %d\n", WEXITSTATUS (status));
  94. return 1;
  95. }
  96. return 0;
  97. }
  98. #define TEST_FUNCTION do_test ()
  99. #include "../test-skeleton.c"