1SD_LISTEN_FDS(3)                 sd_listen_fds                SD_LISTEN_FDS(3)
2
3
4

NAME

6       sd_listen_fds, SD_LISTEN_FDS_START - Check for file descriptors passed
7       by the system manager
8

SYNOPSIS

10       #include <systemd/sd-daemon.h>
11
12       #define SD_LISTEN_FDS_START 3
13
14       int sd_listen_fds(int unset_environment);
15

DESCRIPTION

17       sd_listen_fds() shall be called by a daemon to check for file
18       descriptors passed by the init system as part of the socket-based
19       activation logic.
20
21       If the unset_environment parameter is non-zero, sd_listen_fds() will
22       unset the $LISTEN_FDS and $LISTEN_PID environment variables before
23       returning (regardless of whether the function call itself succeeded or
24       not). Further calls to sd_listen_fds() will then fail, but the
25       variables are no longer inherited by child processes.
26
27       If a daemon receives more than one file descriptor, they will be passed
28       in the same order as configured in the systemd socket unit file (see
29       systemd.socket(5) for details). Nonetheless, it is recommended to
30       verify the correct socket types before using them. To simplify this
31       checking, the functions sd_is_fifo(3), sd_is_socket(3),
32       sd_is_socket_inet(3), sd_is_socket_unix(3) are provided. In order to
33       maximize flexibility, it is recommended to make these checks as loose
34       as possible without allowing incorrect setups. i.e. often, the actual
35       port number a socket is bound to matters little for the service to
36       work, hence it should not be verified. On the other hand, whether a
37       socket is a datagram or stream socket matters a lot for the most common
38       program logics and should be checked.
39
40       This function call will set the FD_CLOEXEC flag for all passed file
41       descriptors to avoid further inheritance to children of the calling
42       process.
43
44       If multiple socket units activate the same service the order of the
45       file descriptors passed to its main process is undefined. If additional
46       file descriptors have been passed to the service manager using
47       sd_pid_notify_with_fds(3)'s "FDSTORE=1" messages, these file
48       descriptors are passed last, in arbitrary order, and with duplicates
49       removed.
50

RETURN VALUE

52       On failure, this call returns a negative errno-style error code. If
53       $LISTEN_FDS/$LISTEN_PID was not set or was not correctly set for this
54       daemon and hence no file descriptors were received, 0 is returned.
55       Otherwise, the number of file descriptors passed is returned. The
56       application may find them starting with file descriptor
57       SD_LISTEN_FDS_START, i.e. file descriptor 3.
58

NOTES

60       These APIs are implemented as a shared library, which can be compiled
61       and linked to with the libsystemd pkg-config(1) file.
62
63       Internally, this function checks whether the $LISTEN_PID environment
64       variable equals the daemon PID. If not, it returns immediately.
65       Otherwise, it parses the number passed in the $LISTEN_FDS environment
66       variable, then sets the FD_CLOEXEC flag for the parsed number of file
67       descriptors starting from SD_LISTEN_FDS_START. Finally, it returns the
68       parsed number.
69

ENVIRONMENT

71       $LISTEN_PID, $LISTEN_FDS
72           Set by the init system for supervised processes that use
73           socket-based activation. This environment variable specifies the
74           data sd_listen_fds() parses. See above for details.
75

SEE ALSO

77       systemd(1), sd-daemon(3), sd_is_fifo(3), sd_is_socket(3),
78       sd_is_socket_inet(3), sd_is_socket_unix(3), daemon(7),
79       systemd.service(5), systemd.socket(5)
80
81
82
83systemd 219                                                   SD_LISTEN_FDS(3)
Impressum