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 status argument you gave to the last call to exit(3), and
  • the arg argument that you passed into on_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:

  • arg should 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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms