chiark / gitweb /
sd-bus: rename default bus address constants, they aren't "paths" but "addresses"
[elogind.git] / src / dbus1-generator / dbus1-generator.c
index e86fc6e56aef2392a95d1961cf16848b91640d64..5d3b1a856afa8281b1066d427b7348c1f9e46ab6 100644 (file)
@@ -40,6 +40,7 @@ static int create_dbus_files(
 
         _cleanup_free_ char *b = NULL, *s = NULL, *lnk = NULL;
         _cleanup_fclose_ FILE *f = NULL;
+        int r;
 
         assert(path);
         assert(name);
@@ -66,7 +67,8 @@ static int create_dbus_files(
                         "# Automatically generated by systemd-dbus1-generator\n\n"
                         "[Unit]\n"
                         "SourcePath=%s\n"
-                        "Description=DBUS1: %s\n\n"
+                        "Description=DBUS1: %s\n"
+                        "Documentation=man:systemd-dbus1-generator(8)\n\n"
                         "[Service]\n"
                         "ExecStart=%s\n"
                         "Type=dbus\n"
@@ -84,17 +86,30 @@ static int create_dbus_files(
                         fprintf(f, "Environment=DBUS_STARTER_BUS_TYPE=%s\n", type);
 
                         if (streq(type, "system"))
-                                fprintf(f, "Environment=DBUS_STARTER_ADDRESS=kernel:/dev/kdbus/0-system\n");
-                        else if (streq(type, "session"))
-                                fprintf(f, "Environment=DBUS_STARTER_ADDRESS=kernel:/dev/kdbus/%lu-user\n", (unsigned long) getuid());
+                                fprintf(f, "Environment=DBUS_STARTER_ADDRESS=" DEFAULT_SYSTEM_BUS_ADDRESS "\n");
+                        else if (streq(type, "session")) {
+                                char *run;
+
+                                run = getenv("XDG_RUNTIME_DIR");
+                                if (!run) {
+                                        log_error("XDG_RUNTIME_DIR not set.");
+                                        return -EINVAL;
+                                }
+
+                                fprintf(f, "Environment=DBUS_STARTER_ADDRESS="KERNEL_USER_BUS_ADDRESS_FMT ";" UNIX_USER_BUS_ADDRESS_FMT "\n",
+                                        getuid(), run);
+                        }
                 }
 
-                fflush(f);
-                if (ferror(f)) {
-                        log_error("Failed to write %s: %m", a);
-                        return -errno;
+                r = fflush_and_check(f);
+                if (r < 0) {
+                        log_error_errno(r, "Failed to write %s: %m", a);
+                        return r;
                 }
 
+                fclose(f);
+                f = NULL;
+
                 service = s;
         }
 
@@ -112,19 +127,21 @@ static int create_dbus_files(
                 "# Automatically generated by systemd-dbus1-generator\n\n"
                 "[Unit]\n"
                 "SourcePath=%s\n"
-                "Description=DBUS1: %s\n\n"
+                "Description=DBUS1: %s\n"
+                "Documentation=man:systemd-dbus1-generator(8)\n\n"
                 "[BusName]\n"
                 "Name=%s\n"
-                "Service=%s\n",
+                "Service=%s\n"
+                "AllowWorld=talk\n",
                 path,
                 name,
                 name,
                 service);
 
-        fflush(f);
-        if (ferror(f)) {
-                log_error("Failed to write %s: %m", b);
-                return -errno;
+        r = fflush_and_check(f);
+        if (r < 0) {
+                log_error_errno(r, "Failed to write %s: %m", b);
+                return r;
         }
 
         lnk = strjoin(arg_dest_late, "/" SPECIAL_BUSNAMES_TARGET ".wants/", name, ".busname", NULL);
@@ -143,34 +160,24 @@ static int create_dbus_files(
 static int add_dbus(const char *path, const char *fname, const char *type) {
         _cleanup_free_ char *name = NULL, *exec = NULL, *user = NULL, *service = NULL;
 
-        ConfigTableItem table[] = {
+        const ConfigTableItem table[] = {
                 { "D-BUS Service", "Name", config_parse_string, 0, &name },
                 { "D-BUS Service", "Exec", config_parse_string, 0, &exec },
                 { "D-BUS Service", "User", config_parse_string, 0, &user },
                 { "D-BUS Service", "SystemdService", config_parse_string, 0, &service },
         };
 
-        _cleanup_fclose_ FILE *f = NULL;
-        _cleanup_free_ char *p = NULL;
+        char *p;
         int r;
 
         assert(path);
         assert(fname);
 
-        p = strjoin(path, "/", fname, NULL);
-        if (!p)
-                return log_oom();
-
-        f = fopen(p, "re");
-        if (!f) {
-                if (errno == -ENOENT)
-                        return 0;
-
-                log_error("Failed to read %s: %m", p);
-                return -errno;
-        }
-
-        r = config_parse(NULL, p, f, "D-BUS Service\0", config_item_table_lookup, table, true, false, NULL);
+        p = strappenda(path, "/", fname);
+        r = config_parse(NULL, p, NULL,
+                         "D-BUS Service\0",
+                         config_item_table_lookup, table,
+                         true, false, true, NULL);
         if (r < 0)
                 return r;
 
@@ -190,7 +197,7 @@ static int add_dbus(const char *path, const char *fname, const char *type) {
         }
 
         if (service) {
-                if (!unit_name_is_valid(service, false)) {
+                if (!unit_name_is_valid(service, TEMPLATE_INVALID)) {
                         log_warning("Unit name %s is not valid, ignoring.", service);
                         return 0;
                 }
@@ -267,15 +274,28 @@ static int link_busnames_target(const char *units) {
 static int link_compatibility(const char *units) {
         const char *f, *t;
 
-        f = strappenda(units, "/systemd-socket-proxy.socket");
+        f = strappenda(units, "/systemd-bus-proxyd.socket");
         t = strappenda(arg_dest, "/" SPECIAL_DBUS_SOCKET);
+        mkdir_parents_label(t, 0755);
+        if (symlink(f, t) < 0) {
+                log_error("Failed to create symlink %s: %m", t);
+                return -errno;
+        }
 
+        f = strappenda(units, "/systemd-bus-proxyd.socket");
+        t = strappenda(arg_dest, "/" SPECIAL_SOCKETS_TARGET ".wants/systemd-bus-proxyd.socket");
         mkdir_parents_label(t, 0755);
         if (symlink(f, t) < 0) {
                 log_error("Failed to create symlink %s: %m", t);
                 return -errno;
         }
 
+        t = strappenda(arg_dest, "/" SPECIAL_DBUS_SERVICE);
+        if (symlink("/dev/null", t) < 0) {
+                log_error("Failed to mask %s: %m", t);
+                return -errno;
+        }
+
         return 0;
 }
 
@@ -299,7 +319,7 @@ int main(int argc, char *argv[]) {
 
         umask(0022);
 
-        if (access("/dev/kdbus/control", F_OK) < 0)
+        if (access("/sys/fs/kdbus/control", F_OK) < 0)
                 return 0;
 
         r = cg_pid_get_owner_uid(0, NULL);
@@ -312,7 +332,7 @@ int main(int argc, char *argv[]) {
                 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));
+                log_error_errno(r, "Failed to determine whether we are running as user or system instance: %m");
                 return r;
         }