1 /* Helper file for tst-{atexit,at_quick_exit,cxa_atexit,on_exit}.
2 Copyright (C) 2017-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
26 /* http://pubs.opengroup.org/onlinepubs/000095399/functions/atexit.html
27 requires that we support at least 32 atexit handlers.
29 The number we actually support is limited by memory. Here we simply
30 check that we support at least the minimum required. */
33 /* Arbitrary sequence matching current registrations. */
34 const char expected
[] = "00000000000000000000000003021121130211";
36 static char crumbs
[sizeof (expected
)];
37 static int next_slot
= 0;
39 /* Helper: flush stdout and _exit. */
41 _exit_with_flush (int code
)
50 crumbs
[next_slot
++] = '0';
56 crumbs
[next_slot
++] = '1';
62 crumbs
[next_slot
++] = '2';
69 crumbs
[next_slot
++] = '3';
77 if (strcmp (crumbs
, expected
) == 0)
80 printf ("crumbs: %s\n", crumbs
);
81 printf ("expected: %s\n", expected
);
88 int slots_remaining
= MAX_ATEXIT
;
90 /* Register this first so it can verify expected order of the rest. */
91 ATEXIT (fn_final
); --slots_remaining
;
93 ATEXIT (fn1
); --slots_remaining
;
94 ATEXIT (fn3
); --slots_remaining
;
95 ATEXIT (fn1
); --slots_remaining
;
96 ATEXIT (fn2
); --slots_remaining
;
97 ATEXIT (fn1
); --slots_remaining
;
98 ATEXIT (fn3
); --slots_remaining
;
100 /* Fill the rest of available slots with fn0. */
101 while (slots_remaining
> 0)
103 ATEXIT (fn0
); --slots_remaining
;
106 /* Verify that handlers registered above are inherited across fork. */
107 const pid_t child
= fork ();
111 printf ("fork: %m\n");
112 _exit_with_flush (3);
118 const pid_t exited
= waitpid (child
, &status
, 0);
121 printf ("unexpected child: %d, expected %d\n", exited
, child
);
122 _exit_with_flush (4);
126 if (WIFEXITED (status
))
127 printf ("unexpected exit status %d from child %d\n",
128 WEXITSTATUS (status
), child
);
129 else if (WIFSIGNALED (status
))
130 printf ("unexpected signal %d from child %d\n",
131 WTERMSIG (status
), child
);
133 printf ("unexpected status %d from child %d\n", status
, child
);
134 _exit_with_flush (5);
140 EXIT (2); /* If we see this exit code, fn_final must have not worked. */
143 #define TEST_FUNCTION do_test
144 #include <support/test-driver.c>