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