chiark / gitweb /
5ffcecae2993feacce3c7544acb30b18f7664440
[elogind.git] / systemctl.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 DBus;
21 using GLib;
22
23 static string type = null;
24 static bool all = false;
25 static bool replace = false;
26
27 public static int job_info_compare(void* key1, void* key2) {
28         Manager.JobInfo *j1 = (Manager.JobInfo*) key1;
29         Manager.JobInfo *j2 = (Manager.JobInfo*) key2;
30
31         return j1->id < j2->id ? -1 : (j1->id > j2->id ? 1 : 0);
32 }
33
34 public static int unit_info_compare(void* key1, void* key2) {
35         Manager.UnitInfo *u1 = (Manager.UnitInfo*) key1;
36         Manager.UnitInfo *u2 = (Manager.UnitInfo*) key2;
37
38         int r = Posix.strcmp(Posix.strrchr(u1->id, '.'), Posix.strrchr(u2->id, '.'));
39         if (r != 0)
40                 return r;
41
42         return Posix.strcmp(u1->id, u2->id);
43 }
44
45 static const OptionEntry entries[] = {
46         { "type",    't', 0, OptionArg.STRING, out type,    "List only particular type of units", "TYPE" },
47         { "all",     'a', 0, OptionArg.NONE,   out all,     "Show all units, including dead ones", null  },
48         { "replace", 0,   0, OptionArg.NONE,   out replace, "When installing a new job, replace existing conflicting ones.", null },
49         { null }
50 };
51
52 int main (string[] args) {
53
54         OptionContext context = new OptionContext(" -- Control systemd");
55         context.add_main_entries(entries, null);
56
57         try {
58                 context.parse(ref args);
59         } catch (GLib.OptionError e) {
60                 message("Failed to parse command line: %s".printf(e.message));
61         }
62
63         try {
64                 Connection bus = Bus.get(BusType.SESSION);
65
66                 Manager manager = bus.get_object (
67                                 "org.freedesktop.systemd1",
68                                 "/org/freedesktop/systemd1",
69                                 "org.freedesktop.systemd1") as Manager;
70
71                 if (args[1] == "list-units" || args.length <= 1) {
72                         var list = manager.list_units();
73                         uint n = 0;
74                         Posix.qsort(list, list.length, sizeof(Manager.UnitInfo), unit_info_compare);
75
76                         stdout.printf("%-45s %-6s %-12s %-17s\n", "UNIT", "LOAD", "ACTIVE", "JOB");
77
78                         foreach (var i in list) {
79
80                                 if (type != null && !i.id.has_suffix(".%s".printf(type)))
81                                         continue;
82
83                                 if (!all && i.active_state == "inactive")
84                                         continue;
85
86                                 stdout.printf("%-45s %-6s %-12s", i.id, i.load_state, i.active_state);
87
88                                 if (i.job_id != 0)
89                                         stdout.printf("→ %-15s", i.job_type);
90
91                                 stdout.puts("\n");
92                                 n++;
93                         }
94
95                         if (all)
96                                 stdout.printf("\n%u units listed.\n", n);
97                         else
98                                 stdout.printf("\n%u live units listed. Pass --all to see dead units, too.\n", n);
99
100
101                 } else if (args[1] == "list-jobs") {
102                         var list = manager.list_jobs();
103                         Posix.qsort(list, list.length, sizeof(Manager.JobInfo), job_info_compare);
104
105                         stdout.printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
106
107                         foreach (var i in list)
108                                 stdout.printf("%4u %-45s → %-15s %-7s\n", i.id, i.name, i.type, i.state);
109
110                         stdout.printf("\n%u jobs listed.\n", list.length);
111
112                 } else if (args[1] == "clear-jobs") {
113
114                         manager.clear_jobs();
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                         for (int i = 2; i < args.length; i++)
124                                 manager.load_unit(args[i]);
125
126                 } else if (args[1] == "cancel") {
127
128                         if (args.length < 3) {
129                                 stderr.printf("Missing argument.\n");
130                                 return 1;
131                         }
132
133                         for (int i = 2; i < args.length; i++) {
134                                 uint32 id;
135
136                                 if (args[i].scanf("%u", out id) != 1) {
137                                         stderr.printf("Failed to parse argument.\n");
138                                         return 1;
139                                 }
140
141                                 ObjectPath p = manager.get_job(id);
142
143                                 Job j = bus.get_object (
144                                                 "org.freedesktop.systemd1",
145                                                 p,
146                                                 "org.freedesktop.systemd1.Job") as Job;
147
148                                 j.cancel();
149                         }
150
151                 } else if (args[1] == "start" ||
152                            args[1] == "stop" ||
153                            args[1] == "reload" ||
154                            args[1] == "restart") {
155
156                         if (args.length < 3) {
157                                 stderr.printf("Missing argument.\n");
158                                 return 1;
159                         }
160
161                         for (int i = 2; i < args.length; i++) {
162
163                                 ObjectPath p = manager.get_unit(args[i]);
164
165                                 Unit u = bus.get_object(
166                                                 "org.freedesktop.systemd1",
167                                                 p,
168                                                 "org.freedesktop.systemd1.Unit") as Unit;
169
170                                 string mode = replace ? "replace" : "fail";
171
172                                 if (args[1] == "start")
173                                         u.start(mode);
174                                 else if (args[1] == "stop")
175                                         u.stop(mode);
176                                 else if (args[1] == "restart")
177                                         u.restart(mode);
178                                 else if (args[1] == "reload")
179                                         u.reload(mode);
180                         }
181
182                 } else {
183                         stderr.printf("Unknown command %s.\n", args[1]);
184                         return 1;
185                 }
186
187         } catch (DBus.Error e) {
188                 stderr.printf("%s\n".printf(e.message));
189         }
190
191         return 0;
192 }