2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
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.
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.
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/>.
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;
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;
37 return j1->id < j2->id ? -1 : (j1->id > j2->id ? 1 : 0);
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;
44 int r = Posix.strcmp(Posix.strrchr(u1->id, '.'), Posix.strrchr(u2->id, '.'));
48 return Posix.strcmp(u1->id, u2->id);
51 public void monitor_on_unit_changed(Unit u) {
52 stdout.printf("Unit %s changed.\n", u.id);
55 public void monitor_on_unit_new(string id, ObjectPath path) {
56 stdout.printf("Unit %s added.\n", id);
58 Unit u = bus.get_object(
59 "org.freedesktop.systemd1",
61 "org.freedesktop.systemd1.Unit") as Unit;
63 u.changed += monitor_on_unit_changed;
65 /* FIXME: We leak memory here */
69 public void monitor_on_job_changed(Job j) {
70 stdout.printf("Job %u changed.\n", j.id);
73 public void monitor_on_job_new(uint32 id, ObjectPath path) {
74 stdout.printf("Job %u added.\n", id);
76 Job j = bus.get_object(
77 "org.freedesktop.systemd1",
79 "org.freedesktop.systemd1.Job") as Job;
81 j.changed += monitor_on_job_changed;
83 /* FIXME: We leak memory here */
87 public void monitor_on_unit_removed(string id, ObjectPath path) {
88 stdout.printf("Unit %s removed.\n", id);
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);
95 public void block_on_job_removed(uint32 id, ObjectPath path, bool success) {
97 for (unowned List<ObjectPath> i = jobs; i != null; i = i.next)
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 },
121 int main (string[] args) {
122 OptionContext context = new OptionContext("[COMMAND [ARGUMENT...]]");
123 context.add_main_entries(entries, null);
124 context.set_description(
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");
147 context.parse(ref args);
148 } catch (GLib.OptionError e) {
149 message("Failed to parse command line: %s".printf(e.message));
153 bus = Bus.get(session ? BusType.SESSION : BusType.SYSTEM);
155 Manager manager = bus.get_object (
156 "org.freedesktop.systemd1",
157 "/org/freedesktop/systemd1",
158 "org.freedesktop.systemd1.Manager") as Manager;
160 if (args[1] == "list-units" || args.length <= 1) {
161 var list = manager.list_units();
163 Posix.qsort(list, list.length, sizeof(Manager.UnitInfo), unit_info_compare);
165 stdout.printf("%-45s %-6s %-12s %-12s %-17s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB");
167 foreach (var i in list) {
169 if (type != null && !i.id.has_suffix(".%s".printf(type)))
172 if (!all && i.active_state == "inactive")
175 stdout.printf("%-45s %-6s %-12s %-12s", i.id, i.load_state, i.active_state, i.sub_state);
178 stdout.printf(" -> %-15s", i.job_type);
185 stdout.printf("\n%u units listed.\n", n);
187 stdout.printf("\n%u live units listed. Pass --all to see dead units, too.\n", n);
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);
194 stdout.printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
196 foreach (var i in list)
197 stdout.printf("%4u %-45s → %-15s %-7s\n", i.id, i.name, i.type, i.state);
199 stdout.printf("\n%u jobs listed.\n", list.length);
201 } else if (args[1] == "clear-jobs") {
203 manager.clear_jobs();
205 } else if (args[1] == "load") {
207 if (args.length < 3) {
208 stderr.printf("Missing argument.\n");
212 for (int i = 2; i < args.length; i++)
213 manager.load_unit(args[i]);
215 } else if (args[1] == "cancel") {
217 if (args.length < 3) {
218 stderr.printf("Missing argument.\n");
222 for (int i = 2; i < args.length; i++) {
225 if (args[i].scanf("%u", out id) != 1) {
226 stderr.printf("Failed to parse argument.\n");
230 ObjectPath p = manager.get_job(id);
232 Job j = bus.get_object (
233 "org.freedesktop.systemd1",
235 "org.freedesktop.systemd1.Job") as Job;
240 } else if (args[1] == "start" ||
242 args[1] == "reload" ||
243 args[1] == "restart") {
245 if (args.length < 3) {
246 stderr.printf("Missing argument.\n");
253 for (int i = 2; i < args.length; i++) {
255 string mode = replace ? "replace" : "fail";
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);
271 } else if (args[1] == "isolate") {
273 if (args.length != 3) {
274 stderr.printf("Missing argument.\n");
278 ObjectPath j = manager.start_unit(args[2], "isolate");
285 } else if (args[1] == "monitor") {
289 var unit_list = manager.list_units();
291 foreach (var i in unit_list) {
292 monitor_on_unit_new(i.id, i.unit_path);
295 monitor_on_job_new(i.job_id, i.job_path);
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;
303 main_loop = new MainLoop();
306 } else if (args[1] == "dump")
307 stdout.puts(manager.dump());
309 else if (args[1] == "snapshot") {
311 ObjectPath p = manager.create_snapshot(args.length > 2 ? args[2] : "");
313 Unit u = bus.get_object(
314 "org.freedesktop.systemd1",
316 "org.freedesktop.systemd1.Unit") as Unit;
318 stdout.printf("%s\n", u.id);
320 } else if (args[1] == "daemon-reload")
323 else if (args[1] == "daemon-reexecute" || args[1] == "daemon-reexec")
326 else if (args[1] == "daemon-exit")
329 else if (args[1] == "show-environment") {
330 foreach(var x in manager.environment)
331 stderr.printf("%s\n", x);
333 } else if (args[1] == "set-environment")
334 manager.set_environment(args[2:args.length]);
336 else if (args[1] == "unset-environment")
337 manager.unset_environment(args[2:args.length]);
340 stderr.printf("Unknown command %s.\n", args[1]);
344 if (jobs != null && block) {
345 manager.job_removed += block_on_job_removed;
347 main_loop = new MainLoop();
351 } catch (DBus.Error e) {
352 stderr.printf("%s\n".printf(e.message));