| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
import cockpit from "cockpit"; |
| 3 |
|
| 4 |
/* SERVICE MANAGEMENT API |
| 5 |
* |
| 6 |
* The "service" module lets you monitor and manage a |
| 7 |
* system service on localhost in a simple way. |
| 8 |
* |
| 9 |
* It mainly exists because talking to the systemd D-Bus API is |
| 10 |
* not trivial enough to do it directly. |
| 11 |
* |
| 12 |
* - proxy = service.proxy(name) |
| 13 |
* |
| 14 |
* Create a proxy that represents the service named NAME. |
| 15 |
* |
| 16 |
* The proxy has properties and methods (described below) that |
| 17 |
* allow you to monitor the state of the service, and perform |
| 18 |
* simple actions on it. |
| 19 |
* |
| 20 |
* Initially, any of the properties can be "null" until their |
| 21 |
* actual values have been retrieved in the background. |
| 22 |
* |
| 23 |
* - proxy.addEventListener('changed', event => { ... }) |
| 24 |
* |
| 25 |
* The 'changed' event is emitted whenever one of the properties |
| 26 |
* of the proxy changes. |
| 27 |
* |
| 28 |
* - proxy.exists |
| 29 |
* |
| 30 |
* A boolean that tells whether the service is known or not. A |
| 31 |
* proxy with 'exists == false' will have 'state == undefined' and |
| 32 |
* 'enabled == undefined'. |
| 33 |
* |
| 34 |
* - proxy.state |
| 35 |
* |
| 36 |
* Either 'undefined' when the state can't be retrieved, or a |
| 37 |
* string that has one of the values "starting", "running", |
| 38 |
* "stopping", "stopped", or "failed". |
| 39 |
* |
| 40 |
* - proxy.enabled |
| 41 |
* |
| 42 |
* Either 'undefined' when the value can't be retrieved, or a |
| 43 |
* boolean that tells whether the service is started 'enabled'. |
| 44 |
* What it means exactly for a service to be enabled depends on |
| 45 |
* the service, but an enabled service is usually started on boot, |
| 46 |
* no matter whether other services need it or not. A disabled |
| 47 |
* service is usually only started when it is needed by some other |
| 48 |
* service. |
| 49 |
* |
| 50 |
* - proxy.unit |
| 51 |
* - proxy.details |
| 52 |
* |
| 53 |
* The raw org.freedesktop.systemd1.Unit and type-specific D-Bus |
| 54 |
* interface proxies for the service. |
| 55 |
* |
| 56 |
* - proxy.service |
| 57 |
* |
| 58 |
* The deprecated name for proxy.details |
| 59 |
* |
| 60 |
* - promise = proxy.start() |
| 61 |
* |
| 62 |
* Start the service. The return value is a standard jQuery |
| 63 |
* promise as returned from DBusClient.call. |
| 64 |
* |
| 65 |
* - promise = proxy.restart() |
| 66 |
* |
| 67 |
* Restart the service. |
| 68 |
* |
| 69 |
* - promise = proxy.tryRestart() |
| 70 |
* |
| 71 |
* Try to restart the service if it's running or starting |
| 72 |
* |
| 73 |
* - promise = proxy.stop() |
| 74 |
* |
| 75 |
* Stop the service. |
| 76 |
* |
| 77 |
* - promise = proxy.enable() |
| 78 |
* |
| 79 |
* Enable the service. |
| 80 |
* |
| 81 |
* - promise = proxy.disable() |
| 82 |
* |
| 83 |
* Disable the service. |
| 84 |
* |
| 85 |
* - journal = proxy.getRunJournal(options) |
| 86 |
* |
| 87 |
* Return the journal of the current (if running) or recent (if failed/stopped) service run, |
| 88 |
* similar to `systemctl status`. `options` is an optional array that gets appended to the `journalctl` call. |
| 89 |
*/ |
| 90 |
|
| 91 |
let systemd_client; |
| 92 |
let systemd_manager; |
| 93 |
|
| 94 |
function wait_valid(proxy, callback) { |
| 95 |
proxy.wait(() => { |
| 96 |
if (proxy.valid) |
| 97 |
callback(); |
| 98 |
}); |
| 99 |
} |
| 100 |
|
| 101 |
function with_systemd_manager(done) { |
| 102 |
if (!systemd_manager) { |
| 103 |
// cached forever, only used for reading/watching; no superuser |
| 104 |
systemd_client = cockpit.dbus("org.freedesktop.systemd1"); |
| 105 |
systemd_manager = systemd_client.proxy("org.freedesktop.systemd1.Manager", |
| 106 |
"/org/freedesktop/systemd1"); |
| 107 |
wait_valid(systemd_manager, () => { |
| 108 |
systemd_manager.Subscribe() |
| 109 |
.catch(error => { |
| 110 |
if (error.name != "org.freedesktop.systemd1.AlreadySubscribed" && |
| 111 |
error.name != "org.freedesktop.DBus.Error.FileExists") |
| 112 |
console.warn("Subscribing to systemd signals failed", error); |
| 113 |
}); |
| 114 |
}); |
| 115 |
} |
| 116 |
wait_valid(systemd_manager, done); |
| 117 |
} |
| 118 |
|
| 119 |
export function proxy(name, kind) { |
| 120 |
const self = { |
| 121 |
exists: null, |
| 122 |
state: null, |
| 123 |
enabled: null, |
| 124 |
|
| 125 |
wait, |
| 126 |
|
| 127 |
start, |
| 128 |
stop, |
| 129 |
restart, |
| 130 |
tryRestart, |
| 131 |
|
| 132 |
enable, |
| 133 |
disable, |
| 134 |
|
| 135 |
getRunJournal, |
| 136 |
}; |
| 137 |
|
| 138 |
cockpit.event_target(self); |
| 139 |
|
| 140 |
let unit; |
| 141 |
let details; |
| 142 |
let wait_promise_resolve; |
| 143 |
const wait_promise = new Promise(resolve => { wait_promise_resolve = resolve }); |
| 144 |
|
| 145 |
if (name.indexOf(".") == -1) |
| 146 |
name = name + ".service"; |
| 147 |
if (kind === undefined) |
| 148 |
kind = "Service"; |
| 149 |
|
| 150 |
function update_from_unit() { |
| 151 |
self.exists = (unit.LoadState != "not-found" || unit.ActiveState != "inactive"); |
| 152 |
|
| 153 |
if (unit.ActiveState == "activating") |
| 154 |
self.state = "starting"; |
| 155 |
else if (unit.ActiveState == "deactivating") |
| 156 |
self.state = "stopping"; |
| 157 |
else if (unit.ActiveState == "active" || unit.ActiveState == "reloading") |
| 158 |
self.state = "running"; |
| 159 |
else if (unit.ActiveState == "failed") |
| 160 |
self.state = "failed"; |
| 161 |
else if (unit.ActiveState == "inactive" && self.exists) |
| 162 |
self.state = "stopped"; |
| 163 |
else |
| 164 |
self.state = undefined; |
| 165 |
|
| 166 |
if (unit.UnitFileState == "enabled" || unit.UnitFileState == "linked") |
| 167 |
self.enabled = true; |
| 168 |
else if (unit.UnitFileState == "disabled" || unit.UnitFileState == "masked") |
| 169 |
self.enabled = false; |
| 170 |
else |
| 171 |
self.enabled = undefined; |
| 172 |
|
| 173 |
self.unit = unit; |
| 174 |
|
| 175 |
self.dispatchEvent("changed"); |
| 176 |
wait_promise_resolve(); |
| 177 |
} |
| 178 |
|
| 179 |
function update_from_details() { |
| 180 |
self.details = details; |
| 181 |
self.service = details; |
| 182 |
self.dispatchEvent("changed"); |
| 183 |
} |
| 184 |
|
| 185 |
with_systemd_manager(function () { |
| 186 |
systemd_manager.LoadUnit(name) |
| 187 |
.then(path => { |
| 188 |
unit = systemd_client.proxy('org.freedesktop.systemd1.Unit', path); |
| 189 |
unit.addEventListener('changed', update_from_unit); |
| 190 |
wait_valid(unit, update_from_unit); |
| 191 |
|
| 192 |
details = systemd_client.proxy('org.freedesktop.systemd1.' + kind, path); |
| 193 |
details.addEventListener('changed', update_from_details); |
| 194 |
wait_valid(details, update_from_details); |
| 195 |
}) |
| 196 |
.catch(() => { |
| 197 |
self.exists = false; |
| 198 |
self.dispatchEvent('changed'); |
| 199 |
}); |
| 200 |
}); |
| 201 |
|
| 202 |
function refresh() { |
| 203 |
if (!unit || !details) |
| 204 |
return Promise.resolve(); |
| 205 |
|
| 206 |
function refresh_interface(path, iface) { |
| 207 |
return systemd_client.call(path, "org.freedesktop.DBus.Properties", "GetAll", [iface]) |
| 208 |
.then(([result]) => { |
| 209 |
const props = { }; |
| 210 |
for (const p in result) |
| 211 |
props[p] = result[p].v; |
| 212 |
systemd_client.notify({ [unit.path]: { [iface]: props } }); |
| 213 |
}) |
| 214 |
.catch(error => console.log(error)); |
| 215 |
} |
| 216 |
|
| 217 |
return Promise.allSettled([ |
| 218 |
refresh_interface(unit.path, "org.freedesktop.systemd1.Unit"), |
| 219 |
refresh_interface(details.path, "org.freedesktop.systemd1." + kind), |
| 220 |
]); |
| 221 |
} |
| 222 |
|
| 223 |
function on_job_new_removed_refresh(event, number, path, unit_id, result) { |
| 224 |
if (unit_id == name) |
| 225 |
refresh(); |
| 226 |
} |
| 227 |
|
| 228 |
/* HACK - https://github.com/systemd/systemd/issues/570#issuecomment-125334529 |
| 229 |
* |
| 230 |
* We need to explicitly get new property values when getting |
| 231 |
* a UnitNew signal since UnitNew doesn't carry them. |
| 232 |
* However, reacting to UnitNew with GetAll could lead to an |
| 233 |
* infinite loop since systemd emits a UnitNew in reaction to |
| 234 |
* GetAll for units that it doesn't want to keep loaded, such |
| 235 |
* as units without unit files. |
| 236 |
* |
| 237 |
* So we ignore UnitNew and instead assume that the unit state |
| 238 |
* only changes in interesting ways when there is a job for it |
| 239 |
* or when the daemon is reloaded (or when we get a property |
| 240 |
* change notification, of course). |
| 241 |
*/ |
| 242 |
|
| 243 |
// This is what we want to do: |
| 244 |
// systemd_manager.addEventListener("UnitNew", function (event, unit_id, path) { |
| 245 |
// if (unit_id == name) |
| 246 |
// refresh(); |
| 247 |
// }); |
| 248 |
|
| 249 |
// This is what we have to do: |
| 250 |
systemd_manager.addEventListener("Reloading", (event, reloading) => { |
| 251 |
if (!reloading) |
| 252 |
refresh(); |
| 253 |
}); |
| 254 |
|
| 255 |
systemd_manager.addEventListener("JobNew", on_job_new_removed_refresh); |
| 256 |
systemd_manager.addEventListener("JobRemoved", on_job_new_removed_refresh); |
| 257 |
|
| 258 |
function wait(callback) { |
| 259 |
wait_promise.then(callback); |
| 260 |
} |
| 261 |
|
| 262 |
/* Actions |
| 263 |
* |
| 264 |
* We don't call methods on the persistent systemd_client, as that does not have superuser |
| 265 |
*/ |
| 266 |
|
| 267 |
function call_manager(dbus, method, args) { |
| 268 |
return dbus.call("/org/freedesktop/systemd1", |
| 269 |
"org.freedesktop.systemd1.Manager", |
| 270 |
method, args); |
| 271 |
} |
| 272 |
|
| 273 |
function call_manager_with_job(method, args) { |
| 274 |
return new Promise((resolve, reject) => { |
| 275 |
const dbus = cockpit.dbus("org.freedesktop.systemd1", { superuser: "try" }); |
| 276 |
let pending_job_path; |
| 277 |
|
| 278 |
const subscription = dbus.subscribe( |
| 279 |
{ interface: "org.freedesktop.systemd1.Manager", member: "JobRemoved" }, |
| 280 |
(_path, _iface, _signal, [_number, path, _unit_id, result]) => { |
| 281 |
if (path == pending_job_path) { |
| 282 |
subscription.remove(); |
| 283 |
dbus.close(); |
| 284 |
refresh().then(() => { |
| 285 |
if (result === "done") |
| 286 |
resolve(); |
| 287 |
else |
| 288 |
reject(new Error(`systemd job ${method} ${JSON.stringify(args)} failed with result ${result}`)); |
| 289 |
}); |
| 290 |
} |
| 291 |
}); |
| 292 |
|
| 293 |
call_manager(dbus, method, args) |
| 294 |
.then(([path]) => { pending_job_path = path }) |
| 295 |
.catch(ex => { |
| 296 |
dbus.close(); |
| 297 |
reject(ex); |
| 298 |
}); |
| 299 |
}); |
| 300 |
} |
| 301 |
|
| 302 |
function call_manager_with_reload(method, args) { |
| 303 |
const dbus = cockpit.dbus("org.freedesktop.systemd1", { superuser: "try" }); |
| 304 |
return call_manager(dbus, method, args) |
| 305 |
.then(() => call_manager(dbus, "Reload", [])) |
| 306 |
.then(refresh) |
| 307 |
.finally(dbus.close); |
| 308 |
} |
| 309 |
|
| 310 |
function start() { |
| 311 |
return call_manager_with_job("StartUnit", [name, "replace"]); |
| 312 |
} |
| 313 |
|
| 314 |
function stop() { |
| 315 |
return call_manager_with_job("StopUnit", [name, "replace"]); |
| 316 |
} |
| 317 |
|
| 318 |
function restart() { |
| 319 |
return call_manager_with_job("RestartUnit", [name, "replace"]); |
| 320 |
} |
| 321 |
|
| 322 |
function tryRestart() { |
| 323 |
return call_manager_with_job("TryRestartUnit", [name, "replace"]); |
| 324 |
} |
| 325 |
|
| 326 |
function enable() { |
| 327 |
return call_manager_with_reload("EnableUnitFiles", [[name], false, false]); |
| 328 |
} |
| 329 |
|
| 330 |
function disable() { |
| 331 |
return call_manager_with_reload("DisableUnitFiles", [[name], false]); |
| 332 |
} |
| 333 |
|
| 334 |
function getRunJournal(options) { |
| 335 |
if (!details || !details.ExecMainStartTimestamp) |
| 336 |
return Promise.reject(new Error("getRunJournal(): unit is not known")); |
| 337 |
|
| 338 |
// collect the service journal since start time; property is μs, journal wants s |
| 339 |
const startTime = Math.floor(details.ExecMainStartTimestamp / 1000000); |
| 340 |
return cockpit.spawn( |
| 341 |
["journalctl", "--unit", name, "--since=@" + startTime.toString()].concat(options || []), |
| 342 |
{ superuser: "try", error: "message" }); |
| 343 |
} |
| 344 |
|
| 345 |
return self; |
| 346 |
} |