4 [DBus (name = "org.freedesktop.systemd1")]
5 public interface Manager : DBus.Object {
7 public struct UnitInfo {
18 public struct JobInfo {
27 public abstract UnitInfo[] ListUnits() throws DBus.Error;
28 public abstract JobInfo[] ListJobs() throws DBus.Error;
30 public abstract ObjectPath LoadUnit(string name) throws DBus.Error;
33 static string type = null;
34 static bool all = false;
36 public static int job_info_compare(void* key1, void* key2) {
37 Manager.JobInfo *j1 = (Manager.JobInfo*) key1;
38 Manager.JobInfo *j2 = (Manager.JobInfo*) key2;
40 return Posix.strcmp(j1->name, j2->name);
43 public static int unit_info_compare(void* key1, void* key2) {
44 Manager.UnitInfo *u1 = (Manager.UnitInfo*) key1;
45 Manager.UnitInfo *u2 = (Manager.UnitInfo*) key2;
47 int r = Posix.strcmp(Posix.strrchr(u1->id, '.'), Posix.strrchr(u2->id, '.'));
51 return Posix.strcmp(u1->id, u2->id);
54 static const OptionEntry entries[] = {
55 { "type", 't', 0, OptionArg.STRING, out type, "List only particular type of units", "TYPE" },
56 { "all", 'a', 0, OptionArg.NONE, out all, "Show all units, including dead ones", null },
60 int main (string[] args) {
62 OptionContext context = new OptionContext(" -- Control systemd");
63 context.add_main_entries(entries, null);
66 context.parse(ref args);
67 } catch (GLib.OptionError e) {
68 message("Failed to parse command line: %s".printf(e.message));
72 Connection bus = Bus.get(BusType.SESSION);
74 Manager manager = bus.get_object (
75 "org.freedesktop.systemd1",
76 "/org/freedesktop/systemd1",
77 "org.freedesktop.systemd1") as Manager;
79 if (args[1] == "list-units" || args.length <= 1) {
80 var list = manager.ListUnits();
82 Posix.qsort(list, list.length, sizeof(Manager.UnitInfo), unit_info_compare);
84 stdout.printf("%-45s %-6s %-12s → %-15s\n\n", "UNIT", "LOAD", "ACTIVE", "JOB");
86 foreach (var i in list) {
88 if (type != null && !i.id.has_suffix(".%s".printf(type)))
91 if (!all && i.active_state == "inactive")
94 stdout.printf("%-45s %-6s %-12s", i.id, i.load_state, i.active_state);
97 stdout.printf("→ %-15s", i.job_type);
104 stdout.printf("\n%u units listed.\n", n);
106 stdout.printf("\n%u live units listed. Pass --all to see dead units, too.\n", n);
109 } else if (args[1] == "list-jobs") {
110 var list = manager.ListJobs();
111 Posix.qsort(list, list.length, sizeof(Manager.JobInfo), job_info_compare);
113 foreach (var i in list)
114 stdout.printf("%-45s → %-15s %-7s\n", i.name, i.type, i.state);
116 } else if (args[1] == "load") {
118 if (args.length < 3) {
119 stderr.printf("Missing argument.\n");
123 manager.LoadUnit(args[2]);
125 stderr.printf("Unknown command %s.\n", args[1]);
129 } catch (DBus.Error e) {
130 stderr.printf("%s\n".printf(e.message));