DAEMON(3)
๐ DAEMON(3) โ the "go run in the background bestie" page ๐
okay so this is a DAEMON(3) page from the Library Functions Manual, and honestly it's giving main character energy that refuses to be perceived. let's get into it ๐ซถ
โจ NAME โจ
daemon โ run in the background ๐
๐ LIBRARY ๐
Standard C Library (libc, -lc) โ the OG, the icon, the one you already linked against ๐
๐ง SYNOPSIS ๐ง
#include <stdlib.h>
int
daemon(int nochdir, int noclose);
int
daemonfd(int chdirfd, int nullfd);
๐ธ DESCRIPTION ๐ธ
so the daemon() function is for programs that literally want to detach themselves from the controlling terminal and vibe in the background as system daemons. no tether, no strings, no terminal holding them back โ periodt. โจ๐ฆ
if the argument nochdir is zero, daemon() changes the current working directory to the root (/). yes, the actual root. it's moving out and going home-home ๐
if the argument noclose is zero, daemon() redirects standard input, standard output, and standard error to /dev/null. it's the void and they're hopping in, no cap ๐ณ๏ธ๐
the daemonfd() function is equivalent to the daemon() function, except that the arguments are the descriptors for the current working directory and to the descriptor to /dev/null. same energy, more control โ iykyk ๐
- if
chdirfdis equal to(-1)the current working directory is not changed. like, we're staying put, respectfully ๐ - if
nullfdis equals to(-1)the redirection of standard input, standard output, and standard error is not closed. (yes, the original page literally says "is equals to" โ iconic typos only ๐)
๐ RETURN VALUES ๐
the daemon() and daemonfd() functions return the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error. slay when zero, sad when negative ๐ญ
๐ฅ ERRORS ๐ฅ
the daemon() and daemonfd() function may fail and set errno for any of the errors specified for the library functions fork(2), open(2), and setsid(2). go read those pages bestie, the lore is there ๐โจ
๐ SEE ALSO ๐
๐ HISTORY ๐
the daemon() function first appeared in 4.4BSD. the daemonfd() function first appeared in FreeBSD 12.0. ancient lore vs. newer lore, and both are iconic in their own way ๐ฐ๏ธ๐
๐ CAVEATS ๐
listen up, this part is important, not me being dramatic for once:
unless the noclose argument is non-zero, daemon() will close the first three file descriptors and redirect them to /dev/null. normally, these correspond to standard input, standard output, and standard error. however, if any of those file descriptors refer to something else, they will still be closed, resulting in incorrect behavior of the calling program. ๐ that's the vibe check failing, hard.
this can happen if any of standard input, standard output, or standard error have been closed before the program was run. so, yeah. ๐
programs using daemon() should therefore either call daemon() before opening any files or sockets, or verify that any file descriptors obtained have values greater than 2. do this and you're slaying; skip it and you're in the group chat asking why everything is broken ๐ญ
also โ thedaemon()function temporarily ignoresSIGHUPwhile callingsetsid(2), to prevent a parent session group leader's calls tofork(2)and then_exit(2)from prematurely terminating the child process. it's protective, it's thoughtful, it's mother ๐ซถ
okay that's the whole page, you're fully caught up bestie ๐ this one came from Debian, dated February 27, 2023 โ so, like, a slightly vintage drop but still absolutely eating. mwah ๐ ๐
๐ฆ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 1993 The Regents of the University of California. All rights reserved
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms