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 TreeView unit_view;
55 private TreeView job_view;
57 private ListStore unit_model;
58 private ListStore job_model;
60 private Button start_button;
61 private Button stop_button;
62 private Button restart_button;
63 private Button reload_button;
64 private Button cancel_button;
66 private Connection bus;
67 private Manager manager;
69 private RightLabel unit_id_label;
70 private RightLabel unit_description_label;
71 private RightLabel unit_load_state_label;
72 private RightLabel unit_active_state_label;
73 private RightLabel unit_fragment_path_label;
74 private RightLabel unit_active_enter_timestamp_label;
75 private RightLabel unit_active_exit_timestamp_label;
76 private RightLabel unit_can_start_label;
77 private RightLabel unit_can_reload_label;
79 private RightLabel job_id_label;
80 private RightLabel job_state_label;
81 private RightLabel job_type_label;
83 public MainWindow() throws DBus.Error {
85 position = WindowPosition.CENTER;
86 set_default_size(1000, 700);
88 destroy += Gtk.main_quit;
90 Notebook notebook = new Notebook();
93 Box unit_vbox = new VBox(false, 6);
94 notebook.append_page(unit_vbox, new Label("Units"));
95 unit_vbox.set_border_width(12);
97 Box job_vbox = new VBox(false, 6);
98 notebook.append_page(job_vbox, new Label("Jobs"));
99 job_vbox.set_border_width(12);
101 unit_model = new ListStore(6, typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(Unit));
102 job_model = new ListStore(5, typeof(string), typeof(string), typeof(string), typeof(string), typeof(Job));
104 unit_view = new TreeView.with_model(unit_model);
105 job_view = new TreeView.with_model(job_model);
107 unit_view.cursor_changed += unit_changed;
108 job_view.cursor_changed += job_changed;
110 unit_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 0);
111 unit_view.insert_column_with_attributes(-1, "Description", new CellRendererText(), "text", 1);
112 unit_view.insert_column_with_attributes(-1, "Load State", new CellRendererText(), "text", 2);
113 unit_view.insert_column_with_attributes(-1, "Active State", new CellRendererText(), "text", 3);
114 unit_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 4);
116 job_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 0);
117 job_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 1);
118 job_view.insert_column_with_attributes(-1, "Type", new CellRendererText(), "text", 2);
119 job_view.insert_column_with_attributes(-1, "State", new CellRendererText(), "text", 3);
121 ScrolledWindow scroll = new ScrolledWindow(null, null);
122 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
123 scroll.set_shadow_type(ShadowType.IN);
124 scroll.add(unit_view);
125 unit_vbox.pack_start(scroll, true, true, 0);
127 scroll = new ScrolledWindow(null, null);
128 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
129 scroll.set_shadow_type(ShadowType.IN);
130 scroll.add(job_view);
131 job_vbox.pack_start(scroll, true, true, 0);
133 unit_id_label = new RightLabel();
134 unit_description_label = new RightLabel();
135 unit_load_state_label = new RightLabel();
136 unit_active_state_label = new RightLabel();
137 unit_fragment_path_label = new RightLabel();
138 unit_active_enter_timestamp_label = new RightLabel();
139 unit_active_exit_timestamp_label = new RightLabel();
140 unit_can_start_label = new RightLabel();
141 unit_can_reload_label = new RightLabel();
143 job_id_label = new RightLabel();
144 job_state_label = new RightLabel();
145 job_type_label = new RightLabel();
147 Table unit_table = new Table(9, 2, false);
148 unit_table.set_row_spacings(6);
149 unit_table.set_border_width(12);
150 unit_vbox.pack_start(unit_table, false, true, 0);
152 Table job_table = new Table(2, 2, false);
153 job_table.set_row_spacings(6);
154 job_table.set_border_width(12);
155 job_vbox.pack_start(job_table, false, true, 0);
157 unit_table.attach(new LeftLabel("Id:"), 0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
158 unit_table.attach(unit_id_label, 1, 2, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
159 unit_table.attach(new LeftLabel("Description:"), 0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
160 unit_table.attach(unit_description_label, 1, 2, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
161 unit_table.attach(new LeftLabel("Load State:"), 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
162 unit_table.attach(unit_load_state_label, 1, 2, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
163 unit_table.attach(new LeftLabel("Active State:"), 0, 1, 3, 4, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
164 unit_table.attach(unit_active_state_label, 1, 2, 3, 4, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
165 unit_table.attach(new LeftLabel("Fragment Path:"), 0, 1, 4, 5, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
166 unit_table.attach(unit_fragment_path_label, 1, 2, 4, 5, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
167 unit_table.attach(new LeftLabel("Active Enter Timestamp:"), 0, 1, 5, 6, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
168 unit_table.attach(unit_active_enter_timestamp_label, 1, 2, 5, 6, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
169 unit_table.attach(new LeftLabel("Active Exit Timestamp:"), 0, 1, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
170 unit_table.attach(unit_active_exit_timestamp_label, 1, 2, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
171 unit_table.attach(new LeftLabel("Can Start/Stop:"), 0, 1, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
172 unit_table.attach(unit_can_start_label, 1, 2, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
173 unit_table.attach(new LeftLabel("Can Reload:"), 0, 1, 8, 9, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
174 unit_table.attach(unit_can_reload_label, 1, 2, 8, 9, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
176 job_table.attach(new LeftLabel("Id:"), 0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
177 job_table.attach(job_id_label, 1, 2, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
178 job_table.attach(new LeftLabel("State:"), 0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
179 job_table.attach(job_state_label, 1, 2, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
180 job_table.attach(new LeftLabel("Type:"), 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
181 job_table.attach(job_type_label, 1, 2, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
183 ButtonBox bbox = new HButtonBox();
184 bbox.set_layout(ButtonBoxStyle.START);
186 unit_vbox.pack_start(bbox, false, true, 0);
188 start_button = new Button.with_mnemonic("_Start");
189 stop_button = new Button.with_mnemonic("Sto_p");
190 reload_button = new Button.with_mnemonic("_Reload");
191 restart_button = new Button.with_mnemonic("Res_tart");
193 start_button.clicked += on_start;
194 stop_button.clicked += on_stop;
195 reload_button.clicked += on_reload;
196 restart_button.clicked += on_restart;
198 bbox.pack_start(start_button, false, true, 0);
199 bbox.pack_start(stop_button, false, true, 0);
200 bbox.pack_start(restart_button, false, true, 0);
201 bbox.pack_start(reload_button, false, true, 0);
203 bbox = new HButtonBox();
204 bbox.set_layout(ButtonBoxStyle.START);
206 job_vbox.pack_start(bbox, false, true, 0);
208 cancel_button = new Button.with_mnemonic("_Cancel");
210 cancel_button.clicked += on_cancel;
212 bbox.pack_start(cancel_button, false, true, 0);
214 bus = Bus.get(session ? BusType.SESSION : BusType.SYSTEM);
216 manager = bus.get_object(
217 "org.freedesktop.systemd1",
218 "/org/freedesktop/systemd1",
219 "org.freedesktop.systemd1") as Manager;
221 manager.unit_new += on_unit_new;
222 manager.job_new += on_job_new;
223 manager.unit_removed += on_unit_removed;
224 manager.job_removed += on_job_removed;
229 populate_unit_model();
230 populate_job_model();
233 public void populate_unit_model() throws DBus.Error {
236 var list = manager.list_units();
238 foreach (var i in list) {
241 Unit u = bus.get_object(
242 "org.freedesktop.systemd1",
244 "org.freedesktop.systemd1.Unit") as Unit;
246 unit_model.append(out iter);
252 4, i.job_type != "" ? "→ %s".printf(i.job_type) : "",
257 public void populate_job_model() throws DBus.Error {
260 var list = manager.list_jobs();
262 foreach (var i in list) {
265 Job j = bus.get_object(
266 "org.freedesktop.systemd1",
268 "org.freedesktop.systemd1.Job") as Job;
270 job_model.append(out iter);
272 0, "%u".printf(i.id),
274 2, "→ %s".printf(i.type),
280 public Unit? get_current_unit() {
282 unit_view.get_cursor(out p, null);
290 unit_model.get_iter(out iter, p);
291 unit_model.get(iter, 5, out u);
296 public void unit_changed() {
297 Unit u = get_current_unit();
305 public void clear_unit() {
306 start_button.set_sensitive(false);
307 stop_button.set_sensitive(false);
308 reload_button.set_sensitive(false);
309 restart_button.set_sensitive(false);
311 unit_id_label.set_text_or_na();
312 unit_description_label.set_text_or_na();
313 unit_load_state_label.set_text_or_na();
314 unit_active_state_label.set_text_or_na();
315 unit_fragment_path_label.set_text_or_na();
316 unit_active_enter_timestamp_label.set_text_or_na();
317 unit_active_exit_timestamp_label.set_text_or_na();
318 unit_can_reload_label.set_text_or_na();
319 unit_can_start_label.set_text_or_na();
322 public void show_unit(Unit unit) {
323 unit_id_label.set_text_or_na(unit.id);
324 unit_description_label.set_text_or_na(unit.description);
325 unit_load_state_label.set_text_or_na(unit.load_state);
326 unit_active_state_label.set_text_or_na(unit.active_state);
327 unit_fragment_path_label.set_text_or_na(unit.fragment_path);
329 uint64 t = unit.active_enter_timestamp;
331 TimeVal tv = { (long) (t / 1000000), (long) (t % 1000000) };
332 unit_active_enter_timestamp_label.set_text_or_na(tv.to_iso8601());
334 unit_active_enter_timestamp_label.set_text_or_na();
336 t = unit.active_exit_timestamp;
338 TimeVal tv = { (long) (t / 1000000), (long) (t % 1000000) };
339 unit_active_exit_timestamp_label.set_text_or_na(tv.to_iso8601());
341 unit_active_exit_timestamp_label.set_text_or_na();
343 bool b = unit.can_start;
344 start_button.set_sensitive(b);
345 stop_button.set_sensitive(b);
346 restart_button.set_sensitive(b);
347 unit_can_start_label.set_text_or_na(b ? "Yes" : "No");
350 reload_button.set_sensitive(b);
351 unit_can_reload_label.set_text_or_na(b ? "Yes" : "No");
354 public Job? get_current_job() {
356 job_view.get_cursor(out p, null);
364 job_model.get_iter(out iter, p);
365 job_model.get(iter, 4, out j);
370 public void job_changed() {
371 Job j = get_current_job();
379 public void clear_job() {
380 job_id_label.set_text_or_na();
381 job_state_label.set_text_or_na();
382 job_type_label.set_text_or_na();
385 public void show_job(Job job) {
386 job_id_label.set_text_or_na("%u".printf(job.id));
387 job_state_label.set_text_or_na(job.state);
388 job_type_label.set_text_or_na(job.job_type);
391 public void on_start() {
392 Unit u = get_current_unit();
399 } catch (DBus.Error e) {
400 message("%s", e.message);
404 public void on_stop() {
405 Unit u = get_current_unit();
412 } catch (DBus.Error e) {
413 message("%s", e.message);
417 public void on_reload() {
418 Unit u = get_current_unit();
425 } catch (DBus.Error e) {
426 message("%s", e.message);
430 public void on_restart() {
431 Unit u = get_current_unit();
437 u.restart("replace");
438 } catch (DBus.Error e) {
439 message("%s", e.message);
443 public void on_cancel() {
444 Job j = get_current_job();
451 } catch (DBus.Error e) {
452 message("%s", e.message);
456 public void on_unit_new(string id, ObjectPath path) {
457 stderr.printf("new path %s", path);
459 Unit u = bus.get_object(
460 "org.freedesktop.systemd1",
462 "org.freedesktop.systemd1.Unit") as Unit;
465 Unit.JobLink jl = u.job;
468 Job j = bus.get_object(
469 "org.freedesktop.systemd1",
471 "org.freedesktop.systemd1.Job") as Job;
477 unit_model.append(out iter);
483 4, t != "" ? "→ %s".printf(t) : "",
487 public void on_job_new(uint32 id, ObjectPath path) {
488 stderr.printf("new path %s", path);
490 Job j = bus.get_object(
491 "org.freedesktop.systemd1",
493 "org.freedesktop.systemd1.Job") as Job;
496 job_model.append(out iter);
498 0, "%u".printf(j.id),
500 2, "→ %s".printf(j.job_type),
505 public void on_unit_removed(string id, ObjectPath path) {
506 stdout.printf("Unit %s removed.\n", id);
511 public void on_job_removed(uint32 id, ObjectPath path) {
512 stdout.printf("Job %u removed.\n", id);
518 static const OptionEntry entries[] = {
519 { "session", 0, 0, OptionArg.NONE, out session, "Connect to session bus", null },
520 { "system", 0, OptionFlags.REVERSE, OptionArg.NONE, out session, "Connect to system bus", null },
524 int main (string[] args) {
527 Gtk.init_with_args(ref args, "[OPTION...]", entries, "systemadm");
529 MainWindow window = new MainWindow();
531 } catch (DBus.Error e) {
532 message("%s", e.message);
533 } catch (GLib.Error e) {
534 message("%s", e.message);