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 atexit(). 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 (*vfuncp) (void); |
| 12 | |
| 13 | /* All functions call exit(), in order to test that exit functions can call |
| 14 | * exit() without screwing everything up. :) |
| 15 | */ |
| 16 | #define make_exitfunc(num) \ |
| 17 | __attribute__ ((__noreturn__)) static \ |
| 18 | void exitfunc##num(void) \ |
| 19 | { \ |
| 20 | printf("Executing exitfunc"#num".\n"); \ |
| 21 | exit(0); \ |
| 22 | } |
| 23 | make_exitfunc(0) |
| 24 | make_exitfunc(1) |
| 25 | make_exitfunc(2) |
| 26 | make_exitfunc(3) |
| 27 | make_exitfunc(4) |
| 28 | make_exitfunc(5) |
| 29 | make_exitfunc(6) |
| 30 | make_exitfunc(7) |
| 31 | make_exitfunc(8) |
| 32 | make_exitfunc(9) |
| 33 | make_exitfunc(10) |
| 34 | make_exitfunc(11) |
| 35 | make_exitfunc(12) |
| 36 | make_exitfunc(13) |
| 37 | make_exitfunc(14) |
| 38 | make_exitfunc(15) |
| 39 | make_exitfunc(16) |
| 40 | make_exitfunc(17) |
| 41 | make_exitfunc(18) |
| 42 | make_exitfunc(19) |
| 43 | make_exitfunc(20) |
| 44 | make_exitfunc(21) |
| 45 | make_exitfunc(22) |
| 46 | make_exitfunc(23) |
| 47 | make_exitfunc(24) |
| 48 | |
| 49 | static vfuncp func_table[] = |
| 50 | { |
| 51 | exitfunc0, exitfunc1, exitfunc2, exitfunc3, exitfunc4, |
| 52 | exitfunc5, exitfunc6, exitfunc7, exitfunc8, exitfunc9, |
| 53 | exitfunc10, exitfunc11, exitfunc12, exitfunc13, exitfunc14, |
| 54 | exitfunc15, exitfunc16, exitfunc17, exitfunc18, exitfunc19, |
| 55 | exitfunc20, exitfunc21, exitfunc22, exitfunc23, exitfunc24 |
| 56 | }; |
| 57 | |
| 58 | /* glibc dynamically adds exit functions, so it will keep adding until |
| 59 | * it runs out of memory! So this will limit the number of exit functions |
| 60 | * we add in the loop below. uClibc has a set limit (currently 20), so the |
| 61 | * loop will go until it can't add any more (so it should not hit this limit). |
| 62 | */ |
| 63 | #define ATEXIT_LIMIT 20 |
| 64 | |
| 65 | int |
| 66 | main ( void ) |
| 67 | { |
| 68 | int i = 0; |
| 69 | int count = 0; |
| 70 | int numfuncs = sizeof(func_table)/sizeof(vfuncp); |
| 71 | |
| 72 | /* loop until no more can be added */ |
| 73 | while(count < ATEXIT_LIMIT && atexit(func_table[i]) >= 0) { |
| 74 | printf("Registered exitfunc%d with atexit()\n", i); |
| 75 | count++; |
| 76 | i = (i+1) % numfuncs; |
| 77 | } |
| 78 | printf("%d functions registered with atexit.\n", count); |
| 79 | |
| 80 | return 0; |
| 81 | } |