Home | History | Annotate | Line # | Download | only in stdlib
      1  1.1  pgoyette /* $NetBSD: h_atexit.c,v 1.1 2011/01/12 19:44:08 pgoyette Exp $ */
      2  1.1  pgoyette 
      3  1.1  pgoyette /*-
      4  1.1  pgoyette  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  1.1  pgoyette  * All rights reserved.
      6  1.1  pgoyette  *
      7  1.1  pgoyette  * This code was contributed to The NetBSD Foundation by Jason R. Thorpe.
      8  1.1  pgoyette  *
      9  1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     10  1.1  pgoyette  * modification, are permitted provided that the following conditions
     11  1.1  pgoyette  * are met:
     12  1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     13  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     14  1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     17  1.1  pgoyette  *
     18  1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  1.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  1.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  1.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  1.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  1.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  1.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  1.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  1.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  1.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  1.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     29  1.1  pgoyette  */
     30  1.1  pgoyette 
     31  1.1  pgoyette #include <sys/cdefs.h>
     32  1.1  pgoyette __COPYRIGHT("@(#) Copyright (c) 2011\
     33  1.1  pgoyette  The NetBSD Foundation, inc. All rights reserved.");
     34  1.1  pgoyette __RCSID("$NetBSD: h_atexit.c,v 1.1 2011/01/12 19:44:08 pgoyette Exp $");
     35  1.1  pgoyette 
     36  1.1  pgoyette #include <stdio.h>
     37  1.1  pgoyette #include <stdlib.h>
     38  1.1  pgoyette #include <signal.h>
     39  1.1  pgoyette #include <string.h>
     40  1.1  pgoyette #include <unistd.h>
     41  1.1  pgoyette 
     42  1.1  pgoyette extern int __cxa_atexit(void (*func)(void *), void *, void *);
     43  1.1  pgoyette extern void __cxa_finalize(void *);
     44  1.1  pgoyette 
     45  1.1  pgoyette static int dso_handle_1;
     46  1.1  pgoyette static int dso_handle_2;
     47  1.1  pgoyette static int dso_handle_3;
     48  1.1  pgoyette 
     49  1.1  pgoyette static int arg_1;
     50  1.1  pgoyette static int arg_2;
     51  1.1  pgoyette static int arg_3;
     52  1.1  pgoyette 
     53  1.1  pgoyette static int exiting_state;
     54  1.1  pgoyette 
     55  1.1  pgoyette #define	ASSERT(expr)							\
     56  1.1  pgoyette do {									\
     57  1.1  pgoyette 	if ((expr) == 0) {						\
     58  1.1  pgoyette 		write(STDERR_FILENO, __func__, strlen(__func__));	\
     59  1.1  pgoyette 		write(STDERR_FILENO, ": ", 2);				\
     60  1.1  pgoyette 		write(STDERR_FILENO, __STRING(expr),			\
     61  1.1  pgoyette 		      strlen(__STRING(expr)));				\
     62  1.1  pgoyette 		write(STDERR_FILENO, "\n", 1);				\
     63  1.1  pgoyette 		my_abort();						\
     64  1.1  pgoyette 	}								\
     65  1.1  pgoyette } while (/*CONSTCOND*/0)
     66  1.1  pgoyette 
     67  1.1  pgoyette #define	SUCCESS()							\
     68  1.1  pgoyette do {									\
     69  1.1  pgoyette 	write(STDOUT_FILENO, __func__, strlen(__func__));		\
     70  1.1  pgoyette 	write(STDOUT_FILENO, "\n", 1);					\
     71  1.1  pgoyette } while (/*CONSTCOND*/0)
     72  1.1  pgoyette 
     73  1.1  pgoyette static void
     74  1.1  pgoyette my_abort(void)
     75  1.1  pgoyette {
     76  1.1  pgoyette 
     77  1.1  pgoyette 	kill(getpid(), SIGABRT);
     78  1.1  pgoyette 	/* NOTREACHED */
     79  1.1  pgoyette }
     80  1.1  pgoyette 
     81  1.1  pgoyette static void
     82  1.1  pgoyette cxa_handler_5(void *arg)
     83  1.1  pgoyette {
     84  1.1  pgoyette 	static int cxa_handler_5_called;
     85  1.1  pgoyette 
     86  1.1  pgoyette 	ASSERT(arg == (void *)&arg_1);
     87  1.1  pgoyette 	ASSERT(cxa_handler_5_called == 0);
     88  1.1  pgoyette 	ASSERT(exiting_state == 5);
     89  1.1  pgoyette 
     90  1.1  pgoyette 	exiting_state--;
     91  1.1  pgoyette 	cxa_handler_5_called = 1;
     92  1.1  pgoyette 	SUCCESS();
     93  1.1  pgoyette }
     94  1.1  pgoyette 
     95  1.1  pgoyette static void
     96  1.1  pgoyette cxa_handler_4(void *arg)
     97  1.1  pgoyette {
     98  1.1  pgoyette 	static int cxa_handler_4_called;
     99  1.1  pgoyette 
    100  1.1  pgoyette 	ASSERT(arg == (void *)&arg_1);
    101  1.1  pgoyette 	ASSERT(cxa_handler_4_called == 0);
    102  1.1  pgoyette 	ASSERT(exiting_state == 4);
    103  1.1  pgoyette 
    104  1.1  pgoyette 	exiting_state--;
    105  1.1  pgoyette 	cxa_handler_4_called = 1;
    106  1.1  pgoyette 	SUCCESS();
    107  1.1  pgoyette }
    108  1.1  pgoyette 
    109  1.1  pgoyette static void
    110  1.1  pgoyette cxa_handler_3(void *arg)
    111  1.1  pgoyette {
    112  1.1  pgoyette 	static int cxa_handler_3_called;
    113  1.1  pgoyette 
    114  1.1  pgoyette 	ASSERT(arg == (void *)&arg_2);
    115  1.1  pgoyette 	ASSERT(cxa_handler_3_called == 0);
    116  1.1  pgoyette 	ASSERT(exiting_state == 3);
    117  1.1  pgoyette 
    118  1.1  pgoyette 	exiting_state--;
    119  1.1  pgoyette 	cxa_handler_3_called = 1;
    120  1.1  pgoyette 	SUCCESS();
    121  1.1  pgoyette }
    122  1.1  pgoyette 
    123  1.1  pgoyette static void
    124  1.1  pgoyette cxa_handler_2(void *arg)
    125  1.1  pgoyette {
    126  1.1  pgoyette 	static int cxa_handler_2_called;
    127  1.1  pgoyette 
    128  1.1  pgoyette 	ASSERT(arg == (void *)&arg_3);
    129  1.1  pgoyette 	ASSERT(cxa_handler_2_called == 0);
    130  1.1  pgoyette 	ASSERT(exiting_state == 2);
    131  1.1  pgoyette 
    132  1.1  pgoyette 	exiting_state--;
    133  1.1  pgoyette 	cxa_handler_2_called = 1;
    134  1.1  pgoyette 	SUCCESS();
    135  1.1  pgoyette }
    136  1.1  pgoyette 
    137  1.1  pgoyette static void
    138  1.1  pgoyette normal_handler_1(void)
    139  1.1  pgoyette {
    140  1.1  pgoyette 	static int normal_handler_1_called;
    141  1.1  pgoyette 
    142  1.1  pgoyette 	ASSERT(normal_handler_1_called == 0);
    143  1.1  pgoyette 	ASSERT(exiting_state == 1);
    144  1.1  pgoyette 
    145  1.1  pgoyette 	exiting_state--;
    146  1.1  pgoyette 	normal_handler_1_called = 1;
    147  1.1  pgoyette 	SUCCESS();
    148  1.1  pgoyette }
    149  1.1  pgoyette 
    150  1.1  pgoyette static void
    151  1.1  pgoyette normal_handler_0(void)
    152  1.1  pgoyette {
    153  1.1  pgoyette 	static int normal_handler_0_called;
    154  1.1  pgoyette 
    155  1.1  pgoyette 	ASSERT(normal_handler_0_called == 0);
    156  1.1  pgoyette 	ASSERT(exiting_state == 0);
    157  1.1  pgoyette 
    158  1.1  pgoyette 	normal_handler_0_called = 1;
    159  1.1  pgoyette 	SUCCESS();
    160  1.1  pgoyette }
    161  1.1  pgoyette 
    162  1.1  pgoyette int
    163  1.1  pgoyette main(int argc, char *argv[])
    164  1.1  pgoyette {
    165  1.1  pgoyette 
    166  1.1  pgoyette 	exiting_state = 5;
    167  1.1  pgoyette 
    168  1.1  pgoyette 	ASSERT(0 == atexit(normal_handler_0));
    169  1.1  pgoyette 	ASSERT(0 == atexit(normal_handler_1));
    170  1.1  pgoyette 	ASSERT(0 == __cxa_atexit(cxa_handler_4, &arg_1, &dso_handle_1));
    171  1.1  pgoyette 	ASSERT(0 == __cxa_atexit(cxa_handler_5, &arg_1, &dso_handle_1));
    172  1.1  pgoyette 	ASSERT(0 == __cxa_atexit(cxa_handler_3, &arg_2, &dso_handle_2));
    173  1.1  pgoyette 	ASSERT(0 == __cxa_atexit(cxa_handler_2, &arg_3, &dso_handle_3));
    174  1.1  pgoyette 
    175  1.1  pgoyette 	__cxa_finalize(&dso_handle_1);
    176  1.1  pgoyette 	__cxa_finalize(&dso_handle_2);
    177  1.1  pgoyette 	exit(0);
    178  1.1  pgoyette }
    179