6

I've come across at_quick_exit and quick_exit while going over stdlib.h and looking for functions that I haven't implemented.

I don't understand the point of having these two functions. Do they have any practical usage?

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
  • 1
    It can be safely used in a signal handler (assuming any registered exit functions are also signal safe). – Shawn Sep 06 '19 at 23:19
  • Still no clue, looks shared object related [glibc master - sourceware.org](https://sourceware.org/git/?p=glibc.git&a=search&h=refs%2Fheads%2Fmaster&st=grep&s=at_quick_exit) – David C. Rankin Sep 06 '19 at 23:21
  • 1
    I assume you're specifically wondering what the need for them is over the use of `at_exit` and `exit`? – Christian Gibbons Sep 06 '19 at 23:21
  • @Shawn: There is no provision that `quick_exit` can be used from a signal handler. – R.. GitHub STOP HELPING ICE Sep 06 '19 at 23:23
  • @R.. See section 7.14.1 paragraph 5 of C11. – Shawn Sep 06 '19 at 23:30
  • @Shawn: Ah, I missed that. – R.. GitHub STOP HELPING ICE Sep 06 '19 at 23:32
  • 2
    @Shawn: I can't find any language restricting what the `at_quick_exit` handlers can do if `quick_exit` is called from a signal handler, which is obviously wrong... Is this an omission or am I overlooking something? – R.. GitHub STOP HELPING ICE Sep 06 '19 at 23:36
  • @R.. the description of `quick_exit()` says *No functions registered by the atexit function or signal handlers registered by the signal function are called.* I misspoke thinking the callbacks have the same requirements as signal handlers given that extra detail. They can't do anything to raise a signal without getting into undefined behavior though. – Shawn Sep 06 '19 at 23:41
  • https://stackoverflow.com/questions/9758495/what-is-the-difference-between-stdquick-exit-and-stdabort-and-why-was-stdq/9759272#9759272 – Hans Passant Sep 06 '19 at 23:42
  • I can picture it being problematic, though - imagine a signal handler being called in the middle of a stdio function, that handler using `quick_exit()` with a callback that uses stdio functions... Anything non-reentrant could be an issue in that sort of scenario. – Shawn Sep 06 '19 at 23:49

2 Answers2

6

Basically it exists in C because of C++. The relevant document from WG 14 C standard committe can be found here.

The document was adapted from the paper accepted by the C++ standard. The idea behind quick_exit is to exit the program without canceling all threads and without executing destructors of static objects. C doesn't has language support for such things as "destructors" at all and the thread support library in C is almost nowhere implemented. The at_quick_exit and quick_exit functions have very little to no meaning at all in C.

In C there is a function _Exit that causes normal program termination to occur and control to be returned to the host environment, but is not required to flush open file descriptors, write unbuffered data, close open files, as opposed to exit(). Basically the at_quick_exit and quick_exit functions are facilities build to run custom user handles and then execute _Exit, while atexit is a facility to execute custom handlers upon calling exit().

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • `_exit` also prevents calling `atexit` recommended following a failed return from the `exec...` family of functions preventing UB if any of the defined `atexit` functions are invalidated by the failed call. – David C. Rankin Sep 06 '19 at 23:40
0

They essentially have no practical usage. The intent seems to be that a function that may have significant nontrivial atexit handlers could use quick_exit to exit with just a minimal subset of such handlers (that it defines by calling at_quick_exit) being called, under conditions where calling all the atexit handlers may not be safe. It may also be called from a signal handler, but it doesn't seem like there'd be anything meaningful you could do from the at_quick_exit handlers in that case.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Might be worth mentioning that `quick_exit` is not required to flush buffers, close open streams, or remove temporary files (but the system may still choose to do so) unlike `exit`. – Christian Gibbons Sep 06 '19 at 23:29
  • *... or the signal handler calls any function in the standard library other than the abort function, the _Exit function, the quick_exit function, ...* – Shawn Sep 06 '19 at 23:34