1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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
31 #include <sys/types.h>
33 #include <sys/socket.h>
36 #include <linux/fcntl.h>
38 #include <sys/fcntl.h>
40 #include <netinet/in.h>
50 #if defined(__linux__)
54 #include "sd-daemon.h"
57 #ifdef SD_EXPORT_SYMBOLS
59 #define _sd_export_ __attribute__ ((visibility("default")))
61 /* Don't export the symbols */
62 #define _sd_export_ __attribute__ ((visibility("hidden")))
68 _sd_export_ int sd_listen_fds(int unset_environment) {
70 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
78 e = getenv("LISTEN_PID");
85 l = strtoul(e, &p, 10);
92 if (!p || p == e || *p || l <= 0) {
98 if (getpid() != (pid_t) l) {
103 e = getenv("LISTEN_FDS");
110 l = strtoul(e, &p, 10);
117 if (!p || p == e || *p) {
122 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) l; fd ++) {
125 flags = fcntl(fd, F_GETFD);
131 if (flags & FD_CLOEXEC)
134 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
143 if (unset_environment) {
144 unsetenv("LISTEN_PID");
145 unsetenv("LISTEN_FDS");
152 _sd_export_ int sd_is_fifo(int fd, const char *path) {
158 if (fstat(fd, &st_fd) < 0)
161 if (!S_ISFIFO(st_fd.st_mode))
167 if (stat(path, &st_path) < 0) {
169 if (errno == ENOENT || errno == ENOTDIR)
176 st_path.st_dev == st_fd.st_dev &&
177 st_path.st_ino == st_fd.st_ino;
183 _sd_export_ int sd_is_special(int fd, const char *path) {
189 if (fstat(fd, &st_fd) < 0)
192 if (!S_ISREG(st_fd.st_mode) && !S_ISCHR(st_fd.st_mode))
198 if (stat(path, &st_path) < 0) {
200 if (errno == ENOENT || errno == ENOTDIR)
206 if (S_ISREG(st_fd.st_mode) && S_ISREG(st_path.st_mode))
208 st_path.st_dev == st_fd.st_dev &&
209 st_path.st_ino == st_fd.st_ino;
210 else if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode))
211 return st_path.st_rdev == st_fd.st_rdev;
219 static int sd_is_socket_internal(int fd, int type, int listening) {
222 if (fd < 0 || type < 0)
225 if (fstat(fd, &st_fd) < 0)
228 if (!S_ISSOCK(st_fd.st_mode))
233 socklen_t l = sizeof(other_type);
235 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
238 if (l != sizeof(other_type))
241 if (other_type != type)
245 if (listening >= 0) {
247 socklen_t l = sizeof(accepting);
249 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
252 if (l != sizeof(accepting))
255 if (!accepting != !listening)
262 union sockaddr_union {
264 struct sockaddr_in in4;
265 struct sockaddr_in6 in6;
266 struct sockaddr_un un;
267 struct sockaddr_storage storage;
270 _sd_export_ int sd_is_socket(int fd, int family, int type, int listening) {
276 r = sd_is_socket_internal(fd, type, listening);
281 union sockaddr_union sockaddr;
284 memset(&sockaddr, 0, sizeof(sockaddr));
285 l = sizeof(sockaddr);
287 if (getsockname(fd, &sockaddr.sa, &l) < 0)
290 if (l < sizeof(sa_family_t))
293 return sockaddr.sa.sa_family == family;
299 _sd_export_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
300 union sockaddr_union sockaddr;
304 if (family != 0 && family != AF_INET && family != AF_INET6)
307 r = sd_is_socket_internal(fd, type, listening);
311 memset(&sockaddr, 0, sizeof(sockaddr));
312 l = sizeof(sockaddr);
314 if (getsockname(fd, &sockaddr.sa, &l) < 0)
317 if (l < sizeof(sa_family_t))
320 if (sockaddr.sa.sa_family != AF_INET &&
321 sockaddr.sa.sa_family != AF_INET6)
325 if (sockaddr.sa.sa_family != family)
329 if (sockaddr.sa.sa_family == AF_INET) {
330 if (l < sizeof(struct sockaddr_in))
333 return htons(port) == sockaddr.in4.sin_port;
335 if (l < sizeof(struct sockaddr_in6))
338 return htons(port) == sockaddr.in6.sin6_port;
345 _sd_export_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
346 union sockaddr_union sockaddr;
350 r = sd_is_socket_internal(fd, type, listening);
354 memset(&sockaddr, 0, sizeof(sockaddr));
355 l = sizeof(sockaddr);
357 if (getsockname(fd, &sockaddr.sa, &l) < 0)
360 if (l < sizeof(sa_family_t))
363 if (sockaddr.sa.sa_family != AF_UNIX)
368 length = strlen(path);
372 return l == offsetof(struct sockaddr_un, sun_path);
375 /* Normal path socket */
377 (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
378 memcmp(path, sockaddr.un.sun_path, length+1) == 0;
380 /* Abstract namespace socket */
382 (l == offsetof(struct sockaddr_un, sun_path) + length) &&
383 memcmp(path, sockaddr.un.sun_path, length) == 0;
389 _sd_export_ int sd_is_mq(int fd, const char *path) {
390 #if !defined(__linux__)
398 if (mq_getattr(fd, &attr) < 0)
402 char fpath[PATH_MAX];
408 if (fstat(fd, &a) < 0)
411 strncpy(stpcpy(fpath, "/dev/mqueue"), path, sizeof(fpath) - 12);
412 fpath[sizeof(fpath)-1] = 0;
414 if (stat(fpath, &b) < 0)
417 if (a.st_dev != b.st_dev ||
418 a.st_ino != b.st_ino)
426 _sd_export_ int sd_notify(int unset_environment, const char *state) {
427 #if defined(DISABLE_SYSTEMD) || !defined(__linux__) || !defined(SOCK_CLOEXEC)
431 struct msghdr msghdr;
433 union sockaddr_union sockaddr;
441 e = getenv("NOTIFY_SOCKET");
445 /* Must be an abstract socket, or an absolute path */
446 if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
451 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
457 memset(&sockaddr, 0, sizeof(sockaddr));
458 sockaddr.sa.sa_family = AF_UNIX;
459 strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
461 if (sockaddr.un.sun_path[0] == '@')
462 sockaddr.un.sun_path[0] = 0;
464 memset(&iovec, 0, sizeof(iovec));
465 iovec.iov_base = (char*) state;
466 iovec.iov_len = strlen(state);
468 memset(&msghdr, 0, sizeof(msghdr));
469 msghdr.msg_name = &sockaddr;
470 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(e);
472 if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
473 msghdr.msg_namelen = sizeof(struct sockaddr_un);
475 msghdr.msg_iov = &iovec;
476 msghdr.msg_iovlen = 1;
478 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
486 if (unset_environment)
487 unsetenv("NOTIFY_SOCKET");
496 _sd_export_ int sd_notifyf(int unset_environment, const char *format, ...) {
497 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
504 va_start(ap, format);
505 r = vasprintf(&p, format, ap);
511 r = sd_notify(unset_environment, p);
518 _sd_export_ int sd_booted(void) {
519 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
525 /* We simply test whether the systemd cgroup hierarchy is
528 if (lstat("/sys/fs/cgroup", &a) < 0)
531 if (lstat("/sys/fs/cgroup/systemd", &b) < 0)
534 return a.st_dev != b.st_dev;