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