]>
Commit | Line | Data |
---|---|---|
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 |
20 | using DBus; |
21 | using GLib; | |
22 | ||
5630af71 LP |
23 | static string type = null; |
24 | static bool all = false; | |
501c7d0b | 25 | static bool replace = false; |
edddf4ff | 26 | static bool session = false; |
5630af71 LP |
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 | ||
501c7d0b | 32 | return j1->id < j2->id ? -1 : (j1->id > j2->id ? 1 : 0); |
5630af71 LP |
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 | ||
c1e1601e LP |
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 | ||
5630af71 | 62 | static const OptionEntry entries[] = { |
edddf4ff LP |
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 }, | |
5630af71 LP |
68 | { null } |
69 | }; | |
70 | ||
71 | int main (string[] args) { | |
72 | ||
edddf4ff | 73 | OptionContext context = new OptionContext("[OPTION...] [COMMAND [ARGUMENT...]]"); |
5630af71 | 74 | context.add_main_entries(entries, null); |
c1e1601e LP |
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 | " restart [NAME...] Restart on or more units\n" + | |
85 | " reload [NAME...] Reload on or more units\n" + | |
86 | " monitor Monitor unit/job changes\n"); | |
5630af71 LP |
87 | |
88 | try { | |
89 | context.parse(ref args); | |
90 | } catch (GLib.OptionError e) { | |
91 | message("Failed to parse command line: %s".printf(e.message)); | |
92 | } | |
93 | ||
94 | try { | |
edddf4ff | 95 | Connection bus = Bus.get(session ? BusType.SESSION : BusType.SYSTEM); |
5630af71 LP |
96 | |
97 | Manager manager = bus.get_object ( | |
98 | "org.freedesktop.systemd1", | |
99 | "/org/freedesktop/systemd1", | |
100 | "org.freedesktop.systemd1") as Manager; | |
101 | ||
102 | if (args[1] == "list-units" || args.length <= 1) { | |
501c7d0b | 103 | var list = manager.list_units(); |
5630af71 LP |
104 | uint n = 0; |
105 | Posix.qsort(list, list.length, sizeof(Manager.UnitInfo), unit_info_compare); | |
106 | ||
10a94420 | 107 | stdout.printf("%-45s %-6s %-12s %-12s %-17s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB"); |
5630af71 LP |
108 | |
109 | foreach (var i in list) { | |
110 | ||
111 | if (type != null && !i.id.has_suffix(".%s".printf(type))) | |
112 | continue; | |
113 | ||
114 | if (!all && i.active_state == "inactive") | |
115 | continue; | |
116 | ||
10a94420 | 117 | stdout.printf("%-45s %-6s %-12s %-12s", i.id, i.load_state, i.active_state, i.sub_state); |
5630af71 LP |
118 | |
119 | if (i.job_id != 0) | |
4f320e4d | 120 | stdout.printf(" → %-15s", i.job_type); |
5630af71 LP |
121 | |
122 | stdout.puts("\n"); | |
123 | n++; | |
124 | } | |
125 | ||
126 | if (all) | |
127 | stdout.printf("\n%u units listed.\n", n); | |
128 | else | |
129 | stdout.printf("\n%u live units listed. Pass --all to see dead units, too.\n", n); | |
130 | ||
131 | ||
132 | } else if (args[1] == "list-jobs") { | |
501c7d0b | 133 | var list = manager.list_jobs(); |
5630af71 LP |
134 | Posix.qsort(list, list.length, sizeof(Manager.JobInfo), job_info_compare); |
135 | ||
501c7d0b | 136 | stdout.printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE"); |
dcc47b17 | 137 | |
5630af71 | 138 | foreach (var i in list) |
501c7d0b | 139 | stdout.printf("%4u %-45s → %-15s %-7s\n", i.id, i.name, i.type, i.state); |
5630af71 | 140 | |
dcc47b17 LP |
141 | stdout.printf("\n%u jobs listed.\n", list.length); |
142 | ||
143 | } else if (args[1] == "clear-jobs") { | |
144 | ||
501c7d0b | 145 | manager.clear_jobs(); |
dcc47b17 | 146 | |
5630af71 LP |
147 | } else if (args[1] == "load") { |
148 | ||
149 | if (args.length < 3) { | |
150 | stderr.printf("Missing argument.\n"); | |
151 | return 1; | |
152 | } | |
153 | ||
17d37c28 | 154 | for (int i = 2; i < args.length; i++) |
501c7d0b LP |
155 | manager.load_unit(args[i]); |
156 | ||
157 | } else if (args[1] == "cancel") { | |
158 | ||
159 | if (args.length < 3) { | |
160 | stderr.printf("Missing argument.\n"); | |
161 | return 1; | |
162 | } | |
163 | ||
17d37c28 | 164 | for (int i = 2; i < args.length; i++) { |
501c7d0b LP |
165 | uint32 id; |
166 | ||
167 | if (args[i].scanf("%u", out id) != 1) { | |
168 | stderr.printf("Failed to parse argument.\n"); | |
169 | return 1; | |
170 | } | |
171 | ||
172 | ObjectPath p = manager.get_job(id); | |
173 | ||
174 | Job j = bus.get_object ( | |
175 | "org.freedesktop.systemd1", | |
176 | p, | |
177 | "org.freedesktop.systemd1.Job") as Job; | |
178 | ||
179 | j.cancel(); | |
180 | } | |
181 | ||
182 | } else if (args[1] == "start" || | |
183 | args[1] == "stop" || | |
184 | args[1] == "reload" || | |
185 | args[1] == "restart") { | |
186 | ||
187 | if (args.length < 3) { | |
188 | stderr.printf("Missing argument.\n"); | |
189 | return 1; | |
190 | } | |
191 | ||
ab8ea244 | 192 | for (int i = 2; i < args.length; i++) { |
501c7d0b LP |
193 | |
194 | ObjectPath p = manager.get_unit(args[i]); | |
195 | ||
196 | Unit u = bus.get_object( | |
197 | "org.freedesktop.systemd1", | |
198 | p, | |
199 | "org.freedesktop.systemd1.Unit") as Unit; | |
200 | ||
201 | string mode = replace ? "replace" : "fail"; | |
202 | ||
203 | if (args[1] == "start") | |
204 | u.start(mode); | |
205 | else if (args[1] == "stop") | |
206 | u.stop(mode); | |
207 | else if (args[1] == "restart") | |
208 | u.restart(mode); | |
209 | else if (args[1] == "reload") | |
210 | u.reload(mode); | |
211 | } | |
212 | ||
c1e1601e LP |
213 | } else if (args[1] == "monitor") { |
214 | ||
215 | manager.subscribe(); | |
216 | ||
217 | manager.unit_new += on_unit_new; | |
218 | manager.unit_removed += on_unit_removed; | |
219 | manager.job_new += on_job_new; | |
220 | manager.job_removed += on_job_removed; | |
221 | ||
222 | MainLoop l = new MainLoop(); | |
223 | l.run(); | |
224 | ||
b152adec LP |
225 | } else if (args[1] == "dump") |
226 | stdout.puts(manager.dump()); | |
227 | else { | |
5630af71 LP |
228 | stderr.printf("Unknown command %s.\n", args[1]); |
229 | return 1; | |
230 | } | |
231 | ||
232 | } catch (DBus.Error e) { | |
233 | stderr.printf("%s\n".printf(e.message)); | |
234 | } | |
235 | ||
236 | return 0; | |
237 | } |