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