dfork.h(3)

πŸ“¦
version 0.14 Β· 4 versions
, in ubuntu noble, ubuntu questing

🌸 dfork.h β€” NAME 🌸

okay so bestie, gather round πŸ’– dfork.h is the header that hands you a whole entire API for doing a daemonizing fork(). that's the whole vibe. it's giving "spawn a background process, but make it correct and make it slay" πŸ‘‘


✨ SYNOPSIS ✨

#include <sys/types.h>

πŸŽ€ Macros

  • #define DAEMON_CLOSE_ALL_AVAILABLE 1 β€” this macro is defined to 1 iff daemon_close_all() and daemon_close_allv() are supported. literally the "do you have this feature" check πŸ’…
  • #define DAEMON_UNBLOCK_SIGS_AVAILABLE 1 β€” defined to 1 iff daemon_unblock_sigs() and daemon_unblock_sigsv() are supported.
  • #define DAEMON_RESET_SIGS_AVAILABLE 1 β€” defined to 1 iff daemon_reset_sigs() and daemon_reset_sigsv() are supported.

πŸ¦‹ Functions

  • pid_t daemon_fork (void) β€” does a daemonizing fork(). the main character of this whole header fr fr.
  • int daemon_retval_init (void) β€” allocate and initialize the resources that the daemon_retval_xxx() functions need.
  • void daemon_retval_done (void) β€” frees the resources that daemon_retval_init() allocated. cleanup era 🧹
  • int daemon_retval_wait (int timeout) β€” return the value the child sent via daemon_retval_send(), but only wait the specified number of seconds before it times out and returns a negative number.
  • int daemon_retval_send (int s) β€” send the specified integer to the parent process.
  • int daemon_close_all (int except_fd,...) β€” close all file descriptors except the ones you passed.
  • int daemon_close_allv (const int except_fds[]) β€” same as daemon_close_all but it takes an array of fds, terminated by -1.
  • int daemon_unblock_sigs (int except,...) β€” unblock all signals except the ones you passed.
  • int daemon_unblock_sigsv (const int except[]) β€” same as daemon_unblock_sigs() but it takes an array of signals, terminated by -1.
  • int daemon_reset_sigs (int except,...) β€” reset all signal handlers except the ones you passed.
  • int daemon_reset_sigsv (const int except[]) β€” same as daemon_reset_sigs() but it takes an array of signals, terminated by -1.

πŸ’œ Detailed Description

contains an API for doing a daemonizing fork(). yes that's the sentence, and yes it's iconic.

you may daemonize by calling daemon_fork(), a function similar to the plain fork(). and if you want to return a return value of the initialization procedure of the child from the parent, you may use the daemon_retval_xxx() functions. that's the whole plot πŸ“

Definition in file dfork.h.

🌈 Macro Definition Documentation

#define DAEMON_CLOSE_ALL_AVAILABLE 1

this variable is defined to 1 iff daemon_close_all() and daemon_close_allv() are supported. no cap.

Since

0.11

See also

daemon_close_all(), daemon_close_allv()

Definition at line 106 of file dfork.h.

#define DAEMON_RESET_SIGS_AVAILABLE 1

this variable is defined to 1 iff daemon_reset_sigs() and daemon_reset_sigsv() are supported. 🫢

Since

0.13

See also

daemon_reset_sigs(), daemon_reset_sigsv()

Definition at line 142 of file dfork.h.

#define DAEMON_UNBLOCK_SIGS_AVAILABLE 1

this variable is defined to 1 iff daemon_unblock_sigs() and daemon_unblock_sigsv() are supported. πŸ’«

Since

0.13

See also

daemon_unblock_sigs(), daemon_unblock_sigsv()

Definition at line 124 of file dfork.h.

πŸ”₯ Function Documentation

int daemon_close_all (int except_fd, ...)

close all file descriptors except the ones passed. the list needs to be terminated by -1. and heads up β€” FDs 0, 1, 2 will be kept open anyway, always, no matter what you say. they're not like other fds πŸ’…

