2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
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.
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.
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/>.
25 static bool session = false;
27 public class LeftLabel : Label {
28 public LeftLabel(string? text = null) {
30 set_markup("<b>%s</b>".printf(text));
36 public class RightLabel : Label {
37 public RightLabel(string? text = null) {
40 set_ellipsize(EllipsizeMode.START);
44 public void set_text_or_na(string? text = null) {
45 if (text == null || text == "")
46 set_markup("<i>n/a</i>");
52 public class MainWindow : Window {
54 private string? current_unit_id;
55 private uint32 current_job_id;
57 private TreeView unit_view;
58 private TreeView job_view;
60 private ListStore unit_model;
61 private ListStore job_model;
63 private Button start_button;
64 private Button stop_button;
65 private Button restart_button;
66 private Button reload_button;
67 private Button cancel_button;
69 private Button server_snapshot_button;
70 private Button server_reload_button;
72 private Connection bus;
73 private Manager manager;
75 private RightLabel unit_id_label;
76 private RightLabel unit_aliases_label;
77 private RightLabel unit_description_label;
78 private RightLabel unit_load_state_label;
79 private RightLabel unit_active_state_label;
80 private RightLabel unit_sub_state_label;
81 private RightLabel unit_fragment_path_label;
82 private RightLabel unit_active_enter_timestamp_label;
83 private RightLabel unit_active_exit_timestamp_label;
84 private RightLabel unit_can_start_label;
85 private RightLabel unit_can_reload_label;
86 private RightLabel unit_cgroup_label;
88 private RightLabel job_id_label;
89 private RightLabel job_state_label;
90 private RightLabel job_type_label;
92 private ComboBox unit_type_combo_box;
94 public MainWindow() throws DBus.Error {
96 position = WindowPosition.CENTER;
97 set_default_size(1000, 700);
99 destroy += Gtk.main_quit;
101 Notebook notebook = new Notebook();
104 Box unit_vbox = new VBox(false, 12);
105 notebook.append_page(unit_vbox, new Label("Units"));
106 unit_vbox.set_border_width(12);
108 Box job_vbox = new VBox(false, 12);
109 notebook.append_page(job_vbox, new Label("Jobs"));
110 job_vbox.set_border_width(12);
112 unit_type_combo_box = new ComboBox.text();
113 Box type_hbox = new HBox(false, 6);
114 type_hbox.pack_start(unit_type_combo_box, false, false, 0);
115 unit_vbox.pack_start(type_hbox, false, false, 0);
117 unit_type_combo_box.append_text("Show All Units");
118 unit_type_combo_box.append_text("Show Only Live Units");
119 unit_type_combo_box.append_text("Services");
120 unit_type_combo_box.append_text("Sockets");
121 unit_type_combo_box.append_text("Devices");
122 unit_type_combo_box.append_text("Mounts");
123 unit_type_combo_box.append_text("Automounts");
124 unit_type_combo_box.append_text("Targets");
125 unit_type_combo_box.append_text("Snapshots");
126 unit_type_combo_box.set_active(1);
127 unit_type_combo_box.changed += unit_type_changed;
129 server_snapshot_button = new Button.with_mnemonic("Take _Snapshot");
130 server_reload_button = new Button.with_mnemonic("Reload _Configuration");
132 server_snapshot_button.clicked += on_server_snapshot;
133 server_reload_button.clicked += on_server_reload;
135 type_hbox.pack_end(server_snapshot_button, false, true, 0);
136 type_hbox.pack_end(server_reload_button, false, true, 0);
138 unit_model = new ListStore(7, typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(Unit));
139 job_model = new ListStore(6, typeof(string), typeof(string), typeof(string), typeof(string), typeof(Job), typeof(uint32));
141 TreeModelFilter unit_model_filter;
142 unit_model_filter = new TreeModelFilter(unit_model, null);
143 unit_model_filter.set_visible_func(unit_filter);
145 unit_view = new TreeView.with_model(unit_model_filter);
146 job_view = new TreeView.with_model(job_model);
148 unit_view.cursor_changed += unit_changed;
149 job_view.cursor_changed += job_changed;
151 unit_view.insert_column_with_attributes(-1, "Load State", new CellRendererText(), "text", 2);
152 unit_view.insert_column_with_attributes(-1, "Active State", new CellRendererText(), "text", 3);
153 unit_view.insert_column_with_attributes(-1, "Unit State", new CellRendererText(), "text", 4);
154 unit_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 0);
155 unit_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 5);
157 job_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 0);
158 job_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 1);
159 job_view.insert_column_with_attributes(-1, "Type", new CellRendererText(), "text", 2);
160 job_view.insert_column_with_attributes(-1, "State", new CellRendererText(), "text", 3);
162 ScrolledWindow scroll = new ScrolledWindow(null, null);
163 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
164 scroll.set_shadow_type(ShadowType.IN);
165 scroll.add(unit_view);
166 unit_vbox.pack_start(scroll, true, true, 0);
168 scroll = new ScrolledWindow(null, null);
169 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
170 scroll.set_shadow_type(ShadowType.IN);
171 scroll.add(job_view);
172 job_vbox.pack_start(scroll, true, true, 0);
174 unit_id_label = new RightLabel();
175 unit_aliases_label = new RightLabel();
176 unit_description_label = new RightLabel();
177 unit_load_state_label = new RightLabel();
178 unit_active_state_label = new RightLabel();
179 unit_sub_state_label = new RightLabel();
180 unit_fragment_path_label = new RightLabel();
181 unit_active_enter_timestamp_label = new RightLabel();
182 unit_active_exit_timestamp_label = new RightLabel();
183 unit_can_start_label = new RightLabel();
184 unit_can_reload_label = new RightLabel();
185 unit_cgroup_label = new RightLabel();
187 job_id_label = new RightLabel();
188 job_state_label = new RightLabel();
189 job_type_label = new RightLabel();
191 Table unit_table = new Table(8, 6, false);
192 unit_table.set_row_spacings(6);
193 unit_table.set_border_width(0);
194 unit_vbox.pack_start(unit_table, false, true, 0);
196 Table job_table = new Table(2, 2, false);
197 job_table.set_row_spacings(6);
198 job_table.set_border_width(0);
199 job_vbox.pack_start(job_table, false, true, 0);
201 unit_table.attach(new LeftLabel("Id:"), 0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
202 unit_table.attach(unit_id_label, 1, 5, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
203 unit_table.attach(new LeftLabel("Aliases:"), 0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
204 unit_table.attach(unit_aliases_label, 1, 5, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
205 unit_table.attach(new LeftLabel("Description:"), 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
206 unit_table.attach(unit_description_label, 1, 5, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
207 unit_table.attach(new LeftLabel("Fragment Path:"), 0, 1, 3, 4, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
208 unit_table.attach(unit_fragment_path_label, 1, 5, 3, 4, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
209 unit_table.attach(new LeftLabel("Control Group:"), 0, 1, 4, 5, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
210 unit_table.attach(unit_cgroup_label, 1, 5, 4, 5, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
212 unit_table.attach(new LeftLabel("Load State:"), 0, 1, 5, 6, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
213 unit_table.attach(unit_load_state_label, 1, 2, 5, 6, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
214 unit_table.attach(new LeftLabel("Active State:"), 0, 1, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
215 unit_table.attach(unit_active_state_label, 1, 2, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
216 unit_table.attach(new LeftLabel("Unit State:"), 0, 1, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
217 unit_table.attach(unit_sub_state_label, 1, 2, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
219 unit_table.attach(new LeftLabel("Active Enter Timestamp:"), 2, 3, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
220 unit_table.attach(unit_active_enter_timestamp_label, 3, 4, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
221 unit_table.attach(new LeftLabel("Active Exit Timestamp:"), 2, 3, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
222 unit_table.attach(unit_active_exit_timestamp_label, 3, 4, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
224 unit_table.attach(new LeftLabel("Can Start/Stop:"), 4, 5, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
225 unit_table.attach(unit_can_start_label, 5, 6, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
226 unit_table.attach(new LeftLabel("Can Reload:"), 4, 5, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
227 unit_table.attach(unit_can_reload_label, 5, 6, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
229 job_table.attach(new LeftLabel("Id:"), 0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
230 job_table.attach(job_id_label, 1, 2, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
231 job_table.attach(new LeftLabel("State:"), 0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
232 job_table.attach(job_state_label, 1, 2, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
233 job_table.attach(new LeftLabel("Type:"), 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
234 job_table.attach(job_type_label, 1, 2, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
236 ButtonBox bbox = new HButtonBox();
237 bbox.set_layout(ButtonBoxStyle.START);
239 unit_vbox.pack_start(bbox, false, true, 0);
241 start_button = new Button.with_mnemonic("_Start");
242 stop_button = new Button.with_mnemonic("Sto_p");
243 reload_button = new Button.with_mnemonic("_Reload");
244 restart_button = new Button.with_mnemonic("Res_tart");
246 start_button.clicked += on_start;
247 stop_button.clicked += on_stop;
248 reload_button.clicked += on_reload;
249 restart_button.clicked += on_restart;
251 bbox.pack_start(start_button, false, true, 0);
252 bbox.pack_start(stop_button, false, true, 0);
253 bbox.pack_start(restart_button, false, true, 0);
254 bbox.pack_start(reload_button, false, true, 0);
256 bbox = new HButtonBox();
257 bbox.set_layout(ButtonBoxStyle.START);
259 job_vbox.pack_start(bbox, false, true, 0);
261 cancel_button = new Button.with_mnemonic("_Cancel");
263 cancel_button.clicked += on_cancel;
265 bbox.pack_start(cancel_button, false, true, 0);
267 bus = Bus.get(session ? BusType.SESSION : BusType.SYSTEM);
269 manager = bus.get_object(
270 "org.freedesktop.systemd1",
271 "/org/freedesktop/systemd1",
272 "org.freedesktop.systemd1.Manager") as Manager;
274 manager.unit_new += on_unit_new;
275 manager.job_new += on_job_new;
276 manager.unit_removed += on_unit_removed;
277 manager.job_removed += on_job_removed;
283 populate_unit_model();
284 populate_job_model();
287 public void populate_unit_model() throws DBus.Error {
290 var list = manager.list_units();
292 foreach (var i in list) {
295 Unit u = bus.get_object(
296 "org.freedesktop.systemd1",
298 "org.freedesktop.systemd1.Unit") as Unit;
300 unit_model.append(out iter);
307 5, i.job_type != "" ? "→ %s".printf(i.job_type) : "",
312 public void populate_job_model() throws DBus.Error {
315 var list = manager.list_jobs();
317 foreach (var i in list) {
320 Job j = bus.get_object(
321 "org.freedesktop.systemd1",
323 "org.freedesktop.systemd1.Job") as Job;
325 job_model.append(out iter);
327 0, "%u".printf(i.id),
329 2, "→ %s".printf(i.type),
336 public Unit? get_current_unit() {
338 unit_view.get_cursor(out p, null);
343 TreeModel model = unit_view.get_model();
347 model.get_iter(out iter, p);
348 model.get(iter, 6, out u);
353 public void unit_changed() {
354 Unit u = get_current_unit();
362 public void clear_unit() {
363 current_unit_id = null;
365 start_button.set_sensitive(false);
366 stop_button.set_sensitive(false);
367 reload_button.set_sensitive(false);
368 restart_button.set_sensitive(false);
370 unit_id_label.set_text_or_na();
371 unit_aliases_label.set_text_or_na();
372 unit_description_label.set_text_or_na();
373 unit_load_state_label.set_text_or_na();
374 unit_active_state_label.set_text_or_na();
375 unit_sub_state_label.set_text_or_na();
376 unit_fragment_path_label.set_text_or_na();
377 unit_active_enter_timestamp_label.set_text_or_na();
378 unit_active_exit_timestamp_label.set_text_or_na();
379 unit_can_reload_label.set_text_or_na();
380 unit_can_start_label.set_text_or_na();
381 unit_cgroup_label.set_text_or_na();
384 public void show_unit(Unit unit) {
385 current_unit_id = unit.id;
387 unit_id_label.set_text_or_na(current_unit_id);
388 unit_aliases_label.set_text_or_na(string.joinv("\n", unit.names));
390 unit_description_label.set_text_or_na(unit.description);
391 unit_load_state_label.set_text_or_na(unit.load_state);
392 unit_active_state_label.set_text_or_na(unit.active_state);
393 unit_sub_state_label.set_text_or_na(unit.sub_state);
394 unit_fragment_path_label.set_text_or_na(unit.fragment_path);
396 uint64 t = unit.active_enter_timestamp;
398 TimeVal tv = { (long) (t / 1000000), (long) (t % 1000000) };
399 unit_active_enter_timestamp_label.set_text_or_na(tv.to_iso8601());
401 unit_active_enter_timestamp_label.set_text_or_na();
403 t = unit.active_exit_timestamp;
405 TimeVal tv = { (long) (t / 1000000), (long) (t % 1000000) };
406 unit_active_exit_timestamp_label.set_text_or_na(tv.to_iso8601());
408 unit_active_exit_timestamp_label.set_text_or_na();
410 bool b = unit.can_start;
411 start_button.set_sensitive(b);
412 stop_button.set_sensitive(b);
413 restart_button.set_sensitive(b);
414 unit_can_start_label.set_text_or_na(b ? "Yes" : "No");
417 reload_button.set_sensitive(b);
418 unit_can_reload_label.set_text_or_na(b ? "Yes" : "No");
420 unit_cgroup_label.set_text_or_na(unit.default_control_group);
423 public Job? get_current_job() {
425 job_view.get_cursor(out p, null);
431 TreeModel model = job_view.get_model();
434 model.get_iter(out iter, p);
435 model.get(iter, 4, out j);
440 public void job_changed() {
441 Job j = get_current_job();
449 public void clear_job() {
452 job_id_label.set_text_or_na();
453 job_state_label.set_text_or_na();
454 job_type_label.set_text_or_na();
456 cancel_button.set_sensitive(false);
459 public void show_job(Job job) {
460 current_job_id = job.id;
462 job_id_label.set_text_or_na("%u".printf(current_job_id));
463 job_state_label.set_text_or_na(job.state);
464 job_type_label.set_text_or_na(job.job_type);
466 cancel_button.set_sensitive(true);
469 public void on_start() {
470 Unit u = get_current_unit();
477 } catch (DBus.Error e) {
478 message("%s", e.message);
482 public void on_stop() {
483 Unit u = get_current_unit();
490 } catch (DBus.Error e) {
491 message("%s", e.message);
495 public void on_reload() {
496 Unit u = get_current_unit();
503 } catch (DBus.Error e) {
504 message("%s", e.message);
508 public void on_restart() {
509 Unit u = get_current_unit();
515 u.restart("replace");
516 } catch (DBus.Error e) {
517 message("%s", e.message);
521 public void on_cancel() {
522 Job j = get_current_job();
529 } catch (DBus.Error e) {
530 message("%s", e.message);
534 public void on_unit_new(string id, ObjectPath path) {
535 Unit u = bus.get_object(
536 "org.freedesktop.systemd1",
538 "org.freedesktop.systemd1.Unit") as Unit;
541 Unit.JobLink jl = u.job;
544 Job j = bus.get_object(
545 "org.freedesktop.systemd1",
547 "org.freedesktop.systemd1.Job") as Job;
553 unit_model.append(out iter);
560 5, t != "" ? "→ %s".printf(t) : "",
564 public void on_job_new(uint32 id, ObjectPath path) {
565 Job j = bus.get_object(
566 "org.freedesktop.systemd1",
568 "org.freedesktop.systemd1.Job") as Job;
571 job_model.append(out iter);
573 0, "%u".printf(j.id),
575 2, "→ %s".printf(j.job_type),
581 public void on_unit_removed(string id, ObjectPath path) {
583 if (!(unit_model.get_iter_first(out iter)))
589 unit_model.get(iter, 0, out name);
592 if (current_unit_id == name)
595 unit_model.remove(iter);
599 } while (unit_model.iter_next(ref iter));
602 public void on_job_removed(uint32 id, ObjectPath path) {
604 if (!(job_model.get_iter_first(out iter)))
610 job_model.get(iter, 5, out j);
613 if (current_job_id == j)
616 job_model.remove(iter);
621 } while (job_model.iter_next(ref iter));
624 public bool unit_filter(TreeModel model, TreeIter iter) {
625 string id, active_state;
627 model.get(iter, 0, out id, 3, out active_state);
632 switch (unit_type_combo_box.get_active()) {
638 return active_state != "inactive";
641 return id.has_suffix(".service");
644 return id.has_suffix(".socket");
647 return id.has_suffix(".device");
650 return id.has_suffix(".mount");
653 return id.has_suffix(".automount");
656 return id.has_suffix(".target");
659 return id.has_suffix(".snapshot");
665 public void unit_type_changed() {
666 TreeModelFilter model = (TreeModelFilter) unit_view.get_model();
671 public void on_server_reload() {
674 } catch (DBus.Error e) {
675 message("%s", e.message);
679 public void on_server_snapshot() {
681 manager.create_snapshot();
683 if (unit_type_combo_box.get_active() != 0)
684 unit_type_combo_box.set_active(8);
686 } catch (DBus.Error e) {
687 message("%s", e.message);
692 static const OptionEntry entries[] = {
693 { "session", 0, 0, OptionArg.NONE, out session, "Connect to session bus", null },
694 { "system", 0, OptionFlags.REVERSE, OptionArg.NONE, out session, "Connect to system bus", null },
698 int main (string[] args) {
701 Gtk.init_with_args(ref args, "[OPTION...]", entries, "systemadm");
703 MainWindow window = new MainWindow();
707 } catch (DBus.Error e) {
708 message("%s", e.message);
709 } catch (GLib.Error e) {
710 message("%s", e.message);