etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libc / stdlib / h_atexit.c
blobb2f91c4c5fe2c93d77bc6a0ceca8385975a9fa91
1 /* $NetBSD: h_atexit.c,v 1.1 2011/01/12 19:44:08 pgoyette Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
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
11 * are met:
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 $");
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <signal.h>
39 #include <string.h>
40 #include <unistd.h>
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;
49 static int arg_1;
50 static int arg_2;
51 static int arg_3;
53 static int exiting_state;
55 #define ASSERT(expr) \
56 do { \
57 if ((expr) == 0) { \
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); \
63 my_abort(); \
64 } \
65 } while (/*CONSTCOND*/0)
67 #define SUCCESS() \
68 do { \
69 write(STDOUT_FILENO, __func__, strlen(__func__)); \
70 write(STDOUT_FILENO, "\n", 1); \
71 } while (/*CONSTCOND*/0)
73 static void
74 my_abort(void)
77 kill(getpid(), SIGABRT);
78 /* NOTREACHED */
81 static void
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);
90 exiting_state--;
91 cxa_handler_5_called = 1;
92 SUCCESS();
95 static void
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);
104 exiting_state--;
105 cxa_handler_4_called = 1;
106 SUCCESS();
109 static void
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);
118 exiting_state--;
119 cxa_handler_3_called = 1;
120 SUCCESS();
123 static void
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);
132 exiting_state--;
133 cxa_handler_2_called = 1;
134 SUCCESS();
137 static void
138 normal_handler_1(void)
140 static int normal_handler_1_called;
142 ASSERT(normal_handler_1_called == 0);
143 ASSERT(exiting_state == 1);
145 exiting_state--;
146 normal_handler_1_called = 1;
147 SUCCESS();
150 static void
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;
159 SUCCESS();
163 main(int argc, char *argv[])
166 exiting_state = 5;
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);
177 exit(0);