lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 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) |
| 7 | */ |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | |
| 11 | typedef void (*efuncp) (int, void *); |
| 12 | |
| 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. |
| 16 | */ |
| 17 | #define make_exitfunc(num) \ |
| 18 | __attribute__ ((__noreturn__)) static \ |
| 19 | void exitfunc##num(int status, void *arg) \ |
| 20 | { \ |
| 21 | printf("Executing exitfunc"#num" (status=%d, arg=%lu)\n", status, (unsigned long)arg); \ |
| 22 | exit((unsigned long)arg); \ |
| 23 | } |
| 24 | make_exitfunc(0) |
| 25 | make_exitfunc(1) |
| 26 | make_exitfunc(2) |
| 27 | make_exitfunc(3) |
| 28 | make_exitfunc(4) |
| 29 | make_exitfunc(5) |
| 30 | make_exitfunc(6) |
| 31 | make_exitfunc(7) |
| 32 | make_exitfunc(8) |
| 33 | make_exitfunc(9) |
| 34 | make_exitfunc(10) |
| 35 | make_exitfunc(11) |
| 36 | make_exitfunc(12) |
| 37 | make_exitfunc(13) |
| 38 | make_exitfunc(14) |
| 39 | make_exitfunc(15) |
| 40 | make_exitfunc(16) |
| 41 | make_exitfunc(17) |
| 42 | make_exitfunc(18) |
| 43 | make_exitfunc(19) |
| 44 | make_exitfunc(20) |
| 45 | make_exitfunc(21) |
| 46 | make_exitfunc(22) |
| 47 | make_exitfunc(23) |
| 48 | make_exitfunc(24) |
| 49 | |
| 50 | static efuncp func_table[] = |
| 51 | { |
| 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 |
| 57 | }; |
| 58 | |
| 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). |
| 63 | */ |
| 64 | #define ON_EXIT_LIMIT 20 |
| 65 | |
| 66 | int |
| 67 | main ( void ) |
| 68 | { |
| 69 | int i = 0; |
| 70 | unsigned long count = 0; |
| 71 | int numfuncs = sizeof(func_table)/sizeof(efuncp); |
| 72 | |
| 73 | /* loop until no more can be added */ |
| 74 | while(count < ON_EXIT_LIMIT && on_exit(func_table[i], (void *)count) >= 0) { |
| 75 | count++; |
| 76 | printf("Registered exitfunc%d with on_exit()\n", i); |
| 77 | i = (i+1) % numfuncs; |
| 78 | } |
| 79 | printf("%lu functions registered with on_exit.\n", count); |
| 80 | exit(count); |
| 81 | } |
| 82 | |