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 Box unit_load_hbox = new HBox(false, 6);
141 unit_load_hbox.pack_start(unit_load_entry, false, true, 0);
142 unit_load_hbox.pack_start(unit_load_button, false, true, 0);
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_hbox, false, true, 24);
154 unit_model = new ListStore(7, typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(Unit));
155 job_model = new ListStore(6, typeof(string), typeof(string), typeof(string), typeof(string), typeof(Job), typeof(uint32));
157 TreeModelFilter unit_model_filter;
158 unit_model_filter = new TreeModelFilter(unit_model, null);
159 unit_model_filter.set_visible_func(unit_filter);
161 unit_view = new TreeView.with_model(unit_model_filter);
162 job_view = new TreeView.with_model(job_model);
164 unit_view.cursor_changed += unit_changed;
165 job_view.cursor_changed += job_changed;
167 unit_view.insert_column_with_attributes(-1, "Load State", new CellRendererText(), "text", 2);
168 unit_view.insert_column_with_attributes(-1, "Active State", new CellRendererText(), "text", 3);
169 unit_view.insert_column_with_attributes(-1, "Unit State", new CellRendererText(), "text", 4);
170 unit_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 0);
171 unit_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 5);
173 job_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 0);
174 job_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 1);
175 job_view.insert_column_with_attributes(-1, "Type", new CellRendererText(), "text", 2);
176 job_view.insert_column_with_attributes(-1, "State", new CellRendererText(), "text", 3);
178 ScrolledWindow scroll = new ScrolledWindow(null, null);
179 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
180 scroll.set_shadow_type(ShadowType.IN);
181 scroll.add(unit_view);
182 unit_vbox.pack_start(scroll, true, true, 0);
184 scroll = new ScrolledWindow(null, null);
185 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
186 scroll.set_shadow_type(ShadowType.IN);
187 scroll.add(job_view);
188 job_vbox.pack_start(scroll, true, true, 0);
190 unit_id_label = new RightLabel();
191 unit_aliases_label = new RightLabel();
192 unit_description_label = new RightLabel();
193 unit_load_state_label = new RightLabel();
194 unit_active_state_label = new RightLabel();
195 unit_sub_state_label = new RightLabel();
196 unit_fragment_path_label = new RightLabel();
197 unit_active_enter_timestamp_label = new RightLabel();
198 unit_active_exit_timestamp_label = new RightLabel();
199 unit_can_start_label = new RightLabel();
200 unit_can_reload_label = new RightLabel();
201 unit_cgroup_label = new RightLabel();
203 job_id_label = new RightLabel();
204 job_state_label = new RightLabel();
205 job_type_label = new RightLabel();
207 Table unit_table = new Table(8, 6, false);
208 unit_table.set_row_spacings(6);
209 unit_table.set_border_width(0);
210 unit_vbox.pack_start(unit_table, false, true, 0);
212 Table job_table = new Table(2, 2, false);
213 job_table.set_row_spacings(6);
214 job_table.set_border_width(0);
215 job_vbox.pack_start(job_table, false, true, 0);
217 unit_table.attach(new LeftLabel("Id:"), 0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
218 unit_table.attach(unit_id_label, 1, 6, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
219 unit_table.attach(new LeftLabel("Aliases:"), 0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
220 unit_table.attach(unit_aliases_label, 1, 6, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
221 unit_table.attach(new LeftLabel("Description:"), 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
222 unit_table.attach(unit_description_label, 1, 6, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
223 unit_table.attach(new LeftLabel("Fragment Path:"), 0, 1, 3, 4, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
224 unit_table.attach(unit_fragment_path_label, 1, 6, 3, 4, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
225 unit_table.attach(new LeftLabel("Control Group:"), 0, 1, 4, 5, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
226 unit_table.attach(unit_cgroup_label, 1, 6, 4, 5, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
228 unit_table.attach(new LeftLabel("Load State:"), 0, 1, 5, 6, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
229 unit_table.attach(unit_load_state_label, 1, 2, 5, 6, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
230 unit_table.attach(new LeftLabel("Active State:"), 0, 1, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
231 unit_table.attach(unit_active_state_label, 1, 2, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
232 unit_table.attach(new LeftLabel("Unit State:"), 0, 1, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
233 unit_table.attach(unit_sub_state_label, 1, 2, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
235 unit_table.attach(new LeftLabel("Active Enter Timestamp:"), 2, 3, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
236 unit_table.attach(unit_active_enter_timestamp_label, 3, 4, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
237 unit_table.attach(new LeftLabel("Active Exit Timestamp:"), 2, 3, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
238 unit_table.attach(unit_active_exit_timestamp_label, 3, 4, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
240 unit_table.attach(new LeftLabel("Can Start/Stop:"), 4, 5, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
241 unit_table.attach(unit_can_start_label, 5, 6, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
242 unit_table.attach(new LeftLabel("Can Reload:"), 4, 5, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
243 unit_table.attach(unit_can_reload_label, 5, 6, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
245 job_table.attach(new LeftLabel("Id:"), 0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
246 job_table.attach(job_id_label, 1, 2, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
247 job_table.attach(new LeftLabel("State:"), 0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
248 job_table.attach(job_state_label, 1, 2, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
249 job_table.attach(new LeftLabel("Type:"), 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
250 job_table.attach(job_type_label, 1, 2, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
252 ButtonBox bbox = new HButtonBox();
253 bbox.set_layout(ButtonBoxStyle.START);
255 unit_vbox.pack_start(bbox, false, true, 0);
257 start_button = new Button.with_mnemonic("_Start");
258 stop_button = new Button.with_mnemonic("Sto_p");
259 reload_button = new Button.with_mnemonic("_Reload");
260 restart_button = new Button.with_mnemonic("Res_tart");
262 start_button.clicked += on_start;
263 stop_button.clicked += on_stop;
264 reload_button.clicked += on_reload;
265 restart_button.clicked += on_restart;
267 bbox.pack_start(start_button, false, true, 0);
268 bbox.pack_start(stop_button, false, true, 0);
269 bbox.pack_start(restart_button, false, true, 0);
270 bbox.pack_start(reload_button, false, true, 0);
272 bbox = new HButtonBox();
273 bbox.set_layout(ButtonBoxStyle.START);
275 job_vbox.pack_start(bbox, false, true, 0);
277 cancel_button = new Button.with_mnemonic("_Cancel");
279 cancel_button.clicked += on_cancel;
281 bbox.pack_start(cancel_button, false, true, 0);
283 bus = Bus.get(session ? BusType.SESSION : BusType.SYSTEM);
285 manager = bus.get_object(
286 "org.freedesktop.systemd1",
287 "/org/freedesktop/systemd1",
288 "org.freedesktop.systemd1.Manager") as Manager;
290 manager.unit_new += on_unit_new;
291 manager.job_new += on_job_new;
292 manager.unit_removed += on_unit_removed;
293 manager.job_removed += on_job_removed;
299 populate_unit_model();
300 populate_job_model();
303 public void populate_unit_model() throws DBus.Error {
306 var list = manager.list_units();
308 foreach (var i in list) {
311 Unit u = bus.get_object(
312 "org.freedesktop.systemd1",
314 "org.freedesktop.systemd1.Unit") as Unit;
316 u.changed += on_unit_changed;
318 unit_model.append(out iter);
325 5, i.job_type != "" ? "→ %s".printf(i.job_type) : "",
330 public void populate_job_model() throws DBus.Error {
333 var list = manager.list_jobs();
335 foreach (var i in list) {
338 Job j = bus.get_object(
339 "org.freedesktop.systemd1",
341 "org.freedesktop.systemd1.Job") as Job;
343 j.changed += on_job_changed;
345 job_model.append(out iter);
347 0, "%u".printf(i.id),
349 2, "→ %s".printf(i.type),
356 public Unit? get_current_unit() {
358 unit_view.get_cursor(out p, null);
363 TreeModel model = unit_view.get_model();
367 model.get_iter(out iter, p);
368 model.get(iter, 6, out u);
373 public void unit_changed() {
374 Unit u = get_current_unit();
382 public void clear_unit() {
383 current_unit_id = null;
385 start_button.set_sensitive(false);
386 stop_button.set_sensitive(false);
387 reload_button.set_sensitive(false);
388 restart_button.set_sensitive(false);
390 unit_id_label.set_text_or_na();
391 unit_aliases_label.set_text_or_na();
392 unit_description_label.set_text_or_na();
393 unit_load_state_label.set_text_or_na();
394 unit_active_state_label.set_text_or_na();
395 unit_sub_state_label.set_text_or_na();
396 unit_fragment_path_label.set_text_or_na();
397 unit_active_enter_timestamp_label.set_text_or_na();
398 unit_active_exit_timestamp_label.set_text_or_na();
399 unit_can_reload_label.set_text_or_na();
400 unit_can_start_label.set_text_or_na();
401 unit_cgroup_label.set_text_or_na();
404 public void show_unit(Unit unit) {
405 current_unit_id = unit.id;
407 unit_id_label.set_text_or_na(current_unit_id);
410 foreach (string i in unit.names) {
411 if (i == current_unit_id)
420 unit_aliases_label.set_text_or_na(a);
421 unit_description_label.set_text_or_na(unit.description);
422 unit_load_state_label.set_text_or_na(unit.load_state);
423 unit_active_state_label.set_text_or_na(unit.active_state);
424 unit_sub_state_label.set_text_or_na(unit.sub_state);
425 unit_fragment_path_label.set_text_or_na(unit.fragment_path);
427 uint64 t = unit.active_enter_timestamp;
429 Time timestamp = Time.local((time_t) (t / 1000000));
430 unit_active_enter_timestamp_label.set_text_or_na(timestamp.format("%a, %d %b %Y %H:%M:%S %z"));
432 unit_active_enter_timestamp_label.set_text_or_na();
434 t = unit.active_exit_timestamp;
436 Time timestamp = Time.local((time_t) (t / 1000000));
437 unit_active_exit_timestamp_label.set_text_or_na(timestamp.format("%a, %d %b %Y %H:%M:%S %z"));
439 unit_active_exit_timestamp_label.set_text_or_na();
441 bool b = unit.can_start;
442 start_button.set_sensitive(b);
443 stop_button.set_sensitive(b);
444 restart_button.set_sensitive(b);
445 unit_can_start_label.set_text_or_na(b ? "Yes" : "No");
448 reload_button.set_sensitive(b);
449 unit_can_reload_label.set_text_or_na(b ? "Yes" : "No");
451 unit_cgroup_label.set_text_or_na(unit.default_control_group);
454 public Job? get_current_job() {
456 job_view.get_cursor(out p, null);
462 TreeModel model = job_view.get_model();
465 model.get_iter(out iter, p);
466 model.get(iter, 4, out j);
471 public void job_changed() {
472 Job j = get_current_job();
480 public void clear_job() {
483 job_id_label.set_text_or_na();
484 job_state_label.set_text_or_na();
485 job_type_label.set_text_or_na();
487 cancel_button.set_sensitive(false);
490 public void show_job(Job job) {
491 current_job_id = job.id;
493 job_id_label.set_text_or_na("%u".printf(current_job_id));
494 job_state_label.set_text_or_na(job.state);
495 job_type_label.set_text_or_na(job.job_type);
497 cancel_button.set_sensitive(true);
500 public void on_start() {
501 Unit u = get_current_unit();
508 } catch (DBus.Error e) {
509 show_error(e.message);
513 public void on_stop() {
514 Unit u = get_current_unit();
521 } catch (DBus.Error e) {
522 show_error(e.message);
526 public void on_reload() {
527 Unit u = get_current_unit();
534 } catch (DBus.Error e) {
535 show_error(e.message);
539 public void on_restart() {
540 Unit u = get_current_unit();
546 u.restart("replace");
547 } catch (DBus.Error e) {
548 show_error(e.message);
552 public void on_cancel() {
553 Job j = get_current_job();
560 } catch (DBus.Error e) {
561 show_error(e.message);
565 public void update_unit_iter(TreeIter iter, string id, Unit u) {
568 Unit.JobLink jl = u.job;
571 Job j = bus.get_object(
572 "org.freedesktop.systemd1",
574 "org.freedesktop.systemd1.Job") as Job;
585 5, t != "" ? "→ %s".printf(t) : "",
589 public void on_unit_new(string id, ObjectPath path) {
590 Unit u = bus.get_object(
591 "org.freedesktop.systemd1",
593 "org.freedesktop.systemd1.Unit") as Unit;
595 u.changed += on_unit_changed;
598 unit_model.append(out iter);
599 update_unit_iter(iter, id, u);
602 public void update_job_iter(TreeIter iter, uint32 id, Job j) {
606 2, "→ %s".printf(j.job_type),
612 public void on_job_new(uint32 id, ObjectPath path) {
613 Job j = bus.get_object(
614 "org.freedesktop.systemd1",
616 "org.freedesktop.systemd1.Job") as Job;
618 j.changed += on_job_changed;
621 job_model.append(out iter);
622 update_job_iter(iter, id, j);
625 public void on_unit_removed(string id, ObjectPath path) {
627 if (!(unit_model.get_iter_first(out iter)))
633 unit_model.get(iter, 0, out name);
636 if (current_unit_id == name)
639 unit_model.remove(iter);
643 } while (unit_model.iter_next(ref iter));
646 public void on_job_removed(uint32 id, ObjectPath path) {
648 if (!(job_model.get_iter_first(out iter)))
654 job_model.get(iter, 5, out j);
657 if (current_job_id == j)
660 job_model.remove(iter);
665 } while (job_model.iter_next(ref iter));
668 public void on_unit_changed(Unit u) {
672 if (!(unit_model.get_iter_first(out iter)))
680 unit_model.get(iter, 0, out name);
683 update_unit_iter(iter, id, u);
685 if (current_unit_id == id)
691 } while (unit_model.iter_next(ref iter));
694 public void on_job_changed(Job j) {
698 if (!(job_model.get_iter_first(out iter)))
706 job_model.get(iter, 5, out k);
709 update_job_iter(iter, id, j);
711 if (current_job_id == id)
717 } while (job_model.iter_next(ref iter));
720 public bool unit_filter(TreeModel model, TreeIter iter) {
721 string id, active_state, job;
723 model.get(iter, 0, out id, 3, out active_state, 5, out job);
728 switch (unit_type_combo_box.get_active()) {
734 return active_state != "inactive" || job != "";
737 return id.has_suffix(".service");
740 return id.has_suffix(".socket");
743 return id.has_suffix(".device");
746 return id.has_suffix(".mount");
749 return id.has_suffix(".automount");
752 return id.has_suffix(".target");
755 return id.has_suffix(".snapshot");
761 public void unit_type_changed() {
762 TreeModelFilter model = (TreeModelFilter) unit_view.get_model();
767 public void on_server_reload() {
770 } catch (DBus.Error e) {
771 show_error(e.message);
775 public void on_server_snapshot() {
777 manager.create_snapshot();
779 if (unit_type_combo_box.get_active() != 0)
780 unit_type_combo_box.set_active(8);
782 } catch (DBus.Error e) {
783 show_error(e.message);
787 public void on_unit_load() {
788 string t = unit_load_entry.get_text();
794 var path = manager.load_unit(t);
796 Unit u = bus.get_object(
797 "org.freedesktop.systemd1",
799 "org.freedesktop.systemd1.Unit") as Unit;
801 var m = new MessageDialog(this,
802 DialogFlags.DESTROY_WITH_PARENT,
805 "Unit available as id %s", u.id);
810 } catch (DBus.Error e) {
811 show_error(e.message);
815 public void on_unit_load_entry_changed() {
816 unit_load_button.set_sensitive(unit_load_entry.get_text() != "");
819 public void show_error(string e) {
820 var m = new MessageDialog(this,
821 DialogFlags.DESTROY_WITH_PARENT,
823 ButtonsType.CLOSE, "%s", e);
831 static const OptionEntry entries[] = {
832 { "session", 0, 0, OptionArg.NONE, out session, "Connect to session bus", null },
833 { "system", 0, OptionFlags.REVERSE, OptionArg.NONE, out session, "Connect to system bus", null },
837 void show_error(string e) {
838 var m = new MessageDialog(null, 0, MessageType.ERROR, ButtonsType.CLOSE, "%s", e);
843 int main (string[] args) {
846 Gtk.init_with_args(ref args, "[OPTION...]", entries, "systemadm");
848 MainWindow window = new MainWindow();
852 } catch (DBus.Error e) {
853 show_error(e.message);
854 } catch (GLib.Error e) {
855 show_error(e.message);