chiark / gitweb /
client: add a very basic Vala command line tool
[elogind.git] / systemctl.vala
1 using DBus;
2 using GLib;
3
4 [DBus (name = "org.freedesktop.systemd1")]
5 public interface Manager : DBus.Object {
6
7         public struct UnitInfo {
8                 string id;
9                 string description;
10                 string load_state;
11                 string active_state;
12                 ObjectPath unit_path;
13                 uint32 job_id;
14                 string job_type;
15                 ObjectPath job_path;
16         }
17
18         public struct JobInfo {
19                 uint32 id;
20                 string name;
21                 string type;
22                 string state;
23                 ObjectPath job_path;
24                 ObjectPath unit_path;
25         }
26
27         public abstract UnitInfo[] ListUnits() throws DBus.Error;
28         public abstract JobInfo[] ListJobs() throws DBus.Error;
29
30         public abstract ObjectPath LoadUnit(string name) throws DBus.Error;
31 }
32
33 static string type = null;
34 static bool all = false;
35
36 public static int job_info_compare(void* key1, void* key2) {
37         Manager.JobInfo *j1 = (Manager.JobInfo*) key1;
38         Manager.JobInfo *j2 = (Manager.JobInfo*) key2;
39
40         return Posix.strcmp(j1->name, j2->name);
41 }
42
43 public static int unit_info_compare(void* key1, void* key2) {
44         Manager.UnitInfo *u1 = (Manager.UnitInfo*) key1;
45         Manager.UnitInfo *u2 = (Manager.UnitInfo*) key2;
46
47         int r = Posix.strcmp(Posix.strrchr(u1->id, '.'), Posix.strrchr(u2->id, '.'));
48         if (r != 0)
49                 return r;
50
51         return Posix.strcmp(u1->id, u2->id);
52 }
53
54 static const OptionEntry entries[] = {
55         { "type", 't', 0, OptionArg.STRING, out type, "List only particular type of units", "TYPE" },
56         { "all",  'a', 0, OptionArg.NONE,   out all,  "Show all units, including dead ones", null  },
57         { null }
58 };
59
60 int main (string[] args) {
61
62         OptionContext context = new OptionContext(" -- Control systemd");
63         context.add_main_entries(entries, null);
64
65         try {
66                 context.parse(ref args);
67         } catch (GLib.OptionError e) {
68                 message("Failed to parse command line: %s".printf(e.message));
69         }
70
71         try {
72                 Connection bus = Bus.get(BusType.SESSION);
73
74                 Manager manager = bus.get_object (
75                                 "org.freedesktop.systemd1",
76                                 "/org/freedesktop/systemd1",
77                                 "org.freedesktop.systemd1") as Manager;
78
79                 if (args[1] == "list-units" || args.length <= 1) {
80                         var list = manager.ListUnits();
81                         uint n = 0;
82                         Posix.qsort(list, list.length, sizeof(Manager.UnitInfo), unit_info_compare);
83
84                         stdout.printf("%-45s %-6s %-12s → %-15s\n\n", "UNIT", "LOAD", "ACTIVE", "JOB");
85
86                         foreach (var i in list) {
87
88                                 if (type != null && !i.id.has_suffix(".%s".printf(type)))
89                                         continue;
90
91                                 if (!all && i.active_state == "inactive")
92                                         continue;
93
94                                 stdout.printf("%-45s %-6s %-12s", i.id, i.load_state, i.active_state);
95
96                                 if (i.job_id != 0)
97                                         stdout.printf("→ %-15s", i.job_type);
98
99                                 stdout.puts("\n");
100                                 n++;
101                         }
102
103                         if (all)
104                                 stdout.printf("\n%u units listed.\n", n);
105                         else
106                                 stdout.printf("\n%u live units listed. Pass --all to see dead units, too.\n", n);
107
108
109                 } else if (args[1] == "list-jobs") {
110                         var list = manager.ListJobs();
111                         Posix.qsort(list, list.length, sizeof(Manager.JobInfo), job_info_compare);
112
113                         foreach (var i in list)
114                                 stdout.printf("%-45s → %-15s %-7s\n", i.name, i.type, i.state);
115
116                 } else if (args[1] == "load") {
117
118                         if (args.length < 3) {
119                                 stderr.printf("Missing argument.\n");
120                                 return 1;
121                         }
122
123                         manager.LoadUnit(args[2]);
124                 } else {
125                         stderr.printf("Unknown command %s.\n", args[1]);
126                         return 1;
127                 }
128
129         } catch (DBus.Error e) {
130                 stderr.printf("%s\n".printf(e.message));
131         }
132
133         return 0;
134 }