h_atexit.c revision 1.1 1 1.1 jmmv /* $NetBSD: h_atexit.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.1 jmmv * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 jmmv * All rights reserved.
6 1.1 jmmv *
7 1.1 jmmv * Redistribution and use in source and binary forms, with or without
8 1.1 jmmv * modification, are permitted provided that the following conditions
9 1.1 jmmv * are met:
10 1.1 jmmv * 1. Redistributions of source code must retain the above copyright
11 1.1 jmmv * notice, this list of conditions and the following disclaimer.
12 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmmv * notice, this list of conditions and the following disclaimer in the
14 1.1 jmmv * documentation and/or other materials provided with the distribution.
15 1.1 jmmv *
16 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jmmv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmmv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmmv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jmmv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jmmv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jmmv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jmmv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jmmv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jmmv * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmmv */
28 1.1 jmmv
29 1.1 jmmv /*
30 1.1 jmmv * Program to test atexit(3) and __cxa_atexit()/__cxa_finalize().
31 1.1 jmmv *
32 1.1 jmmv * Written by Jason R. Thorpe, February 2003.
33 1.1 jmmv * Public domain.
34 1.1 jmmv */
35 1.1 jmmv
36 1.1 jmmv #include <sys/cdefs.h>
37 1.1 jmmv __COPYRIGHT("@(#) Copyright (c) 2008\
38 1.1 jmmv The NetBSD Foundation, inc. All rights reserved.");
39 1.1 jmmv __RCSID("$NetBSD: h_atexit.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $");
40 1.1 jmmv
41 1.1 jmmv #include <stdio.h>
42 1.1 jmmv #include <stdlib.h>
43 1.1 jmmv #include <signal.h>
44 1.1 jmmv #include <string.h>
45 1.1 jmmv #include <unistd.h>
46 1.1 jmmv
47 1.1 jmmv extern int __cxa_atexit(void (*func)(void *), void *, void *);
48 1.1 jmmv extern void __cxa_finalize(void *);
49 1.1 jmmv
50 1.1 jmmv static int dso_handle_1;
51 1.1 jmmv static int dso_handle_2;
52 1.1 jmmv static int dso_handle_3;
53 1.1 jmmv
54 1.1 jmmv static int arg_1;
55 1.1 jmmv static int arg_2;
56 1.1 jmmv static int arg_3;
57 1.1 jmmv
58 1.1 jmmv static int exiting_state;
59 1.1 jmmv
60 1.1 jmmv #define ASSERT(expr) \
61 1.1 jmmv do { \
62 1.1 jmmv if ((expr) == 0) { \
63 1.1 jmmv write(STDERR_FILENO, __func__, strlen(__func__)); \
64 1.1 jmmv write(STDERR_FILENO, ": ", 2); \
65 1.1 jmmv write(STDERR_FILENO, __STRING(expr), \
66 1.1 jmmv strlen(__STRING(expr))); \
67 1.1 jmmv write(STDERR_FILENO, "\n", 1); \
68 1.1 jmmv my_abort(); \
69 1.1 jmmv } \
70 1.1 jmmv } while (/*CONSTCOND*/0)
71 1.1 jmmv
72 1.1 jmmv #define SUCCESS() \
73 1.1 jmmv do { \
74 1.1 jmmv write(STDOUT_FILENO, __func__, strlen(__func__)); \
75 1.1 jmmv write(STDOUT_FILENO, "\n", 1); \
76 1.1 jmmv } while (/*CONSTCOND*/0)
77 1.1 jmmv
78 1.1 jmmv static void
79 1.1 jmmv my_abort(void)
80 1.1 jmmv {
81 1.1 jmmv
82 1.1 jmmv kill(getpid(), SIGABRT);
83 1.1 jmmv /* NOTREACHED */
84 1.1 jmmv }
85 1.1 jmmv
86 1.1 jmmv static void
87 1.1 jmmv cxa_handler_5(void *arg)
88 1.1 jmmv {
89 1.1 jmmv static int cxa_handler_5_called;
90 1.1 jmmv
91 1.1 jmmv ASSERT(arg == (void *)&arg_1);
92 1.1 jmmv ASSERT(cxa_handler_5_called == 0);
93 1.1 jmmv ASSERT(exiting_state == 5);
94 1.1 jmmv
95 1.1 jmmv exiting_state--;
96 1.1 jmmv cxa_handler_5_called = 1;
97 1.1 jmmv SUCCESS();
98 1.1 jmmv }
99 1.1 jmmv
100 1.1 jmmv static void
101 1.1 jmmv cxa_handler_4(void *arg)
102 1.1 jmmv {
103 1.1 jmmv static int cxa_handler_4_called;
104 1.1 jmmv
105 1.1 jmmv ASSERT(arg == (void *)&arg_1);
106 1.1 jmmv ASSERT(cxa_handler_4_called == 0);
107 1.1 jmmv ASSERT(exiting_state == 4);
108 1.1 jmmv
109 1.1 jmmv exiting_state--;
110 1.1 jmmv cxa_handler_4_called = 1;
111 1.1 jmmv SUCCESS();
112 1.1 jmmv }
113 1.1 jmmv
114 1.1 jmmv static void
115 1.1 jmmv cxa_handler_3(void *arg)
116 1.1 jmmv {
117 1.1 jmmv static int cxa_handler_3_called;
118 1.1 jmmv
119 1.1 jmmv ASSERT(arg == (void *)&arg_2);
120 1.1 jmmv ASSERT(cxa_handler_3_called == 0);
121 1.1 jmmv ASSERT(exiting_state == 3);
122 1.1 jmmv
123 1.1 jmmv exiting_state--;
124 1.1 jmmv cxa_handler_3_called = 1;
125 1.1 jmmv SUCCESS();
126 1.1 jmmv }
127 1.1 jmmv
128 1.1 jmmv static void
129 1.1 jmmv cxa_handler_2(void *arg)
130 1.1 jmmv {
131 1.1 jmmv static int cxa_handler_2_called;
132 1.1 jmmv
133 1.1 jmmv ASSERT(arg == (void *)&arg_3);
134 1.1 jmmv ASSERT(cxa_handler_2_called == 0);
135 1.1 jmmv ASSERT(exiting_state == 2);
136 1.1 jmmv
137 1.1 jmmv exiting_state--;
138 1.1 jmmv cxa_handler_2_called = 1;
139 1.1 jmmv SUCCESS();
140 1.1 jmmv }
141 1.1 jmmv
142 1.1 jmmv static void
143 1.1 jmmv normal_handler_1(void)
144 1.1 jmmv {
145 1.1 jmmv static int normal_handler_1_called;
146 1.1 jmmv
147 1.1 jmmv ASSERT(normal_handler_1_called == 0);
148 1.1 jmmv ASSERT(exiting_state == 1);
149 1.1 jmmv
150 1.1 jmmv exiting_state--;
151 1.1 jmmv normal_handler_1_called = 1;
152 1.1 jmmv SUCCESS();
153 1.1 jmmv }
154 1.1 jmmv
155 1.1 jmmv static void
156 1.1 jmmv normal_handler_0(void)
157 1.1 jmmv {
158 1.1 jmmv static int normal_handler_0_called;
159 1.1 jmmv
160 1.1 jmmv ASSERT(normal_handler_0_called == 0);
161 1.1 jmmv ASSERT(exiting_state == 0);
162 1.1 jmmv
163 1.1 jmmv normal_handler_0_called = 1;
164 1.1 jmmv SUCCESS();
165 1.1 jmmv }
166 1.1 jmmv
167 1.1 jmmv int
168 1.1 jmmv main(int argc, char *argv[])
169 1.1 jmmv {
170 1.1 jmmv
171 1.1 jmmv exiting_state = 5;
172 1.1 jmmv
173 1.1 jmmv ASSERT(0 == atexit(normal_handler_0));
174 1.1 jmmv ASSERT(0 == atexit(normal_handler_1));
175 1.1 jmmv ASSERT(0 == __cxa_atexit(cxa_handler_4, &arg_1, &dso_handle_1));
176 1.1 jmmv ASSERT(0 == __cxa_atexit(cxa_handler_5, &arg_1, &dso_handle_1));
177 1.1 jmmv ASSERT(0 == __cxa_atexit(cxa_handler_3, &arg_2, &dso_handle_2));
178 1.1 jmmv ASSERT(0 == __cxa_atexit(cxa_handler_2, &arg_3, &dso_handle_3));
179 1.1 jmmv
180 1.1 jmmv __cxa_finalize(&dso_handle_1);
181 1.1 jmmv __cxa_finalize(&dso_handle_2);
182 1.1 jmmv exit(0);
183 1.1 jmmv }
184