]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/systemadm.vala
virt: detect LXC+libvirt containers
[thirdparty/systemd.git] / src / systemadm.vala
CommitLineData
a7334b09
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
501c7d0b
LP
20using Gtk;
21using GLib;
501c7d0b
LP
22using Pango;
23
af2d49f7 24static bool user = false;
edddf4ff 25
8401f153
ZJS
26public string format_time(uint64 time_ns) {
27 if (time_ns <= 0)
28 return "";
29 Time timestamp = Time.local((time_t) (time_ns / 1000000));
30 return timestamp.format("%a, %d %b %Y %H:%M:%S");
31}
32
661ece10
ZJS
33public void new_column(TreeView view, int column_id, string title) {
34 TreeViewColumn col;
35 col = new TreeViewColumn.with_attributes(title, new CellRendererText(), "text", column_id);
36 col.set_sort_column_id(column_id);
37 view.insert_column(col, -1);
38}
39
501c7d0b
LP
40public class LeftLabel : Label {
41 public LeftLabel(string? text = null) {
42 if (text != null)
43 set_markup("<b>%s</b>".printf(text));
77a38d30 44 set_alignment(0, 0);
501c7d0b
LP
45 set_padding(6, 0);
46 }
47}
48
0dd27daf
ZJS
49public class RightLabel : WrapLabel {
50
501c7d0b 51 public RightLabel(string? text = null) {
501c7d0b 52 set_selectable(true);
0dd27daf 53 set_text_or_na(text);
501c7d0b
LP
54 }
55
56 public void set_text_or_na(string? text = null) {
57 if (text == null || text == "")
58 set_markup("<i>n/a</i>");
59 else
60 set_text(text);
61 }
d4e6a6f6
LP
62
63 public void set_markup_or_na(string? text = null) {
64 if (text == null || text == "")
65 set_markup("<i>n/a</i>");
66 else
67 set_markup(text);
68 }
501c7d0b
LP
69}
70
71public class MainWindow : Window {
72
77a38d30
LP
73 private string? current_unit_id;
74 private uint32 current_job_id;
75
501c7d0b
LP
76 private TreeView unit_view;
77 private TreeView job_view;
78
79 private ListStore unit_model;
80 private ListStore job_model;
81
11216eb0
ZJS
82 private Gee.HashMap<string, Unit> unit_map;
83
501c7d0b
LP
84 private Button start_button;
85 private Button stop_button;
86 private Button restart_button;
87 private Button reload_button;
88 private Button cancel_button;
89
ed42e037
LP
90 private Entry unit_load_entry;
91 private Button unit_load_button;
92
77a38d30
LP
93 private Button server_snapshot_button;
94 private Button server_reload_button;
95
501c7d0b
LP
96 private Manager manager;
97
98 private RightLabel unit_id_label;
d4e6a6f6 99 private RightLabel unit_dependency_label;
501c7d0b
LP
100 private RightLabel unit_description_label;
101 private RightLabel unit_load_state_label;
102 private RightLabel unit_active_state_label;
77a38d30 103 private RightLabel unit_sub_state_label;
6be1e7d5 104 private RightLabel unit_fragment_path_label;
501c7d0b
LP
105 private RightLabel unit_active_enter_timestamp_label;
106 private RightLabel unit_active_exit_timestamp_label;
107 private RightLabel unit_can_start_label;
108 private RightLabel unit_can_reload_label;
77a38d30 109 private RightLabel unit_cgroup_label;
501c7d0b
LP
110
111 private RightLabel job_id_label;
112 private RightLabel job_state_label;
113 private RightLabel job_type_label;
114
77a38d30 115 private ComboBox unit_type_combo_box;
a3c159a2 116 private CheckButton inactive_checkbox;
77a38d30 117
26742b3f 118 public MainWindow() throws IOError {
af2d49f7 119 title = user ? "systemd User Service Manager" : "systemd System Manager";
26742b3f 120 set_position(WindowPosition.CENTER);
501c7d0b
LP
121 set_default_size(1000, 700);
122 set_border_width(12);
51cfdda7 123 destroy.connect(Gtk.main_quit);
501c7d0b
LP
124
125 Notebook notebook = new Notebook();
126 add(notebook);
127
77a38d30 128 Box unit_vbox = new VBox(false, 12);
501c7d0b
LP
129 notebook.append_page(unit_vbox, new Label("Units"));
130 unit_vbox.set_border_width(12);
131
77a38d30 132 Box job_vbox = new VBox(false, 12);
501c7d0b
LP
133 notebook.append_page(job_vbox, new Label("Jobs"));
134 job_vbox.set_border_width(12);
135
77a38d30
LP
136 unit_type_combo_box = new ComboBox.text();
137 Box type_hbox = new HBox(false, 6);
138 type_hbox.pack_start(unit_type_combo_box, false, false, 0);
139 unit_vbox.pack_start(type_hbox, false, false, 0);
140
e377ad0d
ZJS
141 unit_type_combo_box.append_text("All unit types");
142 unit_type_combo_box.append_text("Targets");
77a38d30 143 unit_type_combo_box.append_text("Services");
77a38d30
LP
144 unit_type_combo_box.append_text("Devices");
145 unit_type_combo_box.append_text("Mounts");
146 unit_type_combo_box.append_text("Automounts");
e377ad0d
ZJS
147 unit_type_combo_box.append_text("Swaps");
148 unit_type_combo_box.append_text("Sockets");
149 unit_type_combo_box.append_text("Paths");
150 unit_type_combo_box.append_text("Timers");
77a38d30 151 unit_type_combo_box.append_text("Snapshots");
a3c159a2 152 unit_type_combo_box.set_active(0); // Show All
51cfdda7 153 unit_type_combo_box.changed.connect(unit_type_changed);
77a38d30 154
a3c159a2
ZJS
155 inactive_checkbox = new CheckButton.with_label("inactive too");
156 inactive_checkbox.toggled.connect(unit_type_changed);
157 type_hbox.pack_start(inactive_checkbox, false, false, 0);
158
ed42e037
LP
159 unit_load_entry = new Entry();
160 unit_load_button = new Button.with_mnemonic("_Load");
161 unit_load_button.set_sensitive(false);
162
51cfdda7
LP
163 unit_load_entry.changed.connect(on_unit_load_entry_changed);
164 unit_load_entry.activate.connect(on_unit_load);
165 unit_load_button.clicked.connect(on_unit_load);
ed42e037 166
416f4a84
LP
167 Box unit_load_hbox = new HBox(false, 6);
168 unit_load_hbox.pack_start(unit_load_entry, false, true, 0);
169 unit_load_hbox.pack_start(unit_load_button, false, true, 0);
ed42e037
LP
170
171 server_snapshot_button = new Button.with_mnemonic("Take S_napshot");
77a38d30
LP
172 server_reload_button = new Button.with_mnemonic("Reload _Configuration");
173
51cfdda7
LP
174 server_snapshot_button.clicked.connect(on_server_snapshot);
175 server_reload_button.clicked.connect(on_server_reload);
77a38d30
LP
176
177 type_hbox.pack_end(server_snapshot_button, false, true, 0);
178 type_hbox.pack_end(server_reload_button, false, true, 0);
416f4a84 179 type_hbox.pack_end(unit_load_hbox, false, true, 24);
77a38d30
LP
180
181 unit_model = new ListStore(7, typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(Unit));
182 job_model = new ListStore(6, typeof(string), typeof(string), typeof(string), typeof(string), typeof(Job), typeof(uint32));
183
11216eb0
ZJS
184 unit_map = new Gee.HashMap<string, Unit>();
185
77a38d30
LP
186 TreeModelFilter unit_model_filter;
187 unit_model_filter = new TreeModelFilter(unit_model, null);
188 unit_model_filter.set_visible_func(unit_filter);
501c7d0b 189
661ece10
ZJS
190 TreeModelSort unit_model_sort = new TreeModelSort.with_model(unit_model_filter);
191
192 unit_view = new TreeView.with_model(unit_model_sort);
501c7d0b
LP
193 job_view = new TreeView.with_model(job_model);
194
51cfdda7
LP
195 unit_view.cursor_changed.connect(unit_changed);
196 job_view.cursor_changed.connect(job_changed);
501c7d0b 197
661ece10
ZJS
198 new_column(unit_view, 2, "Load State");
199 new_column(unit_view, 3, "Active State");
200 new_column(unit_view, 4, "Unit State");
201 new_column(unit_view, 0, "Unit");
202 new_column(unit_view, 5, "Job");
501c7d0b 203
661ece10
ZJS
204 new_column(job_view, 0, "Job");
205 new_column(job_view, 1, "Unit");
206 new_column(job_view, 2, "Type");
207 new_column(job_view, 3, "State");
501c7d0b
LP
208
209 ScrolledWindow scroll = new ScrolledWindow(null, null);
210 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
211 scroll.set_shadow_type(ShadowType.IN);
212 scroll.add(unit_view);
213 unit_vbox.pack_start(scroll, true, true, 0);
214
215 scroll = new ScrolledWindow(null, null);
216 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
217 scroll.set_shadow_type(ShadowType.IN);
218 scroll.add(job_view);
219 job_vbox.pack_start(scroll, true, true, 0);
220
221 unit_id_label = new RightLabel();
d4e6a6f6 222 unit_dependency_label = new RightLabel();
501c7d0b
LP
223 unit_description_label = new RightLabel();
224 unit_load_state_label = new RightLabel();
225 unit_active_state_label = new RightLabel();
77a38d30 226 unit_sub_state_label = new RightLabel();
6be1e7d5 227 unit_fragment_path_label = new RightLabel();
501c7d0b
LP
228 unit_active_enter_timestamp_label = new RightLabel();
229 unit_active_exit_timestamp_label = new RightLabel();
230 unit_can_start_label = new RightLabel();
231 unit_can_reload_label = new RightLabel();
77a38d30 232 unit_cgroup_label = new RightLabel();
501c7d0b
LP
233
234 job_id_label = new RightLabel();
235 job_state_label = new RightLabel();
236 job_type_label = new RightLabel();
237
d4e6a6f6 238 unit_dependency_label.set_track_visited_links(false);
37d3b881 239 unit_dependency_label.set_selectable(true);
51cfdda7 240 unit_dependency_label.activate_link.connect(on_activate_link);
d4e6a6f6 241
07ded1e8
LP
242 unit_fragment_path_label.set_track_visited_links(false);
243
77a38d30 244 Table unit_table = new Table(8, 6, false);
501c7d0b 245 unit_table.set_row_spacings(6);
77a38d30 246 unit_table.set_border_width(0);
501c7d0b
LP
247 unit_vbox.pack_start(unit_table, false, true, 0);
248
249 Table job_table = new Table(2, 2, false);
250 job_table.set_row_spacings(6);
77a38d30 251 job_table.set_border_width(0);
501c7d0b
LP
252 job_vbox.pack_start(job_table, false, true, 0);
253
77a38d30 254 unit_table.attach(new LeftLabel("Id:"), 0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
cd1a19e8 255 unit_table.attach(unit_id_label, 1, 6, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
fe7e2814
ZJS
256 unit_table.attach(new LeftLabel("Description:"), 0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
257 unit_table.attach(unit_description_label, 1, 6, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
258 unit_table.attach(new LeftLabel("Dependencies:"), 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
259 unit_table.attach(unit_dependency_label, 1, 6, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
260 unit_table.attach(new LeftLabel("Fragment Path:"), 0, 1, 3, 4, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
261 unit_table.attach(unit_fragment_path_label, 1, 6, 3, 4, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
262 unit_table.attach(new LeftLabel("Control Group:"), 0, 1, 4, 5, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
263 unit_table.attach(unit_cgroup_label, 1, 6, 4, 5, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
264
265 unit_table.attach(new LeftLabel("Load State:"), 0, 1, 5, 6, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
266 unit_table.attach(unit_load_state_label, 1, 2, 5, 6, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
267 unit_table.attach(new LeftLabel("Active State:"), 0, 1, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
268 unit_table.attach(unit_active_state_label, 1, 2, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
269 unit_table.attach(new LeftLabel("Unit State:"), 0, 1, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
270 unit_table.attach(unit_sub_state_label, 1, 2, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
271
272 unit_table.attach(new LeftLabel("Activated:"), 2, 3, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
273 unit_table.attach(unit_active_enter_timestamp_label, 3, 4, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
274 unit_table.attach(new LeftLabel("Deactivated:"), 2, 3, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
275 unit_table.attach(unit_active_exit_timestamp_label, 3, 4, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
276
277 unit_table.attach(new LeftLabel("Can Start/Stop:"), 4, 5, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
278 unit_table.attach(unit_can_start_label, 5, 6, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
279 unit_table.attach(new LeftLabel("Can Reload:"), 4, 5, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
280 unit_table.attach(unit_can_reload_label, 5, 6, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
77a38d30
LP
281
282 job_table.attach(new LeftLabel("Id:"), 0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
283 job_table.attach(job_id_label, 1, 2, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
284 job_table.attach(new LeftLabel("State:"), 0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
285 job_table.attach(job_state_label, 1, 2, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
286 job_table.attach(new LeftLabel("Type:"), 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
287 job_table.attach(job_type_label, 1, 2, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
501c7d0b
LP
288
289 ButtonBox bbox = new HButtonBox();
290 bbox.set_layout(ButtonBoxStyle.START);
291 bbox.set_spacing(6);
292 unit_vbox.pack_start(bbox, false, true, 0);
293
294 start_button = new Button.with_mnemonic("_Start");
295 stop_button = new Button.with_mnemonic("Sto_p");
296 reload_button = new Button.with_mnemonic("_Reload");
297 restart_button = new Button.with_mnemonic("Res_tart");
298
51cfdda7
LP
299 start_button.clicked.connect(on_start);
300 stop_button.clicked.connect(on_stop);
301 reload_button.clicked.connect(on_reload);
302 restart_button.clicked.connect(on_restart);
501c7d0b
LP
303
304 bbox.pack_start(start_button, false, true, 0);
305 bbox.pack_start(stop_button, false, true, 0);
306 bbox.pack_start(restart_button, false, true, 0);
307 bbox.pack_start(reload_button, false, true, 0);
308
309 bbox = new HButtonBox();
310 bbox.set_layout(ButtonBoxStyle.START);
311 bbox.set_spacing(6);
312 job_vbox.pack_start(bbox, false, true, 0);
313
314 cancel_button = new Button.with_mnemonic("_Cancel");
315
51cfdda7 316 cancel_button.clicked.connect(on_cancel);
501c7d0b
LP
317
318 bbox.pack_start(cancel_button, false, true, 0);
319
26742b3f
LP
320 manager = Bus.get_proxy_sync(
321 user ? BusType.SESSION : BusType.SYSTEM,
501c7d0b 322 "org.freedesktop.systemd1",
26742b3f 323 "/org/freedesktop/systemd1");
501c7d0b 324
51cfdda7
LP
325 manager.unit_new.connect(on_unit_new);
326 manager.job_new.connect(on_job_new);
327 manager.unit_removed.connect(on_unit_removed);
328 manager.job_removed.connect(on_job_removed);
c1e1601e
LP
329
330 manager.subscribe();
331
501c7d0b 332 clear_unit();
77a38d30 333 clear_job();
501c7d0b
LP
334 populate_unit_model();
335 populate_job_model();
336 }
337
26742b3f 338 public void populate_unit_model() throws IOError {
501c7d0b
LP
339 unit_model.clear();
340
341 var list = manager.list_units();
342
343 foreach (var i in list) {
344 TreeIter iter;
345
26742b3f
LP
346 Properties p = Bus.get_proxy_sync(
347 user ? BusType.SESSION : BusType.SYSTEM,
c4e2ceae 348 "org.freedesktop.systemd1",
26742b3f 349 i.unit_path);
c4e2ceae
LP
350
351 p.properties_changed.connect(on_unit_changed);
352
26742b3f
LP
353 Unit u = Bus.get_proxy_sync(
354 user ? BusType.SESSION : BusType.SYSTEM,
c1e1601e 355 "org.freedesktop.systemd1",
26742b3f 356 i.unit_path);
c1e1601e 357
11216eb0
ZJS
358 unit_map[i.id] = u;
359
501c7d0b
LP
360 unit_model.append(out iter);
361 unit_model.set(iter,
362 0, i.id,
363 1, i.description,
364 2, i.load_state,
365 3, i.active_state,
77a38d30
LP
366 4, i.sub_state,
367 5, i.job_type != "" ? "→ %s".printf(i.job_type) : "",
368 6, u);
501c7d0b
LP
369 }
370 }
371
26742b3f 372 public void populate_job_model() throws IOError {
501c7d0b
LP
373 job_model.clear();
374
375 var list = manager.list_jobs();
376
377 foreach (var i in list) {
378 TreeIter iter;
379
26742b3f
LP
380 Properties p = Bus.get_proxy_sync(
381 user ? BusType.SESSION : BusType.SYSTEM,
c4e2ceae 382 "org.freedesktop.systemd1",
26742b3f 383 i.job_path);
c4e2ceae
LP
384
385 p.properties_changed.connect(on_job_changed);
386
26742b3f
LP
387 Job j = Bus.get_proxy_sync(
388 user ? BusType.SESSION : BusType.SYSTEM,
c1e1601e 389 "org.freedesktop.systemd1",
26742b3f 390 i.job_path);
c1e1601e 391
501c7d0b
LP
392 job_model.append(out iter);
393 job_model.set(iter,
394 0, "%u".printf(i.id),
395 1, i.name,
396 2, "→ %s".printf(i.type),
397 3, i.state,
77a38d30
LP
398 4, j,
399 5, i.id);
501c7d0b
LP
400 }
401 }
402
403 public Unit? get_current_unit() {
404 TreePath p;
405 unit_view.get_cursor(out p, null);
406
407 if (p == null)
408 return null;
409
77a38d30 410 TreeModel model = unit_view.get_model();
501c7d0b 411 TreeIter iter;
c1e1601e 412 Unit u;
501c7d0b 413
77a38d30
LP
414 model.get_iter(out iter, p);
415 model.get(iter, 6, out u);
501c7d0b 416
c1e1601e 417 return u;
501c7d0b
LP
418 }
419
11216eb0
ZJS
420 public Unit? get_unit(string id) {
421 return this.unit_map[id];
422 }
423
501c7d0b
LP
424 public void unit_changed() {
425 Unit u = get_current_unit();
426
427 if (u == null)
428 clear_unit();
429 else
430 show_unit(u);
431 }
432
433 public void clear_unit() {
77a38d30
LP
434 current_unit_id = null;
435
501c7d0b
LP
436 start_button.set_sensitive(false);
437 stop_button.set_sensitive(false);
438 reload_button.set_sensitive(false);
439 restart_button.set_sensitive(false);
440
441 unit_id_label.set_text_or_na();
442 unit_description_label.set_text_or_na();
d4e6a6f6 443 unit_description_label.set_text_or_na();
501c7d0b
LP
444 unit_load_state_label.set_text_or_na();
445 unit_active_state_label.set_text_or_na();
77a38d30 446 unit_sub_state_label.set_text_or_na();
6be1e7d5 447 unit_fragment_path_label.set_text_or_na();
501c7d0b
LP
448 unit_active_enter_timestamp_label.set_text_or_na();
449 unit_active_exit_timestamp_label.set_text_or_na();
450 unit_can_reload_label.set_text_or_na();
451 unit_can_start_label.set_text_or_na();
77a38d30 452 unit_cgroup_label.set_text_or_na();
501c7d0b
LP
453 }
454
79b1e6cb 455 public string format_unit_link(string i, bool link) {
23b51f17
ZJS
456 Unit? u = get_unit(i);
457 if(u == null)
458 return "<span color='grey'>" + i + "</span";
459
460 string color;
461 switch (u.sub_state) {
462 case "active": color = "blue"; break;
463 case "dead": color = "red"; break;
464 case "running": color = "green"; break;
465 default: color = "black"; break;
466 }
467 string span = "<span underline='none' color='" + color + "'>"
468 + i + "(" +
469 u.sub_state + ")" + "</span>";
79b1e6cb
ZJS
470 if(link)
471 return " <a href='" + i + "'>" + span + "</a>";
472 else
473 return span;
23b51f17
ZJS
474 }
475
476
d4e6a6f6 477 public string make_dependency_string(string? prefix, string word, string[] dependencies) {
8278f069
ZJS
478 Gee.Collection<unowned string> sorted = new Gee.TreeSet<string>();
479 foreach (string i in dependencies)
480 sorted.add(i);
481
d4e6a6f6
LP
482 bool first = true;
483 string r;
484
485 if (prefix == null)
486 r = "";
487 else
488 r = prefix;
489
8278f069 490 foreach (string i in sorted) {
d4e6a6f6
LP
491 if (r != "")
492 r += first ? "\n" : ",";
493
494 if (first) {
8b1451ad 495 r += "<b>" + word + ":</b>";
d4e6a6f6
LP
496 first = false;
497 }
498
79b1e6cb 499 r += format_unit_link(i, true);
d4e6a6f6
LP
500 }
501
502 return r;
503 }
504
501c7d0b 505 public void show_unit(Unit unit) {
77a38d30
LP
506 current_unit_id = unit.id;
507
79b1e6cb 508 string id_display = format_unit_link(current_unit_id, false);
8f38d5a4 509 bool has_alias = false;
2e2ca209
LP
510 foreach (string i in unit.names) {
511 if (i == current_unit_id)
512 continue;
513
8f38d5a4
ZJS
514 if (!has_alias) {
515 id_display += " (aliases:";
516 has_alias = true;
517 }
518
519 id_display += " " + i;
2e2ca209 520 }
8f38d5a4
ZJS
521 if(has_alias)
522 id_display += ")";
2e2ca209 523
79b1e6cb 524 unit_id_label.set_markup_or_na(id_display);
d4e6a6f6
LP
525
526 string[]
527 requires = unit.requires,
528 requires_overridable = unit.requires_overridable,
529 requisite = unit.requisite,
530 requisite_overridable = unit.requisite_overridable,
531 wants = unit.wants,
532 required_by = unit.required_by,
533 required_by_overridable = unit.required_by_overridable,
534 wanted_by = unit.wanted_by,
535 conflicts = unit.conflicts,
536 before = unit.before,
537 after = unit.after;
538
539 unit_dependency_label.set_markup_or_na(
540 make_dependency_string(
541 make_dependency_string(
542 make_dependency_string(
543 make_dependency_string(
544 make_dependency_string(
545 make_dependency_string(
546 make_dependency_string(
547 make_dependency_string(
548 make_dependency_string(
549 make_dependency_string(
550 make_dependency_string(null,
551 "requires", requires),
552 "overridable requires", requires_overridable),
553 "requisite", requisite),
554 "overridable requisite", requisite_overridable),
555 "wants", wants),
556 "conflicts", conflicts),
557 "required by", required_by),
558 "overridable required by", required_by_overridable),
559 "wanted by", wanted_by),
560 "after", after),
561 "before", before));
562
501c7d0b
LP
563 unit_description_label.set_text_or_na(unit.description);
564 unit_load_state_label.set_text_or_na(unit.load_state);
565 unit_active_state_label.set_text_or_na(unit.active_state);
77a38d30 566 unit_sub_state_label.set_text_or_na(unit.sub_state);
eaf67512
LP
567
568 string fp = unit.fragment_path;
569 if (fp != "")
79b1e6cb
ZJS
570 unit_fragment_path_label.set_markup_or_na(
571 "<a href=\"file://" + fp +"\">" +
572 "<span underline='none' color='black'>" + fp + "</span></a>");
07ded1e8 573 else
eaf67512 574 unit_fragment_path_label.set_text_or_na();
501c7d0b 575
8401f153
ZJS
576
577 unit_active_enter_timestamp_label.set_text_or_na(format_time(unit.active_enter_timestamp));
578
579 unit_active_exit_timestamp_label.set_text_or_na(format_time(unit.active_exit_timestamp));
501c7d0b
LP
580
581 bool b = unit.can_start;
582 start_button.set_sensitive(b);
583 stop_button.set_sensitive(b);
584 restart_button.set_sensitive(b);
585 unit_can_start_label.set_text_or_na(b ? "Yes" : "No");
586
587 b = unit.can_reload;
588 reload_button.set_sensitive(b);
589 unit_can_reload_label.set_text_or_na(b ? "Yes" : "No");
77a38d30
LP
590
591 unit_cgroup_label.set_text_or_na(unit.default_control_group);
501c7d0b
LP
592 }
593
594 public Job? get_current_job() {
595 TreePath p;
596 job_view.get_cursor(out p, null);
597
598 if (p == null)
599 return null;
600
601 TreeIter iter;
77a38d30 602 TreeModel model = job_view.get_model();
c1e1601e 603 Job *j;
501c7d0b 604
77a38d30
LP
605 model.get_iter(out iter, p);
606 model.get(iter, 4, out j);
501c7d0b 607
c1e1601e 608 return j;
501c7d0b
LP
609 }
610
611 public void job_changed() {
612 Job j = get_current_job();
613
614 if (j == null)
615 clear_job();
616 else
617 show_job(j);
618 }
619
620 public void clear_job() {
77a38d30
LP
621 current_job_id = 0;
622
501c7d0b
LP
623 job_id_label.set_text_or_na();
624 job_state_label.set_text_or_na();
625 job_type_label.set_text_or_na();
77a38d30
LP
626
627 cancel_button.set_sensitive(false);
501c7d0b
LP
628 }
629
630 public void show_job(Job job) {
77a38d30
LP
631 current_job_id = job.id;
632
633 job_id_label.set_text_or_na("%u".printf(current_job_id));
501c7d0b
LP
634 job_state_label.set_text_or_na(job.state);
635 job_type_label.set_text_or_na(job.job_type);
77a38d30
LP
636
637 cancel_button.set_sensitive(true);
501c7d0b
LP
638 }
639
640 public void on_start() {
641 Unit u = get_current_unit();
642
643 if (u == null)
644 return;
645
646 try {
647 u.start("replace");
734b60d7 648 } catch (Error e) {
e6be8af4 649 show_error(e.message);
501c7d0b
LP
650 }
651 }
652
653 public void on_stop() {
654 Unit u = get_current_unit();
655
656 if (u == null)
657 return;
658
659 try {
660 u.stop("replace");
734b60d7 661 } catch (Error e) {
e6be8af4 662 show_error(e.message);
501c7d0b
LP
663 }
664 }
665
666 public void on_reload() {
667 Unit u = get_current_unit();
668
669 if (u == null)
670 return;
671
672 try {
673 u.reload("replace");
734b60d7 674 } catch (Error e) {
e6be8af4 675 show_error(e.message);
501c7d0b
LP
676 }
677 }
678
679 public void on_restart() {
680 Unit u = get_current_unit();
681
682 if (u == null)
683 return;
684
685 try {
686 u.restart("replace");
734b60d7 687 } catch (Error e) {
e6be8af4 688 show_error(e.message);
501c7d0b
LP
689 }
690 }
691
692 public void on_cancel() {
693 Job j = get_current_job();
694
695 if (j == null)
696 return;
697
698 try {
699 j.cancel();
734b60d7 700 } catch (Error e) {
e6be8af4 701 show_error(e.message);
501c7d0b
LP
702 }
703 }
c1e1601e 704
dd28669f 705 public void update_unit_iter(TreeIter iter, string id, Unit u) {
c1e1601e 706
26742b3f
LP
707 try {
708 string t = "";
709 Unit.JobLink jl = u.job;
c1e1601e 710
26742b3f
LP
711 if (jl.id != 0) {
712 Job j = Bus.get_proxy_sync(
713 user ? BusType.SESSION : BusType.SYSTEM,
714 "org.freedesktop.systemd1",
715 jl.path);
c1e1601e 716
26742b3f
LP
717 t = j.job_type;
718 }
c1e1601e 719
26742b3f
LP
720 unit_model.set(iter,
721 0, id,
722 1, u.description,
723 2, u.load_state,
724 3, u.active_state,
725 4, u.sub_state,
726 5, t != "" ? "→ %s".printf(t) : "",
727 6, u);
734b60d7 728 } catch (Error e) {
26742b3f
LP
729 show_error(e.message);
730 }
c1e1601e
LP
731 }
732
dd28669f 733 public void on_unit_new(string id, ObjectPath path) {
26742b3f 734 try {
dd28669f 735
26742b3f
LP
736 Properties p = Bus.get_proxy_sync(
737 user ? BusType.SESSION : BusType.SYSTEM,
738 "org.freedesktop.systemd1",
739 path);
c1e1601e 740
26742b3f 741 p.properties_changed.connect(on_unit_changed);
c4e2ceae 742
26742b3f
LP
743 TreeIter iter;
744 unit_model.append(out iter);
745
746 Unit u = Bus.get_proxy_sync(
747 user ? BusType.SESSION : BusType.SYSTEM,
748 "org.freedesktop.systemd1",
749 path);
c4e2ceae 750
11216eb0
ZJS
751 unit_map[id] = u;
752
26742b3f 753 update_unit_iter(iter, id, u);
734b60d7 754 } catch (Error e) {
26742b3f
LP
755 show_error(e.message);
756 }
dd28669f
LP
757 }
758
759 public void update_job_iter(TreeIter iter, uint32 id, Job j) {
c1e1601e 760 job_model.set(iter,
dd28669f 761 0, "%u".printf(id),
c1e1601e
LP
762 1, j.unit.id,
763 2, "→ %s".printf(j.job_type),
764 3, j.state,
77a38d30
LP
765 4, j,
766 5, id);
c1e1601e
LP
767 }
768
dd28669f 769 public void on_job_new(uint32 id, ObjectPath path) {
c4e2ceae 770
26742b3f 771 try {
dd28669f 772
26742b3f
LP
773 Properties p = Bus.get_proxy_sync(
774 user ? BusType.SESSION : BusType.SYSTEM,
775 "org.freedesktop.systemd1",
776 path);
dd28669f 777
26742b3f 778 p.properties_changed.connect(on_job_changed);
c4e2ceae 779
26742b3f
LP
780 TreeIter iter;
781 job_model.append(out iter);
782
783 Job j = Bus.get_proxy_sync(
784 user ? BusType.SESSION : BusType.SYSTEM,
785 "org.freedesktop.systemd1",
786 path);
787
788 update_job_iter(iter, id, j);
c4e2ceae 789
734b60d7 790 } catch (Error e) {
26742b3f
LP
791 show_error(e.message);
792 }
dd28669f
LP
793 }
794
c1e1601e 795 public void on_unit_removed(string id, ObjectPath path) {
77a38d30
LP
796 TreeIter iter;
797 if (!(unit_model.get_iter_first(out iter)))
798 return;
799
800 do {
801 string name;
802
803 unit_model.get(iter, 0, out name);
804
805 if (id == name) {
806 if (current_unit_id == name)
807 clear_unit();
c1e1601e 808
77a38d30
LP
809 unit_model.remove(iter);
810 break;
811 }
812
813 } while (unit_model.iter_next(ref iter));
11216eb0
ZJS
814
815 unit_map.unset(id);
c1e1601e
LP
816 }
817
5d44db4a 818 public void on_job_removed(uint32 id, ObjectPath path, string res) {
77a38d30
LP
819 TreeIter iter;
820 if (!(job_model.get_iter_first(out iter)))
821 return;
822
823 do {
824 uint32 j;
825
826 job_model.get(iter, 5, out j);
827
828 if (id == j) {
829 if (current_job_id == j)
830 clear_job();
831
832 job_model.remove(iter);
833
834 break;
835 }
c1e1601e 836
77a38d30
LP
837 } while (job_model.iter_next(ref iter));
838 }
839
c4e2ceae 840 public void on_unit_changed(Properties p, string iface, HashTable<string, Value?> changed_properties, string[] invalidated_properties) {
dd28669f 841
26742b3f
LP
842 try {
843 TreeIter iter;
844 string id;
c4e2ceae 845
26742b3f
LP
846 Unit u = Bus.get_proxy_sync(
847 user ? BusType.SESSION : BusType.SYSTEM,
848 p.get_name(),
849 p.get_object_path());
dd28669f 850
26742b3f
LP
851 if (!(unit_model.get_iter_first(out iter)))
852 return;
dd28669f 853
26742b3f 854 id = u.id;
dd28669f 855
26742b3f
LP
856 do {
857 string name;
dd28669f 858
26742b3f 859 unit_model.get(iter, 0, out name);
dd28669f 860
26742b3f
LP
861 if (id == name) {
862 update_unit_iter(iter, id, u);
dd28669f 863
26742b3f
LP
864 if (current_unit_id == id)
865 show_unit(u);
dd28669f 866
26742b3f
LP
867 break;
868 }
869
870 } while (unit_model.iter_next(ref iter));
871
734b60d7 872 } catch (Error e) {
26742b3f
LP
873 show_error(e.message);
874 }
dd28669f
LP
875 }
876
c4e2ceae 877 public void on_job_changed(Properties p, string iface, HashTable<string, Value?> changed_properties, string[] invalidated_properties) {
26742b3f
LP
878 try {
879 TreeIter iter;
880 uint32 id;
dd28669f 881
26742b3f
LP
882 Job j = Bus.get_proxy_sync(
883 user ? BusType.SESSION : BusType.SYSTEM,
884 p.get_name(),
885 p.get_object_path());
c4e2ceae 886
26742b3f
LP
887 if (!(job_model.get_iter_first(out iter)))
888 return;
dd28669f 889
26742b3f 890 id = j.id;
dd28669f 891
26742b3f
LP
892 do {
893 uint32 k;
dd28669f 894
26742b3f 895 job_model.get(iter, 5, out k);
dd28669f 896
26742b3f
LP
897 if (id == k) {
898 update_job_iter(iter, id, j);
dd28669f 899
26742b3f
LP
900 if (current_job_id == id)
901 show_job(j);
dd28669f 902
26742b3f
LP
903 break;
904 }
dd28669f 905
26742b3f
LP
906 } while (job_model.iter_next(ref iter));
907
734b60d7 908 } catch (Error e) {
26742b3f
LP
909 show_error(e.message);
910 }
dd28669f
LP
911 }
912
77a38d30 913 public bool unit_filter(TreeModel model, TreeIter iter) {
a1d82102 914 string id, active_state, job;
77a38d30 915
a1d82102 916 model.get(iter, 0, out id, 3, out active_state, 5, out job);
77a38d30
LP
917
918 if (id == null)
919 return false;
920
a3c159a2
ZJS
921 if (!inactive_checkbox.get_active()
922 && active_state == "inactive" && job == "")
923 return false;
77a38d30 924
a3c159a2
ZJS
925 switch (unit_type_combo_box.get_active()) {
926 case 0:
927 return true;
928 case 1:
e377ad0d 929 return id.has_suffix(".target");
a3c159a2 930 case 2:
e377ad0d 931 return id.has_suffix(".service");
a3c159a2
ZJS
932 case 3:
933 return id.has_suffix(".device");
934 case 4:
935 return id.has_suffix(".mount");
936 case 5:
937 return id.has_suffix(".automount");
938 case 6:
e377ad0d 939 return id.has_suffix(".swap");
a3c159a2 940 case 7:
e377ad0d
ZJS
941 return id.has_suffix(".socket");
942 case 8:
943 return id.has_suffix(".path");
944 case 9:
945 return id.has_suffix(".timer");
946 case 10:
a3c159a2
ZJS
947 return id.has_suffix(".snapshot");
948 default:
949 assert(false);
950 return false;
77a38d30 951 }
77a38d30
LP
952 }
953
954 public void unit_type_changed() {
661ece10 955 TreeModelFilter model = (TreeModelFilter) ((TreeModelSort) unit_view.get_model()).get_model();
77a38d30
LP
956
957 model.refilter();
958 }
959
960 public void on_server_reload() {
961 try {
962 manager.reload();
734b60d7 963 } catch (Error e) {
e6be8af4 964 show_error(e.message);
77a38d30
LP
965 }
966 }
967
968 public void on_server_snapshot() {
969 try {
970 manager.create_snapshot();
971
972 if (unit_type_combo_box.get_active() != 0)
973 unit_type_combo_box.set_active(8);
974
734b60d7 975 } catch (Error e) {
e6be8af4 976 show_error(e.message);
77a38d30 977 }
c1e1601e 978 }
e6be8af4 979
ed42e037 980 public void on_unit_load() {
92aa6f36
LP
981 string t = unit_load_entry.get_text();
982
983 if (t == "")
984 return;
985
ed42e037 986 try {
92aa6f36 987 var path = manager.load_unit(t);
ed42e037 988
26742b3f
LP
989 Unit u = Bus.get_proxy_sync(
990 user ? BusType.SESSION : BusType.SYSTEM,
ed42e037 991 "org.freedesktop.systemd1",
26742b3f 992 path);
ed42e037
LP
993
994 var m = new MessageDialog(this,
995 DialogFlags.DESTROY_WITH_PARENT,
996 MessageType.INFO,
997 ButtonsType.CLOSE,
998 "Unit available as id %s", u.id);
999 m.title = "Unit";
1000 m.run();
1001 m.destroy();
1002
3ff46747 1003 show_unit(u);
734b60d7 1004 } catch (Error e) {
ed42e037
LP
1005 show_error(e.message);
1006 }
1007 }
1008
1009 public void on_unit_load_entry_changed() {
1010 unit_load_button.set_sensitive(unit_load_entry.get_text() != "");
1011 }
1012
d4e6a6f6
LP
1013 public bool on_activate_link(string uri) {
1014
1015 try {
1016 string path = manager.get_unit(uri);
1017
26742b3f
LP
1018 Unit u = Bus.get_proxy_sync(
1019 user ? BusType.SESSION : BusType.SYSTEM,
d4e6a6f6 1020 "org.freedesktop.systemd1",
26742b3f 1021 path);
d4e6a6f6
LP
1022
1023 show_unit(u);
734b60d7 1024 } catch (Error e) {
d4e6a6f6
LP
1025 show_error(e.message);
1026 }
1027
1028 return true;
1029 }
1030
e6be8af4
LP
1031 public void show_error(string e) {
1032 var m = new MessageDialog(this,
1033 DialogFlags.DESTROY_WITH_PARENT,
1034 MessageType.ERROR,
1035 ButtonsType.CLOSE, "%s", e);
ed42e037 1036 m.title = "Error";
e6be8af4
LP
1037 m.run();
1038 m.destroy();
1039 }
1040
501c7d0b
LP
1041}
1042
edddf4ff 1043static const OptionEntry entries[] = {
af2d49f7
LP
1044 { "user", 0, 0, OptionArg.NONE, out user, "Connect to user service manager", null },
1045 { "system", 0, OptionFlags.REVERSE, OptionArg.NONE, out user, "Connect to system manager", null },
edddf4ff
LP
1046 { null }
1047};
1048
e6be8af4
LP
1049void show_error(string e) {
1050 var m = new MessageDialog(null, 0, MessageType.ERROR, ButtonsType.CLOSE, "%s", e);
1051 m.run();
1052 m.destroy();
1053}
1054
490aed58 1055int main(string[] args) {
501c7d0b
LP
1056
1057 try {
edddf4ff
LP
1058 Gtk.init_with_args(ref args, "[OPTION...]", entries, "systemadm");
1059
501c7d0b
LP
1060 MainWindow window = new MainWindow();
1061 window.show_all();
703b48af
LP
1062
1063 Gtk.main();
26742b3f 1064 } catch (IOError e) {
e6be8af4 1065 show_error(e.message);
edddf4ff 1066 } catch (GLib.Error e) {
b8590c19 1067 stderr.printf("%s\n", e.message);
501c7d0b
LP
1068 }
1069
501c7d0b
LP
1070 return 0;
1071}