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 Entry unit_load_entry;
70 private Button unit_load_button;
72 private Button server_snapshot_button;
73 private Button server_reload_button;
75 private Connection bus;
76 private Manager manager;
78 private RightLabel unit_id_label;
79 private RightLabel unit_aliases_label;
80 private RightLabel unit_description_label;
81 private RightLabel unit_load_state_label;
82 private RightLabel unit_active_state_label;
83 private RightLabel unit_sub_state_label;
84 private RightLabel unit_fragment_path_label;
85 private RightLabel unit_active_enter_timestamp_label;
86 private RightLabel unit_active_exit_timestamp_label;
87 private RightLabel unit_can_start_label;
88 private RightLabel unit_can_reload_label;
89 private RightLabel unit_cgroup_label;
91 private RightLabel job_id_label;
92 private RightLabel job_state_label;
93 private RightLabel job_type_label;
95 private ComboBox unit_type_combo_box;
97 public MainWindow() throws DBus.Error {
99 position = WindowPosition.CENTER;
100 set_default_size(1000, 700);
101 set_border_width(12);
102 destroy += Gtk.main_quit;
104 Notebook notebook = new Notebook();
107 Box unit_vbox = new VBox(false, 12);
108 notebook.append_page(unit_vbox, new Label("Units"));
109 unit_vbox.set_border_width(12);
111 Box job_vbox = new VBox(false, 12);
112 notebook.append_page(job_vbox, new Label("Jobs"));
113 job_vbox.set_border_width(12);
115 unit_type_combo_box = new ComboBox.text();
116 Box type_hbox = new HBox(false, 6);
117 type_hbox.pack_start(unit_type_combo_box, false, false, 0);
118 unit_vbox.pack_start(type_hbox, false, false, 0);
120 unit_type_combo_box.append_text("Show All Units");
121 unit_type_combo_box.append_text("Show Only Live Units");
122 unit_type_combo_box.append_text("Services");
123 unit_type_combo_box.append_text("Sockets");
124 unit_type_combo_box.append_text("Devices");
125 unit_type_combo_box.append_text("Mounts");
126 unit_type_combo_box.append_text("Automounts");
127 unit_type_combo_box.append_text("Targets");
128 unit_type_combo_box.append_text("Snapshots");
129 unit_type_combo_box.set_active(1);
130 unit_type_combo_box.changed += unit_type_changed;
132 unit_load_entry = new Entry();
133 unit_load_button = new Button.with_mnemonic("_Load");
134 unit_load_button.set_sensitive(false);
136 unit_load_entry.changed += on_unit_load_entry_changed;
137 unit_load_entry.activate += on_unit_load;
138 unit_load_button.clicked += on_unit_load;
140 Gtk.Alignment unit_load_button_alignment = new Gtk.Alignment(0.5f, 0.5f, 1f, 1f);
141 unit_load_button_alignment.right_padding = 24;
142 unit_load_button_alignment.add(unit_load_button);
144 server_snapshot_button = new Button.with_mnemonic("Take S_napshot");
145 server_reload_button = new Button.with_mnemonic("Reload _Configuration");
147 server_snapshot_button.clicked += on_server_snapshot;
148 server_reload_button.clicked += on_server_reload;
150 type_hbox.pack_end(server_snapshot_button, false, true, 0);
151 type_hbox.pack_end(server_reload_button, false, true, 0);
152 type_hbox.pack_end(unit_load_button_alignment, false, true, 0);
153 type_hbox.pack_end(unit_load_entry, false, true, 0);
155 unit_model = new ListStore(7, typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(Unit));
156 job_model = new ListStore(6, typeof(string), typeof(string), typeof(string), typeof(string), typeof(Job), typeof(uint32));
158 TreeModelFilter unit_model_filter;
159 unit_model_filter = new TreeModelFilter(unit_model, null);
160 unit_model_filter.set_visible_func(unit_filter);
162 unit_view = new TreeView.with_model(unit_model_filter);
163 job_view = new TreeView.with_model(job_model);
165 unit_view.cursor_changed += unit_changed;
166 job_view.cursor_changed += job_changed;
168 unit_view.insert_column_with_attributes(-1, "Load State", new CellRendererText(), "text", 2);
169 unit_view.insert_column_with_attributes(-1, "Active State", new CellRendererText(), "text", 3);
170 unit_view.insert_column_with_attributes(-1, "Unit State", new CellRendererText(), "text", 4);
171 unit_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 0);
172 unit_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 5);
174 job_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 0);
175 job_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 1);
176 job_view.insert_column_with_attributes(-1, "Type", new CellRendererText(), "text", 2);
177 job_view.insert_column_with_attributes(-1, "State", new CellRendererText(), "text", 3);
179 ScrolledWindow scroll = new ScrolledWindow(null, null);
180 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
181 scroll.set_shadow_type(ShadowType.IN);
182 scroll.add(unit_view);
183 unit_vbox.pack_start(scroll, true, true, 0);
185 scroll = new ScrolledWindow(null, null);
186 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
187 scroll.set_shadow_type(ShadowType.IN);
188 scroll.add(job_view);
189 job_vbox.pack_start(scroll, true, true, 0);
191 unit_id_label = new RightLabel();
192 unit_aliases_label = new RightLabel();
193 unit_description_label = new RightLabel();
194 unit_load_state_label = new RightLabel();
195 unit_active_state_label = new RightLabel();
196 unit_sub_state_label = new RightLabel();
197 unit_fragment_path_label = new RightLabel();
198 unit_active_enter_timestamp_label = new RightLabel();
199 unit_active_exit_timestamp_label = new RightLabel();
200 unit_can_start_label = new RightLabel();
201 unit_can_reload_label = new RightLabel();
202 unit_cgroup_label = new RightLabel();
204 job_id_label = new RightLabel();
205 job_state_label = new RightLabel();
206 job_type_label = new RightLabel();
208 Table unit_table = new Table(8, 6, false);
209 unit_table.set_row_spacings(6);
210 unit_table.set_border_width(0);
211 unit_vbox.pack_start(unit_table, false, true, 0);
213 Table job_table = new Table(2, 2, false);
214 job_table.set_row_spacings(6);
215 job_table.set_border_width(0);
216 job_vbox.pack_start(job_table, false, true, 0);
218 unit_table.attach(new LeftLabel("Id:"), 0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
219 unit_table.attach(unit_id_label, 1, 6, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
220 unit_table.attach(new LeftLabel("Aliases:"), 0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
221 unit_table.attach(unit_aliases_label, 1, 6, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
222 unit_table.attach(new LeftLabel("Description:"), 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
223 unit_table.attach(unit_description_label, 1, 6, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
224 unit_table.attach(new LeftLabel("Fragment Path:"), 0, 1, 3, 4, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
225 unit_table.attach(unit_fragment_path_label, 1, 6, 3, 4, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
226 unit_table.attach(new LeftLabel("Control Group:"), 0, 1, 4, 5, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
227 unit_table.attach(unit_cgroup_label, 1, 6, 4, 5, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
229 unit_table.attach(new LeftLabel("Load State:"), 0, 1, 5, 6, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
230 unit_table.attach(unit_load_state_label, 1, 2, 5, 6, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
231 unit_table.attach(new LeftLabel("Active State:"), 0, 1, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
232 unit_table.attach(unit_active_state_label, 1, 2, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
233 unit_table.attach(new LeftLabel("Unit State:"), 0, 1, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
234 unit_table.attach(unit_sub_state_label, 1, 2, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
236 unit_table.attach(new LeftLabel("Active Enter Timestamp:"), 2, 3, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
237 unit_table.attach(unit_active_enter_timestamp_label, 3, 4, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
238 unit_table.attach(new LeftLabel("Active Exit Timestamp:"), 2, 3, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
239 unit_table.attach(unit_active_exit_timestamp_label, 3, 4, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
241 unit_table.attach(new LeftLabel("Can Start/Stop:"), 4, 5, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
242 unit_table.attach(unit_can_start_label, 5, 6, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
243 unit_table.attach(new LeftLabel("Can Reload:"), 4, 5, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
244 unit_table.attach(unit_can_reload_label, 5, 6, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
246 job_table.attach(new LeftLabel("Id:"), 0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
247 job_table.attach(job_id_label, 1, 2, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
248 job_table.attach(new LeftLabel("State:"), 0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
249 job_table.attach(job_state_label, 1, 2, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
250 job_table.attach(new LeftLabel("Type:"), 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
251 job_table.attach(job_type_label, 1, 2, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
253 ButtonBox bbox = new HButtonBox();
254 bbox.set_layout(ButtonBoxStyle.START);
256 unit_vbox.pack_start(bbox, false, true, 0);
258 start_button = new Button.with_mnemonic("_Start");
259 stop_button = new Button.with_mnemonic("Sto_p");
260 reload_button = new Button.with_mnemonic("_Reload");
261 restart_button = new Button.with_mnemonic("Res_tart");
263 start_button.clicked += on_start;
264 stop_button.clicked += on_stop;
265 reload_button.clicked += on_reload;
266 restart_button.clicked += on_restart;
268 bbox.pack_start(start_button, false, true, 0);
269 bbox.pack_start(stop_button, false, true, 0);
270 bbox.pack_start(restart_button, false, true, 0);
271 bbox.pack_start(reload_button, false, true, 0);
273 bbox = new HButtonBox();
274 bbox.set_layout(ButtonBoxStyle.START);
276 job_vbox.pack_start(bbox, false, true, 0);
278 cancel_button = new Button.with_mnemonic("_Cancel");
280 cancel_button.clicked += on_cancel;
282 bbox.pack_start(cancel_button, false, true, 0);
284 bus = Bus.get(session ? BusType.SESSION : BusType.SYSTEM);
286 manager = bus.get_object(
287 "org.freedesktop.systemd1",
288 "/org/freedesktop/systemd1",
289 "org.freedesktop.systemd1.Manager") as Manager;
291 manager.unit_new += on_unit_new;
292 manager.job_new += on_job_new;
293 manager.unit_removed += on_unit_removed;
294 manager.job_removed += on_job_removed;
300 populate_unit_model();
301 populate_job_model();
304 public void populate_unit_model() throws DBus.Error {
307 var list = manager.list_units();
309 foreach (var i in list) {
312 Unit u = bus.get_object(
313 "org.freedesktop.systemd1",
315 "org.freedesktop.systemd1.Unit") as Unit;
317 u.changed += on_unit_changed;
319 unit_model.append(out iter);
326 5, i.job_type != "" ? "→ %s".printf(i.job_type) : "",
331 public void populate_job_model() throws DBus.Error {
334 var list = manager.list_jobs();
336 foreach (var i in list) {
339 Job j = bus.get_object(
340 "org.freedesktop.systemd1",
342 "org.freedesktop.systemd1.Job") as Job;
344 j.changed += on_job_changed;
346 job_model.append(out iter);
348 0, "%u".printf(i.id),
350 2, "→ %s".printf(i.type),
357 public Unit? get_current_unit() {
359 unit_view.get_cursor(out p, null);
364 TreeModel model = unit_view.get_model();
368 model.get_iter(out iter, p);
369 model.get(iter, 6, out u);
374 public void unit_changed() {
375 Unit u = get_current_unit();
383 public void clear_unit() {
384 current_unit_id = null;
386 start_button.set_sensitive(false);
387 stop_button.set_sensitive(false);
388 reload_button.set_sensitive(false);
389 restart_button.set_sensitive(false);
391 unit_id_label.set_text_or_na();
392 unit_aliases_label.set_text_or_na();
393 unit_description_label.set_text_or_na();
394 unit_load_state_label.set_text_or_na();
395 unit_active_state_label.set_text_or_na();
396 unit_sub_state_label.set_text_or_na();
397 unit_fragment_path_label.set_text_or_na();
398 unit_active_enter_timestamp_label.set_text_or_na();
399 unit_active_exit_timestamp_label.set_text_or_na();
400 unit_can_reload_label.set_text_or_na();
401 unit_can_start_label.set_text_or_na();
402 unit_cgroup_label.set_text_or_na();
405 public void show_unit(Unit unit) {
406 current_unit_id = unit.id;
408 unit_id_label.set_text_or_na(current_unit_id);
411 foreach (string i in unit.names) {
412 if (i == current_unit_id)
421 unit_aliases_label.set_text_or_na(a);
422 unit_description_label.set_text_or_na(unit.description);
423 unit_load_state_label.set_text_or_na(unit.load_state);
424 unit_active_state_label.set_text_or_na(unit.active_state);
425 unit_sub_state_label.set_text_or_na(unit.sub_state);
426 unit_fragment_path_label.set_text_or_na(unit.fragment_path);
428 uint64 t = unit.active_enter_timestamp;
430 Time timestamp = Time.local((time_t) (t / 1000000));
431 unit_active_enter_timestamp_label.set_text_or_na(timestamp.format("%a, %d %b %Y %H:%M:%S %z"));
433 unit_active_enter_timestamp_label.set_text_or_na();
435 t = unit.active_exit_timestamp;
437 Time timestamp = Time.local((time_t) (t / 1000000));
438 unit_active_exit_timestamp_label.set_text_or_na(timestamp.format("%a, %d %b %Y %H:%M:%S %z"));
440 unit_active_exit_timestamp_label.set_text_or_na();
442 bool b = unit.can_start;
443 start_button.set_sensitive(b);
444 stop_button.set_sensitive(b);
445 restart_button.set_sensitive(b);
446 unit_can_start_label.set_text_or_na(b ? "Yes" : "No");
449 reload_button.set_sensitive(b);
450 unit_can_reload_label.set_text_or_na(b ? "Yes" : "No");
452 unit_cgroup_label.set_text_or_na(unit.default_control_group);
455 public Job? get_current_job() {
457 job_view.get_cursor(out p, null);
463 TreeModel model = job_view.get_model();
466 model.get_iter(out iter, p);
467 model.get(iter, 4, out j);
472 public void job_changed() {
473 Job j = get_current_job();
481 public void clear_job() {
484 job_id_label.set_text_or_na();
485 job_state_label.set_text_or_na();
486 job_type_label.set_text_or_na();
488 cancel_button.set_sensitive(false);
491 public void show_job(Job job) {
492 current_job_id = job.id;
494 job_id_label.set_text_or_na("%u".printf(current_job_id));
495 job_state_label.set_text_or_na(job.state);
496 job_type_label.set_text_or_na(job.job_type);
498 cancel_button.set_sensitive(true);
501 public void on_start() {
502 Unit u = get_current_unit();
509 } catch (DBus.Error e) {
510 show_error(e.message);
514 public void on_stop() {
515 Unit u = get_current_unit();
522 } catch (DBus.Error e) {
523 show_error(e.message);
527 public void on_reload() {
528 Unit u = get_current_unit();
535 } catch (DBus.Error e) {
536 show_error(e.message);
540 public void on_restart() {
541 Unit u = get_current_unit();
547 u.restart("replace");
548 } catch (DBus.Error e) {
549 show_error(e.message);
553 public void on_cancel() {
554 Job j = get_current_job();
561 } catch (DBus.Error e) {
562 show_error(e.message);
566 public void update_unit_iter(TreeIter iter, string id, Unit u) {
569 Unit.JobLink jl = u.job;
572 Job j = bus.get_object(
573 "org.freedesktop.systemd1",
575 "org.freedesktop.systemd1.Job") as Job;
586 5, t != "" ? "→ %s".printf(t) : "",
590 public void on_unit_new(string id, ObjectPath path) {
591 Unit u = bus.get_object(
592 "org.freedesktop.systemd1",
594 "org.freedesktop.systemd1.Unit") as Unit;
596 u.changed += on_unit_changed;
599 unit_model.append(out iter);
600 update_unit_iter(iter, id, u);
603 public void update_job_iter(TreeIter iter, uint32 id, Job j) {
607 2, "→ %s".printf(j.job_type),
613 public void on_job_new(uint32 id, ObjectPath path) {
614 Job j = bus.get_object(
615 "org.freedesktop.systemd1",
617 "org.freedesktop.systemd1.Job") as Job;
619 j.changed += on_job_changed;
622 job_model.append(out iter);
623 update_job_iter(iter, id, j);
626 public void on_unit_removed(string id, ObjectPath path) {
628 if (!(unit_model.get_iter_first(out iter)))
634 unit_model.get(iter, 0, out name);
637 if (current_unit_id == name)
640 unit_model.remove(iter);
644 } while (unit_model.iter_next(ref iter));
647 public void on_job_removed(uint32 id, ObjectPath path) {
649 if (!(job_model.get_iter_first(out iter)))
655 job_model.get(iter, 5, out j);
658 if (current_job_id == j)
661 job_model.remove(iter);
666 } while (job_model.iter_next(ref iter));
669 public void on_unit_changed(Unit u) {
673 if (!(unit_model.get_iter_first(out iter)))
681 unit_model.get(iter, 0, out name);
684 update_unit_iter(iter, id, u);
686 if (current_unit_id == id)
692 } while (unit_model.iter_next(ref iter));
695 public void on_job_changed(Job j) {
699 if (!(job_model.get_iter_first(out iter)))
707 job_model.get(iter, 5, out k);
710 update_job_iter(iter, id, j);
712 if (current_job_id == id)
718 } while (job_model.iter_next(ref iter));
721 public bool unit_filter(TreeModel model, TreeIter iter) {
722 string id, active_state, job;
724 model.get(iter, 0, out id, 3, out active_state, 5, out job);
729 switch (unit_type_combo_box.get_active()) {
735 return active_state != "inactive" || job != "";
738 return id.has_suffix(".service");
741 return id.has_suffix(".socket");
744 return id.has_suffix(".device");
747 return id.has_suffix(".mount");
750 return id.has_suffix(".automount");
753 return id.has_suffix(".target");
756 return id.has_suffix(".snapshot");
762 public void unit_type_changed() {
763 TreeModelFilter model = (TreeModelFilter) unit_view.get_model();
768 public void on_server_reload() {
771 } catch (DBus.Error e) {
772 show_error(e.message);
776 public void on_server_snapshot() {
778 manager.create_snapshot();
780 if (unit_type_combo_box.get_active() != 0)
781 unit_type_combo_box.set_active(8);
783 } catch (DBus.Error e) {
784 show_error(e.message);
788 public void on_unit_load() {
790 var path = manager.load_unit(unit_load_entry.get_text());
792 Unit u = bus.get_object(
793 "org.freedesktop.systemd1",
795 "org.freedesktop.systemd1.Unit") as Unit;
797 var m = new MessageDialog(this,
798 DialogFlags.DESTROY_WITH_PARENT,
801 "Unit available as id %s", u.id);
806 } catch (DBus.Error e) {
807 show_error(e.message);
811 public void on_unit_load_entry_changed() {
812 unit_load_button.set_sensitive(unit_load_entry.get_text() != "");
815 public void show_error(string e) {
816 var m = new MessageDialog(this,
817 DialogFlags.DESTROY_WITH_PARENT,
819 ButtonsType.CLOSE, "%s", e);
827 static const OptionEntry entries[] = {
828 { "session", 0, 0, OptionArg.NONE, out session, "Connect to session bus", null },
829 { "system", 0, OptionFlags.REVERSE, OptionArg.NONE, out session, "Connect to system bus", null },
833 void show_error(string e) {
834 var m = new MessageDialog(null, 0, MessageType.ERROR, ButtonsType.CLOSE, "%s", e);
839 int main (string[] args) {
842 Gtk.init_with_args(ref args, "[OPTION...]", entries, "systemadm");
844 MainWindow window = new MainWindow();
848 } catch (DBus.Error e) {
849 show_error(e.message);
850 } catch (GLib.Error e) {
851 show_error(e.message);