chiark / gitweb /
4cb5c55ef332403c8ee16601aa8cac12db357674
[elogind.git] / src / systemadm.vala
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
20 using Gtk;
21 using GLib;
22 using Pango;
23
24 static bool user = false;
25
26 public 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
33 public 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
40 public class LeftLabel : Label {
41         public LeftLabel(string? text = null) {
42                 if (text != null)
43                         set_markup("<b>%s</b>".printf(text));
44                 set_alignment(0, 0);
45                 set_padding(6, 0);
46         }
47 }
48
49 public class RightLabel : WrapLabel {
50
51         public RightLabel(string? text = null) {
52                 set_selectable(true);
53                 set_text_or_na(text);
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         }
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         }
69 }
70
71 public class MainWindow : Window {
72
73         private string? current_unit_id;
74         private uint32 current_job_id;
75
76         private TreeView unit_view;
77         private TreeView job_view;
78
79         private ListStore unit_model;
80         private ListStore job_model;
81
82         private Gee.HashMap<string, Unit> unit_map;
83
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
90         private Entry unit_load_entry;
91         private Button unit_load_button;
92
93         private Button server_snapshot_button;
94         private Button server_reload_button;
95
96         private Manager manager;
97
98         private RightLabel unit_id_label;
99         private RightLabel unit_dependency_label;
100         private RightLabel unit_description_label;
101         private RightLabel unit_load_state_label;
102         private RightLabel unit_active_state_label;
103         private RightLabel unit_sub_state_label;
104         private RightLabel unit_fragment_path_label;
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;
109         private RightLabel unit_cgroup_label;
110
111         private RightLabel job_id_label;
112         private RightLabel job_state_label;
113         private RightLabel job_type_label;
114
115         private ComboBox unit_type_combo_box;
116         private CheckButton inactive_checkbox;
117
118         public MainWindow() throws IOError {
119                 title = user ? "systemd User Service Manager" : "systemd System Manager";
120                 set_position(WindowPosition.CENTER);
121                 set_default_size(1000, 700);
122                 set_border_width(12);
123                 destroy.connect(Gtk.main_quit);
124
125                 Notebook notebook = new Notebook();
126                 add(notebook);
127
128                 Box unit_vbox = new VBox(false, 12);
129                 notebook.append_page(unit_vbox, new Label("Units"));
130                 unit_vbox.set_border_width(12);
131
132                 Box job_vbox = new VBox(false, 12);
133                 notebook.append_page(job_vbox, new Label("Jobs"));
134                 job_vbox.set_border_width(12);
135
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
141                 unit_type_combo_box.append_text("All unit types");
142                 unit_type_combo_box.append_text("Targets");
143                 unit_type_combo_box.append_text("Services");
144                 unit_type_combo_box.append_text("Devices");
145                 unit_type_combo_box.append_text("Mounts");
146                 unit_type_combo_box.append_text("Automounts");
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");
151                 unit_type_combo_box.append_text("Snapshots");
152                 unit_type_combo_box.set_active(0); // Show All
153                 unit_type_combo_box.changed.connect(unit_type_changed);
154
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
159                 unit_load_entry = new Entry();
160                 unit_load_button = new Button.with_mnemonic("_Load");
161                 unit_load_button.set_sensitive(false);
162
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);
166
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);
170
171                 server_snapshot_button = new Button.with_mnemonic("Take S_napshot");
172                 server_reload_button = new Button.with_mnemonic("Reload _Configuration");
173
174                 server_snapshot_button.clicked.connect(on_server_snapshot);
175                 server_reload_button.clicked.connect(on_server_reload);
176
177                 type_hbox.pack_end(server_snapshot_button, false, true, 0);
178                 type_hbox.pack_end(server_reload_button, false, true, 0);
179                 type_hbox.pack_end(unit_load_hbox, false, true, 24);
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
184                 unit_map = new Gee.HashMap<string, Unit>();
185
186                 TreeModelFilter unit_model_filter;
187                 unit_model_filter = new TreeModelFilter(unit_model, null);
188                 unit_model_filter.set_visible_func(unit_filter);
189
190                 TreeModelSort unit_model_sort = new TreeModelSort.with_model(unit_model_filter);
191
192                 unit_view = new TreeView.with_model(unit_model_sort);
193                 job_view = new TreeView.with_model(job_model);
194
195                 unit_view.cursor_changed.connect(unit_changed);
196                 job_view.cursor_changed.connect(job_changed);
197
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");
203
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");
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();
222                 unit_dependency_label = new RightLabel();
223                 unit_description_label = new RightLabel();
224                 unit_load_state_label = new RightLabel();
225                 unit_active_state_label = new RightLabel();
226                 unit_sub_state_label = new RightLabel();
227                 unit_fragment_path_label = new RightLabel();
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();
232                 unit_cgroup_label = new RightLabel();
233
234                 job_id_label = new RightLabel();
235                 job_state_label = new RightLabel();
236                 job_type_label = new RightLabel();
237
238                 unit_dependency_label.set_track_visited_links(false);
239                 unit_dependency_label.set_selectable(true);
240                 unit_dependency_label.activate_link.connect(on_activate_link);
241
242                 unit_fragment_path_label.set_track_visited_links(false);
243
244                 Table unit_table = new Table(8, 6, false);
245                 unit_table.set_row_spacings(6);
246                 unit_table.set_border_width(0);
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);
251                 job_table.set_border_width(0);
252                 job_vbox.pack_start(job_table, false, true, 0);
253
254                 unit_table.attach(new LeftLabel("Id:"),                     0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
255                 unit_table.attach(unit_id_label,                            1, 6, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
256                 unit_table.attach(new LeftLabel("Description:"),            0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
257                 unit_table.attach(unit_description_label,                   1, 6, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
258                 unit_table.attach(new LeftLabel("Dependencies:"),           0, 1, 3, 4, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
259                 unit_table.attach(unit_dependency_label,                    1, 6, 3, 4, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
260                 unit_table.attach(new LeftLabel("Fragment Path:"),          0, 1, 4, 5, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
261                 unit_table.attach(unit_fragment_path_label,                 1, 6, 4, 5, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
262                 unit_table.attach(new LeftLabel("Control Group:"),          0, 1, 5, 6, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
263                 unit_table.attach(unit_cgroup_label,                        1, 6, 5, 6, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
264
265                 unit_table.attach(new LeftLabel("Load State:"),             0, 1, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
266                 unit_table.attach(unit_load_state_label,                    1, 2, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
267                 unit_table.attach(new LeftLabel("Active State:"),           0, 1, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
268                 unit_table.attach(unit_active_state_label,                  1, 2, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
269                 unit_table.attach(new LeftLabel("Unit State:"),             0, 1, 8, 9, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
270                 unit_table.attach(unit_sub_state_label,                     1, 2, 8, 9, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
271
272                 unit_table.attach(new LeftLabel("Active Enter Timestamp:"), 2, 3, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
273                 unit_table.attach(unit_active_enter_timestamp_label,        3, 4, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
274                 unit_table.attach(new LeftLabel("Active Exit Timestamp:"),  2, 3, 8, 9, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
275                 unit_table.attach(unit_active_exit_timestamp_label,         3, 4, 8, 9, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
276
277                 unit_table.attach(new LeftLabel("Can Start/Stop:"),         4, 5, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
278                 unit_table.attach(unit_can_start_label,                     5, 6, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
279                 unit_table.attach(new LeftLabel("Can Reload:"),             4, 5, 8, 9, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
280                 unit_table.attach(unit_can_reload_label,                    5, 6, 8, 9, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
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);
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
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);
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
316                 cancel_button.clicked.connect(on_cancel);
317
318                 bbox.pack_start(cancel_button, false, true, 0);
319
320                 manager = Bus.get_proxy_sync(
321                                 user ? BusType.SESSION : BusType.SYSTEM,
322                                 "org.freedesktop.systemd1",
323                                 "/org/freedesktop/systemd1");
324
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);
329
330                 manager.subscribe();
331
332                 clear_unit();
333                 clear_job();
334                 populate_unit_model();
335                 populate_job_model();
336         }
337
338         public void populate_unit_model() throws IOError {
339                 unit_model.clear();
340
341                 var list = manager.list_units();
342
343                 foreach (var i in list) {
344                         TreeIter iter;
345
346                         Properties p = Bus.get_proxy_sync(
347                                         user ? BusType.SESSION : BusType.SYSTEM,
348                                         "org.freedesktop.systemd1",
349                                         i.unit_path);
350
351                         p.properties_changed.connect(on_unit_changed);
352
353                         Unit u = Bus.get_proxy_sync(
354                                         user ? BusType.SESSION : BusType.SYSTEM,
355                                         "org.freedesktop.systemd1",
356                                         i.unit_path);
357
358                         unit_map[i.id] = u;
359
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,
366                                        4, i.sub_state,
367                                        5, i.job_type != "" ? "→ %s".printf(i.job_type) : "",
368                                        6, u);
369                 }
370         }
371
372         public void populate_job_model() throws IOError {
373                 job_model.clear();
374
375                 var list = manager.list_jobs();
376
377                 foreach (var i in list) {
378                         TreeIter iter;
379
380                         Properties p = Bus.get_proxy_sync(
381                                         user ? BusType.SESSION : BusType.SYSTEM,
382                                         "org.freedesktop.systemd1",
383                                         i.job_path);
384
385                         p.properties_changed.connect(on_job_changed);
386
387                         Job j = Bus.get_proxy_sync(
388                                         user ? BusType.SESSION : BusType.SYSTEM,
389                                         "org.freedesktop.systemd1",
390                                         i.job_path);
391
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,
398                                       4, j,
399                                       5, i.id);
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
410                 TreeModel model = unit_view.get_model();
411                 TreeIter iter;
412                 Unit u;
413
414                 model.get_iter(out iter, p);
415                 model.get(iter, 6, out u);
416
417                 return u;
418         }
419
420         public Unit? get_unit(string id) {
421                 return this.unit_map[id];
422         }
423
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() {
434                 current_unit_id = null;
435
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();
443                 unit_description_label.set_text_or_na();
444                 unit_load_state_label.set_text_or_na();
445                 unit_active_state_label.set_text_or_na();
446                 unit_sub_state_label.set_text_or_na();
447                 unit_fragment_path_label.set_text_or_na();
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();
452                 unit_cgroup_label.set_text_or_na();
453         }
454
455         public string format_unit_link(string i) {
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>";
470                 return  " <a href='" + i + "'>" + span + "</a>";
471         }
472
473
474         public string make_dependency_string(string? prefix, string word, string[] dependencies) {
475                 Gee.Collection<unowned string> sorted = new Gee.TreeSet<string>();
476                 foreach (string i in dependencies)
477                         sorted.add(i);
478
479                 bool first = true;
480                 string r;
481
482                 if (prefix == null)
483                         r = "";
484                 else
485                         r = prefix;
486
487                 foreach (string i in sorted) {
488                         if (r != "")
489                                 r += first ? "\n" : ",";
490
491                         if (first) {
492                                 r += "<b>" + word + ":</b>";
493                                 first = false;
494                         }
495
496                         r += format_unit_link(i);
497                 }
498
499                 return r;
500         }
501
502         public void show_unit(Unit unit) {
503                 current_unit_id = unit.id;
504
505                 string id_display = current_unit_id;
506                 bool has_alias = false;
507                 foreach (string i in unit.names) {
508                         if (i == current_unit_id)
509                                 continue;
510
511                         if (!has_alias) {
512                                 id_display += " (aliases:";
513                                 has_alias = true;
514                         }
515
516                         id_display += " " + i;
517                 }
518                 if(has_alias)
519                         id_display += ")";
520
521                 unit_id_label.set_text_or_na(id_display);
522
523                 string[]
524                         requires = unit.requires,
525                         requires_overridable = unit.requires_overridable,
526                         requisite = unit.requisite,
527                         requisite_overridable = unit.requisite_overridable,
528                         wants = unit.wants,
529                         required_by = unit.required_by,
530                         required_by_overridable = unit.required_by_overridable,
531                         wanted_by = unit.wanted_by,
532                         conflicts = unit.conflicts,
533                         before = unit.before,
534                         after = unit.after;
535
536                 unit_dependency_label.set_markup_or_na(
537                                 make_dependency_string(
538                                 make_dependency_string(
539                                 make_dependency_string(
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(null,
548                                 "requires", requires),
549                                 "overridable requires", requires_overridable),
550                                 "requisite", requisite),
551                                 "overridable requisite", requisite_overridable),
552                                 "wants", wants),
553                                 "conflicts", conflicts),
554                                 "required by", required_by),
555                                 "overridable required by", required_by_overridable),
556                                 "wanted by", wanted_by),
557                                 "after", after),
558                                 "before", before));
559
560                 unit_description_label.set_text_or_na(unit.description);
561                 unit_load_state_label.set_text_or_na(unit.load_state);
562                 unit_active_state_label.set_text_or_na(unit.active_state);
563                 unit_sub_state_label.set_text_or_na(unit.sub_state);
564
565                 string fp = unit.fragment_path;
566                 if (fp != "")
567                         unit_fragment_path_label.set_markup_or_na("<a href=\"file://" + fp +"\">" + fp + "</a>" );
568                 else
569                         unit_fragment_path_label.set_text_or_na();
570
571
572                 unit_active_enter_timestamp_label.set_text_or_na(format_time(unit.active_enter_timestamp));
573
574                 unit_active_exit_timestamp_label.set_text_or_na(format_time(unit.active_exit_timestamp));
575
576                 bool b = unit.can_start;
577                 start_button.set_sensitive(b);
578                 stop_button.set_sensitive(b);
579                 restart_button.set_sensitive(b);
580                 unit_can_start_label.set_text_or_na(b ? "Yes" : "No");
581
582                 b = unit.can_reload;
583                 reload_button.set_sensitive(b);
584                 unit_can_reload_label.set_text_or_na(b ? "Yes" : "No");
585
586                 unit_cgroup_label.set_text_or_na(unit.default_control_group);
587         }
588
589         public Job? get_current_job() {
590                 TreePath p;
591                 job_view.get_cursor(out p, null);
592
593                 if (p == null)
594                         return null;
595
596                 TreeIter iter;
597                 TreeModel model = job_view.get_model();
598                 Job *j;
599
600                 model.get_iter(out iter, p);
601                 model.get(iter, 4, out j);
602
603                 return j;
604         }
605
606         public void job_changed() {
607                 Job j = get_current_job();
608
609                 if (j == null)
610                         clear_job();
611                 else
612                         show_job(j);
613         }
614
615         public void clear_job() {
616                 current_job_id = 0;
617
618                 job_id_label.set_text_or_na();
619                 job_state_label.set_text_or_na();
620                 job_type_label.set_text_or_na();
621
622                 cancel_button.set_sensitive(false);
623         }
624
625         public void show_job(Job job) {
626                 current_job_id = job.id;
627
628                 job_id_label.set_text_or_na("%u".printf(current_job_id));
629                 job_state_label.set_text_or_na(job.state);
630                 job_type_label.set_text_or_na(job.job_type);
631
632                 cancel_button.set_sensitive(true);
633         }
634
635         public void on_start() {
636                 Unit u = get_current_unit();
637
638                 if (u == null)
639                         return;
640
641                 try {
642                         u.start("replace");
643                 } catch (Error e) {
644                         show_error(e.message);
645                 }
646         }
647
648         public void on_stop() {
649                 Unit u = get_current_unit();
650
651                 if (u == null)
652                         return;
653
654                 try {
655                         u.stop("replace");
656                 } catch (Error e) {
657                         show_error(e.message);
658                 }
659         }
660
661         public void on_reload() {
662                 Unit u = get_current_unit();
663
664                 if (u == null)
665                         return;
666
667                 try {
668                         u.reload("replace");
669                 } catch (Error e) {
670                         show_error(e.message);
671                 }
672         }
673
674         public void on_restart() {
675                 Unit u = get_current_unit();
676
677                 if (u == null)
678                         return;
679
680                 try {
681                         u.restart("replace");
682                 } catch (Error e) {
683                         show_error(e.message);
684                 }
685         }
686
687         public void on_cancel() {
688                 Job j = get_current_job();
689
690                 if (j == null)
691                         return;
692
693                 try {
694                         j.cancel();
695                 } catch (Error e) {
696                         show_error(e.message);
697                 }
698         }
699
700         public void update_unit_iter(TreeIter iter, string id, Unit u) {
701
702                 try  {
703                         string t = "";
704                         Unit.JobLink jl = u.job;
705
706                         if (jl.id != 0) {
707                                 Job j = Bus.get_proxy_sync(
708                                                 user ? BusType.SESSION : BusType.SYSTEM,
709                                                 "org.freedesktop.systemd1",
710                                                 jl.path);
711
712                                 t = j.job_type;
713                         }
714
715                         unit_model.set(iter,
716                                        0, id,
717                                        1, u.description,
718                                        2, u.load_state,
719                                        3, u.active_state,
720                                        4, u.sub_state,
721                                        5, t != "" ? "→ %s".printf(t) : "",
722                                        6, u);
723                 } catch (Error e) {
724                         show_error(e.message);
725                 }
726         }
727
728         public void on_unit_new(string id, ObjectPath path) {
729                 try {
730
731                         Properties p = Bus.get_proxy_sync(
732                                         user ? BusType.SESSION : BusType.SYSTEM,
733                                         "org.freedesktop.systemd1",
734                                         path);
735
736                         p.properties_changed.connect(on_unit_changed);
737
738                         TreeIter iter;
739                         unit_model.append(out iter);
740
741                         Unit u = Bus.get_proxy_sync(
742                                         user ? BusType.SESSION : BusType.SYSTEM,
743                                         "org.freedesktop.systemd1",
744                                         path);
745
746                         unit_map[id] = u;
747
748                         update_unit_iter(iter, id, u);
749                 } catch (Error e) {
750                         show_error(e.message);
751                 }
752         }
753
754         public void update_job_iter(TreeIter iter, uint32 id, Job j) {
755                 job_model.set(iter,
756                               0, "%u".printf(id),
757                               1, j.unit.id,
758                               2, "→ %s".printf(j.job_type),
759                               3, j.state,
760                               4, j,
761                               5, id);
762         }
763
764         public void on_job_new(uint32 id, ObjectPath path) {
765
766                 try  {
767
768                         Properties p = Bus.get_proxy_sync(
769                                         user ? BusType.SESSION : BusType.SYSTEM,
770                                         "org.freedesktop.systemd1",
771                                         path);
772
773                         p.properties_changed.connect(on_job_changed);
774
775                         TreeIter iter;
776                         job_model.append(out iter);
777
778                         Job j = Bus.get_proxy_sync(
779                                         user ? BusType.SESSION : BusType.SYSTEM,
780                                         "org.freedesktop.systemd1",
781                                         path);
782
783                         update_job_iter(iter, id, j);
784
785                 } catch (Error e) {
786                         show_error(e.message);
787                 }
788         }
789
790         public void on_unit_removed(string id, ObjectPath path) {
791                 TreeIter iter;
792                 if (!(unit_model.get_iter_first(out iter)))
793                         return;
794
795                 do {
796                         string name;
797
798                         unit_model.get(iter, 0, out name);
799
800                         if (id == name) {
801                                 if (current_unit_id == name)
802                                         clear_unit();
803
804                                 unit_model.remove(iter);
805                                 break;
806                         }
807
808                 } while (unit_model.iter_next(ref iter));
809
810                 unit_map.unset(id);
811         }
812
813         public void on_job_removed(uint32 id, ObjectPath path, string res) {
814                 TreeIter iter;
815                 if (!(job_model.get_iter_first(out iter)))
816                         return;
817
818                 do {
819                         uint32 j;
820
821                         job_model.get(iter, 5, out j);
822
823                         if (id == j) {
824                                 if (current_job_id == j)
825                                         clear_job();
826
827                                 job_model.remove(iter);
828
829                                 break;
830                         }
831
832                 } while (job_model.iter_next(ref iter));
833         }
834
835         public void on_unit_changed(Properties p, string iface, HashTable<string, Value?> changed_properties, string[] invalidated_properties) {
836
837                 try {
838                         TreeIter iter;
839                         string id;
840
841                         Unit u = Bus.get_proxy_sync(
842                                         user ? BusType.SESSION : BusType.SYSTEM,
843                                         p.get_name(),
844                                         p.get_object_path());
845
846                         if (!(unit_model.get_iter_first(out iter)))
847                                 return;
848
849                         id = u.id;
850
851                         do {
852                                 string name;
853
854                                 unit_model.get(iter, 0, out name);
855
856                                 if (id == name) {
857                                         update_unit_iter(iter, id, u);
858
859                                         if (current_unit_id == id)
860                                                 show_unit(u);
861
862                                         break;
863                                 }
864
865                         } while (unit_model.iter_next(ref iter));
866
867                 } catch (Error e) {
868                         show_error(e.message);
869                 }
870         }
871
872         public void on_job_changed(Properties p, string iface, HashTable<string, Value?> changed_properties, string[] invalidated_properties) {
873                 try {
874                         TreeIter iter;
875                         uint32 id;
876
877                         Job j = Bus.get_proxy_sync(
878                                         user ? BusType.SESSION : BusType.SYSTEM,
879                                         p.get_name(),
880                                         p.get_object_path());
881
882                         if (!(job_model.get_iter_first(out iter)))
883                                 return;
884
885                         id = j.id;
886
887                         do {
888                                 uint32 k;
889
890                                 job_model.get(iter, 5, out k);
891
892                                 if (id == k) {
893                                         update_job_iter(iter, id, j);
894
895                                         if (current_job_id == id)
896                                                 show_job(j);
897
898                                         break;
899                                 }
900
901                         } while (job_model.iter_next(ref iter));
902
903                 } catch (Error e) {
904                         show_error(e.message);
905                 }
906         }
907
908         public bool unit_filter(TreeModel model, TreeIter iter) {
909                 string id, active_state, job;
910
911                 model.get(iter, 0, out id, 3, out active_state, 5, out job);
912
913                 if (id == null)
914                         return false;
915
916                 if (!inactive_checkbox.get_active()
917                     && active_state == "inactive" && job == "")
918                         return false;
919
920                 switch (unit_type_combo_box.get_active()) {
921                 case 0:
922                         return true;
923                 case 1:
924                         return id.has_suffix(".target");
925                 case 2:
926                         return id.has_suffix(".service");
927                 case 3:
928                         return id.has_suffix(".device");
929                 case 4:
930                         return id.has_suffix(".mount");
931                 case 5:
932                         return id.has_suffix(".automount");
933                 case 6:
934                         return id.has_suffix(".swap");
935                 case 7:
936                         return id.has_suffix(".socket");
937                 case 8:
938                         return id.has_suffix(".path");
939                 case 9:
940                         return id.has_suffix(".timer");
941                 case 10:
942                         return id.has_suffix(".snapshot");
943                 default:
944                         assert(false);
945                         return false;
946                 }
947         }
948
949         public void unit_type_changed() {
950                 TreeModelFilter model = (TreeModelFilter) ((TreeModelSort) unit_view.get_model()).get_model();
951
952                 model.refilter();
953         }
954
955         public void on_server_reload() {
956                 try {
957                         manager.reload();
958                 } catch (Error e) {
959                         show_error(e.message);
960                 }
961         }
962
963         public void on_server_snapshot() {
964                 try {
965                         manager.create_snapshot();
966
967                         if (unit_type_combo_box.get_active() != 0)
968                                 unit_type_combo_box.set_active(8);
969
970                 } catch (Error e) {
971                         show_error(e.message);
972                 }
973         }
974
975         public void on_unit_load() {
976                 string t = unit_load_entry.get_text();
977
978                 if (t == "")
979                         return;
980
981                 try {
982                         var path = manager.load_unit(t);
983
984                         Unit u = Bus.get_proxy_sync(
985                                         user ? BusType.SESSION : BusType.SYSTEM,
986                                         "org.freedesktop.systemd1",
987                                         path);
988
989                         var m = new MessageDialog(this,
990                                                   DialogFlags.DESTROY_WITH_PARENT,
991                                                   MessageType.INFO,
992                                                   ButtonsType.CLOSE,
993                                                   "Unit available as id %s", u.id);
994                         m.title = "Unit";
995                         m.run();
996                         m.destroy();
997
998                         show_unit(u);
999                 } catch (Error e) {
1000                         show_error(e.message);
1001                 }
1002         }
1003
1004         public void on_unit_load_entry_changed() {
1005                 unit_load_button.set_sensitive(unit_load_entry.get_text() != "");
1006         }
1007
1008         public bool on_activate_link(string uri) {
1009
1010                 try {
1011                         string path = manager.get_unit(uri);
1012
1013                         Unit u = Bus.get_proxy_sync(
1014                                         user ? BusType.SESSION : BusType.SYSTEM,
1015                                         "org.freedesktop.systemd1",
1016                                         path);
1017
1018                         show_unit(u);
1019                 } catch (Error e) {
1020                         show_error(e.message);
1021                 }
1022
1023                 return true;
1024         }
1025
1026         public void show_error(string e) {
1027                 var m = new MessageDialog(this,
1028                                           DialogFlags.DESTROY_WITH_PARENT,
1029                                           MessageType.ERROR,
1030                                           ButtonsType.CLOSE, "%s", e);
1031                 m.title = "Error";
1032                 m.run();
1033                 m.destroy();
1034         }
1035
1036 }
1037
1038 static const OptionEntry entries[] = {
1039         { "user",    0,   0,                   OptionArg.NONE, out user, "Connect to user service manager", null },
1040         { "system",  0,   OptionFlags.REVERSE, OptionArg.NONE, out user, "Connect to system manager",       null },
1041         { null }
1042 };
1043
1044 void show_error(string e) {
1045         var m = new MessageDialog(null, 0, MessageType.ERROR, ButtonsType.CLOSE, "%s", e);
1046         m.run();
1047         m.destroy();
1048 }
1049
1050 int main(string[] args) {
1051
1052         try {
1053                 Gtk.init_with_args(ref args, "[OPTION...]", entries, "systemadm");
1054
1055                 MainWindow window = new MainWindow();
1056                 window.show_all();
1057
1058                 Gtk.main();
1059         } catch (IOError e) {
1060                 show_error(e.message);
1061         } catch (GLib.Error e) {
1062                 stderr.printf("%s\n", e.message);
1063         }
1064
1065         return 0;
1066 }