31 #include <sys/types.h>
33 #include <sys/socket.h>
36 #include <netinet/in.h>
46 #if defined(__linux__) && !defined(SD_DAEMON_DISABLE_MQ)
50 #include "sd-daemon.h"
53 # ifdef SD_EXPORT_SYMBOLS
55 # define _sd_export_ __attribute__ ((visibility("default")))
58 # define _sd_export_ __attribute__ ((visibility("hidden")))
64 _sd_export_
int sd_listen_fds(
int unset_environment) {
66 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
74 e = getenv(
"LISTEN_PID");
81 l = strtoul(e, &p, 10);
88 if (!p || p == e || *p || l <= 0) {
94 if (getpid() != (pid_t) l) {
99 e = getenv(
"LISTEN_FDS");
106 l = strtoul(e, &p, 10);
113 if (!p || p == e || *p) {
118 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) l; fd ++) {
121 flags = fcntl(fd, F_GETFD);
127 if (flags & FD_CLOEXEC)
130 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
139 if (unset_environment) {
140 unsetenv(
"LISTEN_PID");
141 unsetenv(
"LISTEN_FDS");
148 _sd_export_
int sd_is_fifo(
int fd,
const char *path) {
154 if (fstat(fd, &st_fd) < 0)
157 if (!S_ISFIFO(st_fd.st_mode))
163 if (stat(path, &st_path) < 0) {
165 if (errno == ENOENT || errno == ENOTDIR)
172 st_path.st_dev == st_fd.st_dev &&
173 st_path.st_ino == st_fd.st_ino;
179 _sd_export_
int sd_is_special(
int fd,
const char *path) {
185 if (fstat(fd, &st_fd) < 0)
188 if (!S_ISREG(st_fd.st_mode) && !S_ISCHR(st_fd.st_mode))
194 if (stat(path, &st_path) < 0) {
196 if (errno == ENOENT || errno == ENOTDIR)
202 if (S_ISREG(st_fd.st_mode) && S_ISREG(st_path.st_mode))
204 st_path.st_dev == st_fd.st_dev &&
205 st_path.st_ino == st_fd.st_ino;
206 else if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode))
207 return st_path.st_rdev == st_fd.st_rdev;
215 static int sd_is_socket_internal(
int fd,
int type,
int listening) {
218 if (fd < 0 || type < 0)
221 if (fstat(fd, &st_fd) < 0)
224 if (!S_ISSOCK(st_fd.st_mode))
229 socklen_t l =
sizeof(other_type);
231 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
234 if (l !=
sizeof(other_type))
237 if (other_type != type)
241 if (listening >= 0) {
243 socklen_t l =
sizeof(accepting);
245 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
248 if (l !=
sizeof(accepting))
251 if (!accepting != !listening)
260 struct sockaddr_in in4;
261 struct sockaddr_in6 in6;
262 struct sockaddr_un un;
263 struct sockaddr_storage storage;
266 _sd_export_
int sd_is_socket(
int fd,
int family,
int type,
int listening) {
272 r = sd_is_socket_internal(fd, type, listening);
278 socklen_t l =
sizeof(sockaddr);
280 if (getsockname(fd, &sockaddr.sa, &l) < 0)
283 if (l <
sizeof(sa_family_t))
286 return sockaddr.sa.sa_family == family;
292 _sd_export_
int sd_is_socket_inet(
int fd,
int family,
int type,
int listening, uint16_t port) {
294 socklen_t l =
sizeof(sockaddr);
297 if (family != 0 && family != AF_INET && family != AF_INET6)
300 r = sd_is_socket_internal(fd, type, listening);
304 if (getsockname(fd, &sockaddr.sa, &l) < 0)
307 if (l <
sizeof(sa_family_t))
310 if (sockaddr.sa.sa_family != AF_INET &&
311 sockaddr.sa.sa_family != AF_INET6)
315 if (sockaddr.sa.sa_family != family)
319 if (sockaddr.sa.sa_family == AF_INET) {
320 if (l <
sizeof(
struct sockaddr_in))
323 return htons(port) == sockaddr.in4.sin_port;
325 if (l <
sizeof(
struct sockaddr_in6))
328 return htons(port) == sockaddr.in6.sin6_port;
335 _sd_export_
int sd_is_socket_unix(
int fd,
int type,
int listening,
const char *path,
size_t length) {
337 socklen_t l =
sizeof(sockaddr);
340 r = sd_is_socket_internal(fd, type, listening);
344 if (getsockname(fd, &sockaddr.sa, &l) < 0)
347 if (l <
sizeof(sa_family_t))
350 if (sockaddr.sa.sa_family != AF_UNIX)
355 length = strlen(path);
359 return l == offsetof(
struct sockaddr_un, sun_path);
364 (l >= offsetof(
struct sockaddr_un, sun_path) + length + 1) &&
365 memcmp(path, sockaddr.un.sun_path, length+1) == 0;
369 (l == offsetof(
struct sockaddr_un, sun_path) + length) &&
370 memcmp(path, sockaddr.un.sun_path, length) == 0;
376 _sd_export_
int sd_is_mq(
int fd,
const char *path) {
377 #if !defined(__linux__) || defined(SD_DAEMON_DISABLE_MQ)
385 if (mq_getattr(fd, &attr) < 0)
389 char fpath[PATH_MAX];
395 if (fstat(fd, &a) < 0)
398 strncpy(stpcpy(fpath,
"/dev/mqueue"), path,
sizeof(fpath) - 12);
399 fpath[
sizeof(fpath)-1] = 0;
401 if (stat(fpath, &b) < 0)
404 if (a.st_dev != b.st_dev ||
405 a.st_ino != b.st_ino)
413 _sd_export_
int sd_notify(
int unset_environment,
const char *state) {
414 #if defined(DISABLE_SYSTEMD) || !defined(__linux__) || !defined(SOCK_CLOEXEC)
418 struct msghdr msghdr;
428 e = getenv(
"NOTIFY_SOCKET");
433 if ((e[0] !=
'@' && e[0] !=
'/') || e[1] == 0) {
438 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
444 memset(&sockaddr, 0,
sizeof(sockaddr));
445 sockaddr.sa.sa_family = AF_UNIX;
446 strncpy(sockaddr.un.sun_path, e,
sizeof(sockaddr.un.sun_path));
448 if (sockaddr.un.sun_path[0] ==
'@')
449 sockaddr.un.sun_path[0] = 0;
451 memset(&iovec, 0,
sizeof(iovec));
452 iovec.iov_base = (
char*) state;
453 iovec.iov_len = strlen(state);
455 memset(&msghdr, 0,
sizeof(msghdr));
456 msghdr.msg_name = &sockaddr;
457 msghdr.msg_namelen = offsetof(
struct sockaddr_un, sun_path) + strlen(e);
459 if (msghdr.msg_namelen >
sizeof(
struct sockaddr_un))
460 msghdr.msg_namelen =
sizeof(
struct sockaddr_un);
462 msghdr.msg_iov = &iovec;
463 msghdr.msg_iovlen = 1;
465 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
473 if (unset_environment)
474 unsetenv(
"NOTIFY_SOCKET");
483 _sd_export_
int sd_notifyf(
int unset_environment,
const char *format, ...) {
484 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
491 va_start(ap, format);
492 r = vasprintf(&p, format, ap);
498 r = sd_notify(unset_environment, p);
505 _sd_export_
int sd_booted(
void) {
506 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
515 if (lstat(
"/run/systemd/system/", &st) < 0)
518 return !!S_ISDIR(st.st_mode);
#define NULL
A null pointer, defined appropriately for C or C++.