teston_exit.c 2.4 KB

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