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__) && !defined(SD_DAEMON_DISABLE_MQ)
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 = {};
282 socklen_t l = sizeof(sockaddr);
284 if (getsockname(fd, &sockaddr.sa, &l) < 0)
287 if (l < sizeof(sa_family_t))
290 return sockaddr.sa.sa_family == family;
296 _sd_export_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
297 union sockaddr_union sockaddr = {};
298 socklen_t l = sizeof(sockaddr);
301 if (family != 0 && family != AF_INET && family != AF_INET6)
304 r = sd_is_socket_internal(fd, type, listening);
308 if (getsockname(fd, &sockaddr.sa, &l) < 0)
311 if (l < sizeof(sa_family_t))
314 if (sockaddr.sa.sa_family != AF_INET &&
315 sockaddr.sa.sa_family != AF_INET6)
319 if (sockaddr.sa.sa_family != family)
323 if (sockaddr.sa.sa_family == AF_INET) {
324 if (l < sizeof(struct sockaddr_in))
327 return htons(port) == sockaddr.in4.sin_port;
329 if (l < sizeof(struct sockaddr_in6))
332 return htons(port) == sockaddr.in6.sin6_port;
339 _sd_export_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
340 union sockaddr_union sockaddr = {};
341 socklen_t l = sizeof(sockaddr);
344 r = sd_is_socket_internal(fd, type, listening);
348 if (getsockname(fd, &sockaddr.sa, &l) < 0)
351 if (l < sizeof(sa_family_t))
354 if (sockaddr.sa.sa_family != AF_UNIX)
359 length = strlen(path);
363 return l == offsetof(struct sockaddr_un, sun_path);
366 /* Normal path socket */
368 (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
369 memcmp(path, sockaddr.un.sun_path, length+1) == 0;
371 /* Abstract namespace socket */
373 (l == offsetof(struct sockaddr_un, sun_path) + length) &&
374 memcmp(path, sockaddr.un.sun_path, length) == 0;
380 _sd_export_ int sd_is_mq(int fd, const char *path) {
381 #if !defined(__linux__) || defined(SD_DAEMON_DISABLE_MQ)
389 if (mq_getattr(fd, &attr) < 0)
393 char fpath[PATH_MAX];
399 if (fstat(fd, &a) < 0)
402 strncpy(stpcpy(fpath, "/dev/mqueue"), path, sizeof(fpath) - 12);
403 fpath[sizeof(fpath)-1] = 0;
405 if (stat(fpath, &b) < 0)
408 if (a.st_dev != b.st_dev ||
409 a.st_ino != b.st_ino)
417 _sd_export_ int sd_notify(int unset_environment, const char *state) {
418 #if defined(DISABLE_SYSTEMD) || !defined(__linux__) || !defined(SOCK_CLOEXEC)
422 struct msghdr msghdr;
424 union sockaddr_union sockaddr;
432 e = getenv("NOTIFY_SOCKET");
436 /* Must be an abstract socket, or an absolute path */
437 if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
442 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
448 memset(&sockaddr, 0, sizeof(sockaddr));
449 sockaddr.sa.sa_family = AF_UNIX;
450 strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
452 if (sockaddr.un.sun_path[0] == '@')
453 sockaddr.un.sun_path[0] = 0;
455 memset(&iovec, 0, sizeof(iovec));
456 iovec.iov_base = (char*) state;
457 iovec.iov_len = strlen(state);
459 memset(&msghdr, 0, sizeof(msghdr));
460 msghdr.msg_name = &sockaddr;
461 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(e);
463 if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
464 msghdr.msg_namelen = sizeof(struct sockaddr_un);
466 msghdr.msg_iov = &iovec;
467 msghdr.msg_iovlen = 1;
469 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
477 if (unset_environment)
478 unsetenv("NOTIFY_SOCKET");
487 _sd_export_ int sd_notifyf(int unset_environment, const char *format, ...) {
488 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
495 va_start(ap, format);
496 r = vasprintf(&p, format, ap);
502 r = sd_notify(unset_environment, p);
509 _sd_export_ int sd_booted(void) {
510 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
515 /* We test whether the runtime unit file directory has been
516 * created. This takes place in mount-setup.c, so is
517 * guaranteed to happen very early during boot. */
519 if (lstat("/run/systemd/system/", &st) < 0)
522 return !!S_ISDIR(st.st_mode);