]> err.no Git - systemd/commitdiff
systemctl: add --block switch for synchronous starting of units
authorLennart Poettering <lennart@poettering.net>
Sat, 22 May 2010 02:27:24 +0000 (04:27 +0200)
committerLennart Poettering <lennart@poettering.net>
Sat, 22 May 2010 02:27:24 +0000 (04:27 +0200)
fixme
src/dbus-job.c
src/dbus-job.h
src/dbus-manager.c
src/dbus-unit.c
src/job.c
src/job.h
src/systemadm.vala
src/systemctl.vala
src/systemd-interfaces.vala

diff --git a/fixme b/fixme
index 513c32b69c8d8b38cbc4b6432241d05b9bb8cf88..0585d1d6f3d94469535b8392c5e748c61f9f86b6 100644 (file)
--- a/fixme
+++ b/fixme
 
 * teach dbus to talk to systemd when autospawning services
 
-* Figure out which signal handlers we actually have to reset in the
-  forked off child
-
-* Add code to systemctl to wait for an operation to finish
-
 Regularly:
 
 * look for close() vs. close_nointr() vs. close_nointr_nofail()
index 3a6e7159e9959edc41a9623d8ec2d4e90896245b..34e64fc6812e457de3b36255956a6efdea230a0a 100644 (file)
@@ -197,15 +197,19 @@ oom:
         log_error("Failed to allocate job change signal.");
 }
 
