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;
27 public static int job_info_compare(void* key1, void* key2) {
28 Manager.JobInfo *j1 = (Manager.JobInfo*) key1;
29 Manager.JobInfo *j2 = (Manager.JobInfo*) key2;
31 return j1->id < j2->id ? -1 : (j1->id > j2->id ? 1 : 0);
34 public static int unit_info_compare(void* key1, void* key2) {
35 Manager.UnitInfo *u1 = (Manager.UnitInfo*) key1;
36 Manager.UnitInfo *u2 = (Manager.UnitInfo*) key2;
38 int r = Posix.strcmp(Posix.strrchr(u1->id, '.'), Posix.strrchr(u2->id, '.'));
42 return Posix.strcmp(u1->id, u2->id);
45 static const OptionEntry entries[] = {
46 { "type", 't', 0, OptionArg.STRING, out type, "List only particular type of units", "TYPE" },
47 { "all", 'a', 0, OptionArg.NONE, out all, "Show all units, including dead ones", null },
48 { "replace", 0, 0, OptionArg.NONE, out replace, "When installing a new job, replace existing conflicting ones.", null },
52 int main (string[] args) {
54 OptionContext context = new OptionContext(" -- Control systemd");
55 context.add_main_entries(entries, null);
58 context.parse(ref args);
59 } catch (GLib.OptionError e) {
60 message("Failed to parse command line: %s".printf(e.message));
64 Connection bus = Bus.get(BusType.SESSION);
66 Manager manager = bus.get_object (
67 "org.freedesktop.systemd1",
68 "/org/freedesktop/systemd1",
69 "org.freedesktop.systemd1") as Manager;
71 if (args[1] == "list-units" || args.length <= 1) {
72 var list = manager.list_units();
74 Posix.qsort(list, list.length, sizeof(Manager.UnitInfo), unit_info_compare);
76 stdout.printf("%-45s %-6s %-12s %-17s\n", "UNIT", "LOAD", "ACTIVE", "JOB");
78 foreach (var i in list) {
80 if (type != null && !i.id.has_suffix(".%s".printf(type)))
83 if (!all && i.active_state == "inactive")
86 stdout.printf("%-45s %-6s %-12s", i.id, i.load_state, i.active_state);
89 stdout.printf("→ %-15s", i.job_type);
96 stdout.printf("\n%u units listed.\n", n);
98 stdout.printf("\n%u live units listed. Pass --all to see dead units, too.\n", n);
101 } else if (args[1] == "list-jobs") {
102 var list = manager.list_jobs();
103 Posix.qsort(list, list.length, sizeof(Manager.JobInfo), job_info_compare);
105 stdout.printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
107 foreach (var i in list)
108 stdout.printf("%4u %-45s → %-15s %-7s\n", i.id, i.name, i.type, i.state);
110 stdout.printf("\n%u jobs listed.\n", list.length);
112 } else if (args[1] == "clear-jobs") {
114 manager.clear_jobs();
116 } else if (args[1] == "load") {
118 if (args.length < 3) {
119 stderr.printf("Missing argument.\n");
123 for (uint i = 2; i < args.length; i++)
124 manager.load_unit(args[i]);
126 } else if (args[1] == "cancel") {
128 if (args.length < 3) {
129 stderr.printf("Missing argument.\n");
133 for (uint i = 2; i < args.length; i++) {
136 if (args[i].scanf("%u", out id) != 1) {
137 stderr.printf("Failed to parse argument.\n");
141 ObjectPath p = manager.get_job(id);
143 Job j = bus.get_object (
144 "org.freedesktop.systemd1",
146 "org.freedesktop.systemd1.Job") as Job;
151 } else if (args[1] == "start" ||
153 args[1] == "reload" ||
154 args[1] == "restart") {
156 if (args.length < 3) {
157 stderr.printf("Missing argument.\n");
161 for (uint i = 2; i < args.length; i++) {
163 ObjectPath p = manager.get_unit(args[i]);
165 Unit u = bus.get_object(
166 "org.freedesktop.systemd1",
168 "org.freedesktop.systemd1.Unit") as Unit;
170 string mode = replace ? "replace" : "fail";
172 if (args[1] == "start")
174 else if (args[1] == "stop")
176 else if (args[1] == "restart")
178 else if (args[1] == "reload")
183 stderr.printf("Unknown command %s.\n", args[1]);
187 } catch (DBus.Error e) {
188 stderr.printf("%s\n".printf(e.message));