tst-atfork2.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Copyright (C) 2003-2016 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <dlfcn.h>
  16. #include <errno.h>
  17. #include <pthread.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <sys/wait.h>
  22. #include "../test-skeleton.h"
  23. #ifdef __ARCH_USE_MMU__
  24. /* Must be exported. */
  25. int val;
  26. static void
  27. prepare (void)
  28. {
  29. val *= 2;
  30. }
  31. static void
  32. parent (void)
  33. {
  34. val += 4;
  35. }
  36. static void
  37. child (void)
  38. {
  39. val += 8;
  40. }
  41. static int
  42. do_test (void)
  43. {
  44. if (pthread_atfork (prepare, parent, child) != 0)
  45. {
  46. puts ("do_test: atfork failed");
  47. exit (1);
  48. }
  49. void *h = dlopen ("tst-atfork2mod.so", RTLD_LAZY);
  50. if (h == NULL)
  51. {
  52. printf ("dlopen failed: %s\n", dlerror ());
  53. exit (1);
  54. }
  55. /* First trial of fork. */
  56. pid_t pid = fork ();
  57. if (pid == -1)
  58. {
  59. puts ("1st fork failed");
  60. exit (1);
  61. }
  62. if (pid == 0)
  63. {
  64. /* Child. */
  65. if (val != 80)
  66. {
  67. printf ("1st: expected val=%d, got %d\n", 80, val);
  68. exit (2);
  69. }
  70. exit (0);
  71. }
  72. /* Parent. */
  73. if (val != 24)
  74. {
  75. printf ("1st: expected val=%d, got %d\n", 24, val);
  76. exit (1);
  77. }
  78. int status;
  79. if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
  80. {
  81. puts ("1st waitpid failed");
  82. exit (1);
  83. }
  84. if (status != 0)
  85. exit (status);
  86. puts ("unloading now");
  87. /* Unload the module. */
  88. if (dlclose (h) != 0)
  89. {
  90. puts ("dlclose failed");
  91. exit (1);
  92. }
  93. puts ("2nd fork");
  94. /* Second fork trial. */
  95. val = 1;
  96. pid = fork ();
  97. if (pid == -1)
  98. {
  99. puts ("2nd fork failed");
  100. exit (1);
  101. }
  102. if (pid == 0)
  103. {
  104. /* Child. */
  105. if (val != 10)
  106. {
  107. printf ("2nd: expected val=%d, got %d\n", 10, val);
  108. exit (3);
  109. }
  110. exit (0);
  111. }
  112. /* Parent. */
  113. if (val != 6)
  114. {
  115. printf ("2nd: expected val=%d, got %d\n", 6, val);
  116. exit (1);
  117. }
  118. if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
  119. {
  120. puts ("2nd waitpid failed");
  121. exit (1);
  122. }
  123. if (status != 0)
  124. exit (status);
  125. return 0;
  126. }
  127. #define TEST_FUNCTION do_test ()
  128. #include "../test-skeleton.c"
  129. #else
  130. int main(void)
  131. {
  132. printf("Skipping test on non-mmu host!\n");
  133. return 23;
  134. }
  135. #endif