1 /* $NetBSD: h_atexit.c,v 1.1 2011/01/12 19:44:08 pgoyette Exp $ */ 2 3 /*- 4 * Copyright (c) 2011 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code was contributed to The NetBSD Foundation by Jason R. Thorpe. 8 * 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. 17 * 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. 29 */ 30 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 $"); 35 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <signal.h> 39 #include <string.h> 40 #include <unistd.h> 41 42 extern int __cxa_atexit(void (*func)(void *), void *, void *); 43 extern void __cxa_finalize(void *); 44 45 static int dso_handle_1; 46 static int dso_handle_2; 47 static int dso_handle_3; 48 49 static int arg_1; 50 static int arg_2; 51 static int arg_3; 52 53 static int exiting_state; 54 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) 66 67 #define SUCCESS() \ 68 do { \ 69 write(STDOUT_FILENO, __func__, strlen(__func__)); \ 70 write(STDOUT_FILENO, "\n", 1); \ 71 } while (/*CONSTCOND*/0) 72 73 static void 74 my_abort(void) 75 { 76 77 kill(getpid(), SIGABRT); 78 /* NOTREACHED */ 79 } 80 81 static void 82 cxa_handler_5(void *arg) 83 { 84 static int cxa_handler_5_called; 85 86 ASSERT(arg == (void *)&arg_1); 87 ASSERT(cxa_handler_5_called == 0); 88 ASSERT(exiting_state == 5); 89 90 exiting_state--; 91 cxa_handler_5_called = 1; 92 SUCCESS(); 93 } 94 95 static void 96 cxa_handler_4(void *arg) 97 { 98 static int cxa_handler_4_called; 99 100 ASSERT(arg == (void *)&arg_1); 101 ASSERT(cxa_handler_4_called == 0); 102 ASSERT(exiting_state == 4); 103 104 exiting_state--; 105 cxa_handler_4_called = 1; 106 SUCCESS(); 107 } 108 109 static void 110 cxa_handler_3(void *arg) 111 { 112 static int cxa_handler_3_called; 113 114 ASSERT(arg == (void *)&arg_2); 115 ASSERT(cxa_handler_3_called == 0); 116 ASSERT(exiting_state == 3); 117 118 exiting_state--; 119 cxa_handler_3_called = 1; 120 SUCCESS(); 121 } 122 123 static void 124 cxa_handler_2(void *arg) 125 { 126 static int cxa_handler_2_called; 127 128 ASSERT(arg == (void *)&arg_3); 129 ASSERT(cxa_handler_2_called == 0); 130 ASSERT(exiting_state == 2); 131 132 exiting_state--; 133 cxa_handler_2_called = 1; 134 SUCCESS(); 135 } 136 137 static void 138 normal_handler_1(void) 139 { 140 static int normal_handler_1_called; 141 142 ASSERT(normal_handler_1_called == 0); 143 ASSERT(exiting_state == 1); 144 145 exiting_state--; 146 normal_handler_1_called = 1; 147 SUCCESS(); 148 } 149 150 static void 151 normal_handler_0(void) 152 { 153 static int normal_handler_0_called; 154 155 ASSERT(normal_handler_0_called == 0); 156 ASSERT(exiting_state == 0); 157 158 normal_handler_0_called = 1; 159 SUCCESS(); 160 } 161 162 int 163 main(int argc, char *argv[]) 164 { 165 166 exiting_state = 5; 167 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)); 174 175 __cxa_finalize(&dso_handle_1); 176 __cxa_finalize(&dso_handle_2); 177 exit(0); 178 } 179