tst-atexit-common.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Helper file for tst-{atexit,at_quick_exit,cxa_atexit,on_exit}.
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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 <assert.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <sys/wait.h>
  21. /* http://pubs.opengroup.org/onlinepubs/000095399/functions/atexit.html
  22. requires that we support at least 32 atexit handlers.
  23. The number we actually support is limited by memory. Here we simply
  24. check that we support at least the minimum required. */
  25. #define MAX_ATEXIT 32
  26. /* Arbitrary sequence matching current registrations. */
  27. const char expected[] = "00000000000000000000000003021121130211";
  28. static char crumbs[sizeof (expected)];
  29. static int next_slot = 0;
  30. /* Helper: flush stdout and _exit. */
  31. static void
  32. _exit_with_flush (int code)
  33. {
  34. fflush (stdout);
  35. _exit (code);
  36. }
  37. static void
  38. fn0 (void)
  39. {
  40. crumbs[next_slot++] = '0';
  41. }
  42. static void
  43. fn1 (void)
  44. {
  45. crumbs[next_slot++] = '1';
  46. }
  47. static void
  48. fn2 (void)
  49. {
  50. crumbs[next_slot++] = '2';
  51. ATEXIT (fn1);
  52. }
  53. static void
  54. fn3 (void)
  55. {
  56. crumbs[next_slot++] = '3';
  57. ATEXIT (fn2);
  58. ATEXIT (fn0);
  59. }
  60. static void
  61. fn_final (void)
  62. {
  63. if (strcmp (crumbs, expected) == 0)
  64. _exit_with_flush (0);
  65. printf ("crumbs: %s\n", crumbs);
  66. printf ("expected: %s\n", expected);
  67. _exit_with_flush (1);
  68. }
  69. static int
  70. do_test (void)
  71. {
  72. int slots_remaining = MAX_ATEXIT;
  73. /* Register this first so it can verify expected order of the rest. */
  74. ATEXIT (fn_final); --slots_remaining;
  75. ATEXIT (fn1); --slots_remaining;
  76. ATEXIT (fn3); --slots_remaining;
  77. ATEXIT (fn1); --slots_remaining;
  78. ATEXIT (fn2); --slots_remaining;
  79. ATEXIT (fn1); --slots_remaining;
  80. ATEXIT (fn3); --slots_remaining;
  81. /* Fill the rest of available slots with fn0. */
  82. while (slots_remaining > 0)
  83. {
  84. ATEXIT (fn0); --slots_remaining;
  85. }
  86. /* Verify that handlers registered above are inherited across fork. */
  87. const pid_t child = fork ();
  88. switch (child)
  89. {
  90. case -1:
  91. printf ("fork: %m\n");
  92. _exit_with_flush (3);
  93. case 0: /* Child. */
  94. break;
  95. default:
  96. {
  97. int status;
  98. const pid_t exited = waitpid (child, &status, 0);
  99. if (child != exited)
  100. {
  101. printf ("unexpected child: %d, expected %d\n", exited, child);
  102. _exit_with_flush (4);
  103. }
  104. if (status != 0)
  105. {
  106. if (WIFEXITED (status))
  107. printf ("unexpected exit status %d from child %d\n",
  108. WEXITSTATUS (status), child);
  109. else if (WIFSIGNALED (status))
  110. printf ("unexpected signal %d from child %d\n",
  111. WTERMSIG (status), child);
  112. else
  113. printf ("unexpected status %d from child %d\n", status, child);
  114. _exit_with_flush (5);
  115. }
  116. }
  117. break;
  118. }
  119. EXIT (2); /* If we see this exit code, fn_final must have not worked. */
  120. }
  121. #define TEST_FUNCTION do_test
  122. #include <support/test-driver.c>