chiark / gitweb /
mount: make sure network mounts are ordered after network.target
[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 class LeftLabel : Label {
27         public LeftLabel(string? text = null) {
28                 if (text != null)
29                         set_markup("<b>%s</b>".printf(text));
30                 set_alignment(0, 0);
31                 set_padding(6, 0);
32         }
33 }
34
35 public class RightLabel : Label {
36         public RightLabel(string? text = null) {
37                 set_text_or_na(text);
38                 set_alignment(0, 0);
39                 set_ellipsize(EllipsizeMode.START);
40                 set_selectable(true);
41         }
42
43         public void set_text_or_na(string? text = null) {
44                 if (text == null || text == "")
45                         set_markup("<i>n/a</i>");
46                 else
47                         set_text(text);
48         }
49
50         public void set_markup_or_na(string? text = null) {
51                 if (text == null || text == "")
52                         set_markup("<i>n/a</i>");
53                 else
54                         set_markup(text);
55         }
56 }
57
58 public class MainWindow : Window {
59
60         private string? current_unit_id;
61         private uint32 current_job_id;
62
63         private TreeView unit_view;
64         private TreeView job_view;
65
66         private ListStore unit_model;
67         private ListStore job_model;
68
69         private Button start_button;
70         private Button stop_button;
71         private Button restart_button;
72         private Button reload_button;
73         private Button cancel_button;
74
75         private Entry unit_load_entry;
76         private Button unit_load_button;
77
78         private Button server_snapshot_button;
79         private Button server_reload_button;
80
81         private Manager manager;
82
83         private RightLabel unit_id_label;
84         private RightLabel unit_aliases_label;
85         private RightLabel unit_dependency_label;
86         private RightLabel unit_description_label;
87         private RightLabel unit_load_state_label;
88         private RightLabel unit_active_state_label;
89         private RightLabel unit_sub_state_label;
90         private RightLabel unit_fragment_path_label;
91         private RightLabel unit_active_enter_timestamp_label;
92         private RightLabel unit_active_exit_timestamp_label;
93         private RightLabel unit_can_start_label;
94         private RightLabel unit_can_reload_label;
95         private RightLabel unit_cgroup_label;
96
97         private RightLabel job_id_label;
98         private RightLabel job_state_label;
99         private RightLabel job_type_label;
100
101         private ComboBox unit_type_combo_box;
102
103         public MainWindow() throws IOError {
104                 title = user ? "systemd User Service Manager" : "systemd System Manager";
105                 set_position(WindowPosition.CENTER);
106                 set_default_size(1000, 700);
107                 set_border_width(12);
108                 destroy.connect(Gtk.main_quit);
109
110                 Notebook notebook = new Notebook();
111                 add(notebook);
112
113                 Box unit_vbox = new VBox(false, 12);
114                 notebook.append_page(unit_vbox, new Label("Units"));
115                 unit_vbox.set_border_width(12);
116
117                 Box job_vbox = new VBox(false, 12);
118                 notebook.append_page(job_vbox, new Label("Jobs"));
119                 job_vbox.set_border_width(12);
120
121                 unit_type_combo_box = new ComboBox.text();
122                 Box type_hbox = new HBox(false, 6);
123                 type_hbox.pack_start(unit_type_combo_box, false, false, 0);
124                 unit_vbox.pack_start(type_hbox, false, false, 0);
125
126                 unit_type_combo_box.append_text("Show All Units");
127                 unit_type_combo_box.append_text("Show Only Live Units");
128                 unit_type_combo_box.append_text("Services");
129                 unit_type_combo_box.append_text("Sockets");
130                 unit_type_combo_box.append_text("Devices");
131                 unit_type_combo_box.append_text("Mounts");
132                 unit_type_combo_box.append_text("Automounts");
133                 unit_type_combo_box.append_text("Targets");
134                 unit_type_combo_box.append_text("Snapshots");
135                 unit_type_combo_box.set_active(1);
136                 unit_type_combo_box.changed.connect(unit_type_changed);
137
138                 unit_load_entry = new Entry();
139                 unit_load_button = new Button.with_mnemonic("_Load");
140                 unit_load_button.set_sensitive(false);
141
142                 unit_load_entry.changed.connect(on_unit_load_entry_changed);
143                 unit_load_entry.activate.connect(on_unit_load);
144                 unit_load_button.clicked.connect(on_unit_load);
145
146                 Box unit_load_hbox = new HBox(false, 6);
147                 unit_load_hbox.pack_start(unit_load_entry, false, true, 0);
148                 unit_load_hbox.pack_start(unit_load_button, false, true, 0);
149
150                 server_snapshot_button = new Button.with_mnemonic("Take S_napshot");
151                 server_reload_button = new Button.with_mnemonic("Reload _Configuration");
152
153                 server_snapshot_button.clicked.connect(on_server_snapshot);
154                 server_reload_button.clicked.connect(on_server_reload);
155
156                 type_hbox.pack_end(server_snapshot_button, false, true, 0);
157                 type_hbox.pack_end(server_reload_button, false, true, 0);
158                 type_hbox.pack_end(unit_load_hbox, false, true, 24);
159
160                 unit_model = new ListStore(7, typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(Unit));
161                 job_model = new ListStore(6, typeof(string), typeof(string), typeof(string), typeof(string), typeof(Job), typeof(uint32));
162
163                 TreeModelFilter unit_model_filter;
164                 unit_model_filter = new TreeModelFilter(unit_model, null);
165                 unit_model_filter.set_visible_func(unit_filter);
166
167                 unit_view = new TreeView.with_model(unit_model_filter);
168                 job_view = new TreeView.with_model(job_model);
169
170                 unit_view.cursor_changed.connect(unit_changed);
171                 job_view.cursor_changed.connect(job_changed);
172
173                 unit_view.insert_column_with_attributes(-1, "Load State", new CellRendererText(), "text", 2);
174                 unit_view.insert_column_with_attributes(-1, "Active State", new CellRendererText(), "text", 3);
175                 unit_view.insert_column_with_attributes(-1, "Unit State", new CellRendererText(), "text", 4);
176                 unit_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 0);
177                 unit_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 5);
178
179                 job_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 0);
180                 job_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 1);
181                 job_view.insert_column_with_attributes(-1, "Type", new CellRendererText(), "text", 2);
182                 job_view.insert_column_with_attributes(-1, "State", new CellRendererText(), "text", 3);
183
184                 ScrolledWindow scroll = new ScrolledWindow(null, null);
185                 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
186                 scroll.set_shadow_type(ShadowType.IN);
187                 scroll.add(unit_view);
188                 unit_vbox.pack_start(scroll, true, true, 0);
189
190                 scroll = new ScrolledWindow(null, null);
191                 scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
192                 scroll.set_shadow_type(ShadowType.IN);
193                 scroll.add(job_view);
194                 job_vbox.pack_start(scroll, true, true, 0);
195
196                 unit_id_label = new RightLabel();
197                 unit_aliases_label = new RightLabel();
198                 unit_dependency_label = new RightLabel();
199                 unit_description_label = new RightLabel();
200                 unit_load_state_label = new RightLabel();
201                 unit_active_state_label = new RightLabel();
202                 unit_sub_state_label = new RightLabel();
203                 unit_fragment_path_label = new RightLabel();
204                 unit_active_enter_timestamp_label = new RightLabel();
205                 unit_active_exit_timestamp_label = new RightLabel();
206                 unit_can_start_label = new RightLabel();
207                 unit_can_reload_label = new RightLabel();
208                 unit_cgroup_label = new RightLabel();
209
210                 job_id_label = new RightLabel();
211                 job_state_label = new RightLabel();
212                 job_type_label = new RightLabel();
213
214                 unit_dependency_label.set_track_visited_links(false);
215                 unit_dependency_label.set_selectable(false);
216                 unit_dependency_label.activate_link.connect(on_activate_link);
217
218                 unit_fragment_path_label.set_track_visited_links(false);
219
220                 Table unit_table = new Table(8, 6, false);
221                 unit_table.set_row_spacings(6);
222                 unit_table.set_border_width(0);
223                 unit_vbox.pack_start(unit_table, false, true, 0);
224
225                 Table job_table = new Table(2, 2, false);
226                 job_table.set_row_spacings(6);
227                 job_table.set_border_width(0);
228                 job_vbox.pack_start(job_table, false, true, 0);
229
230                 unit_table.attach(new LeftLabel("Id:"),                     0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
231                 unit_table.attach(unit_id_label,                            1, 6, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
232                 unit_table.attach(new LeftLabel("Aliases:"),                0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
233                 unit_table.attach(unit_aliases_label,                       1, 6, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
234                 unit_table.attach(new LeftLabel("Description:"),            0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
235                 unit_table.attach(unit_description_label,                   1, 6, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
236                 unit_table.attach(new LeftLabel("Dependencies:"),           0, 1, 3, 4, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
237                 unit_table.attach(unit_dependency_label,                    1, 6, 3, 4, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
238                 unit_table.attach(new LeftLabel("Fragment Path:"),          0, 1, 4, 5, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
239                 unit_table.attach(unit_fragment_path_label,                 1, 6, 4, 5, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
240                 unit_table.attach(new LeftLabel("Control Group:"),          0, 1, 5, 6, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
241                 unit_table.attach(unit_cgroup_label,                        1, 6, 5, 6, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
242
243                 unit_table.attach(new LeftLabel("Load State:"),             0, 1, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
244                 unit_table.attach(unit_load_state_label,                    1, 2, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
245                 unit_table.attach(new LeftLabel("Active State:"),           0, 1, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
246                 unit_table.attach(unit_active_state_label,                  1, 2, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
247                 unit_table.attach(new LeftLabel("Unit State:"),             0, 1, 8, 9, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
248                 unit_table.attach(unit_sub_state_label,                     1, 2, 8, 9, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
249
250                 unit_table.attach(new LeftLabel("Active Enter Timestamp:"), 2, 3, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
251                 unit_table.attach(unit_active_enter_timestamp_label,        3, 4, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
252                 unit_table.attach(new LeftLabel("Active Exit Timestamp:"),  2, 3, 8, 9, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
253                 unit_table.attach(unit_active_exit_timestamp_label,         3, 4, 8, 9, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
254
255                 unit_table.attach(new LeftLabel("Can Start/Stop:"),         4, 5, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
256                 unit_table.attach(unit_can_start_label,                     5, 6, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
257                 unit_table.attach(new LeftLabel("Can Reload:"),             4, 5, 8, 9, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
258                 unit_table.attach(unit_can_reload_label,                    5, 6, 8, 9, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
259
260                 job_table.attach(new LeftLabel("Id:"),                      0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
261                 job_table.attach(job_id_label,                              1, 2, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
262                 job_table.attach(new LeftLabel("State:"),                   0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
263                 job_table.attach(job_state_label,                           1, 2, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
264                 job_table.attach(new LeftLabel("Type:"),                    0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
265                 job_table.attach(job_type_label,                            1, 2, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
266
267                 ButtonBox bbox = new HButtonBox();
268                 bbox.set_layout(ButtonBoxStyle.START);
269                 bbox.set_spacing(6);
270                 unit_vbox.pack_start(bbox, false, true, 0);
271
272                 start_button = new Button.with_mnemonic("_Start");
273                 stop_button = new Button.with_mnemonic("Sto_p");
274                 reload_button = new Button.with_mnemonic("_Reload");
275                 restart_button = new Button.with_mnemonic("Res_tart");
276
277                 start_button.clicked.connect(on_start);
278                 stop_button.clicked.connect(on_stop);
279                 reload_button.clicked.connect(on_reload);
280                 restart_button.clicked.connect(on_restart);
281
282                 bbox.pack_start(start_button, false, true, 0);
283                 bbox.pack_start(stop_button, false, true, 0);
284                 bbox.pack_start(restart_button, false, true, 0);
285                 bbox.pack_start(reload_button, false, true, 0);
286
287                 bbox = new HButtonBox();
288                 bbox.set_layout(ButtonBoxStyle.START);
289                 bbox.set_spacing(6);
290                 job_vbox.pack_start(bbox, false, true, 0);
291
292                 cancel_button = new Button.with_mnemonic("_Cancel");
293
294                 cancel_button.clicked.connect(on_cancel);
295
296                 bbox.pack_start(cancel_button, false, true, 0);
297
298                 manager = Bus.get_proxy_sync(
299                                 user ? BusType.SESSION : BusType.SYSTEM,
300                                 "org.freedesktop.systemd1",
301                                 "/org/freedesktop/systemd1");
302
303                 manager.unit_new.connect(on_unit_new);
304                 manager.job_new.connect(on_job_new);
305                 manager.unit_removed.connect(on_unit_removed);
306                 manager.job_removed.connect(on_job_removed);
307
308                 manager.subscribe();
309
310                 clear_unit();
311                 clear_job();
312                 populate_unit_model();
313                 populate_job_model();
314         }
315
316         public void populate_unit_model() throws IOError {
317                 unit_model.clear();
318
319                 var list = manager.list_units();
320
321                 foreach (var i in list) {
322                         TreeIter iter;
323
324                         Properties p = Bus.get_proxy_sync(
325                                         user ? BusType.SESSION : BusType.SYSTEM,
326                                         "org.freedesktop.systemd1",
327                                         i.unit_path);
328
329                         p.properties_changed.connect(on_unit_changed);
330
331                         Unit u = Bus.get_proxy_sync(
332                                         user ? BusType.SESSION : BusType.SYSTEM,
333                                         "org.freedesktop.systemd1",
334                                         i.unit_path);
335
336                         unit_model.append(out iter);
337                         unit_model.set(iter,
338                                        0, i.id,
339                                        1, i.description,
340                                        2, i.load_state,
341                                        3, i.active_state,
342                                        4, i.sub_state,
343                                        5, i.job_type != "" ? "→ %s".printf(i.job_type) : "",
344                                        6, u);
345                 }
346         }
347
348         public void populate_job_model() throws IOError {
349                 job_model.clear();
350
351                 var list = manager.list_jobs();
352
353                 foreach (var i in list) {
354                         TreeIter iter;
355
356                         Properties p = Bus.get_proxy_sync(
357                                         user ? BusType.SESSION : BusType.SYSTEM,
358                                         "org.freedesktop.systemd1",
359                                         i.job_path);
360
361                         p.properties_changed.connect(on_job_changed);
362
363                         Job j = Bus.get_proxy_sync(
364                                         user ? BusType.SESSION : BusType.SYSTEM,
365                                         "org.freedesktop.systemd1",
366                                         i.job_path);
367
368                         job_model.append(out iter);
369                         job_model.set(iter,
370                                       0, "%u".printf(i.id),
371                                       1, i.name,
372                                       2, "→ %s".printf(i.type),
373                                       3, i.state,
374                                       4, j,
375                                       5, i.id);
376                 }
377         }
378
379         public Unit? get_current_unit() {
380                 TreePath p;
381                 unit_view.get_cursor(out p, null);
382
383                 if (p == null)
384                         return null;
385
386                 TreeModel model = unit_view.get_model();
387                 TreeIter iter;
388                 Unit u;
389
390                 model.get_iter(out iter, p);
391                 model.get(iter, 6, out u);
392
393                 return u;
394         }
395
396         public void unit_changed() {
397                 Unit u = get_current_unit();
398
399                 if (u == null)
400                         clear_unit();
401                 else
402                         show_unit(u);
403         }
404
405         public void clear_unit() {
406                 current_unit_id = null;
407
408                 start_button.set_sensitive(false);
409                 stop_button.set_sensitive(false);
410                 reload_button.set_sensitive(false);
411                 restart_button.set_sensitive(false);
412
413                 unit_id_label.set_text_or_na();
414                 unit_aliases_label.set_text_or_na();
415                 unit_description_label.set_text_or_na();
416                 unit_description_label.set_text_or_na();
417                 unit_load_state_label.set_text_or_na();
418                 unit_active_state_label.set_text_or_na();
419                 unit_sub_state_label.set_text_or_na();
420                 unit_fragment_path_label.set_text_or_na();
421                 unit_active_enter_timestamp_label.set_text_or_na();
422                 unit_active_exit_timestamp_label.set_text_or_na();
423                 unit_can_reload_label.set_text_or_na();
424                 unit_can_start_label.set_text_or_na();
425                 unit_cgroup_label.set_text_or_na();
426         }
427
428         public string make_dependency_string(string? prefix, string word, string[] dependencies) {
429                 bool first = true;
430                 string r;
431
432                 if (prefix == null)
433                         r = "";
434                 else
435                         r = prefix;
436
437                 foreach (string i in dependencies) {
438                         if (r != "")
439                                 r += first ? "\n" : ",";
440
441                         if (first) {
442                                 r += word;
443                                 first = false;
444                         }
445
446                         r += " <a href=\"" + i + "\">" + i + "</a>";
447                 }
448
449                 return r;
450         }
451
452         public void show_unit(Unit unit) {
453                 current_unit_id = unit.id;
454
455                 unit_id_label.set_text_or_na(current_unit_id);
456
457                 string a = "";
458                 foreach (string i in unit.names) {
459                         if (i == current_unit_id)
460                                 continue;
461
462                         if (a == "")
463                                 a = i;
464                         else
465                                 a += "\n" + i;
466                 }
467
468                 unit_aliases_label.set_text_or_na(a);
469
470                 string[]
471                         requires = unit.requires,
472                         requires_overridable = unit.requires_overridable,
473                         requisite = unit.requisite,
474                         requisite_overridable = unit.requisite_overridable,
475                         wants = unit.wants,
476                         required_by = unit.required_by,
477                         required_by_overridable = unit.required_by_overridable,
478                         wanted_by = unit.wanted_by,
479                         conflicts = unit.conflicts,
480                         before = unit.before,
481                         after = unit.after;
482
483                 unit_dependency_label.set_markup_or_na(
484                                 make_dependency_string(
485                                 make_dependency_string(
486                                 make_dependency_string(
487                                 make_dependency_string(
488                                 make_dependency_string(
489                                 make_dependency_string(
490                                 make_dependency_string(
491                                 make_dependency_string(
492                                 make_dependency_string(
493                                 make_dependency_string(
494                                 make_dependency_string(null,
495                                 "requires", requires),
496                                 "overridable requires", requires_overridable),
497                                 "requisite", requisite),
498                                 "overridable requisite", requisite_overridable),
499                                 "wants", wants),
500                                 "conflicts", conflicts),
501                                 "required by", required_by),
502                                 "overridable required by", required_by_overridable),
503                                 "wanted by", wanted_by),
504                                 "after", after),
505                                 "before", before));
506
507                 unit_description_label.set_text_or_na(unit.description);
508                 unit_load_state_label.set_text_or_na(unit.load_state);
509                 unit_active_state_label.set_text_or_na(unit.active_state);
510                 unit_sub_state_label.set_text_or_na(unit.sub_state);
511
512                 string fp = unit.fragment_path;
513                 if (fp != "")
514                         unit_fragment_path_label.set_markup_or_na("<a href=\"file://" + fp +"\">" + fp + "</a>" );
515                 else
516                         unit_fragment_path_label.set_text_or_na();
517
518                 uint64 t = unit.active_enter_timestamp;
519                 if (t > 0) {
520                         Time timestamp = Time.local((time_t) (t / 1000000));
521                         unit_active_enter_timestamp_label.set_text_or_na(timestamp.format("%a, %d %b %Y %H:%M:%S %z"));
522                 } else
523                         unit_active_enter_timestamp_label.set_text_or_na();
524
525                 t = unit.active_exit_timestamp;
526                 if (t > 0) {
527                         Time timestamp = Time.local((time_t) (t / 1000000));
528                         unit_active_exit_timestamp_label.set_text_or_na(timestamp.format("%a, %d %b %Y %H:%M:%S %z"));
529                 } else
530                         unit_active_exit_timestamp_label.set_text_or_na();
531
532                 bool b = unit.can_start;
533                 start_button.set_sensitive(b);
534                 stop_button.set_sensitive(b);
535                 restart_button.set_sensitive(b);
536                 unit_can_start_label.set_text_or_na(b ? "Yes" : "No");
537
538                 b = unit.can_reload;
539                 reload_button.set_sensitive(b);
540                 unit_can_reload_label.set_text_or_na(b ? "Yes" : "No");
541
542                 unit_cgroup_label.set_text_or_na(unit.default_control_group);
543         }
544
545         public Job? get_current_job() {
546                 TreePath p;
547                 job_view.get_cursor(out p, null);
548
549                 if (p == null)
550                         return null;
551
552                 TreeIter iter;
553                 TreeModel model = job_view.get_model();
554                 Job *j;
555
556                 model.get_iter(out iter, p);
557                 model.get(iter, 4, out j);
558
559                 return j;
560         }
561
562         public void job_changed() {
563                 Job j = get_current_job();
564
565                 if (j == null)
566                         clear_job();
567                 else
568                         show_job(j);
569         }
570
571         public void clear_job() {
572                 current_job_id = 0;
573
574                 job_id_label.set_text_or_na();
575                 job_state_label.set_text_or_na();
576                 job_type_label.set_text_or_na();
577
578                 cancel_button.set_sensitive(false);
579         }
580
581         public void show_job(Job job) {
582                 current_job_id = job.id;
583
584                 job_id_label.set_text_or_na("%u".printf(current_job_id));
585                 job_state_label.set_text_or_na(job.state);
586                 job_type_label.set_text_or_na(job.job_type);
587
588                 cancel_button.set_sensitive(true);
589         }
590
591         public void on_start() {
592                 Unit u = get_current_unit();
593
594                 if (u == null)
595                         return;
596
597                 try {
598                         u.start("replace");
599                 } catch (IOError e) {
600                         show_error(e.message);
601                 }
602         }
603
604         public void on_stop() {
605                 Unit u = get_current_unit();
606
607                 if (u == null)
608                         return;
609
610                 try {
611                         u.stop("replace");
612                 } catch (IOError e) {
613                         show_error(e.message);
614                 }
615         }
616
617         public void on_reload() {
618                 Unit u = get_current_unit();
619
620                 if (u == null)
621                         return;
622
623                 try {
624                         u.reload("replace");
625                 } catch (IOError e) {
626                         show_error(e.message);
627                 }
628         }
629
630         public void on_restart() {
631                 Unit u = get_current_unit();
632
633                 if (u == null)
634                         return;
635
636                 try {
637                         u.restart("replace");
638                 } catch (IOError e) {
639                         show_error(e.message);
640                 }
641         }
642
643         public void on_cancel() {
644                 Job j = get_current_job();
645
646                 if (j == null)
647                         return;
648
649                 try {
650                         j.cancel();
651                 } catch (IOError e) {
652                         show_error(e.message);
653                 }
654         }
655
656         public void update_unit_iter(TreeIter iter, string id, Unit u) {
657
658                 try  {
659                         string t = "";
660                         Unit.JobLink jl = u.job;
661
662                         if (jl.id != 0) {
663                                 Job j = Bus.get_proxy_sync(
664                                                 user ? BusType.SESSION : BusType.SYSTEM,
665                                                 "org.freedesktop.systemd1",
666                                                 jl.path);
667
668                                 t = j.job_type;
669                         }
670
671                         unit_model.set(iter,
672                                        0, id,
673                                        1, u.description,
674                                        2, u.load_state,
675                                        3, u.active_state,
676                                        4, u.sub_state,
677                                        5, t != "" ? "→ %s".printf(t) : "",
678                                        6, u);
679                 } catch (IOError e) {
680                         show_error(e.message);
681                 }
682         }
683
684         public void on_unit_new(string id, ObjectPath path) {
685                 try {
686
687                         Properties p = Bus.get_proxy_sync(
688                                         user ? BusType.SESSION : BusType.SYSTEM,
689                                         "org.freedesktop.systemd1",
690                                         path);
691
692                         p.properties_changed.connect(on_unit_changed);
693
694                         TreeIter iter;
695                         unit_model.append(out iter);
696
697                         Unit u = Bus.get_proxy_sync(
698                                         user ? BusType.SESSION : BusType.SYSTEM,
699                                         "org.freedesktop.systemd1",
700                                         path);
701
702                         update_unit_iter(iter, id, u);
703                 } catch (IOError e) {
704                         show_error(e.message);
705                 }
706         }
707
708         public void update_job_iter(TreeIter iter, uint32 id, Job j) {
709                 job_model.set(iter,
710                               0, "%u".printf(id),
711                               1, j.unit.id,
712                               2, "→ %s".printf(j.job_type),
713                               3, j.state,
714                               4, j,
715                               5, id);
716         }
717
718         public void on_job_new(uint32 id, ObjectPath path) {
719
720                 try  {
721
722                         Properties p = Bus.get_proxy_sync(
723                                         user ? BusType.SESSION : BusType.SYSTEM,
724                                         "org.freedesktop.systemd1",
725                                         path);
726
727                         p.properties_changed.connect(on_job_changed);
728
729                         TreeIter iter;
730                         job_model.append(out iter);
731
732                         Job j = Bus.get_proxy_sync(
733                                         user ? BusType.SESSION : BusType.SYSTEM,
734                                         "org.freedesktop.systemd1",
735                                         path);
736
737                         update_job_iter(iter, id, j);
738
739                 } catch (IOError e) {
740                         show_error(e.message);
741                 }
742         }
743
744         public void on_unit_removed(string id, ObjectPath path) {
745                 TreeIter iter;
746                 if (!(unit_model.get_iter_first(out iter)))
747                         return;
748
749                 do {
750                         string name;
751
752                         unit_model.get(iter, 0, out name);
753
754                         if (id == name) {
755                                 if (current_unit_id == name)
756                                         clear_unit();
757
758                                 unit_model.remove(iter);
759                                 break;
760                         }
761
762                 } while (unit_model.iter_next(ref iter));
763         }
764
765         public void on_job_removed(uint32 id, ObjectPath path, bool success) {
766                 TreeIter iter;
767                 if (!(job_model.get_iter_first(out iter)))
768                         return;
769
770                 do {
771                         uint32 j;
772
773                         job_model.get(iter, 5, out j);
774
775                         if (id == j) {
776                                 if (current_job_id == j)
777                                         clear_job();
778
779                                 job_model.remove(iter);
780
781                                 break;
782                         }
783
784                 } while (job_model.iter_next(ref iter));
785         }
786
787         public void on_unit_changed(Properties p, string iface, HashTable<string, Value?> changed_properties, string[] invalidated_properties) {
788
789                 try {
790                         TreeIter iter;
791                         string id;
792
793                         Unit u = Bus.get_proxy_sync(
794                                         user ? BusType.SESSION : BusType.SYSTEM,
795                                         p.get_name(),
796                                         p.get_object_path());
797
798                         if (!(unit_model.get_iter_first(out iter)))
799                                 return;
800
801                         id = u.id;
802
803                         do {
804                                 string name;
805
806                                 unit_model.get(iter, 0, out name);
807
808                                 if (id == name) {
809                                         update_unit_iter(iter, id, u);
810
811                                         if (current_unit_id == id)
812                                                 show_unit(u);
813
814                                         break;
815                                 }
816
817                         } while (unit_model.iter_next(ref iter));
818
819                 } catch (IOError e) {
820                         show_error(e.message);
821                 }
822         }
823
824         public void on_job_changed(Properties p, string iface, HashTable<string, Value?> changed_properties, string[] invalidated_properties) {
825                 try {
826                         TreeIter iter;
827                         uint32 id;
828
829                         Job j = Bus.get_proxy_sync(
830                                         user ? BusType.SESSION : BusType.SYSTEM,
831                                         p.get_name(),
832                                         p.get_object_path());
833
834                         if (!(job_model.get_iter_first(out iter)))
835                                 return;
836
837                         id = j.id;
838
839                         do {
840                                 uint32 k;
841
842                                 job_model.get(iter, 5, out k);
843
844                                 if (id == k) {
845                                         update_job_iter(iter, id, j);
846
847                                         if (current_job_id == id)
848                                                 show_job(j);
849
850                                         break;
851                                 }
852
853                         } while (job_model.iter_next(ref iter));
854
855                 } catch (IOError e) {
856                         show_error(e.message);
857                 }
858         }
859
860         public bool unit_filter(TreeModel model, TreeIter iter) {
861                 string id, active_state, job;
862
863                 model.get(iter, 0, out id, 3, out active_state, 5, out job);
864
865                 if (id == null)
866                         return false;
867
868                 switch (unit_type_combo_box.get_active()) {
869
870                         case 0:
871                                 return true;
872
873                         case 1:
874                                 return active_state != "inactive" || job != "";
875
876                         case 2:
877                                 return id.has_suffix(".service");
878
879                         case 3:
880                                 return id.has_suffix(".socket");
881
882                         case 4:
883                                 return id.has_suffix(".device");
884
885                         case 5:
886                                 return id.has_suffix(".mount");
887
888                         case 6:
889                                 return id.has_suffix(".automount");
890
891                         case 7:
892                                 return id.has_suffix(".target");
893
894                         case 8:
895                                 return id.has_suffix(".snapshot");
896                 }
897
898                 return false;
899         }
900
901         public void unit_type_changed() {
902                 TreeModelFilter model = (TreeModelFilter) unit_view.get_model();
903
904                 model.refilter();
905         }
906
907         public void on_server_reload() {
908                 try {
909                         manager.reload();
910                 } catch (IOError e) {
911                         show_error(e.message);
912                 }
913         }
914
915         public void on_server_snapshot() {
916                 try {
917                         manager.create_snapshot();
918
919                         if (unit_type_combo_box.get_active() != 0)
920                                 unit_type_combo_box.set_active(8);
921
922                 } catch (IOError e) {
923                         show_error(e.message);
924                 }
925         }
926
927         public void on_unit_load() {
928                 string t = unit_load_entry.get_text();
929
930                 if (t == "")
931                         return;
932
933                 try {
934                         var path = manager.load_unit(t);
935
936                         Unit u = Bus.get_proxy_sync(
937                                         user ? BusType.SESSION : BusType.SYSTEM,
938                                         "org.freedesktop.systemd1",
939                                         path);
940
941                         var m = new MessageDialog(this,
942                                                   DialogFlags.DESTROY_WITH_PARENT,
943                                                   MessageType.INFO,
944                                                   ButtonsType.CLOSE,
945                                                   "Unit available as id %s", u.id);
946                         m.title = "Unit";
947                         m.run();
948                         m.destroy();
949
950                         show_unit(u);
951                 } catch (IOError e) {
952                         show_error(e.message);
953                 }
954         }
955
956         public void on_unit_load_entry_changed() {
957                 unit_load_button.set_sensitive(unit_load_entry.get_text() != "");
958         }
959
960         public bool on_activate_link(string uri) {
961
962                 try {
963                         string path = manager.get_unit(uri);
964
965                         Unit u = Bus.get_proxy_sync(
966                                         user ? BusType.SESSION : BusType.SYSTEM,
967                                         "org.freedesktop.systemd1",
968                                         path);
969
970                         show_unit(u);
971                 } catch (IOError e) {
972                         show_error(e.message);
973                 }
974
975                 return true;
976         }
977
978         public void show_error(string e) {
979                 var m = new MessageDialog(this,
980                                           DialogFlags.DESTROY_WITH_PARENT,
981                                           MessageType.ERROR,
982                                           ButtonsType.CLOSE, "%s", e);
983                 m.title = "Error";
984                 m.run();
985                 m.destroy();
986         }
987
988 }
989
990 static const OptionEntry entries[] = {
991         { "user",    0,   0,                   OptionArg.NONE, out user, "Connect to user service manager", null },
992         { "system",  0,   OptionFlags.REVERSE, OptionArg.NONE, out user, "Connect to system manager",       null },
993         { null }
994 };
995
996 void show_error(string e) {
997         var m = new MessageDialog(null, 0, MessageType.ERROR, ButtonsType.CLOSE, "%s", e);
998         m.run();
999         m.destroy();
1000 }
1001
1002 int main(string[] args) {
1003
1004         try {
1005                 Gtk.init_with_args(ref args, "[OPTION...]", entries, "systemadm");
1006
1007                 MainWindow window = new MainWindow();
1008                 window.show_all();
1009
1010                 Gtk.main();
1011         } catch (IOError e) {
1012                 show_error(e.message);
1013         } catch (GLib.Error e) {
1014                 show_error(e.message);
1015         }
1016
1017         return 0;
1018 }