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