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