c - Is it possible to change the exit code in a function registered with atexit()? -
the man page atexit(3)
says following:
posix.1-2001 says result of calling
exit(3)
more once (i.e., callingexit(3)
within function registered usingatexit()
) undefined. on systems (but not linux), can result in infinite recursion; portable programs should not invokeexit(3)
inside function registered usingatexit()
.
however, i'm interested in modifying exit code in finalizer program. way i've managed calling exit()
within finalization function, man page explicitly warns against that.
is there practical danger against doing this? there implementations approach might cause problems? better, there way of doing this?
you can call _exit()
instead.
within notes section of man page:
the function
_exit()
exit()
, not call functions registeredatexit()
oron_exit()
.
this should avoid "recursive" issue being warned in posix spec. if somehow able guarantee "exit code changing" exit handler runs last, should work perfectly, modulo caveats listed in notes:
whether flushes standard i/o buffers , removes temporary files created
tmpfile(3)
implementation-dependent. on other hand,_exit()
close open file descriptors, , may cause unknown delay, waiting pending output finish. if delay undesired, may useful call functionstcflush(3)
before calling_exit()
. whether pending i/o canceled, , pending i/o may canceled upon_exit()
, implementation-dependent.
Comments
Post a Comment