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 <netinet/in.h>
37 #include "sd-daemon.h"
39 int sd_listen_fds(int unset_environment) {
41 #ifdef DISABLE_SYSTEMD
49 if (!(e = getenv("LISTEN_PID"))) {
55 l = strtoul(e, &p, 10);
62 if (!p || *p || l <= 0) {
68 if (getpid() != (pid_t) l) {
73 if (!(e = getenv("LISTEN_FDS"))) {
79 l = strtoul(e, &p, 10);
94 if (unset_environment) {
95 unsetenv("LISTEN_PID");
96 unsetenv("LISTEN_FDS");
103 int sd_is_fifo(int fd, const char *path) {
109 memset(&st_fd, 0, sizeof(st_fd));
110 if (fstat(fd, &st_fd) < 0)
113 if (!S_ISFIFO(st_fd.st_mode))
119 memset(&st_path, 0, sizeof(st_path));
120 if (fstat(fd, &st_path) < 0) {
122 if (errno == ENOENT || errno == ENOTDIR)
129 st_path.st_dev == st_fd.st_dev &&
130 st_path.st_ino == st_fd.st_ino;
136 static int sd_is_socket_internal(int fd, int type, int listening) {
139 if (fd < 0 || type < 0)
142 if (fstat(fd, &st_fd) < 0)
145 if (!S_ISSOCK(st_fd.st_mode))
150 socklen_t l = sizeof(other_type);
152 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
155 if (l != sizeof(other_type))
158 if (other_type != type)
162 if (listening >= 0) {
164 socklen_t l = sizeof(accepting);
166 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
169 if (l != sizeof(accepting))
172 if (!accepting != !listening)
179 union sockaddr_union {
181 struct sockaddr_in in4;
182 struct sockaddr_in6 in6;
183 struct sockaddr_un un;
184 struct sockaddr_storage storage;
187 int sd_is_socket(int fd, int family, int type, int listening) {
193 if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
197 union sockaddr_union sockaddr;
200 memset(&sockaddr, 0, sizeof(sockaddr));
201 l = sizeof(sockaddr);
203 if (getsockname(fd, &sockaddr.sa, &l) < 0)
206 if (l < sizeof(sa_family_t))
209 return sockaddr.sa.sa_family == family;
215 int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
216 union sockaddr_union sockaddr;
220 if (family != 0 && family != AF_INET && family != AF_INET6)
223 if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
226 memset(&sockaddr, 0, sizeof(sockaddr));
227 l = sizeof(sockaddr);
229 if (getsockname(fd, &sockaddr.sa, &l) < 0)
232 if (l < sizeof(sa_family_t))
235 if (sockaddr.sa.sa_family != AF_INET &&
236 sockaddr.sa.sa_family != AF_INET6)
240 if (sockaddr.sa.sa_family != family)
244 if (sockaddr.sa.sa_family == AF_INET) {
245 if (l < sizeof(struct sockaddr_in))
248 return htons(port) == sockaddr.in4.sin_port;
250 if (l < sizeof(struct sockaddr_in6))
253 return htons(port) == sockaddr.in6.sin6_port;
260 int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
261 union sockaddr_union sockaddr;
265 if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
268 memset(&sockaddr, 0, sizeof(sockaddr));
269 l = sizeof(sockaddr);
271 if (getsockname(fd, &sockaddr.sa, &l) < 0)
274 if (l < sizeof(sa_family_t))
277 if (sockaddr.sa.sa_family != AF_UNIX)
282 length = strlen(path);
286 return l == sizeof(sa_family_t);
289 /* Normal path socket */
291 (l >= sizeof(sa_family_t) + length + 1) &&
292 memcmp(path, sockaddr.un.sun_path, length+1) == 0;
294 /* Abstract namespace socket */
296 (l == sizeof(sa_family_t) + length) &&
297 memcmp(path, sockaddr.un.sun_path, length) == 0;