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