chiark / gitweb /
update fixme
[elogind.git] / src / 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 static bool session = false;
27 static bool block = false;
28 static Connection? bus = null;
29 static List<ObjectPath> jobs = null;
30 static MainLoop main_loop = null;
31 static int exit_code = 0;
32
33 public static int job_info_compare(void* key1, void* key2) {
34         Manager.JobInfo *j1 = (Manager.JobInfo*) key1;
35         Manager.JobInfo *j2 = (Manager.JobInfo*) key2;
36
37         return j1->id < j2->id ? -1 : (j1->id > j2->id ? 1 : 0);
38 }
39
40 public static int unit_info_compare(void* key1, void* key2) {
41         Manager.UnitInfo *u1 = (Manager.UnitInfo*) key1;
42         Manager.UnitInfo *u2 = (Manager.UnitInfo*) key2;
43
44         int r = Posix.strcmp(Posix.strrchr(u1->id, '.'), Posix.strrchr(u2->id, '.'));
45         if (r != 0)
46                 return r;
47
48         return Posix.strcmp(u1->id, u2->id);
49 }
50
51 public void monitor_on_unit_changed(Unit u) {
52         stdout.printf("Unit %s changed.\n", u.id);
53 }
54
55 public void monitor_on_unit_new(string id, ObjectPath path) {
56         stdout.printf("Unit %s added.\n", id);
57
58         Unit u = bus.get_object(
59                         "org.freedesktop.systemd1",
60                         path,
61                         "org.freedesktop.systemd1.Unit") as Unit;
62
63         u.changed += monitor_on_unit_changed;
64
65         /* FIXME: We leak memory here */
66         u.ref();
67 }
68
69 public void monitor_on_job_changed(Job j) {
70         stdout.printf("Job %u changed.\n", j.id);
71 }
72
73 public void monitor_on_job_new(uint32 id, ObjectPath path) {
74         stdout.printf("Job %u added.\n", id);
75
76         Job j = bus.get_object(
77                         "org.freedesktop.systemd1",
78                         path,
79                         "org.freedesktop.systemd1.Job") as Job;
80
81         j.changed += monitor_on_job_changed;
82
83         /* FIXME: We leak memory here */
84         j.ref();
85 }
86
87 public void monitor_on_unit_removed(string id, ObjectPath path) {
88         stdout.printf("Unit %s removed.\n", id);
89 }
90
91 public void monitor_on_job_removed(uint32 id, ObjectPath path, bool success) {
92         stdout.printf("Job %u removed (success=%i).\n", id, (int) success);
93 }
94
95 public void block_on_job_removed(uint32 id, ObjectPath path, bool success) {
96
97         for (unowned List<ObjectPath> i = jobs; i != null; i = i.next)
98                 if (i.data == path) {
99                         jobs.remove_link(i);
100                         break;
101                 }
102
103         if (jobs == null) {
104                 if (!success)
105                         exit_code = 1;
106
107                 main_loop.quit();
108         }
109 }
110
111 static const OptionEntry entries[] = {
112         { "type",    't', 0,                   OptionArg.STRING, out type,    "List only particular type of units", "TYPE" },
113         { "all",     'a', 0,                   OptionArg.NONE,   out all,     "Show all units, including dead ones", null  },
114         { "replace", 0,   0,                   OptionArg.NONE,   out replace, "When installing a new job, replace existing conflicting ones", null },
115         { "session", 0,   0,                   OptionArg.NONE,   out session, "Connect to session bus", null },
116         { "system",  0,   OptionFlags.REVERSE, OptionArg.NONE,   out session, "Connect to system bus", null },
117         { "block",   0,   0,                   OptionArg.NONE,   out block,   "Wait until the operation finished", null },
118         { null }
119 };
120
121 int main (string[] args) {
122         OptionContext context = new OptionContext("[COMMAND [ARGUMENT...]]");
123         context.add_main_entries(entries, null);
124         context.set_description(
125                         "Commands:\n" +
126                         "  list-units                      List units\n" +
127                         "  list-jobs                       List jobs\n" +
128                         "  clear-jobs                      Cancel all jobs\n" +
129                         "  load [NAME...]                  Load one or more units\n" +
130                         "  cancel [JOB...]                 Cancel one or more jobs\n" +
131                         "  start [NAME...]                 Start one or more units\n" +
132                         "  stop [NAME...]                  Stop one or more units\n" +
133                         "  restart [NAME...]               Restart one or more units\n" +
134                         "  reload [NAME...]                Reload one or more units\n" +
135                         "  isolate [NAME]                  Start one unit and stop all others\n" +
136                         "  monitor                         Monitor unit/job changes\n" +
137                         "  dump                            Dump server status\n" +
138                         "  snapshot [NAME]                 Create a snapshot\n" +
139                         "  daemon-reload                   Reload daemon configuration\n" +
140                         "  daemon-reexecute                Reexecute daemon\n" +
141                         "  daemon-exit                     Ask the daemon to quit\n" +
142                         "  show-environment                Dump environment\n" +
143                         "  set-environment [NAME=VALUE...] Set one or more environment variables\n" +
144                         "  unset-environment [NAME...]     Unset one or more environment variables\n");
145
146         try {
147                 context.parse(ref args);
148         } catch (GLib.OptionError e) {
149                 message("Failed to parse command line: %s".printf(e.message));
150         }
151
152         try {
153                 bus = Bus.get(session ? BusType.SESSION : BusType.SYSTEM);
154
155                 Manager manager = bus.get_object (
156                                 "org.freedesktop.systemd1",
157                                 "/org/freedesktop/systemd1",
158                                 "org.freedesktop.systemd1.Manager") as Manager;
159
160                 if (args[1] == "list-units" || args.length <= 1) {
161                         var list = manager.list_units();
162                         uint n = 0;
163                         Posix.qsort(list, list.length, sizeof(Manager.UnitInfo), unit_info_compare);
164
165                         stdout.printf("%-45s %-6s %-12s %-12s %-17s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB");
166
167                         foreach (var i in list) {
168
169                                 if (type != null && !i.id.has_suffix(".%s".printf(type)))
170                                         continue;
171
172                                 if (!all && i.active_state == "inactive")
173                                         continue;
174
175                                 stdout.printf("%-45s %-6s %-12s %-12s", i.id, i.load_state, i.active_state, i.sub_state);
176
177                                 if (i.job_id != 0)
178                                         stdout.printf(" -> %-15s", i.job_type);
179
180                                 stdout.puts("\n");
181                                 n++;
182                         }
183
184                         if (all)
185                                 stdout.printf("\n%u units listed.\n", n);
186                         else
187                                 stdout.printf("\n%u live units listed. Pass --all to see dead units, too.\n", n);
188
189
190                 } else if (args[1] == "list-jobs") {
191                         var list = manager.list_jobs();
192                         Posix.qsort(list, list.length, sizeof(Manager.JobInfo), job_info_compare);
193
194                         stdout.printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
195
196                         foreach (var i in list)
197                                 stdout.printf("%4u %-45s → %-15s %-7s\n", i.id, i.name, i.type, i.state);
198
199                         stdout.printf("\n%u jobs listed.\n", list.length);
200
201                 } else if (args[1] == "clear-jobs") {
202
203                         manager.clear_jobs();
204
205                 } else if (args[1] == "load") {
206
207                         if (args.length < 3) {
208                                 stderr.printf("Missing argument.\n");
209                                 return 1;
210                         }
211
212                         for (int i = 2; i < args.length; i++)
213                                 manager.load_unit(args[i]);
214
215                 } else if (args[1] == "cancel") {
216
217                         if (args.length < 3) {
218                                 stderr.printf("Missing argument.\n");
219                                 return 1;
220                         }
221
222                         for (int i = 2; i < args.length; i++) {
223                                 uint32 id;
224
225                                 if (args[i].scanf("%u", out id) != 1) {
226                                         stderr.printf("Failed to parse argument.\n");
227                                         return 1;
228                                 }
229
230                                 ObjectPath p = manager.get_job(id);
231
232                                 Job j = bus.get_object (
233                                                 "org.freedesktop.systemd1",
234                                                 p,
235                                                 "org.freedesktop.systemd1.Job") as Job;
236
237                                 j.cancel();
238                         }
239
240                 } else if (args[1] == "start" ||
241                            args[1] == "stop" ||
242                            args[1] == "reload" ||
243                            args[1] == "restart") {
244
245                         if (args.length < 3) {
246                                 stderr.printf("Missing argument.\n");
247                                 return 1;
248                         }
249
250                         if (block)
251                                 manager.subscribe();
252
253                         for (int i = 2; i < args.length; i++) {
254
255                                 string mode = replace ? "replace" : "fail";
256                                 ObjectPath j = null;
257
258                                 if (args[1] == "start")
259                                         j = manager.start_unit(args[i], mode);
260                                 else if (args[1] == "stop")
261                                         j = manager.stop_unit(args[i], mode);
262                                 else if (args[1] == "restart")
263                                         j = manager.restart_unit(args[i], mode);
264                                 else if (args[1] == "reload")
265                                         j = manager.reload_unit(args[i], mode);
266
267                                 if (block)
268                                         jobs.append(j);
269                         }
270
271                 } else if (args[1] == "isolate") {
272
273                         if (args.length != 3) {
274                                 stderr.printf("Missing argument.\n");
275                                 return 1;
276                         }
277
278                         ObjectPath j = manager.start_unit(args[2], "isolate");
279
280                         if (block) {
281                                 manager.subscribe();
282                                 jobs.append(j);
283                         }
284
285                 } else if (args[1] == "monitor") {
286
287                         manager.subscribe();
288
289                         var unit_list = manager.list_units();
290
291                         foreach (var i in unit_list) {
292                                 monitor_on_unit_new(i.id, i.unit_path);
293
294                                 if (i.job_id != 0)
295                                         monitor_on_job_new(i.job_id, i.job_path);
296                         }
297
298                         manager.unit_new += monitor_on_unit_new;
299                         manager.unit_removed += monitor_on_unit_removed;
300                         manager.job_new += monitor_on_job_new;
301                         manager.job_removed += monitor_on_job_removed;
302
303                         main_loop = new MainLoop();
304                         main_loop.run();
305
306                 } else if (args[1] == "dump")
307                         stdout.puts(manager.dump());
308
309                 else if (args[1] == "snapshot") {
310
311                         ObjectPath p = manager.create_snapshot(args.length > 2 ? args[2] : "");
312
313                         Unit u = bus.get_object(
314                                         "org.freedesktop.systemd1",
315                                         p,
316                                         "org.freedesktop.systemd1.Unit") as Unit;
317
318                         stdout.printf("%s\n", u.id);
319
320                 } else if (args[1] == "daemon-reload")
321                         manager.reload();
322
323                 else if (args[1] == "daemon-reexecute" || args[1] == "daemon-reexec")
324                         manager.reexecute();
325
326                 else if (args[1] == "daemon-exit")
327                         manager.exit();
328
329                 else if (args[1] == "show-environment") {
330                         foreach(var x in manager.environment)
331                                 stderr.printf("%s\n", x);
332
333                 } else if (args[1] == "set-environment")
334                         manager.set_environment(args[2:args.length]);
335
336                 else if (args[1] == "unset-environment")
337                         manager.unset_environment(args[2:args.length]);
338
339                 else {
340                         stderr.printf("Unknown command %s.\n", args[1]);
341                         return 1;
342                 }
343
344                 if (jobs != null && block) {
345                         manager.job_removed += block_on_job_removed;
346
347                         main_loop = new MainLoop();
348                         main_loop.run();
349                 }
350
351         } catch (DBus.Error e) {
352                 stderr.printf("%s\n".printf(e.message));
353                 return 1;
354         }
355
356         return exit_code;
357 }