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