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 int sd_is_socket(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 int sd_is_socket_inet(int fd, int type, int listening, uint16_t port) {
182 struct sockaddr_in in4;
183 struct sockaddr_in6 in6;
184 struct sockaddr_un un;
185 struct sockaddr_storage storage;
190 if ((r = sd_is_socket(fd, type, listening)) <= 0)
193 memset(&sockaddr, 0, sizeof(sockaddr));
194 l = sizeof(sockaddr);
196 if (getsockname(fd, &sockaddr.sa, &l) < 0)
199 if (l < sizeof(struct sockaddr))
202 if (sockaddr.sa.sa_family != AF_INET &&
203 sockaddr.sa.sa_family != AF_INET6)
207 if (sockaddr.sa.sa_family == AF_INET) {
208 if (l < sizeof(struct sockaddr_in))
211 return htons(port) == sockaddr.in4.sin_port;
213 if (l < sizeof(struct sockaddr_in6))
216 return htons(port) == sockaddr.in6.sin6_port;
223 int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
226 struct sockaddr_in in4;
227 struct sockaddr_in6 in6;
228 struct sockaddr_un un;
229 struct sockaddr_storage storage;
234 if ((r = sd_is_socket(fd, type, listening)) <= 0)
237 memset(&sockaddr, 0, sizeof(sockaddr));
238 l = sizeof(sockaddr);
240 if (getsockname(fd, &sockaddr.sa, &l) < 0)
243 if (l < sizeof(sa_family_t))
246 if (sockaddr.sa.sa_family != AF_UNIX)
251 length = strlen(path);
255 return l == sizeof(sa_family_t);
258 /* Normal path socket */
260 (l >= sizeof(sa_family_t) + length + 1) &&
261 memcmp(path, sockaddr.un.sun_path, length+1) == 0;
263 /* Abstract namespace socket */
265 (l == sizeof(sa_family_t) + length) &&
266 memcmp(path, sockaddr.un.sun_path, length) == 0;