2 * This test program will register the maximum number of exit functions
3 * with on_exit(). When this program exits, each exit function should get
4 * called in the reverse order in which it was registered. (If the system
5 * supports more than 25 exit functions, the function names will loop, but
6 * the effect will be the same. Feel free to add more functions if desired)
11 typedef void (*efuncp) (int, void *);
13 /* All functions call exit(), in order to test that exit functions can call
14 * exit() without screwing everything up. The value passed in through arg gets
15 * used as the next exit status.
17 #define make_exitfunc(num) \
18 __attribute__ ((__noreturn__)) static \
19 void exitfunc##num(int status, void *arg) \
21 printf("Executing exitfunc"#num" (status=%d, arg=%lu)\n", status, (unsigned long)arg); \
22 exit((unsigned long)arg); \
50 static efuncp func_table[] =
52 exitfunc0, exitfunc1, exitfunc2, exitfunc3, exitfunc4,
53 exitfunc5, exitfunc6, exitfunc7, exitfunc8, exitfunc9,
54 exitfunc10, exitfunc11, exitfunc12, exitfunc13, exitfunc14,
55 exitfunc15, exitfunc16, exitfunc17, exitfunc18, exitfunc19,
56 exitfunc20, exitfunc21, exitfunc22, exitfunc23, exitfunc24
59 /* glibc dynamically adds exit functions, so it will keep adding until
60 * it runs out of memory! So this will limit the number of exit functions
61 * we add in the loop below. uClibc has a set limit (currently 20), so the
62 * loop will go until it can't add any more (so it should not hit this limit).
64 #define ON_EXIT_LIMIT 20
70 unsigned long count = 0;
71 int numfuncs = sizeof(func_table)/sizeof(efuncp);
73 /* loop until no more can be added */
74 while(count < ON_EXIT_LIMIT && on_exit(func_table[i], (void *)count) >= 0) {
76 printf("Registered exitfunc%d with on_exit()\n", i);
79 printf("%lu functions registered with on_exit.\n", count);