1 /* $NetBSD: h_atexit.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 * Program to test atexit(3) and __cxa_atexit()/__cxa_finalize().
32 * Written by Jason R. Thorpe, February 2003.
36 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 2008\
38 The NetBSD Foundation, inc. All rights reserved.");
39 __RCSID("$NetBSD: h_atexit.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $");
47 extern int __cxa_atexit(void (*func
)(void *), void *, void *);
48 extern void __cxa_finalize(void *);
50 static int dso_handle_1
;
51 static int dso_handle_2
;
52 static int dso_handle_3
;
58 static int exiting_state
;
60 #define ASSERT(expr) \
63 write(STDERR_FILENO, __func__, strlen(__func__)); \
64 write(STDERR_FILENO, ": ", 2); \
65 write(STDERR_FILENO, __STRING(expr), \
66 strlen(__STRING(expr))); \
67 write(STDERR_FILENO, "\n", 1); \
70 } while (/*CONSTCOND*/0)
74 write(STDOUT_FILENO, __func__, strlen(__func__)); \
75 write(STDOUT_FILENO, "\n", 1); \
76 } while (/*CONSTCOND*/0)
82 kill(getpid(), SIGABRT
);
87 cxa_handler_5(void *arg
)
89 static int cxa_handler_5_called
;
91 ASSERT(arg
== (void *)&arg_1
);
92 ASSERT(cxa_handler_5_called
== 0);
93 ASSERT(exiting_state
== 5);
96 cxa_handler_5_called
= 1;
101 cxa_handler_4(void *arg
)
103 static int cxa_handler_4_called
;
105 ASSERT(arg
== (void *)&arg_1
);
106 ASSERT(cxa_handler_4_called
== 0);
107 ASSERT(exiting_state
== 4);
110 cxa_handler_4_called
= 1;
115 cxa_handler_3(void *arg
)
117 static int cxa_handler_3_called
;
119 ASSERT(arg
== (void *)&arg_2
);
120 ASSERT(cxa_handler_3_called
== 0);
121 ASSERT(exiting_state
== 3);
124 cxa_handler_3_called
= 1;
129 cxa_handler_2(void *arg
)
131 static int cxa_handler_2_called
;
133 ASSERT(arg
== (void *)&arg_3
);
134 ASSERT(cxa_handler_2_called
== 0);
135 ASSERT(exiting_state
== 2);
138 cxa_handler_2_called
= 1;
143 normal_handler_1(void)
145 static int normal_handler_1_called
;
147 ASSERT(normal_handler_1_called
== 0);
148 ASSERT(exiting_state
== 1);
151 normal_handler_1_called
= 1;
156 normal_handler_0(void)
158 static int normal_handler_0_called
;
160 ASSERT(normal_handler_0_called
== 0);
161 ASSERT(exiting_state
== 0);
163 normal_handler_0_called
= 1;
168 main(int argc
, char *argv
[])
173 ASSERT(0 == atexit(normal_handler_0
));
174 ASSERT(0 == atexit(normal_handler_1
));
175 ASSERT(0 == __cxa_atexit(cxa_handler_4
, &arg_1
, &dso_handle_1
));
176 ASSERT(0 == __cxa_atexit(cxa_handler_5
, &arg_1
, &dso_handle_1
));
177 ASSERT(0 == __cxa_atexit(cxa_handler_3
, &arg_2
, &dso_handle_2
));
178 ASSERT(0 == __cxa_atexit(cxa_handler_2
, &arg_3
, &dso_handle_3
));
180 __cxa_finalize(&dso_handle_1
);
181 __cxa_finalize(&dso_handle_2
);