1 /* $NetBSD: atexit.c,v 1.1 2003/03/01 04:20:54 thorpej Exp $ */
4 * Program to test atexit(3) and __cxa_atexit()/__cxa_finalize().
6 * Written by Jason R. Thorpe, February 2003.
16 extern int __cxa_atexit(void (*func
)(void *), void *, void *);
17 extern void __cxa_finalize(void *);
19 static int dso_handle_1
;
20 static int dso_handle_2
;
21 static int dso_handle_3
;
27 static int exiting_state
;
29 #define ASSERT(expr) \
32 write(STDERR_FILENO, __func__, strlen(__func__)); \
33 write(STDERR_FILENO, ": ", 2); \
34 write(STDERR_FILENO, __STRING(expr), \
35 strlen(__STRING(expr))); \
36 write(STDERR_FILENO, "\n", 1); \
39 } while (/*CONSTCOND*/0)
43 write(STDOUT_FILENO, __func__, strlen(__func__)); \
44 write(STDOUT_FILENO, "\n", 1); \
45 } while (/*CONSTCOND*/0)
56 cxa_handler_5(void *arg
)
58 static int cxa_handler_5_called
;
60 ASSERT(arg
== (void *)&arg_1
);
61 ASSERT(cxa_handler_5_called
== 0);
62 ASSERT(exiting_state
== 5);
65 cxa_handler_5_called
= 1;
70 cxa_handler_4(void *arg
)
72 static int cxa_handler_4_called
;
74 ASSERT(arg
== (void *)&arg_1
);
75 ASSERT(cxa_handler_4_called
== 0);
76 ASSERT(exiting_state
== 4);
79 cxa_handler_4_called
= 1;
84 cxa_handler_3(void *arg
)
86 static int cxa_handler_3_called
;
88 ASSERT(arg
== (void *)&arg_2
);
89 ASSERT(cxa_handler_3_called
== 0);
90 ASSERT(exiting_state
== 3);
93 cxa_handler_3_called
= 1;
98 cxa_handler_2(void *arg
)
100 static int cxa_handler_2_called
;
102 ASSERT(arg
== (void *)&arg_3
);
103 ASSERT(cxa_handler_2_called
== 0);
104 ASSERT(exiting_state
== 2);
107 cxa_handler_2_called
= 1;
112 normal_handler_1(void)
114 static int normal_handler_1_called
;
116 ASSERT(normal_handler_1_called
== 0);
117 ASSERT(exiting_state
== 1);
120 normal_handler_1_called
= 1;
125 normal_handler_0(void)
127 static int normal_handler_0_called
;
129 ASSERT(normal_handler_0_called
== 0);
130 ASSERT(exiting_state
== 0);
132 normal_handler_0_called
= 1;
137 main(int argc
, char *argv
[])
142 ASSERT(0 == atexit(normal_handler_0
));
143 ASSERT(0 == atexit(normal_handler_1
));
144 ASSERT(0 == __cxa_atexit(cxa_handler_4
, &arg_1
, &dso_handle_1
));
145 ASSERT(0 == __cxa_atexit(cxa_handler_5
, &arg_1
, &dso_handle_1
));
146 ASSERT(0 == __cxa_atexit(cxa_handler_3
, &arg_2
, &dso_handle_2
));
147 ASSERT(0 == __cxa_atexit(cxa_handler_2
, &arg_3
, &dso_handle_3
));
149 __cxa_finalize(&dso_handle_1
);
150 __cxa_finalize(&dso_handle_2
);