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 <netinet/in.h>
46 #if defined(__linux__) && !defined(SD_DAEMON_DISABLE_MQ)
51 #include "sd-daemon.h"
54 # ifdef SD_EXPORT_SYMBOLS
56 # define _sd_export_ __attribute__ ((visibility("default")))
58 /* Don't export the symbols */
59 # define _sd_export_ __attribute__ ((visibility("hidden")))
65 _sd_export_ int sd_listen_fds(int unset_environment) {
67 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
75 e = getenv("LISTEN_PID");
82 l = strtoul(e, &p, 10);
89 if (!p || p == e || *p || l <= 0) {
95 if (getpid() != (pid_t) l) {
100 e = getenv("LISTEN_FDS");
107 l = strtoul(e, &p, 10);
114 if (!p || p == e || *p) {
119 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) l; fd ++) {
122 flags = fcntl(fd, F_GETFD);
128 if (flags & FD_CLOEXEC)
131 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
140 if (unset_environment) {
141 unsetenv("LISTEN_PID");
142 unsetenv("LISTEN_FDS");
149 _sd_export_ int sd_is_fifo(int fd, const char *path) {
155 if (fstat(fd, &st_fd) < 0)
158 if (!S_ISFIFO(st_fd.st_mode))
164 if (stat(path, &st_path) < 0) {
166 if (errno == ENOENT || errno == ENOTDIR)
173 st_path.st_dev == st_fd.st_dev &&
174 st_path.st_ino == st_fd.st_ino;
180 _sd_export_ int sd_is_special(int fd, const char *path) {
186 if (fstat(fd, &st_fd) < 0)
189 if (!S_ISREG(st_fd.st_mode) && !S_ISCHR(st_fd.st_mode))
195 if (stat(path, &st_path) < 0) {
197 if (errno == ENOENT || errno == ENOTDIR)
203 if (S_ISREG(st_fd.st_mode) && S_ISREG(st_path.st_mode))
205 st_path.st_dev == st_fd.st_dev &&
206 st_path.st_ino == st_fd.st_ino;
207 else if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode))
208 return st_path.st_rdev == st_fd.st_rdev;
216 static int sd_is_socket_internal(int fd, int type, int listening) {
219 if (fd < 0 || type < 0)
222 if (fstat(fd, &st_fd) < 0)
225 if (!S_ISSOCK(st_fd.st_mode))
230 socklen_t l = sizeof(other_type);
232 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
235 if (l != sizeof(other_type))
238 if (other_type != type)
242 if (listening >= 0) {
244 socklen_t l = sizeof(accepting);
246 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
249 if (l != sizeof(accepting))
252 if (!accepting != !listening)
259 union sockaddr_union {
261 struct sockaddr_in in4;
262 struct sockaddr_in6 in6;
263 struct sockaddr_un un;
264 struct sockaddr_storage storage;
267 _sd_export_ int sd_is_socket(int fd, int family, int type, int listening) {
273 r = sd_is_socket_internal(fd, type, listening);
278 union sockaddr_union sockaddr = {};
279 socklen_t l = sizeof(sockaddr);
281 if (getsockname(fd, &sockaddr.sa, &l) < 0)
284 if (l < sizeof(sa_family_t))
287 return sockaddr.sa.sa_family == family;
293 _sd_export_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
294 union sockaddr_union sockaddr = {};
295 socklen_t l = sizeof(sockaddr);
298 if (family != 0 && family != AF_INET && family != AF_INET6)
301 r = sd_is_socket_internal(fd, type, listening);
305 if (getsockname(fd, &sockaddr.sa, &l) < 0)
308 if (l < sizeof(sa_family_t))
311 if (sockaddr.sa.sa_family != AF_INET &&
312 sockaddr.sa.sa_family != AF_INET6)
316 if (sockaddr.sa.sa_family != family)
320 if (sockaddr.sa.sa_family == AF_INET) {
321 if (l < sizeof(struct sockaddr_in))
324 return htons(port) == sockaddr.in4.sin_port;
326 if (l < sizeof(struct sockaddr_in6))
329 return htons(port) == sockaddr.in6.sin6_port;
336 _sd_export_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
337 union sockaddr_union sockaddr = {};
338 socklen_t l = sizeof(sockaddr);
341 r = sd_is_socket_internal(fd, type, listening);
345 if (getsockname(fd, &sockaddr.sa, &l) < 0)
348 if (l < sizeof(sa_family_t))
351 if (sockaddr.sa.sa_family != AF_UNIX)
356 length = strlen(path);
360 return l == offsetof(struct sockaddr_un, sun_path);
363 /* Normal path socket */
365 (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
366 memcmp(path, sockaddr.un.sun_path, length+1) == 0;
368 /* Abstract namespace socket */
370 (l == offsetof(struct sockaddr_un, sun_path) + length) &&
371 memcmp(path, sockaddr.un.sun_path, length) == 0;
377 _sd_export_ int sd_is_mq(int fd, const char *path) {
378 #if !defined(__linux__) || defined(SD_DAEMON_DISABLE_MQ)
386 if (mq_getattr(fd, &attr) < 0)
390 char fpath[PATH_MAX];
396 if (fstat(fd, &a) < 0)
399 strncpy(stpcpy(fpath, "/dev/mqueue"), path, sizeof(fpath) - 12);
400 fpath[sizeof(fpath)-1] = 0;
402 if (stat(fpath, &b) < 0)
405 if (a.st_dev != b.st_dev ||
406 a.st_ino != b.st_ino)
414 _sd_export_ int sd_notify(int unset_environment, const char *state) {
415 #if defined(DISABLE_SYSTEMD) || !defined(__linux__) || !defined(SOCK_CLOEXEC)
419 struct msghdr msghdr;
421 union sockaddr_union sockaddr;
429 e = getenv("NOTIFY_SOCKET");
433 /* Must be an abstract socket, or an absolute path */
434 if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
439 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
445 memzero(&sockaddr, sizeof(sockaddr));
446 sockaddr.sa.sa_family = AF_UNIX;
447 strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
449 if (sockaddr.un.sun_path[0] == '@')
450 sockaddr.un.sun_path[0] = 0;
452 memzero(&iovec, sizeof(iovec));
453 iovec.iov_base = (char*) state;
454 iovec.iov_len = strlen(state);
456 memzero(&msghdr, sizeof(msghdr));
457 msghdr.msg_name = &sockaddr;
458 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(e);
460 if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
461 msghdr.msg_namelen = sizeof(struct sockaddr_un);
463 msghdr.msg_iov = &iovec;
464 msghdr.msg_iovlen = 1;
466 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
474 if (unset_environment)
475 unsetenv("NOTIFY_SOCKET");
484 _sd_export_ int sd_notifyf(int unset_environment, const char *format, ...) {
485 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
492 va_start(ap, format);
493 r = vasprintf(&p, format, ap);
499 r = sd_notify(unset_environment, p);
506 _sd_export_ int sd_booted(void) {
507 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
512 /* We test whether the runtime unit file directory has been
513 * created. This takes place in mount-setup.c, so is
514 * guaranteed to happen very early during boot. */
516 if (lstat("/run/systemd/system/", &st) < 0)
519 return !!S_ISDIR(st.st_mode);
523 _sd_export_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
525 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
528 unsigned long long ll;
534 e = getenv("WATCHDOG_PID");
541 l = strtoul(e, &p, 10);
546 if (!p || p == e || *p || l <= 0) {
551 /* Is this for us? */
552 if (getpid() != (pid_t) l) {
557 e = getenv("WATCHDOG_USEC");
564 ll = strtoull(e, &p, 10);
569 if (!p || p == e || *p || l <= 0) {
580 if (unset_environment) {
581 unsetenv("WATCHDOG_PID");
582 unsetenv("WATCHDOG_USEC");