]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/install.h
7a5859e72950966c76f2e67cca630d59fbad72d3
[thirdparty/systemd.git] / src / shared / install.h
1 #pragma once
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2011 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 typedef enum UnitFileScope UnitFileScope;
23 typedef enum UnitFileState UnitFileState;
24 typedef enum UnitFilePresetMode UnitFilePresetMode;
25 typedef enum UnitFileChangeType UnitFileChangeType;
26 typedef enum UnitFileFlags UnitFileFlags;
27 typedef enum UnitFileType UnitFileType;
28 typedef struct UnitFileChange UnitFileChange;
29 typedef struct UnitFileList UnitFileList;
30 typedef struct UnitFileInstallInfo UnitFileInstallInfo;
31
32 #include <stdbool.h>
33
34 #include "hashmap.h"
35 #include "macro.h"
36 #include "path-lookup.h"
37 #include "strv.h"
38 #include "unit-name.h"
39
40 enum UnitFileScope {
41 UNIT_FILE_SYSTEM,
42 UNIT_FILE_GLOBAL,
43 UNIT_FILE_USER,
44 _UNIT_FILE_SCOPE_MAX,
45 _UNIT_FILE_SCOPE_INVALID = -1
46 };
47
48 enum UnitFileState {
49 UNIT_FILE_ENABLED,
50 UNIT_FILE_ENABLED_RUNTIME,
51 UNIT_FILE_LINKED,
52 UNIT_FILE_LINKED_RUNTIME,
53 UNIT_FILE_MASKED,
54 UNIT_FILE_MASKED_RUNTIME,
55 UNIT_FILE_STATIC,
56 UNIT_FILE_DISABLED,
57 UNIT_FILE_INDIRECT,
58 UNIT_FILE_GENERATED,
59 UNIT_FILE_TRANSIENT,
60 UNIT_FILE_BAD,
61 _UNIT_FILE_STATE_MAX,
62 _UNIT_FILE_STATE_INVALID = -1
63 };
64
65 enum UnitFilePresetMode {
66 UNIT_FILE_PRESET_FULL,
67 UNIT_FILE_PRESET_ENABLE_ONLY,
68 UNIT_FILE_PRESET_DISABLE_ONLY,
69 _UNIT_FILE_PRESET_MAX,
70 _UNIT_FILE_PRESET_INVALID = -1
71 };
72
73 enum UnitFileChangeType {
74 UNIT_FILE_SYMLINK,
75 UNIT_FILE_UNLINK,
76 UNIT_FILE_IS_MASKED,
77 UNIT_FILE_IS_DANGLING,
78 _UNIT_FILE_CHANGE_TYPE_MAX,
79 _UNIT_FILE_CHANGE_INVALID = INT_MIN
80 };
81
82 enum UnitFileFlags {
83 UNIT_FILE_RUNTIME = 1,
84 UNIT_FILE_FORCE = 1 << 1,
85 UNIT_FILE_DRY_RUN = 1 << 2,
86 };
87
88 /* type can either one of the UnitFileChangeTypes listed above, or a negative error.
89 * If source is specified, it should be the contents of the path symlink.
90 * In case of an error, source should be the existing symlink contents or NULL
91 */
92 struct UnitFileChange {
93 int type; /* UnitFileChangeType or bust */
94 char *path;
95 char *source;
96 };
97
98 static inline bool unit_file_changes_have_modification(const UnitFileChange* changes, unsigned n_changes) {
99 unsigned i;
100 for (i = 0; i < n_changes; i++)
101 if (IN_SET(changes[i].type, UNIT_FILE_SYMLINK, UNIT_FILE_UNLINK))
102 return true;
103 return false;
104 }
105
106 struct UnitFileList {
107 char *path;
108 UnitFileState state;
109 };
110
111 enum UnitFileType {
112 UNIT_FILE_TYPE_REGULAR,
113 UNIT_FILE_TYPE_SYMLINK,
114 UNIT_FILE_TYPE_MASKED,
115 _UNIT_FILE_TYPE_MAX,
116 _UNIT_FILE_TYPE_INVALID = -1,
117 };
118
119 struct UnitFileInstallInfo {
120 char *name;
121 char *path;
122
123 char **aliases;
124 char **wanted_by;
125 char **required_by;
126 char **also;
127
128 char *default_instance;
129 char *symlink_target;
130
131 UnitFileType type;
132 bool auxiliary;
133 };
134
135 static inline bool UNIT_FILE_INSTALL_INFO_HAS_RULES(UnitFileInstallInfo *i) {
136 assert(i);
137
138 return !strv_isempty(i->aliases) ||
139 !strv_isempty(i->wanted_by) ||
140 !strv_isempty(i->required_by);
141 }
142
143 static inline bool UNIT_FILE_INSTALL_INFO_HAS_ALSO(UnitFileInstallInfo *i) {
144 assert(i);
145
146 return !strv_isempty(i->also);
147 }
148
149 bool unit_type_may_alias(UnitType type) _const_;
150 bool unit_type_may_template(UnitType type) _const_;
151
152 int unit_file_enable(
153 UnitFileScope scope,
154 UnitFileFlags flags,
155 const char *root_dir,
156 char **files,
157 UnitFileChange **changes,
158 unsigned *n_changes);
159 int unit_file_disable(
160 UnitFileScope scope,
161 UnitFileFlags flags,
162 const char *root_dir,
163 char **files,
164 UnitFileChange **changes,
165 unsigned *n_changes);
166 int unit_file_reenable(
167 UnitFileScope scope,
168 UnitFileFlags flags,
169 const char *root_dir,
170 char **files,
171 UnitFileChange **changes,
172 unsigned *n_changes);
173 int unit_file_preset(
174 UnitFileScope scope,
175 UnitFileFlags flags,
176 const char *root_dir,
177 char **files,
178 UnitFilePresetMode mode,
179 UnitFileChange **changes,
180 unsigned *n_changes);
181 int unit_file_preset_all(
182 UnitFileScope scope,
183 UnitFileFlags flags,
184 const char *root_dir,
185 UnitFilePresetMode mode,
186 UnitFileChange **changes,
187 unsigned *n_changes);
188 int unit_file_mask(
189 UnitFileScope scope,
190 UnitFileFlags flags,
191 const char *root_dir,
192 char **files,
193 UnitFileChange **changes,
194 unsigned *n_changes);
195 int unit_file_unmask(
196 UnitFileScope scope,
197 UnitFileFlags flags,
198 const char *root_dir,
199 char **files,
200 UnitFileChange **changes,
201 unsigned *n_changes);
202 int unit_file_link(
203 UnitFileScope scope,
204 UnitFileFlags flags,
205 const char *root_dir,
206 char **files,
207 UnitFileChange **changes,
208 unsigned *n_changes);
209 int unit_file_revert(
210 UnitFileScope scope,
211 const char *root_dir,
212 char **files,
213 UnitFileChange **changes,
214 unsigned *n_changes);
215 int unit_file_set_default(
216 UnitFileScope scope,
217 UnitFileFlags flags,
218 const char *root_dir,
219 const char *file,
220 UnitFileChange **changes,
221 unsigned *n_changes);
222 int unit_file_get_default(
223 UnitFileScope scope,
224 const char *root_dir,
225 char **name);
226 int unit_file_add_dependency(
227 UnitFileScope scope,
228 UnitFileFlags flags,
229 const char *root_dir,
230 char **files,
231 const char *target,
232 UnitDependency dep,
233 UnitFileChange **changes,
234 unsigned *n_changes);
235
236 int unit_file_get_state(UnitFileScope scope, const char *root_dir, const char *filename, UnitFileState *ret);
237 int unit_file_exists(UnitFileScope scope, const LookupPaths *paths, const char *name);
238
239 int unit_file_get_list(UnitFileScope scope, const char *root_dir, Hashmap *h, char **states, char **patterns);
240 Hashmap* unit_file_list_free(Hashmap *h);
241
242 int unit_file_changes_add(UnitFileChange **changes, unsigned *n_changes, UnitFileChangeType type, const char *path, const char *source);
243 void unit_file_changes_free(UnitFileChange *changes, unsigned n_changes);
244 void unit_file_dump_changes(int r, const char *verb, const UnitFileChange *changes, unsigned n_changes, bool quiet);
245
246 int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char *name);
247
248 const char *unit_file_state_to_string(UnitFileState s) _const_;
249 UnitFileState unit_file_state_from_string(const char *s) _pure_;
250 /* from_string conversion is unreliable because of the overlap between -EPERM and -1 for error. */
251
252 const char *unit_file_change_type_to_string(UnitFileChangeType s) _const_;
253 UnitFileChangeType unit_file_change_type_from_string(const char *s) _pure_;
254
255 const char *unit_file_preset_mode_to_string(UnitFilePresetMode m) _const_;
256 UnitFilePresetMode unit_file_preset_mode_from_string(const char *s) _pure_;