blob: b830850879b951c80d9f4f5bec20432b533ea002 [file] [log] [blame]
Lennart Poettering5008d582010-09-28 02:34:02 +02001/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02006 Copyright 2010 Lennart Poettering, Kay Sievers
Lennart Poettering5008d582010-09-28 02:34:02 +02007
8 systemd is free software; you can redistribute it and/or modify it
Lennart Poettering5430f7f2012-04-12 00:20:58 +02009 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
Lennart Poettering5008d582010-09-28 02:34:02 +020011 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lennart Poettering5430f7f2012-04-12 00:20:58 +020016 Lesser General Public License for more details.
Lennart Poettering5008d582010-09-28 02:34:02 +020017
Lennart Poettering5430f7f2012-04-12 00:20:58 +020018 You should have received a copy of the GNU Lesser General Public License
Lennart Poettering5008d582010-09-28 02:34:02 +020019 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <unistd.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <limits.h>
28#include <dirent.h>
29#include <grp.h>
30#include <pwd.h>
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020031#include <stdio.h>
32#include <stdlib.h>
33#include <stddef.h>
34#include <getopt.h>
35#include <stdbool.h>
36#include <time.h>
37#include <sys/types.h>
38#include <sys/param.h>
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010039#include <glob.h>
40#include <fnmatch.h>
Maciej Wereskiebf4e802014-12-04 10:32:10 +010041#include <sys/xattr.h>
Lennart Poettering5008d582010-09-28 02:34:02 +020042
43#include "log.h"
44#include "util.h"
Dave Reisner54693d92012-09-15 12:58:49 -040045#include "macro.h"
Zbigniew Jędrzejewski-Szmekd39efe72013-03-13 20:20:54 -040046#include "missing.h"
Kay Sievers49e942b2012-04-10 21:54:31 +020047#include "mkdir.h"
Kay Sievers9eb977d2012-05-07 21:36:12 +020048#include "path-util.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020049#include "strv.h"
50#include "label.h"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020051#include "set.h"
Kay Sievers2c210442012-05-07 18:55:45 +020052#include "conf-files.h"
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -070053#include "capability.h"
Lennart Poettering1731e342013-09-17 11:02:02 -050054#include "specifier.h"
Lennart Poetteringeb9da372013-11-06 18:28:39 +010055#include "build.h"
Lennart Poettering849958d2014-06-10 23:02:40 +020056#include "copy.h"
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +010057#include "selinux-util.h"
58#include "btrfs-util.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020059
Andreas Jaeger01000472010-09-29 10:08:24 +020060/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020061 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020062 * properly owned directories beneath /tmp, /var/tmp, /run, which are
63 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020064
Michal Schmidt66ccd032011-12-15 21:31:14 +010065typedef enum ItemType {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010066 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020067 CREATE_FILE = 'f',
68 TRUNCATE_FILE = 'F',
69 CREATE_DIRECTORY = 'd',
70 TRUNCATE_DIRECTORY = 'D',
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +010071 CREATE_SUBVOLUME = 'v',
Lennart Poetteringee17ee72011-07-12 03:56:56 +020072 CREATE_FIFO = 'p',
Lennart Poettering468d7262012-01-17 15:04:12 +010073 CREATE_SYMLINK = 'L',
74 CREATE_CHAR_DEVICE = 'c',
75 CREATE_BLOCK_DEVICE = 'b',
Lennart Poettering849958d2014-06-10 23:02:40 +020076 COPY_FILES = 'C',
Maciej Wereskiebf4e802014-12-04 10:32:10 +010077 SET_XATTR = 't',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010078
79 /* These ones take globs */
Lennart Poetteringcde684a2014-06-10 22:50:46 +020080 WRITE_FILE = 'w',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020081 IGNORE_PATH = 'x',
Michal Sekletar78a92a52013-01-18 16:13:08 +010082 IGNORE_DIRECTORY_PATH = 'X',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020083 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010084 RECURSIVE_REMOVE_PATH = 'R',
Lennart Poetteringe73a03e2014-06-10 23:42:16 +020085 ADJUST_MODE = 'm', /* legacy, 'z' is identical to this */
Michal Schmidt777b87e2011-12-16 18:27:35 +010086 RELABEL_PATH = 'z',
Lennart Poetteringe73a03e2014-06-10 23:42:16 +020087 RECURSIVE_RELABEL_PATH = 'Z',
Michal Schmidt66ccd032011-12-15 21:31:14 +010088} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020089
90typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +010091 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020092
93 char *path;
Lennart Poettering468d7262012-01-17 15:04:12 +010094 char *argument;
Maciej Wereskiebf4e802014-12-04 10:32:10 +010095 char **xattrs;
Lennart Poettering5008d582010-09-28 02:34:02 +020096 uid_t uid;
97 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020098 mode_t mode;
99 usec_t age;
100
Lennart Poettering468d7262012-01-17 15:04:12 +0100101 dev_t major_minor;
102
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200103 bool uid_set:1;
104 bool gid_set:1;
105 bool mode_set:1;
106 bool age_set:1;
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200107 bool mask_perms:1;
Lennart Poettering24f3a372012-06-20 09:05:50 +0200108
109 bool keep_first_level:1;
Lennart Poettering1910cd02014-06-11 01:37:35 +0200110
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200111 bool force:1;
112
Lennart Poettering1910cd02014-06-11 01:37:35 +0200113 bool done:1;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200114} Item;
115
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200116static bool arg_create = false;
117static bool arg_clean = false;
118static bool arg_remove = false;
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -0500119static bool arg_boot = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200120
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200121static char **arg_include_prefixes = NULL;
122static char **arg_exclude_prefixes = NULL;
Michael Marineaucf9a4ab2014-03-13 21:32:13 -0700123static char *arg_root = NULL;
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100124
Josh Triplett7f0a55d2014-10-29 05:02:56 -0700125static const char conf_file_dirs[] = CONF_DIRS_NULSTR("tmpfiles");
Dave Reisner91256702012-06-08 22:31:19 -0400126
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200127#define MAX_DEPTH 256
128
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200129static Hashmap *items = NULL, *globs = NULL;
130static Set *unix_sockets = NULL;
131
Michal Schmidt66ccd032011-12-15 21:31:14 +0100132static bool needs_glob(ItemType t) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200133 return IN_SET(t,
134 WRITE_FILE,
135 IGNORE_PATH,
136 IGNORE_DIRECTORY_PATH,
137 REMOVE_PATH,
138 RECURSIVE_REMOVE_PATH,
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200139 ADJUST_MODE,
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200140 RELABEL_PATH,
141 RECURSIVE_RELABEL_PATH);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100142}
143
144static struct Item* find_glob(Hashmap *h, const char *match) {
145 Item *j;
146 Iterator i;
147
148 HASHMAP_FOREACH(j, h, i)
149 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
150 return j;
151
152 return NULL;
153}
154
Lennart Poettering17b90522011-02-14 21:55:06 +0100155static void load_unix_sockets(void) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200156 _cleanup_fclose_ FILE *f = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100157 char line[LINE_MAX];
158
159 if (unix_sockets)
160 return;
161
162 /* We maintain a cache of the sockets we found in
163 * /proc/net/unix to speed things up a little. */
164
Michal Schmidtd5099ef2014-08-13 01:00:18 +0200165 unix_sockets = set_new(&string_hash_ops);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100166 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100167 return;
168
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100169 f = fopen("/proc/net/unix", "re");
170 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100171 return;
172
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100173 /* Skip header */
174 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100175 goto fail;
176
177 for (;;) {
178 char *p, *s;
179 int k;
180
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100181 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100182 break;
183
184 truncate_nl(line);
185
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100186 p = strchr(line, ':');
187 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100188 continue;
189
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100190 if (strlen(p) < 37)
191 continue;
192
193 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100194 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100195 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100196 p += strspn(p, WHITESPACE);
197
198 if (*p != '/')
199 continue;
200
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100201 s = strdup(p);
202 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100203 goto fail;
204
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100205 path_kill_slashes(s);
206
Zbigniew Jędrzejewski-Szmekef422022013-04-22 23:12:15 -0400207 k = set_consume(unix_sockets, s);
208 if (k < 0 && k != -EEXIST)
209 goto fail;
Lennart Poettering17b90522011-02-14 21:55:06 +0100210 }
211
212 return;
213
214fail:
215 set_free_free(unix_sockets);
216 unix_sockets = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100217}
218
219static bool unix_socket_alive(const char *fn) {
220 assert(fn);
221
222 load_unix_sockets();
223
224 if (unix_sockets)
225 return !!set_get(unix_sockets, (char*) fn);
226
227 /* We don't know, so assume yes */
228 return true;
229}
230
Kay Sievers99d680a2013-03-13 13:12:36 +0100231static int dir_is_mount_point(DIR *d, const char *subdir) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200232
233 union file_handle_union h = {
234 .handle.handle_bytes = MAX_HANDLE_SZ
235 };
236
Kay Sievers99d680a2013-03-13 13:12:36 +0100237 int mount_id_parent, mount_id;
238 int r_p, r;
239
Dave Reisner370c8602014-04-19 13:22:35 -0400240 r_p = name_to_handle_at(dirfd(d), ".", &h.handle, &mount_id_parent, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100241 if (r_p < 0)
242 r_p = -errno;
243
Dave Reisner370c8602014-04-19 13:22:35 -0400244 h.handle.handle_bytes = MAX_HANDLE_SZ;
245 r = name_to_handle_at(dirfd(d), subdir, &h.handle, &mount_id, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100246 if (r < 0)
247 r = -errno;
248
249 /* got no handle; make no assumptions, return error */
250 if (r_p < 0 && r < 0)
251 return r_p;
252
253 /* got both handles; if they differ, it is a mount point */
254 if (r_p >= 0 && r >= 0)
255 return mount_id_parent != mount_id;
256
257 /* got only one handle; assume different mount points if one
258 * of both queries was not supported by the filesystem */
Dave Reisnere7aab542014-10-11 20:35:06 -0400259 if (r_p == -ENOSYS || r_p == -EOPNOTSUPP || r == -ENOSYS || r == -EOPNOTSUPP)
Kay Sievers99d680a2013-03-13 13:12:36 +0100260 return true;
261
262 /* return error */
263 if (r_p < 0)
264 return r_p;
265 return r;
266}
267
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200268static int dir_cleanup(
Michal Sekletar78a92a52013-01-18 16:13:08 +0100269 Item *i,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200270 const char *p,
271 DIR *d,
272 const struct stat *ds,
273 usec_t cutoff,
274 dev_t rootdev,
275 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200276 int maxdepth,
Lennart Poettering265ffa12013-09-17 16:33:30 -0500277 bool keep_this_level) {
278
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200279 struct dirent *dent;
280 struct timespec times[2];
281 bool deleted = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200282 int r = 0;
283
284 while ((dent = readdir(d))) {
285 struct stat s;
286 usec_t age;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200287 _cleanup_free_ char *sub_path = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200288
289 if (streq(dent->d_name, ".") ||
290 streq(dent->d_name, ".."))
291 continue;
292
293 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
Kay Sieversca2f4172013-10-17 03:20:46 +0200294 if (errno == ENOENT)
295 continue;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200296
Kay Sieversca2f4172013-10-17 03:20:46 +0200297 /* FUSE, NFS mounts, SELinux might return EACCES */
298 if (errno == EACCES)
Michal Schmidt56f64d92014-11-28 19:29:59 +0100299 log_debug_errno(errno, "stat(%s/%s) failed: %m", p, dent->d_name);
Kay Sieversca2f4172013-10-17 03:20:46 +0200300 else
Michal Schmidt56f64d92014-11-28 19:29:59 +0100301 log_error_errno(errno, "stat(%s/%s) failed: %m", p, dent->d_name);
Kay Sieversca2f4172013-10-17 03:20:46 +0200302 r = -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200303 continue;
304 }
305
306 /* Stay on the same filesystem */
307 if (s.st_dev != rootdev)
308 continue;
309
Kay Sievers99d680a2013-03-13 13:12:36 +0100310 /* Try to detect bind mounts of the same filesystem instance; they
311 * do not differ in device major/minors. This type of query is not
312 * supported on all kernels or filesystem types though. */
313 if (S_ISDIR(s.st_mode) && dir_is_mount_point(d, dent->d_name) > 0)
314 continue;
315
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200316 /* Do not delete read-only files owned by root */
317 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
318 continue;
319
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200320 sub_path = strjoin(p, "/", dent->d_name, NULL);
321 if (!sub_path) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700322 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200323 goto finish;
324 }
325
326 /* Is there an item configured for this path? */
327 if (hashmap_get(items, sub_path))
328 continue;
329
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100330 if (find_glob(globs, sub_path))
331 continue;
332
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200333 if (S_ISDIR(s.st_mode)) {
334
335 if (mountpoint &&
336 streq(dent->d_name, "lost+found") &&
337 s.st_uid == 0)
338 continue;
339
340 if (maxdepth <= 0)
341 log_warning("Reached max depth on %s.", sub_path);
342 else {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200343 _cleanup_closedir_ DIR *sub_dir;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200344 int q;
345
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200346 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200347 if (!sub_dir) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200348 if (errno != ENOENT) {
Michal Schmidt56f64d92014-11-28 19:29:59 +0100349 log_error_errno(errno, "opendir(%s/%s) failed: %m", p, dent->d_name);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200350 r = -errno;
351 }
352
353 continue;
354 }
355
Michal Sekletar78a92a52013-01-18 16:13:08 +0100356 q = dir_cleanup(i, sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200357 if (q < 0)
358 r = q;
359 }
360
Lennart Poettering24f3a372012-06-20 09:05:50 +0200361 /* Note: if you are wondering why we don't
362 * support the sticky bit for excluding
363 * directories from cleaning like we do it for
364 * other file system objects: well, the sticky
365 * bit already has a meaning for directories,
366 * so we don't want to overload that. */
367
368 if (keep_this_level)
369 continue;
370
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200371 /* Ignore ctime, we change it when deleting */
372 age = MAX(timespec_load(&s.st_mtim),
373 timespec_load(&s.st_atim));
374 if (age >= cutoff)
375 continue;
376
Lukas Nykryna6187d42013-03-01 18:29:59 +0100377 if (i->type != IGNORE_DIRECTORY_PATH || !streq(dent->d_name, p)) {
Lennart Poettering9f6445e32013-12-24 16:39:37 +0100378 log_debug("rmdir '%s'", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200379
Michal Sekletar78a92a52013-01-18 16:13:08 +0100380 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
381 if (errno != ENOENT && errno != ENOTEMPTY) {
Michal Schmidt56f64d92014-11-28 19:29:59 +0100382 log_error_errno(errno, "rmdir(%s): %m", sub_path);
Michal Sekletar78a92a52013-01-18 16:13:08 +0100383 r = -errno;
384 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200385 }
386 }
387
388 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100389 /* Skip files for which the sticky bit is
390 * set. These are semantics we define, and are
391 * unknown elsewhere. See XDG_RUNTIME_DIR
392 * specification for details. */
393 if (s.st_mode & S_ISVTX)
394 continue;
395
Lennart Poettering17b90522011-02-14 21:55:06 +0100396 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200397 if (streq(dent->d_name, ".journal") &&
398 s.st_uid == 0)
399 continue;
400
401 if (streq(dent->d_name, "aquota.user") ||
402 streq(dent->d_name, "aquota.group"))
403 continue;
404 }
405
Lennart Poettering17b90522011-02-14 21:55:06 +0100406 /* Ignore sockets that are listed in /proc/net/unix */
407 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
408 continue;
409
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100410 /* Ignore device nodes */
411 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
412 continue;
413
Lennart Poettering24f3a372012-06-20 09:05:50 +0200414 /* Keep files on this level around if this is
415 * requested */
416 if (keep_this_level)
417 continue;
418
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200419 age = MAX3(timespec_load(&s.st_mtim),
420 timespec_load(&s.st_atim),
421 timespec_load(&s.st_ctim));
422
423 if (age >= cutoff)
424 continue;
425
Lennart Poettering9f6445e32013-12-24 16:39:37 +0100426 log_debug("unlink '%s'", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200427
428 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
429 if (errno != ENOENT) {
Michal Schmidt56f64d92014-11-28 19:29:59 +0100430 log_error_errno(errno, "unlink(%s): %m", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200431 r = -errno;
432 }
433 }
434
435 deleted = true;
436 }
437 }
438
439finish:
440 if (deleted) {
441 /* Restore original directory timestamps */
442 times[0] = ds->st_atim;
443 times[1] = ds->st_mtim;
444
445 if (futimens(dirfd(d), times) < 0)
Michal Schmidt56f64d92014-11-28 19:29:59 +0100446 log_error_errno(errno, "utimensat(%s): %m", p);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200447 }
448
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200449 return r;
450}
451
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200452static int item_set_perms(Item *i, const char *path) {
Michael Olbrich1924a972014-08-17 09:45:00 +0200453 struct stat st;
454 bool st_valid;
455
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200456 assert(i);
457 assert(path);
458
Michael Olbrich1924a972014-08-17 09:45:00 +0200459 st_valid = stat(path, &st) == 0;
460
Michal Schmidt062e01b2011-12-16 18:00:11 +0100461 /* not using i->path directly because it may be a glob */
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200462 if (i->mode_set) {
463 mode_t m = i->mode;
464
Michael Olbrich1924a972014-08-17 09:45:00 +0200465 if (i->mask_perms && st_valid) {
466 if (!(st.st_mode & 0111))
467 m &= ~0111;
468 if (!(st.st_mode & 0222))
469 m &= ~0222;
470 if (!(st.st_mode & 0444))
471 m &= ~0444;
472 if (!S_ISDIR(st.st_mode))
473 m &= ~07000; /* remove sticky/sgid/suid bit, unless directory */
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200474 }
475
Michael Olbrich1924a972014-08-17 09:45:00 +0200476 if (!st_valid || m != (st.st_mode & 07777)) {
Michal Schmidt4a62c712014-11-28 19:57:32 +0100477 if (chmod(path, m) < 0)
478 return log_error_errno(errno, "chmod(%s) failed: %m", path);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100479 }
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200480 }
Michal Schmidt062e01b2011-12-16 18:00:11 +0100481
Michael Olbrich1924a972014-08-17 09:45:00 +0200482 if ((!st_valid || (i->uid != st.st_uid || i->gid != st.st_gid)) &&
483 (i->uid_set || i->gid_set))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100484 if (chown(path,
Lennart Poetteringfed1e722014-11-28 20:51:01 +0100485 i->uid_set ? i->uid : UID_INVALID,
486 i->gid_set ? i->gid : GID_INVALID) < 0) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100487
Michal Schmidt56f64d92014-11-28 19:29:59 +0100488 log_error_errno(errno, "chown(%s) failed: %m", path);
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200489 return -errno;
Michal Schmidt062e01b2011-12-16 18:00:11 +0100490 }
491
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200492 return label_fix(path, false, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100493}
494
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100495static int get_xattrs_from_arg(Item *i) {
496 char *xattr;
497 const char *p;
498 int r;
499
500 assert(i);
501
502 if (!i->argument) {
503 log_error("%s: Argument can't be empty!", i->path);
504 return -EBADMSG;
505 }
506 p = i->argument;
507
508 while ((r = unquote_first_word(&p, &xattr, false)) > 0) {
509 _cleanup_free_ char *tmp = NULL, *name = NULL, *value = NULL;
510 r = split_pair(xattr, "=", &name, &value);
511 if (r < 0) {
512 log_warning("Illegal xattr found: \"%s\" - ignoring.", xattr);
513 free(xattr);
514 continue;
515 }
516 free(xattr);
517 if (streq(name, "") || streq(value, "")) {
518 log_warning("Malformed xattr found: \"%s=%s\" - ignoring.", name, value);
519 continue;
520 }
521 tmp = unquote(value, "\"");
522 if (!tmp)
523 return log_oom();
524 free(value);
525 value = cunescape(tmp);
526 if (!value)
527 return log_oom();
528 if (strv_consume_pair(&i->xattrs, name, value) < 0)
529 return log_oom();
530 name = value = NULL;
531 }
532
533 return r;
534}
535
536static int item_set_xattrs(Item *i, const char *path) {
537 char **name, **value;
538
539 assert(i);
540 assert(path);
541
542 if (strv_isempty(i->xattrs))
543 return 0;
544
545 STRV_FOREACH_PAIR(name, value, i->xattrs) {
546 int n;
547 n = strlen(*value);
548 if (lsetxattr(path, *name, *value, n, 0) < 0) {
549 log_error("Setting extended attribute %s=%s on %s failed: %m", *name, *value, path);
550 return -errno;
551 }
552 }
553 return 0;
554}
555
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400556static int write_one_file(Item *i, const char *path) {
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200557 _cleanup_close_ int fd = -1;
558 int flags, r = 0;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400559 struct stat st;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400560
Lennart Poettering874f1942014-06-10 22:48:56 +0200561 assert(i);
562 assert(path);
563
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200564 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND|O_NOFOLLOW :
565 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC|O_NOFOLLOW : 0;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400566
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200567 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200568 mac_selinux_create_file_prepare(path, S_IFREG);
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200569 fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200570 mac_selinux_create_file_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200571 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400572
573 if (fd < 0) {
574 if (i->type == WRITE_FILE && errno == ENOENT)
575 return 0;
576
Michal Schmidt56f64d92014-11-28 19:29:59 +0100577 log_error_errno(errno, "Failed to create file %s: %m", path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400578 return -errno;
579 }
580
581 if (i->argument) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200582 _cleanup_free_ char *unescaped;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400583 ssize_t n;
584 size_t l;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400585
Dave Reisner54693d92012-09-15 12:58:49 -0400586 unescaped = cunescape(i->argument);
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200587 if (!unescaped)
Dave Reisner54693d92012-09-15 12:58:49 -0400588 return log_oom();
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400589
Dave Reisner54693d92012-09-15 12:58:49 -0400590 l = strlen(unescaped);
591 n = write(fd, unescaped, l);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400592
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400593 if (n < 0 || (size_t) n < l) {
594 log_error("Failed to write file %s: %s", path, n < 0 ? strerror(-n) : "Short write");
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400595 return n < 0 ? n : -EIO;
596 }
597 }
598
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200599 fd = safe_close(fd);
Dave Reisner3612fbc2012-09-12 16:21:00 -0400600
Michal Schmidt4a62c712014-11-28 19:57:32 +0100601 if (stat(path, &st) < 0)
602 return log_error_errno(errno, "stat(%s) failed: %m", path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400603
604 if (!S_ISREG(st.st_mode)) {
605 log_error("%s is not a file.", path);
606 return -EEXIST;
607 }
608
609 r = item_set_perms(i, path);
610 if (r < 0)
611 return r;
612
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100613 r = item_set_xattrs(i, i->path);
614 if (r < 0)
615 return r;
616
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400617 return 0;
618}
619
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200620static int item_set_perms_children(Item *i, const char *path) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200621 _cleanup_closedir_ DIR *d;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200622 int r = 0;
623
624 assert(i);
625 assert(path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100626
627 /* This returns the first error we run into, but nevertheless
628 * tries to go on */
629
630 d = opendir(path);
631 if (!d)
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200632 return errno == ENOENT || errno == ENOTDIR ? 0 : -errno;
Michal Schmidta8d88782011-12-15 23:11:07 +0100633
634 for (;;) {
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200635 _cleanup_free_ char *p = NULL;
Lennart Poettering7d5e9c02012-09-19 22:21:09 +0200636 struct dirent *de;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200637 int q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100638
Florian Weimerd78096b2013-12-19 12:26:07 +0100639 errno = 0;
640 de = readdir(d);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200641 if (!de) {
642 if (errno != 0 && r == 0)
643 r = -errno;
644
Michal Schmidta8d88782011-12-15 23:11:07 +0100645 break;
646 }
647
Michal Schmidta8d88782011-12-15 23:11:07 +0100648 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
649 continue;
650
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200651 p = strjoin(path, "/", de->d_name, NULL);
652 if (!p)
653 return -ENOMEM;
Michal Schmidta8d88782011-12-15 23:11:07 +0100654
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200655 q = item_set_perms(i, p);
656 if (q < 0 && q != -ENOENT && r == 0)
657 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100658
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200659 if (IN_SET(de->d_type, DT_UNKNOWN, DT_DIR)) {
660 q = item_set_perms_children(i, p);
661 if (q < 0 && r == 0)
662 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100663 }
Michal Schmidta8d88782011-12-15 23:11:07 +0100664 }
665
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200666 return r;
Michal Schmidta8d88782011-12-15 23:11:07 +0100667}
668
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200669static int item_set_perms_recursive(Item *i, const char *path) {
670 int r, q;
671
672 assert(i);
673 assert(path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100674
Michal Schmidt062e01b2011-12-16 18:00:11 +0100675 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100676 if (r < 0)
677 return r;
678
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200679 q = item_set_perms_children(i, path);
680 if (q < 0 && r == 0)
681 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100682
683 return r;
684}
685
Michal Schmidt99e68c02011-12-15 23:45:26 +0100686static int glob_item(Item *i, int (*action)(Item *, const char *)) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200687 _cleanup_globfree_ glob_t g = {};
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200688 int r = 0, k;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100689 char **fn;
690
Michal Schmidt99e68c02011-12-15 23:45:26 +0100691 errno = 0;
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400692 k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200693 if (k != 0 && k != GLOB_NOMATCH) {
694 if (errno == 0)
695 errno = EIO;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100696
Michal Schmidt56f64d92014-11-28 19:29:59 +0100697 log_error_errno(errno, "glob(%s) failed: %m", i->path);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200698 return -errno;
699 }
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400700
701 STRV_FOREACH(fn, g.gl_pathv) {
702 k = action(i, *fn);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200703 if (k < 0 && r == 0)
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400704 r = k;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100705 }
706
Michal Schmidt99e68c02011-12-15 23:45:26 +0100707 return r;
708}
709
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200710static int create_item(Item *i) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200711 struct stat st;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200712 int r = 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200713
714 assert(i);
715
716 switch (i->type) {
717
718 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100719 case IGNORE_DIRECTORY_PATH:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200720 case REMOVE_PATH:
721 case RECURSIVE_REMOVE_PATH:
722 return 0;
723
724 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100725 case TRUNCATE_FILE:
Dave Reisner1845fdd2012-09-27 20:48:13 -0400726 r = write_one_file(i, i->path);
727 if (r < 0)
728 return r;
729 break;
Lennart Poettering265ffa12013-09-17 16:33:30 -0500730
Lennart Poettering849958d2014-06-10 23:02:40 +0200731 case COPY_FILES:
Lennart Poetteringe1563472014-06-19 19:36:08 +0200732 r = copy_tree(i->argument, i->path, false);
Lennart Poettering849958d2014-06-10 23:02:40 +0200733 if (r < 0) {
Lennart Poetteringe1563472014-06-19 19:36:08 +0200734 struct stat a, b;
735
Michal Schmidt8d3d7072014-11-28 19:13:53 +0100736 if (r != -EEXIST)
737 return log_error_errno(r, "Failed to copy files to %s: %m", i->path);
Lennart Poetteringe1563472014-06-19 19:36:08 +0200738
Michal Schmidt4a62c712014-11-28 19:57:32 +0100739 if (stat(i->argument, &a) < 0)
740 return log_error_errno(errno, "stat(%s) failed: %m", i->argument);
Lennart Poetteringe1563472014-06-19 19:36:08 +0200741
Michal Schmidt4a62c712014-11-28 19:57:32 +0100742 if (stat(i->path, &b) < 0)
743 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Lennart Poetteringe1563472014-06-19 19:36:08 +0200744
745 if ((a.st_mode ^ b.st_mode) & S_IFMT) {
746 log_debug("Can't copy to %s, file exists already and is of different type", i->path);
747 return 0;
748 }
Lennart Poettering849958d2014-06-10 23:02:40 +0200749 }
750
751 r = item_set_perms(i, i->path);
752 if (r < 0)
753 return r;
754
755 break;
756
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400757 case WRITE_FILE:
758 r = glob_item(i, write_one_file);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100759 if (r < 0)
760 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200761
762 break;
763
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200764 case CREATE_DIRECTORY:
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +0100765 case TRUNCATE_DIRECTORY:
766 case CREATE_SUBVOLUME:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200767
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +0100768 RUN_WITH_UMASK(0000)
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200769 mkdir_parents_label(i->path, 0755);
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +0100770
771 if (i->type == CREATE_SUBVOLUME) {
772 RUN_WITH_UMASK((~i->mode) & 0777)
773 r = btrfs_subvol_make(i->path);
774 } else
775 r = 0;
776
777 if (i->type == CREATE_DIRECTORY || r == -ENOTTY) {
778 RUN_WITH_UMASK(0000)
779 r = mkdir_label(i->path, i->mode);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200780 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200781
Lennart Poetteringe1563472014-06-19 19:36:08 +0200782 if (r < 0) {
Michal Schmidtf6479622014-11-28 18:50:43 +0100783 if (r != -EEXIST)
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +0100784 return log_error_errno(r, "Failed to create directory or subvolume %s: %m", i->path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200785
Michal Schmidt4a62c712014-11-28 19:57:32 +0100786 if (stat(i->path, &st) < 0)
787 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200788
Lennart Poetteringe1563472014-06-19 19:36:08 +0200789 if (!S_ISDIR(st.st_mode)) {
790 log_debug("%s already exists and is not a directory.", i->path);
791 return 0;
792 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200793 }
794
Michal Schmidt062e01b2011-12-16 18:00:11 +0100795 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100796 if (r < 0)
797 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200798
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100799 r = item_set_xattrs(i, i->path);
800 if (r < 0)
801 return r;
802
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200803 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200804
805 case CREATE_FIFO:
806
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200807 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200808 mac_selinux_create_file_prepare(i->path, S_IFIFO);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200809 r = mkfifo(i->path, i->mode);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200810 mac_selinux_create_file_clear();
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200811 }
812
Lennart Poettering1554afa2014-06-17 23:50:22 +0200813 if (r < 0) {
Michal Schmidt4a62c712014-11-28 19:57:32 +0100814 if (errno != EEXIST)
815 return log_error_errno(errno, "Failed to create fifo %s: %m", i->path);
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200816
Michal Schmidt4a62c712014-11-28 19:57:32 +0100817 if (stat(i->path, &st) < 0)
818 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200819
820 if (!S_ISFIFO(st.st_mode)) {
821
822 if (i->force) {
823
824 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200825 mac_selinux_create_file_prepare(i->path, S_IFIFO);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200826 r = mkfifo_atomic(i->path, i->mode);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200827 mac_selinux_create_file_clear();
Lennart Poettering1554afa2014-06-17 23:50:22 +0200828 }
829
Michal Schmidtf6479622014-11-28 18:50:43 +0100830 if (r < 0)
831 return log_error_errno(r, "Failed to create fifo %s: %m", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200832 } else {
833 log_debug("%s is not a fifo.", i->path);
834 return 0;
835 }
836 }
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200837 }
838
Michal Schmidt062e01b2011-12-16 18:00:11 +0100839 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100840 if (r < 0)
841 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200842
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100843 r = item_set_xattrs(i, i->path);
844 if (r < 0)
845 return r;
846
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200847 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100848
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200849 case CREATE_SYMLINK:
Lennart Poettering468d7262012-01-17 15:04:12 +0100850
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200851 mac_selinux_create_file_prepare(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100852 r = symlink(i->argument, i->path);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200853 mac_selinux_create_file_clear();
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200854
Lennart Poettering468d7262012-01-17 15:04:12 +0100855 if (r < 0) {
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200856 _cleanup_free_ char *x = NULL;
Lennart Poettering468d7262012-01-17 15:04:12 +0100857
Michal Schmidt4a62c712014-11-28 19:57:32 +0100858 if (errno != EEXIST)
859 return log_error_errno(errno, "symlink(%s, %s) failed: %m", i->argument, i->path);
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200860
861 r = readlink_malloc(i->path, &x);
862 if (r < 0 || !streq(i->argument, x)) {
863
864 if (i->force) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200865 mac_selinux_create_file_prepare(i->path, S_IFLNK);
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200866 r = symlink_atomic(i->argument, i->path);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200867 mac_selinux_create_file_clear();
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200868
Michal Schmidtf6479622014-11-28 18:50:43 +0100869 if (r < 0)
870 return log_error_errno(r, "symlink(%s, %s) failed: %m", i->argument, i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200871 } else {
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200872 log_debug("%s is not a symlink or does not point to the correct path.", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200873 return 0;
874 }
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200875 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100876 }
877
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100878 r = item_set_xattrs(i, i->path);
879 if (r < 0)
880 return r;
881
Lennart Poettering468d7262012-01-17 15:04:12 +0100882 break;
Lennart Poettering468d7262012-01-17 15:04:12 +0100883
884 case CREATE_BLOCK_DEVICE:
885 case CREATE_CHAR_DEVICE: {
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700886 mode_t file_type;
887
888 if (have_effective_cap(CAP_MKNOD) == 0) {
889 /* In a container we lack CAP_MKNOD. We
Anatol Pomozovab06eef2013-04-14 19:37:54 -0700890 shouldn't attempt to create the device node in
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700891 that case to avoid noise, and we don't support
892 virtualized devices in containers anyway. */
893
894 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
895 return 0;
896 }
897
Lennart Poettering1554afa2014-06-17 23:50:22 +0200898 file_type = i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR;
Lennart Poettering468d7262012-01-17 15:04:12 +0100899
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200900 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200901 mac_selinux_create_file_prepare(i->path, file_type);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200902 r = mknod(i->path, i->mode | file_type, i->major_minor);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200903 mac_selinux_create_file_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200904 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100905
Kay Sievers6555ad82014-06-13 04:11:11 +0200906 if (r < 0) {
907 if (errno == EPERM) {
908 log_debug("We lack permissions, possibly because of cgroup configuration; "
909 "skipping creation of device node %s.", i->path);
910 return 0;
911 }
912
Michal Schmidt4a62c712014-11-28 19:57:32 +0100913 if (errno != EEXIST)
914 return log_error_errno(errno, "Failed to create device node %s: %m", i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100915
Michal Schmidt4a62c712014-11-28 19:57:32 +0100916 if (stat(i->path, &st) < 0)
917 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100918
Lennart Poettering1554afa2014-06-17 23:50:22 +0200919 if ((st.st_mode & S_IFMT) != file_type) {
920
921 if (i->force) {
922
923 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200924 mac_selinux_create_file_prepare(i->path, file_type);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200925 r = mknod_atomic(i->path, i->mode | file_type, i->major_minor);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200926 mac_selinux_create_file_clear();
Lennart Poettering1554afa2014-06-17 23:50:22 +0200927 }
928
Michal Schmidtf6479622014-11-28 18:50:43 +0100929 if (r < 0)
930 return log_error_errno(r, "Failed to create device node %s: %m", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200931 } else {
932 log_debug("%s is not a device node.", i->path);
933 return 0;
934 }
935 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100936 }
937
938 r = item_set_perms(i, i->path);
939 if (r < 0)
940 return r;
941
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100942 r = item_set_xattrs(i, i->path);
943 if (r < 0)
944 return r;
945
Lennart Poettering468d7262012-01-17 15:04:12 +0100946 break;
947 }
948
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200949 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100950 case RELABEL_PATH:
951
952 r = glob_item(i, item_set_perms);
953 if (r < 0)
Lennart Poettering96ca8192013-06-21 15:57:42 +0200954 return r;
Michal Schmidt777b87e2011-12-16 18:27:35 +0100955 break;
956
Michal Schmidta8d88782011-12-15 23:11:07 +0100957 case RECURSIVE_RELABEL_PATH:
958
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200959 r = glob_item(i, item_set_perms_recursive);
Michal Schmidta8d88782011-12-15 23:11:07 +0100960 if (r < 0)
961 return r;
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100962 break;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200963
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100964 case SET_XATTR:
965 r = item_set_xattrs(i, i->path);
966 if (r < 0)
967 return r;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200968 break;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200969 }
970
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200971 log_debug("%s created successfully.", i->path);
972
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100973 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200974}
975
Michal Schmidta0896122011-12-15 21:32:50 +0100976static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200977 int r;
978
979 assert(i);
980
981 switch (i->type) {
982
983 case CREATE_FILE:
984 case TRUNCATE_FILE:
985 case CREATE_DIRECTORY:
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +0100986 case CREATE_SUBVOLUME:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200987 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100988 case CREATE_SYMLINK:
989 case CREATE_BLOCK_DEVICE:
990 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200991 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100992 case IGNORE_DIRECTORY_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200993 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100994 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100995 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100996 case WRITE_FILE:
Lennart Poettering849958d2014-06-10 23:02:40 +0200997 case COPY_FILES:
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100998 case SET_XATTR:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200999 break;
1000
1001 case REMOVE_PATH:
Michal Schmidt4a62c712014-11-28 19:57:32 +01001002 if (remove(instance) < 0 && errno != ENOENT)
1003 return log_error_errno(errno, "remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001004
1005 break;
1006
1007 case TRUNCATE_DIRECTORY:
1008 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +02001009 /* FIXME: we probably should use dir_cleanup() here
1010 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +02001011 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Michal Schmidtf6479622014-11-28 18:50:43 +01001012 if (r < 0 && r != -ENOENT)
1013 return log_error_errno(r, "rm_rf(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001014
1015 break;
1016 }
1017
1018 return 0;
1019}
1020
Michal Schmidta0896122011-12-15 21:32:50 +01001021static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +01001022 int r = 0;
1023
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001024 assert(i);
1025
1026 switch (i->type) {
1027
1028 case CREATE_FILE:
1029 case TRUNCATE_FILE:
1030 case CREATE_DIRECTORY:
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001031 case CREATE_SUBVOLUME:
Lennart Poetteringee17ee72011-07-12 03:56:56 +02001032 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +01001033 case CREATE_SYMLINK:
1034 case CREATE_CHAR_DEVICE:
1035 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001036 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001037 case IGNORE_DIRECTORY_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001038 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001039 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +01001040 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001041 case WRITE_FILE:
Lennart Poettering849958d2014-06-10 23:02:40 +02001042 case COPY_FILES:
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001043 case SET_XATTR:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001044 break;
1045
1046 case REMOVE_PATH:
1047 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +01001048 case RECURSIVE_REMOVE_PATH:
1049 r = glob_item(i, remove_item_instance);
1050 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001051 }
1052
Michal Schmidt99e68c02011-12-15 23:45:26 +01001053 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001054}
1055
Michal Sekletar78a92a52013-01-18 16:13:08 +01001056static int clean_item_instance(Item *i, const char* instance) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +02001057 _cleanup_closedir_ DIR *d = NULL;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001058 struct stat s, ps;
1059 bool mountpoint;
1060 int r;
1061 usec_t cutoff, n;
1062
1063 assert(i);
1064
1065 if (!i->age_set)
1066 return 0;
1067
1068 n = now(CLOCK_REALTIME);
1069 if (n < i->age)
1070 return 0;
1071
1072 cutoff = n - i->age;
1073
1074 d = opendir(instance);
1075 if (!d) {
1076 if (errno == ENOENT || errno == ENOTDIR)
1077 return 0;
1078
Michal Schmidt56f64d92014-11-28 19:29:59 +01001079 log_error_errno(errno, "Failed to open directory %s: %m", i->path);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001080 return -errno;
1081 }
1082
Michal Schmidt4a62c712014-11-28 19:57:32 +01001083 if (fstat(dirfd(d), &s) < 0)
1084 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001085
1086 if (!S_ISDIR(s.st_mode)) {
1087 log_error("%s is not a directory.", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001088 return -ENOTDIR;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001089 }
1090
Michal Schmidt4a62c712014-11-28 19:57:32 +01001091 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0)
1092 return log_error_errno(errno, "stat(%s/..) failed: %m", i->path);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001093
1094 mountpoint = s.st_dev != ps.st_dev ||
1095 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
1096
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001097 r = dir_cleanup(i, instance, d, &s, cutoff, s.st_dev, mountpoint,
1098 MAX_DEPTH, i->keep_first_level);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001099 return r;
1100}
1101
1102static int clean_item(Item *i) {
1103 int r = 0;
1104
1105 assert(i);
1106
1107 switch (i->type) {
1108 case CREATE_DIRECTORY:
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001109 case CREATE_SUBVOLUME:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001110 case TRUNCATE_DIRECTORY:
1111 case IGNORE_PATH:
Lennart Poettering849958d2014-06-10 23:02:40 +02001112 case COPY_FILES:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001113 clean_item_instance(i, i->path);
1114 break;
1115 case IGNORE_DIRECTORY_PATH:
1116 r = glob_item(i, clean_item_instance);
1117 break;
1118 default:
1119 break;
1120 }
1121
1122 return r;
1123}
1124
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001125static int process_item(Item *i) {
1126 int r, q, p;
Zbigniew Jędrzejewski-Szmek9348f0e2014-10-01 07:33:22 -05001127 _cleanup_free_ char *prefix = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001128
1129 assert(i);
1130
Lennart Poettering1910cd02014-06-11 01:37:35 +02001131 if (i->done)
1132 return 0;
1133
1134 i->done = true;
1135
Zbigniew Jędrzejewski-Szmek9348f0e2014-10-01 07:33:22 -05001136 prefix = malloc(strlen(i->path) + 1);
1137 if (!prefix)
1138 return log_oom();
1139
Lennart Poettering1910cd02014-06-11 01:37:35 +02001140 PATH_FOREACH_PREFIX(prefix, i->path) {
1141 Item *j;
1142
1143 j = hashmap_get(items, prefix);
1144 if (j)
1145 process_item(j);
1146 }
1147
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001148 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +01001149 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001150 p = arg_clean ? clean_item(i) : 0;
1151
1152 if (r < 0)
1153 return r;
1154
1155 if (q < 0)
1156 return q;
1157
1158 return p;
1159}
1160
1161static void item_free(Item *i) {
Lennart Poettering753615e2014-06-12 23:07:17 +02001162
1163 if (!i)
1164 return;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001165
1166 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +01001167 free(i->argument);
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001168 strv_free(i->xattrs);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001169 free(i);
1170}
1171
Lennart Poettering14bf2c92013-10-14 04:59:26 +02001172DEFINE_TRIVIAL_CLEANUP_FUNC(Item*, item_free);
Maciej Wereskie2f2fb72013-07-19 15:43:12 +02001173
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001174static bool item_equal(Item *a, Item *b) {
1175 assert(a);
1176 assert(b);
1177
1178 if (!streq_ptr(a->path, b->path))
1179 return false;
1180
1181 if (a->type != b->type)
1182 return false;
1183
1184 if (a->uid_set != b->uid_set ||
1185 (a->uid_set && a->uid != b->uid))
1186 return false;
1187
1188 if (a->gid_set != b->gid_set ||
1189 (a->gid_set && a->gid != b->gid))
1190 return false;
1191
1192 if (a->mode_set != b->mode_set ||
1193 (a->mode_set && a->mode != b->mode))
1194 return false;
1195
1196 if (a->age_set != b->age_set ||
1197 (a->age_set && a->age != b->age))
1198 return false;
1199
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001200 if ((a->type == CREATE_FILE ||
1201 a->type == TRUNCATE_FILE ||
1202 a->type == WRITE_FILE ||
Lennart Poettering849958d2014-06-10 23:02:40 +02001203 a->type == CREATE_SYMLINK ||
1204 a->type == COPY_FILES) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +01001205 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +01001206 return false;
1207
1208 if ((a->type == CREATE_CHAR_DEVICE ||
1209 a->type == CREATE_BLOCK_DEVICE) &&
1210 a->major_minor != b->major_minor)
1211 return false;
1212
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001213 return true;
1214}
1215
Dave Reisnera2aced42013-07-24 11:10:05 -04001216static bool should_include_path(const char *path) {
1217 char **prefix;
1218
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001219 STRV_FOREACH(prefix, arg_exclude_prefixes)
Dave Reisner5c795112013-07-24 11:19:24 -04001220 if (path_startswith(path, *prefix))
1221 return false;
Dave Reisnera2aced42013-07-24 11:10:05 -04001222
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001223 STRV_FOREACH(prefix, arg_include_prefixes)
Dave Reisnera2aced42013-07-24 11:10:05 -04001224 if (path_startswith(path, *prefix))
1225 return true;
Dave Reisnera2aced42013-07-24 11:10:05 -04001226
Dave Reisner5c795112013-07-24 11:19:24 -04001227 /* no matches, so we should include this path only if we
1228 * have no whitelist at all */
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001229 return strv_length(arg_include_prefixes) == 0;
Dave Reisnera2aced42013-07-24 11:10:05 -04001230}
1231
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001232static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001233
1234 static const Specifier specifier_table[] = {
1235 { 'm', specifier_machine_id, NULL },
1236 { 'b', specifier_boot_id, NULL },
1237 { 'H', specifier_host_name, NULL },
1238 { 'v', specifier_kernel_release, NULL },
1239 {}
1240 };
1241
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001242 _cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
1243 _cleanup_(item_freep) Item *i = NULL;
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001244 Item *existing;
Michal Schmidt66ccd032011-12-15 21:31:14 +01001245 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001246 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001247 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +02001248
1249 assert(fname);
1250 assert(line >= 1);
1251 assert(buffer);
1252
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001253 r = sscanf(buffer,
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001254 "%ms %ms %ms %ms %ms %ms %n",
1255 &action,
Lennart Poettering1731e342013-09-17 11:02:02 -05001256 &path,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +01001257 &mode,
1258 &user,
1259 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +01001260 &age,
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001261 &n);
1262 if (r < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001263 log_error("[%s:%u] Syntax error.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001264 return -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +02001265 }
1266
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001267 if (isempty(action)) {
1268 log_error("[%s:%u] Command too short '%s'.", fname, line, action);
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001269 return -EINVAL;
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001270 }
1271
1272 if (strlen(action) > 1 && !in_charset(action+1, "!+")) {
1273 log_error("[%s:%u] Unknown modifiers in command '%s'", fname, line, action);
1274 return -EINVAL;
1275 }
1276
1277 if (strchr(action+1, '!') && !arg_boot)
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001278 return 0;
1279
1280 type = action[0];
1281
Lennart Poettering1731e342013-09-17 11:02:02 -05001282 i = new0(Item, 1);
1283 if (!i)
1284 return log_oom();
1285
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001286 i->force = !!strchr(action+1, '+');
1287
Lennart Poettering1731e342013-09-17 11:02:02 -05001288 r = specifier_printf(path, specifier_table, NULL, &i->path);
1289 if (r < 0) {
1290 log_error("[%s:%u] Failed to replace specifiers: %s", fname, line, path);
1291 return r;
1292 }
1293
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001294 if (n >= 0) {
1295 n += strspn(buffer+n, WHITESPACE);
1296 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
1297 i->argument = unquote(buffer+n, "\"");
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001298 if (!i->argument)
1299 return log_oom();
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001300 }
1301 }
1302
Lennart Poettering753615e2014-06-12 23:07:17 +02001303 switch (type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001304
Michal Schmidt777b87e2011-12-16 18:27:35 +01001305 case CREATE_FILE:
1306 case TRUNCATE_FILE:
1307 case CREATE_DIRECTORY:
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001308 case CREATE_SUBVOLUME:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001309 case TRUNCATE_DIRECTORY:
1310 case CREATE_FIFO:
1311 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001312 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001313 case REMOVE_PATH:
1314 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001315 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001316 case RELABEL_PATH:
1317 case RECURSIVE_RELABEL_PATH:
1318 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001319
1320 case CREATE_SYMLINK:
1321 if (!i->argument) {
Kay Sievers2f3b8732014-06-20 15:57:43 +02001322 i->argument = strappend("/usr/share/factory", i->path);
1323 if (!i->argument)
1324 return log_oom();
Lennart Poettering468d7262012-01-17 15:04:12 +01001325 }
1326 break;
1327
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001328 case WRITE_FILE:
1329 if (!i->argument) {
1330 log_error("[%s:%u] Write file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001331 return -EBADMSG;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001332 }
1333 break;
1334
Lennart Poettering849958d2014-06-10 23:02:40 +02001335 case COPY_FILES:
1336 if (!i->argument) {
Kay Sievers2f3b8732014-06-20 15:57:43 +02001337 i->argument = strappend("/usr/share/factory", i->path);
1338 if (!i->argument)
1339 return log_oom();
Lennart Poettering849958d2014-06-10 23:02:40 +02001340 }
1341
1342 if (!path_is_absolute(i->argument)) {
1343 log_error("[%s:%u] Source path is not absolute.", fname, line);
1344 return -EBADMSG;
1345 }
1346
1347 path_kill_slashes(i->argument);
1348 break;
1349
Lennart Poettering468d7262012-01-17 15:04:12 +01001350 case CREATE_CHAR_DEVICE:
1351 case CREATE_BLOCK_DEVICE: {
1352 unsigned major, minor;
1353
1354 if (!i->argument) {
1355 log_error("[%s:%u] Device file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001356 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001357 }
1358
1359 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1360 log_error("[%s:%u] Can't parse device file major/minor '%s'.", fname, line, i->argument);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001361 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001362 }
1363
1364 i->major_minor = makedev(major, minor);
1365 break;
1366 }
1367
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001368 case SET_XATTR:
1369 if (!i->argument) {
1370 log_error("[%s:%u] Set extended attribute requires argument.", fname, line);
1371 return -EBADMSG;
1372 }
1373 r = get_xattrs_from_arg(i);
1374 if (r < 0)
1375 return r;
1376 break;
1377
Michal Schmidt777b87e2011-12-16 18:27:35 +01001378 default:
Lennart Poettering753615e2014-06-12 23:07:17 +02001379 log_error("[%s:%u] Unknown command type '%c'.", fname, line, type);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001380 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001381 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001382
Michal Schmidta8d88782011-12-15 23:11:07 +01001383 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001384
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001385 if (!path_is_absolute(i->path)) {
1386 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001387 return -EBADMSG;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001388 }
1389
1390 path_kill_slashes(i->path);
1391
Dave Reisnera2aced42013-07-24 11:10:05 -04001392 if (!should_include_path(i->path))
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001393 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001394
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001395 if (arg_root) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001396 char *p;
1397
1398 p = strappend(arg_root, i->path);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001399 if (!p)
1400 return log_oom();
1401
1402 free(i->path);
1403 i->path = p;
1404 }
1405
Lennart Poettering5008d582010-09-28 02:34:02 +02001406 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001407 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001408
Lennart Poetteringd05c5032012-07-16 12:34:54 +02001409 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001410 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001411 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001412 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001413 }
1414
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001415 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001416 }
1417
1418 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001419 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001420
Lennart Poettering4b678342011-07-23 01:17:59 +02001421 r = get_group_creds(&g, &i->gid);
1422 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001423 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001424 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001425 }
1426
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001427 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001428 }
1429
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001430 if (mode && !streq(mode, "-")) {
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001431 const char *mm = mode;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001432 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001433
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001434 if (*mm == '~') {
1435 i->mask_perms = true;
1436 mm++;
1437 }
1438
1439 if (sscanf(mm, "%o", &m) != 1) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001440 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001441 return -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001442 }
1443
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001444 i->mode = m;
1445 i->mode_set = true;
1446 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001447 i->mode =
1448 i->type == CREATE_DIRECTORY ||
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001449 i->type == CREATE_SUBVOLUME ||
Lennart Poettering468d7262012-01-17 15:04:12 +01001450 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001451
1452 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001453 const char *a = age;
1454
1455 if (*a == '~') {
1456 i->keep_first_level = true;
1457 a++;
1458 }
1459
Lennart Poettering7f602782013-04-02 20:38:16 +02001460 if (parse_sec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001461 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001462 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001463 }
1464
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001465 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001466 }
1467
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001468 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001469
Lennart Poettering468d7262012-01-17 15:04:12 +01001470 existing = hashmap_get(h, i->path);
1471 if (existing) {
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001472 if (i->type == SET_XATTR) {
1473 r = strv_extend_strv(&existing->xattrs, i->xattrs);
1474 if (r < 0)
1475 return log_oom();
1476 return 0;
1477 } else if (existing->type == SET_XATTR) {
1478 r = strv_extend_strv(&i->xattrs, existing->xattrs);
1479 if (r < 0)
1480 return log_oom();
1481 r = hashmap_replace(h, i->path, i);
1482 if (r < 0) {
1483 log_error("Failed to replace item for %s.", i->path);
1484 return r;
1485 }
1486 item_free(existing);
1487 } else {
1488 /* Two identical items are fine */
1489 if (!item_equal(existing, i))
1490 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1491 return 0;
1492 }
1493 } else {
1494 r = hashmap_put(h, i->path, i);
1495 if (r < 0) {
1496 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
1497 return r;
1498 }
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001499 }
1500
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001501 i = NULL; /* avoid cleanup */
Lennart Poettering5008d582010-09-28 02:34:02 +02001502
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001503 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001504}
1505
Zbigniew Jędrzejewski-Szmek601185b2014-08-02 11:12:21 -04001506static void help(void) {
Lennart Poettering522d4a42011-02-13 15:08:15 +01001507 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1508 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001509 " -h --help Show this help\n"
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001510 " --version Show package version\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001511 " --create Create marked files/directories\n"
1512 " --clean Clean up marked directories\n"
1513 " --remove Remove marked files/directories\n"
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001514 " --boot Execute actions only safe at boot\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001515 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n"
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001516 " --exclude-prefix=PATH Ignore rules that apply to paths with the specified prefix\n"
1517 " --root=PATH Operate on an alternate filesystem root\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001518 program_invocation_short_name);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001519}
1520
1521static int parse_argv(int argc, char *argv[]) {
1522
1523 enum {
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001524 ARG_VERSION = 0x100,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001525 ARG_CREATE,
1526 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001527 ARG_REMOVE,
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001528 ARG_BOOT,
Dave Reisner5c795112013-07-24 11:19:24 -04001529 ARG_PREFIX,
1530 ARG_EXCLUDE_PREFIX,
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001531 ARG_ROOT,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001532 };
1533
1534 static const struct option options[] = {
Dave Reisner5c795112013-07-24 11:19:24 -04001535 { "help", no_argument, NULL, 'h' },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001536 { "version", no_argument, NULL, ARG_VERSION },
Dave Reisner5c795112013-07-24 11:19:24 -04001537 { "create", no_argument, NULL, ARG_CREATE },
1538 { "clean", no_argument, NULL, ARG_CLEAN },
1539 { "remove", no_argument, NULL, ARG_REMOVE },
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001540 { "boot", no_argument, NULL, ARG_BOOT },
Dave Reisner5c795112013-07-24 11:19:24 -04001541 { "prefix", required_argument, NULL, ARG_PREFIX },
1542 { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX },
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001543 { "root", required_argument, NULL, ARG_ROOT },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001544 {}
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001545 };
1546
1547 int c;
1548
1549 assert(argc >= 0);
1550 assert(argv);
1551
Zbigniew Jędrzejewski-Szmek601185b2014-08-02 11:12:21 -04001552 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001553
1554 switch (c) {
1555
1556 case 'h':
Zbigniew Jędrzejewski-Szmek601185b2014-08-02 11:12:21 -04001557 help();
1558 return 0;
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001559
1560 case ARG_VERSION:
1561 puts(PACKAGE_STRING);
1562 puts(SYSTEMD_FEATURES);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001563 return 0;
1564
1565 case ARG_CREATE:
1566 arg_create = true;
1567 break;
1568
1569 case ARG_CLEAN:
1570 arg_clean = true;
1571 break;
1572
1573 case ARG_REMOVE:
1574 arg_remove = true;
1575 break;
1576
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001577 case ARG_BOOT:
1578 arg_boot = true;
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001579 break;
1580
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001581 case ARG_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001582 if (strv_push(&arg_include_prefixes, optarg) < 0)
Dave Reisnera2aced42013-07-24 11:10:05 -04001583 return log_oom();
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001584 break;
1585
Dave Reisner5c795112013-07-24 11:19:24 -04001586 case ARG_EXCLUDE_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001587 if (strv_push(&arg_exclude_prefixes, optarg) < 0)
Dave Reisner5c795112013-07-24 11:19:24 -04001588 return log_oom();
1589 break;
1590
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001591 case ARG_ROOT:
Lennart Poettering753615e2014-06-12 23:07:17 +02001592 free(arg_root);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001593 arg_root = path_make_absolute_cwd(optarg);
1594 if (!arg_root)
1595 return log_oom();
Lennart Poettering753615e2014-06-12 23:07:17 +02001596
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001597 path_kill_slashes(arg_root);
1598 break;
1599
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001600 case '?':
1601 return -EINVAL;
1602
1603 default:
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001604 assert_not_reached("Unhandled option");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001605 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001606
1607 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001608 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001609 return -EINVAL;
1610 }
1611
1612 return 1;
1613}
1614
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001615static int read_config_file(const char *fn, bool ignore_enoent) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001616 _cleanup_fclose_ FILE *f = NULL;
1617 char line[LINE_MAX];
Michal Sekletar78a92a52013-01-18 16:13:08 +01001618 Iterator iterator;
Lennart Poettering1731e342013-09-17 11:02:02 -05001619 unsigned v = 0;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001620 Item *i;
Lennart Poettering1731e342013-09-17 11:02:02 -05001621 int r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001622
1623 assert(fn);
1624
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001625 r = search_and_fopen_nulstr(fn, "re", arg_root, conf_file_dirs, &f);
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001626 if (r < 0) {
1627 if (ignore_enoent && r == -ENOENT)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001628 return 0;
1629
Michal Schmidt8d3d7072014-11-28 19:13:53 +01001630 return log_error_errno(r, "Failed to open '%s', ignoring: %m", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001631 }
1632
Lennart Poettering1731e342013-09-17 11:02:02 -05001633 FOREACH_LINE(line, f, break) {
1634 char *l;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001635 int k;
1636
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001637 v++;
1638
1639 l = strstrip(line);
1640 if (*l == '#' || *l == 0)
1641 continue;
1642
Lennart Poettering1731e342013-09-17 11:02:02 -05001643 k = parse_line(fn, v, l);
1644 if (k < 0 && r == 0)
1645 r = k;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001646 }
1647
Michal Sekletar78a92a52013-01-18 16:13:08 +01001648 /* we have to determine age parameter for each entry of type X */
1649 HASHMAP_FOREACH(i, globs, iterator) {
1650 Iterator iter;
1651 Item *j, *candidate_item = NULL;
1652
1653 if (i->type != IGNORE_DIRECTORY_PATH)
1654 continue;
1655
1656 HASHMAP_FOREACH(j, items, iter) {
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001657 if (j->type != CREATE_DIRECTORY && j->type != TRUNCATE_DIRECTORY && j->type != CREATE_SUBVOLUME)
Michal Sekletar78a92a52013-01-18 16:13:08 +01001658 continue;
1659
1660 if (path_equal(j->path, i->path)) {
1661 candidate_item = j;
1662 break;
1663 }
1664
1665 if ((!candidate_item && path_startswith(i->path, j->path)) ||
1666 (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
1667 candidate_item = j;
1668 }
1669
Richard Weinberger9ed2a352014-09-09 11:09:37 +02001670 if (candidate_item && candidate_item->age_set) {
Michal Sekletar78a92a52013-01-18 16:13:08 +01001671 i->age = candidate_item->age;
1672 i->age_set = true;
1673 }
1674 }
1675
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001676 if (ferror(f)) {
Michal Schmidt56f64d92014-11-28 19:29:59 +01001677 log_error_errno(errno, "Failed to read from file %s: %m", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001678 if (r == 0)
1679 r = -EIO;
1680 }
1681
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001682 return r;
1683}
1684
Lennart Poettering5008d582010-09-28 02:34:02 +02001685int main(int argc, char *argv[]) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001686 int r, k;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001687 Item *i;
1688 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001689
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001690 r = parse_argv(argc, argv);
1691 if (r <= 0)
Lennart Poettering753615e2014-06-12 23:07:17 +02001692 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001693
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001694 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001695 log_parse_environment();
1696 log_open();
1697
Lennart Poettering4c126262011-08-01 20:52:18 +02001698 umask(0022);
1699
WaLyong Chocc56faf2014-10-23 17:23:46 +09001700 mac_selinux_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001701
Michal Schmidtd5099ef2014-08-13 01:00:18 +02001702 items = hashmap_new(&string_hash_ops);
1703 globs = hashmap_new(&string_hash_ops);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001704
1705 if (!items || !globs) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001706 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001707 goto finish;
1708 }
1709
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001710 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001711
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001712 if (optind < argc) {
1713 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001714
Dave Reisner91256702012-06-08 22:31:19 -04001715 for (j = optind; j < argc; j++) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001716 k = read_config_file(argv[j], false);
1717 if (k < 0 && r == 0)
1718 r = k;
Dave Reisner91256702012-06-08 22:31:19 -04001719 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001720
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001721 } else {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001722 _cleanup_strv_free_ char **files = NULL;
1723 char **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001724
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001725 r = conf_files_list_nulstr(&files, ".conf", arg_root, conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001726 if (r < 0) {
Michal Schmidtda927ba2014-11-28 13:19:16 +01001727 log_error_errno(r, "Failed to enumerate tmpfiles.d files: %m");
Kay Sievers44143302011-04-28 23:51:24 +02001728 goto finish;
1729 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001730
Kay Sievers772f8372011-04-25 21:38:21 +02001731 STRV_FOREACH(f, files) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001732 k = read_config_file(*f, true);
1733 if (k < 0 && r == 0)
1734 r = k;
Lennart Poettering5008d582010-09-28 02:34:02 +02001735 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001736 }
1737
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001738 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001739 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001740
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001741 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001742 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001743
Lennart Poettering5008d582010-09-28 02:34:02 +02001744finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001745 while ((i = hashmap_steal_first(items)))
1746 item_free(i);
1747
Lennart Poettering17b90522011-02-14 21:55:06 +01001748 while ((i = hashmap_steal_first(globs)))
1749 item_free(i);
1750
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001751 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001752 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001753
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001754 free(arg_include_prefixes);
1755 free(arg_exclude_prefixes);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001756 free(arg_root);
Dave Reisnera2aced42013-07-24 11:10:05 -04001757
Lennart Poettering17b90522011-02-14 21:55:06 +01001758 set_free_free(unix_sockets);
1759
WaLyong Chocc56faf2014-10-23 17:23:46 +09001760 mac_selinux_finish();
Lennart Poettering29003cf2010-10-19 19:36:45 +02001761
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001762 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Lennart Poettering5008d582010-09-28 02:34:02 +02001763}