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