chiark / gitweb /
hostname: if no hostname is configured use localhost
[elogind.git] / src / sd-daemon.c
index 0dad73f94d6064e39bad7b84cadf3dea8b3f8b57..33970f6c41bb1bccc2b10b58d95db4966fbc7cda 100644 (file)
@@ -1,4 +1,4 @@
-/*-*- Mode: C; c-basic-offset: 8 -*-*/
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
 
 /***
   Copyright 2010 Lennart Poettering
@@ -141,7 +141,7 @@ int sd_is_fifo(int fd, const char *path) {
                 struct stat st_path;
 
                 memset(&st_path, 0, sizeof(st_path));
-                if (fstat(fd, &st_path) < 0) {
+                if (stat(path, &st_path) < 0) {
 
                         if (errno == ENOENT || errno == ENOTDIR)
                                 return 0;
@@ -325,7 +325,7 @@ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t
 }
 
 int sd_notify(int unset_environment, const char *state) {
-#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
+#if defined(DISABLE_SYSTEMD) || !defined(__linux__) || !defined(SOCK_CLOEXEC)
         return 0;
 #else
         int fd = -1, r;
@@ -344,10 +344,8 @@ int sd_notify(int unset_environment, const char *state) {
                 goto finish;
         }
 
-        if (!(e = getenv("NOTIFY_SOCKET"))) {
-                r = 0;
-                goto finish;
-        }
+        if (!(e = getenv("NOTIFY_SOCKET")))
+                return 0;
 
         /* Must be an abstract socket, or an absolute path */
         if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
@@ -383,7 +381,11 @@ int sd_notify(int unset_environment, const char *state) {
 
         memset(&msghdr, 0, sizeof(msghdr));
         msghdr.msg_name = &sockaddr;
-        msghdr.msg_namelen = sizeof(struct sockaddr_un);
+        msghdr.msg_namelen = sizeof(sa_family_t) + strlen(e);
+
+        if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
+                msghdr.msg_namelen = sizeof(struct sockaddr_un);
+
         msghdr.msg_iov = &iovec;
         msghdr.msg_iovlen = 1;
         msghdr.msg_control = &control;
@@ -394,7 +396,7 @@ int sd_notify(int unset_environment, const char *state) {
                 goto finish;
         }
 
-        r = 0;
+        r = 1;
 
 finish:
         if (unset_environment)
@@ -428,3 +430,23 @@ int sd_notifyf(int unset_environment, const char *format, ...) {
         return r;
 #endif
 }
+
+int sd_booted(void) {
+#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
+        return 0;
+#else
+
+        struct stat a, b;
+
+        /* We simply test whether the systemd cgroup hierarchy is
+         * mounted */
+
+        if (lstat("/cgroup", &a) < 0)
+                return 0;
+
+        if (lstat("/cgroup/systemd", &b) < 0)
+                return 0;
+
+        return a.st_dev != b.st_dev;
+#endif
+}