]>
Commit | Line | Data |
---|---|---|
5630af71 LP |
1 | using DBus; |
2 | using GLib; | |
3 | ||
5630af71 LP |
4 | static string type = null; |
5 | static bool all = false; | |
501c7d0b | 6 | static bool replace = false; |
5630af71 LP |
7 | |
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; | |
11 | ||
501c7d0b | 12 | return j1->id < j2->id ? -1 : (j1->id > j2->id ? 1 : 0); |
5630af71 LP |
13 | } |
14 | ||
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; | |
18 | ||
19 | int r = Posix.strcmp(Posix.strrchr(u1->id, '.'), Posix.strrchr(u2->id, '.')); | |
20 | if (r != 0) | |
21 | return r; | |
22 | ||
23 | return Posix.strcmp(u1->id, u2->id); | |
24 | } | |
25 | ||
26 | static const OptionEntry entries[] = { | |
501c7d0b LP |
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 }, | |
5630af71 LP |
30 | { null } |
31 | }; | |
32 | ||
33 | int main (string[] args) { | |
34 | ||
35 | OptionContext context = new OptionContext(" -- Control systemd"); | |
36 | context.add_main_entries(entries, null); | |
37 | ||
38 | try { | |
39 | context.parse(ref args); | |
40 | } catch (GLib.OptionError e) { | |
41 | message("Failed to parse command line: %s".printf(e.message)); | |
42 | } | |
43 | ||
44 | try { | |
45 | Connection bus = Bus.get(BusType.SESSION); | |
46 | ||
47 | Manager manager = bus.get_object ( | |
48 | "org.freedesktop.systemd1", | |
49 | "/org/freedesktop/systemd1", | |
50 | "org.freedesktop.systemd1") as Manager; | |
51 | ||
52 | if (args[1] == "list-units" || args.length <= 1) { | |
501c7d0b | 53 | var list = manager.list_units(); |
5630af71 LP |
54 | uint n = 0; |
55 | Posix.qsort(list, list.length, sizeof(Manager.UnitInfo), unit_info_compare); | |
56 | ||
dcc47b17 | 57 | stdout.printf("%-45s %-6s %-12s %-17s\n", "UNIT", "LOAD", "ACTIVE", "JOB"); |
5630af71 LP |
58 | |
59 | foreach (var i in list) { | |
60 | ||
61 | if (type != null && !i.id.has_suffix(".%s".printf(type))) | |
62 | continue; | |
63 | ||
64 | if (!all && i.active_state == "inactive") | |
65 | continue; | |
66 | ||
67 | stdout.printf("%-45s %-6s %-12s", i.id, i.load_state, i.active_state); | |
68 | ||
69 | if (i.job_id != 0) | |
70 | stdout.printf("→ %-15s", i.job_type); | |
71 | ||
72 | stdout.puts("\n"); | |
73 | n++; | |
74 | } | |
75 | ||
76 | if (all) | |
77 | stdout.printf("\n%u units listed.\n", n); | |
78 | else | |
79 | stdout.printf("\n%u live units listed. Pass --all to see dead units, too.\n", n); | |
80 | ||
81 | ||
82 | } else if (args[1] == "list-jobs") { | |
501c7d0b | 83 | var list = manager.list_jobs(); |
5630af71 LP |
84 | Posix.qsort(list, list.length, sizeof(Manager.JobInfo), job_info_compare); |
85 | ||
501c7d0b | 86 | stdout.printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE"); |
dcc47b17 | 87 | |
5630af71 | 88 | foreach (var i in list) |
501c7d0b | 89 | stdout.printf("%4u %-45s → %-15s %-7s\n", i.id, i.name, i.type, i.state); |
5630af71 | 90 | |
dcc47b17 LP |
91 | stdout.printf("\n%u jobs listed.\n", list.length); |
92 | ||
93 | } else if (args[1] == "clear-jobs") { | |
94 | ||
501c7d0b | 95 | manager.clear_jobs(); |
dcc47b17 | 96 | |
5630af71 LP |
97 | } else if (args[1] == "load") { |
98 | ||
99 | if (args.length < 3) { | |
100 | stderr.printf("Missing argument.\n"); | |
101 | return 1; | |
102 | } | |
103 | ||
501c7d0b LP |
104 | for (uint i = 2; i < args.length; i++) |
105 | manager.load_unit(args[i]); | |
106 | ||
107 | } else if (args[1] == "cancel") { | |
108 | ||
109 | if (args.length < 3) { | |
110 | stderr.printf("Missing argument.\n"); | |
111 | return 1; | |
112 | } | |
113 | ||
114 | for (uint i = 2; i < args.length; i++) { | |
115 | uint32 id; | |
116 | ||
117 | if (args[i].scanf("%u", out id) != 1) { | |
118 | stderr.printf("Failed to parse argument.\n"); | |
119 | return 1; | |
120 | } | |
121 | ||
122 | ObjectPath p = manager.get_job(id); | |
123 | ||
124 | Job j = bus.get_object ( | |
125 | "org.freedesktop.systemd1", | |
126 | p, | |
127 | "org.freedesktop.systemd1.Job") as Job; | |
128 | ||
129 | j.cancel(); | |
130 | } | |
131 | ||
132 | } else if (args[1] == "start" || | |
133 | args[1] == "stop" || | |
134 | args[1] == "reload" || | |
135 | args[1] == "restart") { | |
136 | ||
137 | if (args.length < 3) { | |
138 | stderr.printf("Missing argument.\n"); | |
139 | return 1; | |
140 | } | |
141 | ||
142 | for (uint i = 2; i < args.length; i++) { | |
143 | ||
144 | ObjectPath p = manager.get_unit(args[i]); | |
145 | ||
146 | Unit u = bus.get_object( | |
147 | "org.freedesktop.systemd1", | |
148 | p, | |
149 | "org.freedesktop.systemd1.Unit") as Unit; | |
150 | ||
151 | string mode = replace ? "replace" : "fail"; | |
152 | ||
153 | if (args[1] == "start") | |
154 | u.start(mode); | |
155 | else if (args[1] == "stop") | |
156 | u.stop(mode); | |
157 | else if (args[1] == "restart") | |
158 | u.restart(mode); | |
159 | else if (args[1] == "reload") | |
160 | u.reload(mode); | |
161 | } | |
162 | ||
5630af71 LP |
163 | } else { |
164 | stderr.printf("Unknown command %s.\n", args[1]); | |
165 | return 1; | |
166 | } | |
167 | ||
168 | } catch (DBus.Error e) { | |
169 | stderr.printf("%s\n".printf(e.message)); | |
170 | } | |
171 | ||
172 | return 0; | |
173 | } |