chiark / gitweb /
move bus_method_call_with_reply() to dbus-common
authorSimon Peeters <peeters.simon@gmail.com>
Wed, 8 Aug 2012 00:04:40 +0000 (02:04 +0200)
committerSimon Peeters <peeters.simon@gmail.com>
Wed, 8 Aug 2012 00:04:40 +0000 (02:04 +0200)
src/shared/dbus-common.c
src/shared/dbus-common.h
src/systemctl/systemctl.c

index 5d64568c3480c5bad20f9810064441df194f0af8..7f0dce5ad393eda0b6292f3e3d793390ffa09a26 100644 (file)
@@ -1237,3 +1237,76 @@ finish:
 
         return (pid_t) pid;
 }
+
+bool bus_error_is_no_service(const DBusError *error) {
+        assert(error);
+
+        if (!dbus_error_is_set(error))
+                return false;
+
+        if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
+                return true;
+
+        if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
+                return true;
+
+        return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
+}
+
+int bus_method_call_with_reply(DBusConnection *bus,
+                                       const char *destination,
+                                       const char *path,
+                                       const char *interface,
+                                       const char *method,
+                                       DBusMessage **return_reply,
+                                       DBusError *return_error,
+                                       int first_arg_type, ...) {
+        DBusError error;
+        DBusMessage *m, *reply;
+        va_list ap;
+        int r = 0;
+
+        dbus_error_init(&error);
+        assert(bus);
+
+        m = dbus_message_new_method_call(destination, path, interface, method);
+        if (!m) {
+                r = log_oom();
+                goto finish;
+        }
+
+        va_start(ap, first_arg_type);
+        if (!dbus_message_append_args_valist(m, first_arg_type, ap)) {
+                va_end(ap);
+                dbus_message_unref(m);
+                r = log_oom();
+                goto finish;
+        }
+        va_end(ap);
+
+        reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
+        dbus_message_unref(m);
+        if (!reply) {
+                log_error("Failed to issue method call: %s", bus_error_message(&error));
+                if (bus_error_is_no_service(&error))
+                        r = -ENOENT;
+                else if (dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED))
+                        r = -EACCES;
+                else if (dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY))
+                        r = -ETIMEDOUT;
+                else
+                        r = -EIO;
+                goto finish;
+        }
+        if (return_reply)
+                *return_reply = reply;
+        else
+                dbus_message_unref(reply);
+finish:
+        if(return_error)
+                *return_error=error;
+        else
+                dbus_error_free(&error);
+
+        return r;
+}
index a6703a78d06da2305830806de0f7237ba54cb72b..e49c3b5258816dcd05f0da3b26cf354cf3ad9a9e 100644 (file)
@@ -203,3 +203,13 @@ void bus_async_unregister_and_exit(DBusConnection *bus, const char *name);
 DBusHandlerResult bus_exit_idle_filter(DBusConnection *bus, DBusMessage *m, void *userdata);
 
 pid_t bus_get_unix_process_id(DBusConnection *connection, const char *name, DBusError *error);
+
+bool bus_error_is_no_service(const DBusError *error);
+int bus_method_call_with_reply(DBusConnection *bus,
+                               const char *destination,
+                               const char *path,
+                               const char *interface,
+                               const char *method,
+                               DBusMessage **return_reply,
+                               DBusError *return_error,
+                               int first_arg_type, ...);
index 1c06507e4cefbeb839ce8de7fe029d6eaa06739f..2e0aaaa9fadbba410b781e1b35ab78e7b7a5c446 100644 (file)
@@ -202,21 +202,6 @@ static const char *ansi_highlight_green(bool b) {
         return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
 }
 
-static bool error_is_no_service(const DBusError *error) {
-        assert(error);
-
-        if (!dbus_error_is_set(error))
-                return false;
-
-        if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
-                return true;
-
-        if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
-                return true;
-
-        return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
-}
-
 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
         assert(error);
 
@@ -245,64 +230,6 @@ static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
         return EXIT_FAILURE;
 }
 
-static int bus_method_call_with_reply(DBusConnection *bus,
-                                       const char *destination,
-                                       const char *path,
-                                       const char *interface,
-                                       const char *method,
-                                       DBusMessage **return_reply,
-                                       DBusError *return_error,
-                                       int first_arg_type, ...) {
-        DBusError error;
-        DBusMessage *m, *reply;
-        va_list ap;
-        int r = 0;
-
-        dbus_error_init(&error);
-        assert(bus);
-
-        m = dbus_message_new_method_call(destination, path, interface, method);
-        if (!m) {
-                r = log_oom();
-                goto finish;
-        }
-
-        va_start(ap, first_arg_type);
-        if (!dbus_message_append_args_valist(m, first_arg_type, ap)) {
-                va_end(ap);
-                dbus_message_unref(m);
-                r = log_oom();
-                goto finish;
-        }
-        va_end(ap);
-
-        reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
-        dbus_message_unref(m);
-        if (!reply) {
-                log_error("Failed to issue method call: %s", bus_error_message(&error));
-                if (error_is_no_service(&error))
-                        r = -ENOENT;
-                else if (dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED))
-                        r = -EACCES;
-                else if (dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY))
-                        r = -ETIMEDOUT;
-                else
-                        r = -EIO;
-                goto finish;
-        }
-        if (return_reply)
-                *return_reply = reply;
-        else
-                dbus_message_unref(reply);
-finish:
-        if(return_error)
-                *return_error=error;
-        else
-                dbus_error_free(&error);
-
-        return r;
-}
-
 static void warn_wall(enum action a) {
         static const char *table[_ACTION_MAX] = {
                 [ACTION_HALT]            = "The system is going down for system halt NOW!",
@@ -4826,7 +4753,7 @@ static int talk_upstart(void) {
 
         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
 
-                if (error_is_no_service(&error)) {
+                if (bus_error_is_no_service(&error)) {
                         r = -EADDRNOTAVAIL;
                         goto finish;
                 }