-void bus_job_send_removed_signal(Job *j) {
+void bus_job_send_removed_signal(Job *j, bool success) {
         char *p = NULL;
         DBusMessage *m = NULL;
+        dbus_bool_t b = success;
 
         assert(j);
 
-        if (set_isempty(j->manager->subscribed) || !j->sent_dbus_new_signal)
+        if (set_isempty(j->manager->subscribed))
                 return;
 
+        if (!j->sent_dbus_new_signal)
+                bus_job_send_change_signal(j);
+
         if (!(p = job_dbus_path(j)))
                 goto oom;
 
@@ -215,6 +219,7 @@ void bus_job_send_removed_signal(Job *j) {
         if (!dbus_message_append_args(m,
                                       DBUS_TYPE_UINT32, &j->id,
                                       DBUS_TYPE_OBJECT_PATH, &p,
+                                      DBUS_TYPE_BOOLEAN, &b,
                                       DBUS_TYPE_INVALID))
                 goto oom;
 
index cf9176052db0b9085672da4071d0833388936b3c..460235885645f93a6189de70f270716cb33f38cd 100644 (file)
@@ -25,7 +25,7 @@
 #include <dbus/dbus.h>
 
 void bus_job_send_change_signal(Job *j);
-void bus_job_send_removed_signal(Job *j);
+void bus_job_send_removed_signal(Job *j, bool success);
 
 extern const DBusObjectPathVTable bus_job_vtable;
 
index 90ab8d1a0cab509b1d78270191962a65bf0a8842..9833b6c85f9b616e16b74c6a5ed1372b5ea04744 100644 (file)
@@ -81,6 +81,7 @@
         "  <signal name=\"JobRemoved\">"                                \
         "   <arg name=\"id\" type=\"u\"/>"                              \
         "   <arg name=\"job\" type=\"o\"/>"                             \
+        "   <arg name=\"success\" type=\"b\"/>"                         \
         "  </signal>"                                                   \
         "  <property name=\"Version\" type=\"s\" access=\"read\"/>"     \
         "  <property name=\"RunningAs\" type=\"s\" access=\"read\"/>"   \
index 5c474c0c1efc4daaa3b74934267ac56ac7fcca52..ba428a8d5c707515769ab0318e7ff07735cbf30e 100644 (file)
@@ -421,9 +421,12 @@ void bus_unit_send_removed_signal(Unit *u) {
 
         assert(u);
 
-        if (set_isempty(u->meta.manager->subscribed) || !u->meta.sent_dbus_new_signal)
+        if (set_isempty(u->meta.manager->subscribed))
                 return;
 
+        if (!u->meta.sent_dbus_new_signal)
+                bus_unit_send_change_signal(u);
+
         if (!(p = unit_dbus_path(u)))
                 goto oom;
 
index 9937ed735b6bffd107c3043559e67fc77f8da729..78fe136328500a4ab29af7c438966112a394d8da 100644 (file)
--- a/src/job.c
+++ b/src/job.c
@@ -56,7 +56,7 @@ void job_free(Job *j) {
 
         /* Detach from next 'bigger' objects */
         if (j->installed) {
-                bus_job_send_removed_signal(j);
+                bus_job_send_removed_signal(j, !j->failed);
 
                 if (j->unit->meta.job == j) {
                         j->unit->meta.job = NULL;
@@ -476,6 +476,7 @@ int job_finish_and_invalidate(Job *j, bool success) {
                 return 0;
         }
 
+        j->failed = !success;
         u = j->unit;
         t = j->type;
         job_free(j);
index 1ae97b75d02a0d7087e33bc32dd369e21252d6e4..054aa530c6c23419c423265084aea53b105b7255 100644 (file)
--- a/src/job.h
+++ b/src/job.h
@@ -108,6 +108,7 @@ struct Job {
         bool override:1;
         bool in_dbus_queue:1;
         bool sent_dbus_new_signal:1;
+        bool failed:1;
 };
 
 Job* job_new(Manager *m, JobType type, Unit *unit);
index b28759a07604cec90a2c0adc1d9cc837326a364c..9e4a2a23409c88ed7dd1130088519191ae20d48a 100644 (file)
@@ -728,7 +728,7 @@ public class MainWindow : Window {
                 } while (unit_model.iter_next(ref iter));
         }
 
-        public void on_job_removed(uint32 id, ObjectPath path) {
+        public void on_job_removed(uint32 id, ObjectPath path, bool success) {
                 TreeIter iter;
                 if (!(job_model.get_iter_first(out iter)))
                         return;
index 821be5a4f86e665114e8a65915d055e4a4759ec0..e1e8a0c832e1d3c6be404193f3108abedc9f3460 100644 (file)
@@ -24,7 +24,11 @@ static string type = null;
 static bool all = false;
 static bool replace = false;
 static bool session = false;
-static Connection bus = null;
+static bool block = false;
+static Connection? bus = null;
+static List<ObjectPath> jobs = null;
+static MainLoop main_loop = null;
+static int exit_code = 0;
 
 public static int job_info_compare(void* key1, void* key2) {
         Manager.JobInfo *j1 = (Manager.JobInfo*) key1;
@@ -44,11 +48,11 @@ public static int unit_info_compare(void* key1, void* key2) {
         return Posix.strcmp(u1->id, u2->id);
 }
 
-public void on_unit_changed(Unit u) {
+public void monitor_on_unit_changed(Unit u) {
         stdout.printf("Unit %s changed.\n", u.id);
 }
 
-public void on_unit_new(string id, ObjectPath path) {
+public void monitor_on_unit_new(string id, ObjectPath path) {
         stdout.printf("Unit %s added.\n", id);
 
         Unit u = bus.get_object(
@@ -56,17 +60,17 @@ public void on_unit_new(string id, ObjectPath path) {
                         path,
                         "org.freedesktop.systemd1.Unit") as Unit;
 
-        u.changed += on_unit_changed;
+        u.changed += monitor_on_unit_changed;
 
         /* FIXME: We leak memory here */
         u.ref();
 }
 
-public void on_job_changed(Job j) {
+public void monitor_on_job_changed(Job j) {
         stdout.printf("Job %u changed.\n", j.id);
 }
 
-public void on_job_new(uint32 id, ObjectPath path) {
+public void monitor_on_job_new(uint32 id, ObjectPath path) {
         stdout.printf("Job %u added.\n", id);
 
         Job j = bus.get_object(
@@ -74,18 +78,34 @@ public void on_job_new(uint32 id, ObjectPath path) {
                         path,
                         "org.freedesktop.systemd1.Job") as Job;
 
-        j.changed += on_job_changed;
+        j.changed += monitor_on_job_changed;
 
         /* FIXME: We leak memory here */
         j.ref();
 }
 
-public void on_unit_removed(string id, ObjectPath path) {
+public void monitor_on_unit_removed(string id, ObjectPath path) {
         stdout.printf("Unit %s removed.\n", id);
 }
 
-public void on_job_removed(uint32 id, ObjectPath path) {
-        stdout.printf("Job %u removed.\n", id);
+public void monitor_on_job_removed(uint32 id, ObjectPath path, bool success) {
+        stdout.printf("Job %u removed (success=%i).\n", id, (int) success);
+}
+
+public void block_on_job_removed(uint32 id, ObjectPath path, bool success) {
+
+        for (unowned List<ObjectPath> i = jobs; i != null; i = i.next)
+                if (i.data == path) {
+                        jobs.remove_link(i);
+                        break;
+                }
+
+        if (jobs == null) {
+                if (!success)
+                        exit_code = 1;
+
+                main_loop.quit();
+        }
 }
 
 static const OptionEntry entries[] = {
@@ -94,11 +114,11 @@ static const OptionEntry entries[] = {
         { "replace", 0,   0,                   OptionArg.NONE,   out replace, "When installing a new job, replace existing conflicting ones", null },
         { "session", 0,   0,                   OptionArg.NONE,   out session, "Connect to session bus", null },
         { "system",  0,   OptionFlags.REVERSE, OptionArg.NONE,   out session, "Connect to system bus", null },
+        { "block",   0,   0,                   OptionArg.NONE,   out block,   "Wait until the operation finished", null },
         { null }
 };
 
 int main (string[] args) {
-
         OptionContext context = new OptionContext("[COMMAND [ARGUMENT...]]");
         context.add_main_entries(entries, null);
         context.set_description(
@@ -226,6 +246,9 @@ int main (string[] args) {
                                 return 1;
                         }
 
+                        if (block)
+                                manager.subscribe();
+
                         for (int i = 2; i < args.length; i++) {
 
                                 ObjectPath p = manager.load_unit(args[i]);
@@ -237,14 +260,19 @@ int main (string[] args) {
 
                                 string mode = replace ? "replace" : "fail";
 
+                                ObjectPath j = null;
+
                                 if (args[1] == "start")
-                                        u.start(mode);
+                                        j = u.start(mode);
                                 else if (args[1] == "stop")
-                                        u.stop(mode);
+                                        j = u.stop(mode);
                                 else if (args[1] == "restart")
-                                        u.restart(mode);
+                                        j = u.restart(mode);
                                 else if (args[1] == "reload")
-                                        u.reload(mode);
+                                        j = u.reload(mode);
+
+                                if (block)
+                                        jobs.append(j);
                         }
 
                 } else if (args[1] == "isolate") {
@@ -261,19 +289,24 @@ int main (string[] args) {
                                         p,
                                         "org.freedesktop.systemd1.Unit") as Unit;
 
-                        u.start("isolate");
+                        ObjectPath j = u.start("isolate");
+
+                        if (block) {
+                                manager.subscribe();
+                                jobs.append(j);
+                        }
 
                 } else if (args[1] == "monitor") {
 
                         manager.subscribe();
 
-                        manager.unit_new += on_unit_new;
-                        manager.unit_removed += on_unit_removed;
-                        manager.job_new += on_job_new;
-                        manager.job_removed += on_job_removed;
+                        manager.unit_new += monitor_on_unit_new;
+                        manager.unit_removed += monitor_on_unit_removed;
+                        manager.job_new += monitor_on_job_new;
+                        manager.job_removed += monitor_on_job_removed;
 
-                        MainLoop l = new MainLoop();
-                        l.run();
+                        main_loop = new MainLoop();
+                        main_loop.run();
 
                 } else if (args[1] == "dump")
                         stdout.puts(manager.dump());
@@ -313,9 +346,17 @@ int main (string[] args) {
                         return 1;
                 }
 
+                if (jobs != null && block) {
+                        manager.job_removed += block_on_job_removed;
+
+                        main_loop = new MainLoop();
+                        main_loop.run();
+                }
+
         } catch (DBus.Error e) {
                 stderr.printf("%s\n".printf(e.message));
+                return 1;
         }
 
-        return 0;
+        return exit_code;
 }
index 7282bf3dd81e2d42d0daaed76de7f3488a0e961c..a8aeb75579b4bcaf13e99bca6ae44ebabc27c8d6 100644 (file)
@@ -71,7 +71,7 @@ public interface Manager : DBus.Object {
         public abstract signal void unit_new(string id, ObjectPath path);
         public abstract signal void unit_removed(string id, ObjectPath path);
         public abstract signal void job_new(uint32 id, ObjectPath path);
-        public abstract signal void job_removed(uint32 id, ObjectPath path);
+        public abstract signal void job_removed(uint32 id, ObjectPath path, bool success);
 }
 
 [DBus (name = "org.freedesktop.systemd1.Unit")]