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