tst-atfork2.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /* Must be exported. */
  24. int val;
  25. static void
  26. prepare (void)
  27. {
  28. val *= 2;
  29. }
  30. static void
  31. parent (void)
  32. {
  33. val += 4;
  34. }
  35. static void
  36. child (void)
  37. {
  38. val += 8;
  39. }
  40. static int
  41. do_test (void)
  42. {
  43. if (pthread_atfork (prepare, parent, child) != 0)
  44. {
  45. puts ("do_test: atfork failed");
  46. exit (1);
  47. }
  48. void *h = dlopen ("tst-atfork2mod.so", RTLD_LAZY);
  49. if (h == NULL)
  50. {
  51. printf ("dlopen failed: %s\n", dlerror ());
  52. exit (1);
  53. }
  54. /* First trial of fork. */
  55. pid_t pid = fork ();
  56. if (pid == -1)
  57. {
  58. puts ("1st fork failed");
  59. exit (1);
  60. }
  61. if (pid == 0)
  62. {
  63. /* Child. */
  64. if (val != 80)
  65. {
  66. printf ("1st: expected val=%d, got %d\n", 80, val);
  67. exit (2);
  68. }
  69. exit (0);
  70. }
  71. /* Parent. */
  72. if (val != 24)
  73. {
  74. printf ("1st: expected val=%d, got %d\n", 24, val);
  75. exit (1);
  76. }
  77. int status;
  78. if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
  79. {
  80. puts ("1st waitpid failed");
  81. exit (1);
  82. }
  83. if (status != 0)
  84. exit (status);
  85. puts ("unloading now");
  86. /* Unload the module. */
  87. if (dlclose (h) != 0)
  88. {
  89. puts ("dlclose failed");
  90. exit (1);
  91. }
  92. puts ("2nd fork");
  93. /* Second fork trial. */
  94. val = 1;
  95. pid = fork ();
  96. if (pid == -1)
  97. {
  98. puts ("2nd fork failed");
  99. exit (1);
  100. }
  101. if (pid == 0)
  102. {
  103. /* Child. */
  104. if (val != 10)
  105. {
  106. printf ("2nd: expected val=%d, got %d\n", 10, val);
  107. exit (3);
  108. }
  109. exit (0);
  110. }
  111. /* Parent. */
  112. if (val != 6)
  113. {
  114. printf ("2nd: expected val=%d, got %d\n", 6, val);
  115. exit (1);
  116. }
  117. if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
  118. {
  119. puts ("2nd waitpid failed");
  120. exit (1);
  121. }
  122. if (status != 0)
  123. exit (status);
  124. return 0;
  125. }
  126. #define TEST_FUNCTION do_test ()
  127. #include "../test-skeleton.c"