blob: d3b2183cbd020b45fc6a50fe24a318beef458ac4 [file] [log] [blame]
Zbigniew Jędrzejewski-Szmek53e1b682017-11-18 17:09:20 +01001/* SPDX-License-Identifier: LGPL-2.1+ */
Lennart Poettering5008d582010-09-28 02:34:02 +02002
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +02003#include <errno.h>
4#include <fcntl.h>
5#include <fnmatch.h>
6#include <getopt.h>
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +02007#include <limits.h>
8#include <linux/fs.h>
9#include <stdbool.h>
10#include <stddef.h>
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020011#include <stdlib.h>
Lennart Poettering52b32b22019-01-18 16:09:30 +010012#include <sys/file.h>
Maciej Wereskiebf4e802014-12-04 10:32:10 +010013#include <sys/xattr.h>
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +010014#include <sysexits.h>
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020015#include <time.h>
16#include <unistd.h>
Lennart Poettering5008d582010-09-28 02:34:02 +020017
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +010018#include "sd-path.h"
19
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020020#include "acl-util.h"
Lennart Poetteringb5efdb82015-10-27 03:01:06 +010021#include "alloc-util.h"
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020022#include "btrfs-util.h"
Lennart Poettering430f0182015-10-26 23:32:16 +010023#include "capability-util.h"
Lennart Poetteringc8b30942015-10-26 20:39:23 +010024#include "chattr-util.h"
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020025#include "conf-files.h"
26#include "copy.h"
Lennart Poetteringa0f29c72015-11-03 12:26:12 +010027#include "def.h"
Reverend Homer8fb3f002016-12-09 12:04:30 +030028#include "dirent-util.h"
Lennart Poettering4f5dd392015-10-23 18:52:53 +020029#include "escape.h"
Lennart Poettering3ffd4af2015-10-25 13:14:12 +010030#include "fd-util.h"
Lennart Poettering0d39fa92015-10-26 18:05:03 +010031#include "fileio.h"
Zbigniew Jędrzejewski-Szmekf97b34a2016-11-07 10:14:59 -050032#include "format-util.h"
Lennart Poetteringf4f15632015-10-26 21:16:26 +010033#include "fs-util.h"
Lennart Poettering7d50b322015-10-27 01:48:17 +010034#include "glob-util.h"
Lennart Poetteringc0044932015-10-25 14:08:25 +010035#include "io-util.h"
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020036#include "label.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020037#include "log.h"
Dave Reisner54693d92012-09-15 12:58:49 -040038#include "macro.h"
Yu Watanabe87938c32018-11-26 01:00:54 +090039#include "main-func.h"
Kay Sievers49e942b2012-04-10 21:54:31 +020040#include "mkdir.h"
Zbigniew Jędrzejewski-Szmek049af8a2018-11-29 10:24:39 +010041#include "mountpoint-util.h"
Lennart Poetteringdcd5c892018-06-12 15:37:53 +020042#include "pager.h"
Lennart Poettering6bedfcb2015-10-26 16:18:16 +010043#include "parse-util.h"
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +010044#include "path-lookup.h"
Kay Sievers9eb977d2012-05-07 21:36:12 +020045#include "path-util.h"
Zbigniew Jędrzejewski-Szmek294bf0c2018-11-20 15:42:57 +010046#include "pretty-print.h"
Lennart Poetteringe5358402019-01-18 16:34:52 +010047#include "rlimit-util.h"
Lennart Poetteringc6878632015-04-04 11:52:57 +020048#include "rm-rf.h"
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +010049#include "selinux-util.h"
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020050#include "set.h"
Lennart Poettering760877e2019-03-13 12:14:47 +010051#include "sort-util.h"
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020052#include "specifier.h"
Lennart Poettering8fcde012015-10-26 22:01:44 +010053#include "stat-util.h"
Lennart Poettering15a5e952015-10-27 01:26:31 +010054#include "stdio-util.h"
Lennart Poettering8b434402015-10-26 22:31:05 +010055#include "string-table.h"
Lennart Poettering07630ce2015-10-24 22:58:24 +020056#include "string-util.h"
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020057#include "strv.h"
Lennart Poetteringaffb60b2015-10-26 23:20:41 +010058#include "umask-util.h"
Lennart Poetteringb1d4f8e2015-10-25 22:32:30 +010059#include "user-util.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020060
Andreas Jaeger01000472010-09-29 10:08:24 +020061/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020062 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020063 * properly owned directories beneath /tmp, /var/tmp, /run, which are
64 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020065
Lennart Poettering1a967b62018-10-25 23:19:00 +020066typedef enum OperationMask {
67 OPERATION_CREATE = 1 << 0,
68 OPERATION_REMOVE = 1 << 1,
69 OPERATION_CLEAN = 1 << 2,
70} OperationMask;
71
Michal Schmidt66ccd032011-12-15 21:31:14 +010072typedef enum ItemType {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010073 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020074 CREATE_FILE = 'f',
Zach Smitheccebf42019-10-28 09:32:16 -070075 TRUNCATE_FILE = 'F', /* deprecated: use f+ */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020076 CREATE_DIRECTORY = 'd',
77 TRUNCATE_DIRECTORY = 'D',
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +010078 CREATE_SUBVOLUME = 'v',
Lennart Poettering5fb13eb2015-10-21 19:46:23 +020079 CREATE_SUBVOLUME_INHERIT_QUOTA = 'q',
80 CREATE_SUBVOLUME_NEW_QUOTA = 'Q',
Lennart Poetteringee17ee72011-07-12 03:56:56 +020081 CREATE_FIFO = 'p',
Lennart Poettering468d7262012-01-17 15:04:12 +010082 CREATE_SYMLINK = 'L',
83 CREATE_CHAR_DEVICE = 'c',
84 CREATE_BLOCK_DEVICE = 'b',
Lennart Poettering849958d2014-06-10 23:02:40 +020085 COPY_FILES = 'C',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010086
87 /* These ones take globs */
Lennart Poettering17493fa2015-04-10 16:22:22 +020088 WRITE_FILE = 'w',
Zbigniew Jędrzejewski-Szmekdf8dee82016-04-20 00:06:25 -040089 EMPTY_DIRECTORY = 'e',
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -050090 SET_XATTR = 't',
91 RECURSIVE_SET_XATTR = 'T',
92 SET_ACL = 'a',
93 RECURSIVE_SET_ACL = 'A',
Lennart Poettering17493fa2015-04-10 16:22:22 +020094 SET_ATTRIBUTE = 'h',
95 RECURSIVE_SET_ATTRIBUTE = 'H',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020096 IGNORE_PATH = 'x',
Michal Sekletar78a92a52013-01-18 16:13:08 +010097 IGNORE_DIRECTORY_PATH = 'X',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020098 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010099 RECURSIVE_REMOVE_PATH = 'R',
Michal Schmidt777b87e2011-12-16 18:27:35 +0100100 RELABEL_PATH = 'z',
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200101 RECURSIVE_RELABEL_PATH = 'Z',
Lennart Poettering17493fa2015-04-10 16:22:22 +0200102 ADJUST_MODE = 'm', /* legacy, 'z' is identical to this */
Michal Schmidt66ccd032011-12-15 21:31:14 +0100103} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200104
105typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +0100106 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200107
108 char *path;
Lennart Poettering468d7262012-01-17 15:04:12 +0100109 char *argument;
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100110 char **xattrs;
Zbigniew Jędrzejewski-Szmek349cc4a2017-10-03 10:41:51 +0200111#if HAVE_ACL
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500112 acl_t acl_access;
113 acl_t acl_default;
114#endif
Lennart Poettering5008d582010-09-28 02:34:02 +0200115 uid_t uid;
116 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200117 mode_t mode;
118 usec_t age;
119
Lennart Poettering468d7262012-01-17 15:04:12 +0100120 dev_t major_minor;
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200121 unsigned attribute_value;
122 unsigned attribute_mask;
Lennart Poettering468d7262012-01-17 15:04:12 +0100123
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200124 bool uid_set:1;
125 bool gid_set:1;
126 bool mode_set:1;
127 bool age_set:1;
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200128 bool mask_perms:1;
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200129 bool attribute_set:1;
Lennart Poettering24f3a372012-06-20 09:05:50 +0200130
131 bool keep_first_level:1;
Lennart Poettering1910cd02014-06-11 01:37:35 +0200132
Zach Smithc55ac242019-10-03 18:09:35 -0700133 bool append_or_force:1;
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200134
William Douglas6d7b5432018-09-10 12:07:29 -0700135 bool allow_failure:1;
136
Lennart Poettering811a1582018-10-26 20:56:37 +0200137 OperationMask done;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200138} Item;
139
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500140typedef struct ItemArray {
141 Item *items;
Lennart Poettering96d10d72018-10-25 13:51:01 +0200142 size_t n_items;
Lennart Poettering64768712018-10-25 13:48:25 +0200143 size_t allocated;
Lennart Poettering811a1582018-10-26 20:56:37 +0200144
145 struct ItemArray *parent;
146 Set *children;
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500147} ItemArray;
148
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100149typedef enum DirectoryType {
Lennart Poettering133bbca2018-10-26 10:32:48 +0200150 DIRECTORY_RUNTIME,
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100151 DIRECTORY_STATE,
152 DIRECTORY_CACHE,
153 DIRECTORY_LOGS,
154 _DIRECTORY_TYPE_MAX,
155} DirectoryType;
156
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +0200157static bool arg_cat_config = false;
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +0100158static bool arg_user = false;
Lennart Poettering1a967b62018-10-25 23:19:00 +0200159static OperationMask arg_operation = 0;
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -0500160static bool arg_boot = false;
Zbigniew Jędrzejewski-Szmek0221d682018-11-11 12:56:29 +0100161static PagerFlags arg_pager_flags = 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200162
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200163static char **arg_include_prefixes = NULL;
164static char **arg_exclude_prefixes = NULL;
Michael Marineaucf9a4ab2014-03-13 21:32:13 -0700165static char *arg_root = NULL;
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +0100166static char *arg_replace = NULL;
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100167
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200168#define MAX_DEPTH 256
169
Lennart Poetteringef43a392015-04-22 18:18:56 +0200170static OrderedHashmap *items = NULL, *globs = NULL;
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200171static Set *unix_sockets = NULL;
172
Yu Watanabe87938c32018-11-26 01:00:54 +0900173STATIC_DESTRUCTOR_REGISTER(items, ordered_hashmap_freep);
174STATIC_DESTRUCTOR_REGISTER(globs, ordered_hashmap_freep);
175STATIC_DESTRUCTOR_REGISTER(unix_sockets, set_free_freep);
176STATIC_DESTRUCTOR_REGISTER(arg_include_prefixes, freep);
177STATIC_DESTRUCTOR_REGISTER(arg_exclude_prefixes, freep);
178STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
179
Zbigniew Jędrzejewski-Szmek303ee602018-12-12 16:45:33 +0100180static int specifier_machine_id_safe(char specifier, const void *data, const void *userdata, char **ret);
181static int specifier_directory(char specifier, const void *data, const void *userdata, char **ret);
Franck Bui4cef1922017-11-16 11:27:29 +0100182
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200183static const Specifier specifier_table[] = {
Franck Bui4cef1922017-11-16 11:27:29 +0100184 { 'm', specifier_machine_id_safe, NULL },
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100185 { 'b', specifier_boot_id, NULL },
186 { 'H', specifier_host_name, NULL },
187 { 'v', specifier_kernel_release, NULL },
ayekatca23eeb2017-11-24 12:44:08 +0100188
Davide Cavalcab75f0c62018-10-13 01:26:48 -0700189 { 'g', specifier_group_name, NULL },
190 { 'G', specifier_group_id, NULL },
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100191 { 'U', specifier_user_id, NULL },
192 { 'u', specifier_user_name, NULL },
193 { 'h', specifier_user_home, NULL },
Lennart Poetteringb294e592018-05-29 11:25:26 +0200194
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100195 { 't', specifier_directory, UINT_TO_PTR(DIRECTORY_RUNTIME) },
196 { 'S', specifier_directory, UINT_TO_PTR(DIRECTORY_STATE) },
197 { 'C', specifier_directory, UINT_TO_PTR(DIRECTORY_CACHE) },
198 { 'L', specifier_directory, UINT_TO_PTR(DIRECTORY_LOGS) },
Lennart Poetteringb294e592018-05-29 11:25:26 +0200199 { 'T', specifier_tmp_dir, NULL },
200 { 'V', specifier_var_tmp_dir, NULL },
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200201 {}
202};
203
Zbigniew Jędrzejewski-Szmek303ee602018-12-12 16:45:33 +0100204static int specifier_machine_id_safe(char specifier, const void *data, const void *userdata, char **ret) {
Franck Bui4cef1922017-11-16 11:27:29 +0100205 int r;
206
Franck Buid8dab752018-01-10 23:28:44 +0100207 /* If /etc/machine_id is missing or empty (e.g. in a chroot environment)
208 * return a recognizable error so that the caller can skip the rule
Franck Bui4cef1922017-11-16 11:27:29 +0100209 * gracefully. */
210
211 r = specifier_machine_id(specifier, data, userdata, ret);
Franck Buid8dab752018-01-10 23:28:44 +0100212 if (IN_SET(r, -ENOENT, -ENOMEDIUM))
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100213 return -ENXIO;
Franck Bui4cef1922017-11-16 11:27:29 +0100214
215 return r;
216}
217
Zbigniew Jędrzejewski-Szmek303ee602018-12-12 16:45:33 +0100218static int specifier_directory(char specifier, const void *data, const void *userdata, char **ret) {
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100219 struct table_entry {
220 uint64_t type;
221 const char *suffix;
222 };
223
224 static const struct table_entry paths_system[] = {
225 [DIRECTORY_RUNTIME] = { SD_PATH_SYSTEM_RUNTIME },
226 [DIRECTORY_STATE] = { SD_PATH_SYSTEM_STATE_PRIVATE },
227 [DIRECTORY_CACHE] = { SD_PATH_SYSTEM_STATE_CACHE },
228 [DIRECTORY_LOGS] = { SD_PATH_SYSTEM_STATE_LOGS },
229 };
230
231 static const struct table_entry paths_user[] = {
232 [DIRECTORY_RUNTIME] = { SD_PATH_USER_RUNTIME },
233 [DIRECTORY_STATE] = { SD_PATH_USER_CONFIGURATION },
234 [DIRECTORY_CACHE] = { SD_PATH_USER_STATE_CACHE },
235 [DIRECTORY_LOGS] = { SD_PATH_USER_CONFIGURATION, "log" },
236 };
237
238 unsigned i;
239 const struct table_entry *paths;
240
241 assert_cc(ELEMENTSOF(paths_system) == ELEMENTSOF(paths_user));
242 paths = arg_user ? paths_user : paths_system;
243
244 i = PTR_TO_UINT(data);
245 assert(i < ELEMENTSOF(paths_system));
246
Zbigniew Jędrzejewski-Szmek51327bc2020-03-23 19:50:59 +0100247 return sd_path_lookup(paths[i].type, paths[i].suffix, ret);
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100248}
249
Franck Bui4cef1922017-11-16 11:27:29 +0100250static int log_unresolvable_specifier(const char *filename, unsigned line) {
251 static bool notified = false;
252
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100253 /* In system mode, this is called when /etc is not fully initialized (e.g.
254 * in a chroot environment) where some specifiers are unresolvable. In user
255 * mode, this is called when some variables are not defined. These cases are
256 * not considered as an error so log at LOG_NOTICE only for the first time
257 * and then downgrade this to LOG_DEBUG for the rest. */
Franck Bui4cef1922017-11-16 11:27:29 +0100258
Lennart Poetteringd02933f2020-04-29 00:07:06 +0200259 log_syntax(NULL,
260 notified ? LOG_DEBUG : LOG_NOTICE,
261 filename, line, 0,
262 "Failed to resolve specifier: %s, skipping",
263 arg_user ? "Required $XDG_... variable not defined" : "uninitialized /etc detected");
Franck Bui4cef1922017-11-16 11:27:29 +0100264
265 if (!notified)
266 log_notice("All rules containing unresolvable specifiers will be skipped.");
267
268 notified = true;
269 return 0;
270}
271
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +0100272static int user_config_paths(char*** ret) {
273 _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
274 _cleanup_free_ char *persistent_config = NULL, *runtime_config = NULL, *data_home = NULL;
275 _cleanup_strv_free_ char **res = NULL;
276 int r;
277
278 r = xdg_user_dirs(&config_dirs, &data_dirs);
279 if (r < 0)
280 return r;
281
282 r = xdg_user_config_dir(&persistent_config, "/user-tmpfiles.d");
283 if (r < 0 && r != -ENXIO)
284 return r;
285
286 r = xdg_user_runtime_dir(&runtime_config, "/user-tmpfiles.d");
287 if (r < 0 && r != -ENXIO)
288 return r;
289
290 r = xdg_user_data_dir(&data_home, "/user-tmpfiles.d");
291 if (r < 0 && r != -ENXIO)
292 return r;
293
294 r = strv_extend_strv_concat(&res, config_dirs, "/user-tmpfiles.d");
295 if (r < 0)
296 return r;
297
298 r = strv_extend(&res, persistent_config);
299 if (r < 0)
300 return r;
301
302 r = strv_extend(&res, runtime_config);
303 if (r < 0)
304 return r;
305
306 r = strv_extend(&res, data_home);
307 if (r < 0)
308 return r;
309
310 r = strv_extend_strv_concat(&res, data_dirs, "/user-tmpfiles.d");
311 if (r < 0)
312 return r;
313
314 r = path_strv_make_absolute_cwd(res);
315 if (r < 0)
316 return r;
317
Lennart Poetteringae2a15b2018-03-22 16:53:26 +0100318 *ret = TAKE_PTR(res);
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +0100319 return 0;
320}
321
Michal Schmidt66ccd032011-12-15 21:31:14 +0100322static bool needs_glob(ItemType t) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200323 return IN_SET(t,
324 WRITE_FILE,
325 IGNORE_PATH,
326 IGNORE_DIRECTORY_PATH,
327 REMOVE_PATH,
328 RECURSIVE_REMOVE_PATH,
Zbigniew Jędrzejewski-Szmekdf8dee82016-04-20 00:06:25 -0400329 EMPTY_DIRECTORY,
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200330 ADJUST_MODE,
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200331 RELABEL_PATH,
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -0500332 RECURSIVE_RELABEL_PATH,
333 SET_XATTR,
334 RECURSIVE_SET_XATTR,
335 SET_ACL,
Lennart Poettering34f64532015-04-09 13:12:06 +0200336 RECURSIVE_SET_ACL,
337 SET_ATTRIBUTE,
338 RECURSIVE_SET_ATTRIBUTE);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100339}
340
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500341static bool takes_ownership(ItemType t) {
342 return IN_SET(t,
343 CREATE_FILE,
344 TRUNCATE_FILE,
345 CREATE_DIRECTORY,
Zbigniew Jędrzejewski-Szmekdf8dee82016-04-20 00:06:25 -0400346 EMPTY_DIRECTORY,
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500347 TRUNCATE_DIRECTORY,
348 CREATE_SUBVOLUME,
Lennart Poettering5fb13eb2015-10-21 19:46:23 +0200349 CREATE_SUBVOLUME_INHERIT_QUOTA,
350 CREATE_SUBVOLUME_NEW_QUOTA,
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500351 CREATE_FIFO,
352 CREATE_SYMLINK,
353 CREATE_CHAR_DEVICE,
354 CREATE_BLOCK_DEVICE,
355 COPY_FILES,
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500356 WRITE_FILE,
357 IGNORE_PATH,
358 IGNORE_DIRECTORY_PATH,
359 REMOVE_PATH,
360 RECURSIVE_REMOVE_PATH);
361}
362
Lennart Poetteringef43a392015-04-22 18:18:56 +0200363static struct Item* find_glob(OrderedHashmap *h, const char *match) {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500364 ItemArray *j;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100365 Iterator i;
366
Lennart Poetteringef43a392015-04-22 18:18:56 +0200367 ORDERED_HASHMAP_FOREACH(j, h, i) {
Lennart Poettering96d10d72018-10-25 13:51:01 +0200368 size_t n;
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500369
Lennart Poettering96d10d72018-10-25 13:51:01 +0200370 for (n = 0; n < j->n_items; n++) {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500371 Item *item = j->items + n;
372
373 if (fnmatch(item->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
374 return item;
375 }
376 }
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100377
378 return NULL;
379}
380
Yu Watanabe71a0be72018-11-26 00:48:15 +0900381static int load_unix_sockets(void) {
382 _cleanup_set_free_free_ Set *sockets = NULL;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200383 _cleanup_fclose_ FILE *f = NULL;
Lennart Poetteringf1ff7342018-01-10 17:28:03 +0100384 int r;
Lennart Poettering17b90522011-02-14 21:55:06 +0100385
386 if (unix_sockets)
Yu Watanabe71a0be72018-11-26 00:48:15 +0900387 return 0;
Lennart Poettering17b90522011-02-14 21:55:06 +0100388
Lennart Poetteringf1ff7342018-01-10 17:28:03 +0100389 /* We maintain a cache of the sockets we found in /proc/net/unix to speed things up a little. */
Lennart Poettering17b90522011-02-14 21:55:06 +0100390
Yu Watanabe71a0be72018-11-26 00:48:15 +0900391 sockets = set_new(&path_hash_ops);
392 if (!sockets)
393 return log_oom();
Lennart Poettering17b90522011-02-14 21:55:06 +0100394
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100395 f = fopen("/proc/net/unix", "re");
Yu Watanabe71a0be72018-11-26 00:48:15 +0900396 if (!f)
397 return log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
398 "Failed to open /proc/net/unix, ignoring: %m");
Lennart Poettering17b90522011-02-14 21:55:06 +0100399
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100400 /* Skip header */
Lennart Poetteringf1ff7342018-01-10 17:28:03 +0100401 r = read_line(f, LONG_LINE_MAX, NULL);
Yu Watanabe71a0be72018-11-26 00:48:15 +0900402 if (r < 0)
403 return log_warning_errno(r, "Failed to skip /proc/net/unix header line: %m");
404 if (r == 0)
405 return log_warning_errno(SYNTHETIC_ERRNO(EIO), "Premature end of file reading /proc/net/unix.");
Lennart Poettering17b90522011-02-14 21:55:06 +0100406
407 for (;;) {
Yu Watanabe71a0be72018-11-26 00:48:15 +0900408 _cleanup_free_ char *line = NULL, *s = NULL;
409 char *p;
Lennart Poettering17b90522011-02-14 21:55:06 +0100410
Lennart Poetteringf1ff7342018-01-10 17:28:03 +0100411 r = read_line(f, LONG_LINE_MAX, &line);
Yu Watanabe71a0be72018-11-26 00:48:15 +0900412 if (r < 0)
413 return log_warning_errno(r, "Failed to read /proc/net/unix line, ignoring: %m");
Lennart Poetteringf1ff7342018-01-10 17:28:03 +0100414 if (r == 0) /* EOF */
Lennart Poettering17b90522011-02-14 21:55:06 +0100415 break;
416
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100417 p = strchr(line, ':');
418 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100419 continue;
420
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100421 if (strlen(p) < 37)
422 continue;
423
424 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100425 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100426 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100427 p += strspn(p, WHITESPACE);
428
429 if (*p != '/')
430 continue;
431
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100432 s = strdup(p);
Yu Watanabe71a0be72018-11-26 00:48:15 +0900433 if (!s)
434 return log_oom();
Lennart Poettering17b90522011-02-14 21:55:06 +0100435
Yu Watanabe858d36c2018-05-31 23:39:31 +0900436 path_simplify(s, false);
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100437
Yu Watanabe71a0be72018-11-26 00:48:15 +0900438 r = set_consume(sockets, s);
439 if (r == -EEXIST)
440 continue;
441 if (r < 0)
442 return log_warning_errno(r, "Failed to add AF_UNIX socket to set, ignoring: %m");
443
444 TAKE_PTR(s);
Lennart Poettering17b90522011-02-14 21:55:06 +0100445 }
446
Yu Watanabe71a0be72018-11-26 00:48:15 +0900447 unix_sockets = TAKE_PTR(sockets);
448 return 1;
Lennart Poettering17b90522011-02-14 21:55:06 +0100449}
450
451static bool unix_socket_alive(const char *fn) {
452 assert(fn);
453
Yu Watanabe71a0be72018-11-26 00:48:15 +0900454 if (load_unix_sockets() < 0)
455 return true; /* We don't know, so assume yes */
Lennart Poettering17b90522011-02-14 21:55:06 +0100456
Yu Watanabe71a0be72018-11-26 00:48:15 +0900457 return !!set_get(unix_sockets, (char*) fn);
Lennart Poettering17b90522011-02-14 21:55:06 +0100458}
459
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -0500460static DIR* xopendirat_nomod(int dirfd, const char *path) {
461 DIR *dir;
462
463 dir = xopendirat(dirfd, path, O_NOFOLLOW|O_NOATIME);
Lennart Poettering15322272015-03-23 18:54:31 +0700464 if (dir)
465 return dir;
466
467 log_debug_errno(errno, "Cannot open %sdirectory \"%s\": %m", dirfd == AT_FDCWD ? "" : "sub", path);
468 if (errno != EPERM)
469 return NULL;
470
471 dir = xopendirat(dirfd, path, O_NOFOLLOW);
472 if (!dir)
473 log_debug_errno(errno, "Cannot open %sdirectory \"%s\": %m", dirfd == AT_FDCWD ? "" : "sub", path);
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -0500474
475 return dir;
476}
477
478static DIR* opendir_nomod(const char *path) {
479 return xopendirat_nomod(AT_FDCWD, path);
480}
481
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200482static int dir_cleanup(
Michal Sekletar78a92a52013-01-18 16:13:08 +0100483 Item *i,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200484 const char *p,
485 DIR *d,
486 const struct stat *ds,
487 usec_t cutoff,
488 dev_t rootdev,
489 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200490 int maxdepth,
Lennart Poettering265ffa12013-09-17 16:33:30 -0500491 bool keep_this_level) {
492
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200493 struct dirent *dent;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200494 bool deleted = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200495 int r = 0;
496
Reverend Homer8fb3f002016-12-09 12:04:30 +0300497 FOREACH_DIRENT_ALL(dent, d, break) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200498 struct stat s;
499 usec_t age;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200500 _cleanup_free_ char *sub_path = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200501
Lennart Poettering49bfc872017-02-02 00:06:18 +0100502 if (dot_or_dot_dot(dent->d_name))
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200503 continue;
504
505 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
Kay Sieversca2f4172013-10-17 03:20:46 +0200506 if (errno == ENOENT)
507 continue;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200508
Kay Sieversca2f4172013-10-17 03:20:46 +0200509 /* FUSE, NFS mounts, SELinux might return EACCES */
Lennart Poetteringecc17092018-01-22 15:29:30 +0100510 r = log_full_errno(errno == EACCES ? LOG_DEBUG : LOG_ERR, errno,
511 "stat(%s/%s) failed: %m", p, dent->d_name);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200512 continue;
513 }
514
515 /* Stay on the same filesystem */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500516 if (s.st_dev != rootdev) {
517 log_debug("Ignoring \"%s/%s\": different filesystem.", p, dent->d_name);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200518 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500519 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200520
Kay Sievers99d680a2013-03-13 13:12:36 +0100521 /* Try to detect bind mounts of the same filesystem instance; they
522 * do not differ in device major/minors. This type of query is not
523 * supported on all kernels or filesystem types though. */
Lennart Poettering60bdc0c2019-06-18 12:42:30 +0200524 if (S_ISDIR(s.st_mode)) {
525 int q;
526
527 q = fd_is_mount_point(dirfd(d), dent->d_name, 0);
528 if (q < 0)
529 log_debug_errno(q, "Failed to determine whether \"%s/%s\" is a mount point, ignoring: %m", p, dent->d_name);
530 else if (q > 0) {
531 log_debug("Ignoring \"%s/%s\": different mount of the same filesystem.", p, dent->d_name);
532 continue;
533 }
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500534 }
Kay Sievers99d680a2013-03-13 13:12:36 +0100535
Lennart Poettering113ed3b2019-06-18 12:41:02 +0200536 sub_path = path_join(p, dent->d_name);
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200537 if (!sub_path) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700538 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200539 goto finish;
540 }
541
542 /* Is there an item configured for this path? */
Lennart Poetteringef43a392015-04-22 18:18:56 +0200543 if (ordered_hashmap_get(items, sub_path)) {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500544 log_debug("Ignoring \"%s\": a separate entry exists.", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200545 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500546 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200547
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500548 if (find_glob(globs, sub_path)) {
549 log_debug("Ignoring \"%s\": a separate glob exists.", sub_path);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100550 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500551 }
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100552
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200553 if (S_ISDIR(s.st_mode)) {
Lennart Poettering52b32b22019-01-18 16:09:30 +0100554 _cleanup_closedir_ DIR *sub_dir = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200555
556 if (mountpoint &&
557 streq(dent->d_name, "lost+found") &&
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500558 s.st_uid == 0) {
Lennart Poettering781bc442019-01-18 16:07:45 +0100559 log_debug("Ignoring directory \"%s\".", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200560 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500561 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200562
563 if (maxdepth <= 0)
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500564 log_warning("Reached max depth on \"%s\".", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200565 else {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200566 int q;
567
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -0500568 sub_dir = xopendirat_nomod(dirfd(d), dent->d_name);
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200569 if (!sub_dir) {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500570 if (errno != ENOENT)
Lennart Poettering781bc442019-01-18 16:07:45 +0100571 r = log_warning_errno(errno, "Opening directory \"%s\" failed, ignoring: %m", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200572
573 continue;
574 }
575
Lennart Poettering52b32b22019-01-18 16:09:30 +0100576 if (flock(dirfd(sub_dir), LOCK_EX|LOCK_NB) < 0) {
577 log_debug_errno(errno, "Couldn't acquire shared BSD lock on directory \"%s\", skipping: %m", p);
578 continue;
579 }
580
Michal Sekletar78a92a52013-01-18 16:13:08 +0100581 q = dir_cleanup(i, sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200582 if (q < 0)
583 r = q;
584 }
585
Lennart Poetteringa23fabc2019-01-18 15:52:31 +0100586 /* Note: if you are wondering why we don't support the sticky bit for excluding
587 * directories from cleaning like we do it for other file system objects: well, the
588 * sticky bit already has a meaning for directories, so we don't want to overload
589 * that. */
Lennart Poettering24f3a372012-06-20 09:05:50 +0200590
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500591 if (keep_this_level) {
Lennart Poettering781bc442019-01-18 16:07:45 +0100592 log_debug("Keeping directory \"%s\".", sub_path);
Lennart Poettering24f3a372012-06-20 09:05:50 +0200593 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500594 }
Lennart Poettering24f3a372012-06-20 09:05:50 +0200595
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200596 /* Ignore ctime, we change it when deleting */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500597 age = timespec_load(&s.st_mtim);
598 if (age >= cutoff) {
599 char a[FORMAT_TIMESTAMP_MAX];
600 /* Follows spelling in stat(1). */
601 log_debug("Directory \"%s\": modify time %s is too new.",
602 sub_path,
603 format_timestamp_us(a, sizeof(a), age));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200604 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500605 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200606
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500607 age = timespec_load(&s.st_atim);
608 if (age >= cutoff) {
609 char a[FORMAT_TIMESTAMP_MAX];
610 log_debug("Directory \"%s\": access time %s is too new.",
611 sub_path,
612 format_timestamp_us(a, sizeof(a), age));
613 continue;
614 }
615
Zbigniew Jędrzejewski-Szmek61253222015-01-31 01:03:09 -0500616 log_debug("Removing directory \"%s\".", sub_path);
617 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0)
Lennart Poettering920ce822018-01-22 15:31:01 +0100618 if (!IN_SET(errno, ENOENT, ENOTEMPTY))
Lennart Poettering781bc442019-01-18 16:07:45 +0100619 r = log_warning_errno(errno, "Failed to remove directory \"%s\", ignoring: %m", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200620
621 } else {
Lennart Poetteringa23fabc2019-01-18 15:52:31 +0100622 /* Skip files for which the sticky bit is set. These are semantics we define, and are
623 * unknown elsewhere. See XDG_RUNTIME_DIR specification for details. */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500624 if (s.st_mode & S_ISVTX) {
625 log_debug("Skipping \"%s\": sticky bit set.", sub_path);
Lennart Poettering9c737362010-11-14 20:12:51 +0100626 continue;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200627 }
628
Lennart Poettering20b6bb92019-06-18 12:41:31 +0200629 if (mountpoint &&
630 S_ISREG(s.st_mode) &&
631 s.st_uid == 0 &&
632 STR_IN_SET(dent->d_name,
633 ".journal",
634 "aquota.user",
635 "aquota.group")) {
636 log_debug("Skipping \"%s\".", sub_path);
637 continue;
638 }
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500639
Lennart Poettering17b90522011-02-14 21:55:06 +0100640 /* Ignore sockets that are listed in /proc/net/unix */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500641 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path)) {
642 log_debug("Skipping \"%s\": live socket.", sub_path);
Lennart Poettering17b90522011-02-14 21:55:06 +0100643 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500644 }
Lennart Poettering17b90522011-02-14 21:55:06 +0100645
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100646 /* Ignore device nodes */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500647 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode)) {
648 log_debug("Skipping \"%s\": a device.", sub_path);
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100649 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500650 }
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100651
Lennart Poetteringa23fabc2019-01-18 15:52:31 +0100652 /* Keep files on this level around if this is requested */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500653 if (keep_this_level) {
654 log_debug("Keeping \"%s\".", sub_path);
Lennart Poettering24f3a372012-06-20 09:05:50 +0200655 continue;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200656 }
657
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500658 age = timespec_load(&s.st_mtim);
659 if (age >= cutoff) {
660 char a[FORMAT_TIMESTAMP_MAX];
661 /* Follows spelling in stat(1). */
662 log_debug("File \"%s\": modify time %s is too new.",
663 sub_path,
664 format_timestamp_us(a, sizeof(a), age));
665 continue;
666 }
667
668 age = timespec_load(&s.st_atim);
669 if (age >= cutoff) {
670 char a[FORMAT_TIMESTAMP_MAX];
671 log_debug("File \"%s\": access time %s is too new.",
672 sub_path,
673 format_timestamp_us(a, sizeof(a), age));
674 continue;
675 }
676
677 age = timespec_load(&s.st_ctim);
678 if (age >= cutoff) {
679 char a[FORMAT_TIMESTAMP_MAX];
680 log_debug("File \"%s\": change time %s is too new.",
681 sub_path,
682 format_timestamp_us(a, sizeof(a), age));
683 continue;
684 }
685
Lennart Poettering781bc442019-01-18 16:07:45 +0100686 log_debug("Removing \"%s\".", sub_path);
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500687 if (unlinkat(dirfd(d), dent->d_name, 0) < 0)
688 if (errno != ENOENT)
Lennart Poettering781bc442019-01-18 16:07:45 +0100689 r = log_warning_errno(errno, "Failed to remove \"%s\", ignoring: %m", sub_path);
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500690
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200691 deleted = true;
692 }
693 }
694
695finish:
696 if (deleted) {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500697 char a[FORMAT_TIMESTAMP_MAX], b[FORMAT_TIMESTAMP_MAX];
Lennart Poettering76e15a92019-01-18 15:51:13 +0100698 usec_t age1, age2;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200699
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500700 age1 = timespec_load(&ds->st_atim);
701 age2 = timespec_load(&ds->st_mtim);
Lennart Poettering76e15a92019-01-18 15:51:13 +0100702
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500703 log_debug("Restoring access and modification time on \"%s\": %s, %s",
704 p,
705 format_timestamp_us(a, sizeof(a), age1),
706 format_timestamp_us(b, sizeof(b), age2));
Lennart Poettering76e15a92019-01-18 15:51:13 +0100707
708 /* Restore original directory timestamps */
709 if (futimens(dirfd(d), (struct timespec[]) {
710 ds->st_atim,
711 ds->st_mtim }) < 0)
712 log_warning_errno(errno, "Failed to revert timestamps of '%s', ignoring: %m", p);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200713 }
714
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200715 return r;
716}
717
Lennart Poettering5579f852018-01-23 14:03:34 +0100718static bool dangerous_hardlinks(void) {
719 _cleanup_free_ char *value = NULL;
720 static int cached = -1;
721 int r;
722
723 /* Check whether the fs.protected_hardlinks sysctl is on. If we can't determine it we assume its off, as that's
724 * what the upstream default is. */
725
726 if (cached >= 0)
727 return cached;
728
729 r = read_one_line_file("/proc/sys/fs/protected_hardlinks", &value);
730 if (r < 0) {
731 log_debug_errno(r, "Failed to read fs.protected_hardlinks sysctl: %m");
732 return true;
733 }
734
735 r = parse_boolean(value);
736 if (r < 0) {
737 log_debug_errno(r, "Failed to parse fs.protected_hardlinks sysctl: %m");
738 return true;
739 }
740
741 cached = r == 0;
742 return cached;
743}
744
Franck Bui774f79b2018-03-02 16:13:07 +0100745static bool hardlink_vulnerable(const struct stat *st) {
Lennart Poettering5579f852018-01-23 14:03:34 +0100746 assert(st);
747
748 return !S_ISDIR(st->st_mode) && st->st_nlink > 1 && dangerous_hardlinks();
749}
750
Lennart Poettering8b364a32019-04-29 13:21:40 +0200751static mode_t process_mask_perms(mode_t mode, mode_t current) {
752
753 if ((current & 0111) == 0)
754 mode &= ~0111;
755 if ((current & 0222) == 0)
756 mode &= ~0222;
757 if ((current & 0444) == 0)
758 mode &= ~0444;
759 if (!S_ISDIR(current))
760 mode &= ~07000; /* remove sticky/sgid/suid bit, unless directory */
761
762 return mode;
763}
764
Lennart Poettering9d874ae2018-08-06 15:40:16 +0200765static int fd_set_perms(Item *i, int fd, const char *path, const struct stat *st) {
Franck Buib206ac82018-04-13 11:39:39 +0200766 struct stat stbuf;
Lennart Poetteringa9f55d02019-04-29 16:11:52 +0200767 mode_t new_mode;
768 bool do_chown;
Franck Bui936f6bd2018-03-02 17:19:32 +0100769
770 assert(i);
771 assert(fd);
Franck Bui4dc7bfd2018-08-20 17:23:12 +0200772 assert(path);
Franck Bui936f6bd2018-03-02 17:19:32 +0100773
774 if (!i->mode_set && !i->uid_set && !i->gid_set)
775 goto shortcut;
776
Franck Buib206ac82018-04-13 11:39:39 +0200777 if (!st) {
778 if (fstat(fd, &stbuf) < 0)
779 return log_error_errno(errno, "fstat(%s) failed: %m", path);
780 st = &stbuf;
781 }
782
Zbigniew Jędrzejewski-Szmekbaaa35a2018-11-20 23:40:44 +0100783 if (hardlink_vulnerable(st))
784 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
785 "Refusing to set permissions on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.",
786 path);
Franck Bui936f6bd2018-03-02 17:19:32 +0100787
Lennart Poetteringa9f55d02019-04-29 16:11:52 +0200788 /* Do we need a chown()? */
789 do_chown =
790 (i->uid_set && i->uid != st->st_uid) ||
791 (i->gid_set && i->gid != st->st_gid);
Lennart Poettering8b364a32019-04-29 13:21:40 +0200792
Lennart Poetteringa9f55d02019-04-29 16:11:52 +0200793 /* Calculate the mode to apply */
794 new_mode = i->mode_set ? (i->mask_perms ?
795 process_mask_perms(i->mode, st->st_mode) :
796 i->mode) :
797 (st->st_mode & 07777);
Lennart Poettering8b364a32019-04-29 13:21:40 +0200798
Lennart Poetteringa9f55d02019-04-29 16:11:52 +0200799 if (i->mode_set && do_chown) {
800 /* Before we issue the chmod() let's reduce the access mode to the common bits of the old and
801 * the new mode. That way there's no time window where the file exists under the old owner
802 * with more than the old access modes — and not under the new owner with more than the new
803 * access modes either. */
804
Franck Bui936f6bd2018-03-02 17:19:32 +0100805 if (S_ISLNK(st->st_mode))
Lennart Poetteringa9f55d02019-04-29 16:11:52 +0200806 log_debug("Skipping temporary mode fix for symlink %s.", path);
Franck Bui936f6bd2018-03-02 17:19:32 +0100807 else {
Lennart Poetteringa9f55d02019-04-29 16:11:52 +0200808 mode_t m = new_mode & st->st_mode; /* Mask new mode by old mode */
Franck Bui936f6bd2018-03-02 17:19:32 +0100809
Lennart Poetteringa9f55d02019-04-29 16:11:52 +0200810 if (((m ^ st->st_mode) & 07777) == 0)
811 log_debug("\"%s\" matches temporary mode %o already.", path, m);
Franck Bui936f6bd2018-03-02 17:19:32 +0100812 else {
Lennart Poetteringa9f55d02019-04-29 16:11:52 +0200813 log_debug("Temporarily changing \"%s\" to mode %o.", path, m);
Franck Bui4dfaa522018-04-11 16:58:49 +0200814 if (fchmod_opath(fd, m) < 0)
815 return log_error_errno(errno, "fchmod() of %s failed: %m", path);
Franck Bui936f6bd2018-03-02 17:19:32 +0100816 }
817 }
818 }
819
Lennart Poetteringa9f55d02019-04-29 16:11:52 +0200820 if (do_chown) {
Franck Bui936f6bd2018-03-02 17:19:32 +0100821 log_debug("Changing \"%s\" to owner "UID_FMT":"GID_FMT,
822 path,
823 i->uid_set ? i->uid : UID_INVALID,
824 i->gid_set ? i->gid : GID_INVALID);
825
826 if (fchownat(fd,
827 "",
828 i->uid_set ? i->uid : UID_INVALID,
829 i->gid_set ? i->gid : GID_INVALID,
830 AT_EMPTY_PATH) < 0)
831 return log_error_errno(errno, "fchownat() of %s failed: %m", path);
832 }
833
Lennart Poetteringa9f55d02019-04-29 16:11:52 +0200834 /* Now, apply the final mode. We do this in two cases: when the user set a mode explicitly, or after a
835 * chown(), since chown()'s mangle the access mode in regards to sgid/suid in some conditions. */
836 if (i->mode_set || do_chown) {
837 if (S_ISLNK(st->st_mode))
838 log_debug("Skipping mode fix for symlink %s.", path);
839 else {
840 /* Check if the chmod() is unnecessary. Note that if we did a chown() before we always
841 * chmod() here again, since it might have mangled the bits. */
842 if (!do_chown && ((new_mode ^ st->st_mode) & 07777) == 0)
843 log_debug("\"%s\" matches mode %o already.", path, new_mode);
844 else {
845 log_debug("Changing \"%s\" to mode %o.", path, new_mode);
846 if (fchmod_opath(fd, new_mode) < 0)
847 return log_error_errno(errno, "fchmod() of %s failed: %m", path);
848 }
849 }
850 }
851
Franck Bui936f6bd2018-03-02 17:19:32 +0100852shortcut:
Lennart Poettering08c84982018-03-27 07:38:26 +0200853 return label_fix(path, 0);
Franck Bui936f6bd2018-03-02 17:19:32 +0100854}
855
Franck Bui5ec9d062018-04-13 17:31:22 +0200856static int path_open_parent_safe(const char *path) {
857 _cleanup_free_ char *dn = NULL;
Zbigniew Jędrzejewski-Szmeka5648b82019-10-24 10:33:20 +0200858 int r, fd;
Franck Bui5ec9d062018-04-13 17:31:22 +0200859
Zbigniew Jędrzejewski-Szmekbaaa35a2018-11-20 23:40:44 +0100860 if (path_equal(path, "/") || !path_is_normalized(path))
861 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
862 "Failed to open parent of '%s': invalid path.",
863 path);
Franck Bui5ec9d062018-04-13 17:31:22 +0200864
865 dn = dirname_malloc(path);
866 if (!dn)
867 return log_oom();
868
Zbigniew Jędrzejewski-Szmeka5648b82019-10-24 10:33:20 +0200869 r = chase_symlinks(dn, arg_root, CHASE_SAFE|CHASE_WARN, NULL, &fd);
870 if (r < 0 && r != -ENOLINK)
871 return log_error_errno(r, "Failed to validate path %s: %m", path);
Franck Bui5ec9d062018-04-13 17:31:22 +0200872
Zbigniew Jędrzejewski-Szmeka5648b82019-10-24 10:33:20 +0200873 return r < 0 ? r : fd;
Franck Bui5ec9d062018-04-13 17:31:22 +0200874}
875
Franck Buiaddc3e32018-03-20 08:58:48 +0100876static int path_open_safe(const char *path) {
Zbigniew Jędrzejewski-Szmeka5648b82019-10-24 10:33:20 +0200877 int r, fd;
Franck Buiaddc3e32018-03-20 08:58:48 +0100878
879 /* path_open_safe() returns a file descriptor opened with O_PATH after
880 * verifying that the path doesn't contain unsafe transitions, except
881 * for its final component as the function does not follow symlink. */
882
883 assert(path);
884
Zbigniew Jędrzejewski-Szmekbaaa35a2018-11-20 23:40:44 +0100885 if (!path_is_normalized(path))
886 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
887 "Failed to open invalid path '%s'.",
888 path);
Franck Buiaddc3e32018-03-20 08:58:48 +0100889
Zbigniew Jędrzejewski-Szmeka5648b82019-10-24 10:33:20 +0200890 r = chase_symlinks(path, arg_root, CHASE_SAFE|CHASE_WARN|CHASE_NOFOLLOW, NULL, &fd);
891 if (r < 0 && r != -ENOLINK)
892 return log_error_errno(r, "Failed to validate path %s: %m", path);
Franck Buiaddc3e32018-03-20 08:58:48 +0100893
Zbigniew Jędrzejewski-Szmeka5648b82019-10-24 10:33:20 +0200894 return r < 0 ? r : fd;
Franck Buiaddc3e32018-03-20 08:58:48 +0100895}
896
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -0500897static int path_set_perms(Item *i, const char *path) {
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200898 _cleanup_close_ int fd = -1;
Michael Olbrich1924a972014-08-17 09:45:00 +0200899
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200900 assert(i);
901 assert(path);
902
Franck Buiaddc3e32018-03-20 08:58:48 +0100903 fd = path_open_safe(path);
Franck Bui54946022018-04-26 15:45:17 +0200904 if (fd < 0)
Franck Buiaddc3e32018-03-20 08:58:48 +0100905 return fd;
Michal Sekletar780e2ee2017-08-31 12:45:25 +0200906
Lennart Poettering9d874ae2018-08-06 15:40:16 +0200907 return fd_set_perms(i, fd, path, NULL);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100908}
909
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200910static int parse_xattrs_from_arg(Item *i) {
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100911 const char *p;
912 int r;
913
914 assert(i);
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -0500915 assert(i->argument);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100916
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100917 p = i->argument;
918
Lennart Poettering4034a062015-03-23 18:55:36 +0700919 for (;;) {
Franck Bui4cef1922017-11-16 11:27:29 +0100920 _cleanup_free_ char *name = NULL, *value = NULL, *xattr = NULL;
Lennart Poettering4034a062015-03-23 18:55:36 +0700921
Zbigniew Jędrzejewski-Szmek4ec85142019-06-28 11:15:05 +0200922 r = extract_first_word(&p, &xattr, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE);
Lennart Poettering4034a062015-03-23 18:55:36 +0700923 if (r < 0)
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200924 log_warning_errno(r, "Failed to parse extended attribute '%s', ignoring: %m", p);
Lennart Poettering4034a062015-03-23 18:55:36 +0700925 if (r <= 0)
926 break;
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -0500927
Franck Bui4cef1922017-11-16 11:27:29 +0100928 r = split_pair(xattr, "=", &name, &value);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100929 if (r < 0) {
Lennart Poettering4034a062015-03-23 18:55:36 +0700930 log_warning_errno(r, "Failed to parse extended attribute, ignoring: %s", xattr);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100931 continue;
932 }
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -0500933
Lennart Poettering4034a062015-03-23 18:55:36 +0700934 if (isempty(name) || isempty(value)) {
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200935 log_warning("Malformed extended attribute found, ignoring: %s", xattr);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100936 continue;
937 }
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -0500938
Lennart Poettering4034a062015-03-23 18:55:36 +0700939 if (strv_push_pair(&i->xattrs, name, value) < 0)
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100940 return log_oom();
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -0500941
Lennart Poettering4034a062015-03-23 18:55:36 +0700942 name = value = NULL;
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100943 }
944
Lennart Poettering4034a062015-03-23 18:55:36 +0700945 return 0;
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100946}
947
Lennart Poettering9d874ae2018-08-06 15:40:16 +0200948static int fd_set_xattrs(Item *i, int fd, const char *path, const struct stat *st) {
Lennart Poettering34d26772018-03-23 15:41:33 +0100949 char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100950 char **name, **value;
951
952 assert(i);
Franck Bui936f6bd2018-03-02 17:19:32 +0100953 assert(fd);
Franck Bui4dc7bfd2018-08-20 17:23:12 +0200954 assert(path);
Franck Bui936f6bd2018-03-02 17:19:32 +0100955
956 xsprintf(procfs_path, "/proc/self/fd/%i", fd);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100957
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100958 STRV_FOREACH_PAIR(name, value, i->xattrs) {
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200959 log_debug("Setting extended attribute '%s=%s' on %s.", *name, *value, path);
Franck Bui936f6bd2018-03-02 17:19:32 +0100960 if (setxattr(procfs_path, *name, *value, strlen(*value), 0) < 0)
Zbigniew Jędrzejewski-Szmek25f027c2017-05-13 11:26:55 -0400961 return log_error_errno(errno, "Setting extended attribute %s=%s on %s failed: %m",
962 *name, *value, path);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100963 }
964 return 0;
965}
966
Franck Bui936f6bd2018-03-02 17:19:32 +0100967static int path_set_xattrs(Item *i, const char *path) {
968 _cleanup_close_ int fd = -1;
969
970 assert(i);
971 assert(path);
972
Franck Buiaddc3e32018-03-20 08:58:48 +0100973 fd = path_open_safe(path);
Franck Bui936f6bd2018-03-02 17:19:32 +0100974 if (fd < 0)
Franck Buiaddc3e32018-03-20 08:58:48 +0100975 return fd;
Franck Bui936f6bd2018-03-02 17:19:32 +0100976
Lennart Poettering9d874ae2018-08-06 15:40:16 +0200977 return fd_set_xattrs(i, fd, path, NULL);
Franck Bui936f6bd2018-03-02 17:19:32 +0100978}
979
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200980static int parse_acls_from_arg(Item *item) {
Zbigniew Jędrzejewski-Szmek349cc4a2017-10-03 10:41:51 +0200981#if HAVE_ACL
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500982 int r;
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500983
984 assert(item);
985
Zach Smithc55ac242019-10-03 18:09:35 -0700986 /* If append_or_force (= modify) is set, we will not modify the acl
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -0500987 * afterwards, so the mask can be added now if necessary. */
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200988
Zach Smithc55ac242019-10-03 18:09:35 -0700989 r = parse_acl(item->argument, &item->acl_access, &item->acl_default, !item->append_or_force);
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500990 if (r < 0)
Lennart Poettering4034a062015-03-23 18:55:36 +0700991 log_warning_errno(r, "Failed to parse ACL \"%s\": %m. Ignoring", item->argument);
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500992#else
Zbigniew Jędrzejewski-Szmek886cf312018-11-21 08:23:20 +0100993 log_warning_errno(SYNTHETIC_ERRNO(ENOSYS), "ACLs are not supported. Ignoring");
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500994#endif
995
996 return 0;
997}
998
Zbigniew Jędrzejewski-Szmek349cc4a2017-10-03 10:41:51 +0200999#if HAVE_ACL
Lennart Poettering48b8aaa2015-04-13 15:16:54 +02001000static int path_set_acl(const char *path, const char *pretty, acl_type_t type, acl_t acl, bool modify) {
1001 _cleanup_(acl_free_charpp) char *t = NULL;
Zbigniew Jędrzejewski-Szmekdd4105b2015-01-18 18:22:27 -05001002 _cleanup_(acl_freep) acl_t dup = NULL;
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -05001003 int r;
1004
Hans-Peter Deifeld873e872015-03-03 00:35:08 +01001005 /* Returns 0 for success, positive error if already warned,
1006 * negative error otherwise. */
1007
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -05001008 if (modify) {
Zbigniew Jędrzejewski-Szmekdd4105b2015-01-18 18:22:27 -05001009 r = acls_for_file(path, type, acl, &dup);
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -05001010 if (r < 0)
1011 return r;
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -05001012
Zbigniew Jędrzejewski-Szmekdd4105b2015-01-18 18:22:27 -05001013 r = calc_acl_mask_if_needed(&dup);
1014 if (r < 0)
1015 return r;
1016 } else {
1017 dup = acl_dup(acl);
1018 if (!dup)
1019 return -errno;
1020
1021 /* the mask was already added earlier if needed */
1022 }
1023
1024 r = add_base_acls_if_needed(&dup, path);
1025 if (r < 0)
1026 return r;
1027
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001028 t = acl_to_any_text(dup, NULL, ',', TEXT_ABBREVIATE);
Lennart Poettering48b8aaa2015-04-13 15:16:54 +02001029 log_debug("Setting %s ACL %s on %s.",
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001030 type == ACL_TYPE_ACCESS ? "access" : "default",
Lennart Poettering48b8aaa2015-04-13 15:16:54 +02001031 strna(t), pretty);
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001032
Zbigniew Jędrzejewski-Szmekdd4105b2015-01-18 18:22:27 -05001033 r = acl_set_file(path, type, dup);
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001034 if (r < 0)
Lennart Poettering3ea40b72015-04-10 14:44:52 +02001035 /* Return positive to indicate we already warned */
1036 return -log_error_errno(errno,
1037 "Setting %s ACL \"%s\" on %s failed: %m",
1038 type == ACL_TYPE_ACCESS ? "access" : "default",
Lennart Poettering48b8aaa2015-04-13 15:16:54 +02001039 strna(t), pretty);
Hans-Peter Deifeld873e872015-03-03 00:35:08 +01001040
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001041 return 0;
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -05001042}
Zbigniew Jędrzejewski-Szmek35888b62015-02-02 20:28:39 -05001043#endif
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -05001044
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001045static int fd_set_acls(Item *item, int fd, const char *path, const struct stat *st) {
Hans-Peter Deifeld873e872015-03-03 00:35:08 +01001046 int r = 0;
Zbigniew Jędrzejewski-Szmek349cc4a2017-10-03 10:41:51 +02001047#if HAVE_ACL
Lennart Poettering34d26772018-03-23 15:41:33 +01001048 char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
Franck Buib206ac82018-04-13 11:39:39 +02001049 struct stat stbuf;
Franck Bui936f6bd2018-03-02 17:19:32 +01001050
1051 assert(item);
1052 assert(fd);
Franck Bui4dc7bfd2018-08-20 17:23:12 +02001053 assert(path);
Franck Bui936f6bd2018-03-02 17:19:32 +01001054
Franck Buib206ac82018-04-13 11:39:39 +02001055 if (!st) {
1056 if (fstat(fd, &stbuf) < 0)
1057 return log_error_errno(errno, "fstat(%s) failed: %m", path);
1058 st = &stbuf;
1059 }
1060
Zbigniew Jędrzejewski-Szmekbaaa35a2018-11-20 23:40:44 +01001061 if (hardlink_vulnerable(st))
1062 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
1063 "Refusing to set ACLs on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.",
1064 path);
Franck Bui936f6bd2018-03-02 17:19:32 +01001065
1066 if (S_ISLNK(st->st_mode)) {
1067 log_debug("Skipping ACL fix for symlink %s.", path);
1068 return 0;
1069 }
1070
1071 xsprintf(procfs_path, "/proc/self/fd/%i", fd);
1072
1073 if (item->acl_access)
Zach Smithc55ac242019-10-03 18:09:35 -07001074 r = path_set_acl(procfs_path, path, ACL_TYPE_ACCESS, item->acl_access, item->append_or_force);
Franck Bui936f6bd2018-03-02 17:19:32 +01001075
remueller26673852018-07-16 16:56:01 +02001076 /* set only default acls to folders */
1077 if (r == 0 && item->acl_default && S_ISDIR(st->st_mode))
Zach Smithc55ac242019-10-03 18:09:35 -07001078 r = path_set_acl(procfs_path, path, ACL_TYPE_DEFAULT, item->acl_default, item->append_or_force);
Franck Bui936f6bd2018-03-02 17:19:32 +01001079
1080 if (r > 0)
1081 return -r; /* already warned */
Lennart Poetteringf8606622020-04-23 14:52:10 +02001082
1083 /* The above procfs paths don't work if /proc is not mounted. */
1084 if (r == -ENOENT && proc_mounted() == 0)
1085 r = -ENOSYS;
1086
Franck Bui936f6bd2018-03-02 17:19:32 +01001087 if (r == -EOPNOTSUPP) {
1088 log_debug_errno(r, "ACLs not supported by file system at %s", path);
1089 return 0;
1090 }
1091 if (r < 0)
1092 return log_error_errno(r, "ACL operation on \"%s\" failed: %m", path);
1093#endif
1094 return r;
1095}
1096
1097static int path_set_acls(Item *item, const char *path) {
1098 int r = 0;
Yu Watanabe545cdb92018-06-29 16:26:49 +09001099#if HAVE_ACL
Lennart Poettering48b8aaa2015-04-13 15:16:54 +02001100 _cleanup_close_ int fd = -1;
Lennart Poettering48b8aaa2015-04-13 15:16:54 +02001101
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05001102 assert(item);
1103 assert(path);
1104
Franck Buiaddc3e32018-03-20 08:58:48 +01001105 fd = path_open_safe(path);
Lennart Poettering48b8aaa2015-04-13 15:16:54 +02001106 if (fd < 0)
Franck Buiaddc3e32018-03-20 08:58:48 +01001107 return fd;
Lennart Poettering48b8aaa2015-04-13 15:16:54 +02001108
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001109 r = fd_set_acls(item, fd, path, NULL);
Yu Watanabe545cdb92018-06-29 16:26:49 +09001110#endif
1111 return r;
1112}
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05001113
Lennart Poetteringbd550f72015-04-10 16:03:24 +02001114static int parse_attribute_from_arg(Item *item) {
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001115
1116 static const struct {
1117 char character;
1118 unsigned value;
1119 } attributes[] = {
1120 { 'A', FS_NOATIME_FL }, /* do not update atime */
1121 { 'S', FS_SYNC_FL }, /* Synchronous updates */
1122 { 'D', FS_DIRSYNC_FL }, /* dirsync behaviour (directories only) */
1123 { 'a', FS_APPEND_FL }, /* writes to file may only append */
1124 { 'c', FS_COMPR_FL }, /* Compress file */
1125 { 'd', FS_NODUMP_FL }, /* do not dump file */
kpengboy8c35b2c2016-05-29 08:31:14 -07001126 { 'e', FS_EXTENT_FL }, /* Extents */
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001127 { 'i', FS_IMMUTABLE_FL }, /* Immutable file */
1128 { 'j', FS_JOURNAL_DATA_FL }, /* Reserved for ext3 */
1129 { 's', FS_SECRM_FL }, /* Secure deletion */
1130 { 'u', FS_UNRM_FL }, /* Undelete */
1131 { 't', FS_NOTAIL_FL }, /* file tail should not be merged */
AsciiWolf13e785f2017-02-24 18:14:02 +01001132 { 'T', FS_TOPDIR_FL }, /* Top of directory hierarchies */
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001133 { 'C', FS_NOCOW_FL }, /* Do not cow file */
Lennart Poettering75006472019-03-28 18:35:03 +01001134 { 'P', FS_PROJINHERIT_FL }, /* Inherit the quota project ID */
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001135 };
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001136
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001137 enum {
1138 MODE_ADD,
1139 MODE_DEL,
1140 MODE_SET
1141 } mode = MODE_ADD;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001142
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001143 unsigned value = 0, mask = 0;
1144 const char *p;
1145
1146 assert(item);
1147
1148 p = item->argument;
1149 if (p) {
1150 if (*p == '+') {
1151 mode = MODE_ADD;
1152 p++;
1153 } else if (*p == '-') {
1154 mode = MODE_DEL;
1155 p++;
1156 } else if (*p == '=') {
1157 mode = MODE_SET;
1158 p++;
1159 }
1160 }
1161
Zbigniew Jędrzejewski-Szmekbaaa35a2018-11-20 23:40:44 +01001162 if (isempty(p) && mode != MODE_SET)
1163 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1164 "Setting file attribute on '%s' needs an attribute specification.",
1165 item->path);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001166
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001167 for (; p && *p ; p++) {
1168 unsigned i, v;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001169
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001170 for (i = 0; i < ELEMENTSOF(attributes); i++)
1171 if (*p == attributes[i].character)
1172 break;
1173
Zbigniew Jędrzejewski-Szmekbaaa35a2018-11-20 23:40:44 +01001174 if (i >= ELEMENTSOF(attributes))
1175 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1176 "Unknown file attribute '%c' on '%s'.",
1177 *p, item->path);
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001178
1179 v = attributes[i].value;
1180
Andreas Rammhold37420952017-09-29 00:37:23 +02001181 SET_FLAG(value, v, IN_SET(mode, MODE_ADD, MODE_SET));
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001182
1183 mask |= v;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001184 }
1185
1186 if (mode == MODE_SET)
Lennart Poetteringd629ba72019-03-28 18:41:39 +01001187 mask |= CHATTR_ALL_FL;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001188
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001189 assert(mask != 0);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001190
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001191 item->attribute_mask = mask;
1192 item->attribute_value = value;
1193 item->attribute_set = true;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001194
1195 return 0;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001196}
1197
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001198static int fd_set_attribute(Item *item, int fd, const char *path, const struct stat *st) {
Franck Bui936f6bd2018-03-02 17:19:32 +01001199 _cleanup_close_ int procfs_fd = -1;
Franck Buib206ac82018-04-13 11:39:39 +02001200 struct stat stbuf;
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001201 unsigned f;
1202 int r;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001203
Franck Bui4dc7bfd2018-08-20 17:23:12 +02001204 assert(item);
1205 assert(fd);
1206 assert(path);
1207
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001208 if (!item->attribute_set || item->attribute_mask == 0)
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001209 return 0;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001210
Franck Buib206ac82018-04-13 11:39:39 +02001211 if (!st) {
1212 if (fstat(fd, &stbuf) < 0)
1213 return log_error_errno(errno, "fstat(%s) failed: %m", path);
1214 st = &stbuf;
1215 }
1216
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001217 /* Issuing the file attribute ioctls on device nodes is not
1218 * safe, as that will be delivered to the drivers, not the
1219 * file system containing the device node. */
Zbigniew Jędrzejewski-Szmekbaaa35a2018-11-20 23:40:44 +01001220 if (!S_ISREG(st->st_mode) && !S_ISDIR(st->st_mode))
1221 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1222 "Setting file flags is only supported on regular files and directories, cannot set on '%s'.",
1223 path);
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001224
1225 f = item->attribute_value & item->attribute_mask;
1226
1227 /* Mask away directory-specific flags */
Franck Bui936f6bd2018-03-02 17:19:32 +01001228 if (!S_ISDIR(st->st_mode))
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001229 f &= ~FS_DIRSYNC_FL;
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001230
Lennart Poetteringf2324782018-03-26 13:25:51 +02001231 procfs_fd = fd_reopen(fd, O_RDONLY|O_CLOEXEC|O_NOATIME);
Franck Bui936f6bd2018-03-02 17:19:32 +01001232 if (procfs_fd < 0)
Lennart Poetteringc3e03772018-08-06 20:19:52 +02001233 return log_error_errno(procfs_fd, "Failed to re-open '%s': %m", path);
Franck Bui936f6bd2018-03-02 17:19:32 +01001234
Lennart Poetteringdb9a4252018-06-25 20:04:07 +02001235 r = chattr_fd(procfs_fd, f, item->attribute_mask, NULL);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001236 if (r < 0)
Yu Watanabe4c701092017-10-04 23:01:32 +09001237 log_full_errno(IN_SET(r, -ENOTTY, -EOPNOTSUPP) ? LOG_DEBUG : LOG_WARNING,
Lennart Poetteringad75a972015-07-22 22:02:14 +02001238 r,
Lennart Poettering15b45fa2018-08-06 15:44:47 +02001239 "Cannot set file attribute for '%s', value=0x%08x, mask=0x%08x, ignoring: %m",
Lennart Poetteringad75a972015-07-22 22:02:14 +02001240 path, item->attribute_value, item->attribute_mask);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001241
1242 return 0;
1243}
1244
Franck Bui936f6bd2018-03-02 17:19:32 +01001245static int path_set_attribute(Item *item, const char *path) {
1246 _cleanup_close_ int fd = -1;
Franck Bui936f6bd2018-03-02 17:19:32 +01001247
1248 if (!item->attribute_set || item->attribute_mask == 0)
1249 return 0;
1250
Franck Buiaddc3e32018-03-20 08:58:48 +01001251 fd = path_open_safe(path);
Franck Bui936f6bd2018-03-02 17:19:32 +01001252 if (fd < 0)
Franck Buiaddc3e32018-03-20 08:58:48 +01001253 return fd;
Franck Bui936f6bd2018-03-02 17:19:32 +01001254
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001255 return fd_set_attribute(item, fd, path, NULL);
Franck Bui936f6bd2018-03-02 17:19:32 +01001256}
1257
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001258static int write_one_file(Item *i, const char *path) {
Franck Bui551470e2018-04-13 17:54:09 +02001259 _cleanup_close_ int fd = -1, dir_fd = -1;
1260 char *bn;
Zach Smithd0ea5c52019-10-03 18:19:18 -07001261 int flags, r;
Franck Bui31c84ff2018-04-13 09:19:24 +02001262
1263 assert(i);
1264 assert(path);
1265 assert(i->argument);
1266 assert(i->type == WRITE_FILE);
1267
Franck Bui551470e2018-04-13 17:54:09 +02001268 /* Validate the path and keep the fd on the directory for opening the
1269 * file so we're sure that it can't be changed behind our back. */
1270 dir_fd = path_open_parent_safe(path);
1271 if (dir_fd < 0)
1272 return dir_fd;
1273
1274 bn = basename(path);
1275
Zach Smithd0ea5c52019-10-03 18:19:18 -07001276 flags = O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY;
1277
Franck Bui31c84ff2018-04-13 09:19:24 +02001278 /* Follows symlinks */
Zach Smithd0ea5c52019-10-03 18:19:18 -07001279 fd = openat(dir_fd, bn, i->append_or_force ? flags|O_APPEND : flags, i->mode);
Franck Bui31c84ff2018-04-13 09:19:24 +02001280 if (fd < 0) {
1281 if (errno == ENOENT) {
1282 log_debug_errno(errno, "Not writing missing file \"%s\": %m", path);
1283 return 0;
1284 }
1285 return log_error_errno(errno, "Failed to open file \"%s\": %m", path);
1286 }
1287
1288 /* 'w' is allowed to write into any kind of files. */
1289 log_debug("Writing to \"%s\".", path);
1290
1291 r = loop_write(fd, i->argument, strlen(i->argument), false);
1292 if (r < 0)
1293 return log_error_errno(r, "Failed to write file \"%s\": %m", path);
1294
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001295 return fd_set_perms(i, fd, path, NULL);
Franck Bui31c84ff2018-04-13 09:19:24 +02001296}
1297
1298static int create_file(Item *i, const char *path) {
Franck Bui5ec9d062018-04-13 17:31:22 +02001299 _cleanup_close_ int fd = -1, dir_fd = -1;
1300 struct stat stbuf, *st = NULL;
1301 int r = 0;
1302 char *bn;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001303
Lennart Poettering874f1942014-06-10 22:48:56 +02001304 assert(i);
1305 assert(path);
Franck Bui5ec9d062018-04-13 17:31:22 +02001306 assert(i->type == CREATE_FILE);
Lennart Poettering874f1942014-06-10 22:48:56 +02001307
Franck Bui5ec9d062018-04-13 17:31:22 +02001308 /* 'f' operates on regular files exclusively. */
1309
1310 /* Validate the path and keep the fd on the directory for opening the
1311 * file so we're sure that it can't be changed behind our back. */
1312 dir_fd = path_open_parent_safe(path);
1313 if (dir_fd < 0)
1314 return dir_fd;
1315
1316 bn = basename(path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001317
Lennart Poettering43ad6e32014-06-18 00:01:39 +02001318 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +02001319 mac_selinux_create_file_prepare(path, S_IFREG);
Franck Bui5ec9d062018-04-13 17:31:22 +02001320 fd = openat(dir_fd, bn, O_CREAT|O_EXCL|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode);
Lennart Poetteringecabcf82014-10-23 19:41:27 +02001321 mac_selinux_create_file_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +02001322 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001323
1324 if (fd < 0) {
Franck Bui5ec9d062018-04-13 17:31:22 +02001325 /* Even on a read-only filesystem, open(2) returns EEXIST if the
1326 * file already exists. It returns EROFS only if it needs to
1327 * create the file. */
1328 if (errno != EEXIST)
1329 return log_error_errno(errno, "Failed to create file %s: %m", path);
1330
1331 /* Re-open the file. At that point it must exist since open(2)
1332 * failed with EEXIST. We still need to check if the perms/mode
1333 * need to be changed. For read-only filesystems, we let
1334 * fd_set_perms() report the error if the perms need to be
1335 * modified. */
1336 fd = openat(dir_fd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH, i->mode);
1337 if (fd < 0)
1338 return log_error_errno(errno, "Failed to re-open file %s: %m", path);
1339
1340 if (fstat(fd, &stbuf) < 0)
1341 return log_error_errno(errno, "stat(%s) failed: %m", path);
1342
1343 if (!S_ISREG(stbuf.st_mode)) {
1344 log_error("%s exists and is not a regular file.", path);
1345 return -EEXIST;
Lennart Poettering49e87292018-01-24 10:54:10 +01001346 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001347
Franck Bui5ec9d062018-04-13 17:31:22 +02001348 st = &stbuf;
1349 } else {
Michael Olbrichf44b28f2015-04-30 20:50:38 +02001350
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001351 log_debug("\"%s\" has been created.", path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001352
Franck Bui5ec9d062018-04-13 17:31:22 +02001353 if (i->argument) {
1354 log_debug("Writing to \"%s\".", path);
Dave Reisner3612fbc2012-09-12 16:21:00 -04001355
Franck Bui5ec9d062018-04-13 17:31:22 +02001356 r = loop_write(fd, i->argument, strlen(i->argument), false);
1357 if (r < 0)
1358 return log_error_errno(r, "Failed to write file \"%s\": %m", path);
1359 }
1360 }
1361
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001362 return fd_set_perms(i, fd, path, st);
Franck Bui5ec9d062018-04-13 17:31:22 +02001363}
1364
1365static int truncate_file(Item *i, const char *path) {
Franck Bui14ab8042018-04-13 17:50:49 +02001366 _cleanup_close_ int fd = -1, dir_fd = -1;
Franck Bui5ec9d062018-04-13 17:31:22 +02001367 struct stat stbuf, *st = NULL;
1368 bool erofs = false;
1369 int r = 0;
Franck Bui14ab8042018-04-13 17:50:49 +02001370 char *bn;
Franck Bui5ec9d062018-04-13 17:31:22 +02001371
1372 assert(i);
1373 assert(path);
Zach Smitheccebf42019-10-28 09:32:16 -07001374 assert(i->type == TRUNCATE_FILE || (i->type == CREATE_FILE && i->append_or_force));
Franck Bui5ec9d062018-04-13 17:31:22 +02001375
1376 /* We want to operate on regular file exclusively especially since
1377 * O_TRUNC is unspecified if the file is neither a regular file nor a
1378 * fifo nor a terminal device. Therefore we first open the file and make
1379 * sure it's a regular one before truncating it. */
1380
Franck Bui14ab8042018-04-13 17:50:49 +02001381 /* Validate the path and keep the fd on the directory for opening the
1382 * file so we're sure that it can't be changed behind our back. */
1383 dir_fd = path_open_parent_safe(path);
1384 if (dir_fd < 0)
1385 return dir_fd;
1386
1387 bn = basename(path);
1388
Franck Bui5ec9d062018-04-13 17:31:22 +02001389 RUN_WITH_UMASK(0000) {
1390 mac_selinux_create_file_prepare(path, S_IFREG);
Franck Bui14ab8042018-04-13 17:50:49 +02001391 fd = openat(dir_fd, bn, O_CREAT|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode);
Franck Bui5ec9d062018-04-13 17:31:22 +02001392 mac_selinux_create_file_clear();
1393 }
1394
1395 if (fd < 0) {
1396 if (errno != EROFS)
1397 return log_error_errno(errno, "Failed to open/create file %s: %m", path);
1398
1399 /* On a read-only filesystem, we don't want to fail if the
1400 * target is already empty and the perms are set. So we still
1401 * proceed with the sanity checks and let the remaining
1402 * operations fail with EROFS if they try to modify the target
1403 * file. */
1404
Franck Bui14ab8042018-04-13 17:50:49 +02001405 fd = openat(dir_fd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH, i->mode);
Franck Bui5ec9d062018-04-13 17:31:22 +02001406 if (fd < 0) {
1407 if (errno == ENOENT) {
1408 log_error("Cannot create file %s on a read-only file system.", path);
1409 return -EROFS;
1410 }
1411
1412 return log_error_errno(errno, "Failed to re-open file %s: %m", path);
1413 }
1414
1415 erofs = true;
1416 }
1417
1418 if (fstat(fd, &stbuf) < 0)
Michal Schmidt4a62c712014-11-28 19:57:32 +01001419 return log_error_errno(errno, "stat(%s) failed: %m", path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001420
Franck Bui5ec9d062018-04-13 17:31:22 +02001421 if (!S_ISREG(stbuf.st_mode)) {
1422 log_error("%s exists and is not a regular file.", path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001423 return -EEXIST;
1424 }
1425
Franck Bui5ec9d062018-04-13 17:31:22 +02001426 if (stbuf.st_size > 0) {
1427 if (ftruncate(fd, 0) < 0) {
1428 r = erofs ? -EROFS : -errno;
1429 return log_error_errno(r, "Failed to truncate file %s: %m", path);
1430 }
1431 } else
1432 st = &stbuf;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001433
Franck Bui5ec9d062018-04-13 17:31:22 +02001434 log_debug("\"%s\" has been created.", path);
1435
1436 if (i->argument) {
1437 log_debug("Writing to \"%s\".", path);
1438
1439 r = loop_write(fd, i->argument, strlen(i->argument), false);
1440 if (r < 0) {
1441 r = erofs ? -EROFS : r;
1442 return log_error_errno(r, "Failed to write file %s: %m", path);
1443 }
1444 }
1445
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001446 return fd_set_perms(i, fd, path, st);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001447}
1448
Franck Buib1f7b172018-04-12 12:19:22 +02001449static int copy_files(Item *i) {
Franck Bui16ba55a2018-04-12 12:55:22 +02001450 _cleanup_close_ int dfd = -1, fd = -1;
1451 char *bn;
Franck Buib1f7b172018-04-12 12:19:22 +02001452 int r;
1453
1454 log_debug("Copying tree \"%s\" to \"%s\".", i->argument, i->path);
1455
Franck Bui16ba55a2018-04-12 12:55:22 +02001456 bn = basename(i->path);
Franck Buib1f7b172018-04-12 12:19:22 +02001457
Franck Bui16ba55a2018-04-12 12:55:22 +02001458 /* Validate the path and use the returned directory fd for copying the
1459 * target so we're sure that the path can't be changed behind our
1460 * back. */
1461 dfd = path_open_parent_safe(i->path);
1462 if (dfd < 0)
1463 return dfd;
Franck Buib1f7b172018-04-12 12:19:22 +02001464
Franck Bui16ba55a2018-04-12 12:55:22 +02001465 r = copy_tree_at(AT_FDCWD, i->argument,
1466 dfd, bn,
1467 i->uid_set ? i->uid : UID_INVALID,
1468 i->gid_set ? i->gid : GID_INVALID,
Christian Göttsche80e7c842019-11-28 12:18:04 +01001469 COPY_REFLINK | COPY_MERGE_EMPTY | COPY_MAC_CREATE);
Franck Buib1f7b172018-04-12 12:19:22 +02001470 if (r < 0) {
1471 struct stat a, b;
1472
Franck Bui16ba55a2018-04-12 12:55:22 +02001473 /* If the target already exists on read-only filesystems, trying
1474 * to create the target will not fail with EEXIST but with
1475 * EROFS. */
1476 if (r == -EROFS && faccessat(dfd, bn, F_OK, AT_SYMLINK_NOFOLLOW) == 0)
1477 r = -EEXIST;
1478
Franck Buib1f7b172018-04-12 12:19:22 +02001479 if (r != -EEXIST)
1480 return log_error_errno(r, "Failed to copy files to %s: %m", i->path);
1481
1482 if (stat(i->argument, &a) < 0)
1483 return log_error_errno(errno, "stat(%s) failed: %m", i->argument);
1484
Franck Bui16ba55a2018-04-12 12:55:22 +02001485 if (fstatat(dfd, bn, &b, AT_SYMLINK_NOFOLLOW) < 0)
Franck Buib1f7b172018-04-12 12:19:22 +02001486 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
1487
1488 if ((a.st_mode ^ b.st_mode) & S_IFMT) {
1489 log_debug("Can't copy to %s, file exists already and is of different type", i->path);
1490 return 0;
1491 }
1492 }
1493
Franck Bui16ba55a2018-04-12 12:55:22 +02001494 fd = openat(dfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH);
1495 if (fd < 0)
1496 return log_error_errno(errno, "Failed to openat(%s): %m", i->path);
1497
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001498 return fd_set_perms(i, fd, i->path, NULL);
Franck Buib1f7b172018-04-12 12:19:22 +02001499}
1500
Franck Bui074bd732018-04-12 18:10:57 +02001501typedef enum {
1502 CREATION_NORMAL,
1503 CREATION_EXISTING,
1504 CREATION_FORCE,
1505 _CREATION_MODE_MAX,
1506 _CREATION_MODE_INVALID = -1
1507} CreationMode;
1508
Lennart Poetteringb82f71c2019-03-22 19:19:32 +01001509static const char *const creation_mode_verb_table[_CREATION_MODE_MAX] = {
Franck Bui074bd732018-04-12 18:10:57 +02001510 [CREATION_NORMAL] = "Created",
1511 [CREATION_EXISTING] = "Found existing",
1512 [CREATION_FORCE] = "Created replacement",
1513};
1514
1515DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(creation_mode_verb, CreationMode);
1516
Franck Buif17a8d62018-08-06 12:29:54 +02001517static int create_directory_or_subvolume(const char *path, mode_t mode, bool subvol, CreationMode *creation) {
Franck Bui4c39d892018-04-27 11:51:14 +02001518 _cleanup_close_ int pfd = -1;
Franck Buif17a8d62018-08-06 12:29:54 +02001519 CreationMode c;
Franck Bui4c39d892018-04-27 11:51:14 +02001520 int r;
Franck Bui4ad36842018-04-26 14:54:20 +02001521
Franck Bui4c39d892018-04-27 11:51:14 +02001522 assert(path);
Franck Bui4ad36842018-04-26 14:54:20 +02001523
Franck Buif17a8d62018-08-06 12:29:54 +02001524 if (!creation)
1525 creation = &c;
1526
Franck Bui4c39d892018-04-27 11:51:14 +02001527 pfd = path_open_parent_safe(path);
1528 if (pfd < 0)
1529 return pfd;
Franck Bui4ad36842018-04-26 14:54:20 +02001530
Franck Bui4c39d892018-04-27 11:51:14 +02001531 if (subvol) {
Franck Bui4ad36842018-04-26 14:54:20 +02001532 if (btrfs_is_subvol(empty_to_root(arg_root)) <= 0)
1533
1534 /* Don't create a subvolume unless the root directory is
1535 * one, too. We do this under the assumption that if the
1536 * root directory is just a plain directory (i.e. very
1537 * light-weight), we shouldn't try to split it up into
1538 * subvolumes (i.e. more heavy-weight). Thus, chroot()
1539 * environments and suchlike will get a full brtfs
1540 * subvolume set up below their tree only if they
1541 * specifically set up a btrfs subvolume for the root
1542 * dir too. */
1543
Franck Bui4c39d892018-04-27 11:51:14 +02001544 subvol = false;
Franck Bui4ad36842018-04-26 14:54:20 +02001545 else {
Franck Bui4c39d892018-04-27 11:51:14 +02001546 RUN_WITH_UMASK((~mode) & 0777)
1547 r = btrfs_subvol_make_fd(pfd, basename(path));
Franck Bui4ad36842018-04-26 14:54:20 +02001548 }
1549 } else
1550 r = 0;
1551
Franck Bui4c39d892018-04-27 11:51:14 +02001552 if (!subvol || r == -ENOTTY)
Franck Bui4ad36842018-04-26 14:54:20 +02001553 RUN_WITH_UMASK(0000)
Franck Bui4c39d892018-04-27 11:51:14 +02001554 r = mkdirat_label(pfd, basename(path), mode);
Franck Bui4ad36842018-04-26 14:54:20 +02001555
1556 if (r < 0) {
1557 int k;
1558
1559 if (!IN_SET(r, -EEXIST, -EROFS))
Franck Bui4c39d892018-04-27 11:51:14 +02001560 return log_error_errno(r, "Failed to create directory or subvolume \"%s\": %m", path);
Franck Bui4ad36842018-04-26 14:54:20 +02001561
Franck Bui4c39d892018-04-27 11:51:14 +02001562 k = is_dir_fd(pfd);
Franck Bui4ad36842018-04-26 14:54:20 +02001563 if (k == -ENOENT && r == -EROFS)
Franck Bui4c39d892018-04-27 11:51:14 +02001564 return log_error_errno(r, "%s does not exist and cannot be created as the file system is read-only.", path);
Franck Bui4ad36842018-04-26 14:54:20 +02001565 if (k < 0)
Franck Bui4c39d892018-04-27 11:51:14 +02001566 return log_error_errno(k, "Failed to check if %s exists: %m", path);
Franck Bui4ad36842018-04-26 14:54:20 +02001567 if (!k) {
Franck Bui4c39d892018-04-27 11:51:14 +02001568 log_warning("\"%s\" already exists and is not a directory.", path);
1569 return -EEXIST;
Franck Bui4ad36842018-04-26 14:54:20 +02001570 }
1571
Franck Buif17a8d62018-08-06 12:29:54 +02001572 *creation = CREATION_EXISTING;
Franck Bui4ad36842018-04-26 14:54:20 +02001573 } else
Franck Buif17a8d62018-08-06 12:29:54 +02001574 *creation = CREATION_NORMAL;
Franck Bui4ad36842018-04-26 14:54:20 +02001575
Franck Buif17a8d62018-08-06 12:29:54 +02001576 log_debug("%s directory \"%s\".", creation_mode_verb_to_string(*creation), path);
Franck Bui4c39d892018-04-27 11:51:14 +02001577
1578 r = openat(pfd, basename(path), O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
1579 if (r < 0)
Lennart Poettering09f12792018-08-06 15:44:24 +02001580 return log_error_errno(errno, "Failed to open directory '%s': %m", basename(path));
1581
Franck Bui4c39d892018-04-27 11:51:14 +02001582 return r;
1583}
1584
1585static int create_directory(Item *i, const char *path) {
1586 _cleanup_close_ int fd = -1;
1587
1588 assert(i);
1589 assert(IN_SET(i->type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY));
1590
Franck Buif17a8d62018-08-06 12:29:54 +02001591 fd = create_directory_or_subvolume(path, i->mode, false, NULL);
Franck Bui4c39d892018-04-27 11:51:14 +02001592 if (fd == -EEXIST)
1593 return 0;
1594 if (fd < 0)
1595 return fd;
1596
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001597 return fd_set_perms(i, fd, path, NULL);
Franck Bui4c39d892018-04-27 11:51:14 +02001598}
1599
1600static int create_subvolume(Item *i, const char *path) {
1601 _cleanup_close_ int fd = -1;
Franck Buif17a8d62018-08-06 12:29:54 +02001602 CreationMode creation;
Franck Bui4c39d892018-04-27 11:51:14 +02001603 int r, q = 0;
1604
1605 assert(i);
1606 assert(IN_SET(i->type, CREATE_SUBVOLUME, CREATE_SUBVOLUME_NEW_QUOTA, CREATE_SUBVOLUME_INHERIT_QUOTA));
1607
Franck Buif17a8d62018-08-06 12:29:54 +02001608 fd = create_directory_or_subvolume(path, i->mode, true, &creation);
Franck Bui4c39d892018-04-27 11:51:14 +02001609 if (fd == -EEXIST)
1610 return 0;
1611 if (fd < 0)
1612 return fd;
Franck Bui4ad36842018-04-26 14:54:20 +02001613
Franck Buif17a8d62018-08-06 12:29:54 +02001614 if (creation == CREATION_NORMAL &&
1615 IN_SET(i->type, CREATE_SUBVOLUME_NEW_QUOTA, CREATE_SUBVOLUME_INHERIT_QUOTA)) {
Franck Bui4c39d892018-04-27 11:51:14 +02001616 r = btrfs_subvol_auto_qgroup_fd(fd, 0, i->type == CREATE_SUBVOLUME_NEW_QUOTA);
Franck Bui4ad36842018-04-26 14:54:20 +02001617 if (r == -ENOTTY)
1618 log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (unsupported fs or dir not a subvolume): %m", i->path);
1619 else if (r == -EROFS)
1620 log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (fs is read-only).", i->path);
1621 else if (r == -ENOPROTOOPT)
1622 log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (quota support is disabled).", i->path);
1623 else if (r < 0)
1624 q = log_error_errno(r, "Failed to adjust quota for subvolume \"%s\": %m", i->path);
1625 else if (r > 0)
1626 log_debug("Adjusted quota for subvolume \"%s\".", i->path);
1627 else if (r == 0)
1628 log_debug("Quota for subvolume \"%s\" already in place, no change made.", i->path);
1629 }
1630
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001631 r = fd_set_perms(i, fd, path, NULL);
1632 if (q < 0) /* prefer the quota change error from above */
Franck Bui4ad36842018-04-26 14:54:20 +02001633 return q;
1634
1635 return r;
1636}
1637
Franck Bui54946022018-04-26 15:45:17 +02001638static int empty_directory(Item *i, const char *path) {
1639 int r;
1640
1641 assert(i);
1642 assert(i->type == EMPTY_DIRECTORY);
1643
1644 r = is_dir(path, false);
1645 if (r == -ENOENT) {
1646 /* Option "e" operates only on existing objects. Do not
1647 * print errors about non-existent files or directories */
1648 log_debug("Skipping missing directory: %s", path);
1649 return 0;
1650 }
1651 if (r < 0)
1652 return log_error_errno(r, "is_dir() failed on path %s: %m", path);
Zbigniew Jędrzejewski-Szmekbaaa35a2018-11-20 23:40:44 +01001653 if (r == 0)
1654 return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
1655 "'%s' already exists and is not a directory.",
1656 path);
Franck Bui54946022018-04-26 15:45:17 +02001657
1658 return path_set_perms(i, path);
1659}
1660
Franck Bui074bd732018-04-12 18:10:57 +02001661static int create_device(Item *i, mode_t file_type) {
Franck Buic7700a72018-04-12 18:33:54 +02001662 _cleanup_close_ int dfd = -1, fd = -1;
Franck Bui074bd732018-04-12 18:10:57 +02001663 CreationMode creation;
Franck Buic7700a72018-04-12 18:33:54 +02001664 char *bn;
Franck Bui074bd732018-04-12 18:10:57 +02001665 int r;
1666
1667 assert(i);
1668 assert(IN_SET(file_type, S_IFBLK, S_IFCHR));
1669
Franck Buic7700a72018-04-12 18:33:54 +02001670 bn = basename(i->path);
1671
1672 /* Validate the path and use the returned directory fd for copying the
1673 * target so we're sure that the path can't be changed behind our
1674 * back. */
1675 dfd = path_open_parent_safe(i->path);
1676 if (dfd < 0)
1677 return dfd;
1678
Franck Bui074bd732018-04-12 18:10:57 +02001679 RUN_WITH_UMASK(0000) {
1680 mac_selinux_create_file_prepare(i->path, file_type);
Franck Buic7700a72018-04-12 18:33:54 +02001681 r = mknodat(dfd, bn, i->mode | file_type, i->major_minor);
Franck Bui074bd732018-04-12 18:10:57 +02001682 mac_selinux_create_file_clear();
1683 }
1684
1685 if (r < 0) {
Franck Buic7700a72018-04-12 18:33:54 +02001686 struct stat st;
1687
Franck Bui074bd732018-04-12 18:10:57 +02001688 if (errno == EPERM) {
1689 log_debug("We lack permissions, possibly because of cgroup configuration; "
1690 "skipping creation of device node %s.", i->path);
1691 return 0;
1692 }
1693
1694 if (errno != EEXIST)
1695 return log_error_errno(errno, "Failed to create device node %s: %m", i->path);
1696
Franck Buic7700a72018-04-12 18:33:54 +02001697 if (fstatat(dfd, bn, &st, 0) < 0)
Franck Bui074bd732018-04-12 18:10:57 +02001698 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
1699
1700 if ((st.st_mode & S_IFMT) != file_type) {
1701
Zach Smithc55ac242019-10-03 18:09:35 -07001702 if (i->append_or_force) {
Franck Bui074bd732018-04-12 18:10:57 +02001703
1704 RUN_WITH_UMASK(0000) {
1705 mac_selinux_create_file_prepare(i->path, file_type);
Franck Buic7700a72018-04-12 18:33:54 +02001706 /* FIXME: need to introduce mknodat_atomic() */
Franck Bui074bd732018-04-12 18:10:57 +02001707 r = mknod_atomic(i->path, i->mode | file_type, i->major_minor);
1708 mac_selinux_create_file_clear();
1709 }
1710
1711 if (r < 0)
1712 return log_error_errno(r, "Failed to create device node \"%s\": %m", i->path);
1713 creation = CREATION_FORCE;
1714 } else {
1715 log_debug("%s is not a device node.", i->path);
1716 return 0;
1717 }
1718 } else
1719 creation = CREATION_EXISTING;
1720 } else
1721 creation = CREATION_NORMAL;
1722
1723 log_debug("%s %s device node \"%s\" %u:%u.",
1724 creation_mode_verb_to_string(creation),
1725 i->type == CREATE_BLOCK_DEVICE ? "block" : "char",
1726 i->path, major(i->mode), minor(i->mode));
1727
Franck Buic7700a72018-04-12 18:33:54 +02001728 fd = openat(dfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH);
1729 if (fd < 0)
1730 return log_error_errno(errno, "Failed to openat(%s): %m", i->path);
1731
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001732 return fd_set_perms(i, fd, i->path, NULL);
Franck Bui074bd732018-04-12 18:10:57 +02001733}
1734
Franck Buia2fc2f82018-04-27 18:11:26 +02001735static int create_fifo(Item *i, const char *path) {
Franck Bui7ea5a872018-04-27 18:17:32 +02001736 _cleanup_close_ int pfd = -1, fd = -1;
Franck Buia2fc2f82018-04-27 18:11:26 +02001737 CreationMode creation;
1738 struct stat st;
Franck Bui7ea5a872018-04-27 18:17:32 +02001739 char *bn;
Franck Buia2fc2f82018-04-27 18:11:26 +02001740 int r;
1741
Franck Bui7ea5a872018-04-27 18:17:32 +02001742 pfd = path_open_parent_safe(path);
1743 if (pfd < 0)
1744 return pfd;
1745
1746 bn = basename(path);
1747
Franck Buia2fc2f82018-04-27 18:11:26 +02001748 RUN_WITH_UMASK(0000) {
1749 mac_selinux_create_file_prepare(path, S_IFIFO);
Franck Bui7ea5a872018-04-27 18:17:32 +02001750 r = mkfifoat(pfd, bn, i->mode);
Franck Buia2fc2f82018-04-27 18:11:26 +02001751 mac_selinux_create_file_clear();
1752 }
1753
1754 if (r < 0) {
1755 if (errno != EEXIST)
1756 return log_error_errno(errno, "Failed to create fifo %s: %m", path);
1757
Franck Bui7ea5a872018-04-27 18:17:32 +02001758 if (fstatat(pfd, bn, &st, AT_SYMLINK_NOFOLLOW) < 0)
Franck Buia2fc2f82018-04-27 18:11:26 +02001759 return log_error_errno(errno, "stat(%s) failed: %m", path);
1760
1761 if (!S_ISFIFO(st.st_mode)) {
1762
Zach Smithc55ac242019-10-03 18:09:35 -07001763 if (i->append_or_force) {
Franck Buia2fc2f82018-04-27 18:11:26 +02001764 RUN_WITH_UMASK(0000) {
1765 mac_selinux_create_file_prepare(path, S_IFIFO);
Franck Bui7ea5a872018-04-27 18:17:32 +02001766 r = mkfifoat_atomic(pfd, bn, i->mode);
Franck Buia2fc2f82018-04-27 18:11:26 +02001767 mac_selinux_create_file_clear();
1768 }
1769
1770 if (r < 0)
1771 return log_error_errno(r, "Failed to create fifo %s: %m", path);
1772 creation = CREATION_FORCE;
1773 } else {
1774 log_warning("\"%s\" already exists and is not a fifo.", path);
1775 return 0;
1776 }
1777 } else
1778 creation = CREATION_EXISTING;
1779 } else
1780 creation = CREATION_NORMAL;
Franck Bui7ea5a872018-04-27 18:17:32 +02001781
Franck Buia2fc2f82018-04-27 18:11:26 +02001782 log_debug("%s fifo \"%s\".", creation_mode_verb_to_string(creation), path);
1783
Franck Bui7ea5a872018-04-27 18:17:32 +02001784 fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH);
1785 if (fd < 0)
Lennart Poetteringa5df05f2018-08-06 15:46:01 +02001786 return log_error_errno(errno, "Failed to openat(%s): %m", path);
Franck Bui7ea5a872018-04-27 18:17:32 +02001787
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001788 return fd_set_perms(i, fd, i->path, NULL);
Franck Buia2fc2f82018-04-27 18:11:26 +02001789}
1790
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001791typedef int (*action_t)(Item *i, const char *path);
1792typedef int (*fdaction_t)(Item *i, int fd, const char *path, const struct stat *st);
Zbigniew Jędrzejewski-Szmek081043c2015-01-18 01:33:39 -05001793
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001794static int item_do(Item *i, int fd, const char *path, fdaction_t action) {
Franck Bui14f34802018-05-24 14:17:07 +02001795 struct stat st;
Franck Bui936f6bd2018-03-02 17:19:32 +01001796 int r = 0, q;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001797
1798 assert(i);
Franck Bui4dc7bfd2018-08-20 17:23:12 +02001799 assert(path);
Franck Bui936f6bd2018-03-02 17:19:32 +01001800 assert(fd >= 0);
Franck Bui14f34802018-05-24 14:17:07 +02001801
1802 if (fstat(fd, &st) < 0) {
Lennart Poettering09f12792018-08-06 15:44:24 +02001803 r = log_error_errno(errno, "fstat() on file failed: %m");
Franck Bui14f34802018-05-24 14:17:07 +02001804 goto finish;
1805 }
Michal Schmidta8d88782011-12-15 23:11:07 +01001806
1807 /* This returns the first error we run into, but nevertheless
1808 * tries to go on */
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001809 r = action(i, fd, path, &st);
Michal Schmidta8d88782011-12-15 23:11:07 +01001810
Franck Bui14f34802018-05-24 14:17:07 +02001811 if (S_ISDIR(st.st_mode)) {
Zbigniew Jędrzejewski-Szmek22dd8d32018-05-11 11:09:37 +02001812 char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
Franck Bui936f6bd2018-03-02 17:19:32 +01001813 _cleanup_closedir_ DIR *d = NULL;
1814 struct dirent *de;
Michal Schmidta8d88782011-12-15 23:11:07 +01001815
Franck Bui936f6bd2018-03-02 17:19:32 +01001816 /* The passed 'fd' was opened with O_PATH. We need to convert
1817 * it into a 'regular' fd before reading the directory content. */
1818 xsprintf(procfs_path, "/proc/self/fd/%i", fd);
Michal Schmidta8d88782011-12-15 23:11:07 +01001819
Franck Bui936f6bd2018-03-02 17:19:32 +01001820 d = opendir(procfs_path);
1821 if (!d) {
Lennart Poettering09f12792018-08-06 15:44:24 +02001822 log_error_errno(errno, "Failed to opendir() '%s': %m", procfs_path);
1823 if (r == 0)
1824 r = -errno;
Franck Bui936f6bd2018-03-02 17:19:32 +01001825 goto finish;
1826 }
Michal Schmidta8d88782011-12-15 23:11:07 +01001827
Franck Bui936f6bd2018-03-02 17:19:32 +01001828 FOREACH_DIRENT_ALL(de, d, q = -errno; goto finish) {
Franck Bui936f6bd2018-03-02 17:19:32 +01001829 int de_fd;
Michal Schmidta8d88782011-12-15 23:11:07 +01001830
Franck Bui936f6bd2018-03-02 17:19:32 +01001831 if (dot_or_dot_dot(de->d_name))
1832 continue;
Michal Schmidta8d88782011-12-15 23:11:07 +01001833
Franck Bui936f6bd2018-03-02 17:19:32 +01001834 de_fd = openat(fd, de->d_name, O_NOFOLLOW|O_CLOEXEC|O_PATH);
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001835 if (de_fd < 0)
1836 q = log_error_errno(errno, "Failed to open() file '%s': %m", de->d_name);
Franck Bui4dc7bfd2018-08-20 17:23:12 +02001837 else {
1838 _cleanup_free_ char *de_path = NULL;
1839
Zbigniew Jędrzejewski-Szmek62a85ee2018-11-30 10:43:57 +01001840 de_path = path_join(path, de->d_name);
Franck Bui4dc7bfd2018-08-20 17:23:12 +02001841 if (!de_path)
1842 q = log_oom();
1843 else
1844 /* Pass ownership of dirent fd over */
1845 q = item_do(i, de_fd, de_path, action);
1846 }
Franck Bui936f6bd2018-03-02 17:19:32 +01001847
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001848 if (q < 0 && r == 0)
1849 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +01001850 }
Michal Schmidta8d88782011-12-15 23:11:07 +01001851 }
Franck Bui936f6bd2018-03-02 17:19:32 +01001852finish:
1853 safe_close(fd);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001854 return r;
Michal Schmidta8d88782011-12-15 23:11:07 +01001855}
1856
Franck Bui936f6bd2018-03-02 17:19:32 +01001857static int glob_item(Item *i, action_t action) {
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -05001858 _cleanup_globfree_ glob_t g = {
Zbigniew Jędrzejewski-Szmekebf31a12015-01-26 10:39:03 -05001859 .gl_opendir = (void *(*)(const char *)) opendir_nomod,
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -05001860 };
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001861 int r = 0, k;
Michal Schmidt99e68c02011-12-15 23:45:26 +01001862 char **fn;
1863
Zbigniew Jędrzejewski-Szmek84e72b52017-04-25 23:50:35 -04001864 k = safe_glob(i->path, GLOB_NOSORT|GLOB_BRACE, &g);
1865 if (k < 0 && k != -ENOENT)
1866 return log_error_errno(k, "glob(%s) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -04001867
1868 STRV_FOREACH(fn, g.gl_pathv) {
1869 k = action(i, *fn);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001870 if (k < 0 && r == 0)
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -04001871 r = k;
Franck Bui936f6bd2018-03-02 17:19:32 +01001872 }
Zbigniew Jędrzejewski-Szmek081043c2015-01-18 01:33:39 -05001873
Franck Bui936f6bd2018-03-02 17:19:32 +01001874 return r;
1875}
1876
1877static int glob_item_recursively(Item *i, fdaction_t action) {
1878 _cleanup_globfree_ glob_t g = {
1879 .gl_opendir = (void *(*)(const char *)) opendir_nomod,
1880 };
1881 int r = 0, k;
1882 char **fn;
1883
1884 k = safe_glob(i->path, GLOB_NOSORT|GLOB_BRACE, &g);
1885 if (k < 0 && k != -ENOENT)
1886 return log_error_errno(k, "glob(%s) failed: %m", i->path);
1887
1888 STRV_FOREACH(fn, g.gl_pathv) {
1889 _cleanup_close_ int fd = -1;
Franck Bui936f6bd2018-03-02 17:19:32 +01001890
1891 /* Make sure we won't trigger/follow file object (such as
1892 * device nodes, automounts, ...) pointed out by 'fn' with
1893 * O_PATH. Note, when O_PATH is used, flags other than
1894 * O_CLOEXEC, O_DIRECTORY, and O_NOFOLLOW are ignored. */
1895
1896 fd = open(*fn, O_CLOEXEC|O_NOFOLLOW|O_PATH);
1897 if (fd < 0) {
Lennart Poettering09f12792018-08-06 15:44:24 +02001898 log_error_errno(errno, "Opening '%s' failed: %m", *fn);
1899 if (r == 0)
1900 r = -errno;
Franck Bui936f6bd2018-03-02 17:19:32 +01001901 continue;
Zbigniew Jędrzejewski-Szmek081043c2015-01-18 01:33:39 -05001902 }
Franck Bui936f6bd2018-03-02 17:19:32 +01001903
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001904 k = item_do(i, fd, *fn, action);
Franck Bui936f6bd2018-03-02 17:19:32 +01001905 if (k < 0 && r == 0)
1906 r = k;
1907
1908 /* we passed fd ownership to the previous call */
1909 fd = -1;
Michal Schmidt99e68c02011-12-15 23:45:26 +01001910 }
1911
Michal Schmidt99e68c02011-12-15 23:45:26 +01001912 return r;
1913}
1914
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001915static int create_item(Item *i) {
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05001916 CreationMode creation;
Franck Buia2fc2f82018-04-27 18:11:26 +02001917 int r = 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001918
1919 assert(i);
1920
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001921 log_debug("Running create action for entry %c %s", (char) i->type, i->path);
1922
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001923 switch (i->type) {
1924
1925 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001926 case IGNORE_DIRECTORY_PATH:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001927 case REMOVE_PATH:
1928 case RECURSIVE_REMOVE_PATH:
1929 return 0;
1930
Zach Smitheccebf42019-10-28 09:32:16 -07001931 case TRUNCATE_FILE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001932 case CREATE_FILE:
Lennart Poettering7fa10742018-01-23 14:14:19 +01001933 RUN_WITH_UMASK(0000)
1934 (void) mkdir_parents_label(i->path, 0755);
1935
Zach Smitheccebf42019-10-28 09:32:16 -07001936 if ((i->type == CREATE_FILE && i->append_or_force) || i->type == TRUNCATE_FILE)
1937 r = truncate_file(i, i->path);
1938 else
1939 r = create_file(i, i->path);
Lennart Poettering265ffa12013-09-17 16:33:30 -05001940
Franck Bui5ec9d062018-04-13 17:31:22 +02001941 if (r < 0)
1942 return r;
1943 break;
1944
Franck Bui7b887f22018-04-12 12:18:19 +02001945 case COPY_FILES:
Lennart Poettering7fa10742018-01-23 14:14:19 +01001946 RUN_WITH_UMASK(0000)
1947 (void) mkdir_parents_label(i->path, 0755);
1948
Franck Buib1f7b172018-04-12 12:19:22 +02001949 r = copy_files(i);
Lennart Poettering849958d2014-06-10 23:02:40 +02001950 if (r < 0)
1951 return r;
Lennart Poettering849958d2014-06-10 23:02:40 +02001952 break;
1953
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001954 case WRITE_FILE:
Franck Bui936f6bd2018-03-02 17:19:32 +01001955 r = glob_item(i, write_one_file);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +01001956 if (r < 0)
1957 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001958
1959 break;
1960
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001961 case CREATE_DIRECTORY:
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001962 case TRUNCATE_DIRECTORY:
Franck Bui4c39d892018-04-27 11:51:14 +02001963 RUN_WITH_UMASK(0000)
1964 (void) mkdir_parents_label(i->path, 0755);
1965
1966 r = create_directory(i, i->path);
1967 if (r < 0)
1968 return r;
1969 break;
1970
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001971 case CREATE_SUBVOLUME:
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02001972 case CREATE_SUBVOLUME_INHERIT_QUOTA:
1973 case CREATE_SUBVOLUME_NEW_QUOTA:
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001974 RUN_WITH_UMASK(0000)
Lennart Poettering7fa10742018-01-23 14:14:19 +01001975 (void) mkdir_parents_label(i->path, 0755);
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001976
Franck Bui4c39d892018-04-27 11:51:14 +02001977 r = create_subvolume(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +01001978 if (r < 0)
1979 return r;
Franck Bui4ad36842018-04-26 14:54:20 +02001980 break;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001981
Franck Bui4ad36842018-04-26 14:54:20 +02001982 case EMPTY_DIRECTORY:
Franck Bui54946022018-04-26 15:45:17 +02001983 r = glob_item(i, empty_directory);
Franck Bui4ad36842018-04-26 14:54:20 +02001984 if (r < 0)
1985 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001986 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +02001987
1988 case CREATE_FIFO:
Franck Buia2fc2f82018-04-27 18:11:26 +02001989 RUN_WITH_UMASK(0000)
Lennart Poettering7fa10742018-01-23 14:14:19 +01001990 (void) mkdir_parents_label(i->path, 0755);
1991
Franck Buia2fc2f82018-04-27 18:11:26 +02001992 r = create_fifo(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +01001993 if (r < 0)
1994 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +02001995 break;
Michal Schmidta8d88782011-12-15 23:11:07 +01001996
Lennart Poettering5c5ccf12015-04-10 18:07:04 +02001997 case CREATE_SYMLINK: {
Lennart Poettering7fa10742018-01-23 14:14:19 +01001998 RUN_WITH_UMASK(0000)
1999 (void) mkdir_parents_label(i->path, 0755);
2000
Lennart Poetteringecabcf82014-10-23 19:41:27 +02002001 mac_selinux_create_file_prepare(i->path, S_IFLNK);
Franck Bui4cef1922017-11-16 11:27:29 +01002002 r = symlink(i->argument, i->path);
Lennart Poetteringecabcf82014-10-23 19:41:27 +02002003 mac_selinux_create_file_clear();
Kay Sieverse9a5ef72012-04-17 16:05:03 +02002004
Lennart Poettering468d7262012-01-17 15:04:12 +01002005 if (r < 0) {
Lennart Poettering2e78fa72014-06-16 13:21:07 +02002006 _cleanup_free_ char *x = NULL;
Lennart Poettering468d7262012-01-17 15:04:12 +01002007
Michal Schmidt4a62c712014-11-28 19:57:32 +01002008 if (errno != EEXIST)
Franck Bui4cef1922017-11-16 11:27:29 +01002009 return log_error_errno(errno, "symlink(%s, %s) failed: %m", i->argument, i->path);
Lennart Poettering2e78fa72014-06-16 13:21:07 +02002010
2011 r = readlink_malloc(i->path, &x);
Franck Bui4cef1922017-11-16 11:27:29 +01002012 if (r < 0 || !streq(i->argument, x)) {
Lennart Poettering2e78fa72014-06-16 13:21:07 +02002013
Zach Smithc55ac242019-10-03 18:09:35 -07002014 if (i->append_or_force) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +02002015 mac_selinux_create_file_prepare(i->path, S_IFLNK);
Franck Bui4cef1922017-11-16 11:27:29 +01002016 r = symlink_atomic(i->argument, i->path);
Lennart Poetteringecabcf82014-10-23 19:41:27 +02002017 mac_selinux_create_file_clear();
Lennart Poettering2e78fa72014-06-16 13:21:07 +02002018
Ricardo Salveti de Araujo2ef5de12018-04-03 10:05:11 -03002019 if (IN_SET(r, -EISDIR, -EEXIST, -ENOTEMPTY)) {
William Douglasb3f58972017-08-09 08:53:03 -07002020 r = rm_rf(i->path, REMOVE_ROOT|REMOVE_PHYSICAL);
2021 if (r < 0)
2022 return log_error_errno(r, "rm -fr %s failed: %m", i->path);
2023
2024 mac_selinux_create_file_prepare(i->path, S_IFLNK);
Franck Bui4cef1922017-11-16 11:27:29 +01002025 r = symlink(i->argument, i->path) < 0 ? -errno : 0;
William Douglasb3f58972017-08-09 08:53:03 -07002026 mac_selinux_create_file_clear();
2027 }
Michal Schmidtf6479622014-11-28 18:50:43 +01002028 if (r < 0)
Franck Bui4cef1922017-11-16 11:27:29 +01002029 return log_error_errno(r, "symlink(%s, %s) failed: %m", i->argument, i->path);
Lennart Poetteringa542c4d2015-05-15 21:48:20 +02002030
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05002031 creation = CREATION_FORCE;
Lennart Poettering1554afa2014-06-17 23:50:22 +02002032 } else {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002033 log_debug("\"%s\" is not a symlink or does not point to the correct path.", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +02002034 return 0;
2035 }
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05002036 } else
2037 creation = CREATION_EXISTING;
2038 } else
Lennart Poetteringa542c4d2015-05-15 21:48:20 +02002039
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05002040 creation = CREATION_NORMAL;
Lennart Poettering7a7d5db2015-02-02 21:34:09 +01002041 log_debug("%s symlink \"%s\".", creation_mode_verb_to_string(creation), i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +01002042 break;
Lennart Poettering5c5ccf12015-04-10 18:07:04 +02002043 }
Lennart Poettering468d7262012-01-17 15:04:12 +01002044
2045 case CREATE_BLOCK_DEVICE:
Franck Bui074bd732018-04-12 18:10:57 +02002046 case CREATE_CHAR_DEVICE:
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -07002047 if (have_effective_cap(CAP_MKNOD) == 0) {
Lennart Poettering713998c2018-08-06 15:46:32 +02002048 /* In a container we lack CAP_MKNOD. We shouldn't attempt to create the device node in that
2049 * case to avoid noise, and we don't support virtualized devices in containers anyway. */
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -07002050
2051 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
2052 return 0;
2053 }
2054
Lennart Poettering7fa10742018-01-23 14:14:19 +01002055 RUN_WITH_UMASK(0000)
2056 (void) mkdir_parents_label(i->path, 0755);
2057
Franck Bui074bd732018-04-12 18:10:57 +02002058 r = create_device(i, i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +01002059 if (r < 0)
2060 return r;
2061
2062 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01002063
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02002064 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01002065 case RELABEL_PATH:
Franck Bui936f6bd2018-03-02 17:19:32 +01002066 r = glob_item(i, path_set_perms);
Michal Schmidt777b87e2011-12-16 18:27:35 +01002067 if (r < 0)
Lennart Poettering96ca8192013-06-21 15:57:42 +02002068 return r;
Michal Schmidt777b87e2011-12-16 18:27:35 +01002069 break;
2070
Michal Schmidta8d88782011-12-15 23:11:07 +01002071 case RECURSIVE_RELABEL_PATH:
Franck Bui936f6bd2018-03-02 17:19:32 +01002072 r = glob_item_recursively(i, fd_set_perms);
Michal Schmidta8d88782011-12-15 23:11:07 +01002073 if (r < 0)
2074 return r;
Maciej Wereskiebf4e802014-12-04 10:32:10 +01002075 break;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02002076
Maciej Wereskiebf4e802014-12-04 10:32:10 +01002077 case SET_XATTR:
Franck Bui936f6bd2018-03-02 17:19:32 +01002078 r = glob_item(i, path_set_xattrs);
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05002079 if (r < 0)
2080 return r;
2081 break;
2082
2083 case RECURSIVE_SET_XATTR:
Franck Bui936f6bd2018-03-02 17:19:32 +01002084 r = glob_item_recursively(i, fd_set_xattrs);
Maciej Wereskiebf4e802014-12-04 10:32:10 +01002085 if (r < 0)
2086 return r;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02002087 break;
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05002088
2089 case SET_ACL:
Franck Bui936f6bd2018-03-02 17:19:32 +01002090 r = glob_item(i, path_set_acls);
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05002091 if (r < 0)
2092 return r;
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05002093 break;
2094
2095 case RECURSIVE_SET_ACL:
Franck Bui936f6bd2018-03-02 17:19:32 +01002096 r = glob_item_recursively(i, fd_set_acls);
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05002097 if (r < 0)
2098 return r;
2099 break;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01002100
Lennart Poettering88ec4df2015-04-08 22:35:52 +02002101 case SET_ATTRIBUTE:
Franck Bui936f6bd2018-03-02 17:19:32 +01002102 r = glob_item(i, path_set_attribute);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01002103 if (r < 0)
2104 return r;
2105 break;
2106
Lennart Poettering88ec4df2015-04-08 22:35:52 +02002107 case RECURSIVE_SET_ATTRIBUTE:
Franck Bui936f6bd2018-03-02 17:19:32 +01002108 r = glob_item_recursively(i, fd_set_attribute);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01002109 if (r < 0)
2110 return r;
2111 break;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002112 }
2113
Michal Schmidtf05bc3f2011-12-15 23:44:23 +01002114 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002115}
2116
Michal Schmidta0896122011-12-15 21:32:50 +01002117static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002118 int r;
2119
2120 assert(i);
2121
2122 switch (i->type) {
2123
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002124 case REMOVE_PATH:
Michal Schmidt4a62c712014-11-28 19:57:32 +01002125 if (remove(instance) < 0 && errno != ENOENT)
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002126 return log_error_errno(errno, "rm(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002127
2128 break;
2129
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002130 case RECURSIVE_REMOVE_PATH:
Lennart Poettering2c575972018-10-29 19:24:56 +01002131 /* FIXME: we probably should use dir_cleanup() here instead of rm_rf() so that 'x' is honoured. */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002132 log_debug("rm -rf \"%s\"", instance);
Lennart Poettering2c575972018-10-29 19:24:56 +01002133 r = rm_rf(instance, REMOVE_ROOT|REMOVE_SUBVOLUME|REMOVE_PHYSICAL);
Michal Schmidtf6479622014-11-28 18:50:43 +01002134 if (r < 0 && r != -ENOENT)
2135 return log_error_errno(r, "rm_rf(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002136
2137 break;
Zbigniew Jędrzejewski-Szmek7fcb4b92015-01-22 23:35:34 -05002138
2139 default:
2140 assert_not_reached("wut?");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002141 }
2142
2143 return 0;
2144}
2145
Michal Schmidta0896122011-12-15 21:32:50 +01002146static int remove_item(Item *i) {
Lennart Poettering2c575972018-10-29 19:24:56 +01002147 int r;
2148
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01002149 assert(i);
2150
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002151 log_debug("Running remove action for entry %c %s", (char) i->type, i->path);
2152
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01002153 switch (i->type) {
2154
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01002155 case TRUNCATE_DIRECTORY:
Lennart Poettering2c575972018-10-29 19:24:56 +01002156 /* FIXME: we probably should use dir_cleanup() here instead of rm_rf() so that 'x' is honoured. */
2157 log_debug("rm -rf \"%s\"", i->path);
2158 r = rm_rf(i->path, REMOVE_PHYSICAL);
2159 if (r < 0 && r != -ENOENT)
2160 return log_error_errno(r, "rm_rf(%s): %m", i->path);
2161
2162 return 0;
2163
2164 case REMOVE_PATH:
Michal Schmidt99e68c02011-12-15 23:45:26 +01002165 case RECURSIVE_REMOVE_PATH:
Franck Bui936f6bd2018-03-02 17:19:32 +01002166 return glob_item(i, remove_item_instance);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01002167
Zbigniew Jędrzejewski-Szmekdf8dee82016-04-20 00:06:25 -04002168 default:
2169 return 0;
2170 }
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01002171}
2172
Michal Sekletar78a92a52013-01-18 16:13:08 +01002173static int clean_item_instance(Item *i, const char* instance) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +02002174 _cleanup_closedir_ DIR *d = NULL;
Michal Sekletar78a92a52013-01-18 16:13:08 +01002175 struct stat s, ps;
2176 bool mountpoint;
Michal Sekletar78a92a52013-01-18 16:13:08 +01002177 usec_t cutoff, n;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002178 char timestamp[FORMAT_TIMESTAMP_MAX];
Michal Sekletar78a92a52013-01-18 16:13:08 +01002179
2180 assert(i);
2181
2182 if (!i->age_set)
2183 return 0;
2184
2185 n = now(CLOCK_REALTIME);
2186 if (n < i->age)
2187 return 0;
2188
2189 cutoff = n - i->age;
2190
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -05002191 d = opendir_nomod(instance);
Michal Sekletar78a92a52013-01-18 16:13:08 +01002192 if (!d) {
Zbigniew Jędrzejewski-Szmekd710aaf2016-07-22 20:27:45 -04002193 if (IN_SET(errno, ENOENT, ENOTDIR)) {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002194 log_debug_errno(errno, "Directory \"%s\": %m", instance);
Michal Sekletar78a92a52013-01-18 16:13:08 +01002195 return 0;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002196 }
Michal Sekletar78a92a52013-01-18 16:13:08 +01002197
Zbigniew Jędrzejewski-Szmekd710aaf2016-07-22 20:27:45 -04002198 return log_error_errno(errno, "Failed to open directory %s: %m", instance);
Michal Sekletar78a92a52013-01-18 16:13:08 +01002199 }
2200
Michal Schmidt4a62c712014-11-28 19:57:32 +01002201 if (fstat(dirfd(d), &s) < 0)
2202 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Michal Sekletar78a92a52013-01-18 16:13:08 +01002203
Zbigniew Jędrzejewski-Szmekbaaa35a2018-11-20 23:40:44 +01002204 if (!S_ISDIR(s.st_mode))
2205 return log_error_errno(SYNTHETIC_ERRNO(ENOTDIR),
2206 "%s is not a directory.", i->path);
Michal Sekletar78a92a52013-01-18 16:13:08 +01002207
Michal Schmidt4a62c712014-11-28 19:57:32 +01002208 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0)
2209 return log_error_errno(errno, "stat(%s/..) failed: %m", i->path);
Michal Sekletar78a92a52013-01-18 16:13:08 +01002210
Thomas Hindoe Paaboel Andersen2bb158c2015-10-23 20:15:17 +02002211 mountpoint = s.st_dev != ps.st_dev || s.st_ino == ps.st_ino;
Michal Sekletar78a92a52013-01-18 16:13:08 +01002212
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002213 log_debug("Cleanup threshold for %s \"%s\" is %s",
2214 mountpoint ? "mount point" : "directory",
2215 instance,
2216 format_timestamp_us(timestamp, sizeof(timestamp), cutoff));
2217
2218 return dir_cleanup(i, instance, d, &s, cutoff, s.st_dev, mountpoint,
2219 MAX_DEPTH, i->keep_first_level);
Michal Sekletar78a92a52013-01-18 16:13:08 +01002220}
2221
2222static int clean_item(Item *i) {
Michal Sekletar78a92a52013-01-18 16:13:08 +01002223 assert(i);
2224
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002225 log_debug("Running clean action for entry %c %s", (char) i->type, i->path);
2226
Michal Sekletar78a92a52013-01-18 16:13:08 +01002227 switch (i->type) {
2228 case CREATE_DIRECTORY:
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01002229 case CREATE_SUBVOLUME:
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02002230 case CREATE_SUBVOLUME_INHERIT_QUOTA:
2231 case CREATE_SUBVOLUME_NEW_QUOTA:
Michal Sekletar78a92a52013-01-18 16:13:08 +01002232 case TRUNCATE_DIRECTORY:
2233 case IGNORE_PATH:
Lennart Poettering849958d2014-06-10 23:02:40 +02002234 case COPY_FILES:
Michal Sekletar78a92a52013-01-18 16:13:08 +01002235 clean_item_instance(i, i->path);
Zbigniew Jędrzejewski-Szmekdf8dee82016-04-20 00:06:25 -04002236 return 0;
Zbigniew Jędrzejewski-Szmek65241c12017-11-22 15:16:48 +01002237 case EMPTY_DIRECTORY:
Michal Sekletar78a92a52013-01-18 16:13:08 +01002238 case IGNORE_DIRECTORY_PATH:
Franck Bui936f6bd2018-03-02 17:19:32 +01002239 return glob_item(i, clean_item_instance);
Michal Sekletar78a92a52013-01-18 16:13:08 +01002240 default:
Zbigniew Jędrzejewski-Szmekdf8dee82016-04-20 00:06:25 -04002241 return 0;
Michal Sekletar78a92a52013-01-18 16:13:08 +01002242 }
Michal Sekletar78a92a52013-01-18 16:13:08 +01002243}
2244
Lennart Poettering599ebe22018-10-25 23:22:18 +02002245static int process_item(Item *i, OperationMask operation) {
Lennart Poettering811a1582018-10-26 20:56:37 +02002246 OperationMask todo;
2247 int r, q, p;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002248
2249 assert(i);
2250
Lennart Poettering811a1582018-10-26 20:56:37 +02002251 todo = operation & ~i->done;
2252 if (todo == 0) /* Everything already done? */
Lennart Poettering1910cd02014-06-11 01:37:35 +02002253 return 0;
2254
Lennart Poettering811a1582018-10-26 20:56:37 +02002255 i->done |= operation;
Lennart Poettering1910cd02014-06-11 01:37:35 +02002256
Zbigniew Jędrzejewski-Szmeka5648b82019-10-24 10:33:20 +02002257 r = chase_symlinks(i->path, arg_root, CHASE_NO_AUTOFS|CHASE_WARN, NULL, NULL);
Lennart Poettering21af3382018-10-26 21:07:38 +02002258 if (r == -EREMOTE) {
Franck Bui145b8d0f2018-11-30 15:43:13 +01002259 log_notice_errno(r, "Skipping %s", i->path);
Lennart Poettering811a1582018-10-26 20:56:37 +02002260 return 0;
Franck Bui145b8d0f2018-11-30 15:43:13 +01002261 }
2262 if (r < 0)
Lennart Poettering48d96902018-11-20 15:56:55 +01002263 log_debug_errno(r, "Failed to determine whether '%s' is below autofs, ignoring: %m", i->path);
Zbigniew Jędrzejewski-Szmek9348f0e2014-10-01 07:33:22 -05002264
Lennart Poettering599ebe22018-10-25 23:22:18 +02002265 r = FLAGS_SET(operation, OPERATION_CREATE) ? create_item(i) : 0;
William Douglas6d7b5432018-09-10 12:07:29 -07002266 /* Failure can only be tolerated for create */
2267 if (i->allow_failure)
2268 r = 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002269
Lennart Poettering599ebe22018-10-25 23:22:18 +02002270 q = FLAGS_SET(operation, OPERATION_REMOVE) ? remove_item(i) : 0;
2271 p = FLAGS_SET(operation, OPERATION_CLEAN) ? clean_item(i) : 0;
Lennart Poettering1a967b62018-10-25 23:19:00 +02002272
Lennart Poettering811a1582018-10-26 20:56:37 +02002273 return r < 0 ? r :
Zbigniew Jędrzejewski-Szmek1db50422015-01-06 09:53:12 -05002274 q < 0 ? q :
2275 p;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002276}
2277
Lennart Poettering599ebe22018-10-25 23:22:18 +02002278static int process_item_array(ItemArray *array, OperationMask operation) {
Lennart Poettering96d10d72018-10-25 13:51:01 +02002279 int r = 0;
2280 size_t n;
Lennart Poettering753615e2014-06-12 23:07:17 +02002281
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002282 assert(array);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002283
Lennart Poettering811a1582018-10-26 20:56:37 +02002284 /* Create any parent first. */
2285 if (FLAGS_SET(operation, OPERATION_CREATE) && array->parent)
2286 r = process_item_array(array->parent, operation & OPERATION_CREATE);
2287
2288 /* Clean up all children first */
2289 if ((operation & (OPERATION_REMOVE|OPERATION_CLEAN)) && !set_isempty(array->children)) {
2290 Iterator i;
2291 ItemArray *c;
2292
2293 SET_FOREACH(c, array->children, i) {
2294 int k;
2295
2296 k = process_item_array(c, operation & (OPERATION_REMOVE|OPERATION_CLEAN));
2297 if (k < 0 && r == 0)
2298 r = k;
2299 }
2300 }
2301
Lennart Poettering96d10d72018-10-25 13:51:01 +02002302 for (n = 0; n < array->n_items; n++) {
2303 int k;
2304
Lennart Poettering599ebe22018-10-25 23:22:18 +02002305 k = process_item(array->items + n, operation);
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002306 if (k < 0 && r == 0)
2307 r = k;
2308 }
2309
2310 return r;
2311}
2312
2313static void item_free_contents(Item *i) {
2314 assert(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002315 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +01002316 free(i->argument);
Maciej Wereskiebf4e802014-12-04 10:32:10 +01002317 strv_free(i->xattrs);
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05002318
Zbigniew Jędrzejewski-Szmek349cc4a2017-10-03 10:41:51 +02002319#if HAVE_ACL
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05002320 acl_free(i->acl_access);
2321 acl_free(i->acl_default);
2322#endif
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002323}
2324
Yu Watanabe87938c32018-11-26 01:00:54 +09002325static ItemArray* item_array_free(ItemArray *a) {
Lennart Poettering96d10d72018-10-25 13:51:01 +02002326 size_t n;
Maciej Wereskie2f2fb72013-07-19 15:43:12 +02002327
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002328 if (!a)
Yu Watanabe87938c32018-11-26 01:00:54 +09002329 return NULL;
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002330
Lennart Poettering96d10d72018-10-25 13:51:01 +02002331 for (n = 0; n < a->n_items; n++)
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002332 item_free_contents(a->items + n);
Lennart Poettering96d10d72018-10-25 13:51:01 +02002333
Lennart Poettering811a1582018-10-26 20:56:37 +02002334 set_free(a->children);
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002335 free(a->items);
Yu Watanabe87938c32018-11-26 01:00:54 +09002336 return mfree(a);
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002337}
2338
Yu Watanabe93bab282018-09-18 08:39:24 +09002339static int item_compare(const Item *a, const Item *b) {
Lennart Poettering17493fa2015-04-10 16:22:22 +02002340 /* Make sure that the ownership taking item is put first, so
2341 * that we first create the node, and then can adjust it */
2342
Yu Watanabe93bab282018-09-18 08:39:24 +09002343 if (takes_ownership(a->type) && !takes_ownership(b->type))
Lennart Poettering17493fa2015-04-10 16:22:22 +02002344 return -1;
Yu Watanabe93bab282018-09-18 08:39:24 +09002345 if (!takes_ownership(a->type) && takes_ownership(b->type))
Lennart Poettering17493fa2015-04-10 16:22:22 +02002346 return 1;
2347
Yu Watanabe93bab282018-09-18 08:39:24 +09002348 return CMP(a->type, b->type);
Lennart Poettering17493fa2015-04-10 16:22:22 +02002349}
2350
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002351static bool item_compatible(Item *a, Item *b) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002352 assert(a);
2353 assert(b);
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002354 assert(streq(a->path, b->path));
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002355
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002356 if (takes_ownership(a->type) && takes_ownership(b->type))
2357 /* check if the items are the same */
2358 return streq_ptr(a->argument, b->argument) &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002359
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002360 a->uid_set == b->uid_set &&
2361 a->uid == b->uid &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002362
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002363 a->gid_set == b->gid_set &&
2364 a->gid == b->gid &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002365
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002366 a->mode_set == b->mode_set &&
2367 a->mode == b->mode &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002368
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002369 a->age_set == b->age_set &&
2370 a->age == b->age &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002371
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002372 a->mask_perms == b->mask_perms &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002373
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002374 a->keep_first_level == b->keep_first_level &&
Lennart Poettering468d7262012-01-17 15:04:12 +01002375
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002376 a->major_minor == b->major_minor;
Lennart Poettering468d7262012-01-17 15:04:12 +01002377
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002378 return true;
2379}
2380
Dave Reisnera2aced42013-07-24 11:10:05 -04002381static bool should_include_path(const char *path) {
2382 char **prefix;
2383
Lennart Poetteringabef3f92014-06-11 10:14:07 +02002384 STRV_FOREACH(prefix, arg_exclude_prefixes)
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002385 if (path_startswith(path, *prefix)) {
2386 log_debug("Entry \"%s\" matches exclude prefix \"%s\", skipping.",
2387 path, *prefix);
Dave Reisner5c795112013-07-24 11:19:24 -04002388 return false;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002389 }
Dave Reisnera2aced42013-07-24 11:10:05 -04002390
Lennart Poetteringabef3f92014-06-11 10:14:07 +02002391 STRV_FOREACH(prefix, arg_include_prefixes)
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002392 if (path_startswith(path, *prefix)) {
2393 log_debug("Entry \"%s\" matches include prefix \"%s\".", path, *prefix);
Dave Reisnera2aced42013-07-24 11:10:05 -04002394 return true;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002395 }
Dave Reisnera2aced42013-07-24 11:10:05 -04002396
Dave Reisner5c795112013-07-24 11:19:24 -04002397 /* no matches, so we should include this path only if we
2398 * have no whitelist at all */
Lennart Poettering7b943bb2017-11-29 12:40:22 +01002399 if (strv_isempty(arg_include_prefixes))
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002400 return true;
2401
2402 log_debug("Entry \"%s\" does not match any include prefix, skipping.", path);
2403 return false;
Dave Reisnera2aced42013-07-24 11:10:05 -04002404}
2405
Franck Bui4cef1922017-11-16 11:27:29 +01002406static int specifier_expansion_from_arg(Item *i) {
2407 _cleanup_free_ char *unescaped = NULL, *resolved = NULL;
2408 char **xattr;
2409 int r;
2410
2411 assert(i);
2412
Frantisek Sumsal4e361ac2019-04-28 14:28:49 +02002413 if (!i->argument)
Franck Bui4cef1922017-11-16 11:27:29 +01002414 return 0;
2415
2416 switch (i->type) {
2417 case COPY_FILES:
2418 case CREATE_SYMLINK:
2419 case CREATE_FILE:
2420 case TRUNCATE_FILE:
2421 case WRITE_FILE:
2422 r = cunescape(i->argument, 0, &unescaped);
2423 if (r < 0)
2424 return log_error_errno(r, "Failed to unescape parameter to write: %s", i->argument);
2425
2426 r = specifier_printf(unescaped, specifier_table, NULL, &resolved);
2427 if (r < 0)
2428 return r;
2429
2430 free_and_replace(i->argument, resolved);
2431 break;
2432
2433 case SET_XATTR:
2434 case RECURSIVE_SET_XATTR:
2435 assert(i->xattrs);
2436
2437 STRV_FOREACH (xattr, i->xattrs) {
2438 r = specifier_printf(*xattr, specifier_table, NULL, &resolved);
2439 if (r < 0)
2440 return r;
2441
2442 free_and_replace(*xattr, resolved);
2443 }
2444 break;
2445
2446 default:
2447 break;
2448 }
2449 return 0;
2450}
2451
Lennart Poetteringa2d1fb82018-05-16 22:15:46 -04002452static int patch_var_run(const char *fname, unsigned line, char **path) {
2453 const char *k;
2454 char *n;
2455
2456 assert(path);
2457 assert(*path);
2458
2459 /* Optionally rewrites lines referencing /var/run/, to use /run/ instead. Why bother? tmpfiles merges lines in
2460 * some cases and detects conflicts in others. If files/directories are specified through two equivalent lines
2461 * this is problematic as neither case will be detected. Ideally we'd detect these cases by resolving symlinks
2462 * early, but that's precisely not what we can do here as this code very likely is running very early on, at a
2463 * time where the paths in question are not available yet, or even more importantly, our own tmpfiles rules
2464 * might create the paths that are intermediary to the listed paths. We can't really cover the generic case,
2465 * but the least we can do is cover the specific case of /var/run vs. /run, as /var/run is a legacy name for
2466 * /run only, and we explicitly document that and require that on systemd systems the former is a symlink to
2467 * the latter. Moreover files below this path are by far the primary usecase for tmpfiles.d/. */
2468
2469 k = path_startswith(*path, "/var/run/");
2470 if (isempty(k)) /* Don't complain about other paths than /var/run, and not about /var/run itself either. */
2471 return 0;
2472
Yu Watanabe2d9b74b2019-06-24 23:59:38 +09002473 n = path_join("/run", k);
Lennart Poetteringa2d1fb82018-05-16 22:15:46 -04002474 if (!n)
2475 return log_oom();
2476
2477 /* Also log about this briefly. We do so at LOG_NOTICE level, as we fixed up the situation automatically, hence
2478 * there's no immediate need for action by the user. However, in the interest of making things less confusing
2479 * to the user, let's still inform the user that these snippets should really be updated. */
Lennart Poetteringafb7e1c2019-07-10 13:00:44 +02002480 log_syntax(NULL, LOG_NOTICE, fname, line, 0, "Line references path below legacy directory /var/run/, updating %s → %s; please update the tmpfiles.d/ drop-in file accordingly.", *path, n);
Lennart Poetteringa2d1fb82018-05-16 22:15:46 -04002481
Lennart Poettering81fa4472018-10-25 13:57:32 +02002482 free_and_replace(*path, n);
Lennart Poetteringa2d1fb82018-05-16 22:15:46 -04002483
2484 return 0;
2485}
2486
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002487static int parse_line(const char *fname, unsigned line, const char *buffer, bool *invalid_config) {
Lennart Poettering1731e342013-09-17 11:02:02 -05002488
Lennart Poetteringcde684a2014-06-10 22:50:46 +02002489 _cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002490 _cleanup_(item_free_contents) Item i = {};
2491 ItemArray *existing;
Lennart Poetteringef43a392015-04-22 18:18:56 +02002492 OrderedHashmap *h;
daurnimator657cf7f2015-03-09 15:11:44 -04002493 int r, pos;
Zach Smithc55ac242019-10-03 18:09:35 -07002494 bool append_or_force = false, boot = false, allow_failure = false;
Lennart Poettering5008d582010-09-28 02:34:02 +02002495
2496 assert(fname);
2497 assert(line >= 1);
2498 assert(buffer);
2499
Richard Maw68685602015-06-23 16:20:53 +00002500 r = extract_many_words(
Lennart Poettering4034a062015-03-23 18:55:36 +07002501 &buffer,
Richard Maw68685602015-06-23 16:20:53 +00002502 NULL,
Zbigniew Jędrzejewski-Szmek4ec85142019-06-28 11:15:05 +02002503 EXTRACT_UNQUOTE,
Lennart Poettering4034a062015-03-23 18:55:36 +07002504 &action,
2505 &path,
2506 &mode,
2507 &user,
2508 &group,
2509 &age,
2510 NULL);
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002511 if (r < 0) {
Zbigniew Jędrzejewski-Szmek751223f2017-11-24 12:19:40 +01002512 if (IN_SET(r, -EINVAL, -EBADSLT))
2513 /* invalid quoting and such or an unknown specifier */
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002514 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002515 return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to parse line: %m");
Lennart Poettering4b936992018-05-10 11:29:11 -07002516 } else if (r < 2) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002517 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002518 return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Syntax error.");
Lennart Poettering5008d582010-09-28 02:34:02 +02002519 }
2520
Lennart Poetteringe7b88b72019-04-08 12:03:33 +02002521 if (!empty_or_dash(buffer)) {
Lennart Poettering4034a062015-03-23 18:55:36 +07002522 i.argument = strdup(buffer);
2523 if (!i.argument)
2524 return log_oom();
2525 }
2526
Lennart Poettering2e78fa72014-06-16 13:21:07 +02002527 if (isempty(action)) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002528 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002529 return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Command too short '%s'.", action);
Lennart Poettering2e78fa72014-06-16 13:21:07 +02002530 }
2531
Zbigniew Jędrzejewski-Szmek5f255142015-01-09 01:11:01 -05002532 for (pos = 1; action[pos]; pos++) {
2533 if (action[pos] == '!' && !boot)
2534 boot = true;
Zach Smithc55ac242019-10-03 18:09:35 -07002535 else if (action[pos] == '+' && !append_or_force)
2536 append_or_force = true;
William Douglas6d7b5432018-09-10 12:07:29 -07002537 else if (action[pos] == '-' && !allow_failure)
2538 allow_failure = true;
Zbigniew Jędrzejewski-Szmek5f255142015-01-09 01:11:01 -05002539 else {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002540 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002541 return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Unknown modifiers in command '%s'", action);
Zbigniew Jędrzejewski-Szmek5f255142015-01-09 01:11:01 -05002542 }
Lennart Poettering2e78fa72014-06-16 13:21:07 +02002543 }
2544
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002545 if (boot && !arg_boot) {
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002546 log_syntax(NULL, LOG_DEBUG, fname, line, 0, "Ignoring entry %s \"%s\" because --boot is not specified.", action, path);
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05002547 return 0;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002548 }
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05002549
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002550 i.type = action[0];
Zach Smithc55ac242019-10-03 18:09:35 -07002551 i.append_or_force = append_or_force;
William Douglas6d7b5432018-09-10 12:07:29 -07002552 i.allow_failure = allow_failure;
Lennart Poettering1731e342013-09-17 11:02:02 -05002553
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002554 r = specifier_printf(path, specifier_table, NULL, &i.path);
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +01002555 if (r == -ENXIO)
Franck Bui4cef1922017-11-16 11:27:29 +01002556 return log_unresolvable_specifier(fname, line);
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002557 if (r < 0) {
Zbigniew Jędrzejewski-Szmek751223f2017-11-24 12:19:40 +01002558 if (IN_SET(r, -EINVAL, -EBADSLT))
2559 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002560 return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to replace specifiers in '%s': %m", path);
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002561 }
Lennart Poettering1731e342013-09-17 11:02:02 -05002562
Lennart Poetteringa2d1fb82018-05-16 22:15:46 -04002563 r = patch_var_run(fname, line, &i.path);
2564 if (r < 0)
2565 return r;
2566
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002567 switch (i.type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01002568
Michal Schmidt777b87e2011-12-16 18:27:35 +01002569 case CREATE_DIRECTORY:
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01002570 case CREATE_SUBVOLUME:
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02002571 case CREATE_SUBVOLUME_INHERIT_QUOTA:
2572 case CREATE_SUBVOLUME_NEW_QUOTA:
Zbigniew Jędrzejewski-Szmekdf8dee82016-04-20 00:06:25 -04002573 case EMPTY_DIRECTORY:
Michal Schmidt777b87e2011-12-16 18:27:35 +01002574 case TRUNCATE_DIRECTORY:
2575 case CREATE_FIFO:
2576 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01002577 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +01002578 case REMOVE_PATH:
2579 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02002580 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01002581 case RELABEL_PATH:
2582 case RECURSIVE_RELABEL_PATH:
Lennart Poetteringc82500c2015-04-10 14:46:05 +02002583 if (i.argument)
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002584 log_syntax(NULL, LOG_WARNING, fname, line, 0, "%c lines don't take argument fields, ignoring.", i.type);
Lennart Poetteringc82500c2015-04-10 14:46:05 +02002585
2586 break;
2587
2588 case CREATE_FILE:
2589 case TRUNCATE_FILE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01002590 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01002591
2592 case CREATE_SYMLINK:
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002593 if (!i.argument) {
Lennart Poetteringb910cc72019-07-11 19:14:16 +02002594 i.argument = path_join("/usr/share/factory", i.path);
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002595 if (!i.argument)
Kay Sievers2f3b8732014-06-20 15:57:43 +02002596 return log_oom();
Lennart Poettering468d7262012-01-17 15:04:12 +01002597 }
2598 break;
2599
Lennart Poettering31ed59c2012-01-18 16:39:04 +01002600 case WRITE_FILE:
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002601 if (!i.argument) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002602 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002603 return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Write file requires argument.");
Lennart Poettering31ed59c2012-01-18 16:39:04 +01002604 }
2605 break;
2606
Lennart Poettering849958d2014-06-10 23:02:40 +02002607 case COPY_FILES:
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002608 if (!i.argument) {
Lennart Poetteringb910cc72019-07-11 19:14:16 +02002609 i.argument = path_join(arg_root, "/usr/share/factory", i.path);
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002610 if (!i.argument)
Kay Sievers2f3b8732014-06-20 15:57:43 +02002611 return log_oom();
Zbigniew Jędrzejewski-Szmek626f8d12019-06-05 14:46:04 +02002612
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002613 } else if (!path_is_absolute(i.argument)) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002614 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002615 return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Source path '%s' is not absolute.", i.argument);
Zbigniew Jędrzejewski-Szmek626f8d12019-06-05 14:46:04 +02002616
2617 } else if (arg_root) {
2618 char *p;
2619
Lennart Poetteringd34cd572019-07-11 18:41:14 +02002620 p = path_join(arg_root, i.argument);
Zbigniew Jędrzejewski-Szmek626f8d12019-06-05 14:46:04 +02002621 if (!p)
2622 return log_oom();
2623 free_and_replace(i.argument, p);
Lennart Poettering849958d2014-06-10 23:02:40 +02002624 }
2625
Yu Watanabe858d36c2018-05-31 23:39:31 +09002626 path_simplify(i.argument, false);
Lennart Poettering849958d2014-06-10 23:02:40 +02002627 break;
2628
Lennart Poettering468d7262012-01-17 15:04:12 +01002629 case CREATE_CHAR_DEVICE:
Lennart Poettering3a47c402018-06-29 12:01:02 +02002630 case CREATE_BLOCK_DEVICE:
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002631 if (!i.argument) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002632 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002633 return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Device file requires argument.");
Lennart Poettering468d7262012-01-17 15:04:12 +01002634 }
2635
Lennart Poettering3a47c402018-06-29 12:01:02 +02002636 r = parse_dev(i.argument, &i.major_minor);
2637 if (r < 0) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002638 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002639 return log_syntax(NULL, LOG_ERR, fname, line, r, "Can't parse device file major/minor '%s'.", i.argument);
Lennart Poettering468d7262012-01-17 15:04:12 +01002640 }
2641
Lennart Poettering468d7262012-01-17 15:04:12 +01002642 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01002643
Maciej Wereskiebf4e802014-12-04 10:32:10 +01002644 case SET_XATTR:
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05002645 case RECURSIVE_SET_XATTR:
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002646 if (!i.argument) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002647 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002648 return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
2649 "Set extended attribute requires argument.");
Maciej Wereskiebf4e802014-12-04 10:32:10 +01002650 }
Lennart Poetteringbd550f72015-04-10 16:03:24 +02002651 r = parse_xattrs_from_arg(&i);
Maciej Wereskiebf4e802014-12-04 10:32:10 +01002652 if (r < 0)
2653 return r;
2654 break;
2655
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05002656 case SET_ACL:
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05002657 case RECURSIVE_SET_ACL:
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05002658 if (!i.argument) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002659 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002660 return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
2661 "Set ACLs requires argument.");
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05002662 }
Lennart Poetteringbd550f72015-04-10 16:03:24 +02002663 r = parse_acls_from_arg(&i);
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05002664 if (r < 0)
2665 return r;
2666 break;
2667
Lennart Poettering88ec4df2015-04-08 22:35:52 +02002668 case SET_ATTRIBUTE:
2669 case RECURSIVE_SET_ATTRIBUTE:
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01002670 if (!i.argument) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002671 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002672 return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
2673 "Set file attribute requires argument.");
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01002674 }
Lennart Poetteringbd550f72015-04-10 16:03:24 +02002675 r = parse_attribute_from_arg(&i);
Zbigniew Jędrzejewski-Szmek751223f2017-11-24 12:19:40 +01002676 if (IN_SET(r, -EINVAL, -EBADSLT))
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002677 *invalid_config = true;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01002678 if (r < 0)
2679 return r;
2680 break;
2681
Michal Schmidt777b87e2011-12-16 18:27:35 +01002682 default:
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002683 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002684 return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
2685 "Unknown command type '%c'.", (char) i.type);
Lennart Poettering5008d582010-09-28 02:34:02 +02002686 }
Lennart Poettering468d7262012-01-17 15:04:12 +01002687
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002688 if (!path_is_absolute(i.path)) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002689 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002690 return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
2691 "Path '%s' not absolute.", i.path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002692 }
2693
Yu Watanabe858d36c2018-05-31 23:39:31 +09002694 path_simplify(i.path, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002695
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002696 if (!should_include_path(i.path))
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04002697 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02002698
Franck Bui4cef1922017-11-16 11:27:29 +01002699 r = specifier_expansion_from_arg(&i);
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +01002700 if (r == -ENXIO)
Franck Bui4cef1922017-11-16 11:27:29 +01002701 return log_unresolvable_specifier(fname, line);
Zbigniew Jędrzejewski-Szmek751223f2017-11-24 12:19:40 +01002702 if (r < 0) {
2703 if (IN_SET(r, -EINVAL, -EBADSLT))
2704 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002705 return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to substitute specifiers in argument: %m");
Zbigniew Jędrzejewski-Szmek751223f2017-11-24 12:19:40 +01002706 }
Franck Bui4cef1922017-11-16 11:27:29 +01002707
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002708 if (arg_root) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +02002709 char *p;
2710
Lennart Poetteringc6134d32019-06-19 15:20:13 +02002711 p = path_join(arg_root, i.path);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002712 if (!p)
2713 return log_oom();
Lennart Poettering81fa4472018-10-25 13:57:32 +02002714 free_and_replace(i.path, p);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002715 }
2716
Lennart Poetteringe7b88b72019-04-08 12:03:33 +02002717 if (!empty_or_dash(user)) {
Lennart Poettering4b678342011-07-23 01:17:59 +02002718 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02002719
Lennart Poetteringfafff8f2018-08-02 18:36:47 +02002720 r = get_user_creds(&u, &i.uid, NULL, NULL, NULL, USER_CREDS_ALLOW_MISSING);
Lennart Poettering4b678342011-07-23 01:17:59 +02002721 if (r < 0) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002722 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002723 return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to resolve user '%s': %m", user);
Lennart Poettering5008d582010-09-28 02:34:02 +02002724 }
2725
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002726 i.uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02002727 }
2728
Lennart Poetteringe7b88b72019-04-08 12:03:33 +02002729 if (!empty_or_dash(group)) {
Lennart Poettering4b678342011-07-23 01:17:59 +02002730 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02002731
Lennart Poetteringfafff8f2018-08-02 18:36:47 +02002732 r = get_group_creds(&g, &i.gid, USER_CREDS_ALLOW_MISSING);
Lennart Poettering4b678342011-07-23 01:17:59 +02002733 if (r < 0) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002734 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002735 return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to resolve group '%s'.", group);
Lennart Poettering5008d582010-09-28 02:34:02 +02002736 }
2737
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002738 i.gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02002739 }
2740
Lennart Poetteringe7b88b72019-04-08 12:03:33 +02002741 if (!empty_or_dash(mode)) {
Lennart Poetteringabef3f92014-06-11 10:14:07 +02002742 const char *mm = mode;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002743 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02002744
Lennart Poetteringabef3f92014-06-11 10:14:07 +02002745 if (*mm == '~') {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002746 i.mask_perms = true;
Lennart Poetteringabef3f92014-06-11 10:14:07 +02002747 mm++;
2748 }
2749
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002750 r = parse_mode(mm, &m);
2751 if (r < 0) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002752 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002753 return log_syntax(NULL, LOG_ERR, fname, line, r, "Invalid mode '%s'.", mode);
Lennart Poettering5008d582010-09-28 02:34:02 +02002754 }
2755
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002756 i.mode = m;
2757 i.mode_set = true;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002758 } else
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02002759 i.mode = IN_SET(i.type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA) ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002760
Lennart Poetteringe7b88b72019-04-08 12:03:33 +02002761 if (!empty_or_dash(age)) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02002762 const char *a = age;
2763
2764 if (*a == '~') {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002765 i.keep_first_level = true;
Lennart Poettering24f3a372012-06-20 09:05:50 +02002766 a++;
2767 }
2768
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002769 r = parse_sec(a, &i.age);
2770 if (r < 0) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002771 *invalid_config = true;
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002772 return log_syntax(NULL, LOG_ERR, fname, line, r, "Invalid age '%s'.", age);
Lennart Poettering5008d582010-09-28 02:34:02 +02002773 }
2774
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002775 i.age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02002776 }
2777
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002778 h = needs_glob(i.type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01002779
Lennart Poetteringef43a392015-04-22 18:18:56 +02002780 existing = ordered_hashmap_get(h, i.path);
Lennart Poettering468d7262012-01-17 15:04:12 +01002781 if (existing) {
Lennart Poettering96d10d72018-10-25 13:51:01 +02002782 size_t n;
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002783
Lennart Poettering96d10d72018-10-25 13:51:01 +02002784 for (n = 0; n < existing->n_items; n++) {
Zach Smithd0ea5c52019-10-03 18:19:18 -07002785 if (!item_compatible(existing->items + n, &i) && !i.append_or_force) {
Lennart Poetteringd02933f2020-04-29 00:07:06 +02002786 log_syntax(NULL, LOG_NOTICE, fname, line, 0, "Duplicate line for path \"%s\", ignoring.", i.path);
Martin Pitt6487ada2015-03-05 14:58:56 +01002787 return 0;
2788 }
Maciej Wereskiebf4e802014-12-04 10:32:10 +01002789 }
2790 } else {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002791 existing = new0(ItemArray, 1);
Lennart Poettering07982ed2018-01-22 15:32:57 +01002792 if (!existing)
2793 return log_oom();
2794
Lennart Poetteringef43a392015-04-22 18:18:56 +02002795 r = ordered_hashmap_put(h, i.path, existing);
Lennart Poetteringccd114f2018-10-25 13:57:44 +02002796 if (r < 0) {
2797 free(existing);
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002798 return log_oom();
Lennart Poetteringccd114f2018-10-25 13:57:44 +02002799 }
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002800 }
2801
Lennart Poettering96d10d72018-10-25 13:51:01 +02002802 if (!GREEDY_REALLOC(existing->items, existing->allocated, existing->n_items + 1))
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002803 return log_oom();
Lennart Poettering5008d582010-09-28 02:34:02 +02002804
Lennart Poettering7ab75292018-10-25 13:53:07 +02002805 existing->items[existing->n_items++] = i;
2806 i = (struct Item) {};
Zbigniew Jędrzejewski-Szmekdd449ac2015-04-10 18:57:05 -04002807
2808 /* Sort item array, to enforce stable ordering of application */
Lennart Poettering96d10d72018-10-25 13:51:01 +02002809 typesafe_qsort(existing->items, existing->n_items, item_compare);
Lennart Poettering17493fa2015-04-10 16:22:22 +02002810
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04002811 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02002812}
2813
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02002814static int cat_config(char **config_dirs, char **args) {
2815 _cleanup_strv_free_ char **files = NULL;
2816 int r;
2817
2818 r = conf_files_list_with_replacement(arg_root, config_dirs, arg_replace, &files, NULL);
2819 if (r < 0)
2820 return r;
2821
2822 return cat_files(NULL, files, 0);
2823}
2824
Lennart Poettering37ec0fd2018-08-09 10:32:31 +02002825static int help(void) {
2826 _cleanup_free_ char *link = NULL;
2827 int r;
2828
2829 r = terminal_urlify_man("systemd-tmpfiles", "8", &link);
2830 if (r < 0)
2831 return log_oom();
2832
Lennart Poettering522d4a42011-02-13 15:08:15 +01002833 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
2834 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Dave Reisner5c795112013-07-24 11:19:24 -04002835 " -h --help Show this help\n"
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01002836 " --user Execute user configuration\n"
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002837 " --version Show package version\n"
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02002838 " --cat-config Show configuration files\n"
Dave Reisner5c795112013-07-24 11:19:24 -04002839 " --create Create marked files/directories\n"
2840 " --clean Clean up marked directories\n"
2841 " --remove Remove marked files/directories\n"
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05002842 " --boot Execute actions only safe at boot\n"
Zbigniew Jędrzejewski-Szmek79ca8882015-01-27 21:22:08 -05002843 " --prefix=PATH Only apply rules with the specified prefix\n"
2844 " --exclude-prefix=PATH Ignore rules with the specified prefix\n"
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002845 " --root=PATH Operate on an alternate filesystem root\n"
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01002846 " --replace=PATH Treat arguments as replacement for PATH\n"
Lennart Poetteringdcd5c892018-06-12 15:37:53 +02002847 " --no-pager Do not pipe output into a pager\n"
Lennart Poettering37ec0fd2018-08-09 10:32:31 +02002848 "\nSee the %s for details.\n"
2849 , program_invocation_short_name
2850 , link
2851 );
2852
2853 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002854}
2855
2856static int parse_argv(int argc, char *argv[]) {
2857
2858 enum {
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002859 ARG_VERSION = 0x100,
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02002860 ARG_CAT_CONFIG,
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01002861 ARG_USER,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002862 ARG_CREATE,
2863 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002864 ARG_REMOVE,
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05002865 ARG_BOOT,
Dave Reisner5c795112013-07-24 11:19:24 -04002866 ARG_PREFIX,
2867 ARG_EXCLUDE_PREFIX,
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002868 ARG_ROOT,
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01002869 ARG_REPLACE,
Lennart Poetteringdcd5c892018-06-12 15:37:53 +02002870 ARG_NO_PAGER,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002871 };
2872
2873 static const struct option options[] = {
Dave Reisner5c795112013-07-24 11:19:24 -04002874 { "help", no_argument, NULL, 'h' },
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01002875 { "user", no_argument, NULL, ARG_USER },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002876 { "version", no_argument, NULL, ARG_VERSION },
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02002877 { "cat-config", no_argument, NULL, ARG_CAT_CONFIG },
Dave Reisner5c795112013-07-24 11:19:24 -04002878 { "create", no_argument, NULL, ARG_CREATE },
2879 { "clean", no_argument, NULL, ARG_CLEAN },
2880 { "remove", no_argument, NULL, ARG_REMOVE },
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05002881 { "boot", no_argument, NULL, ARG_BOOT },
Dave Reisner5c795112013-07-24 11:19:24 -04002882 { "prefix", required_argument, NULL, ARG_PREFIX },
2883 { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX },
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002884 { "root", required_argument, NULL, ARG_ROOT },
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01002885 { "replace", required_argument, NULL, ARG_REPLACE },
Lennart Poetteringdcd5c892018-06-12 15:37:53 +02002886 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002887 {}
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002888 };
2889
Lennart Poettering0f474362015-10-22 19:28:31 +02002890 int c, r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002891
2892 assert(argc >= 0);
2893 assert(argv);
2894
Zbigniew Jędrzejewski-Szmek601185b2014-08-02 11:12:21 -04002895 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002896
2897 switch (c) {
2898
2899 case 'h':
Lennart Poettering37ec0fd2018-08-09 10:32:31 +02002900 return help();
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002901
2902 case ARG_VERSION:
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +02002903 return version();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002904
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02002905 case ARG_CAT_CONFIG:
2906 arg_cat_config = true;
2907 break;
2908
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01002909 case ARG_USER:
2910 arg_user = true;
2911 break;
2912
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002913 case ARG_CREATE:
Lennart Poettering1a967b62018-10-25 23:19:00 +02002914 arg_operation |= OPERATION_CREATE;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002915 break;
2916
2917 case ARG_CLEAN:
Lennart Poettering1a967b62018-10-25 23:19:00 +02002918 arg_operation |= OPERATION_CLEAN;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002919 break;
2920
2921 case ARG_REMOVE:
Lennart Poettering1a967b62018-10-25 23:19:00 +02002922 arg_operation |= OPERATION_REMOVE;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002923 break;
2924
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05002925 case ARG_BOOT:
2926 arg_boot = true;
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05002927 break;
2928
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002929 case ARG_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02002930 if (strv_push(&arg_include_prefixes, optarg) < 0)
Dave Reisnera2aced42013-07-24 11:10:05 -04002931 return log_oom();
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002932 break;
2933
Dave Reisner5c795112013-07-24 11:19:24 -04002934 case ARG_EXCLUDE_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02002935 if (strv_push(&arg_exclude_prefixes, optarg) < 0)
Dave Reisner5c795112013-07-24 11:19:24 -04002936 return log_oom();
2937 break;
2938
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002939 case ARG_ROOT:
Lennart Poettering0f03c2a2015-10-22 19:54:29 +02002940 r = parse_path_argument_and_warn(optarg, true, &arg_root);
Lennart Poettering0f474362015-10-22 19:28:31 +02002941 if (r < 0)
Lennart Poettering0f03c2a2015-10-22 19:54:29 +02002942 return r;
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002943 break;
2944
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01002945 case ARG_REPLACE:
2946 if (!path_is_absolute(optarg) ||
Zbigniew Jędrzejewski-Szmekbaaa35a2018-11-20 23:40:44 +01002947 !endswith(optarg, ".conf"))
2948 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2949 "The argument to --replace= must an absolute path to a config file");
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01002950
2951 arg_replace = optarg;
2952 break;
2953
Lennart Poetteringdcd5c892018-06-12 15:37:53 +02002954 case ARG_NO_PAGER:
Zbigniew Jędrzejewski-Szmek0221d682018-11-11 12:56:29 +01002955 arg_pager_flags |= PAGER_DISABLE;
Lennart Poetteringdcd5c892018-06-12 15:37:53 +02002956 break;
2957
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002958 case '?':
2959 return -EINVAL;
2960
2961 default:
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002962 assert_not_reached("Unhandled option");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002963 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002964
Zbigniew Jędrzejewski-Szmekbaaa35a2018-11-20 23:40:44 +01002965 if (arg_operation == 0 && !arg_cat_config)
2966 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2967 "You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002968
Zbigniew Jędrzejewski-Szmekbaaa35a2018-11-20 23:40:44 +01002969 if (arg_replace && arg_cat_config)
2970 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2971 "Option --replace= is not supported with --cat-config");
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02002972
Zbigniew Jędrzejewski-Szmekbaaa35a2018-11-20 23:40:44 +01002973 if (arg_replace && optind >= argc)
2974 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2975 "When --replace= is given, some configuration items must be specified");
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01002976
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002977 return 1;
2978}
2979
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01002980static int read_config_file(char **config_dirs, const char *fn, bool ignore_enoent, bool *invalid_config) {
Zbigniew Jędrzejewski-Szmekf7ac1ed2016-04-19 22:34:04 -04002981 _cleanup_fclose_ FILE *_f = NULL;
Michal Sekletar78a92a52013-01-18 16:13:08 +01002982 Iterator iterator;
Lennart Poettering1731e342013-09-17 11:02:02 -05002983 unsigned v = 0;
Lennart Poettering2a98ae42018-10-18 16:21:05 +02002984 FILE *f;
Michal Sekletar78a92a52013-01-18 16:13:08 +01002985 Item *i;
Lennart Poettering4e68ec12016-06-21 13:20:23 +02002986 int r = 0;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002987
2988 assert(fn);
2989
Zbigniew Jędrzejewski-Szmekf7ac1ed2016-04-19 22:34:04 -04002990 if (streq(fn, "-")) {
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01002991 log_debug("Reading config from stdin…");
Zbigniew Jędrzejewski-Szmekf7ac1ed2016-04-19 22:34:04 -04002992 fn = "<stdin>";
2993 f = stdin;
2994 } else {
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01002995 r = search_and_fopen(fn, "re", arg_root, (const char**) config_dirs, &_f);
Zbigniew Jędrzejewski-Szmekf7ac1ed2016-04-19 22:34:04 -04002996 if (r < 0) {
2997 if (ignore_enoent && r == -ENOENT) {
2998 log_debug_errno(r, "Failed to open \"%s\", ignoring: %m", fn);
2999 return 0;
3000 }
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003001
Zbigniew Jędrzejewski-Szmekf7ac1ed2016-04-19 22:34:04 -04003002 return log_error_errno(r, "Failed to open '%s': %m", fn);
3003 }
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01003004 log_debug("Reading config file \"%s\"…", fn);
Zbigniew Jędrzejewski-Szmekf7ac1ed2016-04-19 22:34:04 -04003005 f = _f;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003006 }
3007
Lennart Poettering2a98ae42018-10-18 16:21:05 +02003008 for (;;) {
3009 _cleanup_free_ char *line = NULL;
3010 bool invalid_line = false;
Lennart Poettering1731e342013-09-17 11:02:02 -05003011 char *l;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003012 int k;
Lennart Poettering2a98ae42018-10-18 16:21:05 +02003013
3014 k = read_line(f, LONG_LINE_MAX, &line);
3015 if (k < 0)
3016 return log_error_errno(k, "Failed to read '%s': %m", fn);
3017 if (k == 0)
3018 break;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003019
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003020 v++;
3021
3022 l = strstrip(line);
Yu Watanabe4c701092017-10-04 23:01:32 +09003023 if (IN_SET(*l, 0, '#'))
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003024 continue;
3025
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01003026 k = parse_line(fn, v, l, &invalid_line);
3027 if (k < 0) {
3028 if (invalid_line)
3029 /* Allow reporting with a special code if the caller requested this */
3030 *invalid_config = true;
3031 else if (r == 0)
3032 /* The first error becomes our return value */
3033 r = k;
3034 }
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003035 }
3036
Michal Sekletar78a92a52013-01-18 16:13:08 +01003037 /* we have to determine age parameter for each entry of type X */
Lennart Poetteringef43a392015-04-22 18:18:56 +02003038 ORDERED_HASHMAP_FOREACH(i, globs, iterator) {
Michal Sekletar78a92a52013-01-18 16:13:08 +01003039 Iterator iter;
3040 Item *j, *candidate_item = NULL;
3041
3042 if (i->type != IGNORE_DIRECTORY_PATH)
3043 continue;
3044
Lennart Poetteringef43a392015-04-22 18:18:56 +02003045 ORDERED_HASHMAP_FOREACH(j, items, iter) {
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02003046 if (!IN_SET(j->type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA))
Michal Sekletar78a92a52013-01-18 16:13:08 +01003047 continue;
3048
3049 if (path_equal(j->path, i->path)) {
3050 candidate_item = j;
3051 break;
3052 }
3053
3054 if ((!candidate_item && path_startswith(i->path, j->path)) ||
3055 (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
3056 candidate_item = j;
3057 }
3058
Richard Weinberger9ed2a352014-09-09 11:09:37 +02003059 if (candidate_item && candidate_item->age_set) {
Michal Sekletar78a92a52013-01-18 16:13:08 +01003060 i->age = candidate_item->age;
3061 i->age_set = true;
3062 }
3063 }
3064
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003065 if (ferror(f)) {
Michal Schmidt56f64d92014-11-28 19:29:59 +01003066 log_error_errno(errno, "Failed to read from file %s: %m", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003067 if (r == 0)
3068 r = -EIO;
3069 }
3070
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003071 return r;
3072}
3073
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01003074static int parse_arguments(char **config_dirs, char **args, bool *invalid_config) {
3075 char **arg;
3076 int r;
3077
3078 STRV_FOREACH(arg, args) {
3079 r = read_config_file(config_dirs, *arg, false, invalid_config);
3080 if (r < 0)
3081 return r;
3082 }
3083
3084 return 0;
3085}
3086
3087static int read_config_files(char **config_dirs, char **args, bool *invalid_config) {
3088 _cleanup_strv_free_ char **files = NULL;
3089 _cleanup_free_ char *p = NULL;
3090 char **f;
3091 int r;
3092
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02003093 r = conf_files_list_with_replacement(arg_root, config_dirs, arg_replace, &files, &p);
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01003094 if (r < 0)
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02003095 return r;
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01003096
3097 STRV_FOREACH(f, files)
3098 if (p && path_equal(*f, p)) {
3099 log_debug("Parsing arguments at position \"%s\"…", *f);
3100
3101 r = parse_arguments(config_dirs, args, invalid_config);
3102 if (r < 0)
3103 return r;
3104 } else
3105 /* Just warn, ignore result otherwise.
3106 * read_config_file() has some debug output, so no need to print anything. */
3107 (void) read_config_file(config_dirs, *f, true, invalid_config);
3108
3109 return 0;
3110}
3111
Lennart Poettering811a1582018-10-26 20:56:37 +02003112static int link_parent(ItemArray *a) {
3113 const char *path;
3114 char *prefix;
3115 int r;
3116
3117 assert(a);
3118
Lennart Poettering09f467a2018-11-20 16:32:19 +01003119 /* Finds the closest "parent" item array for the specified item array. Then registers the specified item array
Lennart Poettering811a1582018-10-26 20:56:37 +02003120 * as child of it, and fills the parent in, linking them both ways. This allows us to later create parents
3121 * before their children, and clean up/remove children before their parents. */
3122
3123 if (a->n_items <= 0)
3124 return 0;
3125
3126 path = a->items[0].path;
Lennart Poettering6e9417f2019-01-26 15:52:18 +01003127 prefix = newa(char, strlen(path) + 1);
Lennart Poettering811a1582018-10-26 20:56:37 +02003128 PATH_FOREACH_PREFIX(prefix, path) {
3129 ItemArray *j;
3130
3131 j = ordered_hashmap_get(items, prefix);
Lennart Poetteringbd0ce242018-11-20 16:32:23 +01003132 if (!j)
3133 j = ordered_hashmap_get(globs, prefix);
Lennart Poettering811a1582018-10-26 20:56:37 +02003134 if (j) {
3135 r = set_ensure_allocated(&j->children, NULL);
3136 if (r < 0)
3137 return log_oom();
3138
3139 r = set_put(j->children, a);
3140 if (r < 0)
3141 return log_oom();
3142
3143 a->parent = j;
3144 return 1;
3145 }
3146 }
3147
3148 return 0;
3149}
3150
Yu Watanabe87938c32018-11-26 01:00:54 +09003151DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(item_array_hash_ops, char, string_hash_func, string_compare_func,
3152 ItemArray, item_array_free);
3153
3154static int run(int argc, char *argv[]) {
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01003155 _cleanup_strv_free_ char **config_dirs = NULL;
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01003156 bool invalid_config = false;
Lennart Poettering64adb372018-10-26 21:19:36 +02003157 Iterator iterator;
3158 ItemArray *a;
Lennart Poettering44ac4f82018-11-20 16:15:09 +01003159 enum {
3160 PHASE_REMOVE_AND_CLEAN,
3161 PHASE_CREATE,
3162 _PHASE_MAX
3163 } phase;
Yu Watanabe87938c32018-11-26 01:00:54 +09003164 int r, k;
Lennart Poettering5008d582010-09-28 02:34:02 +02003165
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01003166 r = parse_argv(argc, argv);
3167 if (r <= 0)
Yu Watanabe87938c32018-11-26 01:00:54 +09003168 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02003169
Lennart Poettering6bf3c612018-11-20 11:18:22 +01003170 log_setup_service();
Lennart Poettering5008d582010-09-28 02:34:02 +02003171
Lennart Poetteringe5358402019-01-18 16:34:52 +01003172 /* Descending down file system trees might take a lot of fds */
3173 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE);
3174
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01003175 if (arg_user) {
3176 r = user_config_paths(&config_dirs);
Yu Watanabe87938c32018-11-26 01:00:54 +09003177 if (r < 0)
3178 return log_error_errno(r, "Failed to initialize configuration directory list: %m");
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01003179 } else {
3180 config_dirs = strv_split_nulstr(CONF_PATHS_NULSTR("tmpfiles.d"));
Yu Watanabe87938c32018-11-26 01:00:54 +09003181 if (!config_dirs)
3182 return log_oom();
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01003183 }
3184
Lennart Poettering79c91ce2018-01-22 15:33:13 +01003185 if (DEBUG_LOGGING) {
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01003186 _cleanup_free_ char *t = NULL;
3187
3188 t = strv_join(config_dirs, "\n\t");
3189 if (t)
Zbigniew Jędrzejewski-Szmekbb9947b2018-03-29 16:19:33 +02003190 log_debug("Looking for configuration files in (higher priority first):\n\t%s", t);
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01003191 }
3192
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02003193 if (arg_cat_config) {
Zbigniew Jędrzejewski-Szmek0221d682018-11-11 12:56:29 +01003194 (void) pager_open(arg_pager_flags);
Lennart Poetteringdcd5c892018-06-12 15:37:53 +02003195
Yu Watanabe87938c32018-11-26 01:00:54 +09003196 return cat_config(config_dirs, argv + optind);
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02003197 }
3198
3199 umask(0022);
3200
3201 mac_selinux_init();
3202
Yu Watanabe87938c32018-11-26 01:00:54 +09003203 items = ordered_hashmap_new(&item_array_hash_ops);
3204 globs = ordered_hashmap_new(&item_array_hash_ops);
3205 if (!items || !globs)
3206 return log_oom();
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02003207
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01003208 /* If command line arguments are specified along with --replace, read all
3209 * configuration files and insert the positional arguments at the specified
3210 * place. Otherwise, if command line arguments are specified, execute just
3211 * them, and finally, without --replace= or any positional arguments, just
3212 * read configuration and execute it.
3213 */
3214 if (arg_replace || optind >= argc)
3215 r = read_config_files(config_dirs, argv + optind, &invalid_config);
3216 else
3217 r = parse_arguments(config_dirs, argv + optind, &invalid_config);
3218 if (r < 0)
Yu Watanabe87938c32018-11-26 01:00:54 +09003219 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02003220
Lennart Poettering811a1582018-10-26 20:56:37 +02003221 /* Let's now link up all child/parent relationships */
Lennart Poetteringef43a392015-04-22 18:18:56 +02003222 ORDERED_HASHMAP_FOREACH(a, items, iterator) {
Lennart Poettering811a1582018-10-26 20:56:37 +02003223 r = link_parent(a);
3224 if (r < 0)
Yu Watanabe87938c32018-11-26 01:00:54 +09003225 return r;
Lennart Poettering811a1582018-10-26 20:56:37 +02003226 }
3227 ORDERED_HASHMAP_FOREACH(a, globs, iterator) {
3228 r = link_parent(a);
3229 if (r < 0)
Yu Watanabe87938c32018-11-26 01:00:54 +09003230 return r;
Zbigniew Jędrzejewski-Szmek1db50422015-01-06 09:53:12 -05003231 }
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01003232
Lennart Poettering64adb372018-10-26 21:19:36 +02003233 /* If multiple operations are requested, let's first run the remove/clean operations, and only then the create
3234 * operations. i.e. that we first clean out the platform we then build on. */
Lennart Poettering44ac4f82018-11-20 16:15:09 +01003235 for (phase = 0; phase < _PHASE_MAX; phase++) {
Lennart Poettering64adb372018-10-26 21:19:36 +02003236 OperationMask op;
Lennart Poettering5008d582010-09-28 02:34:02 +02003237
Lennart Poettering44ac4f82018-11-20 16:15:09 +01003238 if (phase == PHASE_REMOVE_AND_CLEAN)
Lennart Poettering64adb372018-10-26 21:19:36 +02003239 op = arg_operation & (OPERATION_REMOVE|OPERATION_CLEAN);
Lennart Poettering44ac4f82018-11-20 16:15:09 +01003240 else if (phase == PHASE_CREATE)
Lennart Poettering64adb372018-10-26 21:19:36 +02003241 op = arg_operation & OPERATION_CREATE;
3242 else
3243 assert_not_reached("unexpected phase");
3244
3245 if (op == 0) /* Nothing requested in this phase */
3246 continue;
3247
3248 /* The non-globbing ones usually create things, hence we apply them first */
3249 ORDERED_HASHMAP_FOREACH(a, items, iterator) {
3250 k = process_item_array(a, op);
Yu Watanabe87938c32018-11-26 01:00:54 +09003251 if (k < 0 && r >= 0)
3252 r = k;
Lennart Poettering64adb372018-10-26 21:19:36 +02003253 }
3254
3255 /* The globbing ones usually alter things, hence we apply them second. */
3256 ORDERED_HASHMAP_FOREACH(a, globs, iterator) {
3257 k = process_item_array(a, op);
Yu Watanabe87938c32018-11-26 01:00:54 +09003258 if (k < 0 && r >= 0)
3259 r = k;
Lennart Poettering64adb372018-10-26 21:19:36 +02003260 }
Zbigniew Jędrzejewski-Szmek1db50422015-01-06 09:53:12 -05003261 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02003262
Yu Watanabe87938c32018-11-26 01:00:54 +09003263 if (ERRNO_IS_RESOURCE(-r))
3264 return r;
3265 if (invalid_config)
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01003266 return EX_DATAERR;
Yu Watanabe87938c32018-11-26 01:00:54 +09003267 if (r < 0)
Zbigniew Jędrzejewski-Szmekbb9947b2018-03-29 16:19:33 +02003268 return EX_CANTCREAT;
Yu Watanabe87938c32018-11-26 01:00:54 +09003269 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02003270}
Yu Watanabe87938c32018-11-26 01:00:54 +09003271
3272DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);