chiark / gitweb /
systemctl: add --block switch for synchronous starting of units
[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 on or more units\n" +
132                         "  stop [NAME...]                  Stop on or more units\n" +
133                         "  enter [NAME]                    Start one unit and stop all others\n" +
134                         "  restart [NAME...]               Restart on or more units\n" +
135                         "  reload [NAME...]                Reload on or more units\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                         "  show-environment                Dump environment\n" +
142                         "  set-environment [NAME=VALUE...] Set one or more environment variables\n" +
143                         "  unset-environment [NAME...]     Unset one or more environment variables\n");
144
145         try {
146                 context.parse(ref args);
147         } catch (GLib.OptionError e) {
148                 message("Failed to parse command line: %s".printf(e.message));
149         }
150
151         try {
152                 bus = Bus.get(session ? BusType.SESSION : BusType.SYSTEM);
153
154                 Manager manager = bus.get_object (
155                                 "org.freedesktop.systemd1",
156                                 "/org/freedesktop/systemd1",
157                                 "org.freedesktop.systemd1.Manager") as Manager;
158
159                 if (args[1] == "list-units" || args.length <= 1) {
160                         var list = manager.list_units();
161                         uint n = 0;
162                         Posix.qsort(list, list.length, sizeof(Manager.UnitInfo), unit_info_compare);
163
164                         stdout.printf("%-45s %-6s %-12s %-12s %-17s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB");
165
166                         foreach (var i in list) {
167
168                                 if (type != null && !i.id.has_suffix(".%s".printf(type)))
169                                         continue;
170
171                                 if (!all && i.active_state == "inactive")
172                                         continue;
173
174                                 stdout.printf("%-45s %-6s %-12s %-12s", i.id, i.load_state, i.active_state, i.sub_state);
175
176                                 if (i.job_id != 0)
177                                         stdout.printf(" -> %-15s", i.job_type);
178
179                                 stdout.puts("\n");
180                                 n++;
181                         }
182
183                         if (all)
184                                 stdout.printf("\n%u units listed.\n", n);
185                         else
186                                 stdout.printf("\n%u live units listed. Pass --all to see dead units, too.\n", n);
187
188
189                 } else if (args[1] == "list-jobs") {
190                         var list = manager.list_jobs();
191                         Posix.qsort(list, list.length, sizeof(Manager.JobInfo), job_info_compare);
192
193                         stdout.printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
194
195                         foreach (var i in list)
196                                 stdout.printf("%4u %-45s → %-15s %-7s\n", i.id, i.name, i.type, i.state);
197
198                         stdout.printf("\n%u jobs listed.\n", list.length);
199
200                 } else if (args[1] == "clear-jobs") {
201
202                         manager.clear_jobs();
203
204                 } else if (args[1] == "load") {
205
206                         if (args.length < 3) {
207                                 stderr.printf("Missing argument.\n");
208                                 return 1;
209                         }
210
211                         for (int i = 2; i < args.length; i++)
212                                 manager.load_unit(args[i]);
213
214                 } else if (args[1] == "cancel") {
215
216                         if (args.length < 3) {
217                                 stderr.printf("Missing argument.\n");
218                                 return 1;
219                         }
220
221                         for (int i = 2; i < args.length; i++) {
222                                 uint32 id;
223
224                                 if (args[i].scanf("%u", out id) != 1) {
225                                         stderr.printf("Failed to parse argument.\n");
226                                         return 1;
227                                 }
228
229                                 ObjectPath p = manager.get_job(id);
230
231                                 Job j = bus.get_object (
232                                                 "org.freedesktop.systemd1",
233                                                 p,
234                                                 "org.freedesktop.systemd1.Job") as Job;
235
236                                 j.cancel();
237                         }
238
239                 } else if (args[1] == "start" ||
240                            args[1] == "stop" ||
241                            args[1] == "reload" ||
242                            args[1] == "restart") {
243
244                         if (args.length < 3) {
245                                 stderr.printf("Missing argument.\n");
246                                 return 1;
247                         }
248
249                         if (block)
250                                 manager.subscribe();
251
252                         for (int i = 2; i < args.length; i++) {
253
254                                 ObjectPath p = manager.load_unit(args[i]);
255
256                                 Unit u = bus.get_object(
257                                                 "org.freedesktop.systemd1",
258                                                 p,
259                                                 "org.freedesktop.systemd1.Unit") as Unit;
260
261                                 string mode = replace ? "replace" : "fail";
262
263                                 ObjectPath j = null;
264
265                                 if (args[1] == "start")
266                                         j = u.start(mode);
267                                 else if (args[1] == "stop")
268                                         j = u.stop(mode);
269                                 else if (args[1] == "restart")
270                                         j = u.restart(mode);
271                                 else if (args[1] == "reload")
272                                         j = u.reload(mode);
273
274                                 if (block)
275                                         jobs.append(j);
276                         }
277
278                 } else if (args[1] == "isolate") {
279
280                         if (args.length != 3) {
281                                 stderr.printf("Missing argument.\n");
282                                 return 1;
283                         }
284
285                         ObjectPath p = manager.load_unit(args[2]);
286
287                         Unit u = bus.get_object(
288                                         "org.freedesktop.systemd1",
289                                         p,
290                                         "org.freedesktop.systemd1.Unit") as Unit;
291
292                         ObjectPath j = u.start("isolate");
293
294                         if (block) {
295                                 manager.subscribe();
296                                 jobs.append(j);
297                         }
298
299                 } else if (args[1] == "monitor") {
300
301                         manager.subscribe();
302
303                         manager.unit_new += monitor_on_unit_new;
304                         manager.unit_removed += monitor_on_unit_removed;
305                         manager.job_new += monitor_on_job_new;
306                         manager.job_removed += monitor_on_job_removed;
307
308                         main_loop = new MainLoop();
309                         main_loop.run();
310
311                 } else if (args[1] == "dump")
312                         stdout.puts(manager.dump());
313
314                 else if (args[1] == "snapshot") {
315
316                         ObjectPath p = manager.create_snapshot(args.length > 2 ? args[2] : "");
317
318                         Unit u = bus.get_object(
319                                         "org.freedesktop.systemd1",
320                                         p,
321                                         "org.freedesktop.systemd1.Unit") as Unit;
322
323                         stdout.printf("%s\n", u.id);
324
325                 } else if (args[1] == "daemon-reload")
326                         manager.reload();
327
328                 else if (args[1] == "daemon-reexecute" || args[1] == "daemon-reexec")
329                         manager.reexecute();
330
331                 else if (args[1] == "daemon-exit")
332                         manager.exit();
333
334                 else if (args[1] == "show-environment") {
335                         foreach(var x in manager.environment)
336                                 stderr.printf("%s\n", x);
337
338                 } else if (args[1] == "set-environment")
339                         manager.set_environment(args[2:args.length]);
340
341                 else if (args[1] == "unset-environment")
342                         manager.unset_environment(args[2:args.length]);
343
344                 else {
345                         stderr.printf("Unknown command %s.\n", args[1]);
346                         return 1;
347                 }
348
349                 if (jobs != null && block) {
350                         manager.job_removed += block_on_job_removed;
351
352                         main_loop = new MainLoop();
353                         main_loop.run();
354                 }
355
356         } catch (DBus.Error e) {
357                 stderr.printf("%s\n".printf(e.message));
358                 return 1;
359         }
360
361         return exit_code;
362 }