chiark / gitweb /
bus: the :no-sender hack is now unnecessary, since the new library doesn't require...
[elogind.git] / src / libsystemd-bus / sd-bus.c
index bc88ac977c96f3c4903995485e0eb95f2b2a6a71..7f00eee6e1b6dcbeee6b2aa601bc0908041a80bb 100644 (file)
@@ -1842,7 +1842,7 @@ static int process_timeout(sd_bus *bus) {
         r = bus_message_new_synthetic_error(
                         bus,
                         c->serial,
-                        &SD_BUS_ERROR_MAKE(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
+                        &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
                         &m);
         if (r < 0)
                 return r;
@@ -1986,7 +1986,7 @@ static int process_builtin(sd_bus *bus, sd_bus_message *m) {
                 return 1;
 
         if (streq_ptr(m->member, "Ping"))
-                r = sd_bus_message_new_method_return(bus, m, &reply);
+                r = sd_bus_message_new_method_return(m, &reply);
         else if (streq_ptr(m->member, "GetMachineId")) {
                 sd_id128_t id;
                 char sid[33];
@@ -1995,14 +1995,14 @@ static int process_builtin(sd_bus *bus, sd_bus_message *m) {
                 if (r < 0)
                         return r;
 
-                r = sd_bus_message_new_method_return(bus, m, &reply);
+                r = sd_bus_message_new_method_return(m, &reply);
                 if (r < 0)
                         return r;
 
                 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
         } else {
                 r = sd_bus_message_new_method_errorf(
-                                bus, m, &reply,
+                                m, &reply,
                                 SD_BUS_ERROR_UNKNOWN_METHOD,
                                  "Unknown method '%s' on interface '%s'.", m->member, m->interface);
         }
@@ -2097,7 +2097,7 @@ static int process_running(sd_bus *bus, sd_bus_message **ret) {
         if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
 
                 r = sd_bus_reply_method_errorf(
-                                bus, m,
+                                m,
                                 SD_BUS_ERROR_UNKNOWN_OBJECT,
                                 "Unknown object '%s'.", m->path);
                 if (r < 0)
@@ -2127,7 +2127,7 @@ static int process_closing(sd_bus *bus, sd_bus_message **ret) {
                 r = bus_message_new_synthetic_error(
                                 bus,
                                 c->serial,
-                                &SD_BUS_ERROR_MAKE(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
+                                &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
                                 &m);
                 if (r < 0)
                         return r;
@@ -2626,17 +2626,25 @@ _public_ int sd_bus_detach_event(sd_bus *bus) {
         assert_return(bus, -EINVAL);
         assert_return(bus->event, -ENXIO);
 
-        if (bus->input_io_event_source)
+        if (bus->input_io_event_source) {
+                sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
                 bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
+        }
 
-        if (bus->output_io_event_source)
+        if (bus->output_io_event_source) {
+                sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF);
                 bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
+        }
 
-        if (bus->time_event_source)
+        if (bus->time_event_source) {
+                sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF);
                 bus->time_event_source = sd_event_source_unref(bus->time_event_source);
+        }
 
-        if (bus->quit_event_source)
+        if (bus->quit_event_source) {
+                sd_event_source_set_enabled(bus->quit_event_source, SD_EVENT_OFF);
                 bus->quit_event_source = sd_event_source_unref(bus->quit_event_source);
+        }
 
         if (bus->event)
                 bus->event = sd_event_unref(bus->event);
@@ -2704,3 +2712,75 @@ _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
 
         return -ENXIO;
 }
+
+_public_ char *sd_bus_label_escape(const char *s) {
+        char *r, *t;
+        const char *f;
+
+        assert_return(s, NULL);
+
+        /* Escapes all chars that D-Bus' object path cannot deal
+         * with. Can be reversed with bus_path_unescape(). We special
+         * case the empty string. */
+
+        if (*s == 0)
+                return strdup("_");
+
+        r = new(char, strlen(s)*3 + 1);
+        if (!r)
+                return NULL;
+
+        for (f = s, t = r; *f; f++) {
+
+                /* Escape everything that is not a-zA-Z0-9. We also
+                 * escape 0-9 if it's the first character */
+
+                if (!(*f >= 'A' && *f <= 'Z') &&
+                    !(*f >= 'a' && *f <= 'z') &&
+                    !(f > s && *f >= '0' && *f <= '9')) {
+                        *(t++) = '_';
+                        *(t++) = hexchar(*f >> 4);
+                        *(t++) = hexchar(*f);
+                } else
+                        *(t++) = *f;
+        }
+
+        *t = 0;
+
+        return r;
+}
+
+_public_ char *sd_bus_label_unescape(const char *f) {
+        char *r, *t;
+
+        assert_return(f, NULL);
+
+        /* Special case for the empty string */
+        if (streq(f, "_"))
+                return strdup("");
+
+        r = new(char, strlen(f) + 1);
+        if (!r)
+                return NULL;
+
+        for (t = r; *f; f++) {
+
+                if (*f == '_') {
+                        int a, b;
+
+                        if ((a = unhexchar(f[1])) < 0 ||
+                            (b = unhexchar(f[2])) < 0) {
+                                /* Invalid escape code, let's take it literal then */
+                                *(t++) = '_';
+                        } else {
+                                *(t++) = (char) ((a << 4) | b);
+                                f += 2;
+                        }
+                } else
+                        *(t++) = *f;
+        }
+
+        *t = 0;
+
+        return r;
+}