on_exit(3)
π on_exit(3) β the "run this when we log off" bestie π
okay so this is a page from the Library Functions Manual, and it's all about on_exit, a lil function that lets you β¨schedule a callbackβ¨ for the exact moment your process says bye-bye. it's giving "leave a note before you head out" energy, and honestly? iconic. π«Ά
πΈ LIBRARY πΈ
Standard C library (libc, -lc) β yeah she lives in the classic place, no cap.
β¨ SYNOPSIS β¨
#include <stdlib.h>
int on_exit(typeof(void (int, void *)) *function, void *arg);
π Feature Test Macro Requirements for glibc π
(peep feature_test_macros(7) if you're like "...macro? bestie what")
on_exit():
- Since glibc 2.19:
_DEFAULT_SOURCE
- glibc 2.19 and earlier:
_BSD_SOURCE || _SVID_SOURCE
so like, depending on which glibc era you're in, you gotta define a different macro to even see her. vintage behavior fr fr. π
π DESCRIPTION π
The on_exit() function registers the function you hand it, and that function gets called at normal process termination β meaning it runs whether you go out through exit(3) or through a return from your program's main(). either way, she's there. main character energy. π
When she runs, your function gets passed:
- the
statusargument you gave to the last call toexit(3), and - the
argargument that you passed intoon_exit().
The same function may be registered multiple times, and girl is thorough: it gets called once for each registration. no shortcuts, no deduplication, we respect the hustle. π₯
When a child process is created via fork(2), it inherits copies of its parent's registrations β like, the whole vibe gets duplicated. but upon a successful call to one of the exec(3) functions, all registrations are removed. boom. gone. π
π RETURN VALUE π
on_exit() returns the value 0 if successful. otherwise, it returns a nonzero value. that's it, that's the whole story. simple girl, simple contract. π
π¦ ATTRIBUTES π¦
For an explanation of the terms used in this section, see attributes(7).
- Interface:
on_exit() - Attribute: Thread safety
- Value:
MT-Safe
so she's MT-Safe, which lowkey means she plays nice with threads. very demure, very mindful. π§
β STANDARDS β
None.
not in any standard, bestie. she's a lovable rogue. π
πΈ HISTORY πΈ
SunOS 4, glibc. She was Removed in Solaris (SunOS 5). and the official advice? use the standard atexit(3) instead. like, they literally said "get a standard one, girl." π
π CAVEATS π
β οΈ listen up because this one MATTERS β οΈ
By the time function is executed, stack (auto) variables may already have gone out of scope. so:
argshould not be a pointer to a stack variable. β- it may, however, be a pointer to a heap variable or a global variable. β
if you hand her a dangling stack pointer you're gonna have a bad time, and not the fun kind. π«
π SEE ALSO π
and that's the whole page, bestie β this was on_exit(3) from Linux man-pages 6.20, dated 2026-06-05. go register your cleanup function, protect those heap pointers, and slay. πβ¨π
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- Copyright 1993, David Metcalfe <david@prism.demon.co.uk>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms