1SD_LISTEN_FDS(3) sd_listen_fds SD_LISTEN_FDS(3)
2
3
4
6 sd_listen_fds, sd_listen_fds_with_names, SD_LISTEN_FDS_START - Check
7 for file descriptors passed by the system manager
8
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
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 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 usecase 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 ┌─────────────┬────────────────────────────┐
101 │Name │ 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
126 On failure, these calls returns a negative errno-style error code. If
127 $LISTEN_FDS/$LISTEN_PID was not set or was not correctly set for this
128 daemon and hence no file descriptors were received, 0 is returned.
129 Otherwise, the number of file descriptors passed is returned. The
130 application may find them starting with file descriptor
131 SD_LISTEN_FDS_START, i.e. file descriptor 3.
132
134 These APIs are implemented as a shared library, which can be compiled
135 and linked to with the libsystemd pkg-config(1) file.
136
137 Internally, sd_listen_fds() checks whether the $LISTEN_PID environment
138 variable equals the daemon PID. If not, it returns immediately.
139 Otherwise, it parses the number passed in the $LISTEN_FDS environment
140 variable, then sets the FD_CLOEXEC flag for the parsed number of file
141 descriptors starting from SD_LISTEN_FDS_START. Finally, it returns the
142 parsed number. sd_listen_fds_with_names() does the same but also
143 parses $LISTEN_FDNAMES if set.
144
145 These functions are not designed for services that specify
146 StandardInput=socket as the $LISTEN_FDS variable is not set in their
147 environment.
148
150 $LISTEN_PID, $LISTEN_FDS, $LISTEN_FDNAMES
151 Set by the service manager for supervised processes that use
152 socket-based activation. This environment variable specifies the
153 data sd_listen_fds() and sd_listen_fds_with_names() parses. See
154 above for details.
155
157 systemd(1), sd-daemon(3), sd_is_fifo(3), sd_is_socket(3),
158 sd_is_socket_inet(3), sd_is_socket_unix(3), sd_pid_notify_with_fds(3),
159 daemon(7), systemd.service(5), systemd.socket(5)
160
161
162
163systemd 253 SD_LISTEN_FDS(3)