chiark / gitweb /
systemctl: use automatic cleanup
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 18 Sep 2012 18:22:57 +0000 (20:22 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 19 Sep 2012 06:25:36 +0000 (08:25 +0200)
Introduce a helper method to unref dbus messages and use it.

src/shared/dbus-common.c
src/shared/dbus-common.h
src/systemctl/systemctl.c

index 0c73d6c..c53bf59 100644 (file)
@@ -1318,3 +1318,9 @@ finish:
 
         return r;
 }
+
+
+void dbus_message_unref_p(DBusMessage **reply) {
+        if (*reply)
+                dbus_message_unref(*reply);
+}
index e49c3b5..7294206 100644 (file)
@@ -213,3 +213,6 @@ int bus_method_call_with_reply(DBusConnection *bus,
                                DBusMessage **return_reply,
                                DBusError *return_error,
                                int first_arg_type, ...);
+
+void dbus_message_unref_p(DBusMessage **reply);
+#define _cleanup_dbus_msg_unref_ __attribute__((cleanup(dbus_message_unref_p)))
index 0e564a5..367dd80 100644 (file)
@@ -1441,22 +1441,22 @@ static void check_triggering_units(
                 DBusConnection *bus,
                 const char *unit_name) {
 
-        DBusMessage *reply = NULL;
+        DBusMessage _cleanup_dbus_msg_unref_ *reply = NULL;
         DBusMessageIter iter, sub;
         char *service_trigger = NULL;
         const char *interface = "org.freedesktop.systemd1.Unit",
                    *triggered_by_property = "TriggeredBy";
 
-        char *unit_path = NULL, *n = NULL;
+        char _cleanup_free_ *unit_path = NULL, *n = NULL;
         bool print_warning_label = true;
         int r;
 
         n = unit_name_mangle(unit_name);
         unit_path = unit_dbus_path_from_name(n ? n : unit_name);
-        free(n);
+
         if (!unit_path) {
                 log_error("Could not allocate dbus path.");
-                goto finish;
+                return;
         }
 
         r = bus_method_call_with_reply (
@@ -1471,13 +1471,12 @@ static void check_triggering_units(
                         DBUS_TYPE_STRING, &triggered_by_property,
                         DBUS_TYPE_INVALID);
         if (r)
-                goto finish;
+                return;
 
         if (!dbus_message_iter_init(reply, &iter) ||
             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
                 log_error("Failed to parse reply.");
-                goto finish;
-
+                return;
         }
 
         dbus_message_iter_recurse(&iter, &sub);
@@ -1488,14 +1487,14 @@ static void check_triggering_units(
 
                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
                         log_error("Failed to parse reply.");
-                        goto finish;
+                        return;
                 }
 
                 dbus_message_iter_get_basic(&sub, &service_trigger);
 
                 r = check_one_unit(bus, service_trigger, true);
                 if (r < 0)
-                        goto finish;
+                        return;
                 if (r == 0) {
                         if (print_warning_label) {
                                 log_warning("Warning: Stopping %s, but it can still be activated by:", unit_name);
@@ -1506,11 +1505,6 @@ static void check_triggering_units(
 
                 dbus_message_iter_next(&sub);
         }
-finish:
-        if (reply)
-                dbus_message_unref(reply);
-
-        free(unit_path);
 }
 
 static int start_unit_one(