---
name: "dfork.h"
section: "3"
platform: "linux"
package: "libdaemon-doc"
version: "0.14-7.1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/dfork.h"
---

# dfork.h(3)

Contains an API for doing a daemonizing fork().

## NAME

dfork.h - Contains an API for doing a daemonizing fork().

## SYNOPSIS

`#include <sys/types.h>`\

### Macros

\#define **DAEMON\_CLOSE\_ALL\_AVAILABLE**   1\
 This variable is defined to 1 iff **daemon\_close\_all()** and **daemon\_close\_allv()** are supported.  #define **DAEMON\_UNBLOCK\_SIGS\_AVAILABLE**   1\
 This variable is defined to 1 iff **daemon\_unblock\_sigs()** and **daemon\_unblock\_sigsv()** are supported.  #define **DAEMON\_RESET\_SIGS\_AVAILABLE**   1\
 This variable is defined to 1 iff **daemon\_reset\_sigs()** and **daemon\_reset\_sigsv()** are supported.

### Functions

pid\_t **daemon\_fork** (void)\
 Does a daemonizing fork().  int **daemon\_retval\_init** (void)\
 Allocate and initialize resources required by the daemon\_retval\_xxx() functions.  void **daemon\_retval\_done** (void)\
 Frees the resources allocated by **daemon\_retval\_init()**.  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.  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 those passed.  int **daemon\_close\_allv** (const int except\_fds\[\])\
 Same as daemon\_close\_all but takes an array of fds, terminated by -1.  int **daemon\_unblock\_sigs** (int except,...)\
 Unblock all signals except those passed.  int **daemon\_unblock\_sigsv** (const int except\[\])\
 Same as **daemon\_unblock\_sigs()** but takes an array of signals, terminated by -1.  int **daemon\_reset\_sigs** (int except,...)\
 Reset all signal handlers except those passed.  int **daemon\_reset\_sigsv** (const int except\[\])\
 Same as **daemon\_reset\_sigs()** but takes an array of signals, terminated by -1.

## Detailed Description

Contains an API for doing a daemonizing fork().

You may daemonize by calling **daemon\_fork()**, a function similar to the plain fork(). 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.

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.

**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 those passed. List needs to be terminated by -1. FDs 0, 1, 2 will be kept open anyway.

**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 /, the umask is set to 777.

**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.

**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. The resources allocated by **daemon\_retval\_init()** should be freed in both parent and daemon process. This may be achieved by using **daemon\_retval\_wait()** resp. **daemon\_retval\_send()**, or by using **daemon\_retval\_done()**.

**Examples** **testd.c**.

### int daemon\_retval\_init (void)

Allocate and initialize resources required by the daemon\_retval\_xxx() functions. These functions allow the child to send a value to the parent after completing its initialisation. Call this in the parent before forking.

**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. 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* Thetimeout in seconds

**Returns**

The integer passed **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.

**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.
