blob: d40bd96f1b4169fc1f6c55cfcb0eeec42fefc342 [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>
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -070041#include <sys/capability.h>
Maciej Wereskiebf4e802014-12-04 10:32:10 +010042#include <sys/xattr.h>
Lennart Poettering5008d582010-09-28 02:34:02 +020043
44#include "log.h"
45#include "util.h"
Dave Reisner54693d92012-09-15 12:58:49 -040046#include "macro.h"
Zbigniew Jędrzejewski-Szmekd39efe72013-03-13 20:20:54 -040047#include "missing.h"
Kay Sievers49e942b2012-04-10 21:54:31 +020048#include "mkdir.h"
Kay Sievers9eb977d2012-05-07 21:36:12 +020049#include "path-util.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020050#include "strv.h"
51#include "label.h"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020052#include "set.h"
Kay Sievers2c210442012-05-07 18:55:45 +020053#include "conf-files.h"
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -070054#include "capability.h"
Lennart Poettering1731e342013-09-17 11:02:02 -050055#include "specifier.h"
Lennart Poetteringeb9da372013-11-06 18:28:39 +010056#include "build.h"
Lennart Poettering849958d2014-06-10 23:02:40 +020057#include "copy.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020058
Andreas Jaeger01000472010-09-29 10:08:24 +020059/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020060 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020061 * properly owned directories beneath /tmp, /var/tmp, /run, which are
62 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020063
Michal Schmidt66ccd032011-12-15 21:31:14 +010064typedef enum ItemType {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010065 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020066 CREATE_FILE = 'f',
67 TRUNCATE_FILE = 'F',
68 CREATE_DIRECTORY = 'd',
69 TRUNCATE_DIRECTORY = 'D',
Lennart Poetteringee17ee72011-07-12 03:56:56 +020070 CREATE_FIFO = 'p',
Lennart Poettering468d7262012-01-17 15:04:12 +010071 CREATE_SYMLINK = 'L',
72 CREATE_CHAR_DEVICE = 'c',
73 CREATE_BLOCK_DEVICE = 'b',
Lennart Poettering849958d2014-06-10 23:02:40 +020074 COPY_FILES = 'C',
Maciej Wereskiebf4e802014-12-04 10:32:10 +010075 SET_XATTR = 't',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010076
77 /* These ones take globs */
Lennart Poetteringcde684a2014-06-10 22:50:46 +020078 WRITE_FILE = 'w',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020079 IGNORE_PATH = 'x',
Michal Sekletar78a92a52013-01-18 16:13:08 +010080 IGNORE_DIRECTORY_PATH = 'X',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020081 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010082 RECURSIVE_REMOVE_PATH = 'R',
Lennart Poetteringe73a03e2014-06-10 23:42:16 +020083 ADJUST_MODE = 'm', /* legacy, 'z' is identical to this */
Michal Schmidt777b87e2011-12-16 18:27:35 +010084 RELABEL_PATH = 'z',
Lennart Poetteringe73a03e2014-06-10 23:42:16 +020085 RECURSIVE_RELABEL_PATH = 'Z',
Michal Schmidt66ccd032011-12-15 21:31:14 +010086} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020087
88typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +010089 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020090
91 char *path;
Lennart Poettering468d7262012-01-17 15:04:12 +010092 char *argument;
Maciej Wereskiebf4e802014-12-04 10:32:10 +010093 char **xattrs;
Lennart Poettering5008d582010-09-28 02:34:02 +020094 uid_t uid;
95 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020096 mode_t mode;
97 usec_t age;
98
Lennart Poettering468d7262012-01-17 15:04:12 +010099 dev_t major_minor;
100
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200101 bool uid_set:1;
102 bool gid_set:1;
103 bool mode_set:1;
104 bool age_set:1;
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200105 bool mask_perms:1;
Lennart Poettering24f3a372012-06-20 09:05:50 +0200106
107 bool keep_first_level:1;
Lennart Poettering1910cd02014-06-11 01:37:35 +0200108
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200109 bool force:1;
110
Lennart Poettering1910cd02014-06-11 01:37:35 +0200111 bool done:1;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200112} Item;
113
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200114static bool arg_create = false;
115static bool arg_clean = false;
116static bool arg_remove = false;
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -0500117static bool arg_boot = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200118
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200119static char **arg_include_prefixes = NULL;
120static char **arg_exclude_prefixes = NULL;
Michael Marineaucf9a4ab2014-03-13 21:32:13 -0700121static char *arg_root = NULL;
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100122
Josh Triplett7f0a55d2014-10-29 05:02:56 -0700123static const char conf_file_dirs[] = CONF_DIRS_NULSTR("tmpfiles");
Dave Reisner91256702012-06-08 22:31:19 -0400124
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200125#define MAX_DEPTH 256
126
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200127static Hashmap *items = NULL, *globs = NULL;
128static Set *unix_sockets = NULL;
129
Michal Schmidt66ccd032011-12-15 21:31:14 +0100130static bool needs_glob(ItemType t) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200131 return IN_SET(t,
132 WRITE_FILE,
133 IGNORE_PATH,
134 IGNORE_DIRECTORY_PATH,
135 REMOVE_PATH,
136 RECURSIVE_REMOVE_PATH,
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200137 ADJUST_MODE,
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200138 RELABEL_PATH,
139 RECURSIVE_RELABEL_PATH);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100140}
141
142static struct Item* find_glob(Hashmap *h, const char *match) {
143 Item *j;
144 Iterator i;
145
146 HASHMAP_FOREACH(j, h, i)
147 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
148 return j;
149
150 return NULL;
151}
152
Lennart Poettering17b90522011-02-14 21:55:06 +0100153static void load_unix_sockets(void) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200154 _cleanup_fclose_ FILE *f = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100155 char line[LINE_MAX];
156
157 if (unix_sockets)
158 return;
159
160 /* We maintain a cache of the sockets we found in
161 * /proc/net/unix to speed things up a little. */
162
Michal Schmidtd5099ef2014-08-13 01:00:18 +0200163 unix_sockets = set_new(&string_hash_ops);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100164 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100165 return;
166
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100167 f = fopen("/proc/net/unix", "re");
168 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100169 return;
170
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100171 /* Skip header */
172 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100173 goto fail;
174
175 for (;;) {
176 char *p, *s;
177 int k;
178
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100179 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100180 break;
181
182 truncate_nl(line);
183
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100184 p = strchr(line, ':');
185 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100186 continue;
187
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100188 if (strlen(p) < 37)
189 continue;
190
191 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100192 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100193 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100194 p += strspn(p, WHITESPACE);
195
196 if (*p != '/')
197 continue;
198
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100199 s = strdup(p);
200 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100201 goto fail;
202
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100203 path_kill_slashes(s);
204
Zbigniew Jędrzejewski-Szmekef422022013-04-22 23:12:15 -0400205 k = set_consume(unix_sockets, s);
206 if (k < 0 && k != -EEXIST)
207 goto fail;
Lennart Poettering17b90522011-02-14 21:55:06 +0100208 }
209
210 return;
211
212fail:
213 set_free_free(unix_sockets);
214 unix_sockets = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100215}
216
217static bool unix_socket_alive(const char *fn) {
218 assert(fn);
219
220 load_unix_sockets();
221
222 if (unix_sockets)
223 return !!set_get(unix_sockets, (char*) fn);
224
225 /* We don't know, so assume yes */
226 return true;
227}
228
Kay Sievers99d680a2013-03-13 13:12:36 +0100229static int dir_is_mount_point(DIR *d, const char *subdir) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200230
231 union file_handle_union h = {
232 .handle.handle_bytes = MAX_HANDLE_SZ
233 };
234
Kay Sievers99d680a2013-03-13 13:12:36 +0100235 int mount_id_parent, mount_id;
236 int r_p, r;
237
Dave Reisner370c8602014-04-19 13:22:35 -0400238 r_p = name_to_handle_at(dirfd(d), ".", &h.handle, &mount_id_parent, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100239 if (r_p < 0)
240 r_p = -errno;
241
Dave Reisner370c8602014-04-19 13:22:35 -0400242 h.handle.handle_bytes = MAX_HANDLE_SZ;
243 r = name_to_handle_at(dirfd(d), subdir, &h.handle, &mount_id, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100244 if (r < 0)
245 r = -errno;
246
247 /* got no handle; make no assumptions, return error */
248 if (r_p < 0 && r < 0)
249 return r_p;
250
251 /* got both handles; if they differ, it is a mount point */
252 if (r_p >= 0 && r >= 0)
253 return mount_id_parent != mount_id;
254
255 /* got only one handle; assume different mount points if one
256 * of both queries was not supported by the filesystem */
Dave Reisnere7aab542014-10-11 20:35:06 -0400257 if (r_p == -ENOSYS || r_p == -EOPNOTSUPP || r == -ENOSYS || r == -EOPNOTSUPP)
Kay Sievers99d680a2013-03-13 13:12:36 +0100258 return true;
259
260 /* return error */
261 if (r_p < 0)
262 return r_p;
263 return r;
264}
265
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200266static int dir_cleanup(
Michal Sekletar78a92a52013-01-18 16:13:08 +0100267 Item *i,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200268 const char *p,
269 DIR *d,
270 const struct stat *ds,
271 usec_t cutoff,
272 dev_t rootdev,
273 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200274 int maxdepth,
Lennart Poettering265ffa12013-09-17 16:33:30 -0500275 bool keep_this_level) {
276
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200277 struct dirent *dent;
278 struct timespec times[2];
279 bool deleted = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200280 int r = 0;
281
282 while ((dent = readdir(d))) {
283 struct stat s;
284 usec_t age;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200285 _cleanup_free_ char *sub_path = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200286
287 if (streq(dent->d_name, ".") ||
288 streq(dent->d_name, ".."))
289 continue;
290
291 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
Kay Sieversca2f4172013-10-17 03:20:46 +0200292 if (errno == ENOENT)
293 continue;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200294
Kay Sieversca2f4172013-10-17 03:20:46 +0200295 /* FUSE, NFS mounts, SELinux might return EACCES */
296 if (errno == EACCES)
Michal Schmidt56f64d92014-11-28 19:29:59 +0100297 log_debug_errno(errno, "stat(%s/%s) failed: %m", p, dent->d_name);
Kay Sieversca2f4172013-10-17 03:20:46 +0200298 else
Michal Schmidt56f64d92014-11-28 19:29:59 +0100299 log_error_errno(errno, "stat(%s/%s) failed: %m", p, dent->d_name);
Kay Sieversca2f4172013-10-17 03:20:46 +0200300 r = -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200301 continue;
302 }
303
304 /* Stay on the same filesystem */
305 if (s.st_dev != rootdev)
306 continue;
307
Kay Sievers99d680a2013-03-13 13:12:36 +0100308 /* Try to detect bind mounts of the same filesystem instance; they
309 * do not differ in device major/minors. This type of query is not
310 * supported on all kernels or filesystem types though. */
311 if (S_ISDIR(s.st_mode) && dir_is_mount_point(d, dent->d_name) > 0)
312 continue;
313
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200314 /* Do not delete read-only files owned by root */
315 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
316 continue;
317
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200318 sub_path = strjoin(p, "/", dent->d_name, NULL);
319 if (!sub_path) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700320 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200321 goto finish;
322 }
323
324 /* Is there an item configured for this path? */
325 if (hashmap_get(items, sub_path))
326 continue;
327
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100328 if (find_glob(globs, sub_path))
329 continue;
330
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200331 if (S_ISDIR(s.st_mode)) {
332
333 if (mountpoint &&
334 streq(dent->d_name, "lost+found") &&
335 s.st_uid == 0)
336 continue;
337
338 if (maxdepth <= 0)
339 log_warning("Reached max depth on %s.", sub_path);
340 else {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200341 _cleanup_closedir_ DIR *sub_dir;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200342 int q;
343
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200344 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200345 if (!sub_dir) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200346 if (errno != ENOENT) {
Michal Schmidt56f64d92014-11-28 19:29:59 +0100347 log_error_errno(errno, "opendir(%s/%s) failed: %m", p, dent->d_name);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200348 r = -errno;
349 }
350
351 continue;
352 }
353
Michal Sekletar78a92a52013-01-18 16:13:08 +0100354 q = dir_cleanup(i, sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200355 if (q < 0)
356 r = q;
357 }
358
Lennart Poettering24f3a372012-06-20 09:05:50 +0200359 /* Note: if you are wondering why we don't
360 * support the sticky bit for excluding
361 * directories from cleaning like we do it for
362 * other file system objects: well, the sticky
363 * bit already has a meaning for directories,
364 * so we don't want to overload that. */
365
366 if (keep_this_level)
367 continue;
368
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200369 /* Ignore ctime, we change it when deleting */
370 age = MAX(timespec_load(&s.st_mtim),
371 timespec_load(&s.st_atim));
372 if (age >= cutoff)
373 continue;
374
Lukas Nykryna6187d42013-03-01 18:29:59 +0100375 if (i->type != IGNORE_DIRECTORY_PATH || !streq(dent->d_name, p)) {
Lennart Poettering9f6445e32013-12-24 16:39:37 +0100376 log_debug("rmdir '%s'", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200377
Michal Sekletar78a92a52013-01-18 16:13:08 +0100378 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
379 if (errno != ENOENT && errno != ENOTEMPTY) {
Michal Schmidt56f64d92014-11-28 19:29:59 +0100380 log_error_errno(errno, "rmdir(%s): %m", sub_path);
Michal Sekletar78a92a52013-01-18 16:13:08 +0100381 r = -errno;
382 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200383 }
384 }
385
386 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100387 /* Skip files for which the sticky bit is
388 * set. These are semantics we define, and are
389 * unknown elsewhere. See XDG_RUNTIME_DIR
390 * specification for details. */
391 if (s.st_mode & S_ISVTX)
392 continue;
393
Lennart Poettering17b90522011-02-14 21:55:06 +0100394 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200395 if (streq(dent->d_name, ".journal") &&
396 s.st_uid == 0)
397 continue;
398
399 if (streq(dent->d_name, "aquota.user") ||
400 streq(dent->d_name, "aquota.group"))
401 continue;
402 }
403
Lennart Poettering17b90522011-02-14 21:55:06 +0100404 /* Ignore sockets that are listed in /proc/net/unix */
405 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
406 continue;
407
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100408 /* Ignore device nodes */
409 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
410 continue;
411
Lennart Poettering24f3a372012-06-20 09:05:50 +0200412 /* Keep files on this level around if this is
413 * requested */
414 if (keep_this_level)
415 continue;
416
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200417 age = MAX3(timespec_load(&s.st_mtim),
418 timespec_load(&s.st_atim),
419 timespec_load(&s.st_ctim));
420
421 if (age >= cutoff)
422 continue;
423
Lennart Poettering9f6445e32013-12-24 16:39:37 +0100424 log_debug("unlink '%s'", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200425
426 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
427 if (errno != ENOENT) {
Michal Schmidt56f64d92014-11-28 19:29:59 +0100428 log_error_errno(errno, "unlink(%s): %m", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200429 r = -errno;
430 }
431 }
432
433 deleted = true;
434 }
435 }
436
437finish:
438 if (deleted) {
439 /* Restore original directory timestamps */
440 times[0] = ds->st_atim;
441 times[1] = ds->st_mtim;
442
443 if (futimens(dirfd(d), times) < 0)
Michal Schmidt56f64d92014-11-28 19:29:59 +0100444 log_error_errno(errno, "utimensat(%s): %m", p);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200445 }
446
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200447 return r;
448}
449
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200450static int item_set_perms(Item *i, const char *path) {
Michael Olbrich1924a972014-08-17 09:45:00 +0200451 struct stat st;
452 bool st_valid;
453
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200454 assert(i);
455 assert(path);
456
Michael Olbrich1924a972014-08-17 09:45:00 +0200457 st_valid = stat(path, &st) == 0;
458
Michal Schmidt062e01b2011-12-16 18:00:11 +0100459 /* not using i->path directly because it may be a glob */
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200460 if (i->mode_set) {
461 mode_t m = i->mode;
462
Michael Olbrich1924a972014-08-17 09:45:00 +0200463 if (i->mask_perms && st_valid) {
464 if (!(st.st_mode & 0111))
465 m &= ~0111;
466 if (!(st.st_mode & 0222))
467 m &= ~0222;
468 if (!(st.st_mode & 0444))
469 m &= ~0444;
470 if (!S_ISDIR(st.st_mode))
471 m &= ~07000; /* remove sticky/sgid/suid bit, unless directory */
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200472 }
473
Michael Olbrich1924a972014-08-17 09:45:00 +0200474 if (!st_valid || m != (st.st_mode & 07777)) {
Michal Schmidt4a62c712014-11-28 19:57:32 +0100475 if (chmod(path, m) < 0)
476 return log_error_errno(errno, "chmod(%s) failed: %m", path);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100477 }
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200478 }
Michal Schmidt062e01b2011-12-16 18:00:11 +0100479
Michael Olbrich1924a972014-08-17 09:45:00 +0200480 if ((!st_valid || (i->uid != st.st_uid || i->gid != st.st_gid)) &&
481 (i->uid_set || i->gid_set))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100482 if (chown(path,
Lennart Poetteringfed1e722014-11-28 20:51:01 +0100483 i->uid_set ? i->uid : UID_INVALID,
484 i->gid_set ? i->gid : GID_INVALID) < 0) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100485
Michal Schmidt56f64d92014-11-28 19:29:59 +0100486 log_error_errno(errno, "chown(%s) failed: %m", path);
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200487 return -errno;
Michal Schmidt062e01b2011-12-16 18:00:11 +0100488 }
489
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200490 return label_fix(path, false, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100491}
492
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100493static int get_xattrs_from_arg(Item *i) {
494 char *xattr;
495 const char *p;
496 int r;
497
498 assert(i);
499
500 if (!i->argument) {
501 log_error("%s: Argument can't be empty!", i->path);
502 return -EBADMSG;
503 }
504 p = i->argument;
505
506 while ((r = unquote_first_word(&p, &xattr, false)) > 0) {
507 _cleanup_free_ char *tmp = NULL, *name = NULL, *value = NULL;
508 r = split_pair(xattr, "=", &name, &value);
509 if (r < 0) {
510 log_warning("Illegal xattr found: \"%s\" - ignoring.", xattr);
511 free(xattr);
512 continue;
513 }
514 free(xattr);
515 if (streq(name, "") || streq(value, "")) {
516 log_warning("Malformed xattr found: \"%s=%s\" - ignoring.", name, value);
517 continue;
518 }
519 tmp = unquote(value, "\"");
520 if (!tmp)
521 return log_oom();
522 free(value);
523 value = cunescape(tmp);
524 if (!value)
525 return log_oom();
526 if (strv_consume_pair(&i->xattrs, name, value) < 0)
527 return log_oom();
528 name = value = NULL;
529 }
530
531 return r;
532}
533
534static int item_set_xattrs(Item *i, const char *path) {
535 char **name, **value;
536
537 assert(i);
538 assert(path);
539
540 if (strv_isempty(i->xattrs))
541 return 0;
542
543 STRV_FOREACH_PAIR(name, value, i->xattrs) {
544 int n;
545 n = strlen(*value);
546 if (lsetxattr(path, *name, *value, n, 0) < 0) {
547 log_error("Setting extended attribute %s=%s on %s failed: %m", *name, *value, path);
548 return -errno;
549 }
550 }
551 return 0;
552}
553
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400554static int write_one_file(Item *i, const char *path) {
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200555 _cleanup_close_ int fd = -1;
556 int flags, r = 0;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400557 struct stat st;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400558
Lennart Poettering874f1942014-06-10 22:48:56 +0200559 assert(i);
560 assert(path);
561
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200562 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND|O_NOFOLLOW :
563 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC|O_NOFOLLOW : 0;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400564
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200565 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200566 mac_selinux_create_file_prepare(path, S_IFREG);
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200567 fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200568 mac_selinux_create_file_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200569 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400570
571 if (fd < 0) {
572 if (i->type == WRITE_FILE && errno == ENOENT)
573 return 0;
574
Michal Schmidt56f64d92014-11-28 19:29:59 +0100575 log_error_errno(errno, "Failed to create file %s: %m", path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400576 return -errno;
577 }
578
579 if (i->argument) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200580 _cleanup_free_ char *unescaped;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400581 ssize_t n;
582 size_t l;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400583
Dave Reisner54693d92012-09-15 12:58:49 -0400584 unescaped = cunescape(i->argument);
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200585 if (!unescaped)
Dave Reisner54693d92012-09-15 12:58:49 -0400586 return log_oom();
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400587
Dave Reisner54693d92012-09-15 12:58:49 -0400588 l = strlen(unescaped);
589 n = write(fd, unescaped, l);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400590
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400591 if (n < 0 || (size_t) n < l) {
592 log_error("Failed to write file %s: %s", path, n < 0 ? strerror(-n) : "Short write");
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400593 return n < 0 ? n : -EIO;
594 }
595 }
596
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200597 fd = safe_close(fd);
Dave Reisner3612fbc2012-09-12 16:21:00 -0400598
Michal Schmidt4a62c712014-11-28 19:57:32 +0100599 if (stat(path, &st) < 0)
600 return log_error_errno(errno, "stat(%s) failed: %m", path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400601
602 if (!S_ISREG(st.st_mode)) {
603 log_error("%s is not a file.", path);
604 return -EEXIST;
605 }
606
607 r = item_set_perms(i, path);
608 if (r < 0)
609 return r;
610
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100611 r = item_set_xattrs(i, i->path);
612 if (r < 0)
613 return r;
614
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400615 return 0;
616}
617
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200618static int item_set_perms_children(Item *i, const char *path) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200619 _cleanup_closedir_ DIR *d;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200620 int r = 0;
621
622 assert(i);
623 assert(path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100624
625 /* This returns the first error we run into, but nevertheless
626 * tries to go on */
627
628 d = opendir(path);
629 if (!d)
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200630 return errno == ENOENT || errno == ENOTDIR ? 0 : -errno;
Michal Schmidta8d88782011-12-15 23:11:07 +0100631
632 for (;;) {
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200633 _cleanup_free_ char *p = NULL;
Lennart Poettering7d5e9c02012-09-19 22:21:09 +0200634 struct dirent *de;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200635 int q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100636
Florian Weimerd78096b2013-12-19 12:26:07 +0100637 errno = 0;
638 de = readdir(d);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200639 if (!de) {
640 if (errno != 0 && r == 0)
641 r = -errno;
642
Michal Schmidta8d88782011-12-15 23:11:07 +0100643 break;
644 }
645
Michal Schmidta8d88782011-12-15 23:11:07 +0100646 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
647 continue;
648
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200649 p = strjoin(path, "/", de->d_name, NULL);
650 if (!p)
651 return -ENOMEM;
Michal Schmidta8d88782011-12-15 23:11:07 +0100652
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200653 q = item_set_perms(i, p);
654 if (q < 0 && q != -ENOENT && r == 0)
655 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100656
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200657 if (IN_SET(de->d_type, DT_UNKNOWN, DT_DIR)) {
658 q = item_set_perms_children(i, p);
659 if (q < 0 && r == 0)
660 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100661 }
Michal Schmidta8d88782011-12-15 23:11:07 +0100662 }
663
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200664 return r;
Michal Schmidta8d88782011-12-15 23:11:07 +0100665}
666
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200667static int item_set_perms_recursive(Item *i, const char *path) {
668 int r, q;
669
670 assert(i);
671 assert(path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100672
Michal Schmidt062e01b2011-12-16 18:00:11 +0100673 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100674 if (r < 0)
675 return r;
676
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200677 q = item_set_perms_children(i, path);
678 if (q < 0 && r == 0)
679 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100680
681 return r;
682}
683
Michal Schmidt99e68c02011-12-15 23:45:26 +0100684static int glob_item(Item *i, int (*action)(Item *, const char *)) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200685 _cleanup_globfree_ glob_t g = {};
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200686 int r = 0, k;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100687 char **fn;
688
Michal Schmidt99e68c02011-12-15 23:45:26 +0100689 errno = 0;
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400690 k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200691 if (k != 0 && k != GLOB_NOMATCH) {
692 if (errno == 0)
693 errno = EIO;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100694
Michal Schmidt56f64d92014-11-28 19:29:59 +0100695 log_error_errno(errno, "glob(%s) failed: %m", i->path);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200696 return -errno;
697 }
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400698
699 STRV_FOREACH(fn, g.gl_pathv) {
700 k = action(i, *fn);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200701 if (k < 0 && r == 0)
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400702 r = k;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100703 }
704
Michal Schmidt99e68c02011-12-15 23:45:26 +0100705 return r;
706}
707
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200708static int create_item(Item *i) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200709 struct stat st;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200710 int r = 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200711
712 assert(i);
713
714 switch (i->type) {
715
716 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100717 case IGNORE_DIRECTORY_PATH:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200718 case REMOVE_PATH:
719 case RECURSIVE_REMOVE_PATH:
720 return 0;
721
722 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100723 case TRUNCATE_FILE:
Dave Reisner1845fdd2012-09-27 20:48:13 -0400724 r = write_one_file(i, i->path);
725 if (r < 0)
726 return r;
727 break;
Lennart Poettering265ffa12013-09-17 16:33:30 -0500728
Lennart Poettering849958d2014-06-10 23:02:40 +0200729 case COPY_FILES:
Lennart Poetteringe1563472014-06-19 19:36:08 +0200730 r = copy_tree(i->argument, i->path, false);
Lennart Poettering849958d2014-06-10 23:02:40 +0200731 if (r < 0) {
Lennart Poetteringe1563472014-06-19 19:36:08 +0200732 struct stat a, b;
733
Michal Schmidt8d3d7072014-11-28 19:13:53 +0100734 if (r != -EEXIST)
735 return log_error_errno(r, "Failed to copy files to %s: %m", i->path);
Lennart Poetteringe1563472014-06-19 19:36:08 +0200736
Michal Schmidt4a62c712014-11-28 19:57:32 +0100737 if (stat(i->argument, &a) < 0)
738 return log_error_errno(errno, "stat(%s) failed: %m", i->argument);
Lennart Poetteringe1563472014-06-19 19:36:08 +0200739
Michal Schmidt4a62c712014-11-28 19:57:32 +0100740 if (stat(i->path, &b) < 0)
741 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Lennart Poetteringe1563472014-06-19 19:36:08 +0200742
743 if ((a.st_mode ^ b.st_mode) & S_IFMT) {
744 log_debug("Can't copy to %s, file exists already and is of different type", i->path);
745 return 0;
746 }
Lennart Poettering849958d2014-06-10 23:02:40 +0200747 }
748
749 r = item_set_perms(i, i->path);
750 if (r < 0)
751 return r;
752
753 break;
754
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400755 case WRITE_FILE:
756 r = glob_item(i, write_one_file);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100757 if (r < 0)
758 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200759
760 break;
761
762 case TRUNCATE_DIRECTORY:
763 case CREATE_DIRECTORY:
764
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200765 RUN_WITH_UMASK(0000) {
766 mkdir_parents_label(i->path, 0755);
Lennart Poettering6f045292014-06-18 00:02:08 +0200767 r = mkdir_label(i->path, i->mode);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200768 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200769
Lennart Poetteringe1563472014-06-19 19:36:08 +0200770 if (r < 0) {
Michal Schmidtf6479622014-11-28 18:50:43 +0100771 if (r != -EEXIST)
772 return log_error_errno(r, "Failed to create directory %s: %m", i->path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200773
Michal Schmidt4a62c712014-11-28 19:57:32 +0100774 if (stat(i->path, &st) < 0)
775 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200776
Lennart Poetteringe1563472014-06-19 19:36:08 +0200777 if (!S_ISDIR(st.st_mode)) {
778 log_debug("%s already exists and is not a directory.", i->path);
779 return 0;
780 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200781 }
782
Michal Schmidt062e01b2011-12-16 18:00:11 +0100783 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100784 if (r < 0)
785 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200786
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100787 r = item_set_xattrs(i, i->path);
788 if (r < 0)
789 return r;
790
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200791 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200792
793 case CREATE_FIFO:
794
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200795 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200796 mac_selinux_create_file_prepare(i->path, S_IFIFO);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200797 r = mkfifo(i->path, i->mode);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200798 mac_selinux_create_file_clear();
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200799 }
800
Lennart Poettering1554afa2014-06-17 23:50:22 +0200801 if (r < 0) {
Michal Schmidt4a62c712014-11-28 19:57:32 +0100802 if (errno != EEXIST)
803 return log_error_errno(errno, "Failed to create fifo %s: %m", i->path);
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200804
Michal Schmidt4a62c712014-11-28 19:57:32 +0100805 if (stat(i->path, &st) < 0)
806 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200807
808 if (!S_ISFIFO(st.st_mode)) {
809
810 if (i->force) {
811
812 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200813 mac_selinux_create_file_prepare(i->path, S_IFIFO);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200814 r = mkfifo_atomic(i->path, i->mode);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200815 mac_selinux_create_file_clear();
Lennart Poettering1554afa2014-06-17 23:50:22 +0200816 }
817
Michal Schmidtf6479622014-11-28 18:50:43 +0100818 if (r < 0)
819 return log_error_errno(r, "Failed to create fifo %s: %m", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200820 } else {
821 log_debug("%s is not a fifo.", i->path);
822 return 0;
823 }
824 }
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200825 }
826
Michal Schmidt062e01b2011-12-16 18:00:11 +0100827 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100828 if (r < 0)
829 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200830
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100831 r = item_set_xattrs(i, i->path);
832 if (r < 0)
833 return r;
834
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200835 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100836
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200837 case CREATE_SYMLINK:
Lennart Poettering468d7262012-01-17 15:04:12 +0100838
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200839 mac_selinux_create_file_prepare(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100840 r = symlink(i->argument, i->path);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200841 mac_selinux_create_file_clear();
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200842
Lennart Poettering468d7262012-01-17 15:04:12 +0100843 if (r < 0) {
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200844 _cleanup_free_ char *x = NULL;
Lennart Poettering468d7262012-01-17 15:04:12 +0100845
Michal Schmidt4a62c712014-11-28 19:57:32 +0100846 if (errno != EEXIST)
847 return log_error_errno(errno, "symlink(%s, %s) failed: %m", i->argument, i->path);
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200848
849 r = readlink_malloc(i->path, &x);
850 if (r < 0 || !streq(i->argument, x)) {
851
852 if (i->force) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200853 mac_selinux_create_file_prepare(i->path, S_IFLNK);
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200854 r = symlink_atomic(i->argument, i->path);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200855 mac_selinux_create_file_clear();
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200856
Michal Schmidtf6479622014-11-28 18:50:43 +0100857 if (r < 0)
858 return log_error_errno(r, "symlink(%s, %s) failed: %m", i->argument, i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200859 } else {
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200860 log_debug("%s is not a symlink or does not point to the correct path.", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200861 return 0;
862 }
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200863 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100864 }
865
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100866 r = item_set_xattrs(i, i->path);
867 if (r < 0)
868 return r;
869
Lennart Poettering468d7262012-01-17 15:04:12 +0100870 break;
Lennart Poettering468d7262012-01-17 15:04:12 +0100871
872 case CREATE_BLOCK_DEVICE:
873 case CREATE_CHAR_DEVICE: {
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700874 mode_t file_type;
875
876 if (have_effective_cap(CAP_MKNOD) == 0) {
877 /* In a container we lack CAP_MKNOD. We
Anatol Pomozovab06eef2013-04-14 19:37:54 -0700878 shouldn't attempt to create the device node in
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700879 that case to avoid noise, and we don't support
880 virtualized devices in containers anyway. */
881
882 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
883 return 0;
884 }
885
Lennart Poettering1554afa2014-06-17 23:50:22 +0200886 file_type = i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR;
Lennart Poettering468d7262012-01-17 15:04:12 +0100887
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200888 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200889 mac_selinux_create_file_prepare(i->path, file_type);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200890 r = mknod(i->path, i->mode | file_type, i->major_minor);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200891 mac_selinux_create_file_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200892 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100893
Kay Sievers6555ad82014-06-13 04:11:11 +0200894 if (r < 0) {
895 if (errno == EPERM) {
896 log_debug("We lack permissions, possibly because of cgroup configuration; "
897 "skipping creation of device node %s.", i->path);
898 return 0;
899 }
900
Michal Schmidt4a62c712014-11-28 19:57:32 +0100901 if (errno != EEXIST)
902 return log_error_errno(errno, "Failed to create device node %s: %m", i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100903
Michal Schmidt4a62c712014-11-28 19:57:32 +0100904 if (stat(i->path, &st) < 0)
905 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100906
Lennart Poettering1554afa2014-06-17 23:50:22 +0200907 if ((st.st_mode & S_IFMT) != file_type) {
908
909 if (i->force) {
910
911 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200912 mac_selinux_create_file_prepare(i->path, file_type);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200913 r = mknod_atomic(i->path, i->mode | file_type, i->major_minor);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200914 mac_selinux_create_file_clear();
Lennart Poettering1554afa2014-06-17 23:50:22 +0200915 }
916
Michal Schmidtf6479622014-11-28 18:50:43 +0100917 if (r < 0)
918 return log_error_errno(r, "Failed to create device node %s: %m", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200919 } else {
920 log_debug("%s is not a device node.", i->path);
921 return 0;
922 }
923 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100924 }
925
926 r = item_set_perms(i, i->path);
927 if (r < 0)
928 return r;
929
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100930 r = item_set_xattrs(i, i->path);
931 if (r < 0)
932 return r;
933
Lennart Poettering468d7262012-01-17 15:04:12 +0100934 break;
935 }
936
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200937 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100938 case RELABEL_PATH:
939
940 r = glob_item(i, item_set_perms);
941 if (r < 0)
Lennart Poettering96ca8192013-06-21 15:57:42 +0200942 return r;
Michal Schmidt777b87e2011-12-16 18:27:35 +0100943 break;
944
Michal Schmidta8d88782011-12-15 23:11:07 +0100945 case RECURSIVE_RELABEL_PATH:
946
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200947 r = glob_item(i, item_set_perms_recursive);
Michal Schmidta8d88782011-12-15 23:11:07 +0100948 if (r < 0)
949 return r;
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100950 break;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200951
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100952 case SET_XATTR:
953 r = item_set_xattrs(i, i->path);
954 if (r < 0)
955 return r;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200956 break;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200957 }
958
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200959 log_debug("%s created successfully.", i->path);
960
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100961 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200962}
963
Michal Schmidta0896122011-12-15 21:32:50 +0100964static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200965 int r;
966
967 assert(i);
968
969 switch (i->type) {
970
971 case CREATE_FILE:
972 case TRUNCATE_FILE:
973 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200974 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100975 case CREATE_SYMLINK:
976 case CREATE_BLOCK_DEVICE:
977 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200978 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100979 case IGNORE_DIRECTORY_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200980 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100981 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100982 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100983 case WRITE_FILE:
Lennart Poettering849958d2014-06-10 23:02:40 +0200984 case COPY_FILES:
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100985 case SET_XATTR:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200986 break;
987
988 case REMOVE_PATH:
Michal Schmidt4a62c712014-11-28 19:57:32 +0100989 if (remove(instance) < 0 && errno != ENOENT)
990 return log_error_errno(errno, "remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200991
992 break;
993
994 case TRUNCATE_DIRECTORY:
995 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +0200996 /* FIXME: we probably should use dir_cleanup() here
997 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +0200998 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Michal Schmidtf6479622014-11-28 18:50:43 +0100999 if (r < 0 && r != -ENOENT)
1000 return log_error_errno(r, "rm_rf(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001001
1002 break;
1003 }
1004
1005 return 0;
1006}
1007
Michal Schmidta0896122011-12-15 21:32:50 +01001008static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +01001009 int r = 0;
1010
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001011 assert(i);
1012
1013 switch (i->type) {
1014
1015 case CREATE_FILE:
1016 case TRUNCATE_FILE:
1017 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +02001018 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +01001019 case CREATE_SYMLINK:
1020 case CREATE_CHAR_DEVICE:
1021 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001022 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001023 case IGNORE_DIRECTORY_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001024 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001025 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +01001026 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001027 case WRITE_FILE:
Lennart Poettering849958d2014-06-10 23:02:40 +02001028 case COPY_FILES:
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001029 case SET_XATTR:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001030 break;
1031
1032 case REMOVE_PATH:
1033 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +01001034 case RECURSIVE_REMOVE_PATH:
1035 r = glob_item(i, remove_item_instance);
1036 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001037 }
1038
Michal Schmidt99e68c02011-12-15 23:45:26 +01001039 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001040}
1041
Michal Sekletar78a92a52013-01-18 16:13:08 +01001042static int clean_item_instance(Item *i, const char* instance) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +02001043 _cleanup_closedir_ DIR *d = NULL;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001044 struct stat s, ps;
1045 bool mountpoint;
1046 int r;
1047 usec_t cutoff, n;
1048
1049 assert(i);
1050
1051 if (!i->age_set)
1052 return 0;
1053
1054 n = now(CLOCK_REALTIME);
1055 if (n < i->age)
1056 return 0;
1057
1058 cutoff = n - i->age;
1059
1060 d = opendir(instance);
1061 if (!d) {
1062 if (errno == ENOENT || errno == ENOTDIR)
1063 return 0;
1064
Michal Schmidt56f64d92014-11-28 19:29:59 +01001065 log_error_errno(errno, "Failed to open directory %s: %m", i->path);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001066 return -errno;
1067 }
1068
Michal Schmidt4a62c712014-11-28 19:57:32 +01001069 if (fstat(dirfd(d), &s) < 0)
1070 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001071
1072 if (!S_ISDIR(s.st_mode)) {
1073 log_error("%s is not a directory.", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001074 return -ENOTDIR;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001075 }
1076
Michal Schmidt4a62c712014-11-28 19:57:32 +01001077 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0)
1078 return log_error_errno(errno, "stat(%s/..) failed: %m", i->path);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001079
1080 mountpoint = s.st_dev != ps.st_dev ||
1081 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
1082
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001083 r = dir_cleanup(i, instance, d, &s, cutoff, s.st_dev, mountpoint,
1084 MAX_DEPTH, i->keep_first_level);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001085 return r;
1086}
1087
1088static int clean_item(Item *i) {
1089 int r = 0;
1090
1091 assert(i);
1092
1093 switch (i->type) {
1094 case CREATE_DIRECTORY:
1095 case TRUNCATE_DIRECTORY:
1096 case IGNORE_PATH:
Lennart Poettering849958d2014-06-10 23:02:40 +02001097 case COPY_FILES:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001098 clean_item_instance(i, i->path);
1099 break;
1100 case IGNORE_DIRECTORY_PATH:
1101 r = glob_item(i, clean_item_instance);
1102 break;
1103 default:
1104 break;
1105 }
1106
1107 return r;
1108}
1109
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001110static int process_item(Item *i) {
1111 int r, q, p;
Zbigniew Jędrzejewski-Szmek9348f0e2014-10-01 07:33:22 -05001112 _cleanup_free_ char *prefix = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001113
1114 assert(i);
1115
Lennart Poettering1910cd02014-06-11 01:37:35 +02001116 if (i->done)
1117 return 0;
1118
1119 i->done = true;
1120
Zbigniew Jędrzejewski-Szmek9348f0e2014-10-01 07:33:22 -05001121 prefix = malloc(strlen(i->path) + 1);
1122 if (!prefix)
1123 return log_oom();
1124
Lennart Poettering1910cd02014-06-11 01:37:35 +02001125 PATH_FOREACH_PREFIX(prefix, i->path) {
1126 Item *j;
1127
1128 j = hashmap_get(items, prefix);
1129 if (j)
1130 process_item(j);
1131 }
1132
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001133 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +01001134 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001135 p = arg_clean ? clean_item(i) : 0;
1136
1137 if (r < 0)
1138 return r;
1139
1140 if (q < 0)
1141 return q;
1142
1143 return p;
1144}
1145
1146static void item_free(Item *i) {
Lennart Poettering753615e2014-06-12 23:07:17 +02001147
1148 if (!i)
1149 return;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001150
1151 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +01001152 free(i->argument);
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001153 strv_free(i->xattrs);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001154 free(i);
1155}
1156
Lennart Poettering14bf2c92013-10-14 04:59:26 +02001157DEFINE_TRIVIAL_CLEANUP_FUNC(Item*, item_free);
Maciej Wereskie2f2fb72013-07-19 15:43:12 +02001158
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001159static bool item_equal(Item *a, Item *b) {
1160 assert(a);
1161 assert(b);
1162
1163 if (!streq_ptr(a->path, b->path))
1164 return false;
1165
1166 if (a->type != b->type)
1167 return false;
1168
1169 if (a->uid_set != b->uid_set ||
1170 (a->uid_set && a->uid != b->uid))
1171 return false;
1172
1173 if (a->gid_set != b->gid_set ||
1174 (a->gid_set && a->gid != b->gid))
1175 return false;
1176
1177 if (a->mode_set != b->mode_set ||
1178 (a->mode_set && a->mode != b->mode))
1179 return false;
1180
1181 if (a->age_set != b->age_set ||
1182 (a->age_set && a->age != b->age))
1183 return false;
1184
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001185 if ((a->type == CREATE_FILE ||
1186 a->type == TRUNCATE_FILE ||
1187 a->type == WRITE_FILE ||
Lennart Poettering849958d2014-06-10 23:02:40 +02001188 a->type == CREATE_SYMLINK ||
1189 a->type == COPY_FILES) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +01001190 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +01001191 return false;
1192
1193 if ((a->type == CREATE_CHAR_DEVICE ||
1194 a->type == CREATE_BLOCK_DEVICE) &&
1195 a->major_minor != b->major_minor)
1196 return false;
1197
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001198 return true;
1199}
1200
Dave Reisnera2aced42013-07-24 11:10:05 -04001201static bool should_include_path(const char *path) {
1202 char **prefix;
1203
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001204 STRV_FOREACH(prefix, arg_exclude_prefixes)
Dave Reisner5c795112013-07-24 11:19:24 -04001205 if (path_startswith(path, *prefix))
1206 return false;
Dave Reisnera2aced42013-07-24 11:10:05 -04001207
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001208 STRV_FOREACH(prefix, arg_include_prefixes)
Dave Reisnera2aced42013-07-24 11:10:05 -04001209 if (path_startswith(path, *prefix))
1210 return true;
Dave Reisnera2aced42013-07-24 11:10:05 -04001211
Dave Reisner5c795112013-07-24 11:19:24 -04001212 /* no matches, so we should include this path only if we
1213 * have no whitelist at all */
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001214 return strv_length(arg_include_prefixes) == 0;
Dave Reisnera2aced42013-07-24 11:10:05 -04001215}
1216
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001217static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001218
1219 static const Specifier specifier_table[] = {
1220 { 'm', specifier_machine_id, NULL },
1221 { 'b', specifier_boot_id, NULL },
1222 { 'H', specifier_host_name, NULL },
1223 { 'v', specifier_kernel_release, NULL },
1224 {}
1225 };
1226
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001227 _cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
1228 _cleanup_(item_freep) Item *i = NULL;
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001229 Item *existing;
Michal Schmidt66ccd032011-12-15 21:31:14 +01001230 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001231 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001232 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +02001233
1234 assert(fname);
1235 assert(line >= 1);
1236 assert(buffer);
1237
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001238 r = sscanf(buffer,
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001239 "%ms %ms %ms %ms %ms %ms %n",
1240 &action,
Lennart Poettering1731e342013-09-17 11:02:02 -05001241 &path,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +01001242 &mode,
1243 &user,
1244 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +01001245 &age,
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001246 &n);
1247 if (r < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001248 log_error("[%s:%u] Syntax error.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001249 return -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +02001250 }
1251
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001252 if (isempty(action)) {
1253 log_error("[%s:%u] Command too short '%s'.", fname, line, action);
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001254 return -EINVAL;
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001255 }
1256
1257 if (strlen(action) > 1 && !in_charset(action+1, "!+")) {
1258 log_error("[%s:%u] Unknown modifiers in command '%s'", fname, line, action);
1259 return -EINVAL;
1260 }
1261
1262 if (strchr(action+1, '!') && !arg_boot)
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001263 return 0;
1264
1265 type = action[0];
1266
Lennart Poettering1731e342013-09-17 11:02:02 -05001267 i = new0(Item, 1);
1268 if (!i)
1269 return log_oom();
1270
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001271 i->force = !!strchr(action+1, '+');
1272
Lennart Poettering1731e342013-09-17 11:02:02 -05001273 r = specifier_printf(path, specifier_table, NULL, &i->path);
1274 if (r < 0) {
1275 log_error("[%s:%u] Failed to replace specifiers: %s", fname, line, path);
1276 return r;
1277 }
1278
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001279 if (n >= 0) {
1280 n += strspn(buffer+n, WHITESPACE);
1281 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
1282 i->argument = unquote(buffer+n, "\"");
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001283 if (!i->argument)
1284 return log_oom();
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001285 }
1286 }
1287
Lennart Poettering753615e2014-06-12 23:07:17 +02001288 switch (type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001289
Michal Schmidt777b87e2011-12-16 18:27:35 +01001290 case CREATE_FILE:
1291 case TRUNCATE_FILE:
1292 case CREATE_DIRECTORY:
1293 case TRUNCATE_DIRECTORY:
1294 case CREATE_FIFO:
1295 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001296 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001297 case REMOVE_PATH:
1298 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001299 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001300 case RELABEL_PATH:
1301 case RECURSIVE_RELABEL_PATH:
1302 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001303
1304 case CREATE_SYMLINK:
1305 if (!i->argument) {
Kay Sievers2f3b8732014-06-20 15:57:43 +02001306 i->argument = strappend("/usr/share/factory", i->path);
1307 if (!i->argument)
1308 return log_oom();
Lennart Poettering468d7262012-01-17 15:04:12 +01001309 }
1310 break;
1311
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001312 case WRITE_FILE:
1313 if (!i->argument) {
1314 log_error("[%s:%u] Write file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001315 return -EBADMSG;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001316 }
1317 break;
1318
Lennart Poettering849958d2014-06-10 23:02:40 +02001319 case COPY_FILES:
1320 if (!i->argument) {
Kay Sievers2f3b8732014-06-20 15:57:43 +02001321 i->argument = strappend("/usr/share/factory", i->path);
1322 if (!i->argument)
1323 return log_oom();
Lennart Poettering849958d2014-06-10 23:02:40 +02001324 }
1325
1326 if (!path_is_absolute(i->argument)) {
1327 log_error("[%s:%u] Source path is not absolute.", fname, line);
1328 return -EBADMSG;
1329 }
1330
1331 path_kill_slashes(i->argument);
1332 break;
1333
Lennart Poettering468d7262012-01-17 15:04:12 +01001334 case CREATE_CHAR_DEVICE:
1335 case CREATE_BLOCK_DEVICE: {
1336 unsigned major, minor;
1337
1338 if (!i->argument) {
1339 log_error("[%s:%u] Device file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001340 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001341 }
1342
1343 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1344 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 -04001345 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001346 }
1347
1348 i->major_minor = makedev(major, minor);
1349 break;
1350 }
1351
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001352 case SET_XATTR:
1353 if (!i->argument) {
1354 log_error("[%s:%u] Set extended attribute requires argument.", fname, line);
1355 return -EBADMSG;
1356 }
1357 r = get_xattrs_from_arg(i);
1358 if (r < 0)
1359 return r;
1360 break;
1361
Michal Schmidt777b87e2011-12-16 18:27:35 +01001362 default:
Lennart Poettering753615e2014-06-12 23:07:17 +02001363 log_error("[%s:%u] Unknown command type '%c'.", fname, line, type);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001364 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001365 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001366
Michal Schmidta8d88782011-12-15 23:11:07 +01001367 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001368
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001369 if (!path_is_absolute(i->path)) {
1370 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001371 return -EBADMSG;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001372 }
1373
1374 path_kill_slashes(i->path);
1375
Dave Reisnera2aced42013-07-24 11:10:05 -04001376 if (!should_include_path(i->path))
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001377 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001378
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001379 if (arg_root) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001380 char *p;
1381
1382 p = strappend(arg_root, i->path);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001383 if (!p)
1384 return log_oom();
1385
1386 free(i->path);
1387 i->path = p;
1388 }
1389
Lennart Poettering5008d582010-09-28 02:34:02 +02001390 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001391 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001392
Lennart Poetteringd05c5032012-07-16 12:34:54 +02001393 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001394 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001395 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001396 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001397 }
1398
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001399 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001400 }
1401
1402 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001403 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001404
Lennart Poettering4b678342011-07-23 01:17:59 +02001405 r = get_group_creds(&g, &i->gid);
1406 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001407 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001408 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001409 }
1410
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001411 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001412 }
1413
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001414 if (mode && !streq(mode, "-")) {
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001415 const char *mm = mode;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001416 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001417
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001418 if (*mm == '~') {
1419 i->mask_perms = true;
1420 mm++;
1421 }
1422
1423 if (sscanf(mm, "%o", &m) != 1) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001424 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001425 return -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001426 }
1427
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001428 i->mode = m;
1429 i->mode_set = true;
1430 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001431 i->mode =
1432 i->type == CREATE_DIRECTORY ||
1433 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001434
1435 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001436 const char *a = age;
1437
1438 if (*a == '~') {
1439 i->keep_first_level = true;
1440 a++;
1441 }
1442
Lennart Poettering7f602782013-04-02 20:38:16 +02001443 if (parse_sec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001444 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001445 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001446 }
1447
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001448 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001449 }
1450
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001451 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001452
Lennart Poettering468d7262012-01-17 15:04:12 +01001453 existing = hashmap_get(h, i->path);
1454 if (existing) {
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001455 if (i->type == SET_XATTR) {
1456 r = strv_extend_strv(&existing->xattrs, i->xattrs);
1457 if (r < 0)
1458 return log_oom();
1459 return 0;
1460 } else if (existing->type == SET_XATTR) {
1461 r = strv_extend_strv(&i->xattrs, existing->xattrs);
1462 if (r < 0)
1463 return log_oom();
1464 r = hashmap_replace(h, i->path, i);
1465 if (r < 0) {
1466 log_error("Failed to replace item for %s.", i->path);
1467 return r;
1468 }
1469 item_free(existing);
1470 } else {
1471 /* Two identical items are fine */
1472 if (!item_equal(existing, i))
1473 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1474 return 0;
1475 }
1476 } else {
1477 r = hashmap_put(h, i->path, i);
1478 if (r < 0) {
1479 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
1480 return r;
1481 }
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001482 }
1483
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001484 i = NULL; /* avoid cleanup */
Lennart Poettering5008d582010-09-28 02:34:02 +02001485
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001486 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001487}
1488
Zbigniew Jędrzejewski-Szmek601185b2014-08-02 11:12:21 -04001489static void help(void) {
Lennart Poettering522d4a42011-02-13 15:08:15 +01001490 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1491 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001492 " -h --help Show this help\n"
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001493 " --version Show package version\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001494 " --create Create marked files/directories\n"
1495 " --clean Clean up marked directories\n"
1496 " --remove Remove marked files/directories\n"
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001497 " --boot Execute actions only safe at boot\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001498 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n"
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001499 " --exclude-prefix=PATH Ignore rules that apply to paths with the specified prefix\n"
1500 " --root=PATH Operate on an alternate filesystem root\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001501 program_invocation_short_name);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001502}
1503
1504static int parse_argv(int argc, char *argv[]) {
1505
1506 enum {
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001507 ARG_VERSION = 0x100,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001508 ARG_CREATE,
1509 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001510 ARG_REMOVE,
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001511 ARG_BOOT,
Dave Reisner5c795112013-07-24 11:19:24 -04001512 ARG_PREFIX,
1513 ARG_EXCLUDE_PREFIX,
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001514 ARG_ROOT,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001515 };
1516
1517 static const struct option options[] = {
Dave Reisner5c795112013-07-24 11:19:24 -04001518 { "help", no_argument, NULL, 'h' },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001519 { "version", no_argument, NULL, ARG_VERSION },
Dave Reisner5c795112013-07-24 11:19:24 -04001520 { "create", no_argument, NULL, ARG_CREATE },
1521 { "clean", no_argument, NULL, ARG_CLEAN },
1522 { "remove", no_argument, NULL, ARG_REMOVE },
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001523 { "boot", no_argument, NULL, ARG_BOOT },
Dave Reisner5c795112013-07-24 11:19:24 -04001524 { "prefix", required_argument, NULL, ARG_PREFIX },
1525 { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX },
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001526 { "root", required_argument, NULL, ARG_ROOT },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001527 {}
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001528 };
1529
1530 int c;
1531
1532 assert(argc >= 0);
1533 assert(argv);
1534
Zbigniew Jędrzejewski-Szmek601185b2014-08-02 11:12:21 -04001535 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001536
1537 switch (c) {
1538
1539 case 'h':
Zbigniew Jędrzejewski-Szmek601185b2014-08-02 11:12:21 -04001540 help();
1541 return 0;
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001542
1543 case ARG_VERSION:
1544 puts(PACKAGE_STRING);
1545 puts(SYSTEMD_FEATURES);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001546 return 0;
1547
1548 case ARG_CREATE:
1549 arg_create = true;
1550 break;
1551
1552 case ARG_CLEAN:
1553 arg_clean = true;
1554 break;
1555
1556 case ARG_REMOVE:
1557 arg_remove = true;
1558 break;
1559
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001560 case ARG_BOOT:
1561 arg_boot = true;
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001562 break;
1563
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001564 case ARG_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001565 if (strv_push(&arg_include_prefixes, optarg) < 0)
Dave Reisnera2aced42013-07-24 11:10:05 -04001566 return log_oom();
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001567 break;
1568
Dave Reisner5c795112013-07-24 11:19:24 -04001569 case ARG_EXCLUDE_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001570 if (strv_push(&arg_exclude_prefixes, optarg) < 0)
Dave Reisner5c795112013-07-24 11:19:24 -04001571 return log_oom();
1572 break;
1573
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001574 case ARG_ROOT:
Lennart Poettering753615e2014-06-12 23:07:17 +02001575 free(arg_root);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001576 arg_root = path_make_absolute_cwd(optarg);
1577 if (!arg_root)
1578 return log_oom();
Lennart Poettering753615e2014-06-12 23:07:17 +02001579
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001580 path_kill_slashes(arg_root);
1581 break;
1582
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001583 case '?':
1584 return -EINVAL;
1585
1586 default:
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001587 assert_not_reached("Unhandled option");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001588 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001589
1590 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001591 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001592 return -EINVAL;
1593 }
1594
1595 return 1;
1596}
1597
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001598static int read_config_file(const char *fn, bool ignore_enoent) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001599 _cleanup_fclose_ FILE *f = NULL;
1600 char line[LINE_MAX];
Michal Sekletar78a92a52013-01-18 16:13:08 +01001601 Iterator iterator;
Lennart Poettering1731e342013-09-17 11:02:02 -05001602 unsigned v = 0;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001603 Item *i;
Lennart Poettering1731e342013-09-17 11:02:02 -05001604 int r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001605
1606 assert(fn);
1607
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001608 r = search_and_fopen_nulstr(fn, "re", arg_root, conf_file_dirs, &f);
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001609 if (r < 0) {
1610 if (ignore_enoent && r == -ENOENT)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001611 return 0;
1612
Michal Schmidt8d3d7072014-11-28 19:13:53 +01001613 return log_error_errno(r, "Failed to open '%s', ignoring: %m", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001614 }
1615
Lennart Poettering1731e342013-09-17 11:02:02 -05001616 FOREACH_LINE(line, f, break) {
1617 char *l;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001618 int k;
1619
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001620 v++;
1621
1622 l = strstrip(line);
1623 if (*l == '#' || *l == 0)
1624 continue;
1625
Lennart Poettering1731e342013-09-17 11:02:02 -05001626 k = parse_line(fn, v, l);
1627 if (k < 0 && r == 0)
1628 r = k;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001629 }
1630
Michal Sekletar78a92a52013-01-18 16:13:08 +01001631 /* we have to determine age parameter for each entry of type X */
1632 HASHMAP_FOREACH(i, globs, iterator) {
1633 Iterator iter;
1634 Item *j, *candidate_item = NULL;
1635
1636 if (i->type != IGNORE_DIRECTORY_PATH)
1637 continue;
1638
1639 HASHMAP_FOREACH(j, items, iter) {
1640 if (j->type != CREATE_DIRECTORY && j->type != TRUNCATE_DIRECTORY)
1641 continue;
1642
1643 if (path_equal(j->path, i->path)) {
1644 candidate_item = j;
1645 break;
1646 }
1647
1648 if ((!candidate_item && path_startswith(i->path, j->path)) ||
1649 (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
1650 candidate_item = j;
1651 }
1652
Richard Weinberger9ed2a352014-09-09 11:09:37 +02001653 if (candidate_item && candidate_item->age_set) {
Michal Sekletar78a92a52013-01-18 16:13:08 +01001654 i->age = candidate_item->age;
1655 i->age_set = true;
1656 }
1657 }
1658
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001659 if (ferror(f)) {
Michal Schmidt56f64d92014-11-28 19:29:59 +01001660 log_error_errno(errno, "Failed to read from file %s: %m", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001661 if (r == 0)
1662 r = -EIO;
1663 }
1664
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001665 return r;
1666}
1667
Lennart Poettering5008d582010-09-28 02:34:02 +02001668int main(int argc, char *argv[]) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001669 int r, k;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001670 Item *i;
1671 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001672
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001673 r = parse_argv(argc, argv);
1674 if (r <= 0)
Lennart Poettering753615e2014-06-12 23:07:17 +02001675 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001676
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001677 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001678 log_parse_environment();
1679 log_open();
1680
Lennart Poettering4c126262011-08-01 20:52:18 +02001681 umask(0022);
1682
WaLyong Chocc56faf2014-10-23 17:23:46 +09001683 mac_selinux_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001684
Michal Schmidtd5099ef2014-08-13 01:00:18 +02001685 items = hashmap_new(&string_hash_ops);
1686 globs = hashmap_new(&string_hash_ops);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001687
1688 if (!items || !globs) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001689 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001690 goto finish;
1691 }
1692
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001693 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001694
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001695 if (optind < argc) {
1696 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001697
Dave Reisner91256702012-06-08 22:31:19 -04001698 for (j = optind; j < argc; j++) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001699 k = read_config_file(argv[j], false);
1700 if (k < 0 && r == 0)
1701 r = k;
Dave Reisner91256702012-06-08 22:31:19 -04001702 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001703
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001704 } else {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001705 _cleanup_strv_free_ char **files = NULL;
1706 char **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001707
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001708 r = conf_files_list_nulstr(&files, ".conf", arg_root, conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001709 if (r < 0) {
Michal Schmidtda927ba2014-11-28 13:19:16 +01001710 log_error_errno(r, "Failed to enumerate tmpfiles.d files: %m");
Kay Sievers44143302011-04-28 23:51:24 +02001711 goto finish;
1712 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001713
Kay Sievers772f8372011-04-25 21:38:21 +02001714 STRV_FOREACH(f, files) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001715 k = read_config_file(*f, true);
1716 if (k < 0 && r == 0)
1717 r = k;
Lennart Poettering5008d582010-09-28 02:34:02 +02001718 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001719 }
1720
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001721 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001722 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001723
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001724 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001725 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001726
Lennart Poettering5008d582010-09-28 02:34:02 +02001727finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001728 while ((i = hashmap_steal_first(items)))
1729 item_free(i);
1730
Lennart Poettering17b90522011-02-14 21:55:06 +01001731 while ((i = hashmap_steal_first(globs)))
1732 item_free(i);
1733
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001734 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001735 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001736
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001737 free(arg_include_prefixes);
1738 free(arg_exclude_prefixes);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001739 free(arg_root);
Dave Reisnera2aced42013-07-24 11:10:05 -04001740
Lennart Poettering17b90522011-02-14 21:55:06 +01001741 set_free_free(unix_sockets);
1742
WaLyong Chocc56faf2014-10-23 17:23:46 +09001743 mac_selinux_finish();
Lennart Poettering29003cf2010-10-19 19:36:45 +02001744
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001745 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Lennart Poettering5008d582010-09-28 02:34:02 +02001746}