4 static string type = null;
5 static bool all = false;
6 static bool replace = false;
8 public static int job_info_compare(void* key1, void* key2) {
9 Manager.JobInfo *j1 = (Manager.JobInfo*) key1;
10 Manager.JobInfo *j2 = (Manager.JobInfo*) key2;
12 return j1->id < j2->id ? -1 : (j1->id > j2->id ? 1 : 0);
15 public static int unit_info_compare(void* key1, void* key2) {
16 Manager.UnitInfo *u1 = (Manager.UnitInfo*) key1;
17 Manager.UnitInfo *u2 = (Manager.UnitInfo*) key2;
19 int r = Posix.strcmp(Posix.strrchr(u1->id, '.'), Posix.strrchr(u2->id, '.'));
23 return Posix.strcmp(u1->id, u2->id);
26 static const OptionEntry entries[] = {
27 { "type", 't', 0, OptionArg.STRING, out type, "List only particular type of units", "TYPE" },
28 { "all", 'a', 0, OptionArg.NONE, out all, "Show all units, including dead ones", null },
29 { "replace", 0, 0, OptionArg.NONE, out replace, "When installing a new job, replace existing conflicting ones.", null },
33 int main (string[] args) {
35 OptionContext context = new OptionContext(" -- Control systemd");
36 context.add_main_entries(entries, null);
39 context.parse(ref args);
40 } catch (GLib.OptionError e) {
41 message("Failed to parse command line: %s".printf(e.message));
45 Connection bus = Bus.get(BusType.SESSION);
47 Manager manager = bus.get_object (
48 "org.freedesktop.systemd1",
49 "/org/freedesktop/systemd1",
50 "org.freedesktop.systemd1") as Manager;
52 if (args[1] == "list-units" || args.length <= 1) {
53 var list = manager.list_units();
55 Posix.qsort(list, list.length, sizeof(Manager.UnitInfo), unit_info_compare);
57 stdout.printf("%-45s %-6s %-12s %-17s\n", "UNIT", "LOAD", "ACTIVE", "JOB");
59 foreach (var i in list) {
61 if (type != null && !i.id.has_suffix(".%s".printf(type)))
64 if (!all && i.active_state == "inactive")
67 stdout.printf("%-45s %-6s %-12s", i.id, i.load_state, i.active_state);
70 stdout.printf("→ %-15s", i.job_type);
77 stdout.printf("\n%u units listed.\n", n);
79 stdout.printf("\n%u live units listed. Pass --all to see dead units, too.\n", n);
82 } else if (args[1] == "list-jobs") {
83 var list = manager.list_jobs();
84 Posix.qsort(list, list.length, sizeof(Manager.JobInfo), job_info_compare);
86 stdout.printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
88 foreach (var i in list)
89 stdout.printf("%4u %-45s → %-15s %-7s\n", i.id, i.name, i.type, i.state);
91 stdout.printf("\n%u jobs listed.\n", list.length);
93 } else if (args[1] == "clear-jobs") {
97 } else if (args[1] == "load") {
99 if (args.length < 3) {
100 stderr.printf("Missing argument.\n");
104 for (uint i = 2; i < args.length; i++)
105 manager.load_unit(args[i]);
107 } else if (args[1] == "cancel") {
109 if (args.length < 3) {
110 stderr.printf("Missing argument.\n");
114 for (uint i = 2; i < args.length; i++) {
117 if (args[i].scanf("%u", out id) != 1) {
118 stderr.printf("Failed to parse argument.\n");
122 ObjectPath p = manager.get_job(id);
124 Job j = bus.get_object (
125 "org.freedesktop.systemd1",
127 "org.freedesktop.systemd1.Job") as Job;
132 } else if (args[1] == "start" ||
134 args[1] == "reload" ||
135 args[1] == "restart") {
137 if (args.length < 3) {
138 stderr.printf("Missing argument.\n");
142 for (uint i = 2; i < args.length; i++) {
144 ObjectPath p = manager.get_unit(args[i]);
146 Unit u = bus.get_object(
147 "org.freedesktop.systemd1",
149 "org.freedesktop.systemd1.Unit") as Unit;
151 string mode = replace ? "replace" : "fail";
153 if (args[1] == "start")
155 else if (args[1] == "stop")
157 else if (args[1] == "restart")
159 else if (args[1] == "reload")
164 stderr.printf("Unknown command %s.\n", args[1]);
168 } catch (DBus.Error e) {
169 stderr.printf("%s\n".printf(e.message));