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 and file descriptor store logic. It returns the number of
22       received file descriptors. If no file descriptors have been received,
23       zero is returned. The first file descriptor may be found at file
24       descriptor number 3 (i.e.  SD_LISTEN_FDS_START), the remaining
25       descriptors follow at 4, 5, 6, ..., if any.
26
27       The file descriptors passed this way may be closed at will by the
28       processes receiving them: it's up to the processes themselves to close
29       them after use or whether to leave them open until the process exits
30       (in which case the kernel closes them automatically). Note that the
31       file descriptors received by daemons are duplicates of the file
32       descriptors the service manager originally allocated and bound and of
33       which it continuously keeps a copy (except if Accept=yes is used). This
34       means any socket option changes and other changes made to the sockets
35       will be visible to the service manager too. Most importantly this means
36       it's generally not a good idea to invoke shutdown(2) on such sockets,
37       since it will shut down communication on the file descriptor the
38       service manager holds for the same socket too. Also note that if a
39       daemon is restarted (and its associated sockets are not) it will
40       receive file descriptors to the very same sockets as the earlier
41       invocations, thus all socket options applied then will still apply.
42
43       If a daemon receives more than one file descriptor, they will be passed
44       in the same order as configured in the systemd socket unit file (see
45       systemd.socket(5) for details) — if there's only one such file (see
46       below). Nonetheless, it is recommended to verify the correct socket
47       types before using them. To simplify this checking, the functions
48       sd_is_fifo(3), sd_is_socket(3), sd_is_socket_inet(3),
49       sd_is_socket_unix(3) are provided. In order to maximize flexibility, it
50       is recommended to make these checks as loose as possible without
51       allowing incorrect setups. i.e. often, the actual port number a socket
52       is bound to matters little for the service to work, hence it should not
53       be verified. On the other hand, whether a socket is a datagram or
54       stream socket matters a lot for the most common program logics and
55       should be checked.
56
57       This function call will set the FD_CLOEXEC flag for all passed file
58       descriptors to avoid further inheritance to children of the calling
59       process.
60
61       If multiple socket units activate the same service, the order of the
62       file descriptors passed to its main process is undefined. If additional
63       file descriptors have been passed to the service manager using
64       sd_pid_notify_with_fds(3)'s "FDSTORE=1" messages, these file
65       descriptors are passed last, in arbitrary order, and with duplicates
66       removed.
67
68       If the unset_environment parameter is non-zero, sd_listen_fds() will
69       unset the $LISTEN_FDS, $LISTEN_PID and $LISTEN_FDNAMES environment
70       variables before returning (regardless of whether the function call
71       itself succeeded or not). Further calls to sd_listen_fds() will then
72       return zero, but the variables are no longer inherited by child
73       processes.
74
75       sd_listen_fds_with_names() is like sd_listen_fds(), but optionally also
76       returns an array of strings with identification names for the passed
77       file descriptors, if that is available and the names parameter is
78       non-NULL. This information is read from the $LISTEN_FDNAMES variable,
79       which may contain a colon-separated list of names. For socket-activated
80       services, these names may be configured with the FileDescriptorName=
81       setting in socket unit files, see systemd.socket(5) for details. For
82       file descriptors pushed into the file descriptor store (see above), the
83       name is set via the FDNAME= field transmitted via
84       sd_pid_notify_with_fds(). The primary use case for these names are
85       services which accept a variety of file descriptors which are not
86       recognizable with functions like sd_is_socket() alone, and thus require
87       identification via a name. It is recommended to rely on named file
88       descriptors only if identification via sd_is_socket() and related calls
89       is not sufficient. Note that the names used are not unique in any way.
90       The returned array of strings has as many entries as file descriptors
91       have been received, plus a final NULL pointer terminating the array.
92       The caller needs to free the array itself and each of its elements with
93       libc's free() call after use. If the names parameter is NULL, the call
94       is entirely equivalent to sd_listen_fds().
95
96       Under specific conditions, the following automatic file descriptor
97       names are returned:
98
99       Table 1.  Special names
100       ┌─────────────┬────────────────────────────┐
101Name         Description                
102       ├─────────────┼────────────────────────────┤
103       │"unknown"    │ The process received no    │
104       │             │ name for the specific file │
105       │             │ descriptor from the        │
106       │             │ service manager.           │
107       ├─────────────┼────────────────────────────┤
108       │"stored"     │ The file descriptor        │
109       │             │ originates in the service  │
110       │             │ manager's per-service file │
111       │             │ descriptor store, and the  │
112       │             │ FDNAME= field was absent   │
113       │             │ when the file descriptor   │
114       │             │ was submitted to the       │
115       │             │ service manager.           │
116       ├─────────────┼────────────────────────────┤
117       │"connection" │ The service was activated  │
118       │             │ in per-connection style    │
119       │             │ using Accept=yes in the    │
120       │             │ socket unit file, and the  │
121       │             │ file descriptor is the     │
122       │             │ connection socket.         │
123       └─────────────┴────────────────────────────┘
124
125       For further information on the file descriptor store see the File
126       Descriptor Store[1] overview.
127

RETURN VALUE

129       On failure, these calls returns a negative errno-style error code. If
130       $LISTEN_FDS/$LISTEN_PID was not set or was not correctly set for this
131       daemon and hence no file descriptors were received, 0 is returned.
132       Otherwise, the number of file descriptors passed is returned. The
133       application may find them starting with file descriptor
134       SD_LISTEN_FDS_START, i.e. file descriptor 3.
135

NOTES

137       Functions described here are available as a shared library, which can
138       be compiled against and linked to with the libsystemd pkg-config(1)
139       file.
140
141       The code described here uses getenv(3), which is declared to be not
142       multi-thread-safe. This means that the code calling the functions
143       described here must not call setenv(3) from a parallel thread. It is
144       recommended to only do calls to setenv() from an early phase of the
145       program when no other threads have been started.
146
147       Internally, sd_listen_fds() checks whether the $LISTEN_PID environment
148       variable equals the daemon PID. If not, it returns immediately.
149       Otherwise, it parses the number passed in the $LISTEN_FDS environment
150       variable, then sets the FD_CLOEXEC flag for the parsed number of file
151       descriptors starting from SD_LISTEN_FDS_START. Finally, it returns the
152       parsed number.  sd_listen_fds_with_names() does the same but also
153       parses $LISTEN_FDNAMES if set.
154
155       These functions are not designed for services that specify
156       StandardInput=socket as the $LISTEN_FDS variable is not set in their
157       environment.
158

ENVIRONMENT

160       $LISTEN_PID, $LISTEN_FDS, $LISTEN_FDNAMES
161           Set by the service manager for supervised processes that use
162           socket-based activation. This environment variable specifies the
163           data sd_listen_fds() and sd_listen_fds_with_names() parses. See
164           above for details.
165

SEE ALSO

167       systemd(1), sd-daemon(3), sd_is_fifo(3), sd_is_socket(3),
168       sd_is_socket_inet(3), sd_is_socket_unix(3), sd_pid_notify_with_fds(3),
169       daemon(7), systemd.service(5), systemd.socket(5)
170

NOTES

172        1. File Descriptor Store
173           https://systemd.io/FILE_DESCRIPTOR_STORE
174
175
176
177systemd 254                                                   SD_LISTEN_FDS(3)
Impressum