]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
5008d581 | 2 | |
5008d581 | 3 | #include <errno.h> |
3f6fd1ba LP |
4 | #include <fcntl.h> |
5 | #include <fnmatch.h> | |
6 | #include <getopt.h> | |
5008d581 | 7 | #include <limits.h> |
3f6fd1ba LP |
8 | #include <linux/fs.h> |
9 | #include <stdbool.h> | |
10 | #include <stddef.h> | |
3b63d2d3 | 11 | #include <stdlib.h> |
52b32b2a | 12 | #include <sys/file.h> |
ebf4e801 | 13 | #include <sys/xattr.h> |
d9daae55 | 14 | #include <sysexits.h> |
3f6fd1ba LP |
15 | #include <time.h> |
16 | #include <unistd.h> | |
5008d581 | 17 | |
5a8575ef ZJS |
18 | #include "sd-path.h" |
19 | ||
3f6fd1ba | 20 | #include "acl-util.h" |
b5efdb8a | 21 | #include "alloc-util.h" |
3f6fd1ba | 22 | #include "btrfs-util.h" |
d6b4d1c7 | 23 | #include "build.h" |
430f0182 | 24 | #include "capability-util.h" |
f461a28d | 25 | #include "chase.h" |
c8b3094d | 26 | #include "chattr-util.h" |
3f6fd1ba | 27 | #include "conf-files.h" |
28db6fbf | 28 | #include "constants.h" |
3f6fd1ba | 29 | #include "copy.h" |
1d77721f | 30 | #include "creds-util.h" |
7176f06c | 31 | #include "devnum-util.h" |
8fb3f009 | 32 | #include "dirent-util.h" |
63d3d0a5 | 33 | #include "dissect-image.h" |
4368c60c | 34 | #include "env-util.h" |
8f6fb95c | 35 | #include "errno-util.h" |
4f5dd394 | 36 | #include "escape.h" |
3ffd4af2 | 37 | #include "fd-util.h" |
0d39fa9c | 38 | #include "fileio.h" |
f97b34a6 | 39 | #include "format-util.h" |
f4f15635 | 40 | #include "fs-util.h" |
7d50b32a | 41 | #include "glob-util.h" |
708daf42 | 42 | #include "hexdecoct.h" |
c004493c | 43 | #include "io-util.h" |
0690160e | 44 | #include "label-util.h" |
5008d581 | 45 | #include "log.h" |
54693d9b | 46 | #include "macro.h" |
87938c3b | 47 | #include "main-func.h" |
ddb439b8 | 48 | #include "missing_syscall.h" |
35cd0ba5 | 49 | #include "mkdir-label.h" |
63d3d0a5 | 50 | #include "mount-util.h" |
049af8ad | 51 | #include "mountpoint-util.h" |
08af3cc5 | 52 | #include "nulstr-util.h" |
a3451c2c | 53 | #include "offline-passwd.h" |
dcd5c891 | 54 | #include "pager.h" |
614b022c | 55 | #include "parse-argument.h" |
6bedfcbb | 56 | #include "parse-util.h" |
f2b5ca0e | 57 | #include "path-lookup.h" |
9eb977db | 58 | #include "path-util.h" |
294bf0c3 | 59 | #include "pretty-print.h" |
e5358401 | 60 | #include "rlimit-util.h" |
c6878637 | 61 | #include "rm-rf.h" |
d7b8eec7 | 62 | #include "selinux-util.h" |
3f6fd1ba | 63 | #include "set.h" |
760877e9 | 64 | #include "sort-util.h" |
3f6fd1ba | 65 | #include "specifier.h" |
41fea218 | 66 | #include "stat-util.h" |
15a5e950 | 67 | #include "stdio-util.h" |
8b43440b | 68 | #include "string-table.h" |
07630cea | 69 | #include "string-util.h" |
3f6fd1ba | 70 | #include "strv.h" |
3f532a59 | 71 | #include "terminal-util.h" |
affb60b1 | 72 | #include "umask-util.h" |
b1d4f8e1 | 73 | #include "user-util.h" |
1401882f | 74 | #include "virt.h" |
5008d581 | 75 | |
01000479 | 76 | /* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates |
5008d581 | 77 | * them in the file system. This is intended to be used to create |
db019b8d KS |
78 | * properly owned directories beneath /tmp, /var/tmp, /run, which are |
79 | * volatile and hence need to be recreated on bootup. */ | |
5008d581 | 80 | |
1a967b6b LP |
81 | typedef enum OperationMask { |
82 | OPERATION_CREATE = 1 << 0, | |
83 | OPERATION_REMOVE = 1 << 1, | |
84 | OPERATION_CLEAN = 1 << 2, | |
81a18380 | 85 | OPERATION_PURGE = 1 << 3, |
1a967b6b LP |
86 | } OperationMask; |
87 | ||
66ccd038 | 88 | typedef enum ItemType { |
b8bb3e8f | 89 | /* These ones take file names */ |
2223a025 ZJS |
90 | CREATE_FILE = 'f', |
91 | TRUNCATE_FILE = 'F', /* deprecated: use f+ */ | |
92 | CREATE_DIRECTORY = 'd', | |
93 | TRUNCATE_DIRECTORY = 'D', | |
94 | CREATE_SUBVOLUME = 'v', | |
5fb13eb5 | 95 | CREATE_SUBVOLUME_INHERIT_QUOTA = 'q', |
2223a025 ZJS |
96 | CREATE_SUBVOLUME_NEW_QUOTA = 'Q', |
97 | CREATE_FIFO = 'p', | |
98 | CREATE_SYMLINK = 'L', | |
99 | CREATE_CHAR_DEVICE = 'c', | |
100 | CREATE_BLOCK_DEVICE = 'b', | |
101 | COPY_FILES = 'C', | |
b8bb3e8f LP |
102 | |
103 | /* These ones take globs */ | |
2223a025 ZJS |
104 | WRITE_FILE = 'w', |
105 | EMPTY_DIRECTORY = 'e', | |
106 | SET_XATTR = 't', | |
107 | RECURSIVE_SET_XATTR = 'T', | |
108 | SET_ACL = 'a', | |
109 | RECURSIVE_SET_ACL = 'A', | |
110 | SET_ATTRIBUTE = 'h', | |
111 | RECURSIVE_SET_ATTRIBUTE = 'H', | |
112 | IGNORE_PATH = 'x', | |
113 | IGNORE_DIRECTORY_PATH = 'X', | |
114 | REMOVE_PATH = 'r', | |
115 | RECURSIVE_REMOVE_PATH = 'R', | |
116 | RELABEL_PATH = 'z', | |
117 | RECURSIVE_RELABEL_PATH = 'Z', | |
118 | ADJUST_MODE = 'm', /* legacy, 'z' is identical to this */ | |
66ccd038 | 119 | } ItemType; |
3b63d2d3 | 120 | |
7f7a50dd SK |
121 | typedef enum AgeBy { |
122 | AGE_BY_ATIME = 1 << 0, | |
123 | AGE_BY_BTIME = 1 << 1, | |
124 | AGE_BY_CTIME = 1 << 2, | |
125 | AGE_BY_MTIME = 1 << 3, | |
126 | ||
127 | /* All file timestamp types are checked by default. */ | |
128 | AGE_BY_DEFAULT_FILE = AGE_BY_ATIME | AGE_BY_BTIME | AGE_BY_CTIME | AGE_BY_MTIME, | |
2223a025 | 129 | AGE_BY_DEFAULT_DIR = AGE_BY_ATIME | AGE_BY_BTIME | AGE_BY_MTIME, |
7f7a50dd SK |
130 | } AgeBy; |
131 | ||
3b63d2d3 | 132 | typedef struct Item { |
66ccd038 | 133 | ItemType type; |
3b63d2d3 LP |
134 | |
135 | char *path; | |
468d726b | 136 | char *argument; |
708daf42 LP |
137 | void *binary_argument; /* set if binary data, in which case it takes precedence over 'argument' */ |
138 | size_t binary_argument_size; | |
ebf4e801 | 139 | char **xattrs; |
349cc4a5 | 140 | #if HAVE_ACL |
f8eeeaf9 | 141 | acl_t acl_access; |
26d98cdd | 142 | acl_t acl_access_exec; |
f8eeeaf9 ZJS |
143 | acl_t acl_default; |
144 | #endif | |
5008d581 LP |
145 | uid_t uid; |
146 | gid_t gid; | |
3b63d2d3 LP |
147 | mode_t mode; |
148 | usec_t age; | |
7f7a50dd | 149 | AgeBy age_by_file, age_by_dir; |
3b63d2d3 | 150 | |
468d726b | 151 | dev_t major_minor; |
88ec4dfa LP |
152 | unsigned attribute_value; |
153 | unsigned attribute_mask; | |
468d726b | 154 | |
3b63d2d3 LP |
155 | bool uid_set:1; |
156 | bool gid_set:1; | |
157 | bool mode_set:1; | |
cc43328c LP |
158 | bool uid_only_create:1; |
159 | bool gid_only_create:1; | |
160 | bool mode_only_create:1; | |
3b63d2d3 | 161 | bool age_set:1; |
abef3f91 | 162 | bool mask_perms:1; |
88ec4dfa | 163 | bool attribute_set:1; |
24f3a374 LP |
164 | |
165 | bool keep_first_level:1; | |
1910cd0e | 166 | |
c55ac248 | 167 | bool append_or_force:1; |
2e78fa79 | 168 | |
6d7b5433 WD |
169 | bool allow_failure:1; |
170 | ||
c46c3233 AW |
171 | bool try_replace:1; |
172 | ||
811a1587 | 173 | OperationMask done; |
3b63d2d3 LP |
174 | } Item; |
175 | ||
3f93da98 ZJS |
176 | typedef struct ItemArray { |
177 | Item *items; | |
96d10d78 | 178 | size_t n_items; |
811a1587 LP |
179 | |
180 | struct ItemArray *parent; | |
181 | Set *children; | |
3f93da98 ZJS |
182 | } ItemArray; |
183 | ||
5a8575ef | 184 | typedef enum DirectoryType { |
133bbca4 | 185 | DIRECTORY_RUNTIME, |
5a8575ef ZJS |
186 | DIRECTORY_STATE, |
187 | DIRECTORY_CACHE, | |
188 | DIRECTORY_LOGS, | |
189 | _DIRECTORY_TYPE_MAX, | |
190 | } DirectoryType; | |
191 | ||
a9bc518c LP |
192 | typedef enum { |
193 | CREATION_NORMAL, | |
194 | CREATION_EXISTING, | |
195 | CREATION_FORCE, | |
196 | _CREATION_MODE_MAX, | |
197 | _CREATION_MODE_INVALID = -EINVAL, | |
198 | } CreationMode; | |
199 | ||
1be6a5db | 200 | static CatFlags arg_cat_flags = CAT_CONFIG_OFF; |
9e90d4f4 | 201 | static bool arg_dry_run = false; |
4870133b | 202 | static RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM; |
1a967b6b | 203 | static OperationMask arg_operation = 0; |
81815651 | 204 | static bool arg_boot = false; |
e0ea6af3 | 205 | static bool arg_graceful = false; |
0221d68a | 206 | static PagerFlags arg_pager_flags = 0; |
3b63d2d3 | 207 | |
7bc040fa LP |
208 | static char **arg_include_prefixes = NULL; |
209 | static char **arg_exclude_prefixes = NULL; | |
cf9a4abd | 210 | static char *arg_root = NULL; |
63d3d0a5 | 211 | static char *arg_image = NULL; |
a6d8474f | 212 | static char *arg_replace = NULL; |
84be0c71 | 213 | static ImagePolicy *arg_image_policy = NULL; |
fba6e687 | 214 | |
3b63d2d3 LP |
215 | #define MAX_DEPTH 256 |
216 | ||
a5e4f3ab | 217 | typedef struct Context { |
376d0495 ZJS |
218 | OrderedHashmap *items; |
219 | OrderedHashmap *globs; | |
a5e4f3ab | 220 | Set *unix_sockets; |
376d0495 ZJS |
221 | Hashmap *uid_cache; |
222 | Hashmap *gid_cache; | |
a5e4f3ab | 223 | } Context; |
7bc040fa | 224 | |
7f13af72 AAF |
225 | STATIC_DESTRUCTOR_REGISTER(arg_include_prefixes, strv_freep); |
226 | STATIC_DESTRUCTOR_REGISTER(arg_exclude_prefixes, strv_freep); | |
87938c3b | 227 | STATIC_DESTRUCTOR_REGISTER(arg_root, freep); |
63d3d0a5 | 228 | STATIC_DESTRUCTOR_REGISTER(arg_image, freep); |
84be0c71 | 229 | STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep); |
87938c3b | 230 | |
a9bc518c LP |
231 | static const char *const creation_mode_verb_table[_CREATION_MODE_MAX] = { |
232 | [CREATION_NORMAL] = "Created", | |
233 | [CREATION_EXISTING] = "Found existing", | |
234 | [CREATION_FORCE] = "Created replacement", | |
235 | }; | |
236 | ||
237 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(creation_mode_verb, CreationMode); | |
238 | ||
a5e4f3ab LP |
239 | static void context_done(Context *c) { |
240 | assert(c); | |
241 | ||
242 | ordered_hashmap_free(c->items); | |
243 | ordered_hashmap_free(c->globs); | |
244 | ||
245 | set_free(c->unix_sockets); | |
376d0495 ZJS |
246 | |
247 | hashmap_free(c->uid_cache); | |
248 | hashmap_free(c->gid_cache); | |
a5e4f3ab LP |
249 | } |
250 | ||
6db98098 | 251 | /* Different kinds of errors that mean that information is not available in the environment. */ |
cf1ab844 | 252 | static bool ERRNO_IS_NOINFO(int r) { |
6db98098 ZJS |
253 | return IN_SET(abs(r), |
254 | EUNATCH, /* os-release or machine-id missing */ | |
255 | ENOMEDIUM, /* machine-id or another file empty */ | |
5dd814d7 | 256 | ENOPKG, /* machine-id is uninitialized */ |
6db98098 | 257 | ENXIO); /* env var is unset */ |
4cef1923 FB |
258 | } |
259 | ||
5ac41a16 ZJS |
260 | static int specifier_directory( |
261 | char specifier, | |
262 | const void *data, | |
263 | const char *root, | |
264 | const void *userdata, | |
265 | char **ret) { | |
266 | ||
5a8575ef ZJS |
267 | struct table_entry { |
268 | uint64_t type; | |
269 | const char *suffix; | |
270 | }; | |
271 | ||
272 | static const struct table_entry paths_system[] = { | |
273 | [DIRECTORY_RUNTIME] = { SD_PATH_SYSTEM_RUNTIME }, | |
274 | [DIRECTORY_STATE] = { SD_PATH_SYSTEM_STATE_PRIVATE }, | |
275 | [DIRECTORY_CACHE] = { SD_PATH_SYSTEM_STATE_CACHE }, | |
276 | [DIRECTORY_LOGS] = { SD_PATH_SYSTEM_STATE_LOGS }, | |
277 | }; | |
278 | ||
279 | static const struct table_entry paths_user[] = { | |
280 | [DIRECTORY_RUNTIME] = { SD_PATH_USER_RUNTIME }, | |
b50aadaf | 281 | [DIRECTORY_STATE] = { SD_PATH_USER_STATE_PRIVATE }, |
5a8575ef | 282 | [DIRECTORY_CACHE] = { SD_PATH_USER_STATE_CACHE }, |
b50aadaf | 283 | [DIRECTORY_LOGS] = { SD_PATH_USER_STATE_PRIVATE, "log" }, |
5a8575ef ZJS |
284 | }; |
285 | ||
5a8575ef | 286 | const struct table_entry *paths; |
de61a04b LP |
287 | _cleanup_free_ char *p = NULL; |
288 | unsigned i; | |
289 | int r; | |
5a8575ef ZJS |
290 | |
291 | assert_cc(ELEMENTSOF(paths_system) == ELEMENTSOF(paths_user)); | |
4870133b | 292 | paths = arg_runtime_scope == RUNTIME_SCOPE_USER ? paths_user : paths_system; |
5a8575ef ZJS |
293 | |
294 | i = PTR_TO_UINT(data); | |
295 | assert(i < ELEMENTSOF(paths_system)); | |
296 | ||
de61a04b LP |
297 | r = sd_path_lookup(paths[i].type, paths[i].suffix, &p); |
298 | if (r < 0) | |
299 | return r; | |
300 | ||
301 | if (arg_root) { | |
302 | _cleanup_free_ char *j = NULL; | |
303 | ||
304 | j = path_join(arg_root, p); | |
305 | if (!j) | |
306 | return -ENOMEM; | |
307 | ||
308 | *ret = TAKE_PTR(j); | |
309 | } else | |
310 | *ret = TAKE_PTR(p); | |
311 | ||
312 | return 0; | |
5a8575ef ZJS |
313 | } |
314 | ||
4cef1923 FB |
315 | static int log_unresolvable_specifier(const char *filename, unsigned line) { |
316 | static bool notified = false; | |
317 | ||
1401882f ZJS |
318 | /* In system mode, this is called when /etc is not fully initialized and some specifiers are |
319 | * unresolvable. In user mode, this is called when some variables are not defined. These cases are | |
320 | * not considered a fatal error, so log at LOG_NOTICE only for the first time and then downgrade this | |
321 | * to LOG_DEBUG for the rest. | |
322 | * | |
323 | * If we're running in a chroot (--root was used or sd_booted() reports that systemd is not running), | |
324 | * always use LOG_DEBUG. We may be called to initialize a chroot before booting and there is no | |
325 | * expectation that machine-id and other files will be populated. | |
326 | */ | |
327 | ||
328 | int log_level = notified || arg_root || running_in_chroot() > 0 ? | |
329 | LOG_DEBUG : LOG_NOTICE; | |
4cef1923 | 330 | |
d02933fd | 331 | log_syntax(NULL, |
1401882f | 332 | log_level, |
d02933fd | 333 | filename, line, 0, |
1401882f | 334 | "Failed to resolve specifier: %s, skipping.", |
4870133b | 335 | arg_runtime_scope == RUNTIME_SCOPE_USER ? "Required $XDG_... variable not defined" : "uninitialized /etc/ detected"); |
4cef1923 FB |
336 | |
337 | if (!notified) | |
1401882f ZJS |
338 | log_full(log_level, |
339 | "All rules containing unresolvable specifiers will be skipped."); | |
4cef1923 FB |
340 | |
341 | notified = true; | |
342 | return 0; | |
343 | } | |
344 | ||
9e90d4f4 ZJS |
345 | #define log_action(would, doing, fmt, ...) \ |
346 | log_full(arg_dry_run ? LOG_INFO : LOG_DEBUG, \ | |
347 | fmt, \ | |
348 | arg_dry_run ? (would) : (doing), \ | |
349 | __VA_ARGS__) | |
350 | ||
f2b5ca0e ZJS |
351 | static int user_config_paths(char*** ret) { |
352 | _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL; | |
353 | _cleanup_free_ char *persistent_config = NULL, *runtime_config = NULL, *data_home = NULL; | |
354 | _cleanup_strv_free_ char **res = NULL; | |
355 | int r; | |
356 | ||
357 | r = xdg_user_dirs(&config_dirs, &data_dirs); | |
358 | if (r < 0) | |
359 | return r; | |
360 | ||
361 | r = xdg_user_config_dir(&persistent_config, "/user-tmpfiles.d"); | |
6db98098 | 362 | if (r < 0 && !ERRNO_IS_NOINFO(r)) |
f2b5ca0e ZJS |
363 | return r; |
364 | ||
365 | r = xdg_user_runtime_dir(&runtime_config, "/user-tmpfiles.d"); | |
6db98098 | 366 | if (r < 0 && !ERRNO_IS_NOINFO(r)) |
f2b5ca0e ZJS |
367 | return r; |
368 | ||
369 | r = xdg_user_data_dir(&data_home, "/user-tmpfiles.d"); | |
6db98098 | 370 | if (r < 0 && !ERRNO_IS_NOINFO(r)) |
f2b5ca0e ZJS |
371 | return r; |
372 | ||
373 | r = strv_extend_strv_concat(&res, config_dirs, "/user-tmpfiles.d"); | |
374 | if (r < 0) | |
375 | return r; | |
376 | ||
69f3c619 LP |
377 | r = strv_extend_many( |
378 | &res, | |
379 | persistent_config, | |
380 | runtime_config, | |
381 | data_home); | |
f2b5ca0e ZJS |
382 | if (r < 0) |
383 | return r; | |
384 | ||
385 | r = strv_extend_strv_concat(&res, data_dirs, "/user-tmpfiles.d"); | |
386 | if (r < 0) | |
387 | return r; | |
388 | ||
389 | r = path_strv_make_absolute_cwd(res); | |
390 | if (r < 0) | |
391 | return r; | |
392 | ||
ae2a15bc | 393 | *ret = TAKE_PTR(res); |
f2b5ca0e ZJS |
394 | return 0; |
395 | } | |
396 | ||
81a18380 LB |
397 | static bool needs_purge(ItemType t) { |
398 | return IN_SET(t, | |
399 | COPY_FILES, | |
400 | TRUNCATE_FILE, | |
401 | CREATE_FILE, | |
402 | WRITE_FILE, | |
403 | EMPTY_DIRECTORY, | |
404 | CREATE_SUBVOLUME, | |
405 | CREATE_SUBVOLUME_INHERIT_QUOTA, | |
406 | CREATE_SUBVOLUME_NEW_QUOTA, | |
407 | CREATE_CHAR_DEVICE, | |
408 | CREATE_BLOCK_DEVICE, | |
409 | CREATE_SYMLINK, | |
410 | CREATE_FIFO, | |
411 | CREATE_DIRECTORY, | |
412 | TRUNCATE_DIRECTORY); | |
413 | } | |
414 | ||
66ccd038 | 415 | static bool needs_glob(ItemType t) { |
cde684a2 LP |
416 | return IN_SET(t, |
417 | WRITE_FILE, | |
df8dee85 | 418 | EMPTY_DIRECTORY, |
b705ab6a ZJS |
419 | SET_XATTR, |
420 | RECURSIVE_SET_XATTR, | |
421 | SET_ACL, | |
34f64536 LP |
422 | RECURSIVE_SET_ACL, |
423 | SET_ATTRIBUTE, | |
d88b65f6 LP |
424 | RECURSIVE_SET_ATTRIBUTE, |
425 | IGNORE_PATH, | |
426 | IGNORE_DIRECTORY_PATH, | |
427 | REMOVE_PATH, | |
428 | RECURSIVE_REMOVE_PATH, | |
429 | RELABEL_PATH, | |
430 | RECURSIVE_RELABEL_PATH, | |
431 | ADJUST_MODE); | |
b8bb3e8f LP |
432 | } |
433 | ||
3f93da98 ZJS |
434 | static bool takes_ownership(ItemType t) { |
435 | return IN_SET(t, | |
436 | CREATE_FILE, | |
437 | TRUNCATE_FILE, | |
438 | CREATE_DIRECTORY, | |
439 | TRUNCATE_DIRECTORY, | |
440 | CREATE_SUBVOLUME, | |
5fb13eb5 LP |
441 | CREATE_SUBVOLUME_INHERIT_QUOTA, |
442 | CREATE_SUBVOLUME_NEW_QUOTA, | |
3f93da98 ZJS |
443 | CREATE_FIFO, |
444 | CREATE_SYMLINK, | |
445 | CREATE_CHAR_DEVICE, | |
446 | CREATE_BLOCK_DEVICE, | |
447 | COPY_FILES, | |
3f93da98 | 448 | WRITE_FILE, |
d88b65f6 | 449 | EMPTY_DIRECTORY, |
3f93da98 ZJS |
450 | IGNORE_PATH, |
451 | IGNORE_DIRECTORY_PATH, | |
452 | REMOVE_PATH, | |
453 | RECURSIVE_REMOVE_PATH); | |
454 | } | |
455 | ||
ef43a391 | 456 | static struct Item* find_glob(OrderedHashmap *h, const char *match) { |
3f93da98 | 457 | ItemArray *j; |
b8bb3e8f | 458 | |
e5876c29 ZJS |
459 | ORDERED_HASHMAP_FOREACH(j, h) |
460 | FOREACH_ARRAY(item, j->items, j->n_items) | |
3f93da98 ZJS |
461 | if (fnmatch(item->path, match, FNM_PATHNAME|FNM_PERIOD) == 0) |
462 | return item; | |
b8bb3e8f LP |
463 | return NULL; |
464 | } | |
465 | ||
a5e4f3ab | 466 | static int load_unix_sockets(Context *c) { |
fe2f05b7 | 467 | _cleanup_set_free_ Set *sockets = NULL; |
7fd1b19b | 468 | _cleanup_fclose_ FILE *f = NULL; |
f1ff734f | 469 | int r; |
17b90525 | 470 | |
a5e4f3ab | 471 | if (c->unix_sockets) |
71a0be76 | 472 | return 0; |
17b90525 | 473 | |
f1ff734f | 474 | /* We maintain a cache of the sockets we found in /proc/net/unix to speed things up a little. */ |
17b90525 | 475 | |
fdcad0c2 | 476 | f = fopen("/proc/net/unix", "re"); |
71a0be76 YW |
477 | if (!f) |
478 | return log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno, | |
479 | "Failed to open /proc/net/unix, ignoring: %m"); | |
17b90525 | 480 | |
fdcad0c2 | 481 | /* Skip header */ |
f1ff734f | 482 | r = read_line(f, LONG_LINE_MAX, NULL); |
71a0be76 YW |
483 | if (r < 0) |
484 | return log_warning_errno(r, "Failed to skip /proc/net/unix header line: %m"); | |
485 | if (r == 0) | |
486 | return log_warning_errno(SYNTHETIC_ERRNO(EIO), "Premature end of file reading /proc/net/unix."); | |
17b90525 LP |
487 | |
488 | for (;;) { | |
fe2f05b7 | 489 | _cleanup_free_ char *line = NULL; |
71a0be76 | 490 | char *p; |
17b90525 | 491 | |
f1ff734f | 492 | r = read_line(f, LONG_LINE_MAX, &line); |
71a0be76 YW |
493 | if (r < 0) |
494 | return log_warning_errno(r, "Failed to read /proc/net/unix line, ignoring: %m"); | |
f1ff734f | 495 | if (r == 0) /* EOF */ |
17b90525 LP |
496 | break; |
497 | ||
fdcad0c2 LP |
498 | p = strchr(line, ':'); |
499 | if (!p) | |
500 | continue; | |
501 | ||
502 | if (strlen(p) < 37) | |
17b90525 LP |
503 | continue; |
504 | ||
fdcad0c2 | 505 | p += 37; |
17b90525 | 506 | p += strspn(p, WHITESPACE); |
fdcad0c2 | 507 | p += strcspn(p, WHITESPACE); /* skip one more word */ |
17b90525 LP |
508 | p += strspn(p, WHITESPACE); |
509 | ||
fe2f05b7 | 510 | if (!path_is_absolute(p)) |
17b90525 LP |
511 | continue; |
512 | ||
fe2f05b7 | 513 | r = set_put_strdup_full(&sockets, &path_hash_ops_free, p); |
71a0be76 YW |
514 | if (r < 0) |
515 | return log_warning_errno(r, "Failed to add AF_UNIX socket to set, ignoring: %m"); | |
71a0be76 | 516 | } |
17b90525 | 517 | |
a5e4f3ab | 518 | c->unix_sockets = TAKE_PTR(sockets); |
71a0be76 | 519 | return 1; |
17b90525 LP |
520 | } |
521 | ||
a5e4f3ab LP |
522 | static bool unix_socket_alive(Context *c, const char *fn) { |
523 | assert(c); | |
17b90525 LP |
524 | assert(fn); |
525 | ||
a5e4f3ab | 526 | if (load_unix_sockets(c) < 0) |
71a0be76 | 527 | return true; /* We don't know, so assume yes */ |
17b90525 | 528 | |
a5e4f3ab | 529 | return set_contains(c->unix_sockets, fn); |
17b90525 LP |
530 | } |
531 | ||
708daf42 LP |
532 | /* Accessors for the argument in binary format */ |
533 | static const void* item_binary_argument(const Item *i) { | |
534 | assert(i); | |
535 | return i->binary_argument ?: i->argument; | |
536 | } | |
537 | ||
538 | static size_t item_binary_argument_size(const Item *i) { | |
539 | assert(i); | |
540 | return i->binary_argument ? i->binary_argument_size : strlen_ptr(i->argument); | |
541 | } | |
542 | ||
df99a9ef ZJS |
543 | static DIR* xopendirat_nomod(int dirfd, const char *path) { |
544 | DIR *dir; | |
545 | ||
546 | dir = xopendirat(dirfd, path, O_NOFOLLOW|O_NOATIME); | |
1532227a LP |
547 | if (dir) |
548 | return dir; | |
549 | ||
08c25eee DDM |
550 | if (!IN_SET(errno, ENOENT, ELOOP)) |
551 | log_debug_errno(errno, "Cannot open %sdirectory \"%s\": %m", dirfd == AT_FDCWD ? "" : "sub", path); | |
552 | ||
1532227a LP |
553 | if (errno != EPERM) |
554 | return NULL; | |
555 | ||
556 | dir = xopendirat(dirfd, path, O_NOFOLLOW); | |
557 | if (!dir) | |
558 | log_debug_errno(errno, "Cannot open %sdirectory \"%s\": %m", dirfd == AT_FDCWD ? "" : "sub", path); | |
df99a9ef ZJS |
559 | |
560 | return dir; | |
561 | } | |
562 | ||
563 | static DIR* opendir_nomod(const char *path) { | |
564 | return xopendirat_nomod(AT_FDCWD, path); | |
565 | } | |
566 | ||
7eeda1da ZJS |
567 | static int opendir_and_stat( |
568 | const char *path, | |
569 | DIR **ret, | |
570 | struct statx *ret_sx, | |
571 | bool *ret_mountpoint) { | |
572 | ||
573 | _cleanup_closedir_ DIR *d = NULL; | |
983dbcef | 574 | STRUCT_NEW_STATX_DEFINE(st1); |
7eeda1da ZJS |
575 | int r; |
576 | ||
577 | assert(path); | |
578 | assert(ret); | |
579 | assert(ret_sx); | |
580 | assert(ret_mountpoint); | |
581 | ||
582 | /* Do opendir() and statx() on the directory. | |
583 | * Return 1 if successful, 0 if file doesn't exist or is not a directory, | |
584 | * negative errno otherwise. | |
585 | */ | |
586 | ||
587 | d = opendir_nomod(path); | |
588 | if (!d) { | |
589 | bool ignore = IN_SET(errno, ENOENT, ENOTDIR); | |
590 | r = log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, | |
591 | errno, "Failed to open directory %s: %m", path); | |
592 | if (!ignore) | |
593 | return r; | |
594 | ||
595 | *ret = NULL; | |
596 | *ret_sx = (struct statx) {}; | |
597 | *ret_mountpoint = NULL; | |
598 | return 0; | |
599 | } | |
600 | ||
983dbcef | 601 | r = statx_fallback(dirfd(d), "", AT_EMPTY_PATH, STATX_MODE|STATX_INO|STATX_ATIME|STATX_MTIME, &st1.sx); |
7eeda1da ZJS |
602 | if (r < 0) |
603 | return log_error_errno(r, "statx(%s) failed: %m", path); | |
604 | ||
983dbcef ZJS |
605 | if (FLAGS_SET(st1.sx.stx_attributes_mask, STATX_ATTR_MOUNT_ROOT)) |
606 | *ret_mountpoint = FLAGS_SET(st1.sx.stx_attributes, STATX_ATTR_MOUNT_ROOT); | |
7eeda1da | 607 | else { |
983dbcef | 608 | STRUCT_NEW_STATX_DEFINE(st2); |
7eeda1da | 609 | |
983dbcef ZJS |
610 | r = statx_fallback(dirfd(d), "..", 0, STATX_INO, &st2.sx); |
611 | if (r < 0) | |
612 | return log_error_errno(r, "statx(%s/..) failed: %m", path); | |
7eeda1da | 613 | |
983dbcef | 614 | *ret_mountpoint = !statx_mount_same(&st1.nsx, &st2.nsx); |
7eeda1da ZJS |
615 | } |
616 | ||
617 | *ret = TAKE_PTR(d); | |
983dbcef | 618 | *ret_sx = st1.sx; |
7eeda1da ZJS |
619 | return 1; |
620 | } | |
621 | ||
7f7a50dd SK |
622 | static bool needs_cleanup( |
623 | nsec_t atime, | |
624 | nsec_t btime, | |
625 | nsec_t ctime, | |
626 | nsec_t mtime, | |
627 | nsec_t cutoff, | |
628 | const char *sub_path, | |
629 | AgeBy age_by, | |
630 | bool is_dir) { | |
631 | ||
632 | if (FLAGS_SET(age_by, AGE_BY_MTIME) && mtime != NSEC_INFINITY && mtime >= cutoff) { | |
7f7a50dd SK |
633 | /* Follows spelling in stat(1). */ |
634 | log_debug("%s \"%s\": modify time %s is too new.", | |
635 | is_dir ? "Directory" : "File", | |
636 | sub_path, | |
0086ef19 | 637 | FORMAT_TIMESTAMP_STYLE(mtime / NSEC_PER_USEC, TIMESTAMP_US)); |
7f7a50dd SK |
638 | |
639 | return false; | |
640 | } | |
641 | ||
642 | if (FLAGS_SET(age_by, AGE_BY_ATIME) && atime != NSEC_INFINITY && atime >= cutoff) { | |
7f7a50dd SK |
643 | log_debug("%s \"%s\": access time %s is too new.", |
644 | is_dir ? "Directory" : "File", | |
645 | sub_path, | |
0086ef19 | 646 | FORMAT_TIMESTAMP_STYLE(atime / NSEC_PER_USEC, TIMESTAMP_US)); |
7f7a50dd SK |
647 | |
648 | return false; | |
649 | } | |
650 | ||
651 | /* | |
652 | * Note: Unless explicitly specified by the user, "ctime" is ignored | |
653 | * by default for directories, because we change it when deleting. | |
654 | */ | |
655 | if (FLAGS_SET(age_by, AGE_BY_CTIME) && ctime != NSEC_INFINITY && ctime >= cutoff) { | |
7f7a50dd SK |
656 | log_debug("%s \"%s\": change time %s is too new.", |
657 | is_dir ? "Directory" : "File", | |
658 | sub_path, | |
0086ef19 | 659 | FORMAT_TIMESTAMP_STYLE(ctime / NSEC_PER_USEC, TIMESTAMP_US)); |
7f7a50dd SK |
660 | |
661 | return false; | |
662 | } | |
663 | ||
664 | if (FLAGS_SET(age_by, AGE_BY_BTIME) && btime != NSEC_INFINITY && btime >= cutoff) { | |
7f7a50dd SK |
665 | log_debug("%s \"%s\": birth time %s is too new.", |
666 | is_dir ? "Directory" : "File", | |
667 | sub_path, | |
0086ef19 | 668 | FORMAT_TIMESTAMP_STYLE(btime / NSEC_PER_USEC, TIMESTAMP_US)); |
7f7a50dd SK |
669 | |
670 | return false; | |
671 | } | |
672 | ||
673 | return true; | |
674 | } | |
675 | ||
3b63d2d3 | 676 | static int dir_cleanup( |
a5e4f3ab | 677 | Context *c, |
78a92a5a | 678 | Item *i, |
3b63d2d3 LP |
679 | const char *p, |
680 | DIR *d, | |
ddb439b8 LP |
681 | nsec_t self_atime_nsec, |
682 | nsec_t self_mtime_nsec, | |
683 | nsec_t cutoff_nsec, | |
684 | dev_t rootdev_major, | |
685 | dev_t rootdev_minor, | |
3b63d2d3 | 686 | bool mountpoint, |
24f3a374 | 687 | int maxdepth, |
7f7a50dd SK |
688 | bool keep_this_level, |
689 | AgeBy age_by_file, | |
690 | AgeBy age_by_dir) { | |
265ffa1e | 691 | |
3b63d2d3 | 692 | bool deleted = false; |
3b63d2d3 LP |
693 | int r = 0; |
694 | ||
a5e4f3ab LP |
695 | assert(c); |
696 | assert(i); | |
697 | assert(d); | |
698 | ||
c7f0d9e5 | 699 | FOREACH_DIRENT_ALL(de, d, break) { |
7fd1b19b | 700 | _cleanup_free_ char *sub_path = NULL; |
ddb439b8 | 701 | nsec_t atime_nsec, mtime_nsec, ctime_nsec, btime_nsec; |
3b63d2d3 | 702 | |
c7f0d9e5 | 703 | if (dot_or_dot_dot(de->d_name)) |
3b63d2d3 | 704 | continue; |
5008d581 | 705 | |
ca194a2a | 706 | /* If statx() is supported, use it. It's preferable over fstatat() since it tells us |
69e3234d | 707 | * explicitly where we are looking at a mount point, for free as side information. Determining |
ca194a2a LP |
708 | * the same information without statx() is hard, see the complexity of path_is_mount_point(), |
709 | * and also much slower as it requires a number of syscalls instead of just one. Hence, when | |
710 | * we have modern statx() we use it instead of fstat() and do proper mount point checks, | |
711 | * while on older kernels's well do traditional st_dev based detection of mount points. | |
712 | * | |
1bd0cefc | 713 | * Using statx() for detecting mount points also has the benefit that we handle weird file |
ca194a2a LP |
714 | * systems such as overlayfs better where each file is originating from a different |
715 | * st_dev. */ | |
716 | ||
717 | STRUCT_STATX_DEFINE(sx); | |
718 | ||
719 | r = statx_fallback( | |
c7f0d9e5 | 720 | dirfd(d), de->d_name, |
ca194a2a LP |
721 | AT_SYMLINK_NOFOLLOW|AT_NO_AUTOMOUNT, |
722 | STATX_TYPE|STATX_MODE|STATX_UID|STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_BTIME, | |
723 | &sx); | |
724 | if (r == -ENOENT) | |
725 | continue; | |
726 | if (r < 0) { | |
727 | /* FUSE, NFS mounts, SELinux might return EACCES */ | |
db6873bf ZJS |
728 | log_full_errno(r == -EACCES ? LOG_DEBUG : LOG_ERR, r, |
729 | "statx(%s/%s) failed: %m", p, de->d_name); | |
ca194a2a | 730 | continue; |
3b63d2d3 LP |
731 | } |
732 | ||
ca194a2a LP |
733 | if (FLAGS_SET(sx.stx_attributes_mask, STATX_ATTR_MOUNT_ROOT)) { |
734 | /* Yay, we have the mount point API, use it */ | |
735 | if (FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT)) { | |
c7f0d9e5 | 736 | log_debug("Ignoring \"%s/%s\": different mount points.", p, de->d_name); |
ddb439b8 LP |
737 | continue; |
738 | } | |
ca194a2a LP |
739 | } else { |
740 | /* So we might have statx() but the STATX_ATTR_MOUNT_ROOT flag is not supported, fall | |
741 | * back to traditional stx_dev checking. */ | |
742 | if (sx.stx_dev_major != rootdev_major || | |
743 | sx.stx_dev_minor != rootdev_minor) { | |
c7f0d9e5 | 744 | log_debug("Ignoring \"%s/%s\": different filesystem.", p, de->d_name); |
ddb439b8 LP |
745 | continue; |
746 | } | |
747 | ||
ad36bb9f ZJS |
748 | /* Try to detect bind mounts of the same filesystem instance; they do not differ in |
749 | * device major/minors. This type of query is not supported on all kernels or | |
750 | * filesystem types though. */ | |
ca194a2a LP |
751 | if (S_ISDIR(sx.stx_mode)) { |
752 | int q; | |
3b63d2d3 | 753 | |
c7f0d9e5 | 754 | q = fd_is_mount_point(dirfd(d), de->d_name, 0); |
ca194a2a | 755 | if (q < 0) |
c7f0d9e5 | 756 | log_debug_errno(q, "Failed to determine whether \"%s/%s\" is a mount point, ignoring: %m", p, de->d_name); |
ca194a2a | 757 | else if (q > 0) { |
c7f0d9e5 | 758 | log_debug("Ignoring \"%s/%s\": different mount of the same filesystem.", p, de->d_name); |
ca194a2a LP |
759 | continue; |
760 | } | |
60bdc0ca | 761 | } |
582deb84 | 762 | } |
99d680ac | 763 | |
41fea218 AV |
764 | atime_nsec = FLAGS_SET(sx.stx_mask, STATX_ATIME) ? statx_timestamp_load_nsec(&sx.stx_atime) : 0; |
765 | mtime_nsec = FLAGS_SET(sx.stx_mask, STATX_MTIME) ? statx_timestamp_load_nsec(&sx.stx_mtime) : 0; | |
766 | ctime_nsec = FLAGS_SET(sx.stx_mask, STATX_CTIME) ? statx_timestamp_load_nsec(&sx.stx_ctime) : 0; | |
767 | btime_nsec = FLAGS_SET(sx.stx_mask, STATX_BTIME) ? statx_timestamp_load_nsec(&sx.stx_btime) : 0; | |
ca194a2a | 768 | |
c7f0d9e5 | 769 | sub_path = path_join(p, de->d_name); |
cde684a2 | 770 | if (!sub_path) { |
0d0f0c50 | 771 | r = log_oom(); |
3b63d2d3 LP |
772 | goto finish; |
773 | } | |
774 | ||
775 | /* Is there an item configured for this path? */ | |
a5e4f3ab | 776 | if (ordered_hashmap_get(c->items, sub_path)) { |
582deb84 | 777 | log_debug("Ignoring \"%s\": a separate entry exists.", sub_path); |
3b63d2d3 | 778 | continue; |
582deb84 | 779 | } |
3b63d2d3 | 780 | |
a5e4f3ab | 781 | if (find_glob(c->globs, sub_path)) { |
582deb84 | 782 | log_debug("Ignoring \"%s\": a separate glob exists.", sub_path); |
b8bb3e8f | 783 | continue; |
582deb84 | 784 | } |
b8bb3e8f | 785 | |
ca194a2a | 786 | if (S_ISDIR(sx.stx_mode)) { |
52b32b2a | 787 | _cleanup_closedir_ DIR *sub_dir = NULL; |
3b63d2d3 LP |
788 | |
789 | if (mountpoint && | |
c7f0d9e5 | 790 | streq(de->d_name, "lost+found") && |
ca194a2a | 791 | sx.stx_uid == 0) { |
781bc44e | 792 | log_debug("Ignoring directory \"%s\".", sub_path); |
3b63d2d3 | 793 | continue; |
582deb84 | 794 | } |
3b63d2d3 LP |
795 | |
796 | if (maxdepth <= 0) | |
582deb84 | 797 | log_warning("Reached max depth on \"%s\".", sub_path); |
3b63d2d3 | 798 | else { |
3b63d2d3 LP |
799 | int q; |
800 | ||
c7f0d9e5 | 801 | sub_dir = xopendirat_nomod(dirfd(d), de->d_name); |
cde684a2 | 802 | if (!sub_dir) { |
582deb84 | 803 | if (errno != ENOENT) |
781bc44e | 804 | r = log_warning_errno(errno, "Opening directory \"%s\" failed, ignoring: %m", sub_path); |
3b63d2d3 LP |
805 | |
806 | continue; | |
807 | } | |
808 | ||
9e90d4f4 ZJS |
809 | if (!arg_dry_run && |
810 | flock(dirfd(sub_dir), LOCK_EX|LOCK_NB) < 0) { | |
b572e8da | 811 | log_debug_errno(errno, "Couldn't acquire shared BSD lock on directory \"%s\", skipping: %m", sub_path); |
52b32b2a LP |
812 | continue; |
813 | } | |
814 | ||
a5e4f3ab | 815 | q = dir_cleanup(c, i, |
ddb439b8 LP |
816 | sub_path, sub_dir, |
817 | atime_nsec, mtime_nsec, cutoff_nsec, | |
818 | rootdev_major, rootdev_minor, | |
7f7a50dd SK |
819 | false, maxdepth-1, false, |
820 | age_by_file, age_by_dir); | |
3b63d2d3 LP |
821 | if (q < 0) |
822 | r = q; | |
823 | } | |
824 | ||
a23fabc6 LP |
825 | /* Note: if you are wondering why we don't support the sticky bit for excluding |
826 | * directories from cleaning like we do it for other file system objects: well, the | |
827 | * sticky bit already has a meaning for directories, so we don't want to overload | |
828 | * that. */ | |
24f3a374 | 829 | |
582deb84 | 830 | if (keep_this_level) { |
781bc44e | 831 | log_debug("Keeping directory \"%s\".", sub_path); |
24f3a374 | 832 | continue; |
582deb84 | 833 | } |
24f3a374 | 834 | |
7f7a50dd SK |
835 | /* |
836 | * Check the file timestamps of an entry against the | |
837 | * given cutoff time; delete if it is older. | |
838 | */ | |
839 | if (!needs_cleanup(atime_nsec, btime_nsec, ctime_nsec, mtime_nsec, | |
840 | cutoff_nsec, sub_path, age_by_dir, true)) | |
3b63d2d3 LP |
841 | continue; |
842 | ||
9e90d4f4 ZJS |
843 | log_action("Would remove", "Removing", "%s directory \"%s\"", sub_path); |
844 | if (!arg_dry_run && | |
845 | unlinkat(dirfd(d), de->d_name, AT_REMOVEDIR) < 0 && | |
846 | !IN_SET(errno, ENOENT, ENOTEMPTY)) | |
847 | r = log_warning_errno(errno, "Failed to remove directory \"%s\", ignoring: %m", sub_path); | |
3b63d2d3 LP |
848 | |
849 | } else { | |
9e90d4f4 ZJS |
850 | _cleanup_close_ int fd = -EBADF; /* This file descriptor is defined here so that the |
851 | * lock that is taken below is only dropped _after_ | |
852 | * the unlink operation has finished. */ | |
65e179a1 | 853 | |
a23fabc6 LP |
854 | /* Skip files for which the sticky bit is set. These are semantics we define, and are |
855 | * unknown elsewhere. See XDG_RUNTIME_DIR specification for details. */ | |
ca194a2a | 856 | if (sx.stx_mode & S_ISVTX) { |
582deb84 | 857 | log_debug("Skipping \"%s\": sticky bit set.", sub_path); |
9c73736d | 858 | continue; |
582deb84 | 859 | } |
9c73736d | 860 | |
20b6bb95 | 861 | if (mountpoint && |
ca194a2a LP |
862 | S_ISREG(sx.stx_mode) && |
863 | sx.stx_uid == 0 && | |
c7f0d9e5 | 864 | STR_IN_SET(de->d_name, |
20b6bb95 LP |
865 | ".journal", |
866 | "aquota.user", | |
867 | "aquota.group")) { | |
868 | log_debug("Skipping \"%s\".", sub_path); | |
869 | continue; | |
870 | } | |
3b63d2d3 | 871 | |
17b90525 | 872 | /* Ignore sockets that are listed in /proc/net/unix */ |
a5e4f3ab | 873 | if (S_ISSOCK(sx.stx_mode) && unix_socket_alive(c, sub_path)) { |
582deb84 | 874 | log_debug("Skipping \"%s\": live socket.", sub_path); |
17b90525 | 875 | continue; |
582deb84 | 876 | } |
17b90525 | 877 | |
78ab08eb | 878 | /* Ignore device nodes */ |
ca194a2a | 879 | if (S_ISCHR(sx.stx_mode) || S_ISBLK(sx.stx_mode)) { |
582deb84 | 880 | log_debug("Skipping \"%s\": a device.", sub_path); |
78ab08eb | 881 | continue; |
582deb84 | 882 | } |
78ab08eb | 883 | |
9e90d4f4 | 884 | /* Keep files on this level if this was requested */ |
582deb84 ZJS |
885 | if (keep_this_level) { |
886 | log_debug("Keeping \"%s\".", sub_path); | |
24f3a374 | 887 | continue; |
582deb84 | 888 | } |
24f3a374 | 889 | |
7f7a50dd SK |
890 | if (!needs_cleanup(atime_nsec, btime_nsec, ctime_nsec, mtime_nsec, |
891 | cutoff_nsec, sub_path, age_by_file, false)) | |
3b63d2d3 | 892 | continue; |
3b63d2d3 | 893 | |
9e90d4f4 | 894 | if (!arg_dry_run) { |
e40b11be | 895 | fd = xopenat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME|O_NONBLOCK); |
9e90d4f4 ZJS |
896 | if (fd < 0 && !IN_SET(fd, -ENOENT, -ELOOP)) |
897 | log_warning_errno(fd, "Opening file \"%s\" failed, ignoring: %m", sub_path); | |
898 | if (fd >= 0 && flock(fd, LOCK_EX|LOCK_NB) < 0 && errno == EAGAIN) { | |
899 | log_debug_errno(errno, "Couldn't acquire shared BSD lock on file \"%s\", skipping: %m", sub_path); | |
900 | continue; | |
901 | } | |
65e179a1 DDM |
902 | } |
903 | ||
9e90d4f4 ZJS |
904 | log_action("Would remove", "Removing", "%s \"%s\"", sub_path); |
905 | if (!arg_dry_run && | |
906 | unlinkat(dirfd(d), de->d_name, 0) < 0 && | |
907 | errno != ENOENT) | |
908 | r = log_warning_errno(errno, "Failed to remove \"%s\", ignoring: %m", sub_path); | |
582deb84 | 909 | |
3b63d2d3 LP |
910 | deleted = true; |
911 | } | |
912 | } | |
913 | ||
914 | finish: | |
b1935cc9 | 915 | if (deleted && (self_atime_nsec < NSEC_INFINITY || self_mtime_nsec < NSEC_INFINITY)) { |
ddb439b8 | 916 | struct timespec ts[2]; |
76e15a9c | 917 | |
9e90d4f4 ZJS |
918 | log_action("Would restore", "Restoring", |
919 | "%s access and modification time on \"%s\": %s, %s", | |
920 | p, | |
921 | FORMAT_TIMESTAMP_STYLE(self_atime_nsec / NSEC_PER_USEC, TIMESTAMP_US), | |
922 | FORMAT_TIMESTAMP_STYLE(self_mtime_nsec / NSEC_PER_USEC, TIMESTAMP_US)); | |
ddb439b8 LP |
923 | |
924 | timespec_store_nsec(ts + 0, self_atime_nsec); | |
925 | timespec_store_nsec(ts + 1, self_mtime_nsec); | |
76e15a9c LP |
926 | |
927 | /* Restore original directory timestamps */ | |
9e90d4f4 ZJS |
928 | if (!arg_dry_run && |
929 | futimens(dirfd(d), ts) < 0) | |
76e15a9c | 930 | log_warning_errno(errno, "Failed to revert timestamps of '%s', ignoring: %m", p); |
3b63d2d3 LP |
931 | } |
932 | ||
3b63d2d3 LP |
933 | return r; |
934 | } | |
935 | ||
5579f856 LP |
936 | static bool dangerous_hardlinks(void) { |
937 | _cleanup_free_ char *value = NULL; | |
938 | static int cached = -1; | |
939 | int r; | |
940 | ||
ad36bb9f ZJS |
941 | /* Check whether the fs.protected_hardlinks sysctl is on. If we can't determine it we assume its off, |
942 | * as that's what the upstream default is. */ | |
5579f856 LP |
943 | |
944 | if (cached >= 0) | |
945 | return cached; | |
946 | ||
947 | r = read_one_line_file("/proc/sys/fs/protected_hardlinks", &value); | |
948 | if (r < 0) { | |
949 | log_debug_errno(r, "Failed to read fs.protected_hardlinks sysctl: %m"); | |
950 | return true; | |
951 | } | |
952 | ||
953 | r = parse_boolean(value); | |
954 | if (r < 0) { | |
955 | log_debug_errno(r, "Failed to parse fs.protected_hardlinks sysctl: %m"); | |
956 | return true; | |
957 | } | |
958 | ||
959 | cached = r == 0; | |
960 | return cached; | |
961 | } | |
962 | ||
774f79b5 | 963 | static bool hardlink_vulnerable(const struct stat *st) { |
5579f856 LP |
964 | assert(st); |
965 | ||
966 | return !S_ISDIR(st->st_mode) && st->st_nlink > 1 && dangerous_hardlinks(); | |
967 | } | |
968 | ||
8b364a38 LP |
969 | static mode_t process_mask_perms(mode_t mode, mode_t current) { |
970 | ||
971 | if ((current & 0111) == 0) | |
972 | mode &= ~0111; | |
973 | if ((current & 0222) == 0) | |
974 | mode &= ~0222; | |
975 | if ((current & 0444) == 0) | |
976 | mode &= ~0444; | |
977 | if (!S_ISDIR(current)) | |
978 | mode &= ~07000; /* remove sticky/sgid/suid bit, unless directory */ | |
979 | ||
980 | return mode; | |
981 | } | |
982 | ||
a9bc518c | 983 | static int fd_set_perms( |
a5e4f3ab | 984 | Context *c, |
a9bc518c LP |
985 | Item *i, |
986 | int fd, | |
987 | const char *path, | |
988 | const struct stat *st, | |
989 | CreationMode creation) { | |
990 | ||
cc43328c | 991 | bool do_chown, do_chmod; |
b206ac8e | 992 | struct stat stbuf; |
a9f55d0e | 993 | mode_t new_mode; |
cc43328c LP |
994 | uid_t new_uid; |
995 | gid_t new_gid; | |
dee00c19 | 996 | int r; |
1924a97d | 997 | |
a5e4f3ab | 998 | assert(c); |
cde684a2 | 999 | assert(i); |
b4b0f87c | 1000 | assert(fd >= 0); |
4dc7bfdf | 1001 | assert(path); |
48b8aaa8 | 1002 | |
936f6bdb FB |
1003 | if (!i->mode_set && !i->uid_set && !i->gid_set) |
1004 | goto shortcut; | |
1924a97d | 1005 | |
b206ac8e FB |
1006 | if (!st) { |
1007 | if (fstat(fd, &stbuf) < 0) | |
1008 | return log_error_errno(errno, "fstat(%s) failed: %m", path); | |
1009 | st = &stbuf; | |
1010 | } | |
1011 | ||
baaa35ad ZJS |
1012 | if (hardlink_vulnerable(st)) |
1013 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
1014 | "Refusing to set permissions on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.", | |
1015 | path); | |
cc43328c LP |
1016 | new_uid = i->uid_set && (creation != CREATION_EXISTING || !i->uid_only_create) ? i->uid : st->st_uid; |
1017 | new_gid = i->gid_set && (creation != CREATION_EXISTING || !i->gid_only_create) ? i->gid : st->st_gid; | |
5579f856 | 1018 | |
a9f55d0e | 1019 | /* Do we need a chown()? */ |
cc43328c | 1020 | do_chown = (new_uid != st->st_uid) || (new_gid != st->st_gid); |
8b364a38 | 1021 | |
a9f55d0e | 1022 | /* Calculate the mode to apply */ |
cc43328c LP |
1023 | new_mode = i->mode_set && (creation != CREATION_EXISTING || !i->mode_only_create) ? |
1024 | (i->mask_perms ? process_mask_perms(i->mode, st->st_mode) : i->mode) : | |
1025 | (st->st_mode & 07777); | |
a9f55d0e | 1026 | |
cc43328c LP |
1027 | do_chmod = ((new_mode ^ st->st_mode) & 07777) != 0; |
1028 | ||
1029 | if (do_chmod && do_chown) { | |
a9f55d0e LP |
1030 | /* Before we issue the chmod() let's reduce the access mode to the common bits of the old and |
1031 | * the new mode. That way there's no time window where the file exists under the old owner | |
1032 | * with more than the old access modes — and not under the new owner with more than the new | |
1033 | * access modes either. */ | |
8b364a38 | 1034 | |
936f6bdb | 1035 | if (S_ISLNK(st->st_mode)) |
a9f55d0e | 1036 | log_debug("Skipping temporary mode fix for symlink %s.", path); |
51207ca1 | 1037 | else { |
a9f55d0e | 1038 | mode_t m = new_mode & st->st_mode; /* Mask new mode by old mode */ |
abef3f91 | 1039 | |
a9f55d0e LP |
1040 | if (((m ^ st->st_mode) & 07777) == 0) |
1041 | log_debug("\"%s\" matches temporary mode %o already.", path, m); | |
48b8aaa8 | 1042 | else { |
9e90d4f4 ZJS |
1043 | log_action("Would temporarily change", "Temporarily changing", |
1044 | "%s \"%s\" to mode %o", path, m); | |
1045 | if (!arg_dry_run) { | |
1046 | r = fchmod_opath(fd, m); | |
1047 | if (r < 0) | |
1048 | return log_error_errno(r, "fchmod() of %s failed: %m", path); | |
1049 | } | |
48b8aaa8 | 1050 | } |
062e01bb | 1051 | } |
51207ca1 | 1052 | } |
062e01bb | 1053 | |
a9f55d0e | 1054 | if (do_chown) { |
9e90d4f4 ZJS |
1055 | log_action("Would change", "Changing", |
1056 | "%s \"%s\" to owner "UID_FMT":"GID_FMT, path, new_uid, new_gid); | |
cc43328c | 1057 | |
9e90d4f4 ZJS |
1058 | if (!arg_dry_run && |
1059 | fchownat(fd, "", | |
cc43328c LP |
1060 | new_uid != st->st_uid ? new_uid : UID_INVALID, |
1061 | new_gid != st->st_gid ? new_gid : GID_INVALID, | |
936f6bdb FB |
1062 | AT_EMPTY_PATH) < 0) |
1063 | return log_error_errno(errno, "fchownat() of %s failed: %m", path); | |
582deb84 | 1064 | } |
062e01bb | 1065 | |
a9f55d0e LP |
1066 | /* Now, apply the final mode. We do this in two cases: when the user set a mode explicitly, or after a |
1067 | * chown(), since chown()'s mangle the access mode in regards to sgid/suid in some conditions. */ | |
cc43328c | 1068 | if (do_chmod || do_chown) { |
a9f55d0e LP |
1069 | if (S_ISLNK(st->st_mode)) |
1070 | log_debug("Skipping mode fix for symlink %s.", path); | |
1071 | else { | |
9e90d4f4 ZJS |
1072 | log_action("Would change", "Changing", "%s \"%s\" to mode %o", path, new_mode); |
1073 | if (!arg_dry_run) { | |
1074 | r = fchmod_opath(fd, new_mode); | |
1075 | if (r < 0) | |
1076 | return log_error_errno(r, "fchmod() of %s failed: %m", path); | |
1077 | } | |
a9f55d0e LP |
1078 | } |
1079 | } | |
1080 | ||
59793c8f | 1081 | shortcut: |
03bc11d1 | 1082 | return label_fix_full(fd, /* inode_path= */ NULL, /* label_path= */ path, 0); |
062e01bb MS |
1083 | } |
1084 | ||
244c2a83 | 1085 | static int path_open_parent_safe(const char *path, bool allow_failure) { |
5ec9d065 | 1086 | _cleanup_free_ char *dn = NULL; |
a5648b80 | 1087 | int r, fd; |
5ec9d065 | 1088 | |
4609d3bb | 1089 | if (!path_is_normalized(path)) |
244c2a83 LB |
1090 | return log_full_errno(allow_failure ? LOG_INFO : LOG_ERR, |
1091 | SYNTHETIC_ERRNO(EINVAL), | |
1092 | "Failed to open parent of '%s': path not normalized%s.", | |
1093 | path, | |
1094 | allow_failure ? ", ignoring" : ""); | |
5ec9d065 | 1095 | |
4609d3bb LP |
1096 | r = path_extract_directory(path, &dn); |
1097 | if (r < 0) | |
244c2a83 LB |
1098 | return log_full_errno(allow_failure ? LOG_INFO : LOG_ERR, |
1099 | r, | |
1100 | "Unable to determine parent directory of '%s'%s: %m", | |
1101 | path, | |
1102 | allow_failure ? ", ignoring" : ""); | |
5ec9d065 | 1103 | |
f461a28d | 1104 | r = chase(dn, arg_root, allow_failure ? CHASE_SAFE : CHASE_SAFE|CHASE_WARN, NULL, &fd); |
4609d3bb LP |
1105 | if (r == -ENOLINK) /* Unsafe symlink: already covered by CHASE_WARN */ |
1106 | return r; | |
1107 | if (r < 0) | |
244c2a83 LB |
1108 | return log_full_errno(allow_failure ? LOG_INFO : LOG_ERR, |
1109 | r, | |
1110 | "Failed to open path '%s'%s: %m", | |
1111 | dn, | |
1112 | allow_failure ? ", ignoring" : ""); | |
5ec9d065 | 1113 | |
4609d3bb | 1114 | return fd; |
5ec9d065 FB |
1115 | } |
1116 | ||
addc3e30 | 1117 | static int path_open_safe(const char *path) { |
a5648b80 | 1118 | int r, fd; |
addc3e30 FB |
1119 | |
1120 | /* path_open_safe() returns a file descriptor opened with O_PATH after | |
1121 | * verifying that the path doesn't contain unsafe transitions, except | |
1122 | * for its final component as the function does not follow symlink. */ | |
1123 | ||
1124 | assert(path); | |
1125 | ||
baaa35ad | 1126 | if (!path_is_normalized(path)) |
4609d3bb | 1127 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to open invalid path '%s'.", path); |
addc3e30 | 1128 | |
f461a28d | 1129 | r = chase(path, arg_root, CHASE_SAFE|CHASE_WARN|CHASE_NOFOLLOW, NULL, &fd); |
4609d3bb LP |
1130 | if (r == -ENOLINK) |
1131 | return r; /* Unsafe symlink: already covered by CHASE_WARN */ | |
1132 | if (r < 0) | |
1133 | return log_error_errno(r, "Failed to open path %s: %m", path); | |
addc3e30 | 1134 | |
4609d3bb | 1135 | return fd; |
addc3e30 FB |
1136 | } |
1137 | ||
a9bc518c | 1138 | static int path_set_perms( |
a5e4f3ab | 1139 | Context *c, |
a9bc518c LP |
1140 | Item *i, |
1141 | const char *path, | |
1142 | CreationMode creation) { | |
1143 | ||
254d1313 | 1144 | _cleanup_close_ int fd = -EBADF; |
936f6bdb | 1145 | |
a5e4f3ab | 1146 | assert(c); |
936f6bdb FB |
1147 | assert(i); |
1148 | assert(path); | |
1149 | ||
addc3e30 | 1150 | fd = path_open_safe(path); |
54946021 | 1151 | if (fd < 0) |
addc3e30 | 1152 | return fd; |
d460ba18 | 1153 | |
a5e4f3ab | 1154 | return fd_set_perms(c, i, fd, path, /* st= */ NULL, creation); |
936f6bdb FB |
1155 | } |
1156 | ||
bd550f78 | 1157 | static int parse_xattrs_from_arg(Item *i) { |
ebf4e801 MW |
1158 | const char *p; |
1159 | int r; | |
1160 | ||
1161 | assert(i); | |
ebf4e801 | 1162 | |
3a632fc1 | 1163 | assert_se(p = i->argument); |
4034a06d | 1164 | for (;;) { |
4cef1923 | 1165 | _cleanup_free_ char *name = NULL, *value = NULL, *xattr = NULL; |
4034a06d | 1166 | |
4ec85141 | 1167 | r = extract_first_word(&p, &xattr, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE); |
4034a06d | 1168 | if (r < 0) |
bd550f78 | 1169 | log_warning_errno(r, "Failed to parse extended attribute '%s', ignoring: %m", p); |
4034a06d LP |
1170 | if (r <= 0) |
1171 | break; | |
505ef0e3 | 1172 | |
4cef1923 | 1173 | r = split_pair(xattr, "=", &name, &value); |
ebf4e801 | 1174 | if (r < 0) { |
4034a06d | 1175 | log_warning_errno(r, "Failed to parse extended attribute, ignoring: %s", xattr); |
ebf4e801 MW |
1176 | continue; |
1177 | } | |
505ef0e3 | 1178 | |
4034a06d | 1179 | if (isempty(name) || isempty(value)) { |
bd550f78 | 1180 | log_warning("Malformed extended attribute found, ignoring: %s", xattr); |
ebf4e801 MW |
1181 | continue; |
1182 | } | |
505ef0e3 | 1183 | |
4034a06d | 1184 | if (strv_push_pair(&i->xattrs, name, value) < 0) |
ebf4e801 | 1185 | return log_oom(); |
505ef0e3 | 1186 | |
4034a06d | 1187 | name = value = NULL; |
ebf4e801 MW |
1188 | } |
1189 | ||
4034a06d | 1190 | return 0; |
ebf4e801 MW |
1191 | } |
1192 | ||
a9bc518c | 1193 | static int fd_set_xattrs( |
a5e4f3ab | 1194 | Context *c, |
a9bc518c LP |
1195 | Item *i, |
1196 | int fd, | |
1197 | const char *path, | |
1198 | const struct stat *st, | |
1199 | CreationMode creation) { | |
1200 | ||
a5e4f3ab | 1201 | assert(c); |
ebf4e801 | 1202 | assert(i); |
b4b0f87c | 1203 | assert(fd >= 0); |
4dc7bfdf | 1204 | assert(path); |
936f6bdb | 1205 | |
ebf4e801 | 1206 | STRV_FOREACH_PAIR(name, value, i->xattrs) { |
9e90d4f4 ZJS |
1207 | log_action("Would set", "Setting", |
1208 | "%s extended attribute '%s=%s' on %s", *name, *value, path); | |
1209 | ||
1210 | if (!arg_dry_run && | |
1211 | setxattr(FORMAT_PROC_FD_PATH(fd), *name, *value, strlen(*value), 0) < 0) | |
25f027c5 ZJS |
1212 | return log_error_errno(errno, "Setting extended attribute %s=%s on %s failed: %m", |
1213 | *name, *value, path); | |
ebf4e801 MW |
1214 | } |
1215 | return 0; | |
1216 | } | |
1217 | ||
a9bc518c | 1218 | static int path_set_xattrs( |
a5e4f3ab | 1219 | Context *c, |
a9bc518c LP |
1220 | Item *i, |
1221 | const char *path, | |
1222 | CreationMode creation) { | |
1223 | ||
254d1313 | 1224 | _cleanup_close_ int fd = -EBADF; |
936f6bdb | 1225 | |
a5e4f3ab | 1226 | assert(c); |
936f6bdb FB |
1227 | assert(i); |
1228 | assert(path); | |
1229 | ||
addc3e30 | 1230 | fd = path_open_safe(path); |
936f6bdb | 1231 | if (fd < 0) |
addc3e30 | 1232 | return fd; |
936f6bdb | 1233 | |
a5e4f3ab | 1234 | return fd_set_xattrs(c, i, fd, path, /* st = */ NULL, creation); |
936f6bdb FB |
1235 | } |
1236 | ||
bd550f78 | 1237 | static int parse_acls_from_arg(Item *item) { |
349cc4a5 | 1238 | #if HAVE_ACL |
f8eeeaf9 | 1239 | int r; |
f8eeeaf9 ZJS |
1240 | |
1241 | assert(item); | |
1242 | ||
c55ac248 | 1243 | /* If append_or_force (= modify) is set, we will not modify the acl |
50d9e46d | 1244 | * afterwards, so the mask can be added now if necessary. */ |
bd550f78 | 1245 | |
26d98cdd MY |
1246 | r = parse_acl(item->argument, &item->acl_access, &item->acl_access_exec, |
1247 | &item->acl_default, !item->append_or_force); | |
f8eeeaf9 | 1248 | if (r < 0) |
f939a403 | 1249 | log_full_errno(arg_graceful && IN_SET(r, -EINVAL, -ENOENT, -ESRCH) ? LOG_DEBUG : LOG_WARNING, |
e0ea6af3 | 1250 | r, "Failed to parse ACL \"%s\", ignoring: %m", item->argument); |
f8eeeaf9 | 1251 | #else |
26d98cdd | 1252 | log_warning("ACLs are not supported, ignoring."); |
f8eeeaf9 ZJS |
1253 | #endif |
1254 | ||
1255 | return 0; | |
1256 | } | |
1257 | ||
349cc4a5 | 1258 | #if HAVE_ACL |
26d98cdd MY |
1259 | static int parse_acl_cond_exec( |
1260 | const char *path, | |
1261 | acl_t access, /* could be empty (NULL) */ | |
1262 | acl_t cond_exec, | |
1263 | const struct stat *st, | |
1264 | bool append, | |
1265 | acl_t *ret) { | |
1266 | ||
1267 | _cleanup_(acl_freep) acl_t parsed = NULL; | |
1268 | acl_entry_t entry; | |
1269 | acl_permset_t permset; | |
1270 | bool has_exec; | |
1271 | int r; | |
1272 | ||
1273 | assert(path); | |
1274 | assert(ret); | |
1275 | assert(st); | |
1276 | ||
1277 | parsed = access ? acl_dup(access) : acl_init(0); | |
1278 | if (!parsed) | |
1279 | return -errno; | |
1280 | ||
1281 | /* Since we substitute 'X' with 'x' in parse_acl(), we just need to copy the entries over | |
1282 | * for directories */ | |
1283 | if (S_ISDIR(st->st_mode)) { | |
1284 | for (r = acl_get_entry(cond_exec, ACL_FIRST_ENTRY, &entry); | |
1285 | r > 0; | |
1286 | r = acl_get_entry(cond_exec, ACL_NEXT_ENTRY, &entry)) { | |
1287 | ||
1288 | acl_entry_t parsed_entry; | |
1289 | ||
1290 | if (acl_create_entry(&parsed, &parsed_entry) < 0) | |
1291 | return -errno; | |
1292 | ||
1293 | if (acl_copy_entry(parsed_entry, entry) < 0) | |
1294 | return -errno; | |
1295 | } | |
1296 | if (r < 0) | |
1297 | return -errno; | |
1298 | ||
1299 | goto finish; | |
1300 | } | |
1301 | ||
1302 | has_exec = st->st_mode & S_IXUSR; | |
1303 | ||
1304 | if (!has_exec && append) { | |
1305 | _cleanup_(acl_freep) acl_t old = NULL; | |
1306 | ||
1307 | old = acl_get_file(path, ACL_TYPE_ACCESS); | |
1308 | if (!old) | |
1309 | return -errno; | |
1310 | ||
1311 | for (r = acl_get_entry(old, ACL_FIRST_ENTRY, &entry); | |
1312 | r > 0; | |
1313 | r = acl_get_entry(old, ACL_NEXT_ENTRY, &entry)) { | |
1314 | ||
1315 | if (acl_get_permset(entry, &permset) < 0) | |
1316 | return -errno; | |
1317 | ||
1318 | r = acl_get_perm(permset, ACL_EXECUTE); | |
1319 | if (r < 0) | |
1320 | return -errno; | |
1321 | if (r > 0) { | |
1322 | has_exec = true; | |
1323 | break; | |
1324 | } | |
1325 | } | |
1326 | if (r < 0) | |
1327 | return -errno; | |
1328 | } | |
1329 | ||
1330 | /* Check if we're about to set the execute bit in acl_access */ | |
1331 | if (!has_exec && access) { | |
1332 | for (r = acl_get_entry(access, ACL_FIRST_ENTRY, &entry); | |
1333 | r > 0; | |
1334 | r = acl_get_entry(access, ACL_NEXT_ENTRY, &entry)) { | |
1335 | ||
1336 | if (acl_get_permset(entry, &permset) < 0) | |
1337 | return -errno; | |
1338 | ||
1339 | r = acl_get_perm(permset, ACL_EXECUTE); | |
1340 | if (r < 0) | |
1341 | return -errno; | |
1342 | if (r > 0) { | |
1343 | has_exec = true; | |
1344 | break; | |
1345 | } | |
1346 | } | |
1347 | if (r < 0) | |
1348 | return -errno; | |
1349 | } | |
1350 | ||
1351 | for (r = acl_get_entry(cond_exec, ACL_FIRST_ENTRY, &entry); | |
1352 | r > 0; | |
1353 | r = acl_get_entry(cond_exec, ACL_NEXT_ENTRY, &entry)) { | |
1354 | ||
1355 | acl_entry_t parsed_entry; | |
1356 | ||
1357 | if (acl_create_entry(&parsed, &parsed_entry) < 0) | |
1358 | return -errno; | |
1359 | ||
1360 | if (acl_copy_entry(parsed_entry, entry) < 0) | |
1361 | return -errno; | |
1362 | ||
1363 | if (!has_exec) { | |
1364 | if (acl_get_permset(parsed_entry, &permset) < 0) | |
1365 | return -errno; | |
1366 | ||
1367 | if (acl_delete_perm(permset, ACL_EXECUTE) < 0) | |
1368 | return -errno; | |
1369 | } | |
1370 | } | |
1371 | if (r < 0) | |
1372 | return -errno; | |
1373 | ||
1374 | finish: | |
1375 | if (!append) { /* want_mask = true */ | |
1376 | r = calc_acl_mask_if_needed(&parsed); | |
1377 | if (r < 0) | |
1378 | return r; | |
1379 | } | |
1380 | ||
1381 | *ret = TAKE_PTR(parsed); | |
1382 | ||
1383 | return 0; | |
1384 | } | |
1385 | ||
a9bc518c | 1386 | static int path_set_acl( |
a5e4f3ab | 1387 | Context *c, |
a9bc518c LP |
1388 | const char *path, |
1389 | const char *pretty, | |
1390 | acl_type_t type, | |
1391 | acl_t acl, | |
1392 | bool modify) { | |
1393 | ||
48b8aaa8 | 1394 | _cleanup_(acl_free_charpp) char *t = NULL; |
dd4105b0 | 1395 | _cleanup_(acl_freep) acl_t dup = NULL; |
50d9e46d ZJS |
1396 | int r; |
1397 | ||
a5e4f3ab LP |
1398 | assert(c); |
1399 | ||
1400 | /* Returns 0 for success, positive error if already warned, negative error otherwise. */ | |
d873e877 | 1401 | |
50d9e46d | 1402 | if (modify) { |
dd4105b0 | 1403 | r = acls_for_file(path, type, acl, &dup); |
50d9e46d ZJS |
1404 | if (r < 0) |
1405 | return r; | |
50d9e46d | 1406 | |
dd4105b0 ZJS |
1407 | r = calc_acl_mask_if_needed(&dup); |
1408 | if (r < 0) | |
1409 | return r; | |
1410 | } else { | |
1411 | dup = acl_dup(acl); | |
1412 | if (!dup) | |
1413 | return -errno; | |
1414 | ||
1415 | /* the mask was already added earlier if needed */ | |
1416 | } | |
1417 | ||
1418 | r = add_base_acls_if_needed(&dup, path); | |
1419 | if (r < 0) | |
1420 | return r; | |
1421 | ||
582deb84 | 1422 | t = acl_to_any_text(dup, NULL, ',', TEXT_ABBREVIATE); |
9e90d4f4 ZJS |
1423 | log_action("Would set", "Setting", |
1424 | "%s %s ACL %s on %s", | |
1425 | type == ACL_TYPE_ACCESS ? "access" : "default", | |
1426 | strna(t), pretty); | |
50d9e46d | 1427 | |
9e90d4f4 ZJS |
1428 | if (!arg_dry_run && |
1429 | acl_set_file(path, type, dup) < 0) { | |
3045c416 PC |
1430 | if (ERRNO_IS_NOT_SUPPORTED(errno)) |
1431 | /* No error if filesystem doesn't support ACLs. Return negative. */ | |
1432 | return -errno; | |
1433 | else | |
1434 | /* Return positive to indicate we already warned */ | |
1435 | return -log_error_errno(errno, | |
1436 | "Setting %s ACL \"%s\" on %s failed: %m", | |
1437 | type == ACL_TYPE_ACCESS ? "access" : "default", | |
1438 | strna(t), pretty); | |
1439 | } | |
582deb84 | 1440 | return 0; |
50d9e46d | 1441 | } |
35888b67 | 1442 | #endif |
50d9e46d | 1443 | |
a9bc518c | 1444 | static int fd_set_acls( |
a5e4f3ab | 1445 | Context *c, |
a9bc518c LP |
1446 | Item *item, |
1447 | int fd, | |
1448 | const char *path, | |
1449 | const struct stat *st, | |
1450 | CreationMode creation) { | |
1451 | ||
d873e877 | 1452 | int r = 0; |
349cc4a5 | 1453 | #if HAVE_ACL |
26d98cdd | 1454 | _cleanup_(acl_freep) acl_t access_with_exec_parsed = NULL; |
b206ac8e | 1455 | struct stat stbuf; |
48b8aaa8 | 1456 | |
a5e4f3ab | 1457 | assert(c); |
f8eeeaf9 | 1458 | assert(item); |
b4b0f87c | 1459 | assert(fd >= 0); |
4dc7bfdf | 1460 | assert(path); |
48b8aaa8 | 1461 | |
b206ac8e FB |
1462 | if (!st) { |
1463 | if (fstat(fd, &stbuf) < 0) | |
1464 | return log_error_errno(errno, "fstat(%s) failed: %m", path); | |
1465 | st = &stbuf; | |
1466 | } | |
1467 | ||
baaa35ad ZJS |
1468 | if (hardlink_vulnerable(st)) |
1469 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
1470 | "Refusing to set ACLs on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.", | |
1471 | path); | |
5579f856 | 1472 | |
936f6bdb | 1473 | if (S_ISLNK(st->st_mode)) { |
48b8aaa8 LP |
1474 | log_debug("Skipping ACL fix for symlink %s.", path); |
1475 | return 0; | |
1476 | } | |
1477 | ||
26d98cdd MY |
1478 | if (item->acl_access_exec) { |
1479 | r = parse_acl_cond_exec(FORMAT_PROC_FD_PATH(fd), | |
1480 | item->acl_access, | |
1481 | item->acl_access_exec, | |
1482 | st, | |
1483 | item->append_or_force, | |
1484 | &access_with_exec_parsed); | |
1485 | if (r < 0) | |
1486 | return log_error_errno(r, "Failed to parse conditionalized execute bit for \"%s\": %m", path); | |
1487 | ||
a5e4f3ab | 1488 | r = path_set_acl(c, FORMAT_PROC_FD_PATH(fd), path, ACL_TYPE_ACCESS, access_with_exec_parsed, item->append_or_force); |
26d98cdd | 1489 | } else if (item->acl_access) |
a5e4f3ab | 1490 | r = path_set_acl(c, FORMAT_PROC_FD_PATH(fd), path, ACL_TYPE_ACCESS, item->acl_access, item->append_or_force); |
f8eeeaf9 | 1491 | |
2667385b RM |
1492 | /* set only default acls to folders */ |
1493 | if (r == 0 && item->acl_default && S_ISDIR(st->st_mode)) | |
a5e4f3ab | 1494 | r = path_set_acl(c, FORMAT_PROC_FD_PATH(fd), path, ACL_TYPE_DEFAULT, item->acl_default, item->append_or_force); |
f8eeeaf9 | 1495 | |
3045c416 PC |
1496 | if (ERRNO_IS_NOT_SUPPORTED(r)) { |
1497 | log_debug_errno(r, "ACLs not supported by file system at %s", path); | |
1498 | return 0; | |
1499 | } | |
1500 | ||
d873e877 | 1501 | if (r > 0) |
26d98cdd | 1502 | return -r; /* already warned in path_set_acl */ |
f8606626 LP |
1503 | |
1504 | /* The above procfs paths don't work if /proc is not mounted. */ | |
1505 | if (r == -ENOENT && proc_mounted() == 0) | |
1506 | r = -ENOSYS; | |
1507 | ||
936f6bdb FB |
1508 | if (r < 0) |
1509 | return log_error_errno(r, "ACL operation on \"%s\" failed: %m", path); | |
d873e877 HPD |
1510 | #endif |
1511 | return r; | |
f8eeeaf9 ZJS |
1512 | } |
1513 | ||
a5e4f3ab LP |
1514 | static int path_set_acls( |
1515 | Context *c, | |
1516 | Item *item, | |
1517 | const char *path, | |
1518 | CreationMode creation) { | |
1519 | ||
936f6bdb | 1520 | int r = 0; |
545cdb90 | 1521 | #if HAVE_ACL |
254d1313 | 1522 | _cleanup_close_ int fd = -EBADF; |
936f6bdb | 1523 | |
a5e4f3ab | 1524 | assert(c); |
936f6bdb FB |
1525 | assert(item); |
1526 | assert(path); | |
1527 | ||
addc3e30 | 1528 | fd = path_open_safe(path); |
936f6bdb | 1529 | if (fd < 0) |
addc3e30 | 1530 | return fd; |
936f6bdb | 1531 | |
a5e4f3ab | 1532 | r = fd_set_acls(c, item, fd, path, /* st= */ NULL, creation); |
545cdb90 YW |
1533 | #endif |
1534 | return r; | |
1535 | } | |
936f6bdb | 1536 | |
bd550f78 | 1537 | static int parse_attribute_from_arg(Item *item) { |
88ec4dfa LP |
1538 | static const struct { |
1539 | char character; | |
1540 | unsigned value; | |
1541 | } attributes[] = { | |
1542 | { 'A', FS_NOATIME_FL }, /* do not update atime */ | |
1543 | { 'S', FS_SYNC_FL }, /* Synchronous updates */ | |
1544 | { 'D', FS_DIRSYNC_FL }, /* dirsync behaviour (directories only) */ | |
1545 | { 'a', FS_APPEND_FL }, /* writes to file may only append */ | |
1546 | { 'c', FS_COMPR_FL }, /* Compress file */ | |
1547 | { 'd', FS_NODUMP_FL }, /* do not dump file */ | |
8c35b2ca | 1548 | { 'e', FS_EXTENT_FL }, /* Extents */ |
88ec4dfa LP |
1549 | { 'i', FS_IMMUTABLE_FL }, /* Immutable file */ |
1550 | { 'j', FS_JOURNAL_DATA_FL }, /* Reserved for ext3 */ | |
1551 | { 's', FS_SECRM_FL }, /* Secure deletion */ | |
1552 | { 'u', FS_UNRM_FL }, /* Undelete */ | |
1553 | { 't', FS_NOTAIL_FL }, /* file tail should not be merged */ | |
13e785f7 | 1554 | { 'T', FS_TOPDIR_FL }, /* Top of directory hierarchies */ |
88ec4dfa | 1555 | { 'C', FS_NOCOW_FL }, /* Do not cow file */ |
75006470 | 1556 | { 'P', FS_PROJINHERIT_FL }, /* Inherit the quota project ID */ |
22c3a6ca | 1557 | }; |
88ec4dfa | 1558 | |
22c3a6ca GB |
1559 | enum { |
1560 | MODE_ADD, | |
1561 | MODE_DEL, | |
1562 | MODE_SET | |
1563 | } mode = MODE_ADD; | |
22c3a6ca | 1564 | |
88ec4dfa LP |
1565 | unsigned value = 0, mask = 0; |
1566 | const char *p; | |
22c3a6ca | 1567 | |
88ec4dfa LP |
1568 | assert(item); |
1569 | ||
1570 | p = item->argument; | |
1571 | if (p) { | |
1572 | if (*p == '+') { | |
1573 | mode = MODE_ADD; | |
1574 | p++; | |
1575 | } else if (*p == '-') { | |
1576 | mode = MODE_DEL; | |
1577 | p++; | |
1578 | } else if (*p == '=') { | |
1579 | mode = MODE_SET; | |
1580 | p++; | |
1581 | } | |
22c3a6ca GB |
1582 | } |
1583 | ||
baaa35ad ZJS |
1584 | if (isempty(p) && mode != MODE_SET) |
1585 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1586 | "Setting file attribute on '%s' needs an attribute specification.", | |
1587 | item->path); | |
88ec4dfa LP |
1588 | |
1589 | for (; p && *p ; p++) { | |
1590 | unsigned i, v; | |
1591 | ||
1592 | for (i = 0; i < ELEMENTSOF(attributes); i++) | |
1593 | if (*p == attributes[i].character) | |
1594 | break; | |
1595 | ||
baaa35ad ZJS |
1596 | if (i >= ELEMENTSOF(attributes)) |
1597 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
ad36bb9f | 1598 | "Unknown file attribute '%c' on '%s'.", *p, item->path); |
88ec4dfa LP |
1599 | |
1600 | v = attributes[i].value; | |
1601 | ||
3742095b | 1602 | SET_FLAG(value, v, IN_SET(mode, MODE_ADD, MODE_SET)); |
88ec4dfa LP |
1603 | |
1604 | mask |= v; | |
22c3a6ca GB |
1605 | } |
1606 | ||
1607 | if (mode == MODE_SET) | |
d629ba70 | 1608 | mask |= CHATTR_ALL_FL; |
22c3a6ca | 1609 | |
88ec4dfa | 1610 | assert(mask != 0); |
22c3a6ca | 1611 | |
88ec4dfa LP |
1612 | item->attribute_mask = mask; |
1613 | item->attribute_value = value; | |
1614 | item->attribute_set = true; | |
22c3a6ca GB |
1615 | |
1616 | return 0; | |
22c3a6ca GB |
1617 | } |
1618 | ||
a9bc518c | 1619 | static int fd_set_attribute( |
a5e4f3ab | 1620 | Context *c, |
a9bc518c LP |
1621 | Item *item, |
1622 | int fd, | |
1623 | const char *path, | |
1624 | const struct stat *st, | |
1625 | CreationMode creation) { | |
1626 | ||
b206ac8e | 1627 | struct stat stbuf; |
88ec4dfa LP |
1628 | unsigned f; |
1629 | int r; | |
22c3a6ca | 1630 | |
a5e4f3ab | 1631 | assert(c); |
4dc7bfdf | 1632 | assert(item); |
b4b0f87c | 1633 | assert(fd >= 0); |
4dc7bfdf FB |
1634 | assert(path); |
1635 | ||
88ec4dfa | 1636 | if (!item->attribute_set || item->attribute_mask == 0) |
22c3a6ca | 1637 | return 0; |
22c3a6ca | 1638 | |
b206ac8e FB |
1639 | if (!st) { |
1640 | if (fstat(fd, &stbuf) < 0) | |
1641 | return log_error_errno(errno, "fstat(%s) failed: %m", path); | |
1642 | st = &stbuf; | |
1643 | } | |
1644 | ||
c5d554aa LP |
1645 | /* Issuing the file attribute ioctls on device nodes is not safe, as that will be delivered to the |
1646 | * drivers, not the file system containing the device node. */ | |
baaa35ad ZJS |
1647 | if (!S_ISREG(st->st_mode) && !S_ISDIR(st->st_mode)) |
1648 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1649 | "Setting file flags is only supported on regular files and directories, cannot set on '%s'.", | |
1650 | path); | |
88ec4dfa LP |
1651 | |
1652 | f = item->attribute_value & item->attribute_mask; | |
1653 | ||
1654 | /* Mask away directory-specific flags */ | |
936f6bdb | 1655 | if (!S_ISDIR(st->st_mode)) |
22c3a6ca | 1656 | f &= ~FS_DIRSYNC_FL; |
88ec4dfa | 1657 | |
9e90d4f4 ZJS |
1658 | log_action("Would try to set", "Trying to set", |
1659 | "%s file attributes 0x%08x on %s", | |
1660 | f & item->attribute_mask, | |
1661 | path); | |
1662 | ||
1663 | if (!arg_dry_run) { | |
1664 | _cleanup_close_ int procfs_fd = -EBADF; | |
1665 | ||
1666 | procfs_fd = fd_reopen(fd, O_RDONLY|O_CLOEXEC|O_NOATIME); | |
1667 | if (procfs_fd < 0) | |
1668 | return log_error_errno(procfs_fd, "Failed to reopen '%s': %m", path); | |
1669 | ||
1670 | unsigned previous, current; | |
1671 | r = chattr_full(procfs_fd, NULL, f, item->attribute_mask, &previous, ¤t, CHATTR_FALLBACK_BITWISE); | |
1672 | if (r == -ENOANO) | |
1673 | log_warning("Cannot set file attributes for '%s', maybe due to incompatibility in specified attributes, " | |
1674 | "previous=0x%08x, current=0x%08x, expected=0x%08x, ignoring.", | |
1675 | path, previous, current, (previous & ~item->attribute_mask) | (f & item->attribute_mask)); | |
1676 | else if (r < 0) | |
1677 | log_full_errno(ERRNO_IS_NOT_SUPPORTED(r) ? LOG_DEBUG : LOG_WARNING, r, | |
1678 | "Cannot set file attributes for '%s', value=0x%08x, mask=0x%08x, ignoring: %m", | |
1679 | path, item->attribute_value, item->attribute_mask); | |
1680 | } | |
22c3a6ca GB |
1681 | |
1682 | return 0; | |
1683 | } | |
1684 | ||
a5e4f3ab LP |
1685 | static int path_set_attribute( |
1686 | Context *c, | |
1687 | Item *item, | |
1688 | const char *path, | |
1689 | CreationMode creation) { | |
1690 | ||
254d1313 | 1691 | _cleanup_close_ int fd = -EBADF; |
936f6bdb | 1692 | |
a5e4f3ab LP |
1693 | assert(c); |
1694 | assert(item); | |
1695 | ||
936f6bdb FB |
1696 | if (!item->attribute_set || item->attribute_mask == 0) |
1697 | return 0; | |
1698 | ||
addc3e30 | 1699 | fd = path_open_safe(path); |
936f6bdb | 1700 | if (fd < 0) |
addc3e30 | 1701 | return fd; |
936f6bdb | 1702 | |
a5e4f3ab | 1703 | return fd_set_attribute(c, item, fd, path, /* st= */ NULL, creation); |
936f6bdb FB |
1704 | } |
1705 | ||
708daf42 LP |
1706 | static int write_argument_data(Item *i, int fd, const char *path) { |
1707 | int r; | |
1708 | ||
1709 | assert(i); | |
1710 | assert(fd >= 0); | |
1711 | assert(path); | |
1712 | ||
1713 | if (item_binary_argument_size(i) == 0) | |
1714 | return 0; | |
1715 | ||
1716 | assert(item_binary_argument(i)); | |
1717 | ||
9e90d4f4 | 1718 | log_action("Would write", "Writing", "%s to \"%s\"", path); |
708daf42 | 1719 | |
9e90d4f4 ZJS |
1720 | if (!arg_dry_run) { |
1721 | r = loop_write(fd, item_binary_argument(i), item_binary_argument_size(i)); | |
1722 | if (r < 0) | |
1723 | return log_error_errno(r, "Failed to write file \"%s\": %m", path); | |
1724 | } | |
708daf42 LP |
1725 | |
1726 | return 0; | |
1727 | } | |
1728 | ||
a5e4f3ab | 1729 | static int write_one_file(Context *c, Item *i, const char *path, CreationMode creation) { |
254d1313 | 1730 | _cleanup_close_ int fd = -EBADF, dir_fd = -EBADF; |
4609d3bb | 1731 | _cleanup_free_ char *bn = NULL; |
1baaf8aa | 1732 | int r; |
31c84ff1 | 1733 | |
a5e4f3ab | 1734 | assert(c); |
31c84ff1 FB |
1735 | assert(i); |
1736 | assert(path); | |
31c84ff1 FB |
1737 | assert(i->type == WRITE_FILE); |
1738 | ||
4609d3bb LP |
1739 | r = path_extract_filename(path, &bn); |
1740 | if (r < 0) | |
1741 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", path); | |
1742 | if (r == O_DIRECTORY) | |
1743 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Cannot open path '%s' for writing, is a directory.", path); | |
1744 | ||
1745 | /* Validate the path and keep the fd on the directory for opening the file so we're sure that it | |
1746 | * can't be changed behind our back. */ | |
244c2a83 | 1747 | dir_fd = path_open_parent_safe(path, i->allow_failure); |
551470ec FB |
1748 | if (dir_fd < 0) |
1749 | return dir_fd; | |
1750 | ||
9e90d4f4 ZJS |
1751 | /* Follow symlinks. Open with O_PATH in dry-run mode to make sure we don't use the path inadvertently. */ |
1752 | int flags = O_NONBLOCK | O_CLOEXEC | O_WRONLY | O_NOCTTY | i->append_or_force * O_APPEND | arg_dry_run * O_PATH; | |
1753 | fd = openat(dir_fd, bn, flags, i->mode); | |
31c84ff1 FB |
1754 | if (fd < 0) { |
1755 | if (errno == ENOENT) { | |
1756 | log_debug_errno(errno, "Not writing missing file \"%s\": %m", path); | |
1757 | return 0; | |
1758 | } | |
1baaf8aa LP |
1759 | |
1760 | if (i->allow_failure) | |
1761 | return log_debug_errno(errno, "Failed to open file \"%s\", ignoring: %m", path); | |
1762 | ||
31c84ff1 FB |
1763 | return log_error_errno(errno, "Failed to open file \"%s\": %m", path); |
1764 | } | |
1765 | ||
1766 | /* 'w' is allowed to write into any kind of files. */ | |
31c84ff1 | 1767 | |
708daf42 | 1768 | r = write_argument_data(i, fd, path); |
31c84ff1 | 1769 | if (r < 0) |
708daf42 | 1770 | return r; |
31c84ff1 | 1771 | |
a5e4f3ab | 1772 | return fd_set_perms(c, i, fd, path, NULL, creation); |
31c84ff1 FB |
1773 | } |
1774 | ||
a5e4f3ab LP |
1775 | static int create_file( |
1776 | Context *c, | |
1777 | Item *i, | |
1778 | const char *path) { | |
1779 | ||
254d1313 | 1780 | _cleanup_close_ int fd = -EBADF, dir_fd = -EBADF; |
4609d3bb | 1781 | _cleanup_free_ char *bn = NULL; |
5ec9d065 | 1782 | struct stat stbuf, *st = NULL; |
a9bc518c | 1783 | CreationMode creation; |
5ec9d065 | 1784 | int r = 0; |
d4e9eb91 | 1785 | |
a5e4f3ab | 1786 | assert(c); |
874f1947 LP |
1787 | assert(i); |
1788 | assert(path); | |
5ec9d065 FB |
1789 | assert(i->type == CREATE_FILE); |
1790 | ||
1791 | /* 'f' operates on regular files exclusively. */ | |
1792 | ||
4609d3bb LP |
1793 | r = path_extract_filename(path, &bn); |
1794 | if (r < 0) | |
1795 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", path); | |
1796 | if (r == O_DIRECTORY) | |
1797 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Cannot open path '%s' for writing, is a directory.", path); | |
1798 | ||
9e90d4f4 ZJS |
1799 | if (arg_dry_run) { |
1800 | log_info("Would create file %s", path); | |
1801 | return 0; | |
1802 | ||
1803 | /* The opening of the directory below would fail if it doesn't exist, | |
1804 | * so log and exit before even trying to do that. */ | |
1805 | } | |
1806 | ||
4609d3bb LP |
1807 | /* Validate the path and keep the fd on the directory for opening the file so we're sure that it |
1808 | * can't be changed behind our back. */ | |
244c2a83 | 1809 | dir_fd = path_open_parent_safe(path, i->allow_failure); |
5ec9d065 FB |
1810 | if (dir_fd < 0) |
1811 | return dir_fd; | |
874f1947 | 1812 | |
2053593f | 1813 | WITH_UMASK(0000) { |
ecabcf8b | 1814 | mac_selinux_create_file_prepare(path, S_IFREG); |
05c4affe | 1815 | fd = RET_NERRNO(openat(dir_fd, bn, O_CREAT|O_EXCL|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode)); |
ecabcf8b | 1816 | mac_selinux_create_file_clear(); |
5c0d398d | 1817 | } |
d4e9eb91 DR |
1818 | |
1819 | if (fd < 0) { | |
c5d554aa LP |
1820 | /* Even on a read-only filesystem, open(2) returns EEXIST if the file already exists. It |
1821 | * returns EROFS only if it needs to create the file. */ | |
05c4affe LP |
1822 | if (fd != -EEXIST) |
1823 | return log_error_errno(fd, "Failed to create file %s: %m", path); | |
5ec9d065 | 1824 | |
c5d554aa LP |
1825 | /* Re-open the file. At that point it must exist since open(2) failed with EEXIST. We still |
1826 | * need to check if the perms/mode need to be changed. For read-only filesystems, we let | |
1827 | * fd_set_perms() report the error if the perms need to be modified. */ | |
5ec9d065 FB |
1828 | fd = openat(dir_fd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH, i->mode); |
1829 | if (fd < 0) | |
f7862b2a | 1830 | return log_error_errno(errno, "Failed to reopen file %s: %m", path); |
5ec9d065 FB |
1831 | |
1832 | if (fstat(fd, &stbuf) < 0) | |
1833 | return log_error_errno(errno, "stat(%s) failed: %m", path); | |
1834 | ||
d7a0f1f4 FS |
1835 | if (!S_ISREG(stbuf.st_mode)) |
1836 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
1837 | "%s exists and is not a regular file.", | |
1838 | path); | |
d4e9eb91 | 1839 | |
5ec9d065 | 1840 | st = &stbuf; |
a9bc518c LP |
1841 | creation = CREATION_EXISTING; |
1842 | } else { | |
8f6fb95c LP |
1843 | r = write_argument_data(i, fd, path); |
1844 | if (r < 0) | |
1845 | return r; | |
a9bc518c LP |
1846 | |
1847 | creation = CREATION_NORMAL; | |
d4e9eb91 DR |
1848 | } |
1849 | ||
a5e4f3ab | 1850 | return fd_set_perms(c, i, fd, path, st, creation); |
5ec9d065 | 1851 | } |
d4e9eb91 | 1852 | |
a5e4f3ab LP |
1853 | static int truncate_file( |
1854 | Context *c, | |
1855 | Item *i, | |
1856 | const char *path) { | |
1857 | ||
254d1313 | 1858 | _cleanup_close_ int fd = -EBADF, dir_fd = -EBADF; |
4609d3bb | 1859 | _cleanup_free_ char *bn = NULL; |
5ec9d065 | 1860 | struct stat stbuf, *st = NULL; |
a9bc518c | 1861 | CreationMode creation; |
5ec9d065 FB |
1862 | bool erofs = false; |
1863 | int r = 0; | |
1864 | ||
a5e4f3ab | 1865 | assert(c); |
5ec9d065 FB |
1866 | assert(i); |
1867 | assert(path); | |
eccebf4b | 1868 | assert(i->type == TRUNCATE_FILE || (i->type == CREATE_FILE && i->append_or_force)); |
d4e9eb91 | 1869 | |
4609d3bb LP |
1870 | /* We want to operate on regular file exclusively especially since O_TRUNC is unspecified if the file |
1871 | * is neither a regular file nor a fifo nor a terminal device. Therefore we first open the file and | |
1872 | * make sure it's a regular one before truncating it. */ | |
1873 | ||
1874 | r = path_extract_filename(path, &bn); | |
1875 | if (r < 0) | |
1876 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", path); | |
1877 | if (r == O_DIRECTORY) | |
1878 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Cannot open path '%s' for truncation, is a directory.", path); | |
3612fbc1 | 1879 | |
4609d3bb LP |
1880 | /* Validate the path and keep the fd on the directory for opening the file so we're sure that it |
1881 | * can't be changed behind our back. */ | |
244c2a83 | 1882 | dir_fd = path_open_parent_safe(path, i->allow_failure); |
14ab804e FB |
1883 | if (dir_fd < 0) |
1884 | return dir_fd; | |
1885 | ||
9e90d4f4 ZJS |
1886 | if (arg_dry_run) { |
1887 | log_info("Would truncate %s", path); | |
1888 | return 0; | |
1889 | } | |
1890 | ||
a9bc518c LP |
1891 | creation = CREATION_EXISTING; |
1892 | fd = RET_NERRNO(openat(dir_fd, bn, O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode)); | |
1893 | if (fd == -ENOENT) { | |
1894 | creation = CREATION_NORMAL; /* Didn't work without O_CREATE, try again with */ | |
1895 | ||
2053593f | 1896 | WITH_UMASK(0000) { |
a9bc518c LP |
1897 | mac_selinux_create_file_prepare(path, S_IFREG); |
1898 | fd = RET_NERRNO(openat(dir_fd, bn, O_CREAT|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode)); | |
1899 | mac_selinux_create_file_clear(); | |
1900 | } | |
5ec9d065 FB |
1901 | } |
1902 | ||
1903 | if (fd < 0) { | |
05c4affe LP |
1904 | if (fd != -EROFS) |
1905 | return log_error_errno(fd, "Failed to open/create file %s: %m", path); | |
5ec9d065 | 1906 | |
c5d554aa LP |
1907 | /* On a read-only filesystem, we don't want to fail if the target is already empty and the |
1908 | * perms are set. So we still proceed with the sanity checks and let the remaining operations | |
1909 | * fail with EROFS if they try to modify the target file. */ | |
5ec9d065 | 1910 | |
14ab804e | 1911 | fd = openat(dir_fd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH, i->mode); |
5ec9d065 | 1912 | if (fd < 0) { |
d7a0f1f4 FS |
1913 | if (errno == ENOENT) |
1914 | return log_error_errno(SYNTHETIC_ERRNO(EROFS), | |
1915 | "Cannot create file %s on a read-only file system.", | |
1916 | path); | |
5ec9d065 | 1917 | |
f7862b2a | 1918 | return log_error_errno(errno, "Failed to reopen file %s: %m", path); |
5ec9d065 FB |
1919 | } |
1920 | ||
1921 | erofs = true; | |
a9bc518c | 1922 | creation = CREATION_EXISTING; |
5ec9d065 FB |
1923 | } |
1924 | ||
1925 | if (fstat(fd, &stbuf) < 0) | |
4a62c710 | 1926 | return log_error_errno(errno, "stat(%s) failed: %m", path); |
d4e9eb91 | 1927 | |
d7a0f1f4 FS |
1928 | if (!S_ISREG(stbuf.st_mode)) |
1929 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
1930 | "%s exists and is not a regular file.", | |
1931 | path); | |
d4e9eb91 | 1932 | |
5ec9d065 FB |
1933 | if (stbuf.st_size > 0) { |
1934 | if (ftruncate(fd, 0) < 0) { | |
1935 | r = erofs ? -EROFS : -errno; | |
1936 | return log_error_errno(r, "Failed to truncate file %s: %m", path); | |
1937 | } | |
1938 | } else | |
1939 | st = &stbuf; | |
d4e9eb91 | 1940 | |
5ec9d065 FB |
1941 | log_debug("\"%s\" has been created.", path); |
1942 | ||
708daf42 LP |
1943 | if (item_binary_argument(i)) { |
1944 | r = write_argument_data(i, fd, path); | |
2f3745dd | 1945 | if (r < 0) |
708daf42 | 1946 | return r; |
5ec9d065 FB |
1947 | } |
1948 | ||
a5e4f3ab | 1949 | return fd_set_perms(c, i, fd, path, st, creation); |
d4e9eb91 DR |
1950 | } |
1951 | ||
a5e4f3ab | 1952 | static int copy_files(Context *c, Item *i) { |
254d1313 | 1953 | _cleanup_close_ int dfd = -EBADF, fd = -EBADF; |
4609d3bb | 1954 | _cleanup_free_ char *bn = NULL; |
8f6fb95c | 1955 | struct stat st, a; |
b1f7b17f FB |
1956 | int r; |
1957 | ||
9e90d4f4 ZJS |
1958 | log_action("Would copy", "Copying", "%s tree \"%s\" to \"%s\"", i->argument, i->path); |
1959 | if (arg_dry_run) | |
1960 | return 0; | |
b1f7b17f | 1961 | |
4609d3bb LP |
1962 | r = path_extract_filename(i->path, &bn); |
1963 | if (r < 0) | |
1964 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", i->path); | |
b1f7b17f | 1965 | |
4609d3bb LP |
1966 | /* Validate the path and use the returned directory fd for copying the target so we're sure that the |
1967 | * path can't be changed behind our back. */ | |
244c2a83 | 1968 | dfd = path_open_parent_safe(i->path, i->allow_failure); |
16ba55ad FB |
1969 | if (dfd < 0) |
1970 | return dfd; | |
b1f7b17f | 1971 | |
16ba55ad FB |
1972 | r = copy_tree_at(AT_FDCWD, i->argument, |
1973 | dfd, bn, | |
1974 | i->uid_set ? i->uid : UID_INVALID, | |
1975 | i->gid_set ? i->gid : GID_INVALID, | |
1fd5ec56 | 1976 | COPY_REFLINK | ((i->append_or_force) ? COPY_MERGE : COPY_MERGE_EMPTY) | COPY_MAC_CREATE | COPY_HARDLINKS, |
ad6fae7f | 1977 | NULL, NULL); |
b1f7b17f | 1978 | |
8f6fb95c LP |
1979 | fd = openat(dfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); |
1980 | if (fd < 0) { | |
1981 | if (r < 0) /* Look at original error first */ | |
b1f7b17f FB |
1982 | return log_error_errno(r, "Failed to copy files to %s: %m", i->path); |
1983 | ||
8f6fb95c LP |
1984 | return log_error_errno(errno, "Failed to openat(%s): %m", i->path); |
1985 | } | |
1986 | ||
1987 | if (fstat(fd, &st) < 0) | |
1988 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
b1f7b17f | 1989 | |
8f6fb95c LP |
1990 | if (stat(i->argument, &a) < 0) |
1991 | return log_error_errno(errno, "Failed to stat(%s): %m", i->argument); | |
b1f7b17f | 1992 | |
8f6fb95c LP |
1993 | if (((st.st_mode ^ a.st_mode) & S_IFMT) != 0) { |
1994 | log_debug("Can't copy to %s, file exists already and is of different type", i->path); | |
1995 | return 0; | |
b1f7b17f FB |
1996 | } |
1997 | ||
a5e4f3ab | 1998 | return fd_set_perms(c, i, fd, i->path, &st, _CREATION_MODE_INVALID); |
b1f7b17f FB |
1999 | } |
2000 | ||
a9bc518c LP |
2001 | static int create_directory_or_subvolume( |
2002 | const char *path, | |
2003 | mode_t mode, | |
2004 | bool subvol, | |
244c2a83 | 2005 | bool allow_failure, |
8f6fb95c | 2006 | struct stat *ret_st, |
a9bc518c | 2007 | CreationMode *ret_creation) { |
074bd73f | 2008 | |
4609d3bb | 2009 | _cleanup_free_ char *bn = NULL; |
254d1313 | 2010 | _cleanup_close_ int pfd = -EBADF; |
8f6fb95c LP |
2011 | CreationMode creation; |
2012 | struct stat st; | |
a9bc518c | 2013 | int r, fd; |
4ad36844 | 2014 | |
4c39d899 | 2015 | assert(path); |
4ad36844 | 2016 | |
4609d3bb LP |
2017 | r = path_extract_filename(path, &bn); |
2018 | if (r < 0) | |
2019 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", path); | |
2020 | ||
244c2a83 | 2021 | pfd = path_open_parent_safe(path, allow_failure); |
4c39d899 FB |
2022 | if (pfd < 0) |
2023 | return pfd; | |
4ad36844 | 2024 | |
4c39d899 | 2025 | if (subvol) { |
4368c60c AV |
2026 | r = getenv_bool("SYSTEMD_TMPFILES_FORCE_SUBVOL"); |
2027 | if (r < 0) { | |
2028 | if (r != -ENXIO) /* env var is unset */ | |
2029 | log_warning_errno(r, "Cannot parse value of $SYSTEMD_TMPFILES_FORCE_SUBVOL, ignoring."); | |
2030 | r = btrfs_is_subvol(empty_to_root(arg_root)) > 0; | |
2031 | } | |
8f6fb95c | 2032 | if (r == 0) |
92631578 LP |
2033 | /* Don't create a subvolume unless the root directory is one, too. We do this under |
2034 | * the assumption that if the root directory is just a plain directory (i.e. very | |
2035 | * light-weight), we shouldn't try to split it up into subvolumes (i.e. more | |
2036 | * heavy-weight). Thus, chroot() environments and suchlike will get a full brtfs | |
2037 | * subvolume set up below their tree only if they specifically set up a btrfs | |
2038 | * subvolume for the root dir too. */ | |
4c39d899 | 2039 | subvol = false; |
4ad36844 | 2040 | else { |
9e90d4f4 ZJS |
2041 | log_action("Would create", "Creating", "%s btrfs subvolume %s", path); |
2042 | if (!arg_dry_run) | |
2043 | WITH_UMASK((~mode) & 0777) | |
2044 | r = btrfs_subvol_make(pfd, bn); | |
2045 | else | |
2046 | r = 0; | |
4ad36844 FB |
2047 | } |
2048 | } else | |
2049 | r = 0; | |
2050 | ||
9e90d4f4 ZJS |
2051 | if (!subvol || ERRNO_IS_NEG_NOT_SUPPORTED(r)) { |
2052 | log_action("Would create", "Creating", "%s directory \"%s\"", path); | |
2053 | if (!arg_dry_run) | |
2054 | WITH_UMASK(0000) | |
2055 | r = mkdirat_label(pfd, bn, mode); | |
2056 | } | |
2057 | ||
2058 | if (arg_dry_run) | |
2059 | return 0; | |
4ad36844 | 2060 | |
8f6fb95c | 2061 | creation = r >= 0 ? CREATION_NORMAL : CREATION_EXISTING; |
4ad36844 | 2062 | |
8f6fb95c LP |
2063 | fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_DIRECTORY|O_PATH); |
2064 | if (fd < 0) { | |
2065 | /* We couldn't open it because it is not actually a directory? */ | |
2066 | if (errno == ENOTDIR) | |
2067 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "\"%s\" already exists and is not a directory.", path); | |
2068 | ||
2069 | /* Then look at the original error */ | |
2070 | if (r < 0) | |
244c2a83 LB |
2071 | return log_full_errno(allow_failure ? LOG_INFO : LOG_ERR, |
2072 | r, | |
2073 | "Failed to create directory or subvolume \"%s\"%s: %m", | |
2074 | path, | |
2075 | allow_failure ? ", ignoring" : ""); | |
4ad36844 | 2076 | |
8f6fb95c LP |
2077 | return log_error_errno(errno, "Failed to open directory/subvolume we just created '%s': %m", path); |
2078 | } | |
4ad36844 | 2079 | |
8f6fb95c LP |
2080 | if (fstat(fd, &st) < 0) |
2081 | return log_error_errno(errno, "Failed to fstat(%s): %m", path); | |
4ad36844 | 2082 | |
8f6fb95c | 2083 | assert(S_ISDIR(st.st_mode)); /* we used O_DIRECTORY above */ |
4c39d899 | 2084 | |
8f6fb95c | 2085 | log_debug("%s directory \"%s\".", creation_mode_verb_to_string(creation), path); |
09f12798 | 2086 | |
8f6fb95c LP |
2087 | if (ret_st) |
2088 | *ret_st = st; | |
a9bc518c | 2089 | if (ret_creation) |
8f6fb95c | 2090 | *ret_creation = creation; |
a9bc518c LP |
2091 | |
2092 | return fd; | |
4c39d899 FB |
2093 | } |
2094 | ||
a5e4f3ab LP |
2095 | static int create_directory( |
2096 | Context *c, | |
2097 | Item *i, | |
2098 | const char *path) { | |
2099 | ||
254d1313 | 2100 | _cleanup_close_ int fd = -EBADF; |
a9bc518c | 2101 | CreationMode creation; |
8f6fb95c | 2102 | struct stat st; |
4c39d899 | 2103 | |
a5e4f3ab | 2104 | assert(c); |
4c39d899 FB |
2105 | assert(i); |
2106 | assert(IN_SET(i->type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY)); | |
2107 | ||
9e90d4f4 ZJS |
2108 | if (arg_dry_run) { |
2109 | log_info("Would create directory %s", path); | |
2110 | return 0; | |
2111 | } | |
2112 | ||
244c2a83 | 2113 | fd = create_directory_or_subvolume(path, i->mode, /* subvol= */ false, i->allow_failure, &st, &creation); |
4c39d899 FB |
2114 | if (fd == -EEXIST) |
2115 | return 0; | |
2116 | if (fd < 0) | |
2117 | return fd; | |
2118 | ||
a5e4f3ab | 2119 | return fd_set_perms(c, i, fd, path, &st, creation); |
4c39d899 FB |
2120 | } |
2121 | ||
a5e4f3ab LP |
2122 | static int create_subvolume( |
2123 | Context *c, | |
2124 | Item *i, | |
2125 | const char *path) { | |
2126 | ||
254d1313 | 2127 | _cleanup_close_ int fd = -EBADF; |
f17a8d61 | 2128 | CreationMode creation; |
8f6fb95c | 2129 | struct stat st; |
4c39d899 FB |
2130 | int r, q = 0; |
2131 | ||
a5e4f3ab | 2132 | assert(c); |
4c39d899 FB |
2133 | assert(i); |
2134 | assert(IN_SET(i->type, CREATE_SUBVOLUME, CREATE_SUBVOLUME_NEW_QUOTA, CREATE_SUBVOLUME_INHERIT_QUOTA)); | |
2135 | ||
9e90d4f4 ZJS |
2136 | if (arg_dry_run) { |
2137 | log_info("Would create subvolume %s", path); | |
2138 | return 0; | |
2139 | } | |
2140 | ||
244c2a83 | 2141 | fd = create_directory_or_subvolume(path, i->mode, /* subvol = */ true, i->allow_failure, &st, &creation); |
4c39d899 FB |
2142 | if (fd == -EEXIST) |
2143 | return 0; | |
2144 | if (fd < 0) | |
2145 | return fd; | |
4ad36844 | 2146 | |
f17a8d61 FB |
2147 | if (creation == CREATION_NORMAL && |
2148 | IN_SET(i->type, CREATE_SUBVOLUME_NEW_QUOTA, CREATE_SUBVOLUME_INHERIT_QUOTA)) { | |
4c39d899 | 2149 | r = btrfs_subvol_auto_qgroup_fd(fd, 0, i->type == CREATE_SUBVOLUME_NEW_QUOTA); |
4ad36844 FB |
2150 | if (r == -ENOTTY) |
2151 | log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (unsupported fs or dir not a subvolume): %m", i->path); | |
2152 | else if (r == -EROFS) | |
2153 | log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (fs is read-only).", i->path); | |
4b019d2f | 2154 | else if (r == -ENOTCONN) |
4ad36844 FB |
2155 | log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (quota support is disabled).", i->path); |
2156 | else if (r < 0) | |
2157 | q = log_error_errno(r, "Failed to adjust quota for subvolume \"%s\": %m", i->path); | |
2158 | else if (r > 0) | |
2159 | log_debug("Adjusted quota for subvolume \"%s\".", i->path); | |
2160 | else if (r == 0) | |
2161 | log_debug("Quota for subvolume \"%s\" already in place, no change made.", i->path); | |
2162 | } | |
2163 | ||
a5e4f3ab | 2164 | r = fd_set_perms(c, i, fd, path, &st, creation); |
9d874aec | 2165 | if (q < 0) /* prefer the quota change error from above */ |
4ad36844 FB |
2166 | return q; |
2167 | ||
2168 | return r; | |
2169 | } | |
2170 | ||
a5e4f3ab LP |
2171 | static int empty_directory( |
2172 | Context *c, | |
2173 | Item *i, | |
2174 | const char *path, | |
2175 | CreationMode creation) { | |
2176 | ||
254d1313 | 2177 | _cleanup_close_ int fd = -EBADF; |
27f6aa0b | 2178 | struct stat st; |
54946021 FB |
2179 | int r; |
2180 | ||
a5e4f3ab | 2181 | assert(c); |
54946021 FB |
2182 | assert(i); |
2183 | assert(i->type == EMPTY_DIRECTORY); | |
2184 | ||
f461a28d | 2185 | r = chase(path, arg_root, CHASE_SAFE|CHASE_WARN, NULL, &fd); |
27f6aa0b | 2186 | if (r == -ENOLINK) /* Unsafe symlink: already covered by CHASE_WARN */ |
e56074a2 | 2187 | return r; |
54946021 | 2188 | if (r == -ENOENT) { |
27f6aa0b LP |
2189 | /* Option "e" operates only on existing objects. Do not print errors about non-existent files |
2190 | * or directories */ | |
2191 | log_debug_errno(r, "Skipping missing directory: %s", path); | |
54946021 FB |
2192 | return 0; |
2193 | } | |
2194 | if (r < 0) | |
27f6aa0b LP |
2195 | return log_error_errno(r, "Failed to open directory '%s': %m", path); |
2196 | ||
2197 | if (fstat(fd, &st) < 0) | |
2198 | return log_error_errno(errno, "Failed to fstat(%s): %m", path); | |
2199 | if (!S_ISDIR(st.st_mode)) { | |
2200 | log_warning("'%s' already exists and is not a directory.", path); | |
b88ba6c7 ZJS |
2201 | return 0; |
2202 | } | |
54946021 | 2203 | |
a5e4f3ab | 2204 | return fd_set_perms(c, i, fd, path, &st, creation); |
54946021 FB |
2205 | } |
2206 | ||
a5e4f3ab LP |
2207 | static int create_device( |
2208 | Context *c, | |
2209 | Item *i, | |
2210 | mode_t file_type) { | |
2211 | ||
254d1313 | 2212 | _cleanup_close_ int dfd = -EBADF, fd = -EBADF; |
4609d3bb | 2213 | _cleanup_free_ char *bn = NULL; |
074bd73f | 2214 | CreationMode creation; |
8f6fb95c | 2215 | struct stat st; |
074bd73f FB |
2216 | int r; |
2217 | ||
a5e4f3ab | 2218 | assert(c); |
074bd73f | 2219 | assert(i); |
a8ec0abe | 2220 | assert(IN_SET(i->type, CREATE_BLOCK_DEVICE, CREATE_CHAR_DEVICE)); |
074bd73f FB |
2221 | assert(IN_SET(file_type, S_IFBLK, S_IFCHR)); |
2222 | ||
4609d3bb LP |
2223 | r = path_extract_filename(i->path, &bn); |
2224 | if (r < 0) | |
2225 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", i->path); | |
2226 | if (r == O_DIRECTORY) | |
ad36bb9f ZJS |
2227 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), |
2228 | "Cannot open path '%s' for creating device node, is a directory.", i->path); | |
c7700a77 | 2229 | |
9e90d4f4 ZJS |
2230 | if (arg_dry_run) { |
2231 | log_info("Would create device node %s", i->path); | |
2232 | return 0; | |
2233 | } | |
2234 | ||
4609d3bb LP |
2235 | /* Validate the path and use the returned directory fd for copying the target so we're sure that the |
2236 | * path can't be changed behind our back. */ | |
244c2a83 | 2237 | dfd = path_open_parent_safe(i->path, i->allow_failure); |
c7700a77 FB |
2238 | if (dfd < 0) |
2239 | return dfd; | |
2240 | ||
2053593f | 2241 | WITH_UMASK(0000) { |
074bd73f | 2242 | mac_selinux_create_file_prepare(i->path, file_type); |
05c4affe | 2243 | r = RET_NERRNO(mknodat(dfd, bn, i->mode | file_type, i->major_minor)); |
074bd73f FB |
2244 | mac_selinux_create_file_clear(); |
2245 | } | |
8f6fb95c | 2246 | creation = r >= 0 ? CREATION_NORMAL : CREATION_EXISTING; |
074bd73f | 2247 | |
8f6fb95c LP |
2248 | /* Try to open the inode via O_PATH, regardless if we could create it or not. Maybe everything is in |
2249 | * order anyway and we hence can ignore the error to create the device node */ | |
2250 | fd = openat(dfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
2251 | if (fd < 0) { | |
2252 | /* OK, so opening the inode failed, let's look at the original error then. */ | |
c7700a77 | 2253 | |
8f6fb95c LP |
2254 | if (r < 0) { |
2255 | if (ERRNO_IS_PRIVILEGE(r)) | |
2256 | goto handle_privilege; | |
074bd73f | 2257 | |
8f6fb95c LP |
2258 | return log_error_errno(r, "Failed to create device node '%s': %m", i->path); |
2259 | } | |
074bd73f | 2260 | |
8f6fb95c LP |
2261 | return log_error_errno(errno, "Failed to open device node '%s' we just created: %m", i->path); |
2262 | } | |
074bd73f | 2263 | |
8f6fb95c LP |
2264 | if (fstat(fd, &st) < 0) |
2265 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
074bd73f | 2266 | |
8f6fb95c | 2267 | if (((st.st_mode ^ file_type) & S_IFMT) != 0) { |
074bd73f | 2268 | |
8f6fb95c LP |
2269 | if (i->append_or_force) { |
2270 | fd = safe_close(fd); | |
074bd73f | 2271 | |
2053593f | 2272 | WITH_UMASK(0000) { |
8f6fb95c LP |
2273 | mac_selinux_create_file_prepare(i->path, file_type); |
2274 | r = mknodat_atomic(dfd, bn, i->mode | file_type, i->major_minor); | |
2275 | mac_selinux_create_file_clear(); | |
2276 | } | |
2277 | if (ERRNO_IS_PRIVILEGE(r)) | |
2278 | goto handle_privilege; | |
2279 | if (IN_SET(r, -EISDIR, -EEXIST, -ENOTEMPTY)) { | |
2280 | r = rm_rf_child(dfd, bn, REMOVE_PHYSICAL); | |
074bd73f | 2281 | if (r < 0) |
8f6fb95c LP |
2282 | return log_error_errno(r, "rm -rf %s failed: %m", i->path); |
2283 | ||
2284 | mac_selinux_create_file_prepare(i->path, file_type); | |
2285 | r = RET_NERRNO(mknodat(dfd, bn, i->mode | file_type, i->major_minor)); | |
2286 | mac_selinux_create_file_clear(); | |
074bd73f | 2287 | } |
8f6fb95c LP |
2288 | if (r < 0) |
2289 | return log_error_errno(r, "Failed to create device node '%s': %m", i->path); | |
2290 | ||
2291 | fd = openat(dfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
2292 | if (fd < 0) | |
2293 | return log_error_errno(errno, "Failed to open device node we just created '%s': %m", i->path); | |
2294 | ||
2295 | /* Validate type before change ownership below */ | |
2296 | if (fstat(fd, &st) < 0) | |
2297 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
2298 | ||
2299 | if (((st.st_mode ^ file_type) & S_IFMT) != 0) | |
ad36bb9f ZJS |
2300 | return log_error_errno(SYNTHETIC_ERRNO(EBADF), |
2301 | "Device node we just created is not a device node, refusing."); | |
8f6fb95c LP |
2302 | |
2303 | creation = CREATION_FORCE; | |
2304 | } else { | |
2305 | log_warning("\"%s\" already exists and is not a device node.", i->path); | |
2306 | return 0; | |
2307 | } | |
2308 | } | |
074bd73f FB |
2309 | |
2310 | log_debug("%s %s device node \"%s\" %u:%u.", | |
2311 | creation_mode_verb_to_string(creation), | |
2312 | i->type == CREATE_BLOCK_DEVICE ? "block" : "char", | |
2313 | i->path, major(i->mode), minor(i->mode)); | |
2314 | ||
a5e4f3ab | 2315 | return fd_set_perms(c, i, fd, i->path, &st, creation); |
c7700a77 | 2316 | |
8f6fb95c LP |
2317 | handle_privilege: |
2318 | log_debug_errno(r, | |
2319 | "We lack permissions, possibly because of cgroup configuration; " | |
2320 | "skipping creation of device node '%s'.", i->path); | |
2321 | return 0; | |
074bd73f FB |
2322 | } |
2323 | ||
a5e4f3ab | 2324 | static int create_fifo(Context *c, Item *i) { |
254d1313 | 2325 | _cleanup_close_ int pfd = -EBADF, fd = -EBADF; |
4609d3bb | 2326 | _cleanup_free_ char *bn = NULL; |
a2fc2f8d FB |
2327 | CreationMode creation; |
2328 | struct stat st; | |
2329 | int r; | |
2330 | ||
a5e4f3ab | 2331 | assert(c); |
8f6fb95c LP |
2332 | assert(i); |
2333 | assert(i->type == CREATE_FIFO); | |
2334 | ||
4609d3bb LP |
2335 | r = path_extract_filename(i->path, &bn); |
2336 | if (r < 0) | |
8f6fb95c | 2337 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", i->path); |
4609d3bb | 2338 | if (r == O_DIRECTORY) |
ad36bb9f ZJS |
2339 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), |
2340 | "Cannot open path '%s' for creating FIFO, is a directory.", i->path); | |
4609d3bb | 2341 | |
9e90d4f4 ZJS |
2342 | if (arg_dry_run) { |
2343 | log_info("Would create fifo %s", i->path); | |
2344 | return 0; | |
2345 | } | |
2346 | ||
244c2a83 | 2347 | pfd = path_open_parent_safe(i->path, i->allow_failure); |
7ea5a87f FB |
2348 | if (pfd < 0) |
2349 | return pfd; | |
2350 | ||
2053593f | 2351 | WITH_UMASK(0000) { |
8f6fb95c | 2352 | mac_selinux_create_file_prepare(i->path, S_IFIFO); |
05c4affe | 2353 | r = RET_NERRNO(mkfifoat(pfd, bn, i->mode)); |
a2fc2f8d FB |
2354 | mac_selinux_create_file_clear(); |
2355 | } | |
2356 | ||
8f6fb95c | 2357 | creation = r >= 0 ? CREATION_NORMAL : CREATION_EXISTING; |
a2fc2f8d | 2358 | |
1751bdde | 2359 | /* Open the inode via O_PATH, regardless if we managed to create it or not. Maybe it is already the FIFO we want */ |
8f6fb95c LP |
2360 | fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); |
2361 | if (fd < 0) { | |
2362 | if (r < 0) | |
2363 | return log_error_errno(r, "Failed to create FIFO %s: %m", i->path); /* original error! */ | |
a2fc2f8d | 2364 | |
8f6fb95c LP |
2365 | return log_error_errno(errno, "Failed to open FIFO we just created %s: %m", i->path); |
2366 | } | |
a2fc2f8d | 2367 | |
8f6fb95c LP |
2368 | if (fstat(fd, &st) < 0) |
2369 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
2370 | ||
2371 | if (!S_ISFIFO(st.st_mode)) { | |
a2fc2f8d | 2372 | |
8f6fb95c LP |
2373 | if (i->append_or_force) { |
2374 | fd = safe_close(fd); | |
2375 | ||
2053593f | 2376 | WITH_UMASK(0000) { |
8f6fb95c LP |
2377 | mac_selinux_create_file_prepare(i->path, S_IFIFO); |
2378 | r = mkfifoat_atomic(pfd, bn, i->mode); | |
2379 | mac_selinux_create_file_clear(); | |
2380 | } | |
2381 | if (IN_SET(r, -EISDIR, -EEXIST, -ENOTEMPTY)) { | |
2382 | r = rm_rf_child(pfd, bn, REMOVE_PHYSICAL); | |
a2fc2f8d | 2383 | if (r < 0) |
8f6fb95c LP |
2384 | return log_error_errno(r, "rm -rf %s failed: %m", i->path); |
2385 | ||
2386 | mac_selinux_create_file_prepare(i->path, S_IFIFO); | |
2387 | r = RET_NERRNO(mkfifoat(pfd, bn, i->mode)); | |
2388 | mac_selinux_create_file_clear(); | |
a2fc2f8d | 2389 | } |
8f6fb95c LP |
2390 | if (r < 0) |
2391 | return log_error_errno(r, "Failed to create FIFO %s: %m", i->path); | |
7ea5a87f | 2392 | |
8f6fb95c LP |
2393 | fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); |
2394 | if (fd < 0) | |
2395 | return log_error_errno(errno, "Failed to open FIFO we just created '%s': %m", i->path); | |
a2fc2f8d | 2396 | |
8f6fb95c LP |
2397 | /* Validate type before change ownership below */ |
2398 | if (fstat(fd, &st) < 0) | |
2399 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
2400 | ||
2401 | if (!S_ISFIFO(st.st_mode)) | |
ad36bb9f ZJS |
2402 | return log_error_errno(SYNTHETIC_ERRNO(EBADF), |
2403 | "FIFO inode we just created is not a FIFO, refusing."); | |
8f6fb95c LP |
2404 | |
2405 | creation = CREATION_FORCE; | |
2406 | } else { | |
2407 | log_warning("\"%s\" already exists and is not a FIFO.", i->path); | |
2408 | return 0; | |
2409 | } | |
2410 | } | |
2411 | ||
2412 | log_debug("%s fifo \"%s\".", creation_mode_verb_to_string(creation), i->path); | |
7ea5a87f | 2413 | |
a5e4f3ab | 2414 | return fd_set_perms(c, i, fd, i->path, &st, creation); |
a2fc2f8d FB |
2415 | } |
2416 | ||
a5e4f3ab | 2417 | static int create_symlink(Context *c, Item *i) { |
254d1313 | 2418 | _cleanup_close_ int pfd = -EBADF, fd = -EBADF; |
9e430ce3 LP |
2419 | _cleanup_free_ char *bn = NULL; |
2420 | CreationMode creation; | |
2421 | struct stat st; | |
2422 | bool good = false; | |
2423 | int r; | |
2424 | ||
a5e4f3ab | 2425 | assert(c); |
9e430ce3 LP |
2426 | assert(i); |
2427 | ||
2428 | r = path_extract_filename(i->path, &bn); | |
2429 | if (r < 0) | |
2430 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", i->path); | |
2431 | if (r == O_DIRECTORY) | |
ad36bb9f ZJS |
2432 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), |
2433 | "Cannot open path '%s' for creating FIFO, is a directory.", i->path); | |
9e430ce3 | 2434 | |
9e90d4f4 ZJS |
2435 | if (arg_dry_run) { |
2436 | log_info("Would create symlink %s -> %s", i->path, i->argument); | |
2437 | return 0; | |
2438 | } | |
2439 | ||
244c2a83 | 2440 | pfd = path_open_parent_safe(i->path, i->allow_failure); |
9e430ce3 LP |
2441 | if (pfd < 0) |
2442 | return pfd; | |
2443 | ||
2444 | mac_selinux_create_file_prepare(i->path, S_IFLNK); | |
2445 | r = RET_NERRNO(symlinkat(i->argument, pfd, bn)); | |
2446 | mac_selinux_create_file_clear(); | |
2447 | ||
2448 | creation = r >= 0 ? CREATION_NORMAL : CREATION_EXISTING; | |
2449 | ||
2450 | fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
2451 | if (fd < 0) { | |
2452 | if (r < 0) | |
2453 | return log_error_errno(r, "Failed to create symlink '%s': %m", i->path); /* original error! */ | |
2454 | ||
2455 | return log_error_errno(errno, "Failed to open symlink we just created '%s': %m", i->path); | |
2456 | } | |
2457 | ||
2458 | if (fstat(fd, &st) < 0) | |
2459 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
2460 | ||
2461 | if (S_ISLNK(st.st_mode)) { | |
2462 | _cleanup_free_ char *x = NULL; | |
2463 | ||
2464 | r = readlinkat_malloc(fd, "", &x); | |
2465 | if (r < 0) | |
2466 | return log_error_errno(r, "readlinkat(%s) failed: %m", i->path); | |
2467 | ||
2468 | good = streq(x, i->argument); | |
2469 | } else | |
2470 | good = false; | |
2471 | ||
2472 | if (!good) { | |
2473 | if (!i->append_or_force) { | |
2474 | log_debug("\"%s\" is not a symlink or does not point to the correct path.", i->path); | |
2475 | return 0; | |
2476 | } | |
2477 | ||
2478 | fd = safe_close(fd); | |
2479 | ||
2480 | mac_selinux_create_file_prepare(i->path, S_IFLNK); | |
2481 | r = symlinkat_atomic_full(i->argument, pfd, bn, /* make_relative= */ false); | |
2482 | mac_selinux_create_file_clear(); | |
2483 | if (IN_SET(r, -EISDIR, -EEXIST, -ENOTEMPTY)) { | |
2484 | r = rm_rf_child(pfd, bn, REMOVE_PHYSICAL); | |
2485 | if (r < 0) | |
2486 | return log_error_errno(r, "rm -rf %s failed: %m", i->path); | |
2487 | ||
2488 | mac_selinux_create_file_prepare(i->path, S_IFLNK); | |
2489 | r = RET_NERRNO(symlinkat(i->argument, pfd, i->path)); | |
2490 | mac_selinux_create_file_clear(); | |
2491 | } | |
2492 | if (r < 0) | |
2493 | return log_error_errno(r, "symlink(%s, %s) failed: %m", i->argument, i->path); | |
2494 | ||
2495 | fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
2496 | if (fd < 0) | |
2497 | return log_error_errno(errno, "Failed to open symlink we just created '%s': %m", i->path); | |
2498 | ||
2499 | /* Validate type before change ownership below */ | |
2500 | if (fstat(fd, &st) < 0) | |
2501 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
2502 | ||
2503 | if (!S_ISLNK(st.st_mode)) | |
2504 | return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Symlink we just created is not a symlink, refusing."); | |
2505 | ||
2506 | creation = CREATION_FORCE; | |
2507 | } | |
2508 | ||
2509 | log_debug("%s symlink \"%s\".", creation_mode_verb_to_string(creation), i->path); | |
a5e4f3ab | 2510 | return fd_set_perms(c, i, fd, i->path, &st, creation); |
9e430ce3 LP |
2511 | } |
2512 | ||
a5e4f3ab LP |
2513 | typedef int (*action_t)(Context *c, Item *i, const char *path, CreationMode creation); |
2514 | typedef int (*fdaction_t)(Context *c, Item *i, int fd, const char *path, const struct stat *st, CreationMode creation); | |
a9bc518c LP |
2515 | |
2516 | static int item_do( | |
a5e4f3ab | 2517 | Context *c, |
a9bc518c LP |
2518 | Item *i, |
2519 | int fd, | |
2520 | const char *path, | |
2521 | CreationMode creation, | |
2522 | fdaction_t action) { | |
081043cf | 2523 | |
14f3480a | 2524 | struct stat st; |
936f6bdb | 2525 | int r = 0, q; |
e73a03e0 | 2526 | |
a5e4f3ab | 2527 | assert(c); |
e73a03e0 | 2528 | assert(i); |
4dc7bfdf | 2529 | assert(path); |
936f6bdb | 2530 | assert(fd >= 0); |
14f3480a FB |
2531 | |
2532 | if (fstat(fd, &st) < 0) { | |
09f12798 | 2533 | r = log_error_errno(errno, "fstat() on file failed: %m"); |
14f3480a FB |
2534 | goto finish; |
2535 | } | |
a8d88783 | 2536 | |
a9bc518c | 2537 | /* This returns the first error we run into, but nevertheless tries to go on */ |
a5e4f3ab | 2538 | r = action(c, i, fd, path, &st, creation); |
a8d88783 | 2539 | |
14f3480a | 2540 | if (S_ISDIR(st.st_mode)) { |
936f6bdb | 2541 | _cleanup_closedir_ DIR *d = NULL; |
a8d88783 | 2542 | |
ddb6eeaf LP |
2543 | /* The passed 'fd' was opened with O_PATH. We need to convert it into a 'regular' fd before |
2544 | * reading the directory content. */ | |
2545 | d = opendir(FORMAT_PROC_FD_PATH(fd)); | |
936f6bdb | 2546 | if (!d) { |
ddb6eeaf | 2547 | log_error_errno(errno, "Failed to opendir() '%s': %m", FORMAT_PROC_FD_PATH(fd)); |
09f12798 LP |
2548 | if (r == 0) |
2549 | r = -errno; | |
936f6bdb FB |
2550 | goto finish; |
2551 | } | |
a8d88783 | 2552 | |
936f6bdb | 2553 | FOREACH_DIRENT_ALL(de, d, q = -errno; goto finish) { |
936f6bdb | 2554 | int de_fd; |
a8d88783 | 2555 | |
936f6bdb FB |
2556 | if (dot_or_dot_dot(de->d_name)) |
2557 | continue; | |
2558 | ||
2559 | de_fd = openat(fd, de->d_name, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
9d874aec LP |
2560 | if (de_fd < 0) |
2561 | q = log_error_errno(errno, "Failed to open() file '%s': %m", de->d_name); | |
4dc7bfdf FB |
2562 | else { |
2563 | _cleanup_free_ char *de_path = NULL; | |
2564 | ||
62a85ee0 | 2565 | de_path = path_join(path, de->d_name); |
4dc7bfdf FB |
2566 | if (!de_path) |
2567 | q = log_oom(); | |
2568 | else | |
2569 | /* Pass ownership of dirent fd over */ | |
a5e4f3ab | 2570 | q = item_do(c, i, de_fd, de_path, CREATION_EXISTING, action); |
4dc7bfdf | 2571 | } |
a8d88783 | 2572 | |
e73a03e0 LP |
2573 | if (q < 0 && r == 0) |
2574 | r = q; | |
a8d88783 | 2575 | } |
a8d88783 | 2576 | } |
936f6bdb FB |
2577 | finish: |
2578 | safe_close(fd); | |
e73a03e0 | 2579 | return r; |
a8d88783 MS |
2580 | } |
2581 | ||
a5e4f3ab | 2582 | static int glob_item(Context *c, Item *i, action_t action) { |
df99a9ef | 2583 | _cleanup_globfree_ glob_t g = { |
ebf31a1f | 2584 | .gl_opendir = (void *(*)(const char *)) opendir_nomod, |
df99a9ef | 2585 | }; |
e73a03e0 | 2586 | int r = 0, k; |
99e68c0b | 2587 | |
a5e4f3ab LP |
2588 | assert(c); |
2589 | assert(i); | |
2590 | ||
84e72b5e ZJS |
2591 | k = safe_glob(i->path, GLOB_NOSORT|GLOB_BRACE, &g); |
2592 | if (k < 0 && k != -ENOENT) | |
2593 | return log_error_errno(k, "glob(%s) failed: %m", i->path); | |
99e68c0b | 2594 | |
c84a9488 | 2595 | STRV_FOREACH(fn, g.gl_pathv) { |
a9bc518c | 2596 | /* We pass CREATION_EXISTING here, since if we are globbing for it, it always has to exist */ |
a5e4f3ab | 2597 | k = action(c, i, *fn, CREATION_EXISTING); |
e73a03e0 | 2598 | if (k < 0 && r == 0) |
99e68c0b | 2599 | r = k; |
936f6bdb FB |
2600 | } |
2601 | ||
2602 | return r; | |
2603 | } | |
081043cf | 2604 | |
a5e4f3ab LP |
2605 | static int glob_item_recursively( |
2606 | Context *c, | |
2607 | Item *i, | |
2608 | fdaction_t action) { | |
2609 | ||
936f6bdb FB |
2610 | _cleanup_globfree_ glob_t g = { |
2611 | .gl_opendir = (void *(*)(const char *)) opendir_nomod, | |
2612 | }; | |
2613 | int r = 0, k; | |
936f6bdb FB |
2614 | |
2615 | k = safe_glob(i->path, GLOB_NOSORT|GLOB_BRACE, &g); | |
2616 | if (k < 0 && k != -ENOENT) | |
2617 | return log_error_errno(k, "glob(%s) failed: %m", i->path); | |
2618 | ||
2619 | STRV_FOREACH(fn, g.gl_pathv) { | |
254d1313 | 2620 | _cleanup_close_ int fd = -EBADF; |
936f6bdb FB |
2621 | |
2622 | /* Make sure we won't trigger/follow file object (such as | |
2623 | * device nodes, automounts, ...) pointed out by 'fn' with | |
2624 | * O_PATH. Note, when O_PATH is used, flags other than | |
2625 | * O_CLOEXEC, O_DIRECTORY, and O_NOFOLLOW are ignored. */ | |
2626 | ||
2627 | fd = open(*fn, O_CLOEXEC|O_NOFOLLOW|O_PATH); | |
2628 | if (fd < 0) { | |
09f12798 LP |
2629 | log_error_errno(errno, "Opening '%s' failed: %m", *fn); |
2630 | if (r == 0) | |
2631 | r = -errno; | |
936f6bdb FB |
2632 | continue; |
2633 | } | |
2634 | ||
a5e4f3ab | 2635 | k = item_do(c, i, fd, *fn, CREATION_EXISTING, action); |
936f6bdb FB |
2636 | if (k < 0 && r == 0) |
2637 | r = k; | |
2638 | ||
2639 | /* we passed fd ownership to the previous call */ | |
254d1313 | 2640 | fd = -EBADF; |
c84a9488 | 2641 | } |
99e68c0b | 2642 | |
99e68c0b MS |
2643 | return r; |
2644 | } | |
2645 | ||
c46c3233 AW |
2646 | static int rm_if_wrong_type_safe( |
2647 | mode_t mode, | |
2648 | int parent_fd, | |
21e43a7c | 2649 | const struct stat *parent_st, /* Only used if follow_links below is true. */ |
c46c3233 AW |
2650 | const char *name, |
2651 | int flags) { | |
2652 | _cleanup_free_ char *parent_name = NULL; | |
2653 | bool follow_links = !FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW); | |
2654 | struct stat st; | |
2655 | int r; | |
2656 | ||
2657 | assert(name); | |
2658 | assert((mode & ~S_IFMT) == 0); | |
2659 | assert(!follow_links || parent_st); | |
2660 | assert((flags & ~AT_SYMLINK_NOFOLLOW) == 0); | |
2661 | ||
117aa014 ZJS |
2662 | if (mode == 0) |
2663 | return 0; | |
2664 | ||
c46c3233 AW |
2665 | if (!filename_is_valid(name)) |
2666 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "\"%s\" is not a valid filename.", name); | |
2667 | ||
2668 | r = fstatat_harder(parent_fd, name, &st, flags, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE); | |
2669 | if (r < 0) { | |
2670 | (void) fd_get_path(parent_fd, &parent_name); | |
2671 | return log_full_errno(r == -ENOENT? LOG_DEBUG : LOG_ERR, r, | |
9e90d4f4 | 2672 | "Failed to stat \"%s/%s\": %m", parent_name ?: "...", name); |
c46c3233 AW |
2673 | } |
2674 | ||
2675 | /* Fail before removing anything if this is an unsafe transition. */ | |
2676 | if (follow_links && unsafe_transition(parent_st, &st)) { | |
2677 | (void) fd_get_path(parent_fd, &parent_name); | |
2678 | return log_error_errno(SYNTHETIC_ERRNO(ENOLINK), | |
9e90d4f4 | 2679 | "Unsafe transition from \"%s\" to \"%s\".", parent_name ?: "...", name); |
c46c3233 AW |
2680 | } |
2681 | ||
2682 | if ((st.st_mode & S_IFMT) == mode) | |
2683 | return 0; | |
2684 | ||
2685 | (void) fd_get_path(parent_fd, &parent_name); | |
9e90d4f4 | 2686 | log_notice("Wrong file type 0o%o; rm -rf \"%s/%s\"", st.st_mode & S_IFMT, parent_name ?: "...", name); |
c46c3233 AW |
2687 | |
2688 | /* If the target of the symlink was the wrong type, the link needs to be removed instead of the | |
2689 | * target, so make sure it is identified as a link and not a directory. */ | |
2690 | if (follow_links) { | |
2691 | r = fstatat_harder(parent_fd, name, &st, AT_SYMLINK_NOFOLLOW, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE); | |
2692 | if (r < 0) | |
9e90d4f4 | 2693 | return log_error_errno(r, "Failed to stat \"%s/%s\": %m", parent_name ?: "...", name); |
c46c3233 AW |
2694 | } |
2695 | ||
2696 | /* Do not remove mount points. */ | |
2697 | r = fd_is_mount_point(parent_fd, name, follow_links ? AT_SYMLINK_FOLLOW : 0); | |
2698 | if (r < 0) | |
9e90d4f4 ZJS |
2699 | (void) log_warning_errno(r, "Failed to check if \"%s/%s\" is a mount point: %m; continuing.", |
2700 | parent_name ?: "...", name); | |
c46c3233 AW |
2701 | else if (r > 0) |
2702 | return log_error_errno(SYNTHETIC_ERRNO(EBUSY), | |
9e90d4f4 | 2703 | "Not removing \"%s/%s\" because it is a mount point.", parent_name ?: "...", name); |
c46c3233 | 2704 | |
9e90d4f4 ZJS |
2705 | log_action("Would remove", "Removing", "%s %s/%s", parent_name ?: "...", name); |
2706 | if (!arg_dry_run) { | |
2707 | if ((st.st_mode & S_IFMT) == S_IFDIR) { | |
2708 | _cleanup_close_ int child_fd = -EBADF; | |
c46c3233 | 2709 | |
9e90d4f4 ZJS |
2710 | child_fd = openat(parent_fd, name, O_NOCTTY | O_CLOEXEC | O_DIRECTORY); |
2711 | if (child_fd < 0) | |
2712 | return log_error_errno(errno, "Failed to open \"%s/%s\": %m", parent_name ?: "...", name); | |
c46c3233 | 2713 | |
9e90d4f4 ZJS |
2714 | r = rm_rf_children(TAKE_FD(child_fd), REMOVE_ROOT|REMOVE_SUBVOLUME|REMOVE_PHYSICAL, &st); |
2715 | if (r < 0) | |
2716 | return log_error_errno(r, "Failed to remove contents of \"%s/%s\": %m", parent_name ?: "...", name); | |
c46c3233 | 2717 | |
9e90d4f4 ZJS |
2718 | r = unlinkat_harder(parent_fd, name, AT_REMOVEDIR, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE); |
2719 | } else | |
2720 | r = unlinkat_harder(parent_fd, name, 0, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE); | |
2721 | if (r < 0) | |
2722 | return log_error_errno(r, "Failed to remove \"%s/%s\": %m", parent_name ?: "...", name); | |
2723 | } | |
c46c3233 | 2724 | |
9e90d4f4 ZJS |
2725 | /* This is covered by the log_notice "Wrong file type...". |
2726 | * It is logged earlier because it gives context to other error messages that might follow. */ | |
c46c3233 AW |
2727 | return -ENOENT; |
2728 | } | |
2729 | ||
2730 | /* If child_mode is non-zero, rm_if_wrong_type_safe will be executed for the last path component. */ | |
2731 | static int mkdir_parents_rm_if_wrong_type(mode_t child_mode, const char *path) { | |
254d1313 | 2732 | _cleanup_close_ int parent_fd = -EBADF; |
c46c3233 | 2733 | struct stat parent_st; |
c46c3233 | 2734 | size_t path_len; |
21e43a7c | 2735 | int r; |
c46c3233 AW |
2736 | |
2737 | assert(path); | |
2738 | assert((child_mode & ~S_IFMT) == 0); | |
2739 | ||
2740 | path_len = strlen(path); | |
2741 | ||
2742 | if (!is_path(path)) | |
2743 | /* rm_if_wrong_type_safe already logs errors. */ | |
117aa014 | 2744 | return rm_if_wrong_type_safe(child_mode, AT_FDCWD, NULL, path, AT_SYMLINK_NOFOLLOW); |
c46c3233 AW |
2745 | |
2746 | if (child_mode != 0 && endswith(path, "/")) | |
2747 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2748 | "Trailing path separators are only allowed if child_mode is not set; got \"%s\"", path); | |
2749 | ||
2750 | /* Get the parent_fd and stat. */ | |
c68cafba YW |
2751 | parent_fd = openat(AT_FDCWD, path_is_absolute(path) ? "/" : ".", O_NOCTTY | O_CLOEXEC | O_DIRECTORY); |
2752 | if (parent_fd < 0) | |
2753 | return log_error_errno(errno, "Failed to open root: %m"); | |
2754 | ||
c46c3233 AW |
2755 | if (fstat(parent_fd, &parent_st) < 0) |
2756 | return log_error_errno(errno, "Failed to stat root: %m"); | |
2757 | ||
2758 | /* Check every parent directory in the path, except the last component */ | |
21e43a7c | 2759 | for (const char *e = path;;) { |
254d1313 | 2760 | _cleanup_close_ int next_fd = -EBADF; |
c46c3233 | 2761 | char t[path_len + 1]; |
21e43a7c | 2762 | const char *s; |
c46c3233 AW |
2763 | |
2764 | /* Find the start of the next path component. */ | |
2765 | s = e + strspn(e, "/"); | |
2766 | /* Find the end of the next path component. */ | |
2767 | e = s + strcspn(s, "/"); | |
2768 | ||
2769 | /* Copy the path component to t so it can be a null terminated string. */ | |
2770 | *((char*) mempcpy(t, s, e - s)) = 0; | |
2771 | ||
2772 | /* Is this the last component? If so, then check the type */ | |
2773 | if (*e == 0) | |
117aa014 | 2774 | return rm_if_wrong_type_safe(child_mode, parent_fd, &parent_st, t, AT_SYMLINK_NOFOLLOW); |
c46c3233 | 2775 | |
21e43a7c YW |
2776 | r = rm_if_wrong_type_safe(S_IFDIR, parent_fd, &parent_st, t, 0); |
2777 | /* Remove dangling symlinks. */ | |
2778 | if (r == -ENOENT) | |
2779 | r = rm_if_wrong_type_safe(S_IFDIR, parent_fd, &parent_st, t, AT_SYMLINK_NOFOLLOW); | |
c46c3233 | 2780 | if (r == -ENOENT) { |
9e90d4f4 ZJS |
2781 | if (!arg_dry_run) { |
2782 | WITH_UMASK(0000) | |
2783 | r = mkdirat_label(parent_fd, t, 0755); | |
2784 | if (r < 0) { | |
2785 | _cleanup_free_ char *parent_name = NULL; | |
2786 | ||
2787 | (void) fd_get_path(parent_fd, &parent_name); | |
2788 | return log_error_errno(r, "Failed to mkdir \"%s\" at \"%s\": %m", t, strnull(parent_name)); | |
2789 | } | |
c46c3233 AW |
2790 | } |
2791 | } else if (r < 0) | |
2792 | /* rm_if_wrong_type_safe already logs errors. */ | |
2793 | return r; | |
2794 | ||
7c248223 | 2795 | next_fd = RET_NERRNO(openat(parent_fd, t, O_NOCTTY | O_CLOEXEC | O_DIRECTORY)); |
c46c3233 | 2796 | if (next_fd < 0) { |
21e43a7c YW |
2797 | _cleanup_free_ char *parent_name = NULL; |
2798 | ||
c46c3233 | 2799 | (void) fd_get_path(parent_fd, &parent_name); |
7c248223 | 2800 | return log_error_errno(next_fd, "Failed to open \"%s\" at \"%s\": %m", t, strnull(parent_name)); |
c46c3233 | 2801 | } |
7c248223 LP |
2802 | r = RET_NERRNO(fstat(next_fd, &parent_st)); |
2803 | if (r < 0) { | |
21e43a7c YW |
2804 | _cleanup_free_ char *parent_name = NULL; |
2805 | ||
c46c3233 | 2806 | (void) fd_get_path(parent_fd, &parent_name); |
21e43a7c | 2807 | return log_error_errno(r, "Failed to stat \"%s\" at \"%s\": %m", t, strnull(parent_name)); |
c46c3233 AW |
2808 | } |
2809 | ||
ee3455cf | 2810 | close_and_replace(parent_fd, next_fd); |
c46c3233 AW |
2811 | } |
2812 | } | |
2813 | ||
2814 | static int mkdir_parents_item(Item *i, mode_t child_mode) { | |
2815 | int r; | |
9e90d4f4 | 2816 | |
c46c3233 AW |
2817 | if (i->try_replace) { |
2818 | r = mkdir_parents_rm_if_wrong_type(child_mode, i->path); | |
2819 | if (r < 0 && r != -ENOENT) | |
2820 | return r; | |
2821 | } else | |
2053593f | 2822 | WITH_UMASK(0000) |
9e90d4f4 ZJS |
2823 | if (!arg_dry_run) |
2824 | (void) mkdir_parents_label(i->path, 0755); | |
c46c3233 AW |
2825 | |
2826 | return 0; | |
2827 | } | |
2828 | ||
a5e4f3ab | 2829 | static int create_item(Context *c, Item *i) { |
9e430ce3 | 2830 | int r; |
5008d581 | 2831 | |
a5e4f3ab | 2832 | assert(c); |
3b63d2d3 | 2833 | assert(i); |
5008d581 | 2834 | |
582deb84 ZJS |
2835 | log_debug("Running create action for entry %c %s", (char) i->type, i->path); |
2836 | ||
3b63d2d3 LP |
2837 | switch (i->type) { |
2838 | ||
2839 | case IGNORE_PATH: | |
78a92a5a | 2840 | case IGNORE_DIRECTORY_PATH: |
3b63d2d3 LP |
2841 | case REMOVE_PATH: |
2842 | case RECURSIVE_REMOVE_PATH: | |
2843 | return 0; | |
5008d581 | 2844 | |
eccebf4b | 2845 | case TRUNCATE_FILE: |
3b63d2d3 | 2846 | case CREATE_FILE: |
c46c3233 AW |
2847 | r = mkdir_parents_item(i, S_IFREG); |
2848 | if (r < 0) | |
2849 | return r; | |
7fa10748 | 2850 | |
eccebf4b | 2851 | if ((i->type == CREATE_FILE && i->append_or_force) || i->type == TRUNCATE_FILE) |
a5e4f3ab | 2852 | r = truncate_file(c, i, i->path); |
eccebf4b | 2853 | else |
a5e4f3ab | 2854 | r = create_file(c, i, i->path); |
5ec9d065 FB |
2855 | if (r < 0) |
2856 | return r; | |
2857 | break; | |
2858 | ||
7b887f22 | 2859 | case COPY_FILES: |
c46c3233 AW |
2860 | r = mkdir_parents_item(i, 0); |
2861 | if (r < 0) | |
2862 | return r; | |
7fa10748 | 2863 | |
a5e4f3ab | 2864 | r = copy_files(c, i); |
849958d1 LP |
2865 | if (r < 0) |
2866 | return r; | |
849958d1 LP |
2867 | break; |
2868 | ||
d4e9eb91 | 2869 | case WRITE_FILE: |
a5e4f3ab | 2870 | r = glob_item(c, i, write_one_file); |
f05bc3f7 MS |
2871 | if (r < 0) |
2872 | return r; | |
5008d581 | 2873 | |
3b63d2d3 LP |
2874 | break; |
2875 | ||
3b63d2d3 | 2876 | case CREATE_DIRECTORY: |
d7b8eec7 | 2877 | case TRUNCATE_DIRECTORY: |
c46c3233 AW |
2878 | r = mkdir_parents_item(i, S_IFDIR); |
2879 | if (r < 0) | |
2880 | return r; | |
4c39d899 | 2881 | |
a5e4f3ab | 2882 | r = create_directory(c, i, i->path); |
4c39d899 FB |
2883 | if (r < 0) |
2884 | return r; | |
2885 | break; | |
2886 | ||
d7b8eec7 | 2887 | case CREATE_SUBVOLUME: |
5fb13eb5 LP |
2888 | case CREATE_SUBVOLUME_INHERIT_QUOTA: |
2889 | case CREATE_SUBVOLUME_NEW_QUOTA: | |
c46c3233 AW |
2890 | r = mkdir_parents_item(i, S_IFDIR); |
2891 | if (r < 0) | |
2892 | return r; | |
d7b8eec7 | 2893 | |
a5e4f3ab | 2894 | r = create_subvolume(c, i, i->path); |
4ad36844 FB |
2895 | if (r < 0) |
2896 | return r; | |
2897 | break; | |
5fb13eb5 | 2898 | |
df8dee85 | 2899 | case EMPTY_DIRECTORY: |
a5e4f3ab | 2900 | r = glob_item(c, i, empty_directory); |
f05bc3f7 MS |
2901 | if (r < 0) |
2902 | return r; | |
3b63d2d3 | 2903 | break; |
ee17ee7c LP |
2904 | |
2905 | case CREATE_FIFO: | |
c46c3233 AW |
2906 | r = mkdir_parents_item(i, S_IFIFO); |
2907 | if (r < 0) | |
2908 | return r; | |
7fa10748 | 2909 | |
a5e4f3ab | 2910 | r = create_fifo(c, i); |
f05bc3f7 MS |
2911 | if (r < 0) |
2912 | return r; | |
ee17ee7c | 2913 | break; |
a8d88783 | 2914 | |
9e430ce3 | 2915 | case CREATE_SYMLINK: |
c46c3233 AW |
2916 | r = mkdir_parents_item(i, S_IFLNK); |
2917 | if (r < 0) | |
2918 | return r; | |
7fa10748 | 2919 | |
a5e4f3ab | 2920 | r = create_symlink(c, i); |
9e430ce3 LP |
2921 | if (r < 0) |
2922 | return r; | |
a542c4dc | 2923 | |
468d726b | 2924 | break; |
468d726b LP |
2925 | |
2926 | case CREATE_BLOCK_DEVICE: | |
074bd73f | 2927 | case CREATE_CHAR_DEVICE: |
26c45a6c | 2928 | if (have_effective_cap(CAP_MKNOD) <= 0) { |
713998cf LP |
2929 | /* In a container we lack CAP_MKNOD. We shouldn't attempt to create the device node in that |
2930 | * case to avoid noise, and we don't support virtualized devices in containers anyway. */ | |
cb7ed9df LP |
2931 | |
2932 | log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path); | |
2933 | return 0; | |
2934 | } | |
2935 | ||
c46c3233 AW |
2936 | r = mkdir_parents_item(i, i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR); |
2937 | if (r < 0) | |
2938 | return r; | |
7fa10748 | 2939 | |
a5e4f3ab | 2940 | r = create_device(c, i, i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR); |
468d726b LP |
2941 | if (r < 0) |
2942 | return r; | |
2943 | ||
2944 | break; | |
468d726b | 2945 | |
e73a03e0 | 2946 | case ADJUST_MODE: |
777b87e7 | 2947 | case RELABEL_PATH: |
a5e4f3ab | 2948 | r = glob_item(c, i, path_set_perms); |
777b87e7 | 2949 | if (r < 0) |
96ca8194 | 2950 | return r; |
777b87e7 MS |
2951 | break; |
2952 | ||
a8d88783 | 2953 | case RECURSIVE_RELABEL_PATH: |
a5e4f3ab | 2954 | r = glob_item_recursively(c, i, fd_set_perms); |
a8d88783 MS |
2955 | if (r < 0) |
2956 | return r; | |
ebf4e801 | 2957 | break; |
e73a03e0 | 2958 | |
ebf4e801 | 2959 | case SET_XATTR: |
a5e4f3ab | 2960 | r = glob_item(c, i, path_set_xattrs); |
b705ab6a ZJS |
2961 | if (r < 0) |
2962 | return r; | |
2963 | break; | |
2964 | ||
2965 | case RECURSIVE_SET_XATTR: | |
a5e4f3ab | 2966 | r = glob_item_recursively(c, i, fd_set_xattrs); |
ebf4e801 MW |
2967 | if (r < 0) |
2968 | return r; | |
e73a03e0 | 2969 | break; |
f8eeeaf9 ZJS |
2970 | |
2971 | case SET_ACL: | |
a5e4f3ab | 2972 | r = glob_item(c, i, path_set_acls); |
f8eeeaf9 ZJS |
2973 | if (r < 0) |
2974 | return r; | |
b705ab6a ZJS |
2975 | break; |
2976 | ||
2977 | case RECURSIVE_SET_ACL: | |
a5e4f3ab | 2978 | r = glob_item_recursively(c, i, fd_set_acls); |
b705ab6a ZJS |
2979 | if (r < 0) |
2980 | return r; | |
2981 | break; | |
22c3a6ca | 2982 | |
88ec4dfa | 2983 | case SET_ATTRIBUTE: |
a5e4f3ab | 2984 | r = glob_item(c, i, path_set_attribute); |
22c3a6ca GB |
2985 | if (r < 0) |
2986 | return r; | |
2987 | break; | |
2988 | ||
88ec4dfa | 2989 | case RECURSIVE_SET_ATTRIBUTE: |
a5e4f3ab | 2990 | r = glob_item_recursively(c, i, fd_set_attribute); |
22c3a6ca GB |
2991 | if (r < 0) |
2992 | return r; | |
2993 | break; | |
3b63d2d3 LP |
2994 | } |
2995 | ||
f05bc3f7 | 2996 | return 0; |
3b63d2d3 LP |
2997 | } |
2998 | ||
b1935cc9 ZJS |
2999 | static int remove_recursive( |
3000 | Context *c, | |
3001 | Item *i, | |
3002 | const char *instance, | |
3003 | bool remove_instance) { | |
3004 | ||
3005 | _cleanup_closedir_ DIR *d = NULL; | |
3006 | STRUCT_STATX_DEFINE(sx); | |
3007 | bool mountpoint; | |
81a18380 LB |
3008 | int r; |
3009 | ||
b1935cc9 ZJS |
3010 | r = opendir_and_stat(instance, &d, &sx, &mountpoint); |
3011 | if (r < 0) | |
3012 | return r; | |
3013 | if (r == 0) { | |
3014 | if (remove_instance) { | |
9e90d4f4 ZJS |
3015 | log_action("Would remove", "Removing", "%s file \"%s\".", instance); |
3016 | if (!arg_dry_run && | |
3017 | remove(instance) < 0 && | |
3018 | errno != ENOENT) | |
b1935cc9 ZJS |
3019 | return log_error_errno(errno, "rm %s: %m", instance); |
3020 | } | |
3021 | return 0; | |
3022 | } | |
3023 | ||
3024 | r = dir_cleanup(c, i, instance, d, | |
3025 | /* self_atime_nsec= */ NSEC_INFINITY, | |
3026 | /* self_mtime_nsec= */ NSEC_INFINITY, | |
3027 | /* cutoff_nsec= */ NSEC_INFINITY, | |
3028 | sx.stx_dev_major, sx.stx_dev_minor, | |
3029 | mountpoint, | |
3030 | MAX_DEPTH, | |
3031 | /* keep_this_level= */ false, | |
3032 | /* age_by_file= */ 0, | |
3033 | /* age_by_dir= */ 0); | |
3034 | if (r < 0) | |
3035 | return r; | |
81a18380 | 3036 | |
b1935cc9 ZJS |
3037 | if (remove_instance) { |
3038 | log_debug("Removing directory \"%s\".", instance); | |
3039 | r = RET_NERRNO(rmdir(instance)); | |
3040 | if (r < 0 && !IN_SET(r, -ENOENT, -ENOTEMPTY)) | |
3041 | return log_error_errno(r, "Failed to remove %s: %m", instance); | |
3042 | } | |
81a18380 LB |
3043 | return 0; |
3044 | } | |
3045 | ||
b1935cc9 ZJS |
3046 | static int purge_item_instance(Context *c, Item *i, const char *instance, CreationMode creation) { |
3047 | return remove_recursive(c, i, instance, /* remove_instance= */ true); | |
3048 | } | |
81a18380 | 3049 | |
b1935cc9 | 3050 | static int purge_item(Context *c, Item *i) { |
81a18380 LB |
3051 | assert(i); |
3052 | ||
3053 | if (!needs_purge(i->type)) | |
3054 | return 0; | |
3055 | ||
b1935cc9 | 3056 | log_debug("Running purge action for entry %c %s", (char) i->type, i->path); |
81a18380 LB |
3057 | |
3058 | if (needs_glob(i->type)) | |
3059 | return glob_item(c, i, purge_item_instance); | |
3060 | ||
3061 | return purge_item_instance(c, i, i->path, CREATION_EXISTING); | |
3062 | } | |
3063 | ||
a5e4f3ab LP |
3064 | static int remove_item_instance( |
3065 | Context *c, | |
3066 | Item *i, | |
3067 | const char *instance, | |
3068 | CreationMode creation) { | |
3069 | ||
a5e4f3ab | 3070 | assert(c); |
3b63d2d3 LP |
3071 | assert(i); |
3072 | ||
3073 | switch (i->type) { | |
3074 | ||
3b63d2d3 | 3075 | case REMOVE_PATH: |
9e90d4f4 ZJS |
3076 | log_action("Would remove", "Removing", "%s \"%s\".", instance); |
3077 | if (!arg_dry_run && | |
3078 | remove(instance) < 0 && | |
3079 | errno != ENOENT) | |
b1935cc9 | 3080 | return log_error_errno(errno, "rm %s: %m", instance); |
3b63d2d3 | 3081 | |
b1935cc9 | 3082 | return 0; |
3b63d2d3 | 3083 | |
3b63d2d3 | 3084 | case RECURSIVE_REMOVE_PATH: |
b1935cc9 | 3085 | return remove_recursive(c, i, instance, /* remove_instance= */ true); |
7fcb4b9b ZJS |
3086 | |
3087 | default: | |
04499a70 | 3088 | assert_not_reached(); |
3b63d2d3 | 3089 | } |
3b63d2d3 LP |
3090 | } |
3091 | ||
a5e4f3ab | 3092 | static int remove_item(Context *c, Item *i) { |
a5e4f3ab | 3093 | assert(c); |
b8bb3e8f LP |
3094 | assert(i); |
3095 | ||
582deb84 ZJS |
3096 | log_debug("Running remove action for entry %c %s", (char) i->type, i->path); |
3097 | ||
b8bb3e8f LP |
3098 | switch (i->type) { |
3099 | ||
b8bb3e8f | 3100 | case TRUNCATE_DIRECTORY: |
b1935cc9 | 3101 | return remove_recursive(c, i, i->path, /* remove_instance= */ false); |
2c575977 LP |
3102 | |
3103 | case REMOVE_PATH: | |
99e68c0b | 3104 | case RECURSIVE_REMOVE_PATH: |
a5e4f3ab | 3105 | return glob_item(c, i, remove_item_instance); |
b8bb3e8f | 3106 | |
df8dee85 ZJS |
3107 | default: |
3108 | return 0; | |
3109 | } | |
b8bb3e8f LP |
3110 | } |
3111 | ||
7f7a50dd SK |
3112 | static char *age_by_to_string(AgeBy ab, bool is_dir) { |
3113 | static const char ab_map[] = { 'a', 'b', 'c', 'm' }; | |
3114 | size_t j = 0; | |
3115 | char *ret; | |
3116 | ||
3117 | ret = new(char, ELEMENTSOF(ab_map) + 1); | |
3118 | if (!ret) | |
3119 | return NULL; | |
3120 | ||
3121 | for (size_t i = 0; i < ELEMENTSOF(ab_map); i++) | |
3122 | if (FLAGS_SET(ab, 1U << i)) | |
3123 | ret[j++] = is_dir ? ascii_toupper(ab_map[i]) : ab_map[i]; | |
3124 | ||
3125 | ret[j] = 0; | |
3126 | return ret; | |
3127 | } | |
3128 | ||
a9bc518c | 3129 | static int clean_item_instance( |
a5e4f3ab | 3130 | Context *c, |
a9bc518c LP |
3131 | Item *i, |
3132 | const char* instance, | |
3133 | CreationMode creation) { | |
3134 | ||
78a92a5a MS |
3135 | assert(i); |
3136 | ||
3137 | if (!i->age_set) | |
3138 | return 0; | |
3139 | ||
7eeda1da | 3140 | usec_t n = now(CLOCK_REALTIME); |
78a92a5a MS |
3141 | if (n < i->age) |
3142 | return 0; | |
3143 | ||
7eeda1da | 3144 | usec_t cutoff = n - i->age; |
78a92a5a | 3145 | |
7eeda1da ZJS |
3146 | _cleanup_closedir_ DIR *d = NULL; |
3147 | STRUCT_STATX_DEFINE(sx); | |
3148 | bool mountpoint; | |
3149 | int r; | |
78a92a5a | 3150 | |
7eeda1da ZJS |
3151 | r = opendir_and_stat(instance, &d, &sx, &mountpoint); |
3152 | if (r <= 0) | |
3153 | return r; | |
78a92a5a | 3154 | |
7f7a50dd SK |
3155 | if (DEBUG_LOGGING) { |
3156 | _cleanup_free_ char *ab_f = NULL, *ab_d = NULL; | |
3157 | ||
3158 | ab_f = age_by_to_string(i->age_by_file, false); | |
3159 | if (!ab_f) | |
3160 | return log_oom(); | |
3161 | ||
3162 | ab_d = age_by_to_string(i->age_by_dir, true); | |
3163 | if (!ab_d) | |
3164 | return log_oom(); | |
3165 | ||
3166 | log_debug("Cleanup threshold for %s \"%s\" is %s; age-by: %s%s", | |
3167 | mountpoint ? "mount point" : "directory", | |
3168 | instance, | |
0086ef19 | 3169 | FORMAT_TIMESTAMP_STYLE(cutoff, TIMESTAMP_US), |
7f7a50dd SK |
3170 | ab_f, ab_d); |
3171 | } | |
582deb84 | 3172 | |
a5e4f3ab | 3173 | return dir_cleanup(c, i, instance, d, |
41fea218 AV |
3174 | statx_timestamp_load_nsec(&sx.stx_atime), |
3175 | statx_timestamp_load_nsec(&sx.stx_mtime), | |
ca194a2a | 3176 | cutoff * NSEC_PER_USEC, |
7eeda1da ZJS |
3177 | sx.stx_dev_major, sx.stx_dev_minor, |
3178 | mountpoint, | |
7f7a50dd SK |
3179 | MAX_DEPTH, i->keep_first_level, |
3180 | i->age_by_file, i->age_by_dir); | |
78a92a5a MS |
3181 | } |
3182 | ||
a5e4f3ab LP |
3183 | static int clean_item(Context *c, Item *i) { |
3184 | assert(c); | |
78a92a5a MS |
3185 | assert(i); |
3186 | ||
582deb84 ZJS |
3187 | log_debug("Running clean action for entry %c %s", (char) i->type, i->path); |
3188 | ||
78a92a5a | 3189 | switch (i->type) { |
a5e4f3ab | 3190 | |
78a92a5a | 3191 | case CREATE_DIRECTORY: |
9cb3204f | 3192 | case TRUNCATE_DIRECTORY: |
d7b8eec7 | 3193 | case CREATE_SUBVOLUME: |
5fb13eb5 LP |
3194 | case CREATE_SUBVOLUME_INHERIT_QUOTA: |
3195 | case CREATE_SUBVOLUME_NEW_QUOTA: | |
849958d1 | 3196 | case COPY_FILES: |
a5e4f3ab | 3197 | clean_item_instance(c, i, i->path, CREATION_EXISTING); |
df8dee85 | 3198 | return 0; |
a5e4f3ab | 3199 | |
65241c14 | 3200 | case EMPTY_DIRECTORY: |
9cb3204f | 3201 | case IGNORE_PATH: |
78a92a5a | 3202 | case IGNORE_DIRECTORY_PATH: |
a5e4f3ab LP |
3203 | return glob_item(c, i, clean_item_instance); |
3204 | ||
78a92a5a | 3205 | default: |
df8dee85 | 3206 | return 0; |
78a92a5a | 3207 | } |
78a92a5a MS |
3208 | } |
3209 | ||
a5e4f3ab LP |
3210 | static int process_item( |
3211 | Context *c, | |
3212 | Item *i, | |
3213 | OperationMask operation) { | |
3214 | ||
811a1587 | 3215 | OperationMask todo; |
bd6d28f2 ZJS |
3216 | _cleanup_free_ char *_path = NULL; |
3217 | const char *path; | |
81a18380 | 3218 | int r; |
3b63d2d3 | 3219 | |
a5e4f3ab | 3220 | assert(c); |
3b63d2d3 LP |
3221 | assert(i); |
3222 | ||
811a1587 LP |
3223 | todo = operation & ~i->done; |
3224 | if (todo == 0) /* Everything already done? */ | |
1910cd0e LP |
3225 | return 0; |
3226 | ||
811a1587 | 3227 | i->done |= operation; |
1910cd0e | 3228 | |
bd6d28f2 ZJS |
3229 | path = i->path; |
3230 | if (string_is_glob(path)) { | |
3231 | /* We can't easily check whether a glob matches any autofs path, so let's do the check only | |
3232 | * for the non-glob part. */ | |
3233 | ||
3234 | r = glob_non_glob_prefix(path, &_path); | |
3235 | if (r < 0 && r != -ENOENT) | |
3236 | return log_debug_errno(r, "Failed to deglob path: %m"); | |
3237 | if (r >= 0) | |
3238 | path = _path; | |
3239 | } | |
3240 | ||
f461a28d | 3241 | r = chase(path, arg_root, CHASE_NO_AUTOFS|CHASE_NONEXISTENT|CHASE_WARN, NULL, NULL); |
21af3386 | 3242 | if (r == -EREMOTE) { |
bd6d28f2 | 3243 | log_notice_errno(r, "Skipping %s", i->path); /* We log the configured path, to not confuse the user. */ |
811a1587 | 3244 | return 0; |
145b8d0f FB |
3245 | } |
3246 | if (r < 0) | |
48d96904 | 3247 | log_debug_errno(r, "Failed to determine whether '%s' is below autofs, ignoring: %m", i->path); |
655f2da0 | 3248 | |
a5e4f3ab | 3249 | r = FLAGS_SET(operation, OPERATION_CREATE) ? create_item(c, i) : 0; |
6d7b5433 WD |
3250 | /* Failure can only be tolerated for create */ |
3251 | if (i->allow_failure) | |
3252 | r = 0; | |
3b63d2d3 | 3253 | |
81a18380 LB |
3254 | RET_GATHER(r, FLAGS_SET(operation, OPERATION_REMOVE) ? remove_item(c, i) : 0); |
3255 | RET_GATHER(r, FLAGS_SET(operation, OPERATION_CLEAN) ? clean_item(c, i) : 0); | |
3256 | RET_GATHER(r, FLAGS_SET(operation, OPERATION_PURGE) ? purge_item(c, i) : 0); | |
1a967b6b | 3257 | |
81a18380 | 3258 | return r; |
3b63d2d3 LP |
3259 | } |
3260 | ||
a5e4f3ab LP |
3261 | static int process_item_array( |
3262 | Context *c, | |
3263 | ItemArray *array, | |
3264 | OperationMask operation) { | |
3265 | ||
96d10d78 | 3266 | int r = 0; |
753615e8 | 3267 | |
a5e4f3ab | 3268 | assert(c); |
3f93da98 ZJS |
3269 | assert(array); |
3270 | ||
811a1587 LP |
3271 | /* Create any parent first. */ |
3272 | if (FLAGS_SET(operation, OPERATION_CREATE) && array->parent) | |
a5e4f3ab | 3273 | r = process_item_array(c, array->parent, operation & OPERATION_CREATE); |
811a1587 LP |
3274 | |
3275 | /* Clean up all children first */ | |
81a18380 | 3276 | if ((operation & (OPERATION_REMOVE|OPERATION_CLEAN|OPERATION_PURGE)) && !set_isempty(array->children)) { |
a5e4f3ab | 3277 | ItemArray *cc; |
811a1587 | 3278 | |
e5876c29 ZJS |
3279 | SET_FOREACH(cc, array->children) |
3280 | RET_GATHER(r, process_item_array(c, cc, operation & (OPERATION_REMOVE|OPERATION_CLEAN|OPERATION_PURGE))); | |
811a1587 LP |
3281 | } |
3282 | ||
e5876c29 ZJS |
3283 | FOREACH_ARRAY(item, array->items, array->n_items) |
3284 | RET_GATHER(r, process_item(c, item, operation)); | |
3f93da98 ZJS |
3285 | |
3286 | return r; | |
3287 | } | |
3b63d2d3 | 3288 | |
3f93da98 ZJS |
3289 | static void item_free_contents(Item *i) { |
3290 | assert(i); | |
3b63d2d3 | 3291 | free(i->path); |
468d726b | 3292 | free(i->argument); |
708daf42 | 3293 | free(i->binary_argument); |
ebf4e801 | 3294 | strv_free(i->xattrs); |
f8eeeaf9 | 3295 | |
349cc4a5 | 3296 | #if HAVE_ACL |
9f804ab0 SJ |
3297 | if (i->acl_access) |
3298 | acl_free(i->acl_access); | |
3299 | ||
26d98cdd MY |
3300 | if (i->acl_access_exec) |
3301 | acl_free(i->acl_access_exec); | |
3302 | ||
9f804ab0 SJ |
3303 | if (i->acl_default) |
3304 | acl_free(i->acl_default); | |
f8eeeaf9 | 3305 | #endif |
3b63d2d3 LP |
3306 | } |
3307 | ||
87938c3b | 3308 | static ItemArray* item_array_free(ItemArray *a) { |
3f93da98 | 3309 | if (!a) |
87938c3b | 3310 | return NULL; |
3f93da98 | 3311 | |
e5876c29 ZJS |
3312 | FOREACH_ARRAY(item, a->items, a->n_items) |
3313 | item_free_contents(item); | |
96d10d78 | 3314 | |
811a1587 | 3315 | set_free(a->children); |
3f93da98 | 3316 | free(a->items); |
87938c3b | 3317 | return mfree(a); |
3f93da98 | 3318 | } |
e2f2fb78 | 3319 | |
93bab288 | 3320 | static int item_compare(const Item *a, const Item *b) { |
17493fa5 LP |
3321 | /* Make sure that the ownership taking item is put first, so |
3322 | * that we first create the node, and then can adjust it */ | |
3323 | ||
93bab288 | 3324 | if (takes_ownership(a->type) && !takes_ownership(b->type)) |
17493fa5 | 3325 | return -1; |
93bab288 | 3326 | if (!takes_ownership(a->type) && takes_ownership(b->type)) |
17493fa5 LP |
3327 | return 1; |
3328 | ||
93bab288 | 3329 | return CMP(a->type, b->type); |
17493fa5 LP |
3330 | } |
3331 | ||
a6aafd6a | 3332 | static bool item_compatible(const Item *a, const Item *b) { |
bfe95f35 LP |
3333 | assert(a); |
3334 | assert(b); | |
3f93da98 | 3335 | assert(streq(a->path, b->path)); |
bfe95f35 | 3336 | |
3f93da98 ZJS |
3337 | if (takes_ownership(a->type) && takes_ownership(b->type)) |
3338 | /* check if the items are the same */ | |
708daf42 LP |
3339 | return memcmp_nn(item_binary_argument(a), item_binary_argument_size(a), |
3340 | item_binary_argument(b), item_binary_argument_size(b)) == 0 && | |
bfe95f35 | 3341 | |
3f93da98 ZJS |
3342 | a->uid_set == b->uid_set && |
3343 | a->uid == b->uid && | |
cc43328c | 3344 | a->uid_only_create == b->uid_only_create && |
bfe95f35 | 3345 | |
3f93da98 ZJS |
3346 | a->gid_set == b->gid_set && |
3347 | a->gid == b->gid && | |
cc43328c | 3348 | a->gid_only_create == b->gid_only_create && |
bfe95f35 | 3349 | |
3f93da98 ZJS |
3350 | a->mode_set == b->mode_set && |
3351 | a->mode == b->mode && | |
cc43328c | 3352 | a->mode_only_create == b->mode_only_create && |
bfe95f35 | 3353 | |
3f93da98 ZJS |
3354 | a->age_set == b->age_set && |
3355 | a->age == b->age && | |
bfe95f35 | 3356 | |
7f7a50dd SK |
3357 | a->age_by_file == b->age_by_file && |
3358 | a->age_by_dir == b->age_by_dir && | |
3359 | ||
3f93da98 | 3360 | a->mask_perms == b->mask_perms && |
bfe95f35 | 3361 | |
3f93da98 | 3362 | a->keep_first_level == b->keep_first_level && |
468d726b | 3363 | |
3f93da98 | 3364 | a->major_minor == b->major_minor; |
468d726b | 3365 | |
bfe95f35 LP |
3366 | return true; |
3367 | } | |
3368 | ||
a2aced4a | 3369 | static bool should_include_path(const char *path) { |
abef3f91 | 3370 | STRV_FOREACH(prefix, arg_exclude_prefixes) |
582deb84 ZJS |
3371 | if (path_startswith(path, *prefix)) { |
3372 | log_debug("Entry \"%s\" matches exclude prefix \"%s\", skipping.", | |
3373 | path, *prefix); | |
5c795114 | 3374 | return false; |
582deb84 | 3375 | } |
a2aced4a | 3376 | |
abef3f91 | 3377 | STRV_FOREACH(prefix, arg_include_prefixes) |
582deb84 ZJS |
3378 | if (path_startswith(path, *prefix)) { |
3379 | log_debug("Entry \"%s\" matches include prefix \"%s\".", path, *prefix); | |
a2aced4a | 3380 | return true; |
582deb84 | 3381 | } |
a2aced4a | 3382 | |
6b000af4 | 3383 | /* no matches, so we should include this path only if we have no allow list at all */ |
7b943bb7 | 3384 | if (strv_isempty(arg_include_prefixes)) |
582deb84 ZJS |
3385 | return true; |
3386 | ||
3387 | log_debug("Entry \"%s\" does not match any include prefix, skipping.", path); | |
3388 | return false; | |
a2aced4a DR |
3389 | } |
3390 | ||
172e9cc3 | 3391 | static int specifier_expansion_from_arg(const Specifier *specifier_table, Item *i) { |
4cef1923 FB |
3392 | int r; |
3393 | ||
3394 | assert(i); | |
3395 | ||
4e361acc | 3396 | if (!i->argument) |
4cef1923 FB |
3397 | return 0; |
3398 | ||
3399 | switch (i->type) { | |
3400 | case COPY_FILES: | |
3401 | case CREATE_SYMLINK: | |
3402 | case CREATE_FILE: | |
3403 | case TRUNCATE_FILE: | |
e437538f ZJS |
3404 | case WRITE_FILE: { |
3405 | _cleanup_free_ char *unescaped = NULL, *resolved = NULL; | |
3406 | ssize_t l; | |
3407 | ||
3408 | l = cunescape(i->argument, 0, &unescaped); | |
3409 | if (l < 0) | |
3410 | return log_error_errno(l, "Failed to unescape parameter to write: %s", i->argument); | |
4cef1923 | 3411 | |
de61a04b | 3412 | r = specifier_printf(unescaped, PATH_MAX-1, specifier_table, arg_root, NULL, &resolved); |
4cef1923 FB |
3413 | if (r < 0) |
3414 | return r; | |
3415 | ||
e437538f ZJS |
3416 | return free_and_replace(i->argument, resolved); |
3417 | } | |
4cef1923 | 3418 | case SET_XATTR: |
de010b0b | 3419 | case RECURSIVE_SET_XATTR: |
dfe01841 | 3420 | STRV_FOREACH(xattr, i->xattrs) { |
e437538f ZJS |
3421 | _cleanup_free_ char *resolved = NULL; |
3422 | ||
de61a04b | 3423 | r = specifier_printf(*xattr, SIZE_MAX, specifier_table, arg_root, NULL, &resolved); |
4cef1923 FB |
3424 | if (r < 0) |
3425 | return r; | |
3426 | ||
3427 | free_and_replace(*xattr, resolved); | |
3428 | } | |
e437538f | 3429 | return 0; |
de010b0b | 3430 | |
4cef1923 | 3431 | default: |
e437538f | 3432 | return 0; |
4cef1923 | 3433 | } |
4cef1923 FB |
3434 | } |
3435 | ||
a2d1fb88 LP |
3436 | static int patch_var_run(const char *fname, unsigned line, char **path) { |
3437 | const char *k; | |
3438 | char *n; | |
3439 | ||
3440 | assert(path); | |
3441 | assert(*path); | |
3442 | ||
ad36bb9f ZJS |
3443 | /* Optionally rewrites lines referencing /var/run/, to use /run/ instead. Why bother? tmpfiles merges |
3444 | * lines in some cases and detects conflicts in others. If files/directories are specified through | |
3445 | * two equivalent lines this is problematic as neither case will be detected. Ideally we'd detect | |
3446 | * these cases by resolving symlinks early, but that's precisely not what we can do here as this code | |
3447 | * very likely is running very early on, at a time where the paths in question are not available yet, | |
3448 | * or even more importantly, our own tmpfiles rules might create the paths that are intermediary to | |
3449 | * the listed paths. We can't really cover the generic case, but the least we can do is cover the | |
3450 | * specific case of /var/run vs. /run, as /var/run is a legacy name for /run only, and we explicitly | |
3451 | * document that and require that on systemd systems the former is a symlink to the latter. Moreover | |
3452 | * files below this path are by far the primary use case for tmpfiles.d/. */ | |
a2d1fb88 LP |
3453 | |
3454 | k = path_startswith(*path, "/var/run/"); | |
ad36bb9f ZJS |
3455 | if (isempty(k)) /* Don't complain about paths other than under /var/run, |
3456 | * and not about /var/run itself either. */ | |
a2d1fb88 LP |
3457 | return 0; |
3458 | ||
2d9b74ba | 3459 | n = path_join("/run", k); |
a2d1fb88 LP |
3460 | if (!n) |
3461 | return log_oom(); | |
3462 | ||
ad36bb9f ZJS |
3463 | /* Also log about this briefly. We do so at LOG_NOTICE level, as we fixed up the situation |
3464 | * automatically, hence there's no immediate need for action by the user. However, in the interest of | |
3465 | * making things less confusing to the user, let's still inform the user that these snippets should | |
3466 | * really be updated. */ | |
b88ba6c7 ZJS |
3467 | log_syntax(NULL, LOG_NOTICE, fname, line, 0, |
3468 | "Line references path below legacy directory /var/run/, updating %s → %s; please update the tmpfiles.d/ drop-in file accordingly.", | |
3469 | *path, n); | |
a2d1fb88 | 3470 | |
81fa4479 | 3471 | free_and_replace(*path, n); |
a2d1fb88 LP |
3472 | |
3473 | return 0; | |
3474 | } | |
3475 | ||
a3451c2c LP |
3476 | static int find_uid(const char *user, uid_t *ret_uid, Hashmap **cache) { |
3477 | int r; | |
3478 | ||
3479 | assert(user); | |
3480 | assert(ret_uid); | |
3481 | ||
3482 | /* First: parse as numeric UID string */ | |
3483 | r = parse_uid(user, ret_uid); | |
3484 | if (r >= 0) | |
3485 | return r; | |
3486 | ||
3487 | /* Second: pass to NSS if we are running "online" */ | |
3488 | if (!arg_root) | |
3489 | return get_user_creds(&user, ret_uid, NULL, NULL, NULL, 0); | |
3490 | ||
3491 | /* Third, synthesize "root" unconditionally */ | |
3492 | if (streq(user, "root")) { | |
3493 | *ret_uid = 0; | |
3494 | return 0; | |
3495 | } | |
3496 | ||
3497 | /* Fourth: use fgetpwent() to read /etc/passwd directly, if we are "offline" */ | |
3498 | return name_to_uid_offline(arg_root, user, ret_uid, cache); | |
3499 | } | |
3500 | ||
3501 | static int find_gid(const char *group, gid_t *ret_gid, Hashmap **cache) { | |
3502 | int r; | |
3503 | ||
3504 | assert(group); | |
3505 | assert(ret_gid); | |
3506 | ||
3507 | /* First: parse as numeric GID string */ | |
3508 | r = parse_gid(group, ret_gid); | |
3509 | if (r >= 0) | |
3510 | return r; | |
3511 | ||
3512 | /* Second: pass to NSS if we are running "online" */ | |
3513 | if (!arg_root) | |
3514 | return get_group_creds(&group, ret_gid, 0); | |
3515 | ||
3516 | /* Third, synthesize "root" unconditionally */ | |
3517 | if (streq(group, "root")) { | |
3518 | *ret_gid = 0; | |
3519 | return 0; | |
3520 | } | |
3521 | ||
3522 | /* Fourth: use fgetgrent() to read /etc/group directly, if we are "offline" */ | |
3523 | return name_to_gid_offline(arg_root, group, ret_gid, cache); | |
3524 | } | |
3525 | ||
7f7a50dd SK |
3526 | static int parse_age_by_from_arg(const char *age_by_str, Item *item) { |
3527 | AgeBy ab_f = 0, ab_d = 0; | |
3528 | ||
3529 | static const struct { | |
3530 | char age_by_chr; | |
3531 | AgeBy age_by_flag; | |
3532 | } age_by_types[] = { | |
3533 | { 'a', AGE_BY_ATIME }, | |
3534 | { 'b', AGE_BY_BTIME }, | |
3535 | { 'c', AGE_BY_CTIME }, | |
3536 | { 'm', AGE_BY_MTIME }, | |
3537 | }; | |
3538 | ||
3539 | assert(age_by_str); | |
3540 | assert(item); | |
3541 | ||
3542 | if (isempty(age_by_str)) | |
3543 | return -EINVAL; | |
3544 | ||
3545 | for (const char *s = age_by_str; *s != 0; s++) { | |
3546 | size_t i; | |
3547 | ||
3548 | /* Ignore whitespace. */ | |
3549 | if (strchr(WHITESPACE, *s)) | |
3550 | continue; | |
3551 | ||
3552 | for (i = 0; i < ELEMENTSOF(age_by_types); i++) { | |
3553 | /* Check lower-case for files, upper-case for directories. */ | |
3554 | if (*s == age_by_types[i].age_by_chr) { | |
3555 | ab_f |= age_by_types[i].age_by_flag; | |
3556 | break; | |
3557 | } else if (*s == ascii_toupper(age_by_types[i].age_by_chr)) { | |
3558 | ab_d |= age_by_types[i].age_by_flag; | |
3559 | break; | |
3560 | } | |
3561 | } | |
3562 | ||
3563 | /* Invalid character. */ | |
3564 | if (i >= ELEMENTSOF(age_by_types)) | |
3565 | return -EINVAL; | |
3566 | } | |
3567 | ||
3568 | /* No match. */ | |
3569 | if (ab_f == 0 && ab_d == 0) | |
3570 | return -EINVAL; | |
3571 | ||
3572 | item->age_by_file = ab_f > 0 ? ab_f : AGE_BY_DEFAULT_FILE; | |
3573 | item->age_by_dir = ab_d > 0 ? ab_d : AGE_BY_DEFAULT_DIR; | |
3574 | ||
3575 | return 0; | |
3576 | } | |
3577 | ||
a6aafd6a | 3578 | static bool is_duplicated_item(ItemArray *existing, const Item *i) { |
9af74e0f FB |
3579 | assert(existing); |
3580 | assert(i); | |
3581 | ||
e5876c29 | 3582 | FOREACH_ARRAY(e, existing->items, existing->n_items) { |
9af74e0f FB |
3583 | if (item_compatible(e, i)) |
3584 | continue; | |
3585 | ||
3586 | /* Only multiple 'w+' lines for the same path are allowed. */ | |
3587 | if (e->type != WRITE_FILE || !e->append_or_force || | |
3588 | i->type != WRITE_FILE || !i->append_or_force) | |
3589 | return true; | |
3590 | } | |
3591 | ||
3592 | return false; | |
3593 | } | |
3594 | ||
a3451c2c LP |
3595 | static int parse_line( |
3596 | const char *fname, | |
3597 | unsigned line, | |
3598 | const char *buffer, | |
f6a1346e ZJS |
3599 | bool *invalid_config, |
3600 | void *context) { | |
1731e34a | 3601 | |
f6a1346e | 3602 | Context *c = ASSERT_PTR(context); |
cde684a2 | 3603 | _cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL; |
7f7a50dd SK |
3604 | _cleanup_(item_free_contents) Item i = { |
3605 | /* The "age-by" argument considers all file timestamp types by default. */ | |
3606 | .age_by_file = AGE_BY_DEFAULT_FILE, | |
3607 | .age_by_dir = AGE_BY_DEFAULT_DIR, | |
3608 | }; | |
3f93da98 | 3609 | ItemArray *existing; |
ef43a391 | 3610 | OrderedHashmap *h; |
e0ea6af3 ZJS |
3611 | bool append_or_force = false, boot = false, allow_failure = false, try_replace = false, |
3612 | unbase64 = false, from_cred = false, missing_user_or_group = false; | |
e5876c29 | 3613 | int r; |
3b63d2d3 LP |
3614 | |
3615 | assert(fname); | |
3616 | assert(line >= 1); | |
3617 | assert(buffer); | |
3618 | ||
172e9cc3 ZJS |
3619 | const Specifier specifier_table[] = { |
3620 | { 'a', specifier_architecture, NULL }, | |
3621 | { 'b', specifier_boot_id, NULL }, | |
3622 | { 'B', specifier_os_build_id, NULL }, | |
9a5893e9 ZJS |
3623 | { 'H', specifier_hostname, NULL }, |
3624 | { 'l', specifier_short_hostname, NULL }, | |
6db98098 | 3625 | { 'm', specifier_machine_id, NULL }, |
172e9cc3 ZJS |
3626 | { 'o', specifier_os_id, NULL }, |
3627 | { 'v', specifier_kernel_release, NULL }, | |
3628 | { 'w', specifier_os_version_id, NULL }, | |
3629 | { 'W', specifier_os_variant_id, NULL }, | |
3630 | ||
3631 | { 'h', specifier_user_home, NULL }, | |
3632 | ||
3633 | { 'C', specifier_directory, UINT_TO_PTR(DIRECTORY_CACHE) }, | |
3634 | { 'L', specifier_directory, UINT_TO_PTR(DIRECTORY_LOGS) }, | |
3635 | { 'S', specifier_directory, UINT_TO_PTR(DIRECTORY_STATE) }, | |
3636 | { 't', specifier_directory, UINT_TO_PTR(DIRECTORY_RUNTIME) }, | |
3637 | ||
4870133b | 3638 | COMMON_CREDS_SPECIFIERS(arg_runtime_scope), |
172e9cc3 ZJS |
3639 | COMMON_TMP_SPECIFIERS, |
3640 | {} | |
3641 | }; | |
3642 | ||
68685607 | 3643 | r = extract_many_words( |
4034a06d | 3644 | &buffer, |
68685607 | 3645 | NULL, |
4b6621fe | 3646 | EXTRACT_UNQUOTE | EXTRACT_CUNESCAPE, |
4034a06d LP |
3647 | &action, |
3648 | &path, | |
3649 | &mode, | |
3650 | &user, | |
3651 | &group, | |
3652 | &age, | |
3653 | NULL); | |
d9daae55 | 3654 | if (r < 0) { |
751223fe ZJS |
3655 | if (IN_SET(r, -EINVAL, -EBADSLT)) |
3656 | /* invalid quoting and such or an unknown specifier */ | |
d9daae55 | 3657 | *invalid_config = true; |
d02933fd | 3658 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to parse line: %m"); |
4b93699d | 3659 | } else if (r < 2) { |
d9daae55 | 3660 | *invalid_config = true; |
d02933fd | 3661 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Syntax error."); |
5008d581 LP |
3662 | } |
3663 | ||
e7b88b7b | 3664 | if (!empty_or_dash(buffer)) { |
4034a06d LP |
3665 | i.argument = strdup(buffer); |
3666 | if (!i.argument) | |
3667 | return log_oom(); | |
3668 | } | |
3669 | ||
2e78fa79 | 3670 | if (isempty(action)) { |
d9daae55 | 3671 | *invalid_config = true; |
ad36bb9f ZJS |
3672 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3673 | "Command too short '%s'.", action); | |
2e78fa79 LP |
3674 | } |
3675 | ||
e5876c29 | 3676 | for (int pos = 1; action[pos]; pos++) |
5f255144 ZJS |
3677 | if (action[pos] == '!' && !boot) |
3678 | boot = true; | |
c55ac248 ZS |
3679 | else if (action[pos] == '+' && !append_or_force) |
3680 | append_or_force = true; | |
6d7b5433 WD |
3681 | else if (action[pos] == '-' && !allow_failure) |
3682 | allow_failure = true; | |
c46c3233 AW |
3683 | else if (action[pos] == '=' && !try_replace) |
3684 | try_replace = true; | |
708daf42 LP |
3685 | else if (action[pos] == '~' && !unbase64) |
3686 | unbase64 = true; | |
e52f6f63 LP |
3687 | else if (action[pos] == '^' && !from_cred) |
3688 | from_cred = true; | |
5f255144 | 3689 | else { |
d9daae55 | 3690 | *invalid_config = true; |
ad36bb9f ZJS |
3691 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3692 | "Unknown modifiers in command '%s'.", action); | |
5f255144 | 3693 | } |
2e78fa79 | 3694 | |
582deb84 | 3695 | if (boot && !arg_boot) { |
ad36bb9f ZJS |
3696 | log_syntax(NULL, LOG_DEBUG, fname, line, 0, |
3697 | "Ignoring entry %s \"%s\" because --boot is not specified.", action, path); | |
c4708f13 | 3698 | return 0; |
582deb84 | 3699 | } |
c4708f13 | 3700 | |
3f93da98 | 3701 | i.type = action[0]; |
c55ac248 | 3702 | i.append_or_force = append_or_force; |
6d7b5433 | 3703 | i.allow_failure = allow_failure; |
c46c3233 | 3704 | i.try_replace = try_replace; |
2e78fa79 | 3705 | |
de61a04b | 3706 | r = specifier_printf(path, PATH_MAX-1, specifier_table, arg_root, NULL, &i.path); |
6db98098 | 3707 | if (ERRNO_IS_NOINFO(r)) |
4cef1923 | 3708 | return log_unresolvable_specifier(fname, line); |
d9daae55 | 3709 | if (r < 0) { |
751223fe ZJS |
3710 | if (IN_SET(r, -EINVAL, -EBADSLT)) |
3711 | *invalid_config = true; | |
ad36bb9f ZJS |
3712 | return log_syntax(NULL, LOG_ERR, fname, line, r, |
3713 | "Failed to replace specifiers in '%s': %m", path); | |
d9daae55 | 3714 | } |
1731e34a | 3715 | |
a2d1fb88 LP |
3716 | r = patch_var_run(fname, line, &i.path); |
3717 | if (r < 0) | |
3718 | return r; | |
3719 | ||
35c18a51 LP |
3720 | if (!path_is_absolute(i.path)) { |
3721 | *invalid_config = true; | |
3722 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), | |
3723 | "Path '%s' not absolute.", i.path); | |
3724 | } | |
3725 | ||
3726 | path_simplify(i.path); | |
3727 | ||
3f93da98 | 3728 | switch (i.type) { |
468d726b | 3729 | |
777b87e7 | 3730 | case CREATE_DIRECTORY: |
d7b8eec7 | 3731 | case CREATE_SUBVOLUME: |
5fb13eb5 LP |
3732 | case CREATE_SUBVOLUME_INHERIT_QUOTA: |
3733 | case CREATE_SUBVOLUME_NEW_QUOTA: | |
df8dee85 | 3734 | case EMPTY_DIRECTORY: |
777b87e7 MS |
3735 | case TRUNCATE_DIRECTORY: |
3736 | case CREATE_FIFO: | |
3737 | case IGNORE_PATH: | |
78a92a5a | 3738 | case IGNORE_DIRECTORY_PATH: |
777b87e7 MS |
3739 | case REMOVE_PATH: |
3740 | case RECURSIVE_REMOVE_PATH: | |
e73a03e0 | 3741 | case ADJUST_MODE: |
777b87e7 MS |
3742 | case RELABEL_PATH: |
3743 | case RECURSIVE_RELABEL_PATH: | |
c82500c6 | 3744 | if (i.argument) |
ad36bb9f ZJS |
3745 | log_syntax(NULL, LOG_WARNING, fname, line, 0, |
3746 | "%c lines don't take argument fields, ignoring.", (char) i.type); | |
c82500c6 LP |
3747 | break; |
3748 | ||
3749 | case CREATE_FILE: | |
3750 | case TRUNCATE_FILE: | |
777b87e7 | 3751 | break; |
468d726b LP |
3752 | |
3753 | case CREATE_SYMLINK: | |
708daf42 LP |
3754 | if (unbase64) { |
3755 | *invalid_config = true; | |
ad36bb9f ZJS |
3756 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3757 | "base64 decoding not supported for symlink targets."); | |
708daf42 | 3758 | } |
468d726b LP |
3759 | break; |
3760 | ||
31ed59c5 | 3761 | case WRITE_FILE: |
3f93da98 | 3762 | if (!i.argument) { |
d9daae55 | 3763 | *invalid_config = true; |
ad36bb9f ZJS |
3764 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3765 | "Write file requires argument."); | |
31ed59c5 LP |
3766 | } |
3767 | break; | |
3768 | ||
849958d1 | 3769 | case COPY_FILES: |
708daf42 LP |
3770 | if (unbase64) { |
3771 | *invalid_config = true; | |
ad36bb9f ZJS |
3772 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3773 | "base64 decoding not supported for copy sources."); | |
708daf42 | 3774 | } |
849958d1 LP |
3775 | break; |
3776 | ||
468d726b | 3777 | case CREATE_CHAR_DEVICE: |
3a47c40d | 3778 | case CREATE_BLOCK_DEVICE: |
708daf42 LP |
3779 | if (unbase64) { |
3780 | *invalid_config = true; | |
ad36bb9f ZJS |
3781 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3782 | "base64 decoding not supported for device node creation."); | |
708daf42 LP |
3783 | } |
3784 | ||
3f93da98 | 3785 | if (!i.argument) { |
d9daae55 | 3786 | *invalid_config = true; |
ad36bb9f ZJS |
3787 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3788 | "Device file requires argument."); | |
468d726b LP |
3789 | } |
3790 | ||
7176f06c | 3791 | r = parse_devnum(i.argument, &i.major_minor); |
3a47c40d | 3792 | if (r < 0) { |
d9daae55 | 3793 | *invalid_config = true; |
ad36bb9f ZJS |
3794 | return log_syntax(NULL, LOG_ERR, fname, line, r, |
3795 | "Can't parse device file major/minor '%s'.", i.argument); | |
468d726b LP |
3796 | } |
3797 | ||
468d726b | 3798 | break; |
468d726b | 3799 | |
ebf4e801 | 3800 | case SET_XATTR: |
b705ab6a | 3801 | case RECURSIVE_SET_XATTR: |
708daf42 LP |
3802 | if (unbase64) { |
3803 | *invalid_config = true; | |
ad36bb9f ZJS |
3804 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3805 | "base64 decoding not supported for extended attributes."); | |
708daf42 | 3806 | } |
3f93da98 | 3807 | if (!i.argument) { |
d9daae55 | 3808 | *invalid_config = true; |
d02933fd LP |
3809 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3810 | "Set extended attribute requires argument."); | |
ebf4e801 | 3811 | } |
bd550f78 | 3812 | r = parse_xattrs_from_arg(&i); |
ebf4e801 MW |
3813 | if (r < 0) |
3814 | return r; | |
3815 | break; | |
3816 | ||
f8eeeaf9 | 3817 | case SET_ACL: |
b705ab6a | 3818 | case RECURSIVE_SET_ACL: |
708daf42 LP |
3819 | if (unbase64) { |
3820 | *invalid_config = true; | |
ad36bb9f ZJS |
3821 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3822 | "base64 decoding not supported for ACLs."); | |
708daf42 | 3823 | } |
f8eeeaf9 | 3824 | if (!i.argument) { |
d9daae55 | 3825 | *invalid_config = true; |
d02933fd LP |
3826 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3827 | "Set ACLs requires argument."); | |
f8eeeaf9 | 3828 | } |
bd550f78 | 3829 | r = parse_acls_from_arg(&i); |
f8eeeaf9 ZJS |
3830 | if (r < 0) |
3831 | return r; | |
3832 | break; | |
3833 | ||
88ec4dfa LP |
3834 | case SET_ATTRIBUTE: |
3835 | case RECURSIVE_SET_ATTRIBUTE: | |
708daf42 LP |
3836 | if (unbase64) { |
3837 | *invalid_config = true; | |
ad36bb9f ZJS |
3838 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3839 | "base64 decoding not supported for file attributes."); | |
708daf42 | 3840 | } |
22c3a6ca | 3841 | if (!i.argument) { |
d9daae55 | 3842 | *invalid_config = true; |
d02933fd LP |
3843 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3844 | "Set file attribute requires argument."); | |
22c3a6ca | 3845 | } |
bd550f78 | 3846 | r = parse_attribute_from_arg(&i); |
751223fe | 3847 | if (IN_SET(r, -EINVAL, -EBADSLT)) |
d9daae55 | 3848 | *invalid_config = true; |
22c3a6ca GB |
3849 | if (r < 0) |
3850 | return r; | |
3851 | break; | |
3852 | ||
777b87e7 | 3853 | default: |
d9daae55 | 3854 | *invalid_config = true; |
d02933fd LP |
3855 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3856 | "Unknown command type '%c'.", (char) i.type); | |
3b63d2d3 | 3857 | } |
468d726b | 3858 | |
3f93da98 | 3859 | if (!should_include_path(i.path)) |
7f2c1f4d | 3860 | return 0; |
5008d581 | 3861 | |
e52f6f63 LP |
3862 | if (!unbase64) { |
3863 | /* Do specifier expansion except if base64 mode is enabled */ | |
708daf42 | 3864 | r = specifier_expansion_from_arg(specifier_table, &i); |
6db98098 | 3865 | if (ERRNO_IS_NOINFO(r)) |
708daf42 LP |
3866 | return log_unresolvable_specifier(fname, line); |
3867 | if (r < 0) { | |
3868 | if (IN_SET(r, -EINVAL, -EBADSLT)) | |
3869 | *invalid_config = true; | |
ad36bb9f ZJS |
3870 | return log_syntax(NULL, LOG_ERR, fname, line, r, |
3871 | "Failed to substitute specifiers in argument: %m"); | |
708daf42 | 3872 | } |
751223fe | 3873 | } |
4cef1923 | 3874 | |
1ed8887e | 3875 | switch (i.type) { |
05381430 DL |
3876 | case CREATE_SYMLINK: |
3877 | if (!i.argument) { | |
3878 | i.argument = path_join("/usr/share/factory", i.path); | |
3879 | if (!i.argument) | |
3880 | return log_oom(); | |
3881 | } | |
3882 | break; | |
3883 | ||
1ed8887e DL |
3884 | case COPY_FILES: |
3885 | if (!i.argument) { | |
3886 | i.argument = path_join("/usr/share/factory", i.path); | |
3887 | if (!i.argument) | |
3888 | return log_oom(); | |
3889 | } else if (!path_is_absolute(i.argument)) { | |
3890 | *invalid_config = true; | |
ad36bb9f ZJS |
3891 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3892 | "Source path '%s' is not absolute.", i.argument); | |
1ed8887e DL |
3893 | |
3894 | } | |
3895 | ||
3896 | if (!empty_or_root(arg_root)) { | |
3897 | char *p; | |
3898 | ||
3899 | p = path_join(arg_root, i.argument); | |
3900 | if (!p) | |
3901 | return log_oom(); | |
3902 | free_and_replace(i.argument, p); | |
3903 | } | |
3904 | ||
3905 | path_simplify(i.argument); | |
3906 | ||
3907 | if (laccess(i.argument, F_OK) == -ENOENT) { | |
3908 | /* Silently skip over lines where the source file is missing. */ | |
ad36bb9f ZJS |
3909 | log_syntax(NULL, LOG_DEBUG, fname, line, 0, |
3910 | "Copy source path '%s' does not exist, skipping line.", i.argument); | |
1ed8887e DL |
3911 | return 0; |
3912 | } | |
3913 | ||
3914 | break; | |
3915 | ||
3916 | default: | |
3917 | break; | |
3918 | } | |
3919 | ||
e52f6f63 LP |
3920 | if (from_cred) { |
3921 | if (!i.argument) | |
ad36bb9f ZJS |
3922 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL), |
3923 | "Reading from credential requested, but no credential name specified."); | |
e52f6f63 | 3924 | if (!credential_name_valid(i.argument)) |
ad36bb9f ZJS |
3925 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL), |
3926 | "Credential name not valid: %s", i.argument); | |
e52f6f63 LP |
3927 | |
3928 | r = read_credential(i.argument, &i.binary_argument, &i.binary_argument_size); | |
3929 | if (IN_SET(r, -ENXIO, -ENOENT)) { | |
3930 | /* Silently skip over lines that have no credentials passed */ | |
9109009d | 3931 | log_syntax(NULL, LOG_DEBUG, fname, line, 0, |
6db98098 | 3932 | "Credential '%s' not specified, skipping line.", i.argument); |
e52f6f63 LP |
3933 | return 0; |
3934 | } | |
3935 | if (r < 0) | |
3936 | return log_error_errno(r, "Failed to read credential '%s': %m", i.argument); | |
3937 | } | |
3938 | ||
3939 | /* If base64 decoding is requested, do so now */ | |
3940 | if (unbase64 && item_binary_argument(&i)) { | |
3941 | _cleanup_free_ void *data = NULL; | |
3942 | size_t data_size = 0; | |
3943 | ||
bdd2036e MY |
3944 | r = unbase64mem_full(item_binary_argument(&i), item_binary_argument_size(&i), /* secure = */ false, |
3945 | &data, &data_size); | |
e52f6f63 LP |
3946 | if (r < 0) |
3947 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to base64 decode specified argument '%s': %m", i.argument); | |
3948 | ||
3949 | free_and_replace(i.binary_argument, data); | |
3950 | i.binary_argument_size = data_size; | |
3951 | } | |
3952 | ||
7dc6477d | 3953 | if (!empty_or_root(arg_root)) { |
cde684a2 LP |
3954 | char *p; |
3955 | ||
c6134d3e | 3956 | p = path_join(arg_root, i.path); |
cf9a4abd MM |
3957 | if (!p) |
3958 | return log_oom(); | |
81fa4479 | 3959 | free_and_replace(i.path, p); |
cf9a4abd MM |
3960 | } |
3961 | ||
e7b88b7b | 3962 | if (!empty_or_dash(user)) { |
cc43328c LP |
3963 | const char *u; |
3964 | ||
3965 | u = startswith(user, ":"); | |
3966 | if (u) | |
3967 | i.uid_only_create = true; | |
3968 | else | |
3969 | u = user; | |
3970 | ||
376d0495 | 3971 | r = find_uid(u, &i.uid, &c->uid_cache); |
e0ea6af3 ZJS |
3972 | if (r == -ESRCH && arg_graceful) { |
3973 | log_syntax(NULL, LOG_DEBUG, fname, line, r, | |
3974 | "%s: user '%s' not found, not adjusting ownership.", i.path, u); | |
3975 | missing_user_or_group = true; | |
3976 | } else if (r < 0) { | |
d9daae55 | 3977 | *invalid_config = true; |
cc43328c | 3978 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to resolve user '%s': %m", u); |
e0ea6af3 ZJS |
3979 | } else |
3980 | i.uid_set = true; | |
3b63d2d3 LP |
3981 | } |
3982 | ||
e7b88b7b | 3983 | if (!empty_or_dash(group)) { |
cc43328c LP |
3984 | const char *g; |
3985 | ||
3986 | g = startswith(group, ":"); | |
3987 | if (g) | |
3988 | i.gid_only_create = true; | |
3989 | else | |
3990 | g = group; | |
3991 | ||
376d0495 | 3992 | r = find_gid(g, &i.gid, &c->gid_cache); |
e0ea6af3 ZJS |
3993 | if (r == -ESRCH && arg_graceful) { |
3994 | log_syntax(NULL, LOG_DEBUG, fname, line, r, | |
3995 | "%s: group '%s' not found, not adjusting ownership.", i.path, g); | |
3996 | missing_user_or_group = true; | |
3997 | } else if (r < 0) { | |
d9daae55 | 3998 | *invalid_config = true; |
e0ea6af3 ZJS |
3999 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to resolve group '%s': %m", g); |
4000 | } else | |
4001 | i.gid_set = true; | |
3b63d2d3 LP |
4002 | } |
4003 | ||
e7b88b7b | 4004 | if (!empty_or_dash(mode)) { |
cc43328c | 4005 | const char *mm; |
3b63d2d3 LP |
4006 | unsigned m; |
4007 | ||
e5876c29 | 4008 | for (mm = mode;; mm++) |
cc43328c LP |
4009 | if (*mm == '~') |
4010 | i.mask_perms = true; | |
4011 | else if (*mm == ':') | |
4012 | i.mode_only_create = true; | |
4013 | else | |
4014 | break; | |
abef3f91 | 4015 | |
d02933fd LP |
4016 | r = parse_mode(mm, &m); |
4017 | if (r < 0) { | |
d9daae55 | 4018 | *invalid_config = true; |
d02933fd | 4019 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Invalid mode '%s'.", mode); |
3b63d2d3 LP |
4020 | } |
4021 | ||
3f93da98 ZJS |
4022 | i.mode = m; |
4023 | i.mode_set = true; | |
3b63d2d3 | 4024 | } else |
2223a025 ZJS |
4025 | i.mode = IN_SET(i.type, |
4026 | CREATE_DIRECTORY, | |
4027 | TRUNCATE_DIRECTORY, | |
4028 | CREATE_SUBVOLUME, | |
4029 | CREATE_SUBVOLUME_INHERIT_QUOTA, | |
4030 | CREATE_SUBVOLUME_NEW_QUOTA) ? 0755 : 0644; | |
3b63d2d3 | 4031 | |
e0ea6af3 ZJS |
4032 | if (missing_user_or_group && (i.mode & ~0777) != 0) { |
4033 | /* Refuse any special bits for nodes where we couldn't resolve the ownership properly. */ | |
4034 | mode_t adjusted = i.mode & 0777; | |
4035 | log_syntax(NULL, LOG_INFO, fname, line, 0, | |
4036 | "Changing mode 0%o to 0%o because of changed ownership.", i.mode, adjusted); | |
4037 | i.mode = adjusted; | |
4038 | } | |
4039 | ||
e7b88b7b | 4040 | if (!empty_or_dash(age)) { |
24f3a374 | 4041 | const char *a = age; |
7f7a50dd | 4042 | _cleanup_free_ char *seconds = NULL, *age_by = NULL; |
24f3a374 LP |
4043 | |
4044 | if (*a == '~') { | |
3f93da98 | 4045 | i.keep_first_level = true; |
24f3a374 LP |
4046 | a++; |
4047 | } | |
4048 | ||
7f7a50dd SK |
4049 | /* Format: "age-by:age"; where age-by is "[abcmABCM]+". */ |
4050 | r = split_pair(a, ":", &age_by, &seconds); | |
4051 | if (r == -ENOMEM) | |
4052 | return log_oom(); | |
4053 | if (r < 0 && r != -EINVAL) | |
4054 | return log_error_errno(r, "Failed to parse age-by for '%s': %m", age); | |
4055 | if (r >= 0) { | |
4056 | /* We found a ":", parse the "age-by" part. */ | |
4057 | r = parse_age_by_from_arg(age_by, &i); | |
4058 | if (r == -ENOMEM) | |
4059 | return log_oom(); | |
4060 | if (r < 0) { | |
4061 | *invalid_config = true; | |
4062 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Invalid age-by '%s'.", age_by); | |
4063 | } | |
4064 | ||
4065 | /* For parsing the "age" part, after the ":". */ | |
4066 | a = seconds; | |
4067 | } | |
4068 | ||
d02933fd LP |
4069 | r = parse_sec(a, &i.age); |
4070 | if (r < 0) { | |
d9daae55 | 4071 | *invalid_config = true; |
7f7a50dd | 4072 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Invalid age '%s'.", a); |
3b63d2d3 LP |
4073 | } |
4074 | ||
3f93da98 | 4075 | i.age_set = true; |
3b63d2d3 LP |
4076 | } |
4077 | ||
a5e4f3ab | 4078 | h = needs_glob(i.type) ? c->globs : c->items; |
bfe95f35 | 4079 | |
ef43a391 | 4080 | existing = ordered_hashmap_get(h, i.path); |
468d726b | 4081 | if (existing) { |
9af74e0f FB |
4082 | if (is_duplicated_item(existing, &i)) { |
4083 | log_syntax(NULL, LOG_NOTICE, fname, line, 0, | |
4084 | "Duplicate line for path \"%s\", ignoring.", i.path); | |
4085 | return 0; | |
ebf4e801 MW |
4086 | } |
4087 | } else { | |
3f93da98 | 4088 | existing = new0(ItemArray, 1); |
07982ed1 LP |
4089 | if (!existing) |
4090 | return log_oom(); | |
4091 | ||
ef43a391 | 4092 | r = ordered_hashmap_put(h, i.path, existing); |
ccd114f0 LP |
4093 | if (r < 0) { |
4094 | free(existing); | |
3f93da98 | 4095 | return log_oom(); |
ccd114f0 | 4096 | } |
bfe95f35 LP |
4097 | } |
4098 | ||
319a4f4b | 4099 | if (!GREEDY_REALLOC(existing->items, existing->n_items + 1)) |
3f93da98 | 4100 | return log_oom(); |
5008d581 | 4101 | |
088d71f8 | 4102 | existing->items[existing->n_items++] = TAKE_STRUCT(i); |
dd449aca ZJS |
4103 | |
4104 | /* Sort item array, to enforce stable ordering of application */ | |
96d10d78 | 4105 | typesafe_qsort(existing->items, existing->n_items, item_compare); |
17493fa5 | 4106 | |
7f2c1f4d | 4107 | return 0; |
5008d581 LP |
4108 | } |
4109 | ||
ceaaeb9b ZJS |
4110 | static int cat_config(char **config_dirs, char **args) { |
4111 | _cleanup_strv_free_ char **files = NULL; | |
4112 | int r; | |
4113 | ||
4114 | r = conf_files_list_with_replacement(arg_root, config_dirs, arg_replace, &files, NULL); | |
4115 | if (r < 0) | |
4116 | return r; | |
4117 | ||
1be6a5db ZJS |
4118 | pager_open(arg_pager_flags); |
4119 | ||
4120 | return cat_files(NULL, files, arg_cat_flags); | |
ceaaeb9b ZJS |
4121 | } |
4122 | ||
dd04fb32 LP |
4123 | static int exclude_default_prefixes(void) { |
4124 | int r; | |
4125 | ||
4126 | /* Provide an easy way to exclude virtual/memory file systems from what we do here. Useful in | |
4127 | * combination with --root= where we probably don't want to apply stuff to these dirs as they are | |
4128 | * likely over-mounted if the root directory is actually used, and it wouldbe less than ideal to have | |
4129 | * all kinds of files created/adjusted underneath these mount points. */ | |
4130 | ||
69f3c619 | 4131 | r = strv_extend_many( |
dd04fb32 | 4132 | &arg_exclude_prefixes, |
69f3c619 LP |
4133 | "/dev", |
4134 | "/proc", | |
4135 | "/run", | |
4136 | "/sys"); | |
dd04fb32 LP |
4137 | if (r < 0) |
4138 | return log_oom(); | |
4139 | ||
69f3c619 | 4140 | strv_uniq(arg_exclude_prefixes); |
dd04fb32 LP |
4141 | return 0; |
4142 | } | |
4143 | ||
37ec0fdd LP |
4144 | static int help(void) { |
4145 | _cleanup_free_ char *link = NULL; | |
4146 | int r; | |
4147 | ||
4148 | r = terminal_urlify_man("systemd-tmpfiles", "8", &link); | |
4149 | if (r < 0) | |
4150 | return log_oom(); | |
4151 | ||
9c8a0ca6 ZJS |
4152 | printf("%1$s COMMAND [OPTIONS...] [CONFIGURATION FILE...]\n" |
4153 | "\n%2$sCreate, delete, and clean up files and directories.%4$s\n" | |
4154 | "\n%3$sCommands:%4$s\n" | |
4155 | " --create Create files and directories\n" | |
4156 | " --clean Clean up files and directories\n" | |
4157 | " --remove Remove files and directories\n" | |
5c795114 | 4158 | " -h --help Show this help\n" |
eb9da376 | 4159 | " --version Show package version\n" |
9c8a0ca6 ZJS |
4160 | "\n%3$sOptions:%4$s\n" |
4161 | " --user Execute user configuration\n" | |
ceaaeb9b | 4162 | " --cat-config Show configuration files\n" |
1be6a5db | 4163 | " --tldr Show non-comment parts of configuration\n" |
81815651 | 4164 | " --boot Execute actions only safe at boot\n" |
9a27ef09 | 4165 | " --graceful Quietly ignore unknown users or groups\n" |
81a18380 | 4166 | " --purge Delete all files owned by the configuration files\n" |
79ca888f ZJS |
4167 | " --prefix=PATH Only apply rules with the specified prefix\n" |
4168 | " --exclude-prefix=PATH Ignore rules with the specified prefix\n" | |
dd04fb32 | 4169 | " -E Ignore rules prefixed with /dev, /proc, /run, /sys\n" |
d9daae55 | 4170 | " --root=PATH Operate on an alternate filesystem root\n" |
63d3d0a5 | 4171 | " --image=PATH Operate on disk image as filesystem root\n" |
84be0c71 | 4172 | " --image-policy=POLICY Specify disk image dissection policy\n" |
a6d8474f | 4173 | " --replace=PATH Treat arguments as replacement for PATH\n" |
9e90d4f4 | 4174 | " --dry-run Just print what would be done\n" |
dcd5c891 | 4175 | " --no-pager Do not pipe output into a pager\n" |
9c8a0ca6 | 4176 | "\nSee the %5$s for details.\n", |
bc556335 DDM |
4177 | program_invocation_short_name, |
4178 | ansi_highlight(), | |
9c8a0ca6 | 4179 | ansi_underline(), |
bc556335 DDM |
4180 | ansi_normal(), |
4181 | link); | |
37ec0fdd LP |
4182 | |
4183 | return 0; | |
3b63d2d3 LP |
4184 | } |
4185 | ||
4186 | static int parse_argv(int argc, char *argv[]) { | |
3b63d2d3 | 4187 | enum { |
eb9da376 | 4188 | ARG_VERSION = 0x100, |
ceaaeb9b | 4189 | ARG_CAT_CONFIG, |
1be6a5db | 4190 | ARG_TLDR, |
f2b5ca0e | 4191 | ARG_USER, |
3b63d2d3 LP |
4192 | ARG_CREATE, |
4193 | ARG_CLEAN, | |
fba6e687 | 4194 | ARG_REMOVE, |
81a18380 | 4195 | ARG_PURGE, |
81815651 | 4196 | ARG_BOOT, |
e0ea6af3 | 4197 | ARG_GRACEFUL, |
5c795114 DR |
4198 | ARG_PREFIX, |
4199 | ARG_EXCLUDE_PREFIX, | |
cf9a4abd | 4200 | ARG_ROOT, |
63d3d0a5 | 4201 | ARG_IMAGE, |
06e78680 | 4202 | ARG_IMAGE_POLICY, |
a6d8474f | 4203 | ARG_REPLACE, |
9e90d4f4 | 4204 | ARG_DRY_RUN, |
dcd5c891 | 4205 | ARG_NO_PAGER, |
3b63d2d3 LP |
4206 | }; |
4207 | ||
4208 | static const struct option options[] = { | |
5c795114 | 4209 | { "help", no_argument, NULL, 'h' }, |
f2b5ca0e | 4210 | { "user", no_argument, NULL, ARG_USER }, |
eb9da376 | 4211 | { "version", no_argument, NULL, ARG_VERSION }, |
ceaaeb9b | 4212 | { "cat-config", no_argument, NULL, ARG_CAT_CONFIG }, |
1be6a5db | 4213 | { "tldr", no_argument, NULL, ARG_TLDR }, |
5c795114 DR |
4214 | { "create", no_argument, NULL, ARG_CREATE }, |
4215 | { "clean", no_argument, NULL, ARG_CLEAN }, | |
4216 | { "remove", no_argument, NULL, ARG_REMOVE }, | |
81a18380 | 4217 | { "purge", no_argument, NULL, ARG_PURGE }, |
81815651 | 4218 | { "boot", no_argument, NULL, ARG_BOOT }, |
e0ea6af3 | 4219 | { "graceful", no_argument, NULL, ARG_GRACEFUL }, |
5c795114 DR |
4220 | { "prefix", required_argument, NULL, ARG_PREFIX }, |
4221 | { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX }, | |
cf9a4abd | 4222 | { "root", required_argument, NULL, ARG_ROOT }, |
63d3d0a5 | 4223 | { "image", required_argument, NULL, ARG_IMAGE }, |
06e78680 | 4224 | { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY }, |
a6d8474f | 4225 | { "replace", required_argument, NULL, ARG_REPLACE }, |
9e90d4f4 | 4226 | { "dry-run", no_argument, NULL, ARG_DRY_RUN }, |
dcd5c891 | 4227 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, |
eb9da376 | 4228 | {} |
3b63d2d3 LP |
4229 | }; |
4230 | ||
0f474365 | 4231 | int c, r; |
3b63d2d3 LP |
4232 | |
4233 | assert(argc >= 0); | |
4234 | assert(argv); | |
4235 | ||
dd04fb32 | 4236 | while ((c = getopt_long(argc, argv, "hE", options, NULL)) >= 0) |
3b63d2d3 LP |
4237 | |
4238 | switch (c) { | |
4239 | ||
4240 | case 'h': | |
37ec0fdd | 4241 | return help(); |
eb9da376 LP |
4242 | |
4243 | case ARG_VERSION: | |
3f6fd1ba | 4244 | return version(); |
3b63d2d3 | 4245 | |
ceaaeb9b | 4246 | case ARG_CAT_CONFIG: |
1be6a5db ZJS |
4247 | arg_cat_flags = CAT_CONFIG_ON; |
4248 | break; | |
4249 | ||
4250 | case ARG_TLDR: | |
4251 | arg_cat_flags = CAT_TLDR; | |
ceaaeb9b ZJS |
4252 | break; |
4253 | ||
f2b5ca0e | 4254 | case ARG_USER: |
4870133b | 4255 | arg_runtime_scope = RUNTIME_SCOPE_USER; |
f2b5ca0e ZJS |
4256 | break; |
4257 | ||
3b63d2d3 | 4258 | case ARG_CREATE: |
1a967b6b | 4259 | arg_operation |= OPERATION_CREATE; |
3b63d2d3 LP |
4260 | break; |
4261 | ||
4262 | case ARG_CLEAN: | |
1a967b6b | 4263 | arg_operation |= OPERATION_CLEAN; |
3b63d2d3 LP |
4264 | break; |
4265 | ||
4266 | case ARG_REMOVE: | |
1a967b6b | 4267 | arg_operation |= OPERATION_REMOVE; |
3b63d2d3 LP |
4268 | break; |
4269 | ||
81815651 ZJS |
4270 | case ARG_BOOT: |
4271 | arg_boot = true; | |
c4708f13 ZJS |
4272 | break; |
4273 | ||
81a18380 LB |
4274 | case ARG_PURGE: |
4275 | arg_operation |= OPERATION_PURGE; | |
4276 | break; | |
4277 | ||
e0ea6af3 ZJS |
4278 | case ARG_GRACEFUL: |
4279 | arg_graceful = true; | |
4280 | break; | |
4281 | ||
fba6e687 | 4282 | case ARG_PREFIX: |
7f13af72 | 4283 | if (strv_extend(&arg_include_prefixes, optarg) < 0) |
a2aced4a | 4284 | return log_oom(); |
fba6e687 LP |
4285 | break; |
4286 | ||
5c795114 | 4287 | case ARG_EXCLUDE_PREFIX: |
7f13af72 | 4288 | if (strv_extend(&arg_exclude_prefixes, optarg) < 0) |
5c795114 DR |
4289 | return log_oom(); |
4290 | break; | |
4291 | ||
63d3d0a5 | 4292 | case ARG_ROOT: |
614b022c | 4293 | r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root); |
dd04fb32 LP |
4294 | if (r < 0) |
4295 | return r; | |
dd04fb32 LP |
4296 | break; |
4297 | ||
63d3d0a5 | 4298 | case ARG_IMAGE: |
3537577c ZJS |
4299 | #ifdef STANDALONE |
4300 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
4301 | "This systemd-tmpfiles version is compiled without support for --image=."); | |
4302 | #else | |
614b022c | 4303 | r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image); |
63d3d0a5 LP |
4304 | if (r < 0) |
4305 | return r; | |
3537577c ZJS |
4306 | #endif |
4307 | /* Imply -E here since it makes little sense to create files persistently in the /run mountpoint of a disk image */ | |
63d3d0a5 LP |
4308 | _fallthrough_; |
4309 | ||
4310 | case 'E': | |
4311 | r = exclude_default_prefixes(); | |
0f474365 | 4312 | if (r < 0) |
0f03c2a4 | 4313 | return r; |
63d3d0a5 | 4314 | |
cf9a4abd MM |
4315 | break; |
4316 | ||
06e78680 YW |
4317 | case ARG_IMAGE_POLICY: |
4318 | r = parse_image_policy_argument(optarg, &arg_image_policy); | |
4319 | if (r < 0) | |
4320 | return r; | |
4321 | break; | |
4322 | ||
a6d8474f | 4323 | case ARG_REPLACE: |
37ee46f7 | 4324 | if (!path_is_absolute(optarg)) |
baaa35ad | 4325 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
37ee46f7 ZJS |
4326 | "The argument to --replace= must be an absolute path."); |
4327 | if (!endswith(optarg, ".conf")) | |
4328 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
4329 | "The argument to --replace= must have the extension '.conf'."); | |
a6d8474f ZJS |
4330 | |
4331 | arg_replace = optarg; | |
4332 | break; | |
4333 | ||
9e90d4f4 ZJS |
4334 | case ARG_DRY_RUN: |
4335 | arg_dry_run = true; | |
4336 | break; | |
4337 | ||
dcd5c891 | 4338 | case ARG_NO_PAGER: |
0221d68a | 4339 | arg_pager_flags |= PAGER_DISABLE; |
dcd5c891 LP |
4340 | break; |
4341 | ||
3b63d2d3 LP |
4342 | case '?': |
4343 | return -EINVAL; | |
4344 | ||
4345 | default: | |
04499a70 | 4346 | assert_not_reached(); |
3b63d2d3 | 4347 | } |
3b63d2d3 | 4348 | |
1be6a5db | 4349 | if (arg_operation == 0 && arg_cat_flags == CAT_CONFIG_OFF) |
baaa35ad | 4350 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
81a18380 | 4351 | "You need to specify at least one of --clean, --create, --remove, or --purge."); |
3b63d2d3 | 4352 | |
1be6a5db | 4353 | if (arg_replace && arg_cat_flags != CAT_CONFIG_OFF) |
baaa35ad | 4354 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
1be6a5db | 4355 | "Option --replace= is not supported with --cat-config/--tldr."); |
ceaaeb9b | 4356 | |
baaa35ad ZJS |
4357 | if (arg_replace && optind >= argc) |
4358 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1be6a5db | 4359 | "When --replace= is given, some configuration items must be specified."); |
a6d8474f | 4360 | |
4870133b | 4361 | if (arg_root && arg_runtime_scope == RUNTIME_SCOPE_USER) |
b63aacaa LP |
4362 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
4363 | "Combination of --user and --root= is not supported."); | |
4364 | ||
63d3d0a5 | 4365 | if (arg_image && arg_root) |
ad36bb9f ZJS |
4366 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
4367 | "Please specify either --root= or --image=, the combination of both is not supported."); | |
63d3d0a5 | 4368 | |
3b63d2d3 LP |
4369 | return 1; |
4370 | } | |
4371 | ||
1d77721f | 4372 | static int read_config_file( |
a5e4f3ab | 4373 | Context *c, |
1d77721f LP |
4374 | char **config_dirs, |
4375 | const char *fn, | |
4376 | bool ignore_enoent, | |
4377 | bool *invalid_config) { | |
4378 | ||
bec890e3 | 4379 | ItemArray *ia; |
4e68ec18 | 4380 | int r = 0; |
fba6e687 | 4381 | |
a5e4f3ab | 4382 | assert(c); |
fba6e687 LP |
4383 | assert(fn); |
4384 | ||
f6a1346e ZJS |
4385 | r = conf_file_read(arg_root, (const char**) config_dirs, fn, |
4386 | parse_line, c, ignore_enoent, invalid_config); | |
4387 | if (r <= 0) | |
4388 | return r; | |
fba6e687 | 4389 | |
78a92a5a | 4390 | /* we have to determine age parameter for each entry of type X */ |
a5e4f3ab | 4391 | ORDERED_HASHMAP_FOREACH(ia, c->globs) |
e5876c29 | 4392 | FOREACH_ARRAY(i, ia->items, ia->n_items) { |
bec890e3 | 4393 | ItemArray *ja; |
e5876c29 | 4394 | Item *candidate_item = NULL; |
78a92a5a | 4395 | |
bec890e3 | 4396 | if (i->type != IGNORE_DIRECTORY_PATH) |
78a92a5a MS |
4397 | continue; |
4398 | ||
a5e4f3ab | 4399 | ORDERED_HASHMAP_FOREACH(ja, c->items) |
e5876c29 | 4400 | FOREACH_ARRAY(j, ja->items, ja->n_items) { |
875e7b25 ZJS |
4401 | if (!IN_SET(j->type, CREATE_DIRECTORY, |
4402 | TRUNCATE_DIRECTORY, | |
4403 | CREATE_SUBVOLUME, | |
4404 | CREATE_SUBVOLUME_INHERIT_QUOTA, | |
4405 | CREATE_SUBVOLUME_NEW_QUOTA)) | |
bec890e3 | 4406 | continue; |
78a92a5a | 4407 | |
bec890e3 MM |
4408 | if (path_equal(j->path, i->path)) { |
4409 | candidate_item = j; | |
4410 | break; | |
4411 | } | |
4412 | ||
875e7b25 ZJS |
4413 | if (candidate_item |
4414 | ? (path_startswith(j->path, candidate_item->path) && fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0) | |
4415 | : path_startswith(i->path, j->path) != NULL) | |
bec890e3 MM |
4416 | candidate_item = j; |
4417 | } | |
4418 | ||
4419 | if (candidate_item && candidate_item->age_set) { | |
4420 | i->age = candidate_item->age; | |
4421 | i->age_set = true; | |
4422 | } | |
78a92a5a | 4423 | } |
78a92a5a | 4424 | |
fba6e687 LP |
4425 | return r; |
4426 | } | |
4427 | ||
a5e4f3ab LP |
4428 | static int parse_arguments( |
4429 | Context *c, | |
4430 | char **config_dirs, | |
4431 | char **args, | |
4432 | bool *invalid_config) { | |
a6d8474f ZJS |
4433 | int r; |
4434 | ||
a5e4f3ab LP |
4435 | assert(c); |
4436 | ||
a6d8474f | 4437 | STRV_FOREACH(arg, args) { |
a5e4f3ab | 4438 | r = read_config_file(c, config_dirs, *arg, false, invalid_config); |
a6d8474f ZJS |
4439 | if (r < 0) |
4440 | return r; | |
4441 | } | |
4442 | ||
4443 | return 0; | |
4444 | } | |
4445 | ||
a5e4f3ab LP |
4446 | static int read_config_files( |
4447 | Context *c, | |
4448 | char **config_dirs, | |
4449 | char **args, | |
4450 | bool *invalid_config) { | |
4451 | ||
a6d8474f ZJS |
4452 | _cleanup_strv_free_ char **files = NULL; |
4453 | _cleanup_free_ char *p = NULL; | |
a6d8474f ZJS |
4454 | int r; |
4455 | ||
a5e4f3ab LP |
4456 | assert(c); |
4457 | ||
ceaaeb9b | 4458 | r = conf_files_list_with_replacement(arg_root, config_dirs, arg_replace, &files, &p); |
a6d8474f | 4459 | if (r < 0) |
ceaaeb9b | 4460 | return r; |
a6d8474f ZJS |
4461 | |
4462 | STRV_FOREACH(f, files) | |
4463 | if (p && path_equal(*f, p)) { | |
28e5e1e9 | 4464 | log_debug("Parsing arguments at position \"%s\"%s", *f, special_glyph(SPECIAL_GLYPH_ELLIPSIS)); |
a6d8474f | 4465 | |
a5e4f3ab | 4466 | r = parse_arguments(c, config_dirs, args, invalid_config); |
a6d8474f ZJS |
4467 | if (r < 0) |
4468 | return r; | |
4469 | } else | |
4470 | /* Just warn, ignore result otherwise. | |
4471 | * read_config_file() has some debug output, so no need to print anything. */ | |
a5e4f3ab | 4472 | (void) read_config_file(c, config_dirs, *f, true, invalid_config); |
a6d8474f ZJS |
4473 | |
4474 | return 0; | |
4475 | } | |
4476 | ||
a5e4f3ab | 4477 | static int read_credential_lines(Context *c, bool *invalid_config) { |
1d77721f LP |
4478 | _cleanup_free_ char *j = NULL; |
4479 | const char *d; | |
4480 | int r; | |
4481 | ||
a5e4f3ab LP |
4482 | assert(c); |
4483 | ||
1d77721f LP |
4484 | r = get_credentials_dir(&d); |
4485 | if (r == -ENXIO) | |
4486 | return 0; | |
4487 | if (r < 0) | |
4488 | return log_error_errno(r, "Failed to get credentials directory: %m"); | |
4489 | ||
4490 | j = path_join(d, "tmpfiles.extra"); | |
4491 | if (!j) | |
4492 | return log_oom(); | |
4493 | ||
a5e4f3ab | 4494 | (void) read_config_file(c, /* config_dirs= */ NULL, j, /* ignore_enoent= */ true, invalid_config); |
1d77721f LP |
4495 | return 0; |
4496 | } | |
4497 | ||
a5e4f3ab | 4498 | static int link_parent(Context *c, ItemArray *a) { |
811a1587 LP |
4499 | const char *path; |
4500 | char *prefix; | |
4501 | int r; | |
4502 | ||
a5e4f3ab | 4503 | assert(c); |
811a1587 LP |
4504 | assert(a); |
4505 | ||
ad36bb9f ZJS |
4506 | /* Finds the closest "parent" item array for the specified item array. Then registers the specified |
4507 | * item array as child of it, and fills the parent in, linking them both ways. This allows us to | |
4508 | * later create parents before their children, and clean up/remove children before their parents. | |
4509 | */ | |
811a1587 LP |
4510 | |
4511 | if (a->n_items <= 0) | |
4512 | return 0; | |
4513 | ||
4514 | path = a->items[0].path; | |
6e9417f5 | 4515 | prefix = newa(char, strlen(path) + 1); |
811a1587 LP |
4516 | PATH_FOREACH_PREFIX(prefix, path) { |
4517 | ItemArray *j; | |
4518 | ||
a5e4f3ab | 4519 | j = ordered_hashmap_get(c->items, prefix); |
bd0ce244 | 4520 | if (!j) |
a5e4f3ab | 4521 | j = ordered_hashmap_get(c->globs, prefix); |
811a1587 | 4522 | if (j) { |
de7fef4b | 4523 | r = set_ensure_put(&j->children, NULL, a); |
811a1587 LP |
4524 | if (r < 0) |
4525 | return log_oom(); | |
4526 | ||
4527 | a->parent = j; | |
4528 | return 1; | |
4529 | } | |
4530 | } | |
4531 | ||
4532 | return 0; | |
4533 | } | |
4534 | ||
87938c3b YW |
4535 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(item_array_hash_ops, char, string_hash_func, string_compare_func, |
4536 | ItemArray, item_array_free); | |
4537 | ||
4538 | static int run(int argc, char *argv[]) { | |
3537577c | 4539 | #ifndef STANDALONE |
63d3d0a5 | 4540 | _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; |
a4b3e942 | 4541 | _cleanup_(umount_and_freep) char *mounted_dir = NULL; |
3537577c | 4542 | #endif |
f2b5ca0e | 4543 | _cleanup_strv_free_ char **config_dirs = NULL; |
a5e4f3ab | 4544 | _cleanup_(context_done) Context c = {}; |
d9daae55 | 4545 | bool invalid_config = false; |
64adb379 | 4546 | ItemArray *a; |
44ac4f88 | 4547 | enum { |
81a18380 | 4548 | PHASE_PURGE, |
44ac4f88 LP |
4549 | PHASE_REMOVE_AND_CLEAN, |
4550 | PHASE_CREATE, | |
4551 | _PHASE_MAX | |
4552 | } phase; | |
87938c3b | 4553 | int r, k; |
3b63d2d3 | 4554 | |
fdcad0c2 LP |
4555 | r = parse_argv(argc, argv); |
4556 | if (r <= 0) | |
87938c3b | 4557 | return r; |
5008d581 | 4558 | |
d2acb93d | 4559 | log_setup(); |
5008d581 | 4560 | |
01131684 LP |
4561 | /* We require /proc/ for a lot of our operations, i.e. for adjusting access modes, for anything |
4562 | * SELinux related, for recursive operation, for xattr, acl and chattr handling, for btrfs stuff and | |
4563 | * a lot more. It's probably the majority of invocations where /proc/ is required. Since people | |
4564 | * apparently invoke it without anyway and are surprised about the failures, let's catch this early | |
4565 | * and output a nice and friendly warning. */ | |
4566 | if (proc_mounted() == 0) | |
4567 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), | |
4568 | "/proc/ is not mounted, but required for successful operation of systemd-tmpfiles. " | |
4569 | "Please mount /proc/. Alternatively, consider using the --root= or --image= switches."); | |
4570 | ||
e5358401 LP |
4571 | /* Descending down file system trees might take a lot of fds */ |
4572 | (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); | |
4573 | ||
4870133b LP |
4574 | switch (arg_runtime_scope) { |
4575 | ||
4576 | case RUNTIME_SCOPE_USER: | |
f2b5ca0e | 4577 | r = user_config_paths(&config_dirs); |
87938c3b YW |
4578 | if (r < 0) |
4579 | return log_error_errno(r, "Failed to initialize configuration directory list: %m"); | |
4870133b LP |
4580 | break; |
4581 | ||
4582 | case RUNTIME_SCOPE_SYSTEM: | |
f2b5ca0e | 4583 | config_dirs = strv_split_nulstr(CONF_PATHS_NULSTR("tmpfiles.d")); |
87938c3b YW |
4584 | if (!config_dirs) |
4585 | return log_oom(); | |
4870133b LP |
4586 | break; |
4587 | ||
4588 | default: | |
4589 | assert_not_reached(); | |
f2b5ca0e ZJS |
4590 | } |
4591 | ||
79c91cec | 4592 | if (DEBUG_LOGGING) { |
f2b5ca0e ZJS |
4593 | _cleanup_free_ char *t = NULL; |
4594 | ||
b8e35011 LP |
4595 | STRV_FOREACH(i, config_dirs) { |
4596 | _cleanup_free_ char *j = NULL; | |
4597 | ||
4598 | j = path_join(arg_root, *i); | |
4599 | if (!j) | |
4600 | return log_oom(); | |
4601 | ||
c2bc710b | 4602 | if (!strextend(&t, "\n\t", j)) |
b8e35011 LP |
4603 | return log_oom(); |
4604 | } | |
4605 | ||
4606 | log_debug("Looking for configuration files in (higher priority first):%s", t); | |
f2b5ca0e ZJS |
4607 | } |
4608 | ||
1be6a5db | 4609 | if (arg_cat_flags != CAT_CONFIG_OFF) |
87938c3b | 4610 | return cat_config(config_dirs, argv + optind); |
ceaaeb9b ZJS |
4611 | |
4612 | umask(0022); | |
4613 | ||
a452c807 | 4614 | r = mac_init(); |
a9ba0e32 CG |
4615 | if (r < 0) |
4616 | return r; | |
ceaaeb9b | 4617 | |
3537577c | 4618 | #ifndef STANDALONE |
63d3d0a5 LP |
4619 | if (arg_image) { |
4620 | assert(!arg_root); | |
4621 | ||
4622 | r = mount_image_privately_interactively( | |
4623 | arg_image, | |
84be0c71 | 4624 | arg_image_policy, |
4b5de5dd LP |
4625 | DISSECT_IMAGE_GENERIC_ROOT | |
4626 | DISSECT_IMAGE_REQUIRE_ROOT | | |
4627 | DISSECT_IMAGE_VALIDATE_OS | | |
4628 | DISSECT_IMAGE_RELAX_VAR_CHECK | | |
c65f854a | 4629 | DISSECT_IMAGE_FSCK | |
f4a63ce2 LP |
4630 | DISSECT_IMAGE_GROWFS | |
4631 | DISSECT_IMAGE_ALLOW_USERSPACE_VERITY, | |
a4b3e942 | 4632 | &mounted_dir, |
a133d2c3 | 4633 | /* ret_dir_fd= */ NULL, |
e330f97a | 4634 | &loop_device); |
63d3d0a5 LP |
4635 | if (r < 0) |
4636 | return r; | |
4637 | ||
a4b3e942 | 4638 | arg_root = strdup(mounted_dir); |
63d3d0a5 LP |
4639 | if (!arg_root) |
4640 | return log_oom(); | |
4641 | } | |
3537577c ZJS |
4642 | #else |
4643 | assert(!arg_image); | |
4644 | #endif | |
63d3d0a5 | 4645 | |
a5e4f3ab LP |
4646 | c.items = ordered_hashmap_new(&item_array_hash_ops); |
4647 | c.globs = ordered_hashmap_new(&item_array_hash_ops); | |
4648 | if (!c.items || !c.globs) | |
87938c3b | 4649 | return log_oom(); |
ceaaeb9b | 4650 | |
a6d8474f ZJS |
4651 | /* If command line arguments are specified along with --replace, read all |
4652 | * configuration files and insert the positional arguments at the specified | |
4653 | * place. Otherwise, if command line arguments are specified, execute just | |
4654 | * them, and finally, without --replace= or any positional arguments, just | |
4655 | * read configuration and execute it. | |
4656 | */ | |
4657 | if (arg_replace || optind >= argc) | |
a5e4f3ab | 4658 | r = read_config_files(&c, config_dirs, argv + optind, &invalid_config); |
a6d8474f | 4659 | else |
a5e4f3ab | 4660 | r = parse_arguments(&c, config_dirs, argv + optind, &invalid_config); |
a6d8474f | 4661 | if (r < 0) |
87938c3b | 4662 | return r; |
5008d581 | 4663 | |
a5e4f3ab | 4664 | r = read_credential_lines(&c, &invalid_config); |
1d77721f LP |
4665 | if (r < 0) |
4666 | return r; | |
4667 | ||
811a1587 | 4668 | /* Let's now link up all child/parent relationships */ |
a5e4f3ab LP |
4669 | ORDERED_HASHMAP_FOREACH(a, c.items) { |
4670 | r = link_parent(&c, a); | |
811a1587 | 4671 | if (r < 0) |
87938c3b | 4672 | return r; |
1db50423 | 4673 | } |
a5e4f3ab LP |
4674 | ORDERED_HASHMAP_FOREACH(a, c.globs) { |
4675 | r = link_parent(&c, a); | |
811a1587 | 4676 | if (r < 0) |
87938c3b | 4677 | return r; |
811a1587 LP |
4678 | } |
4679 | ||
ad36bb9f ZJS |
4680 | /* If multiple operations are requested, let's first run the remove/clean operations, and only then |
4681 | * the create operations. i.e. that we first clean out the platform we then build on. */ | |
44ac4f88 | 4682 | for (phase = 0; phase < _PHASE_MAX; phase++) { |
64adb379 | 4683 | OperationMask op; |
b8bb3e8f | 4684 | |
81a18380 LB |
4685 | if (phase == PHASE_PURGE) |
4686 | op = arg_operation & OPERATION_PURGE; | |
4687 | else if (phase == PHASE_REMOVE_AND_CLEAN) | |
64adb379 | 4688 | op = arg_operation & (OPERATION_REMOVE|OPERATION_CLEAN); |
44ac4f88 | 4689 | else if (phase == PHASE_CREATE) |
64adb379 LP |
4690 | op = arg_operation & OPERATION_CREATE; |
4691 | else | |
04499a70 | 4692 | assert_not_reached(); |
64adb379 LP |
4693 | |
4694 | if (op == 0) /* Nothing requested in this phase */ | |
4695 | continue; | |
4696 | ||
4697 | /* The non-globbing ones usually create things, hence we apply them first */ | |
a5e4f3ab LP |
4698 | ORDERED_HASHMAP_FOREACH(a, c.items) { |
4699 | k = process_item_array(&c, a, op); | |
87938c3b YW |
4700 | if (k < 0 && r >= 0) |
4701 | r = k; | |
64adb379 LP |
4702 | } |
4703 | ||
4704 | /* The globbing ones usually alter things, hence we apply them second. */ | |
a5e4f3ab LP |
4705 | ORDERED_HASHMAP_FOREACH(a, c.globs) { |
4706 | k = process_item_array(&c, a, op); | |
87938c3b YW |
4707 | if (k < 0 && r >= 0) |
4708 | r = k; | |
64adb379 | 4709 | } |
1db50423 | 4710 | } |
3b63d2d3 | 4711 | |
95d2155a | 4712 | if (ERRNO_IS_RESOURCE(r)) |
87938c3b YW |
4713 | return r; |
4714 | if (invalid_config) | |
d9daae55 | 4715 | return EX_DATAERR; |
87938c3b | 4716 | if (r < 0) |
bb9947be | 4717 | return EX_CANTCREAT; |
87938c3b | 4718 | return 0; |
5008d581 | 4719 | } |
87938c3b YW |
4720 | |
4721 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |