|
| 1 | +#!/usr/bin/env node |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +const manager = "org.freedesktop.systemd1"; |
| 6 | +const rootObjectPath = "/org/freedesktop/systemd1"; |
| 7 | +const args = process.argv.slice(2); |
| 8 | + |
| 9 | +function fail() { |
| 10 | + process.stderr.write("doctor-switch busctl shim: unexpected invocation or unavailable unit\n"); |
| 11 | + process.exit(1); |
| 12 | +} |
| 13 | + |
| 14 | +function splitWords(value) { |
| 15 | + const words = []; |
| 16 | + let word = ""; |
| 17 | + let quote = ""; |
| 18 | + let escaped = false; |
| 19 | + for (const character of value) { |
| 20 | + if (escaped) { |
| 21 | + word += character; |
| 22 | + escaped = false; |
| 23 | + } else if (character === "\\" && quote !== "'") { |
| 24 | + escaped = true; |
| 25 | + } else if (quote) { |
| 26 | + if (character === quote) { |
| 27 | + quote = ""; |
| 28 | + } else { |
| 29 | + word += character; |
| 30 | + } |
| 31 | + } else if (character === '"' || character === "'") { |
| 32 | + quote = character; |
| 33 | + } else if (/\s/u.test(character)) { |
| 34 | + if (word) { |
| 35 | + words.push(word); |
| 36 | + word = ""; |
| 37 | + } |
| 38 | + } else { |
| 39 | + word += character; |
| 40 | + } |
| 41 | + } |
| 42 | + if (quote || escaped) { |
| 43 | + fail(); |
| 44 | + } |
| 45 | + if (word) { |
| 46 | + words.push(word); |
| 47 | + } |
| 48 | + return words; |
| 49 | +} |
| 50 | + |
| 51 | +function encodeUnitName(name) { |
| 52 | + return Array.from(Buffer.from(name), (byte) => |
| 53 | + (byte >= 48 && byte <= 57) || (byte >= 65 && byte <= 90) || (byte >= 97 && byte <= 122) |
| 54 | + ? String.fromCharCode(byte) |
| 55 | + : `_${byte.toString(16).padStart(2, "0")}`, |
| 56 | + ).join(""); |
| 57 | +} |
| 58 | + |
| 59 | +function writeProperties(properties) { |
| 60 | + process.stdout.write(properties.map((property) => JSON.stringify(property)).join("\n") + "\n"); |
| 61 | +} |
| 62 | + |
| 63 | +if (args[0] !== "--user" || args[1] !== "--json=short") { |
| 64 | + fail(); |
| 65 | +} |
| 66 | + |
| 67 | +const invocation = args.slice(2); |
| 68 | +if (invocation[0] === "call") { |
| 69 | + const [command, destination, objectPath, interfaceName, method, signature, unitName] = invocation; |
| 70 | + if ( |
| 71 | + invocation.length !== 7 || |
| 72 | + command !== "call" || |
| 73 | + destination !== manager || |
| 74 | + objectPath !== rootObjectPath || |
| 75 | + interfaceName !== `${manager}.Manager` || |
| 76 | + method !== "LoadUnit" || |
| 77 | + signature !== "s" || |
| 78 | + !unitName.endsWith(".service") || |
| 79 | + path.basename(unitName) !== unitName || |
| 80 | + !fs.existsSync(path.join(process.env.HOME, ".config/systemd/user", unitName)) |
| 81 | + ) { |
| 82 | + fail(); |
| 83 | + } |
| 84 | + writeProperties([{ type: "o", data: [`${rootObjectPath}/unit/${encodeUnitName(unitName)}`] }]); |
| 85 | + process.exit(0); |
| 86 | +} |
| 87 | + |
| 88 | +if (invocation[0] !== "get-property" || invocation[1] !== manager) { |
| 89 | + fail(); |
| 90 | +} |
| 91 | + |
| 92 | +const unitName = `${process.env.OPENCLAW_SYSTEMD_UNIT || "openclaw-gateway"}`.replace( |
| 93 | + /(?:\.service)?$/u, |
| 94 | + ".service", |
| 95 | +); |
| 96 | +const unitPath = path.join(process.env.HOME, ".config/systemd/user", unitName); |
| 97 | +if (invocation[2] !== `${rootObjectPath}/unit/${encodeUnitName(unitName)}`) { |
| 98 | + fail(); |
| 99 | +} |
| 100 | + |
| 101 | +if (invocation[3] === `${manager}.Unit`) { |
| 102 | + if ( |
| 103 | + invocation.length !== 7 || |
| 104 | + invocation.slice(4).join(" ") !== "FragmentPath DropInPaths NeedDaemonReload" |
| 105 | + ) { |
| 106 | + fail(); |
| 107 | + } |
| 108 | + writeProperties([ |
| 109 | + { type: "s", data: unitPath }, |
| 110 | + { type: "as", data: [] }, |
| 111 | + { type: "b", data: false }, |
| 112 | + ]); |
| 113 | + process.exit(0); |
| 114 | +} |
| 115 | + |
| 116 | +if ( |
| 117 | + invocation[3] !== `${manager}.Service` || |
| 118 | + invocation.length !== 9 || |
| 119 | + invocation.slice(4).join(" ") !== |
| 120 | + "ExecStart WorkingDirectory Environment EnvironmentFiles UnsetEnvironment" |
| 121 | +) { |
| 122 | + fail(); |
| 123 | +} |
| 124 | + |
| 125 | +let programArguments = []; |
| 126 | +let workingDirectory = ""; |
| 127 | +const environment = new Map(); |
| 128 | +const environmentFiles = []; |
| 129 | +const unsetEnvironment = []; |
| 130 | +const expandHome = (value) => |
| 131 | + value.replace(/%%|%h/gu, (match) => (match === "%h" ? process.env.HOME : "%")); |
| 132 | + |
| 133 | +let content; |
| 134 | +try { |
| 135 | + content = fs.readFileSync(unitPath, "utf8"); |
| 136 | +} catch { |
| 137 | + fail(); |
| 138 | +} |
| 139 | + |
| 140 | +for (const rawLine of content.split(/\r?\n/u)) { |
| 141 | + const line = rawLine.trim(); |
| 142 | + const separator = line.indexOf("="); |
| 143 | + if (separator < 0 || line.startsWith("#") || line.startsWith(";")) { |
| 144 | + continue; |
| 145 | + } |
| 146 | + const directive = line.slice(0, separator); |
| 147 | + const value = line.slice(separator + 1).trim(); |
| 148 | + if (directive === "ExecStart") { |
| 149 | + programArguments = splitWords(value).map(expandHome); |
| 150 | + } else if (directive === "WorkingDirectory") { |
| 151 | + workingDirectory = expandHome(splitWords(value)[0] || "").replace(/^-/, ""); |
| 152 | + } else if (directive === "Environment") { |
| 153 | + if (!value) { |
| 154 | + environment.clear(); |
| 155 | + } |
| 156 | + for (const assignment of splitWords(value)) { |
| 157 | + const assignmentSeparator = assignment.indexOf("="); |
| 158 | + if (assignmentSeparator <= 0) { |
| 159 | + fail(); |
| 160 | + } |
| 161 | + environment.set( |
| 162 | + assignment.slice(0, assignmentSeparator), |
| 163 | + expandHome(assignment.slice(assignmentSeparator + 1)), |
| 164 | + ); |
| 165 | + } |
| 166 | + } else if (directive === "EnvironmentFile") { |
| 167 | + if (!value) { |
| 168 | + environmentFiles.length = 0; |
| 169 | + } |
| 170 | + for (const filename of splitWords(value)) { |
| 171 | + const optional = filename.startsWith("-"); |
| 172 | + environmentFiles.push([expandHome(optional ? filename.slice(1) : filename), optional]); |
| 173 | + } |
| 174 | + } else if (directive === "UnsetEnvironment") { |
| 175 | + if (!value) { |
| 176 | + unsetEnvironment.length = 0; |
| 177 | + } else { |
| 178 | + unsetEnvironment.push(...splitWords(value)); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +if (!programArguments.length) { |
| 184 | + fail(); |
| 185 | +} |
| 186 | + |
| 187 | +writeProperties([ |
| 188 | + { |
| 189 | + type: "a(sasbttttuii)", |
| 190 | + data: [[programArguments[0], programArguments, false, 0, 0, 0, 0, 0, 0, 0]], |
| 191 | + }, |
| 192 | + { type: "s", data: workingDirectory }, |
| 193 | + { type: "as", data: Array.from(environment, ([key, value]) => `${key}=${value}`) }, |
| 194 | + { type: "a(sb)", data: environmentFiles }, |
| 195 | + { type: "as", data: unsetEnvironment }, |
| 196 | +]); |
0 commit comments