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

NAME

6       sd_listen_fds, sd_listen_fds_with_names, SD_LISTEN_FDS_START - Check
7       for file descriptors passed 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
16       int sd_listen_fds_with_names(int unset_environment, char*** names);
17

DESCRIPTION

19       sd_listen_fds() may be invoked by a daemon to check for file
20       descriptors passed by the service manager as part of the socket-based
21       activation logic. It returns the number of received file descriptors.
22       If no file descriptors have been received, zero is returned. The first
23       file descriptor may be found at file descriptor number 3 (i.e.
24       SD_LISTEN_FDS_START), the remaining descriptors follow at 4, 5, 6, ...,
25       if any.
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) — if there's only one such file (see
30       below). Nonetheless, it is recommended to verify the correct socket
31       types before using them. To simplify this checking, the functions
32       sd_is_fifo(3), sd_is_socket(3), sd_is_socket_inet(3),
33       sd_is_socket_unix(3) are provided. In order to maximize flexibility, it
34       is recommended to make these checks as loose as possible without
35       allowing incorrect setups. i.e. often, the actual port number a socket
36       is bound to matters little for the service to work, hence it should not
37       be verified. On the other hand, whether a socket is a datagram or
38       stream socket matters a lot for the most common program logics and
39       should be checked.
40
41       This function call will set the FD_CLOEXEC flag for all passed file
42       descriptors to avoid further inheritance to children of the calling
43       process.
44
45       If multiple socket units activate the same service, the order of the
46       file descriptors passed to its main process is undefined. If additional
47       file descriptors have been passed to the service manager using
48       sd_pid_notify_with_fds(3)'s "FDSTORE=1" messages, these file
49       descriptors are passed last, in arbitrary order, and with duplicates
50       removed.
51
52       If the unset_environment parameter is non-zero, sd_listen_fds() will
53       unset the $LISTEN_FDS, $LISTEN_PID and $LISTEN_FDNAMES environment
54       variables before returning (regardless of whether the function call
55       itself succeeded or not). Further calls to sd_listen_fds() will then
56       return zero, but the variables are no longer inherited by child
57       processes.
58
59       sd_listen_fds_with_names() is like sd_listen_fds(), but optionally also
60       returns an array of strings with identification names for the passed
61       file descriptors, if that is available and the names parameter is
62       non-NULL. This information is read from the $LISTEN_FDNAMES variable,
63       which may contain a colon-separated list of names. For socket-activated
64       services, these names may be configured with the FileDescriptorName=
65       setting in socket unit files, see systemd.socket(5) for details. For
66       file descriptors pushed into the file descriptor store (see above), the
67       name is set via the FDNAME= field transmitted via
68       sd_pid_notify_with_fds(). The primary usecase for these names are
69       services which accept a variety of file descriptors which are not
70       recognizable with functions like sd_is_socket() alone, and thus require
71       identification via a name. It is recommended to rely on named file
72       descriptors only if identification via sd_is_socket() and related calls
73       is not sufficient. Note that the names used are not unique in any way.
74       The returned array of strings has as many entries as file descriptors
75       have been received, plus a final NULL pointer terminating the array.
76       The caller needs to free the array itself and each of its elements with
77       libc's free() call after use. If the names parameter is NULL, the call
78       is entirely equivalent to sd_listen_fds().
79
80       Under specific conditions, the following automatic file descriptor
81       names are returned:
82
83       Table 1.  Special names
84       ┌─────────────┬────────────────────────────┐
85Name         Description                
86       ├─────────────┼────────────────────────────┤
87       │"unknown"    │ The process received no    │
88       │             │ name for the specific file │
89       │             │ descriptor from the        │
90       │             │ service manager.           │
91       ├─────────────┼────────────────────────────┤
92       │"stored"     │ The file descriptor        │
93       │             │ originates in the service  │
94       │             │ manager's per-service file │
95       │             │ descriptor store, and the  │
96       │             │ FDNAME= field was absent   │
97       │             │ when the file descriptor   │
98       │             │ was submitted to the       │
99       │             │ service manager.           │
100       ├─────────────┼────────────────────────────┤
101       │"connection" │ The service was activated  │
102       │             │ in per-connection style    │
103       │             │ using Accept=yes in the    │
104       │             │ socket unit file, and the  │
105       │             │ file descriptor is the     │
106       │             │ connection socket.         │
107       └─────────────┴────────────────────────────┘
108

RETURN VALUE

110       On failure, these calls returns a negative errno-style error code. If
111       $LISTEN_FDS/$LISTEN_PID was not set or was not correctly set for this
112       daemon and hence no file descriptors were received, 0 is returned.
113       Otherwise, the number of file descriptors passed is returned. The
114       application may find them starting with file descriptor
115       SD_LISTEN_FDS_START, i.e. file descriptor 3.
116

NOTES

118       These APIs are implemented as a shared library, which can be compiled
119       and linked to with the libsystemd pkg-config(1) file.
120
121       Internally, sd_listen_fds() checks whether the $LISTEN_PID environment
122       variable equals the daemon PID. If not, it returns immediately.
123       Otherwise, it parses the number passed in the $LISTEN_FDS environment
124       variable, then sets the FD_CLOEXEC flag for the parsed number of file
125       descriptors starting from SD_LISTEN_FDS_START. Finally, it returns the
126       parsed number.  sd_listen_fds_with_names() does the same but also
127       parses $LISTEN_FDNAMES if set.
128

ENVIRONMENT

130       $LISTEN_PID, $LISTEN_FDS, $LISTEN_FDNAMES
131           Set by the service manager for supervised processes that use
132           socket-based activation. This environment variable specifies the
133           data sd_listen_fds() and sd_listen_fds_with_names() parses. See
134           above for details.
135

SEE ALSO

137       systemd(1), sd-daemon(3), sd_is_fifo(3), sd_is_socket(3),
138       sd_is_socket_inet(3), sd_is_socket_unix(3), sd_pid_notify_with_fds(3),
139       daemon(7), systemd.service(5), systemd.socket(5)
140
141
142
143systemd 248                                                   SD_LISTEN_FDS(3)
Impressum