summaryrefslogtreecommitdiff
path: root/systemadm.vala
blob: c761511c1c52f32a0c67f2f071716ed5675e9d35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/***
  This file is part of systemd.

  Copyright 2010 Lennart Poettering

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

using Gtk;
using GLib;
using DBus;
using Pango;

static bool session = false;

public class LeftLabel : Label {
        public LeftLabel(string? text = null) {
                if (text != null)
                        set_markup("<b>%s</b>".printf(text));
                set_alignment(1, 0);
                set_padding(6, 0);
        }
}

public class RightLabel : Label {
        public RightLabel(string? text = null) {
                set_text_or_na(text);
                set_alignment(0, 1);
                set_ellipsize(EllipsizeMode.START);
                set_selectable(true);
        }

        public void set_text_or_na(string? text = null) {
                if (text == null || text == "")
                        set_markup("<i>n/a</i>");
                else
                        set_text(text);
        }
}

public class MainWindow : Window {

        private TreeView unit_view;
        private TreeView job_view;

        private ListStore unit_model;
        private ListStore job_model;

        private Button start_button;
        private Button stop_button;
        private Button restart_button;
        private Button reload_button;
        private Button cancel_button;

        private Connection bus;
        private Manager manager;

        private RightLabel unit_id_label;
        private RightLabel unit_description_label;
        private RightLabel unit_load_state_label;
        private RightLabel unit_active_state_label;
        private RightLabel unit_fragment_path_label;
        private RightLabel unit_active_enter_timestamp_label;
        private RightLabel unit_active_exit_timestamp_label;
        private RightLabel unit_can_start_label;
        private RightLabel unit_can_reload_label;

        private RightLabel job_id_label;
        private RightLabel job_state_label;
        private RightLabel job_type_label;

        public MainWindow() throws DBus.Error {
                title = "systemdadm";
                position = WindowPosition.CENTER;
                set_default_size(1000, 700);
                set_border_width(12);
                destroy += Gtk.main_quit;

                Notebook notebook = new Notebook();
                add(notebook);

                Box unit_vbox = new VBox(false, 6);
                notebook.append_page(unit_vbox, new Label("Units"));
                unit_vbox.set_border_width(12);

                Box job_vbox = new VBox(false, 6);
                notebook.append_page(job_vbox, new Label("Jobs"));
                job_vbox.set_border_width(12);

                unit_model = new ListStore(6, typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(Unit));
                job_model = new ListStore(5, typeof(string), typeof(string), typeof(string), typeof(string), typeof(Job));

                unit_view = new TreeView.with_model(unit_model);
                job_view = new TreeView.with_model(job_model);

                unit_view.cursor_changed += unit_changed;
                job_view.cursor_changed += job_changed;

                unit_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 0);
                unit_view.insert_column_with_attributes(-1, "Description", new CellRendererText(), "text", 1);
                unit_view.insert_column_with_attributes(-1, "Load State", new CellRendererText(), "text", 2);
                unit_view.insert_column_with_attributes(-1, "Active State", new CellRendererText(), "text", 3);
                unit_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 4);

                job_view.insert_column_with_attributes(-1, "Job", new CellRendererText(), "text", 0);
                job_view.insert_column_with_attributes(-1, "Unit", new CellRendererText(), "text", 1);
                job_view.insert_column_with_attributes(-1, "Type", new CellRendererText(), "text", 2);
                job_view.insert_column_with_attributes(-1, "State", new CellRendererText(), "text", 3);

                ScrolledWindow scroll = new ScrolledWindow(null, null);
                scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
                scroll.set_shadow_type(ShadowType.IN);
                scroll.add(unit_view);
                unit_vbox.pack_start(scroll, true, true, 0);

                scroll = new ScrolledWindow(null, null);
                scroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
                scroll.set_shadow_type(ShadowType.IN);
                scroll.add(job_view);
                job_vbox.pack_start(scroll, true, true, 0);

                unit_id_label = new RightLabel();
                unit_description_label = new RightLabel();
                unit_load_state_label = new RightLabel();
                unit_active_state_label = new RightLabel();
                unit_fragment_path_label = new RightLabel();
                unit_active_enter_timestamp_label = new RightLabel();
                unit_active_exit_timestamp_label = new RightLabel();
                unit_can_start_label = new RightLabel();
                unit_can_reload_label = new RightLabel();

                job_id_label = new RightLabel();
                job_state_label = new RightLabel();
                job_type_label = new RightLabel();

                Table unit_table = new Table(9, 2, false);
                unit_table.set_row_spacings(6);
                unit_table.set_border_width(12);
                unit_vbox.pack_start(unit_table, false, true, 0);

                Table job_table = new Table(2, 2, false);
                job_table.set_row_spacings(6);
                job_table.set_border_width(12);
                job_vbox.pack_start(job_table, false, true, 0);

                unit_table.attach(new LeftLabel("Id:"), 0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                unit_table.attach(unit_id_label, 1, 2, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                unit_table.attach(new LeftLabel("Description:"), 0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                unit_table.attach(unit_description_label, 1, 2, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                unit_table.attach(new LeftLabel("Load State:"), 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                unit_table.attach(unit_load_state_label, 1, 2, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                unit_table.attach(new LeftLabel("Active State:"), 0, 1, 3, 4, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                unit_table.attach(unit_active_state_label, 1, 2, 3, 4, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                unit_table.attach(new LeftLabel("Fragment Path:"), 0, 1, 4, 5, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                unit_table.attach(unit_fragment_path_label, 1, 2, 4, 5, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                unit_table.attach(new LeftLabel("Active Enter Timestamp:"), 0, 1, 5, 6, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                unit_table.attach(unit_active_enter_timestamp_label, 1, 2, 5, 6, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                unit_table.attach(new LeftLabel("Active Exit Timestamp:"), 0, 1, 6, 7, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                unit_table.attach(unit_active_exit_timestamp_label, 1, 2, 6, 7, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                unit_table.attach(new LeftLabel("Can Start/Stop:"), 0, 1, 7, 8, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                unit_table.attach(unit_can_start_label, 1, 2, 7, 8, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                unit_table.attach(new LeftLabel("Can Reload:"), 0, 1, 8, 9, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                unit_table.attach(unit_can_reload_label, 1, 2, 8, 9, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);

                job_table.attach(new LeftLabel("Id:"), 0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                job_table.attach(job_id_label, 1, 2, 0, 1, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                job_table.attach(new LeftLabel("State:"), 0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                job_table.attach(job_state_label, 1, 2, 1, 2, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                job_table.attach(new LeftLabel("Type:"), 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
                job_table.attach(job_type_label, 1, 2, 2, 3, AttachOptions.EXPAND|AttachOptions.FILL, AttachOptions.FILL, 0, 0);

                ButtonBox bbox = new HButtonBox();
                bbox.set_layout(ButtonBoxStyle.START);
                bbox.set_spacing(6);
                unit_vbox.pack_start(bbox, false, true, 0);

                start_button = new Button.with_mnemonic("_Start");
                stop_button = new Button.with_mnemonic("Sto_p");
                reload_button = new Button.with_mnemonic("_Reload");
                restart_button = new Button.with_mnemonic("Res_tart");

                start_button.clicked += on_start;
                stop_button.clicked += on_stop;
                reload_button.clicked += on_reload;
                restart_button.clicked += on_restart;

                bbox.pack_start(start_button, false, true, 0);
                bbox.pack_start(stop_button, false, true, 0);
                bbox.pack_start(restart_button, false, true, 0);
                bbox.pack_start(reload_button, false, true, 0);

                bbox = new HButtonBox();
                bbox.set_layout(ButtonBoxStyle.START);
                bbox.set_spacing(6);
                job_vbox.pack_start(bbox, false, true, 0);

                cancel_button = new Button.with_mnemonic("_Cancel");

                cancel_button.clicked += on_cancel;

                bbox.pack_start(cancel_button, false, true, 0);

                bus = Bus.get(session ? BusType.SESSION : BusType.SYSTEM);

                manager = bus.get_object(
                                "org.freedesktop.systemd1",
                                "/org/freedesktop/systemd1",
                                "org.freedesktop.systemd1") as Manager;

                manager.unit_new += on_unit_new;
                manager.job_new += on_job_new;
                manager.unit_removed += on_unit_removed;
                manager.job_removed += on_job_removed;

                manager.subscribe();

                clear_unit();
                populate_unit_model();
                populate_job_model();
        }

        public void populate_unit_model() throws DBus.Error {
                unit_model.clear();

                var list = manager.list_units();

                foreach (var i in list) {
                        TreeIter iter;

                        Unit u = bus.get_object(
                                        "org.freedesktop.systemd1",
                                        i.unit_path,
                                        "org.freedesktop.systemd1.Unit") as Unit;

                        unit_model.append(out iter);
                        unit_model.set(iter,
                                       0, i.id,
                                       1, i.description,
                                       2, i.load_state,
                                       3, i.active_state,
                                       4, i.job_type != "" ? "→ %s".printf(i.job_type) : "",
                                       5, u);
                }
        }

        public void populate_job_model() throws DBus.Error {
                job_model.clear();

                var list = manager.list_jobs();

                foreach (var i in list) {
                        TreeIter iter;

                        Job j = bus.get_object(
                                        "org.freedesktop.systemd1",
                                        i.job_path,
                                        "org.freedesktop.systemd1.Job") as Job;

                        job_model.append(out iter);
                        job_model.set(iter,
                                      0, "%u".printf(i.id),
                                      1, i.name,
                                      2, "→ %s".printf(i.type),
                                      3, i.state,
                                      4, j);
                }
        }

        public Unit? get_current_unit() {
                TreePath p;
                unit_view.get_cursor(out p, null);

                if (p == null)
                        return null;

                TreeIter iter;
                Unit u;

                unit_model.get_iter(out iter, p);
                unit_model.get(iter, 5, out u);

                return u;
        }

        public void unit_changed() {
                Unit u = get_current_unit();

                if (u == null)
                        clear_unit();
                else
                        show_unit(u);
        }

        public void clear_unit() {
                start_button.set_sensitive(false);
                stop_button.set_sensitive(false);
                reload_button.set_sensitive(false);
                restart_button.set_sensitive(false);

                unit_id_label.set_text_or_na();
                unit_description_label.set_text_or_na();
                unit_load_state_label.set_text_or_na();
                unit_active_state_label.set_text_or_na();
                unit_fragment_path_label.set_text_or_na();
                unit_active_enter_timestamp_label.set_text_or_na();
                unit_active_exit_timestamp_label.set_text_or_na();
                unit_can_reload_label.set_text_or_na();
                unit_can_start_label.set_text_or_na();
        }

        public void show_unit(Unit unit) {
                unit_id_label.set_text_or_na(unit.id);
                unit_description_label.set_text_or_na(unit.description);
                unit_load_state_label.set_text_or_na(unit.load_state);
                unit_active_state_label.set_text_or_na(unit.active_state);
                unit_fragment_path_label.set_text_or_na(unit.fragment_path);

                uint64 t = unit.active_enter_timestamp;
                if (t > 0) {
                        TimeVal tv = { (long) (t / 1000000), (long) (t % 1000000) };
                        unit_active_enter_timestamp_label.set_text_or_na(tv.to_iso8601());
                } else
                        unit_active_enter_timestamp_label.set_text_or_na();

                t = unit.active_exit_timestamp;
                if (t > 0) {
                        TimeVal tv = { (long) (t / 1000000), (long) (t % 1000000) };
                        unit_active_exit_timestamp_label.set_text_or_na(tv.to_iso8601());
                } else
                        unit_active_exit_timestamp_label.set_text_or_na();

                bool b = unit.can_start;
                start_button.set_sensitive(b);
                stop_button.set_sensitive(b);
                restart_button.set_sensitive(b);
                unit_can_start_label.set_text_or_na(b ? "Yes" : "No");

                b = unit.can_reload;
                reload_button.set_sensitive(b);
                unit_can_reload_label.set_text_or_na(b ? "Yes" : "No");
        }

        public Job? get_current_job() {
                TreePath p;
                job_view.get_cursor(out p, null);

                if (p == null)
                        return null;

                TreeIter iter;
                Job *j;

                job_model.get_iter(out iter, p);
                job_model.get(iter, 4, out j);

                return j;
        }

        public void job_changed() {
                Job j = get_current_job();

                if (j == null)
                        clear_job();
                else
                        show_job(j);
        }

        public void clear_job() {
                job_id_label.set_text_or_na();
                job_state_label.set_text_or_na();
                job_type_label.set_text_or_na();
        }

        public void show_job(Job job) {
                job_id_label.set_text_or_na("%u".printf(job.id));
                job_state_label.set_text_or_na(job.state);
                job_type_label.set_text_or_na(job.job_type);
        }

        public void on_start() {
                Unit u = get_current_unit();

                if (u == null)
                        return;

                try {
                        u.start("replace");
                } catch (DBus.Error e) {
                        message("%s", e.message);
                }
        }

        public void on_stop() {
                Unit u = get_current_unit();

                if (u == null)
                        return;

                try {
                        u.stop("replace");
                } catch (DBus.Error e) {
                        message("%s", e.message);
                }
        }

        public void on_reload() {
                Unit u = get_current_unit();

                if (u == null)
                        return;

                try {
                        u.reload("replace");
                } catch (DBus.Error e) {
                        message("%s", e.message);
                }
        }

        public void on_restart() {
                Unit u = get_current_unit();

                if (u == null)
                        return;

                try {
                        u.restart("replace");
                } catch (DBus.Error e) {
                        message("%s", e.message);
                }
        }

        public void on_cancel() {
                Job j = get_current_job();

                if (j == null)
                        return;

                try {
                        j.cancel();
                } catch (DBus.Error e) {
                        message("%s", e.message);
                }
        }

        public void on_unit_new(string id, ObjectPath path) {
                stderr.printf("new path %s", path);

                Unit u = bus.get_object(
                                "org.freedesktop.systemd1",
                                path,
                                "org.freedesktop.systemd1.Unit") as Unit;

                string t = "";
                Unit.JobLink jl = u.job;

                if (jl.id != 0) {
                        Job j = bus.get_object(
                                        "org.freedesktop.systemd1",
                                        jl.path,
                                        "org.freedesktop.systemd1.Job") as Job;

                        t = j.job_type;
                }

                TreeIter iter;
                unit_model.append(out iter);
                unit_model.set(iter,
                               0, u.id,
                               1, u.description,
                               2, u.load_state,
                               3, u.active_state,
                               4, t != "" ? "→ %s".printf(t) : "",
                               5, u);
        }

        public void on_job_new(uint32 id, ObjectPath path) {
                stderr.printf("new path %s", path);

                Job j = bus.get_object(
                                "org.freedesktop.systemd1",
                                path,
                                "org.freedesktop.systemd1.Job") as Job;

                TreeIter iter;
                job_model.append(out iter);
                job_model.set(iter,
                              0, "%u".printf(j.id),
                              1, j.unit.id,
                              2, "→ %s".printf(j.job_type),
                              3, j.state,
                              4, j);
        }

        public void on_unit_removed(string id, ObjectPath path) {
                stdout.printf("Unit %s removed.\n", id);

                /* FIXME */
        }

        public void on_job_removed(uint32 id, ObjectPath path) {
                stdout.printf("Job %u removed.\n", id);

                /* FIXME */
        }
}

static const OptionEntry entries[] = {
        { "session", 0,   0,                   OptionArg.NONE,   out session, "Connect to session bus", null },
        { "system",  0,   OptionFlags.REVERSE, OptionArg.NONE,   out session, "Connect to system bus", null },
        { null }
};

int main (string[] args) {

        try {
                Gtk.init_with_args(ref args, "[OPTION...]", entries, "systemadm");

                MainWindow window = new MainWindow();
                window.show_all();
        } catch (DBus.Error e) {
                message("%s", e.message);
        } catch (GLib.Error e) {
                message("%s", e.message);
        }

        Gtk.main();
        return 0;
}