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