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;
32 public abstract void ClearJobs() throws DBus.Error;
35 static string type = null;
36 static bool all = false;
38 public static int job_info_compare(void* key1, void* key2) {
39 Manager.JobInfo *j1 = (Manager.JobInfo*) key1;
40 Manager.JobInfo *j2 = (Manager.JobInfo*) key2;
42 return Posix.strcmp(j1->name, j2->name);
45 public static int unit_info_compare(void* key1, void* key2) {
46 Manager.UnitInfo *u1 = (Manager.UnitInfo*) key1;
47 Manager.UnitInfo *u2 = (Manager.UnitInfo*) key2;
49 int r = Posix.strcmp(Posix.strrchr(u1->id, '.'), Posix.strrchr(u2->id, '.'));
53 return Posix.strcmp(u1->id, u2->id);
56 static const OptionEntry entries[] = {
57 { "type", 't', 0, OptionArg.STRING, out type, "List only particular type of units", "TYPE" },
58 { "all", 'a', 0, OptionArg.NONE, out all, "Show all units, including dead ones", null },
62 int main (string[] args) {
64 OptionContext context = new OptionContext(" -- Control systemd");
65 context.add_main_entries(entries, null);
68 context.parse(ref args);
69 } catch (GLib.OptionError e) {
70 message("Failed to parse command line: %s".printf(e.message));
74 Connection bus = Bus.get(BusType.SESSION);
76 Manager manager = bus.get_object (
77 "org.freedesktop.systemd1",
78 "/org/freedesktop/systemd1",
79 "org.freedesktop.systemd1") as Manager;
81 if (args[1] == "list-units" || args.length <= 1) {
82 var list = manager.ListUnits();
84 Posix.qsort(list, list.length, sizeof(Manager.UnitInfo), unit_info_compare);
86 stdout.printf("%-45s %-6s %-12s %-17s\n", "UNIT", "LOAD", "ACTIVE", "JOB");
88 foreach (var i in list) {
90 if (type != null && !i.id.has_suffix(".%s".printf(type)))
93 if (!all && i.active_state == "inactive")
96 stdout.printf("%-45s %-6s %-12s", i.id, i.load_state, i.active_state);
99 stdout.printf("→ %-15s", i.job_type);
106 stdout.printf("\n%u units listed.\n", n);
108 stdout.printf("\n%u live units listed. Pass --all to see dead units, too.\n", n);
111 } else if (args[1] == "list-jobs") {
112 var list = manager.ListJobs();
113 Posix.qsort(list, list.length, sizeof(Manager.JobInfo), job_info_compare);
115 stdout.printf("%-45s %-17s %-7s\n", "UNIT", "TYPE", "STATE");
117 foreach (var i in list)
118 stdout.printf("%-45s → %-15s %-7s\n", i.name, i.type, i.state);
120 stdout.printf("\n%u jobs listed.\n", list.length);
122 } else if (args[1] == "clear-jobs") {
126 } else if (args[1] == "load") {
128 if (args.length < 3) {
129 stderr.printf("Missing argument.\n");
133 manager.LoadUnit(args[2]);
135 stderr.printf("Unknown command %s.\n", args[1]);
139 } catch (DBus.Error e) {
140 stderr.printf("%s\n".printf(e.message));