@push.rocks/smartdaemon 2.18.0 → 2.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@ import * as plugins from './smartdaemon.plugins.js';
2
2
  import { environmentByteLimit, environmentVariableLimit, isCleanText, renderServiceRelations, systemdAddressFamilies,
3
3
  systemdCapabilities, type TSystemdServiceStartup, type ISystemdServiceDependencies, type ISystemdServiceInstallTargets,
4
4
  type ISystemdServiceCredential, type TSystemdCapability, type TSystemdAddressFamily } from './systemd.definition.js';
5
+ import { fileDescriptorStorePreserveMajor } from './systemd.managerversion.js';
5
6
  export type { ISystemdServiceStartup, ISystemdServiceNotifyStartup, TSystemdServiceStartup,
6
7
  ISystemdServiceDependencies, ISystemdServiceInstallTargets, ISystemdServiceCredential, TSystemdCapability,
7
8
  TSystemdAddressFamily } from './systemd.definition.js';
@@ -65,7 +66,9 @@ interface ISystemdServiceDefinitionBaseOptions {
65
66
  * When the manager releases the store (`FileDescriptorStorePreserve=`, systemd 254 or later):
66
67
  * `'no'` when the service stops, `'restart'` (systemd's default) once it is neither active nor
67
68
  * about to restart, `'yes'` only when the unit leaves the manager's memory or is cleaned with
68
- * `SystemdUnit.cleanFileDescriptorStore()`. Requires `fileDescriptorStoreMax`.
69
+ * `SystemdUnit.cleanFileDescriptorStore()`. Requires `fileDescriptorStoreMax`. Sets
70
+ * `requiredManagerMajor` to 254, so `SystemdUnitFile.install()` refuses an older manager, which
71
+ * would ignore the line.
69
72
  */
70
73
  fileDescriptorStorePreserve?: 'no' | 'yes' | 'restart';
71
74
  /**
@@ -172,6 +175,12 @@ export class SystemdServiceDefinition {
172
175
  public readonly scope: 'system' | 'user';
173
176
  public readonly content: string;
174
177
  public readonly sha256: string;
178
+ /**
179
+ * The oldest systemd major whose manager reads every directive this definition renders, when it
180
+ * renders one an older manager would ignore: 254 with `fileDescriptorStorePreserve`; otherwise
181
+ * null. `SystemdUnitFile.install()` refuses a definition on an older manager.
182
+ */
183
+ public readonly requiredManagerMajor: number | null;
175
184
 
176
185
  constructor(options: ISystemdServiceDefinitionOptions) {
177
186
  const fail = (option: TSystemdServiceDefinitionOption): never => {
@@ -508,6 +517,8 @@ export class SystemdServiceDefinition {
508
517
  ].join('\n');
509
518
  requireValue(Buffer.byteLength(this.content) <= 16384, 'options');
510
519
  this.sha256 = plugins.crypto.createHash('sha256').update(this.content).digest('hex');
520
+ this.requiredManagerMajor = options.fileDescriptorStorePreserve !== undefined
521
+ ? fileDescriptorStorePreserveMajor : null;
511
522
  Object.freeze(this);
512
523
  }
513
524
  }
@@ -1,6 +1,12 @@
1
1
  import * as plugins from './smartdaemon.plugins.js';
2
2
  import { isCleanText } from './systemd.definition.js';
3
3
  import { captureUserEnvironment, inspectRuntimeDirectory, isSafeDirectory } from './systemd.userenvironment.js';
4
+ import {
5
+ type ISystemdManagerVersion,
6
+ managerVersionArguments,
7
+ parseManagerVersion,
8
+ transientScopeMajor,
9
+ } from './systemd.managerversion.js';
4
10
 
5
11
  export interface ISystemdTransientScopeOptions {
6
12
  /** The calling user's manager; the only manager a transient scope is started in. */
@@ -51,8 +57,16 @@ export type TSystemdTransientScopeErrorCode =
51
57
  | 'invalid_options'
52
58
  /** The process is not running on Linux. */
53
59
  | 'unsupported_platform'
54
- /** No runtime directory is set or it does not exist, or the manager does not answer systemctl. */
60
+ /**
61
+ * No runtime directory is set or it does not exist, or the manager does not answer systemctl
62
+ * with a version it reports as systemd does (`SystemdUnit.inspectManagerVersion()`).
63
+ */
55
64
  | 'user_manager_unavailable'
65
+ /**
66
+ * The user manager is older than systemd 254, whose systemd-run this call needs; always a
67
+ * `SystemdTransientScopeManagerTooOldError`. Nothing was started.
68
+ */
69
+ | 'manager_too_old'
56
70
  /** systemd-run exited without reporting the scope running; the program has not run. */
57
71
  | 'scope_start_failed'
58
72
  /** The manager probe or the program did not finish, and close its output, in time. */
@@ -74,6 +88,24 @@ export class SystemdTransientScopeError extends Error {
74
88
  }
75
89
  }
76
90
 
91
+ /** Every `manager_too_old` refusal: names the user manager's version and the one required. */
92
+ export class SystemdTransientScopeManagerTooOldError extends SystemdTransientScopeError {
93
+ declare public readonly code: 'manager_too_old';
94
+
95
+ constructor(
96
+ unitName: string,
97
+ /** The user manager's version the probe read. */
98
+ public readonly managerVersion: ISystemdManagerVersion,
99
+ /** The oldest systemd major the call needs. */
100
+ public readonly requiredMajor: number,
101
+ ) {
102
+ super('manager_too_old', unitName, '');
103
+ this.message = `Systemd transient scope failed: manager_too_old (${unitName}: systemd ` +
104
+ `${managerVersion.major} running, ${requiredMajor} required).`;
105
+ this.name = 'SystemdTransientScopeManagerTooOldError';
106
+ }
107
+ }
108
+
77
109
  const outputLimit = 1048576;
78
110
  const reservedName = (name: string) =>
79
111
  name === 'XDG_RUNTIME_DIR' || name === 'INVOCATION_ID' || name.startsWith('SYSTEMD_') || name.startsWith('DBUS_');
@@ -227,7 +259,9 @@ const programStderr = (stderr: string, unitName: string): string | undefined =>
227
259
  * must not keep the program's stdout or stderr open, or the call runs into its timeout.
228
260
  *
229
261
  * On `timed_out` and `output_limit` the direct process is killed; processes it started stay in
230
- * the scope. A program that exits with a failure status is a result, not an error.
262
+ * the scope. A program that exits with a failure status is a result, not an error. A user manager
263
+ * older than systemd 254 is refused with `SystemdTransientScopeManagerTooOldError` before
264
+ * systemd-run is started.
231
265
  */
232
266
  export const runInTransientScope = async (
233
267
  options: ISystemdTransientScopeOptions,
@@ -247,13 +281,19 @@ export const runInTransientScope = async (
247
281
  if (admission !== 'private') fail('invalid_options');
248
282
 
249
283
  const probe = await runProcess(validated.systemctlPath,
250
- ['--user', '--no-ask-password', '--no-pager', 'show', '--property=Version'],
284
+ ['--user', '--no-ask-password', '--no-pager', ...managerVersionArguments],
251
285
  { PATH: '/usr/bin:/bin', LANG: 'C', LC_ALL: 'C', XDG_RUNTIME_DIR: runtimeDirectory! }, validated.timeoutMs);
252
286
  if (probe.kind === 'timed_out') fail('timed_out', probe.stderr);
253
- if (probe.kind !== 'exited' || probe.code !== 0 || !probe.stdout.startsWith('Version=')) {
254
- fail('user_manager_unavailable', probe.kind === 'spawn_failed' ? '' : probe.stderr,
287
+ const managerVersion = probe.kind === 'exited' && probe.code === 0 ? parseManagerVersion(probe.stdout) : undefined;
288
+ if (managerVersion === undefined) {
289
+ return fail('user_manager_unavailable', probe.kind === 'spawn_failed' ? '' : probe.stderr,
255
290
  probe.kind === 'spawn_failed' ? probe.error : undefined);
256
291
  }
292
+ // systemd-run before 254 knows no --expand-environment; the running user manager's version is the
293
+ // one this call can read before starting anything, so it stands for the installation's.
294
+ if (managerVersion.major < transientScopeMajor) {
295
+ throw new SystemdTransientScopeManagerTooOldError(unitName, managerVersion, transientScopeMajor);
296
+ }
257
297
 
258
298
  // systemd-run registers its own process as the scope, then executes the program in place with
259
299
  // its environment, so that environment is the program's own plus INVOCATION_ID.
@@ -8,6 +8,14 @@ import {
8
8
  isUserManagerEnvironment,
9
9
  } from './systemd.userenvironment.js';
10
10
  import { environmentByteLimit, environmentVariableLimit, isCleanText } from './systemd.definition.js';
11
+ import {
12
+ type ISystemdManagerVersion,
13
+ fileDescriptorStoreCleanMajor,
14
+ fileDescriptorStorePreserveMajor,
15
+ managerVersionArguments,
16
+ parseManagerVersion,
17
+ } from './systemd.managerversion.js';
18
+ export type { ISystemdManagerVersion } from './systemd.managerversion.js';
11
19
 
12
20
  export interface ISystemdUnitOptions {
13
21
  unitName: string;
@@ -171,8 +179,17 @@ export class SystemdUnitError extends Error {
171
179
  /**
172
180
  * `released_uninspected`: only `cleanFileDescriptorStore()`. The manager released the store, and
173
181
  * the snapshot read afterwards failed; `cause` is that inspection's error.
182
+ * `version_unknown`: the manager answered, but its `Version` property is not one this package
183
+ * reads, so a call that needs a minimum manager version refuses.
184
+ * `manager_too_old`: always a `SystemdUnitManagerTooOldError`, naming both versions.
174
185
  */
175
- public readonly code: 'invalid_options' | 'command_failed' | 'invalid_state' | 'released_uninspected',
186
+ public readonly code:
187
+ | 'invalid_options'
188
+ | 'command_failed'
189
+ | 'invalid_state'
190
+ | 'released_uninspected'
191
+ | 'version_unknown'
192
+ | 'manager_too_old',
176
193
  options?: ErrorOptions,
177
194
  ) {
178
195
  super(`Systemd unit operation failed: ${code}.`, options);
@@ -180,6 +197,23 @@ export class SystemdUnitError extends Error {
180
197
  }
181
198
  }
182
199
 
200
+ /** Every `manager_too_old` refusal: the running manager is older than the call needs. */
201
+ export class SystemdUnitManagerTooOldError extends SystemdUnitError {
202
+ declare public readonly code: 'manager_too_old';
203
+
204
+ constructor(
205
+ /** The running manager's version the refusal read. */
206
+ public readonly managerVersion: ISystemdManagerVersion,
207
+ /** The oldest systemd major the refused call needs. */
208
+ public readonly requiredMajor: number,
209
+ ) {
210
+ super('manager_too_old');
211
+ this.message = `Systemd unit operation failed: manager_too_old (systemd ${managerVersion.major} ` +
212
+ `running, ${requiredMajor} required).`;
213
+ this.name = 'SystemdUnitManagerTooOldError';
214
+ }
215
+ }
216
+
183
217
  // systemd's format_timespan() units, largest first; a year and a month are 365.25 and 30.44 days.
184
218
  const timespanUnits = new Map<string, bigint>([
185
219
  ['y', 31557600000000n], ['month', 2629800000000n], ['w', 604800000000n], ['d', 86400000000n],
@@ -514,11 +548,42 @@ export class SystemdUnit {
514
548
  });
515
549
  }
516
550
 
551
+ /**
552
+ * Reads the running manager's version (`systemctl show --property=Version`) through the unit's
553
+ * scope: the system manager, or the calling user's manager for user scope. A manager that does
554
+ * not answer rejects with `command_failed`; an answer this package cannot read, with
555
+ * `version_unknown`.
556
+ */
557
+ public async inspectManagerVersion(): Promise<ISystemdManagerVersion> {
558
+ const version = parseManagerVersion(await this.command([...managerVersionArguments]));
559
+ if (version === undefined) throw new SystemdUnitError('version_unknown');
560
+ return version;
561
+ }
562
+
563
+ /**
564
+ * Resolves with the running manager's version when its major is at least `requiredMajor`, an
565
+ * integer from 1 to 9999; otherwise rejects with a `SystemdUnitManagerTooOldError`. Fails closed:
566
+ * an unreadable version rejects with `version_unknown`, an unanswered read with
567
+ * `command_failed`, and a malformed `requiredMajor` with `invalid_options` before any command.
568
+ */
569
+ public async requireManagerVersion(requiredMajor: number): Promise<ISystemdManagerVersion> {
570
+ if (!Number.isSafeInteger(requiredMajor) || requiredMajor < 1 || requiredMajor > 9999) {
571
+ throw new SystemdUnitError('invalid_options');
572
+ }
573
+ const version = await this.inspectManagerVersion();
574
+ if (version.major < requiredMajor) throw new SystemdUnitManagerTooOldError(version, requiredMajor);
575
+ return version;
576
+ }
577
+
517
578
  /**
518
579
  * Reports the loaded service settings that a definition renders, so an owner can verify an
519
580
  * installed unit carries them. Reads only; the manager must have reloaded changed files first.
581
+ * Needs systemd 254 or later, which first reports `FileDescriptorStorePreserve`: an older manager
582
+ * is refused with `SystemdUnitManagerTooOldError` before the unit is read, and so is a manager
583
+ * whose version is unreadable, with `version_unknown`.
520
584
  */
521
585
  public async inspectServiceSettings(): Promise<ISystemdServiceSettings> {
586
+ await this.requireManagerVersion(fileDescriptorStorePreserveMajor);
522
587
  const keys = ['Id', 'Type', 'NotifyAccess', 'Restart', 'RestartUSec', 'KillMode', 'TimeoutStopUSec',
523
588
  'TimeoutStartUSec', 'RestartPreventExitStatus', 'SuccessExitStatus', 'StartLimitIntervalUSec', 'StartLimitBurst',
524
589
  'Delegate', 'LimitNOFILE', 'LimitNOFILESoft', 'TasksMax', 'OOMScoreAdjust', 'FileDescriptorStoreMax',
@@ -530,7 +595,7 @@ export class SystemdUnit {
530
595
  if (separator < 1 || !keys.includes(key) || fields.has(key)) throw new SystemdUnitError('invalid_state');
531
596
  fields.set(key, line.slice(separator + 1));
532
597
  }
533
- // An older manager omits a property it does not know; the count check refuses that snapshot.
598
+ // A manager that omits a requested property it should know fails this count check.
534
599
  if (fields.size !== keys.length || fields.get('Id') !== this.unitName) throw new SystemdUnitError('invalid_state');
535
600
  const mode = (key: string, empty = false): string => {
536
601
  const value = fields.get(key)!;
@@ -596,8 +661,13 @@ export class SystemdUnit {
596
661
  * or that could not be run rejects with `command_failed`; so does one whose `systemctl` call
597
662
  * exceeded `commandTimeoutMs`, whose outcome is then unknown, so after `command_failed` a caller
598
663
  * must `inspect()` the unit before treating the store as kept.
664
+ *
665
+ * Needs systemd 254 or later, the first that cleans a store: the manager's version is read first,
666
+ * and an older manager is refused with `SystemdUnitManagerTooOldError`, an unreadable version with
667
+ * `version_unknown` and an unanswered read with `command_failed`, each before any clean is run.
599
668
  */
600
669
  public async cleanFileDescriptorStore(): Promise<ISystemdUnitState> {
670
+ await this.requireManagerVersion(fileDescriptorStoreCleanMajor);
601
671
  await this.command(['clean', '--what=fdstore', this.unitName]);
602
672
  try {
603
673
  return await this.inspect();
@@ -270,6 +270,11 @@ export class SystemdUnitFile {
270
270
  /**
271
271
  * Expected previous digest is mandatory; null admits an absent file. Exact desired bytes are idempotent.
272
272
  * Retains changed definitions beside the unit before replacement. A failed reload never rolls code back.
273
+ *
274
+ * A definition with a `requiredManagerMajor` is admitted only on a manager at least that new,
275
+ * read before any directory, file or other systemctl effect: an older manager rejects with
276
+ * the unit's `SystemdUnitManagerTooOldError` (`manager_too_old`), an unreadable version with
277
+ * `SystemdUnitError` `version_unknown` and a failed read with `SystemdUnitError` `command_failed`.
273
278
  */
274
279
  public async install(
275
280
  definition: SystemdServiceDefinition,
@@ -288,6 +293,10 @@ export class SystemdUnitFile {
288
293
  // instead of a wrapped write_failed.
289
294
  const maskDirectories = this.maskDirectories();
290
295
  if (maskDirectories === null) throw new SystemdUnitFileError('invalid_options');
296
+ // A manager older than the definition needs would load the unit without the newer directives.
297
+ if (definition.requiredManagerMajor !== null) {
298
+ await this.unit.requireManagerVersion(definition.requiredManagerMajor);
299
+ }
291
300
  requireValue(!this.busy, 'busy');
292
301
  this.busy = true;
293
302
  let temporary: string | undefined;
@@ -0,0 +1,38 @@
1
+ /** The version of a running systemd manager, as the manager itself reports it. */
2
+ export interface ISystemdManagerVersion {
3
+ /** systemd's major version: the leading number of `version`, `255` for `255.4-1ubuntu8.17`. */
4
+ readonly major: number;
5
+ /**
6
+ * The manager's `Version` property verbatim, as its build names it: `255.4-1ubuntu8.17`,
7
+ * `253.5-1`, `256~rc3` or `257.1-1.fc41`. It is the running manager's version, which differs
8
+ * from `systemctl --version` after an upgrade until the manager re-executes.
9
+ */
10
+ readonly version: string;
11
+ }
12
+
13
+ /** Internal: the systemd major that first reads `FileDescriptorStorePreserve=` and reports it. */
14
+ export const fileDescriptorStorePreserveMajor = 254;
15
+
16
+ /** Internal: the systemd major whose `systemctl clean` first releases a file descriptor store (`--what=fdstore`). */
17
+ export const fileDescriptorStoreCleanMajor = 254;
18
+
19
+ /**
20
+ * Internal: the systemd major whose `systemd-run` first takes `--expand-environment` and reports a
21
+ * started scope as `runInTransientScope()` reads it.
22
+ */
23
+ export const transientScopeMajor = 254;
24
+
25
+ /** Internal: the `systemctl` verb and argument that print the manager's `Version` property. */
26
+ export const managerVersionArguments: readonly string[] = ['show', '--property=Version'];
27
+
28
+ /**
29
+ * Internal: parses the output of `systemctl show --property=Version`, exactly one
30
+ * `Version=<version>` line. The version is a major of 1–4 digits without a leading zero,
31
+ * optionally followed by a separator (`.`, `~`, `-`, `+`, `^` or `_`) and printable ASCII, at
32
+ * most 256 characters in all. Anything else, `systemctl --version`'s `systemd 255 (255.4-1)`
33
+ * format included, is refused with undefined.
34
+ */
35
+ export const parseManagerVersion = (stdout: string): ISystemdManagerVersion | undefined => {
36
+ const match = /^Version=(([1-9][0-9]{0,3})(?:[.~+^_-][\x21-\x7e]{0,251})?)\n$/.exec(stdout);
37
+ return match ? Object.freeze({ major: Number(match[2]), version: match[1] }) : undefined;
38
+ };