blob: 01874fdde66b7b0e624a18de18397d4b7f6dce2a [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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
11typedef 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 \
18void exitfunc##num(void) \
19{ \
20 printf("Executing exitfunc"#num".\n"); \
21 exit(0); \
22}
23make_exitfunc(0)
24make_exitfunc(1)
25make_exitfunc(2)
26make_exitfunc(3)
27make_exitfunc(4)
28make_exitfunc(5)
29make_exitfunc(6)
30make_exitfunc(7)
31make_exitfunc(8)
32make_exitfunc(9)
33make_exitfunc(10)
34make_exitfunc(11)
35make_exitfunc(12)
36make_exitfunc(13)
37make_exitfunc(14)
38make_exitfunc(15)
39make_exitfunc(16)
40make_exitfunc(17)
41make_exitfunc(18)
42make_exitfunc(19)
43make_exitfunc(20)
44make_exitfunc(21)
45make_exitfunc(22)
46make_exitfunc(23)
47make_exitfunc(24)
48
49static 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
65int
66main ( 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}