1SD_LISTEN_FDS(3) sd_listen_fds SD_LISTEN_FDS(3)
2
3
4
6 sd_listen_fds - Check for file descriptors passed by the init system.
7
9 #include "sd-daemon.h"
10
11 #define SD_LISTEN_FDS_START 3
12
13 int sd_listen_fds(int unset_environment);
14
16 sd_listen_fds() shall be called by a daemon to check for file
17 descriptors passed by the init system as part of the socket-based
18 activation logic.
19
20 If the unset_environment parameter is non-zero sd_listen_fds() will
21 unset the $LISTEN_FDS/$LISTEN_PID environment variables before
22 returning (regardless whether the function call itself succeeded or
23 not). Further calls to sd_listen_fds() will then fail, but the
24 variables are no longer inherited by child processes.
25
26 If a daemon receives more than one file descriptor, they will be passed
27 in the same order as configured in the systemd socket definition file.
28 Nonetheless it is recommended to verify the correct socket types before
29 using them. To simplify this checking the functions sd_is_fifo(3),
30 sd_is_socket(3), sd_is_socket_inet(3), sd_is_socket_unix(3) are
31 provided. In order to maximize flexibility it is recommended to make
32 these checks as loose as possible without allowing incorrect setups.
33 i.e. often the actual port number a socket is bound to matters little
34 for the service to work, hence it should not be verified. On the other
35 hand, whether a socket is a datagram or stream socket matters a lot for
36 the most common program logics and should be checked.
37
38 This function call will set the FD_CLOEXEC flag for all passed file
39 descriptors to avoid further inheritance to children of the calling
40 process.
41
43 On failure, this call returns a negative errno-style error code. If
44 $LISTEN_FDS/$LISTEN_PID was not set or was not correctly set for this
45 daemon and hence no file descriptors were received, 0 is returned.
46 Otherwise the number of file descriptors passed is returned. The
47 application may find them starting with file descriptor
48 SD_LISTEN_FDS_START, i.e. file descriptor 3.
49
51 This function is provided by the reference implementation of APIs for
52 new-style daemons and distributed with the systemd package. The
53 algorithm it implements is simple, and can easily be reimplemented in
54 daemons if it is important to support this interface without using the
55 reference implementation.
56
57 Internally, this function checks whether the $LISTEN_PID environment
58 variable equals the daemon PID. If not, it returns immediately.
59 Otherwise it parses the number passed in the $LISTEN_FDS environment
60 variable, then sets the FD_CLOEXEC flag for the parsed number of file
61 descriptors starting from SD_LISTEN_FDS_START. Finally it returns the
62 parsed number.
63
64 For details about the algorithm check the liberally licensed reference
65 implementation sources:
66 http://cgit.freedesktop.org/systemd/tree/src/sd-daemon.c resp.
67 http://cgit.freedesktop.org/systemd/tree/src/sd-daemon.h
68
69 sd_listen_fds() is implemented in the reference implementation´s
70 drop-in sd-daemon.c and sd-daemon.h files. It is recommended that
71 applications consuming these APIs copy the implementation into their
72 source tree. For more details about the reference implementation see
73 sd_daemon(7)
74
75 If -DDISABLE_SYSTEMD is set during compilation this function will
76 always return 0 and otherwise become a NOP.
77
79 $LISTEN_PID, $LISTEN_FDS
80 Set by the init system for supervised processes that use
81 socket-based activation. This environment variable specifies the
82 data sd_listen_fds() parses. See above for details.
83
85 systemd(1), sd-daemon(7), sd_is_fifo(3), sd_is_socket(3),
86 sd_is_socket_inet(3), sd_is_socket_unix(3), daemon(7),
87 systemd.service(5), systemd.socket(5)
88
90 Lennart Poettering <lennart@poettering.net>
91 Developer
92
93
94
95systemd 09/14/2010 SD_LISTEN_FDS(3)