testatexit.c 2.1 KB

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