1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 Copyright 2010 Lennart Poettering
6 Permission is hereby granted, free of charge, to any person
7 obtaining a copy of this software and associated documentation files
8 (the "Software"), to deal in the Software without restriction,
9 including without limitation the rights to use, copy, modify, merge,
10 publish, distribute, sublicense, and/or sell copies of the Software,
11 and to permit persons to whom the Software is furnished to do so,
12 subject to the following conditions:
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 #include <sys/types.h>
29 #include <sys/socket.h>
31 #include <sys/fcntl.h>
32 #include <netinet/in.h>
38 #include "sd-daemon.h"
40 int sd_listen_fds(int unset_environment) {
42 #ifdef DISABLE_SYSTEMD
50 if (!(e = getenv("LISTEN_PID"))) {
56 l = strtoul(e, &p, 10);
63 if (!p || *p || l <= 0) {
69 if (getpid() != (pid_t) l) {
74 if (!(e = getenv("LISTEN_FDS"))) {
80 l = strtoul(e, &p, 10);
93 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) l; fd ++) {
96 if ((flags = fcntl(fd, F_GETFD)) < 0) {
101 if (flags & FD_CLOEXEC)
104 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
113 if (unset_environment) {
114 unsetenv("LISTEN_PID");
115 unsetenv("LISTEN_FDS");
122 int sd_is_fifo(int fd, const char *path) {
128 memset(&st_fd, 0, sizeof(st_fd));
129 if (fstat(fd, &st_fd) < 0)
132 if (!S_ISFIFO(st_fd.st_mode))
138 memset(&st_path, 0, sizeof(st_path));
139 if (fstat(fd, &st_path) < 0) {
141 if (errno == ENOENT || errno == ENOTDIR)
148 st_path.st_dev == st_fd.st_dev &&
149 st_path.st_ino == st_fd.st_ino;
155 static int sd_is_socket_internal(int fd, int type, int listening) {
158 if (fd < 0 || type < 0)
161 if (fstat(fd, &st_fd) < 0)
164 if (!S_ISSOCK(st_fd.st_mode))
169 socklen_t l = sizeof(other_type);
171 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
174 if (l != sizeof(other_type))
177 if (other_type != type)
181 if (listening >= 0) {
183 socklen_t l = sizeof(accepting);
185 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
188 if (l != sizeof(accepting))
191 if (!accepting != !listening)
198 union sockaddr_union {
200 struct sockaddr_in in4;
201 struct sockaddr_in6 in6;
202 struct sockaddr_un un;
203 struct sockaddr_storage storage;
206 int sd_is_socket(int fd, int family, int type, int listening) {
212 if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
216 union sockaddr_union sockaddr;
219 memset(&sockaddr, 0, sizeof(sockaddr));
220 l = sizeof(sockaddr);
222 if (getsockname(fd, &sockaddr.sa, &l) < 0)
225 if (l < sizeof(sa_family_t))
228 return sockaddr.sa.sa_family == family;
234 int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
235 union sockaddr_union sockaddr;
239 if (family != 0 && family != AF_INET && family != AF_INET6)
242 if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
245 memset(&sockaddr, 0, sizeof(sockaddr));
246 l = sizeof(sockaddr);
248 if (getsockname(fd, &sockaddr.sa, &l) < 0)
251 if (l < sizeof(sa_family_t))
254 if (sockaddr.sa.sa_family != AF_INET &&
255 sockaddr.sa.sa_family != AF_INET6)
259 if (sockaddr.sa.sa_family != family)
263 if (sockaddr.sa.sa_family == AF_INET) {
264 if (l < sizeof(struct sockaddr_in))
267 return htons(port) == sockaddr.in4.sin_port;
269 if (l < sizeof(struct sockaddr_in6))
272 return htons(port) == sockaddr.in6.sin6_port;
279 int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
280 union sockaddr_union sockaddr;
284 if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
287 memset(&sockaddr, 0, sizeof(sockaddr));
288 l = sizeof(sockaddr);
290 if (getsockname(fd, &sockaddr.sa, &l) < 0)
293 if (l < sizeof(sa_family_t))
296 if (sockaddr.sa.sa_family != AF_UNIX)
301 length = strlen(path);
305 return l == sizeof(sa_family_t);
308 /* Normal path socket */
310 (l >= sizeof(sa_family_t) + length + 1) &&
311 memcmp(path, sockaddr.un.sun_path, length+1) == 0;
313 /* Abstract namespace socket */
315 (l == sizeof(sa_family_t) + length) &&
316 memcmp(path, sockaddr.un.sun_path, length) == 0;