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>
35 #include <sys/fcntl.h>
36 #include <netinet/in.h>
46 #if defined(__linux__)
50 #include "sd-daemon.h"
52 #if (__GNUC__ >= 4) && !defined(SD_EXPORT_SYMBOLS)
53 #define _sd_hidden_ __attribute__ ((visibility("hidden")))
58 _sd_hidden_ int sd_listen_fds(int unset_environment) {
60 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
68 if (!(e = getenv("LISTEN_PID"))) {
74 l = strtoul(e, &p, 10);
81 if (!p || *p || l <= 0) {
87 if (getpid() != (pid_t) l) {
92 if (!(e = getenv("LISTEN_FDS"))) {
98 l = strtoul(e, &p, 10);
110 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) l; fd ++) {
113 if ((flags = fcntl(fd, F_GETFD)) < 0) {
118 if (flags & FD_CLOEXEC)
121 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
130 if (unset_environment) {
131 unsetenv("LISTEN_PID");
132 unsetenv("LISTEN_FDS");
139 _sd_hidden_ int sd_is_fifo(int fd, const char *path) {
145 memset(&st_fd, 0, sizeof(st_fd));
146 if (fstat(fd, &st_fd) < 0)
149 if (!S_ISFIFO(st_fd.st_mode))
155 memset(&st_path, 0, sizeof(st_path));
156 if (stat(path, &st_path) < 0) {
158 if (errno == ENOENT || errno == ENOTDIR)
165 st_path.st_dev == st_fd.st_dev &&
166 st_path.st_ino == st_fd.st_ino;
172 _sd_hidden_ int sd_is_special(int fd, const char *path) {
178 if (fstat(fd, &st_fd) < 0)
181 if (!S_ISREG(st_fd.st_mode) && !S_ISCHR(st_fd.st_mode))
187 if (stat(path, &st_path) < 0) {
189 if (errno == ENOENT || errno == ENOTDIR)
195 if (S_ISREG(st_fd.st_mode) && S_ISREG(st_path.st_mode))
197 st_path.st_dev == st_fd.st_dev &&
198 st_path.st_ino == st_fd.st_ino;
199 else if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode))
200 return st_path.st_rdev == st_fd.st_rdev;
208 static int sd_is_socket_internal(int fd, int type, int listening) {
211 if (fd < 0 || type < 0)
214 if (fstat(fd, &st_fd) < 0)
217 if (!S_ISSOCK(st_fd.st_mode))
222 socklen_t l = sizeof(other_type);
224 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
227 if (l != sizeof(other_type))
230 if (other_type != type)
234 if (listening >= 0) {
236 socklen_t l = sizeof(accepting);
238 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
241 if (l != sizeof(accepting))
244 if (!accepting != !listening)
251 union sockaddr_union {
253 struct sockaddr_in in4;
254 struct sockaddr_in6 in6;
255 struct sockaddr_un un;
256 struct sockaddr_storage storage;
259 _sd_hidden_ int sd_is_socket(int fd, int family, int type, int listening) {
265 if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
269 union sockaddr_union sockaddr;
272 memset(&sockaddr, 0, sizeof(sockaddr));
273 l = sizeof(sockaddr);
275 if (getsockname(fd, &sockaddr.sa, &l) < 0)
278 if (l < sizeof(sa_family_t))
281 return sockaddr.sa.sa_family == family;
287 _sd_hidden_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
288 union sockaddr_union sockaddr;
292 if (family != 0 && family != AF_INET && family != AF_INET6)
295 if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
298 memset(&sockaddr, 0, sizeof(sockaddr));
299 l = sizeof(sockaddr);
301 if (getsockname(fd, &sockaddr.sa, &l) < 0)
304 if (l < sizeof(sa_family_t))
307 if (sockaddr.sa.sa_family != AF_INET &&
308 sockaddr.sa.sa_family != AF_INET6)
312 if (sockaddr.sa.sa_family != family)
316 if (sockaddr.sa.sa_family == AF_INET) {
317 if (l < sizeof(struct sockaddr_in))
320 return htons(port) == sockaddr.in4.sin_port;
322 if (l < sizeof(struct sockaddr_in6))
325 return htons(port) == sockaddr.in6.sin6_port;
332 _sd_hidden_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
333 union sockaddr_union sockaddr;
337 if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
340 memset(&sockaddr, 0, sizeof(sockaddr));
341 l = sizeof(sockaddr);
343 if (getsockname(fd, &sockaddr.sa, &l) < 0)
346 if (l < sizeof(sa_family_t))
349 if (sockaddr.sa.sa_family != AF_UNIX)
354 length = strlen(path);
358 return l == offsetof(struct sockaddr_un, sun_path);
361 /* Normal path socket */
363 (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
364 memcmp(path, sockaddr.un.sun_path, length+1) == 0;
366 /* Abstract namespace socket */
368 (l == offsetof(struct sockaddr_un, sun_path) + length) &&
369 memcmp(path, sockaddr.un.sun_path, length) == 0;
375 _sd_hidden_ int sd_is_mq(int fd, const char *path) {
376 #if !defined(__linux__)
384 if (mq_getattr(fd, &attr) < 0)
388 char fpath[PATH_MAX];
394 if (fstat(fd, &a) < 0)
397 strncpy(stpcpy(fpath, "/dev/mqueue"), path, sizeof(fpath) - 12);
398 fpath[sizeof(fpath)-1] = 0;
400 if (stat(fpath, &b) < 0)
403 if (a.st_dev != b.st_dev ||
404 a.st_ino != b.st_ino)
412 _sd_hidden_ int sd_notify(int unset_environment, const char *state) {
413 #if defined(DISABLE_SYSTEMD) || !defined(__linux__) || !defined(SOCK_CLOEXEC)
417 struct msghdr msghdr;
419 union sockaddr_union sockaddr;
427 if (!(e = getenv("NOTIFY_SOCKET")))
430 /* Must be an abstract socket, or an absolute path */
431 if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
436 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
441 memset(&sockaddr, 0, sizeof(sockaddr));
442 sockaddr.sa.sa_family = AF_UNIX;
443 strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
445 if (sockaddr.un.sun_path[0] == '@')
446 sockaddr.un.sun_path[0] = 0;
448 memset(&iovec, 0, sizeof(iovec));
449 iovec.iov_base = (char*) state;
450 iovec.iov_len = strlen(state);
452 memset(&msghdr, 0, sizeof(msghdr));
453 msghdr.msg_name = &sockaddr;
454 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(e);
456 if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
457 msghdr.msg_namelen = sizeof(struct sockaddr_un);
459 msghdr.msg_iov = &iovec;
460 msghdr.msg_iovlen = 1;
462 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
470 if (unset_environment)
471 unsetenv("NOTIFY_SOCKET");
480 _sd_hidden_ int sd_notifyf(int unset_environment, const char *format, ...) {
481 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
488 va_start(ap, format);
489 r = vasprintf(&p, format, ap);
495 r = sd_notify(unset_environment, p);
502 _sd_hidden_ int sd_booted(void) {
503 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
509 /* We simply test whether the systemd cgroup hierarchy is
512 if (lstat("/sys/fs/cgroup", &a) < 0)
515 if (lstat("/sys/fs/cgroup/systemd", &b) < 0)
518 return a.st_dev != b.st_dev;