You can call _exit()
instead.
Within the Notes section of the man page:
The function
_exit()
is likeexit()
, but does not call any functions registered withatexit()
oron_exit()
.
This should avoid the "recursive" issue that is being warned about in the POSIX spec. If you are somehow able to guarantee that your "exit code changing" exit handler runs last, this should work perfectly, modulo the caveats also listed in the Notes:
Whether it flushes standard I/O buffers and removes temporary files created with
tmpfile(3)
is implementation-dependent. On the other hand,_exit()
does close open file descriptors, and this may cause an unknown delay, waiting for pending output to finish. If the delay is undesired, it may be useful to call functions liketcflush(3)
before calling_exit()
. Whether any pending I/O is canceled, and which pending I/O may be canceled upon_exit()
, is implementation-dependent.