systemd_zbus/
types.rs

1use serde::{Deserialize, Serialize};
2use zbus::zvariant::{OwnedObjectPath, OwnedValue, Type, Value};
3
4use crate::{enum_impl_serde_str, enum_impl_str_conv, impl_value_conversions_as_str};
5
6#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Type)]
7pub enum UnitType {
8    Service,
9    Mount,
10    Swap,
11    Socket,
12    Target,
13    Device,
14    Automount,
15    Timer,
16    Path,
17    Slice,
18    Scope,
19}
20
21enum_impl_str_conv!(UnitType, {
22    "service": Service,
23    "mount": Mount,
24    "swap": Swap,
25    "socket": Socket,
26    "target": Target,
27    "device": Device,
28    "automount": Automount,
29    "timer": Timer,
30    "path": Path,
31    "slice": Slice,
32    "scope": Scope,
33});
34enum_impl_serde_str!(UnitType);
35impl_value_conversions_as_str!(UnitType);
36
37#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Type, Serialize, Deserialize)]
38pub enum UnitFileFlags {
39    /// Will enable or disable the unit for runtime only.
40    ///
41    /// Defined as `#define SD_SYSTEMD_UNIT_RUNTIME  (UINT64_C(1) << 0)`
42    Runtime,
43    /// Controls whether symlinks pointing to other units shall be replaced if necessary.
44    ///
45    /// Defined as `#define SD_SYSTEMD_UNIT_FORCE    (UINT64_C(1) << 1)`
46    Force,
47    /// Will add or remove the symlinks in `/etc/systemd/system.attached` and `/run/systemd/system.attached`.
48    ///
49    /// Defined as `#define SD_SYSTEMD_UNIT_PORTABLE (UINT64_C(1) << 2)`
50    Portable,
51}
52
53#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Type)]
54#[zvariant(signature = "s")]
55pub enum Mode {
56    Replace,
57    Fail,
58    Isolate,
59    IgnoreDependencies,
60    IgnoreRequirements,
61}
62
63enum_impl_str_conv!(Mode, {
64    "replace": Replace,
65    "fail": Fail,
66    "isolate": Isolate,
67    "ignore-dependencies": IgnoreDependencies,
68    "ignore-requirements": IgnoreRequirements,
69});
70enum_impl_serde_str!(Mode);
71impl_value_conversions_as_str!(Mode);
72
73#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Type)]
74#[zvariant(signature = "s")]
75pub enum LoadState {
76    Stub,
77    Loaded,
78    NotFound,
79    BadSetting,
80    Error,
81    Merged,
82    Masked,
83}
84
85enum_impl_str_conv!(LoadState, {
86    "stub": Stub,
87    "loaded": Loaded,
88    "not-found": NotFound,
89    "bad-setting": BadSetting,
90    "error": Error,
91    "merged": Merged,
92    "masked": Masked,
93});
94enum_impl_serde_str!(LoadState);
95impl_value_conversions_as_str!(LoadState);
96
97#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Type)]
98#[zvariant(signature = "s")]
99pub enum ActiveState {
100    Active,
101    Reloading,
102    Inactive,
103    Failed,
104    Activating,
105    Deactivating,
106    Maintenance,
107}
108
109enum_impl_str_conv!(ActiveState, {
110    "active": Active,
111    "reloading": Reloading,
112    "inactive": Inactive,
113    "failed": Failed,
114    "activating": Activating,
115    "deactivating": Deactivating,
116    "maintenance" : Maintenance,
117});
118enum_impl_serde_str!(ActiveState);
119impl_value_conversions_as_str!(ActiveState);
120
121#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Type)]
122#[zvariant(signature = "s")]
123pub enum UnitFileState {
124    Enabled,
125    EnabledRuntime,
126    Linked,
127    LinkedRuntime,
128    Masked,
129    MaskedRuntime,
130    Static,
131    Disabled,
132    Invalid,
133}
134
135enum_impl_str_conv!(UnitFileState, {
136    "enabled": Enabled,
137    "enabled-runtime": EnabledRuntime,
138    "linked":  Linked,
139    "linked-runtime":  LinkedRuntime,
140    "masked":  Masked,
141    "masked-runtime":  MaskedRuntime,
142    "static": Static,
143    "disabled": Disabled,
144    "invalid":  Invalid,
145
146});
147enum_impl_serde_str!(UnitFileState);
148impl_value_conversions_as_str!(UnitFileState);
149
150/// The `SubState` of a unit is specific to the unit type, it is best to run
151/// ```ignore
152/// systemctl --state=help
153/// ```
154/// to get a list of substate groups. Many states are in multiple groups.
155#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Type)]
156#[zvariant(signature = "s")]
157pub enum SubState {
158    Dead,
159    Waiting,
160    Running,
161    Failed,
162    Tentative,
163    Plugged,
164    Mounting,
165    MountingDone,
166    Mounted,
167    Remounting,
168    Unmounting,
169    RemountingSigterm,
170    RemountingSigkill,
171    UnmountingSigterm,
172    UnmountingSigkill,
173    Cleaning,
174    Abandoned,
175    StopSigterm,
176    StopSigkill,
177    Condition,
178    StartPre,
179    Start,
180    StartPost,
181    Exited,
182    Reload,
183    Stop,
184    StopWatchdog,
185    StopPost,
186    FinalWatchdog,
187    FinalSigterm,
188    FinalSigkill,
189    AutoRestart,
190    AutoRestartQueued,
191    Active,
192    StartChown,
193    Listening,
194    StopPre,
195    StopPreSigterm,
196    StopPreSigkill,
197    Activating,
198    ActivatingDone,
199    Deactivating,
200    DeactivatingSigterm,
201    DeactivatingSigkill,
202    Elapsed,
203}
204
205enum_impl_str_conv!(SubState, {
206    "dead": Dead,
207    "waiting": Waiting,
208    "running": Running,
209    "failed": Failed,
210    "tentative": Tentative,
211    "plugged": Plugged,
212    "mounting": Mounting,
213    "mounting-done": MountingDone,
214    "mounted": Mounted,
215    "remounting": Remounting,
216    "unmounting": Unmounting,
217    "remounting-sigterm": RemountingSigterm,
218    "remounting-sigkill": RemountingSigkill,
219    "unmounting-sigterm": UnmountingSigterm,
220    "unmounting-sigkill": UnmountingSigkill,
221    "cleaning": Cleaning,
222    "abandoned": Abandoned,
223    "stop-sigterm": StopSigterm,
224    "stop-sigkill": StopSigkill,
225    "condition": Condition,
226    "start-pre": StartPre,
227    "start": Start,
228    "start-post": StartPost,
229    "exited": Exited,
230    "reload": Reload,
231    "stop": Stop,
232    "stop-watchdog": StopWatchdog,
233    "stop-post": StopPost,
234    "final-watchdog": FinalWatchdog,
235    "final-sigterm": FinalSigterm,
236    "final-sigkill": FinalSigkill,
237    "auto-restart": AutoRestart,
238    "auto-restart-queued": AutoRestartQueued,
239    "active": Active,
240    "start-chown": StartChown,
241    "listening": Listening,
242    "stop-pre": StopPre,
243    "stop-pre-sigterm": StopPreSigterm,
244    "stop-pre-sigkill": StopPreSigkill,
245    "activating": Activating,
246    "activating-done": ActivatingDone,
247    "deactivating": Deactivating,
248    "deactivating-sigterm": DeactivatingSigterm,
249    "deactivating-sigkill": DeactivatingSigkill,
250    "elapsed": Elapsed,
251});
252enum_impl_serde_str!(SubState);
253impl_value_conversions_as_str!(SubState);
254
255#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize)]
256pub struct Unit {
257    /// The primary name
258    pub name: String,
259    /// The human readable description string
260    pub description: String,
261    /// The load state (i.e. whether the unit file has been loaded successfully)
262    pub load: LoadState,
263    /// The active state (i.e. whether the unit is currently started or not)
264    pub active: ActiveState,
265    /// The sub state (a more fine-grained version of the active state that is specific to the unit type, which the active state is not)
266    pub sub_state: SubState,
267    /// A unit that is being followed in its state by this unit, if there is any, otherwise the empty string.
268    pub followed_unit: String,
269    /// The unit object path
270    pub path: OwnedObjectPath,
271    /// If there is a job queued for the job unit, the numeric job id, 0 otherwise
272    pub queued_job: u32,
273    /// The job type as string
274    pub job_type: String,
275    /// The job object path
276    pub job_path: OwnedObjectPath,
277}
278
279#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize)]
280pub struct Job {
281    /// The numeric job id
282    pub job_id: u32,
283    /// The primary unit name for this job
284    pub unit_name: String,
285    /// The job type
286    pub job_type: JobType,
287    /// The job state
288    pub job_state: JobState,
289    /// The job object path
290    pub job_path: OwnedObjectPath,
291    /// The job unit path
292    pub unit_path: OwnedObjectPath,
293}
294
295/// The type of a job.
296#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Type)]
297#[zvariant(signature = "s")]
298pub enum JobType {
299    Start,
300    VerifyActive,
301    Stop,
302    Reload,
303    Restart,
304    TryRestart,
305    ReloadOrStart,
306}
307enum_impl_str_conv!(JobType, {
308    "start": Start,
309    "verify-active": VerifyActive,
310    "stop": Stop,
311    "reload": Reload,
312    "restart": Restart,
313    "try-restart": TryRestart,
314    "reload-or-start": ReloadOrStart,
315});
316enum_impl_serde_str!(JobType);
317impl_value_conversions_as_str!(JobType);
318
319/// The state of a job.
320#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Type)]
321#[zvariant(signature = "s")]
322pub enum JobState {
323    /// The job is waiting for another job to complete.
324    Waiting,
325
326    /// The job is running.
327    Running,
328}
329enum_impl_str_conv!(JobState, {
330    "waiting": Waiting,
331    "running": Running,
332});
333enum_impl_serde_str!(JobState);
334impl_value_conversions_as_str!(JobState);
335
336#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize)]
337pub struct EnquedJob {
338    /// The numeric job id
339    pub job_id: u32,
340    /// The job object path
341    pub job_path: OwnedObjectPath,
342    /// The primary unit name for this job
343    pub unit_name: String,
344    /// The job unit path
345    pub unit_path: OwnedObjectPath,
346    /// The job type
347    pub job_type: String,
348}
349
350#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize)]
351pub struct EnqueJob {
352    pub job: EnquedJob,
353    pub affected_jobs: Vec<EnquedJob>,
354}
355
356#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize, Value, OwnedValue)]
357pub struct KeyValue<K, V>
358where
359    K: Type,
360    V: Type,
361{
362    /// The key.
363    pub key: K,
364
365    /// The value.
366    pub value: V,
367}
368
369#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Type)]
370#[zvariant(signature = "s")]
371pub enum ChangeType {
372    Symlink,
373    Unlink,
374}
375
376enum_impl_str_conv!(ChangeType, {
377    "symlink": Symlink,
378    "unlink": Unlink,
379});
380enum_impl_serde_str!(ChangeType);
381impl_value_conversions_as_str!(ChangeType);
382
383#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize)]
384pub struct Change {
385    change_type: ChangeType,
386    /// Filename of the symlink
387    symlink_file: String,
388    /// Destination of the symlink
389    symlink_dest: String,
390}
391
392/// Unused: `condition` in `PathWatch`, temporarily `String` only
393#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Type)]
394#[zvariant(signature = "s")]
395pub enum PathWatchCondition {
396    Exists,
397    ExistsGlob,
398    Changed,
399    Modified,
400    DirectoryNotEmpty,
401}
402enum_impl_str_conv!(PathWatchCondition, {
403    "PathExists": Exists,
404    "PathExistsGlob": ExistsGlob,
405    "PathChanged": Changed,
406    "PathModified": Modified,
407    "PathDirectoryNotEmpty": DirectoryNotEmpty,
408});
409enum_impl_serde_str!(PathWatchCondition);
410impl_value_conversions_as_str!(PathWatchCondition);
411
412/// A path watch specification for a Path unit.
413#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize, Value, OwnedValue)]
414pub struct PathWatch {
415    /// The condition to watch for.
416    pub condition: String, // TODO: PathWatchCondition,
417
418    /// The path to watch.
419    ///
420    /// If the condition is [`ExistsGlob`][PathWatchCondition::ExistsGlob],
421    /// the path is a glob pattern.
422    pub path: String,
423}
424
425/// An exec command, augmented with runtime data.
426#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize, Value, OwnedValue)]
427pub struct Exec {
428    /// The binary to run.
429    pub binary_path: String,
430
431    /// The arguments to the binary, including `argv[0]`.
432    pub arguments: Vec<String>,
433
434    /// If true, it is considered a failure if the process exits uncleanly.
435    pub is_failure: bool,
436
437    /// The last start time of the process in microseconds on the realtime clock, or 0 if it was never started yet.
438    pub last_start_realtime_us: u64,
439
440    /// The last start time of the process in microseconds on the monotonic clock, or 0 if it was never started yet.
441    pub last_start_monotonic_us: u64,
442
443    /// The last exit time of the process in microseconds on the realtime clock, or 0 if it never finished yet.
444    pub last_exit_realtime_us: u64,
445
446    /// The last exit time of the process in microseconds on the monotonic clock, or 0 if it never finished yet.
447    pub last_exit_monotonic_us: u64,
448
449    /// The PID of the process, or 0 if it was never started yet.
450    pub pid: u32,
451
452    /// The last exit code of the process.
453    pub last_exit_code: i32,
454
455    /// The last status of the process.
456    pub last_status: i32,
457}
458
459/// An exec command, augmented with runtime data.
460#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize, Value, OwnedValue)]
461pub struct ExecEx {
462    /// The binary to run.
463    pub binary_path: String,
464
465    /// The arguments to the binary, including `argv[0]`.
466    pub arguments: Vec<String>,
467
468    /// List of options.
469    ///
470    /// This can include the following options:
471    ///   "ignore-failure"
472    ///   "privileged"
473    ///   "no-setuid"
474    ///   "ambient"
475    ///   "no-env-expand"
476    ///
477    /// More options may be added by systemd in future releases.
478    pub options: Vec<String>,
479
480    /// The last start time of the process in microseconds on the realtime clock, or 0 if it was never started yet.
481    pub last_start_realtime_us: u64,
482
483    /// The last start time of the process in microseconds on the monotonic clock, or 0 if it was never started yet.
484    pub last_start_monotonic_us: u64,
485
486    /// The last exit time of the process in microseconds on the realtime clock, or 0 if it never finished yet.
487    pub last_exit_realtime_us: u64,
488
489    /// The last finexitme of the process in microseconds on the monotonic clock, or 0 if it never finished yet.
490    pub last_exit_monotonic_us: u64,
491
492    /// The PID of the process, or 0 if it was never started yet.
493    pub pid: u32,
494
495    /// The last exit code of the process.
496    pub last_exit_code: i32,
497
498    /// The last status of the process.
499    pub last_status: i32,
500}
501
502/// Unused: A realtime calendar specification. `base` in `TimerCalendar`, temporarily `String` only
503#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize, Value, OwnedValue)]
504pub struct TimerCalendar {
505    /// The timer base.
506    pub base: String, // TODO: TimerCalendarBase,
507
508    /// The calendar specification string.
509    pub calendar_spec: String,
510
511    /// The next elapsation point on the realtime clock.
512    pub next_elapse_realtime_usec: u64,
513}
514
515/// A calender timer base.
516#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Type)]
517#[zvariant(signature = "s")]
518pub enum TimerCalendarBase {
519    OnCalendar,
520}
521enum_impl_str_conv!(TimerCalendarBase, {
522    "OnCalendar": OnCalendar,
523});
524enum_impl_serde_str!(TimerCalendarBase);
525impl_value_conversions_as_str!(TimerCalendarBase);
526
527/// A realtime calendar specification.
528#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize, Value, OwnedValue)]
529pub struct TimerMonotonic {
530    /// The timer base.
531    pub base: String, // TODO: TimerMonotonicBase,
532
533    /// The timer offset relative to the timer base.
534    pub offset_usec: u64,
535
536    /// The next elapsation point on the realtime clock.
537    pub next_elapse_realtime_usec: u64,
538}
539
540/// Unused: A monotonic timer base. `base` in `TimerMonotonic`, temporarily `String` only
541#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Type)]
542#[zvariant(signature = "s")]
543pub enum TimerMonotonicBase {
544    OnActiveUSec,
545    OnBootUSec,
546    OnStartupUSec,
547    OnUnitActiveUSec,
548    OnUnitInactiveUSec,
549}
550enum_impl_str_conv!(TimerMonotonicBase, {
551    "OnActiveUSec": OnActiveUSec,
552    "OnBootUSec": OnBootUSec,
553    "OnStartupUSec": OnStartupUSec,
554    "OnUnitActiveUSec": OnUnitActiveUSec,
555    "OnUnitInactiveUSec": OnUnitInactiveUSec,
556});
557enum_impl_serde_str!(TimerMonotonicBase);
558impl_value_conversions_as_str!(TimerMonotonicBase);
559
560/// The bind mount is recursive.
561pub const BIND_MOUNT_RECURSIVE: u64 = 0x4000;
562
563#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize, Value, OwnedValue)]
564pub struct BindMount {
565    /// The source folder of the bind mount.
566    pub source: String,
567
568    /// The destination folder of the bind mount.
569    pub destination: String,
570
571    /// Ignore the bind mount if the source folder does not exist.
572    pub ignore_non_existing: bool,
573
574    /// Additional options for the bind mount as bitmask.
575    ///
576    /// Currently only the [`BIND_MOUNT_RECURSIVE`] option exists.
577    pub options: u64,
578}
579
580/// A process spawned by systemd for a unit.
581#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize, Value, OwnedValue)]
582pub struct Process {
583    /// The cgroup controller of the process.
584    pub cgroup_controller: String,
585
586    /// The PID of the process.
587    pub pid: u32,
588
589    /// The command line of the process.
590    pub command_line: String,
591}
592
593/// A symbolic link created for a runtime,state, cache or log directort directory.
594#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize, Value, OwnedValue)]
595pub struct DirectorySymlink {
596    /// The path of the symbolic link target.
597    pub target_path: String,
598
599    /// The path of the symbolic link.
600    pub symlink_path: String,
601
602    /// Flags, currently unused.
603    pub flags: u64,
604}
605
606/// Definition of mount image.
607#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize, Value, OwnedValue)]
608pub struct MountImage {
609    /// The path of the image or block device.
610    pub image_path: String,
611
612    /// The mount point for the root filesystem of the image.
613    pub mount_point: String,
614
615    /// Ignore the extension image if the path does not exist.
616    pub ignore_non_existing: bool,
617
618    /// Mount options for the image.
619    pub mount_options: Vec<PartitionMountOptions>,
620}
621
622/// Mount options for a partition from an image.
623#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize, Value, OwnedValue)]
624pub struct PartitionMountOptions {
625    /// The partition to apply the mount options to.
626    pub partition: String,
627
628    /// The mount options.
629    pub mount_options: String,
630}
631
632/// Definition of an extension image.
633#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize, Value, OwnedValue)]
634pub struct ExtensionImage {
635    /// The path of the image or block device.
636    pub image_path: String,
637
638    /// Ignore the extension image if the path does not exist.
639    pub ignore_non_existing: bool,
640
641    /// Mount options for the image.
642    pub mount_options: Vec<PartitionMountOptions>,
643}