1 /* $NetBSD: h_atexit.c,v 1.1 2011/01/12 19:44:08 pgoyette Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code was contributed to The NetBSD Foundation by Jason R. Thorpe.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __COPYRIGHT("@(#) Copyright (c) 2011\
33 The NetBSD Foundation, inc. All rights reserved.");
34 __RCSID("$NetBSD: h_atexit.c,v 1.1 2011/01/12 19:44:08 pgoyette Exp $");
42 extern int __cxa_atexit(void (*func
)(void *), void *, void *);
43 extern void __cxa_finalize(void *);
45 static int dso_handle_1
;
46 static int dso_handle_2
;
47 static int dso_handle_3
;
53 static int exiting_state
;
55 #define ASSERT(expr) \
58 write(STDERR_FILENO, __func__, strlen(__func__)); \
59 write(STDERR_FILENO, ": ", 2); \
60 write(STDERR_FILENO, __STRING(expr), \
61 strlen(__STRING(expr))); \
62 write(STDERR_FILENO, "\n", 1); \
65 } while (/*CONSTCOND*/0)
69 write(STDOUT_FILENO, __func__, strlen(__func__)); \
70 write(STDOUT_FILENO, "\n", 1); \
71 } while (/*CONSTCOND*/0)
77 kill(getpid(), SIGABRT
);
82 cxa_handler_5(void *arg
)
84 static int cxa_handler_5_called
;
86 ASSERT(arg
== (void *)&arg_1
);
87 ASSERT(cxa_handler_5_called
== 0);
88 ASSERT(exiting_state
== 5);
91 cxa_handler_5_called
= 1;
96 cxa_handler_4(void *arg
)
98 static int cxa_handler_4_called
;
100 ASSERT(arg
== (void *)&arg_1
);
101 ASSERT(cxa_handler_4_called
== 0);
102 ASSERT(exiting_state
== 4);
105 cxa_handler_4_called
= 1;
110 cxa_handler_3(void *arg
)
112 static int cxa_handler_3_called
;
114 ASSERT(arg
== (void *)&arg_2
);
115 ASSERT(cxa_handler_3_called
== 0);
116 ASSERT(exiting_state
== 3);
119 cxa_handler_3_called
= 1;
124 cxa_handler_2(void *arg
)
126 static int cxa_handler_2_called
;
128 ASSERT(arg
== (void *)&arg_3
);
129 ASSERT(cxa_handler_2_called
== 0);
130 ASSERT(exiting_state
== 2);
133 cxa_handler_2_called
= 1;
138 normal_handler_1(void)
140 static int normal_handler_1_called
;
142 ASSERT(normal_handler_1_called
== 0);
143 ASSERT(exiting_state
== 1);
146 normal_handler_1_called
= 1;
151 normal_handler_0(void)
153 static int normal_handler_0_called
;
155 ASSERT(normal_handler_0_called
== 0);
156 ASSERT(exiting_state
== 0);
158 normal_handler_0_called
= 1;
163 main(int argc
, char *argv
[])
168 ASSERT(0 == atexit(normal_handler_0
));
169 ASSERT(0 == atexit(normal_handler_1
));
170 ASSERT(0 == __cxa_atexit(cxa_handler_4
, &arg_1
, &dso_handle_1
));
171 ASSERT(0 == __cxa_atexit(cxa_handler_5
, &arg_1
, &dso_handle_1
));
172 ASSERT(0 == __cxa_atexit(cxa_handler_3
, &arg_2
, &dso_handle_2
));
173 ASSERT(0 == __cxa_atexit(cxa_handler_2
, &arg_3
, &dso_handle_3
));
175 __cxa_finalize(&dso_handle_1
);
176 __cxa_finalize(&dso_handle_2
);