chiark / gitweb /
nspawn: fix buggy mount_binds, now works for bind-mounted files
[elogind.git] / src / dbus1-generator / dbus1-generator.c
index 93c6392f8349f0f7a240ab5de74253d1a2705f3d..dd299edc75e8f62c0e7c5f118bd5fb08bab12c09 100644 (file)
@@ -43,6 +43,7 @@ static int create_dbus_files(
 
         assert(path);
         assert(name);
+        assert(service || exec);
 
         if (!service) {
                 _cleanup_free_ char *a = NULL;
@@ -64,7 +65,7 @@ static int create_dbus_files(
                 fprintf(f,
                         "# Automatically generated by systemd-dbus1-generator\n\n"
                         "[Unit]\n"
-                        "Source=%s\n"
+                        "SourcePath=%s\n"
                         "Description=DBUS1: %s\n\n"
                         "[Service]\n"
                         "ExecStart=%s\n"
@@ -110,7 +111,7 @@ static int create_dbus_files(
         fprintf(f,
                 "# Automatically generated by systemd-dbus1-generator\n\n"
                 "[Unit]\n"
-                "Source=%s\n"
+                "SourcePath=%s\n"
                 "Description=DBUS1: %s\n\n"
                 "[BusName]\n"
                 "Name=%s\n"
@@ -132,7 +133,7 @@ static int create_dbus_files(
 
         mkdir_parents_label(lnk, 0755);
         if (symlink(b, lnk)) {
-                log_error("Failed to create symlinks %s: %m", lnk);
+                log_error("Failed to create symlink %s: %m", lnk);
                 return -errno;
         }
 
@@ -183,6 +184,11 @@ static int add_dbus(const char *path, const char *fname, const char *type) {
                 return 0;
         }
 
+        if (streq(name, "org.freedesktop.systemd1")) {
+                log_debug("Skipping %s, identified as systemd.", p);
+                return 0;
+        }
+
         if (service) {
                 if (!unit_name_is_valid(service, false)) {
                         log_warning("Unit name %s is not valid, ignoring.", service);
@@ -193,7 +199,7 @@ static int add_dbus(const char *path, const char *fname, const char *type) {
                         return 0;
                 }
         } else {
-                if (!exec) {
+                if (streq(exec, "/bin/false") || !exec) {
                         log_warning("Neither service name nor binary path specified, ignoring %s.", p);
                         return 0;
                 }
@@ -207,25 +213,15 @@ static int add_dbus(const char *path, const char *fname, const char *type) {
         return create_dbus_files(p, name, service, exec, user, type);
 }
 
-static int parse_dbus_fragments(void) {
+static int parse_dbus_fragments(const char *path, const char *type) {
         _cleanup_closedir_ DIR *d = NULL;
         struct dirent *de;
-        const char *p, *type;
         int r;
 
-        r = cg_pid_get_owner_uid(0, NULL);
-        if (r >= 0) {
-                p = "/usr/share/dbus-1/services";
-                type = "session";
-        } else if (r == -ENOENT) {
-                p = "/usr/share/dbus-1/system-services";
-                type = "systemd";
-        } else if (r < 0) {
-                log_error("Failed to determine whether we are running as user or system instance: %s", strerror(-r));
-                return r;
-        }
+        assert(path);
+        assert(type);
 
-        d = opendir(p);
+        d = opendir(path);
         if (!d) {
                 if (errno == -ENOENT)
                         return 0;
@@ -241,7 +237,7 @@ static int parse_dbus_fragments(void) {
                 if (!endswith(de->d_name, ".service"))
                         continue;
 
-                q = add_dbus(p, de->d_name, type);
+                q = add_dbus(path, de->d_name, type);
                 if (q < 0)
                         r = q;
         }
@@ -253,8 +249,24 @@ fail:
         return -errno;
 }
 
+static int link_busnames_target(const char *units) {
+        const char *f, *t;
+
+        f = strappenda(units, "/" SPECIAL_BUSNAMES_TARGET);
+        t = strappenda(arg_dest, "/" SPECIAL_BASIC_TARGET ".wants/" SPECIAL_BUSNAMES_TARGET);
+
+        mkdir_parents_label(t, 0755);
+        if (symlink(f, t) < 0) {
+                log_error("Failed to create symlink %s: %m", t);
+                return -errno;
+        }
+
+        return 0;
+}
+
 int main(int argc, char *argv[]) {
-        int r;
+        const char *path, *type, *units;
+        int r, q;
 
         if (argc > 1 && argc != 4) {
                 log_error("This program takes three or no arguments.");
@@ -273,7 +285,26 @@ int main(int argc, char *argv[]) {
         if (access("/dev/kdbus/control", F_OK) < 0)
                 return 0;
 
-        r = parse_dbus_fragments();
+        r = cg_pid_get_owner_uid(0, NULL);
+        if (r >= 0) {
+                path = "/usr/share/dbus-1/services";
+                type = "session";
+                units = USER_DATA_UNIT_PATH;
+        } else if (r == -ENOENT) {
+                path = "/usr/share/dbus-1/system-services";
+                type = "system";
+                units = SYSTEM_DATA_UNIT_PATH;
+        } else {
+                log_error("Failed to determine whether we are running as user or system instance: %s", strerror(-r));
+                return r;
+        }
+
+        r = parse_dbus_fragments(path, type);
+
+        /* FIXME: One day this should just be pulled in statically from basic.target */
+        q = link_busnames_target(units);
+        if (q < 0)
+                r = q;
 
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }