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