tst-getpid3.c 2.1 KB

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