h_atexit.c revision 274626
1102050Stjr/* $NetBSD: h_atexit.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */ 2128004Stjr 3102050Stjr/* 4102050Stjr * Copyright (c) 2008 The NetBSD Foundation, Inc. 5227753Stheraven * All rights reserved. 6227753Stheraven * 7227753Stheraven * Redistribution and use in source and binary forms, with or without 8227753Stheraven * modification, are permitted provided that the following conditions 9227753Stheraven * are met: 10102050Stjr * 1. Redistributions of source code must retain the above copyright 11102050Stjr * notice, this list of conditions and the following disclaimer. 12102050Stjr * 2. Redistributions in binary form must reproduce the above copyright 13102050Stjr * notice, this list of conditions and the following disclaimer in the 14102050Stjr * documentation and/or other materials provided with the distribution. 15102050Stjr * 16102050Stjr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17102050Stjr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18102050Stjr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19102050Stjr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20102050Stjr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21102050Stjr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22102050Stjr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23102050Stjr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24102050Stjr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25102050Stjr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26102050Stjr * POSSIBILITY OF SUCH DAMAGE. 27102050Stjr */ 28102050Stjr 29102050Stjr/* 30102050Stjr * Program to test atexit(3) and __cxa_atexit()/__cxa_finalize(). 31102050Stjr * 32102050Stjr * Written by Jason R. Thorpe, February 2003. 33102050Stjr * Public domain. 34102050Stjr */ 35102050Stjr 36129153Stjr#include <sys/cdefs.h> 37102050Stjr__COPYRIGHT("@(#) Copyright (c) 2008\ 38102050Stjr The NetBSD Foundation, inc. All rights reserved."); 39227753Stheraven__RCSID("$NetBSD: h_atexit.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $"); 40227753Stheraven 41227753Stheraven#include <stdio.h> 42227753Stheraven#include <stdlib.h> 43227753Stheraven#include <signal.h> 44227753Stheraven#include <string.h> 45128004Stjr#include <unistd.h> 46102050Stjr 47227753Stheravenextern int __cxa_atexit(void (*func)(void *), void *, void *); 48102050Stjrextern void __cxa_finalize(void *); 49 50#ifdef __FreeBSD__ 51/* 52 * See comments in ../../lib/libc/stdlib/h_atexit.c about the deviation 53 * between FreeBSD and NetBSD with this helper program 54 */ 55static void *dso_handle_1 = (void *)1; 56static void *dso_handle_2 = (void *)2; 57static void *dso_handle_3 = (void *)3; 58#else 59static int dso_handle_1; 60static int dso_handle_2; 61static int dso_handle_3; 62#endif 63 64static int arg_1; 65static int arg_2; 66static int arg_3; 67 68static int exiting_state; 69 70#define ASSERT(expr) \ 71do { \ 72 if ((expr) == 0) { \ 73 write(STDERR_FILENO, __func__, strlen(__func__)); \ 74 write(STDERR_FILENO, ": ", 2); \ 75 write(STDERR_FILENO, __STRING(expr), \ 76 strlen(__STRING(expr))); \ 77 write(STDERR_FILENO, "\n", 1); \ 78 my_abort(); \ 79 } \ 80} while (/*CONSTCOND*/0) 81 82#define SUCCESS() \ 83do { \ 84 write(STDOUT_FILENO, __func__, strlen(__func__)); \ 85 write(STDOUT_FILENO, "\n", 1); \ 86} while (/*CONSTCOND*/0) 87 88static void 89my_abort(void) 90{ 91 92 kill(getpid(), SIGABRT); 93 /* NOTREACHED */ 94} 95 96static void 97cxa_handler_5(void *arg) 98{ 99 static int cxa_handler_5_called; 100 101 ASSERT(arg == (void *)&arg_1); 102 ASSERT(cxa_handler_5_called == 0); 103 ASSERT(exiting_state == 5); 104 105 exiting_state--; 106 cxa_handler_5_called = 1; 107 SUCCESS(); 108} 109 110static void 111cxa_handler_4(void *arg) 112{ 113 static int cxa_handler_4_called; 114 115 ASSERT(arg == (void *)&arg_1); 116 ASSERT(cxa_handler_4_called == 0); 117 ASSERT(exiting_state == 4); 118 119 exiting_state--; 120 cxa_handler_4_called = 1; 121 SUCCESS(); 122} 123 124static void 125cxa_handler_3(void *arg) 126{ 127 static int cxa_handler_3_called; 128 129 ASSERT(arg == (void *)&arg_2); 130 ASSERT(cxa_handler_3_called == 0); 131 ASSERT(exiting_state == 3); 132 133 exiting_state--; 134 cxa_handler_3_called = 1; 135 SUCCESS(); 136} 137 138static void 139cxa_handler_2(void *arg) 140{ 141 static int cxa_handler_2_called; 142 143 ASSERT(arg == (void *)&arg_3); 144 ASSERT(cxa_handler_2_called == 0); 145 ASSERT(exiting_state == 2); 146 147 exiting_state--; 148 cxa_handler_2_called = 1; 149 SUCCESS(); 150} 151 152static void 153normal_handler_1(void) 154{ 155 static int normal_handler_1_called; 156 157 ASSERT(normal_handler_1_called == 0); 158 ASSERT(exiting_state == 1); 159 160 exiting_state--; 161 normal_handler_1_called = 1; 162 SUCCESS(); 163} 164 165static void 166normal_handler_0(void) 167{ 168 static int normal_handler_0_called; 169 170 ASSERT(normal_handler_0_called == 0); 171 ASSERT(exiting_state == 0); 172 173 normal_handler_0_called = 1; 174 SUCCESS(); 175} 176 177int 178main(int argc, char *argv[]) 179{ 180 181 exiting_state = 5; 182 183#ifdef __FreeBSD__ 184 ASSERT(0 == atexit(normal_handler_0)); 185 ASSERT(0 == atexit(normal_handler_1)); 186 ASSERT(0 == __cxa_atexit(cxa_handler_4, &arg_1, dso_handle_1)); 187 ASSERT(0 == __cxa_atexit(cxa_handler_5, &arg_1, dso_handle_1)); 188 ASSERT(0 == __cxa_atexit(cxa_handler_3, &arg_2, dso_handle_2)); 189 ASSERT(0 == __cxa_atexit(cxa_handler_2, &arg_3, dso_handle_3)); 190 191 __cxa_finalize(dso_handle_1); 192 __cxa_finalize(dso_handle_2); 193#else 194 ASSERT(0 == __cxa_atexit(cxa_handler_4, &arg_1, &dso_handle_1)); 195 ASSERT(0 == __cxa_atexit(cxa_handler_5, &arg_1, &dso_handle_1)); 196 ASSERT(0 == __cxa_atexit(cxa_handler_3, &arg_2, &dso_handle_2)); 197 ASSERT(0 == __cxa_atexit(cxa_handler_2, &arg_3, &dso_handle_3)); 198 199 __cxa_finalize(&dso_handle_1); 200 __cxa_finalize(&dso_handle_2); 201#endif 202 exit(0); 203} 204