1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <stdbool.h>
5
6 typedef enum UnitFilePresetMode UnitFilePresetMode;
7 typedef enum UnitFileChangeType UnitFileChangeType;
8 typedef enum UnitFileFlags UnitFileFlags;
9 typedef enum UnitFileType UnitFileType;
10 typedef struct UnitFileChange UnitFileChange;
11 typedef struct UnitFileList UnitFileList;
12 typedef struct UnitFileInstallInfo UnitFileInstallInfo;
13
14 #include "hashmap.h"
15 #include "macro.h"
16 #include "path-lookup.h"
17 #include "strv.h"
18 #include "unit-file.h"
19 #include "unit-name.h"
20
21 enum UnitFilePresetMode {
22 UNIT_FILE_PRESET_FULL,
23 UNIT_FILE_PRESET_ENABLE_ONLY,
24 UNIT_FILE_PRESET_DISABLE_ONLY,
25 _UNIT_FILE_PRESET_MAX,
26 _UNIT_FILE_PRESET_INVALID = -EINVAL,
27 };
28
29 /* This enum type is anonymous, since we usually store it in an 'int', as we overload it with negative errno
30 * values. */
31 enum {
32 UNIT_FILE_SYMLINK,
33 UNIT_FILE_UNLINK,
34 UNIT_FILE_IS_MASKED,
35 UNIT_FILE_IS_DANGLING,
36 UNIT_FILE_DESTINATION_NOT_PRESENT,
37 UNIT_FILE_AUXILIARY_FAILED,
38 _UNIT_FILE_CHANGE_TYPE_MAX,
39 _UNIT_FILE_CHANGE_TYPE_INVALID = -EINVAL,
40 };
41
42 enum UnitFileFlags {
43 UNIT_FILE_RUNTIME = 1 << 0, /* Public API via DBUS, do not change */
44 UNIT_FILE_FORCE = 1 << 1, /* Public API via DBUS, do not change */
45 UNIT_FILE_PORTABLE = 1 << 2, /* Public API via DBUS, do not change */
46 UNIT_FILE_DRY_RUN = 1 << 3,
47 UNIT_FILE_IGNORE_AUXILIARY_FAILURE = 1 << 4,
48 _UNIT_FILE_FLAGS_MASK_PUBLIC = UNIT_FILE_RUNTIME|UNIT_FILE_PORTABLE|UNIT_FILE_FORCE,
49 };
50
51 /* type can either one of the UNIT_FILE_SYMLINK, UNIT_FILE_UNLINK, … listed above, or a negative errno value.
52 * If source is specified, it should be the contents of the path symlink. In case of an error, source should
53 * be the existing symlink contents or NULL. */
54 struct UnitFileChange {
55 int type_or_errno; /* UNIT_FILE_SYMLINK, … if positive, errno if negative */
56 char *path;
57 char *source;
58 };
59
unit_file_changes_have_modification(const UnitFileChange * changes,size_t n_changes)60 static inline bool unit_file_changes_have_modification(const UnitFileChange* changes, size_t n_changes) {
61 for (size_t i = 0; i < n_changes; i++)
62 if (IN_SET(changes[i].type_or_errno, UNIT_FILE_SYMLINK, UNIT_FILE_UNLINK))
63 return true;
64 return false;
65 }
66
67 struct UnitFileList {
68 char *path;
69 UnitFileState state;
70 };
71
72 enum UnitFileType {
73 UNIT_FILE_TYPE_REGULAR,
74 UNIT_FILE_TYPE_LINKED,
75 UNIT_FILE_TYPE_ALIAS,
76 UNIT_FILE_TYPE_MASKED,
77 _UNIT_FILE_TYPE_MAX,
78 _UNIT_FILE_TYPE_INVALID = -EINVAL,
79 };
80
81 struct UnitFileInstallInfo {
82 char *name;
83 char *path;
84 char *root;
85
86 char **aliases;
87 char **wanted_by;
88 char **required_by;
89 char **also;
90
91 char *default_instance;
92 char *symlink_target;
93
94 UnitFileType type;
95 bool auxiliary;
96 };
97
98 int unit_file_enable(
99 LookupScope scope,
100 UnitFileFlags flags,
101 const char *root_dir,
102 char **files,
103 UnitFileChange **changes,
104 size_t *n_changes);
105 int unit_file_disable(
106 LookupScope scope,
107 UnitFileFlags flags,
108 const char *root_dir,
109 char **files,
110 UnitFileChange **changes,
111 size_t *n_changes);
112 int unit_file_reenable(
113 LookupScope scope,
114 UnitFileFlags flags,
115 const char *root_dir,
116 char **names_or_paths,
117 UnitFileChange **changes,
118 size_t *n_changes);
119 int unit_file_preset(
120 LookupScope scope,
121 UnitFileFlags flags,
122 const char *root_dir,
123 char **files,
124 UnitFilePresetMode mode,
125 UnitFileChange **changes,
126 size_t *n_changes);
127 int unit_file_preset_all(
128 LookupScope scope,
129 UnitFileFlags flags,
130 const char *root_dir,
131 UnitFilePresetMode mode,
132 UnitFileChange **changes,
133 size_t *n_changes);
134 int unit_file_mask(
135 LookupScope scope,
136 UnitFileFlags flags,
137 const char *root_dir,
138 char **files,
139 UnitFileChange **changes,
140 size_t *n_changes);
141 int unit_file_unmask(
142 LookupScope scope,
143 UnitFileFlags flags,
144 const char *root_dir,
145 char **files,
146 UnitFileChange **changes,
147 size_t *n_changes);
148 int unit_file_link(
149 LookupScope scope,
150 UnitFileFlags flags,
151 const char *root_dir,
152 char **files,
153 UnitFileChange **changes,
154 size_t *n_changes);
155 int unit_file_revert(
156 LookupScope scope,
157 const char *root_dir,
158 char **files,
159 UnitFileChange **changes,
160 size_t *n_changes);
161 int unit_file_set_default(
162 LookupScope scope,
163 UnitFileFlags flags,
164 const char *root_dir,
165 const char *file,
166 UnitFileChange **changes,
167 size_t *n_changes);
168 int unit_file_get_default(
169 LookupScope scope,
170 const char *root_dir,
171 char **name);
172 int unit_file_add_dependency(
173 LookupScope scope,
174 UnitFileFlags flags,
175 const char *root_dir,
176 char **files,
177 const char *target,
178 UnitDependency dep,
179 UnitFileChange **changes,
180 size_t *n_changes);
181
182 int unit_file_lookup_state(
183 LookupScope scope,
184 const LookupPaths *paths,
185 const char *name,
186 UnitFileState *ret);
187
188 int unit_file_get_state(LookupScope scope, const char *root_dir, const char *filename, UnitFileState *ret);
189 int unit_file_exists(LookupScope scope, const LookupPaths *paths, const char *name);
190
191 int unit_file_get_list(LookupScope scope, const char *root_dir, Hashmap *h, char **states, char **patterns);
192 Hashmap* unit_file_list_free(Hashmap *h);
193
194 int unit_file_changes_add(UnitFileChange **changes, size_t *n_changes, int type, const char *path, const char *source);
195 void unit_file_changes_free(UnitFileChange *changes, size_t n_changes);
196 void unit_file_dump_changes(int r, const char *verb, const UnitFileChange *changes, size_t n_changes, bool quiet);
197
198 int unit_file_verify_alias(
199 const UnitFileInstallInfo *info,
200 const char *dst,
201 char **ret_dst,
202 UnitFileChange **changes,
203 size_t *n_changes);
204
205 typedef struct UnitFilePresetRule UnitFilePresetRule;
206
207 typedef struct {
208 UnitFilePresetRule *rules;
209 size_t n_rules;
210 bool initialized;
211 } UnitFilePresets;
212
213 void unit_file_presets_freep(UnitFilePresets *p);
214 int unit_file_query_preset(LookupScope scope, const char *root_dir, const char *name, UnitFilePresets *cached);
215
216 const char *unit_file_state_to_string(UnitFileState s) _const_;
217 UnitFileState unit_file_state_from_string(const char *s) _pure_;
218 /* from_string conversion is unreliable because of the overlap between -EPERM and -1 for error. */
219
220 const char *unit_file_change_type_to_string(int s) _const_;
221 int unit_file_change_type_from_string(const char *s) _pure_;
222
223 const char *unit_file_preset_mode_to_string(UnitFilePresetMode m) _const_;
224 UnitFilePresetMode unit_file_preset_mode_from_string(const char *s) _pure_;
225