]> git.ipfire.org Git - people/ms/systemd.git/blame - systemctl.vala
manager: properly read timerfd elapse counter
[people/ms/systemd.git] / systemctl.vala
CommitLineData
a7334b09
LP
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
5630af71
LP
20using DBus;
21using GLib;
22
5630af71
LP
23static string type = null;
24static bool all = false;
501c7d0b 25static bool replace = false;
5630af71
LP
26
27public static int job_info_compare(void* key1, void* key2) {
28 Manager.JobInfo *j1 = (Manager.JobInfo*) key1;
29 Manager.JobInfo *j2 = (Manager.JobInfo*) key2;
30
501c7d0b 31 return j1->id < j2->id ? -1 : (j1->id > j2->id ? 1 : 0);
5630af71
LP
32}
33
34public static int unit_info_compare(void* key1, void* key2) {
35 Manager.UnitInfo *u1 = (Manager.UnitInfo*) key1;
36 Manager.UnitInfo *u2 = (Manager.UnitInfo*) key2;
37
38 int r = Posix.strcmp(Posix.strrchr(u1->id, '.'), Posix.strrchr(u2->id, '.'));
39 if (r != 0)
40 return r;
41
42 return Posix.strcmp(u1->id, u2->id);
43}
44
c1e1601e
LP
45public void on_unit_new(string id, ObjectPath path) {
46 stdout.printf("Unit %s added.\n", id);
47}
48
49public void on_job_new(uint32 id, ObjectPath path) {
50 stdout.printf("Job %u added.\n", id);
51}
52
53public void on_unit_removed(string id, ObjectPath path) {
54 stdout.printf("Unit %s removed.\n", id);
55}
56
57public void on_job_removed(uint32 id, ObjectPath path) {
58 stdout.printf("Job %u removed.\n", id);
59}
60
5630af71 61static const OptionEntry entries[] = {
501c7d0b
LP
62 { "type", 't', 0, OptionArg.STRING, out type, "List only particular type of units", "TYPE" },
63 { "all", 'a', 0, OptionArg.NONE, out all, "Show all units, including dead ones", null },
64 { "replace", 0, 0, OptionArg.NONE, out replace, "When installing a new job, replace existing conflicting ones.", null },
5630af71
LP
65 { null }
66};
67
68int main (string[] args) {
69
c1e1601e 70 OptionContext context = new OptionContext(" [COMMAND [ARGUMENT...]]");
5630af71 71 context.add_main_entries(entries, null);
c1e1601e
LP
72 context.set_description(
73 "Commands:\n" +
74 " list-units List units\n" +
75 " list-jobs List jobs\n" +
76 " clear-jobs Cancel all jobs\n" +
77 " load [NAME...] Load one or more units\n" +
78 " cancel [JOB...] Cancel one or more jobs\n" +
79 " start [NAME...] Start on or more units\n" +
80 " stop [NAME...] Stop on or more units\n" +
81 " restart [NAME...] Restart on or more units\n" +
82 " reload [NAME...] Reload on or more units\n" +
83 " monitor Monitor unit/job changes\n");
5630af71
LP
84
85 try {
86 context.parse(ref args);
87 } catch (GLib.OptionError e) {
88 message("Failed to parse command line: %s".printf(e.message));
89 }
90
91 try {
92 Connection bus = Bus.get(BusType.SESSION);
93
94 Manager manager = bus.get_object (
95 "org.freedesktop.systemd1",
96 "/org/freedesktop/systemd1",
97 "org.freedesktop.systemd1") as Manager;
98
99 if (args[1] == "list-units" || args.length <= 1) {
501c7d0b 100 var list = manager.list_units();
5630af71
LP
101 uint n = 0;
102 Posix.qsort(list, list.length, sizeof(Manager.UnitInfo), unit_info_compare);
103
dcc47b17 104 stdout.printf("%-45s %-6s %-12s %-17s\n", "UNIT", "LOAD", "ACTIVE", "JOB");
5630af71
LP
105
106 foreach (var i in list) {
107
108 if (type != null && !i.id.has_suffix(".%s".printf(type)))
109 continue;
110
111 if (!all && i.active_state == "inactive")
112 continue;
113
114 stdout.printf("%-45s %-6s %-12s", i.id, i.load_state, i.active_state);
115
116 if (i.job_id != 0)
117 stdout.printf("→ %-15s", i.job_type);
118
119 stdout.puts("\n");
120 n++;
121 }
122
123 if (all)
124 stdout.printf("\n%u units listed.\n", n);
125 else
126 stdout.printf("\n%u live units listed. Pass --all to see dead units, too.\n", n);
127
128
129 } else if (args[1] == "list-jobs") {
501c7d0b 130 var list = manager.list_jobs();
5630af71
LP
131 Posix.qsort(list, list.length, sizeof(Manager.JobInfo), job_info_compare);
132
501c7d0b 133 stdout.printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
dcc47b17 134
5630af71 135 foreach (var i in list)
501c7d0b 136 stdout.printf("%4u %-45s → %-15s %-7s\n", i.id, i.name, i.type, i.state);
5630af71 137
dcc47b17
LP
138 stdout.printf("\n%u jobs listed.\n", list.length);
139
140 } else if (args[1] == "clear-jobs") {
141
501c7d0b 142 manager.clear_jobs();
dcc47b17 143
5630af71
LP
144 } else if (args[1] == "load") {
145
146 if (args.length < 3) {
147 stderr.printf("Missing argument.\n");
148 return 1;
149 }
150
17d37c28 151 for (int i = 2; i < args.length; i++)
501c7d0b
LP
152 manager.load_unit(args[i]);
153
154 } else if (args[1] == "cancel") {
155
156 if (args.length < 3) {
157 stderr.printf("Missing argument.\n");
158 return 1;
159 }
160
17d37c28 161 for (int i = 2; i < args.length; i++) {
501c7d0b
LP
162 uint32 id;
163
164 if (args[i].scanf("%u", out id) != 1) {
165 stderr.printf("Failed to parse argument.\n");
166 return 1;
167 }
168
169 ObjectPath p = manager.get_job(id);
170
171 Job j = bus.get_object (
172 "org.freedesktop.systemd1",
173 p,
174 "org.freedesktop.systemd1.Job") as Job;
175
176 j.cancel();
177 }
178
179 } else if (args[1] == "start" ||
180 args[1] == "stop" ||
181 args[1] == "reload" ||
182 args[1] == "restart") {
183
184 if (args.length < 3) {
185 stderr.printf("Missing argument.\n");
186 return 1;
187 }
188
ab8ea244 189 for (int i = 2; i < args.length; i++) {
501c7d0b
LP
190
191 ObjectPath p = manager.get_unit(args[i]);
192
193 Unit u = bus.get_object(
194 "org.freedesktop.systemd1",
195 p,
196 "org.freedesktop.systemd1.Unit") as Unit;
197
198 string mode = replace ? "replace" : "fail";
199
200 if (args[1] == "start")
201 u.start(mode);
202 else if (args[1] == "stop")
203 u.stop(mode);
204 else if (args[1] == "restart")
205 u.restart(mode);
206 else if (args[1] == "reload")
207 u.reload(mode);
208 }
209
c1e1601e
LP
210 } else if (args[1] == "monitor") {
211
212 manager.subscribe();
213
214 manager.unit_new += on_unit_new;
215 manager.unit_removed += on_unit_removed;
216 manager.job_new += on_job_new;
217 manager.job_removed += on_job_removed;
218
219 MainLoop l = new MainLoop();
220 l.run();
221
5630af71
LP
222 } else {
223 stderr.printf("Unknown command %s.\n", args[1]);
224 return 1;
225 }
226
227 } catch (DBus.Error e) {
228 stderr.printf("%s\n".printf(e.message));
229 }
230
231 return 0;
232}