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