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 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
51 If the unset_environment parameter is non-zero, sd_listen_fds() will
52 unset the $LISTEN_FDS, $LISTEN_PID and $LISTEN_FDNAMES environment
53 variables before returning (regardless of whether the function call
54 itself succeeded or not). Further calls to sd_listen_fds() will then
55 return zero, but the variables are no longer inherited by child
56 processes.
57
58 sd_listen_fds_with_names() is like sd_listen_fds(), but optionally also
59 returns an array of strings with identification names for the passed
60 file descriptors, if that is available and the names parameter is
61 non-NULL. This information is read from the $LISTEN_FDNAMES variable,
62 which may contain a colon-separated list of names. For socket-activated
63 services, these names may be configured with the FileDescriptorName=
64 setting in socket unit files, see systemd.socket(5) for details. For
65 file descriptors pushed into the file descriptor store (see above), the
66 name is set via the FDNAME= field transmitted via
67 sd_pid_notify_with_fds(). The primary usecase for these names are
68 services which accept a variety of file descriptors which are not
69 recognizable with functions like sd_is_socket() alone, and thus require
70 identification via a name. It is recommended to rely on named file
71 descriptors only if identification via sd_is_socket() and related calls
72 is not sufficient. Note that the names used are not unique in any way.
73 The returned array of strings has as many entries as file descriptors
74 have been received, plus a final NULL pointer terminating the array.
75 The caller needs to free the array itself and each of its elements with
76 libc's free() call after use. If the names parameter is NULL, the call
77 is entirely equivalent to sd_listen_fds().
78
79 Under specific conditions, the following automatic file descriptor
80 names are returned:
81
82 Table 1. Special names
83 ┌─────────────┬────────────────────────────┐
84 │Name │ Description │
85 ├─────────────┼────────────────────────────┤
86 │"unknown" │ The process received no │
87 │ │ name for the specific file │
88 │ │ descriptor from the │
89 │ │ service manager. │
90 ├─────────────┼────────────────────────────┤
91 │"stored" │ The file descriptor │
92 │ │ originates in the service │
93 │ │ manager's per-service file │
94 │ │ descriptor store, and the │
95 │ │ FDNAME= field was absent │
96 │ │ when the file descriptor │
97 │ │ was submitted to the │
98 │ │ service manager. │
99 ├─────────────┼────────────────────────────┤
100 │"connection" │ The service was activated │
101 │ │ in per-connection style │
102 │ │ using Accept=yes in the │
103 │ │ socket unit file, and the │
104 │ │ file descriptor is the │
105 │ │ connection socket. │
106 └─────────────┴────────────────────────────┘
107
109 On failure, these calls returns a negative errno-style error code. If
110 $LISTEN_FDS/$LISTEN_PID was not set or was not correctly set for this
111 daemon and hence no file descriptors were received, 0 is returned.
112 Otherwise, the number of file descriptors passed is returned. The
113 application may find them starting with file descriptor
114 SD_LISTEN_FDS_START, i.e. file descriptor 3.
115
117 These APIs are implemented as a shared library, which can be compiled
118 and linked to with the libsystemd pkg-config(1) file.
119
120 Internally, sd_listen_fds() checks whether the $LISTEN_PID environment
121 variable equals the daemon PID. If not, it returns immediately.
122 Otherwise, it parses the number passed in the $LISTEN_FDS environment
123 variable, then sets the FD_CLOEXEC flag for the parsed number of file
124 descriptors starting from SD_LISTEN_FDS_START. Finally, it returns the
125 parsed number. sd_listen_fds_with_names() does the same but also
126 parses $LISTEN_FDNAMES if set.
127
129 $LISTEN_PID, $LISTEN_FDS, $LISTEN_FDNAMES
130 Set by the service manager for supervised processes that use
131 socket-based activation. This environment variable specifies the
132 data sd_listen_fds() and sd_listen_fds_with_names() parses. See
133 above for details.
134
136 systemd(1), sd-daemon(3), sd_is_fifo(3), sd_is_socket(3),
137 sd_is_socket_inet(3), sd_is_socket_unix(3), sd_pid_notify_with_fds(3),
138 daemon(7), systemd.service(5), systemd.socket(5)
139
140
141
142systemd 241 SD_LISTEN_FDS(3)