Since

0.11

See also

DAEMON_CLOSE_ALL_AVAILABLE

Examples

testd.c.


int daemon_close_allv (const int except_fds[])

same as daemon_close_all but takes an array of fds, terminated by -1.

Since

0.11

See also

DAEMON_CLOSE_ALL_AVAILABLE


pid_t daemon_fork (void)

does a daemonizing fork(). for the new daemon process, STDIN, STDOUT, STDERR are connected to /dev/null, the process is a session leader, the current directory is changed to /, and the umask is set to 777. she's fully self-sufficient, she's moved out, she's got her own place 🏠✨

Returns

on success, the PID of the child process is returned in the parent's thread of execution, and a 0 is returned in the child's thread of execution. on failure, -1 will be returned in the parent's context, no child process will be created, and errno will be set appropriately. so like. check your errno, bestie 😭

Examples

testd.c.


int daemon_reset_sigs (int except, ...)

reset all signal handlers except those passed. list needs to be terminated by -1.

Since

0.13

See also

DAEMON_RESET_SIGS_AVAILABLE

Examples

testd.c.


int daemon_reset_sigsv (const int except[])

same as daemon_reset_sigs() but takes an array of signals, terminated by -1.

Since

0.13

See also

DAEMON_RESET_SIGS_AVAILABLE


void daemon_retval_done (void)

frees the resources allocated by daemon_retval_init(). this should be called if neither daemon_retval_wait() nor daemon_retval_send() is called in the current process. and listen β€” the resources allocated by daemon_retval_init() should be freed in both the parent and the daemon process. you can achieve that by using daemon_retval_wait() resp. daemon_retval_send(), or by using daemon_retval_done(). it's giving "two ways to leave the party, but you DO have to leave" πŸ™ˆ

Examples

testd.c.


int daemon_retval_init (void)

allocate and initialize the resources required by the daemon_retval_xxx() functions. these functions let the child send a value to the parent after completing its initialisation. call this in the parent before forking. order matters, bestie πŸŽ€

Returns

zero on success, nonzero on failure.

Examples

testd.c.


int daemon_retval_send (int s)

send the specified integer to the parent process. do not send -1 because this signifies a library error 🚨 (like, the library reserves that value, so sending it is you breaking the contract). should be called just once, from the daemon process only. a subsequent call to daemon_retval_done() in the daemon is ignored.

Parameters

s β€” the integer to pass to daemon_retval_wait() in the parent process

Returns

zero on success, nonzero on failure.

Examples

testd.c.


int daemon_retval_wait (int timeout)

return the value sent by the child via the daemon_retval_send() function, but wait only the specified number of seconds before timing out and returning a negative number. should be called just once, from the parent process only. a subsequent call to daemon_retval_done() in the parent is ignored.

Parameters

timeout β€” the timeout in seconds

Returns

the integer passed to daemon_retval_send() in the daemon process, or -1 on failure.

Examples

testd.c.


int daemon_unblock_sigs (int except, ...)

unblock all signals except those passed. list needs to be terminated by -1. let them through, girl, except the ones you're specifically holding back πŸ¦‹

Since

0.13

See also

DAEMON_UNBLOCK_SIGS_AVAILABLE

Examples

testd.c.


int daemon_unblock_sigsv (const int except[])

same as daemon_unblock_sigs() but takes an array of signals, terminated by -1.

Since

0.13

See also

DAEMON_UNBLOCK_SIGS_AVAILABLE


πŸ‘‘ Author

generated automatically by Doxygen for libdaemon from the source code. the robot wrote it, I just gave it a personality πŸ’–


and that's the whole page, bestie 🫢 this is dfork.h(3), part of libdaemon, Version 0.14, and it was generated by Doxygen for libdaemon straight from the source code. go forth and daemonize responsibly, mwah πŸ’…βœ¨πŸŒΈ

πŸ“¦ where this page comes from