DAEMON(3)

๐Ÿ“ฆ
version 15.1 ยท 2 versions
, in freebsd 15.1

๐Ÿ’– 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 chdirfd is equal to (-1) the current working directory is not changed. like, we're staying put, respectfully ๐Ÿ™ˆ
  • if nullfd is 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 โ€” the daemon() function temporarily ignores SIGHUP while calling setsid(2), to prevent a parent session group leader's calls to fork(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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms