blob: 44ea51e26b61d9f2fa665742858638c9fc750740 [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 Poettering5008d582010-09-28 02:34:02 +020057
Andreas Jaeger01000472010-09-29 10:08:24 +020058/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020059 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020060 * properly owned directories beneath /tmp, /var/tmp, /run, which are
61 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020062
Michal Schmidt66ccd032011-12-15 21:31:14 +010063typedef enum ItemType {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010064 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020065 CREATE_FILE = 'f',
66 TRUNCATE_FILE = 'F',
67 CREATE_DIRECTORY = 'd',
68 TRUNCATE_DIRECTORY = 'D',
Lennart Poetteringee17ee72011-07-12 03:56:56 +020069 CREATE_FIFO = 'p',
Lennart Poettering468d7262012-01-17 15:04:12 +010070 CREATE_SYMLINK = 'L',
71 CREATE_CHAR_DEVICE = 'c',
72 CREATE_BLOCK_DEVICE = 'b',
Lennart Poettering849958d2014-06-10 23:02:40 +020073 COPY_FILES = 'C',
Maciej Wereskiebf4e802014-12-04 10:32:10 +010074 SET_XATTR = 't',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010075
76 /* These ones take globs */
Lennart Poetteringcde684a2014-06-10 22:50:46 +020077 WRITE_FILE = 'w',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020078 IGNORE_PATH = 'x',
Michal Sekletar78a92a52013-01-18 16:13:08 +010079 IGNORE_DIRECTORY_PATH = 'X',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020080 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010081 RECURSIVE_REMOVE_PATH = 'R',
Lennart Poetteringe73a03e2014-06-10 23:42:16 +020082 ADJUST_MODE = 'm', /* legacy, 'z' is identical to this */
Michal Schmidt777b87e2011-12-16 18:27:35 +010083 RELABEL_PATH = 'z',
Lennart Poetteringe73a03e2014-06-10 23:42:16 +020084 RECURSIVE_RELABEL_PATH = 'Z',
Michal Schmidt66ccd032011-12-15 21:31:14 +010085} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020086
87typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +010088 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020089
90 char *path;
Lennart Poettering468d7262012-01-17 15:04:12 +010091 char *argument;
Maciej Wereskiebf4e802014-12-04 10:32:10 +010092 char **xattrs;
Lennart Poettering5008d582010-09-28 02:34:02 +020093 uid_t uid;
94 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020095 mode_t mode;
96 usec_t age;
97
Lennart Poettering468d7262012-01-17 15:04:12 +010098 dev_t major_minor;
99
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200100 bool uid_set:1;
101 bool gid_set:1;
102 bool mode_set:1;
103 bool age_set:1;
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200104 bool mask_perms:1;
Lennart Poettering24f3a372012-06-20 09:05:50 +0200105
106 bool keep_first_level:1;
Lennart Poettering1910cd02014-06-11 01:37:35 +0200107
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200108 bool force:1;
109
Lennart Poettering1910cd02014-06-11 01:37:35 +0200110 bool done:1;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200111} Item;
112
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200113static bool arg_create = false;
114static bool arg_clean = false;
115static bool arg_remove = false;
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -0500116static bool arg_boot = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200117
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200118static char **arg_include_prefixes = NULL;
119static char **arg_exclude_prefixes = NULL;
Michael Marineaucf9a4ab2014-03-13 21:32:13 -0700120static char *arg_root = NULL;
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100121
Josh Triplett7f0a55d2014-10-29 05:02:56 -0700122static const char conf_file_dirs[] = CONF_DIRS_NULSTR("tmpfiles");
Dave Reisner91256702012-06-08 22:31:19 -0400123
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200124#define MAX_DEPTH 256
125
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200126static Hashmap *items = NULL, *globs = NULL;
127static Set *unix_sockets = NULL;
128
Michal Schmidt66ccd032011-12-15 21:31:14 +0100129static bool needs_glob(ItemType t) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200130 return IN_SET(t,
131 WRITE_FILE,
132 IGNORE_PATH,
133 IGNORE_DIRECTORY_PATH,
134 REMOVE_PATH,
135 RECURSIVE_REMOVE_PATH,
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200136 ADJUST_MODE,
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200137 RELABEL_PATH,
138 RECURSIVE_RELABEL_PATH);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100139}
140
141static struct Item* find_glob(Hashmap *h, const char *match) {
142 Item *j;
143 Iterator i;
144
145 HASHMAP_FOREACH(j, h, i)
146 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
147 return j;
148
149 return NULL;
150}
151
Lennart Poettering17b90522011-02-14 21:55:06 +0100152static void load_unix_sockets(void) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200153 _cleanup_fclose_ FILE *f = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100154 char line[LINE_MAX];
155
156 if (unix_sockets)
157 return;
158
159 /* We maintain a cache of the sockets we found in
160 * /proc/net/unix to speed things up a little. */
161
Michal Schmidtd5099ef2014-08-13 01:00:18 +0200162 unix_sockets = set_new(&string_hash_ops);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100163 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100164 return;
165
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100166 f = fopen("/proc/net/unix", "re");
167 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100168 return;
169
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100170 /* Skip header */
171 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100172 goto fail;
173
174 for (;;) {
175 char *p, *s;
176 int k;
177
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100178 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100179 break;
180
181 truncate_nl(line);
182
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100183 p = strchr(line, ':');
184 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100185 continue;
186
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100187 if (strlen(p) < 37)
188 continue;
189
190 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100191 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100192 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100193 p += strspn(p, WHITESPACE);
194
195 if (*p != '/')
196 continue;
197
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100198 s = strdup(p);
199 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100200 goto fail;
201
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100202 path_kill_slashes(s);
203
Zbigniew Jędrzejewski-Szmekef422022013-04-22 23:12:15 -0400204 k = set_consume(unix_sockets, s);
205 if (k < 0 && k != -EEXIST)
206 goto fail;
Lennart Poettering17b90522011-02-14 21:55:06 +0100207 }
208
209 return;
210
211fail:
212 set_free_free(unix_sockets);
213 unix_sockets = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100214}
215
216static bool unix_socket_alive(const char *fn) {
217 assert(fn);
218
219 load_unix_sockets();
220
221 if (unix_sockets)
222 return !!set_get(unix_sockets, (char*) fn);
223
224 /* We don't know, so assume yes */
225 return true;
226}
227
Kay Sievers99d680a2013-03-13 13:12:36 +0100228static int dir_is_mount_point(DIR *d, const char *subdir) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200229
230 union file_handle_union h = {
231 .handle.handle_bytes = MAX_HANDLE_SZ
232 };
233
Kay Sievers99d680a2013-03-13 13:12:36 +0100234 int mount_id_parent, mount_id;
235 int r_p, r;
236
Dave Reisner370c8602014-04-19 13:22:35 -0400237 r_p = name_to_handle_at(dirfd(d), ".", &h.handle, &mount_id_parent, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100238 if (r_p < 0)
239 r_p = -errno;
240
Dave Reisner370c8602014-04-19 13:22:35 -0400241 h.handle.handle_bytes = MAX_HANDLE_SZ;
242 r = name_to_handle_at(dirfd(d), subdir, &h.handle, &mount_id, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100243 if (r < 0)
244 r = -errno;
245
246 /* got no handle; make no assumptions, return error */
247 if (r_p < 0 && r < 0)
248 return r_p;
249
250 /* got both handles; if they differ, it is a mount point */
251 if (r_p >= 0 && r >= 0)
252 return mount_id_parent != mount_id;
253
254 /* got only one handle; assume different mount points if one
255 * of both queries was not supported by the filesystem */
Dave Reisnere7aab542014-10-11 20:35:06 -0400256 if (r_p == -ENOSYS || r_p == -EOPNOTSUPP || r == -ENOSYS || r == -EOPNOTSUPP)
Kay Sievers99d680a2013-03-13 13:12:36 +0100257 return true;
258
259 /* return error */
260 if (r_p < 0)
261 return r_p;
262 return r;
263}
264
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200265static int dir_cleanup(
Michal Sekletar78a92a52013-01-18 16:13:08 +0100266 Item *i,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200267 const char *p,
268 DIR *d,
269 const struct stat *ds,
270 usec_t cutoff,
271 dev_t rootdev,
272 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200273 int maxdepth,
Lennart Poettering265ffa12013-09-17 16:33:30 -0500274 bool keep_this_level) {
275
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200276 struct dirent *dent;
277 struct timespec times[2];
278 bool deleted = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200279 int r = 0;
280
281 while ((dent = readdir(d))) {
282 struct stat s;
283 usec_t age;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200284 _cleanup_free_ char *sub_path = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200285
286 if (streq(dent->d_name, ".") ||
287 streq(dent->d_name, ".."))
288 continue;
289
290 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
Kay Sieversca2f4172013-10-17 03:20:46 +0200291 if (errno == ENOENT)
292 continue;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200293
Kay Sieversca2f4172013-10-17 03:20:46 +0200294 /* FUSE, NFS mounts, SELinux might return EACCES */
295 if (errno == EACCES)
Michal Schmidt56f64d92014-11-28 19:29:59 +0100296 log_debug_errno(errno, "stat(%s/%s) failed: %m", p, dent->d_name);
Kay Sieversca2f4172013-10-17 03:20:46 +0200297 else
Michal Schmidt56f64d92014-11-28 19:29:59 +0100298 log_error_errno(errno, "stat(%s/%s) failed: %m", p, dent->d_name);
Kay Sieversca2f4172013-10-17 03:20:46 +0200299 r = -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200300 continue;
301 }
302
303 /* Stay on the same filesystem */
304 if (s.st_dev != rootdev)
305 continue;
306
Kay Sievers99d680a2013-03-13 13:12:36 +0100307 /* Try to detect bind mounts of the same filesystem instance; they
308 * do not differ in device major/minors. This type of query is not
309 * supported on all kernels or filesystem types though. */
310 if (S_ISDIR(s.st_mode) && dir_is_mount_point(d, dent->d_name) > 0)
311 continue;
312
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200313 /* Do not delete read-only files owned by root */
314 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
315 continue;
316
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200317 sub_path = strjoin(p, "/", dent->d_name, NULL);
318 if (!sub_path) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700319 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200320 goto finish;
321 }
322
323 /* Is there an item configured for this path? */
324 if (hashmap_get(items, sub_path))
325 continue;
326
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100327 if (find_glob(globs, sub_path))
328 continue;
329
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200330 if (S_ISDIR(s.st_mode)) {
331
332 if (mountpoint &&
333 streq(dent->d_name, "lost+found") &&
334 s.st_uid == 0)
335 continue;
336
337 if (maxdepth <= 0)
338 log_warning("Reached max depth on %s.", sub_path);
339 else {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200340 _cleanup_closedir_ DIR *sub_dir;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200341 int q;
342
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200343 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200344 if (!sub_dir) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200345 if (errno != ENOENT) {
Michal Schmidt56f64d92014-11-28 19:29:59 +0100346 log_error_errno(errno, "opendir(%s/%s) failed: %m", p, dent->d_name);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200347 r = -errno;
348 }
349
350 continue;
351 }
352
Michal Sekletar78a92a52013-01-18 16:13:08 +0100353 q = dir_cleanup(i, sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200354 if (q < 0)
355 r = q;
356 }
357
Lennart Poettering24f3a372012-06-20 09:05:50 +0200358 /* Note: if you are wondering why we don't
359 * support the sticky bit for excluding
360 * directories from cleaning like we do it for
361 * other file system objects: well, the sticky
362 * bit already has a meaning for directories,
363 * so we don't want to overload that. */
364
365 if (keep_this_level)
366 continue;
367
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200368 /* Ignore ctime, we change it when deleting */
369 age = MAX(timespec_load(&s.st_mtim),
370 timespec_load(&s.st_atim));
371 if (age >= cutoff)
372 continue;
373
Lukas Nykryna6187d42013-03-01 18:29:59 +0100374 if (i->type != IGNORE_DIRECTORY_PATH || !streq(dent->d_name, p)) {
Lennart Poettering9f6445e32013-12-24 16:39:37 +0100375 log_debug("rmdir '%s'", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200376
Michal Sekletar78a92a52013-01-18 16:13:08 +0100377 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
378 if (errno != ENOENT && errno != ENOTEMPTY) {
Michal Schmidt56f64d92014-11-28 19:29:59 +0100379 log_error_errno(errno, "rmdir(%s): %m", sub_path);
Michal Sekletar78a92a52013-01-18 16:13:08 +0100380 r = -errno;
381 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200382 }
383 }
384
385 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100386 /* Skip files for which the sticky bit is
387 * set. These are semantics we define, and are
388 * unknown elsewhere. See XDG_RUNTIME_DIR
389 * specification for details. */
390 if (s.st_mode & S_ISVTX)
391 continue;
392
Lennart Poettering17b90522011-02-14 21:55:06 +0100393 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200394 if (streq(dent->d_name, ".journal") &&
395 s.st_uid == 0)
396 continue;
397
398 if (streq(dent->d_name, "aquota.user") ||
399 streq(dent->d_name, "aquota.group"))
400 continue;
401 }
402
Lennart Poettering17b90522011-02-14 21:55:06 +0100403 /* Ignore sockets that are listed in /proc/net/unix */
404 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
405 continue;
406
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100407 /* Ignore device nodes */
408 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
409 continue;
410
Lennart Poettering24f3a372012-06-20 09:05:50 +0200411 /* Keep files on this level around if this is
412 * requested */
413 if (keep_this_level)
414 continue;
415
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200416 age = MAX3(timespec_load(&s.st_mtim),
417 timespec_load(&s.st_atim),
418 timespec_load(&s.st_ctim));
419
420 if (age >= cutoff)
421 continue;
422
Lennart Poettering9f6445e32013-12-24 16:39:37 +0100423 log_debug("unlink '%s'", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200424
425 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
426 if (errno != ENOENT) {
Michal Schmidt56f64d92014-11-28 19:29:59 +0100427 log_error_errno(errno, "unlink(%s): %m", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200428 r = -errno;
429 }
430 }
431
432 deleted = true;
433 }
434 }
435
436finish:
437 if (deleted) {
438 /* Restore original directory timestamps */
439 times[0] = ds->st_atim;
440 times[1] = ds->st_mtim;
441
442 if (futimens(dirfd(d), times) < 0)
Michal Schmidt56f64d92014-11-28 19:29:59 +0100443 log_error_errno(errno, "utimensat(%s): %m", p);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200444 }
445
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200446 return r;
447}
448
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200449static int item_set_perms(Item *i, const char *path) {
Michael Olbrich1924a972014-08-17 09:45:00 +0200450 struct stat st;
451 bool st_valid;
452
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200453 assert(i);
454 assert(path);
455
Michael Olbrich1924a972014-08-17 09:45:00 +0200456 st_valid = stat(path, &st) == 0;
457
Michal Schmidt062e01b2011-12-16 18:00:11 +0100458 /* not using i->path directly because it may be a glob */
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200459 if (i->mode_set) {
460 mode_t m = i->mode;
461
Michael Olbrich1924a972014-08-17 09:45:00 +0200462 if (i->mask_perms && st_valid) {
463 if (!(st.st_mode & 0111))
464 m &= ~0111;
465 if (!(st.st_mode & 0222))
466 m &= ~0222;
467 if (!(st.st_mode & 0444))
468 m &= ~0444;
469 if (!S_ISDIR(st.st_mode))
470 m &= ~07000; /* remove sticky/sgid/suid bit, unless directory */
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200471 }
472
Michael Olbrich1924a972014-08-17 09:45:00 +0200473 if (!st_valid || m != (st.st_mode & 07777)) {
Michal Schmidt4a62c712014-11-28 19:57:32 +0100474 if (chmod(path, m) < 0)
475 return log_error_errno(errno, "chmod(%s) failed: %m", path);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100476 }
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200477 }
Michal Schmidt062e01b2011-12-16 18:00:11 +0100478
Michael Olbrich1924a972014-08-17 09:45:00 +0200479 if ((!st_valid || (i->uid != st.st_uid || i->gid != st.st_gid)) &&
480 (i->uid_set || i->gid_set))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100481 if (chown(path,
Lennart Poetteringfed1e722014-11-28 20:51:01 +0100482 i->uid_set ? i->uid : UID_INVALID,
483 i->gid_set ? i->gid : GID_INVALID) < 0) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100484
Michal Schmidt56f64d92014-11-28 19:29:59 +0100485 log_error_errno(errno, "chown(%s) failed: %m", path);
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200486 return -errno;
Michal Schmidt062e01b2011-12-16 18:00:11 +0100487 }
488
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200489 return label_fix(path, false, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100490}
491
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100492static int get_xattrs_from_arg(Item *i) {
493 char *xattr;
494 const char *p;
495 int r;
496
497 assert(i);
498
499 if (!i->argument) {
500 log_error("%s: Argument can't be empty!", i->path);
501 return -EBADMSG;
502 }
503 p = i->argument;
504
505 while ((r = unquote_first_word(&p, &xattr, false)) > 0) {
506 _cleanup_free_ char *tmp = NULL, *name = NULL, *value = NULL;
507 r = split_pair(xattr, "=", &name, &value);
508 if (r < 0) {
509 log_warning("Illegal xattr found: \"%s\" - ignoring.", xattr);
510 free(xattr);
511 continue;
512 }
513 free(xattr);
514 if (streq(name, "") || streq(value, "")) {
515 log_warning("Malformed xattr found: \"%s=%s\" - ignoring.", name, value);
516 continue;
517 }
518 tmp = unquote(value, "\"");
519 if (!tmp)
520 return log_oom();
521 free(value);
522 value = cunescape(tmp);
523 if (!value)
524 return log_oom();
525 if (strv_consume_pair(&i->xattrs, name, value) < 0)
526 return log_oom();
527 name = value = NULL;
528 }
529
530 return r;
531}
532
533static int item_set_xattrs(Item *i, const char *path) {
534 char **name, **value;
535
536 assert(i);
537 assert(path);
538
539 if (strv_isempty(i->xattrs))
540 return 0;
541
542 STRV_FOREACH_PAIR(name, value, i->xattrs) {
543 int n;
544 n = strlen(*value);
545 if (lsetxattr(path, *name, *value, n, 0) < 0) {
546 log_error("Setting extended attribute %s=%s on %s failed: %m", *name, *value, path);
547 return -errno;
548 }
549 }
550 return 0;
551}
552
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400553static int write_one_file(Item *i, const char *path) {
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200554 _cleanup_close_ int fd = -1;
555 int flags, r = 0;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400556 struct stat st;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400557
Lennart Poettering874f1942014-06-10 22:48:56 +0200558 assert(i);
559 assert(path);
560
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200561 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND|O_NOFOLLOW :
562 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC|O_NOFOLLOW : 0;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400563
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200564 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200565 mac_selinux_create_file_prepare(path, S_IFREG);
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200566 fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200567 mac_selinux_create_file_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200568 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400569
570 if (fd < 0) {
571 if (i->type == WRITE_FILE && errno == ENOENT)
572 return 0;
573
Michal Schmidt56f64d92014-11-28 19:29:59 +0100574 log_error_errno(errno, "Failed to create file %s: %m", path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400575 return -errno;
576 }
577
578 if (i->argument) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200579 _cleanup_free_ char *unescaped;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400580 ssize_t n;
581 size_t l;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400582
Dave Reisner54693d92012-09-15 12:58:49 -0400583 unescaped = cunescape(i->argument);
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200584 if (!unescaped)
Dave Reisner54693d92012-09-15 12:58:49 -0400585 return log_oom();
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400586
Dave Reisner54693d92012-09-15 12:58:49 -0400587 l = strlen(unescaped);
588 n = write(fd, unescaped, l);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400589
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400590 if (n < 0 || (size_t) n < l) {
591 log_error("Failed to write file %s: %s", path, n < 0 ? strerror(-n) : "Short write");
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400592 return n < 0 ? n : -EIO;
593 }
594 }
595
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200596 fd = safe_close(fd);
Dave Reisner3612fbc2012-09-12 16:21:00 -0400597
Michal Schmidt4a62c712014-11-28 19:57:32 +0100598 if (stat(path, &st) < 0)
599 return log_error_errno(errno, "stat(%s) failed: %m", path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400600
601 if (!S_ISREG(st.st_mode)) {
602 log_error("%s is not a file.", path);
603 return -EEXIST;
604 }
605
606 r = item_set_perms(i, path);
607 if (r < 0)
608 return r;
609
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100610 r = item_set_xattrs(i, i->path);
611 if (r < 0)
612 return r;
613
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400614 return 0;
615}
616
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200617static int item_set_perms_children(Item *i, const char *path) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200618 _cleanup_closedir_ DIR *d;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200619 int r = 0;
620
621 assert(i);
622 assert(path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100623
624 /* This returns the first error we run into, but nevertheless
625 * tries to go on */
626
627 d = opendir(path);
628 if (!d)
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200629 return errno == ENOENT || errno == ENOTDIR ? 0 : -errno;
Michal Schmidta8d88782011-12-15 23:11:07 +0100630
631 for (;;) {
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200632 _cleanup_free_ char *p = NULL;
Lennart Poettering7d5e9c02012-09-19 22:21:09 +0200633 struct dirent *de;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200634 int q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100635
Florian Weimerd78096b2013-12-19 12:26:07 +0100636 errno = 0;
637 de = readdir(d);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200638 if (!de) {
639 if (errno != 0 && r == 0)
640 r = -errno;
641
Michal Schmidta8d88782011-12-15 23:11:07 +0100642 break;
643 }
644
Michal Schmidta8d88782011-12-15 23:11:07 +0100645 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
646 continue;
647
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200648 p = strjoin(path, "/", de->d_name, NULL);
649 if (!p)
650 return -ENOMEM;
Michal Schmidta8d88782011-12-15 23:11:07 +0100651
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200652 q = item_set_perms(i, p);
653 if (q < 0 && q != -ENOENT && r == 0)
654 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100655
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200656 if (IN_SET(de->d_type, DT_UNKNOWN, DT_DIR)) {
657 q = item_set_perms_children(i, p);
658 if (q < 0 && r == 0)
659 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100660 }
Michal Schmidta8d88782011-12-15 23:11:07 +0100661 }
662
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200663 return r;
Michal Schmidta8d88782011-12-15 23:11:07 +0100664}
665
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200666static int item_set_perms_recursive(Item *i, const char *path) {
667 int r, q;
668
669 assert(i);
670 assert(path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100671
Michal Schmidt062e01b2011-12-16 18:00:11 +0100672 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100673 if (r < 0)
674 return r;
675
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200676 q = item_set_perms_children(i, path);
677 if (q < 0 && r == 0)
678 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100679
680 return r;
681}
682
Michal Schmidt99e68c02011-12-15 23:45:26 +0100683static int glob_item(Item *i, int (*action)(Item *, const char *)) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200684 _cleanup_globfree_ glob_t g = {};
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200685 int r = 0, k;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100686 char **fn;
687
Michal Schmidt99e68c02011-12-15 23:45:26 +0100688 errno = 0;
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400689 k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200690 if (k != 0 && k != GLOB_NOMATCH) {
691 if (errno == 0)
692 errno = EIO;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100693
Michal Schmidt56f64d92014-11-28 19:29:59 +0100694 log_error_errno(errno, "glob(%s) failed: %m", i->path);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200695 return -errno;
696 }
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400697
698 STRV_FOREACH(fn, g.gl_pathv) {
699 k = action(i, *fn);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200700 if (k < 0 && r == 0)
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400701 r = k;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100702 }
703
Michal Schmidt99e68c02011-12-15 23:45:26 +0100704 return r;
705}
706
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200707static int create_item(Item *i) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200708 struct stat st;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200709 int r = 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200710
711 assert(i);
712
713 switch (i->type) {
714
715 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100716 case IGNORE_DIRECTORY_PATH:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200717 case REMOVE_PATH:
718 case RECURSIVE_REMOVE_PATH:
719 return 0;
720
721 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100722 case TRUNCATE_FILE:
Dave Reisner1845fdd2012-09-27 20:48:13 -0400723 r = write_one_file(i, i->path);
724 if (r < 0)
725 return r;
726 break;
Lennart Poettering265ffa12013-09-17 16:33:30 -0500727
Lennart Poettering849958d2014-06-10 23:02:40 +0200728 case COPY_FILES:
Lennart Poetteringe1563472014-06-19 19:36:08 +0200729 r = copy_tree(i->argument, i->path, false);
Lennart Poettering849958d2014-06-10 23:02:40 +0200730 if (r < 0) {
Lennart Poetteringe1563472014-06-19 19:36:08 +0200731 struct stat a, b;
732
Michal Schmidt8d3d7072014-11-28 19:13:53 +0100733 if (r != -EEXIST)
734 return log_error_errno(r, "Failed to copy files to %s: %m", i->path);
Lennart Poetteringe1563472014-06-19 19:36:08 +0200735
Michal Schmidt4a62c712014-11-28 19:57:32 +0100736 if (stat(i->argument, &a) < 0)
737 return log_error_errno(errno, "stat(%s) failed: %m", i->argument);
Lennart Poetteringe1563472014-06-19 19:36:08 +0200738
Michal Schmidt4a62c712014-11-28 19:57:32 +0100739 if (stat(i->path, &b) < 0)
740 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Lennart Poetteringe1563472014-06-19 19:36:08 +0200741
742 if ((a.st_mode ^ b.st_mode) & S_IFMT) {
743 log_debug("Can't copy to %s, file exists already and is of different type", i->path);
744 return 0;
745 }
Lennart Poettering849958d2014-06-10 23:02:40 +0200746 }
747
748 r = item_set_perms(i, i->path);
749 if (r < 0)
750 return r;
751
752 break;
753
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400754 case WRITE_FILE:
755 r = glob_item(i, write_one_file);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100756 if (r < 0)
757 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200758
759 break;
760
761 case TRUNCATE_DIRECTORY:
762 case CREATE_DIRECTORY:
763
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200764 RUN_WITH_UMASK(0000) {
765 mkdir_parents_label(i->path, 0755);
Lennart Poettering6f045292014-06-18 00:02:08 +0200766 r = mkdir_label(i->path, i->mode);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200767 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200768
Lennart Poetteringe1563472014-06-19 19:36:08 +0200769 if (r < 0) {
Michal Schmidtf6479622014-11-28 18:50:43 +0100770 if (r != -EEXIST)
771 return log_error_errno(r, "Failed to create directory %s: %m", i->path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200772
Michal Schmidt4a62c712014-11-28 19:57:32 +0100773 if (stat(i->path, &st) < 0)
774 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200775
Lennart Poetteringe1563472014-06-19 19:36:08 +0200776 if (!S_ISDIR(st.st_mode)) {
777 log_debug("%s already exists and is not a directory.", i->path);
778 return 0;
779 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200780 }
781
Michal Schmidt062e01b2011-12-16 18:00:11 +0100782 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100783 if (r < 0)
784 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200785
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100786 r = item_set_xattrs(i, i->path);
787 if (r < 0)
788 return r;
789
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200790 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200791
792 case CREATE_FIFO:
793
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200794 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200795 mac_selinux_create_file_prepare(i->path, S_IFIFO);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200796 r = mkfifo(i->path, i->mode);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200797 mac_selinux_create_file_clear();
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200798 }
799
Lennart Poettering1554afa2014-06-17 23:50:22 +0200800 if (r < 0) {
Michal Schmidt4a62c712014-11-28 19:57:32 +0100801 if (errno != EEXIST)
802 return log_error_errno(errno, "Failed to create fifo %s: %m", i->path);
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200803
Michal Schmidt4a62c712014-11-28 19:57:32 +0100804 if (stat(i->path, &st) < 0)
805 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200806
807 if (!S_ISFIFO(st.st_mode)) {
808
809 if (i->force) {
810
811 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200812 mac_selinux_create_file_prepare(i->path, S_IFIFO);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200813 r = mkfifo_atomic(i->path, i->mode);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200814 mac_selinux_create_file_clear();
Lennart Poettering1554afa2014-06-17 23:50:22 +0200815 }
816
Michal Schmidtf6479622014-11-28 18:50:43 +0100817 if (r < 0)
818 return log_error_errno(r, "Failed to create fifo %s: %m", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200819 } else {
820 log_debug("%s is not a fifo.", i->path);
821 return 0;
822 }
823 }
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200824 }
825
Michal Schmidt062e01b2011-12-16 18:00:11 +0100826 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100827 if (r < 0)
828 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200829
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100830 r = item_set_xattrs(i, i->path);
831 if (r < 0)
832 return r;
833
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200834 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100835
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200836 case CREATE_SYMLINK:
Lennart Poettering468d7262012-01-17 15:04:12 +0100837
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200838 mac_selinux_create_file_prepare(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100839 r = symlink(i->argument, i->path);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200840 mac_selinux_create_file_clear();
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200841
Lennart Poettering468d7262012-01-17 15:04:12 +0100842 if (r < 0) {
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200843 _cleanup_free_ char *x = NULL;
Lennart Poettering468d7262012-01-17 15:04:12 +0100844
Michal Schmidt4a62c712014-11-28 19:57:32 +0100845 if (errno != EEXIST)
846 return log_error_errno(errno, "symlink(%s, %s) failed: %m", i->argument, i->path);
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200847
848 r = readlink_malloc(i->path, &x);
849 if (r < 0 || !streq(i->argument, x)) {
850
851 if (i->force) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200852 mac_selinux_create_file_prepare(i->path, S_IFLNK);
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200853 r = symlink_atomic(i->argument, i->path);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200854 mac_selinux_create_file_clear();
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200855
Michal Schmidtf6479622014-11-28 18:50:43 +0100856 if (r < 0)
857 return log_error_errno(r, "symlink(%s, %s) failed: %m", i->argument, i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200858 } else {
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200859 log_debug("%s is not a symlink or does not point to the correct path.", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200860 return 0;
861 }
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200862 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100863 }
864
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100865 r = item_set_xattrs(i, i->path);
866 if (r < 0)
867 return r;
868
Lennart Poettering468d7262012-01-17 15:04:12 +0100869 break;
Lennart Poettering468d7262012-01-17 15:04:12 +0100870
871 case CREATE_BLOCK_DEVICE:
872 case CREATE_CHAR_DEVICE: {
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700873 mode_t file_type;
874
875 if (have_effective_cap(CAP_MKNOD) == 0) {
876 /* In a container we lack CAP_MKNOD. We
Anatol Pomozovab06eef2013-04-14 19:37:54 -0700877 shouldn't attempt to create the device node in
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700878 that case to avoid noise, and we don't support
879 virtualized devices in containers anyway. */
880
881 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
882 return 0;
883 }
884
Lennart Poettering1554afa2014-06-17 23:50:22 +0200885 file_type = i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR;
Lennart Poettering468d7262012-01-17 15:04:12 +0100886
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200887 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200888 mac_selinux_create_file_prepare(i->path, file_type);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200889 r = mknod(i->path, i->mode | file_type, i->major_minor);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200890 mac_selinux_create_file_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200891 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100892
Kay Sievers6555ad82014-06-13 04:11:11 +0200893 if (r < 0) {
894 if (errno == EPERM) {
895 log_debug("We lack permissions, possibly because of cgroup configuration; "
896 "skipping creation of device node %s.", i->path);
897 return 0;
898 }
899
Michal Schmidt4a62c712014-11-28 19:57:32 +0100900 if (errno != EEXIST)
901 return log_error_errno(errno, "Failed to create device node %s: %m", i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100902
Michal Schmidt4a62c712014-11-28 19:57:32 +0100903 if (stat(i->path, &st) < 0)
904 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100905
Lennart Poettering1554afa2014-06-17 23:50:22 +0200906 if ((st.st_mode & S_IFMT) != file_type) {
907
908 if (i->force) {
909
910 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200911 mac_selinux_create_file_prepare(i->path, file_type);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200912 r = mknod_atomic(i->path, i->mode | file_type, i->major_minor);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200913 mac_selinux_create_file_clear();
Lennart Poettering1554afa2014-06-17 23:50:22 +0200914 }
915
Michal Schmidtf6479622014-11-28 18:50:43 +0100916 if (r < 0)
917 return log_error_errno(r, "Failed to create device node %s: %m", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200918 } else {
919 log_debug("%s is not a device node.", i->path);
920 return 0;
921 }
922 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100923 }
924
925 r = item_set_perms(i, i->path);
926 if (r < 0)
927 return r;
928
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100929 r = item_set_xattrs(i, i->path);
930 if (r < 0)
931 return r;
932
Lennart Poettering468d7262012-01-17 15:04:12 +0100933 break;
934 }
935
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200936 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100937 case RELABEL_PATH:
938
939 r = glob_item(i, item_set_perms);
940 if (r < 0)
Lennart Poettering96ca8192013-06-21 15:57:42 +0200941 return r;
Michal Schmidt777b87e2011-12-16 18:27:35 +0100942 break;
943
Michal Schmidta8d88782011-12-15 23:11:07 +0100944 case RECURSIVE_RELABEL_PATH:
945
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200946 r = glob_item(i, item_set_perms_recursive);
Michal Schmidta8d88782011-12-15 23:11:07 +0100947 if (r < 0)
948 return r;
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100949 break;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200950
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100951 case SET_XATTR:
952 r = item_set_xattrs(i, i->path);
953 if (r < 0)
954 return r;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200955 break;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200956 }
957
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200958 log_debug("%s created successfully.", i->path);
959
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100960 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200961}
962
Michal Schmidta0896122011-12-15 21:32:50 +0100963static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200964 int r;
965
966 assert(i);
967
968 switch (i->type) {
969
970 case CREATE_FILE:
971 case TRUNCATE_FILE:
972 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200973 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100974 case CREATE_SYMLINK:
975 case CREATE_BLOCK_DEVICE:
976 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200977 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100978 case IGNORE_DIRECTORY_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200979 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100980 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100981 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100982 case WRITE_FILE:
Lennart Poettering849958d2014-06-10 23:02:40 +0200983 case COPY_FILES:
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100984 case SET_XATTR:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200985 break;
986
987 case REMOVE_PATH:
Michal Schmidt4a62c712014-11-28 19:57:32 +0100988 if (remove(instance) < 0 && errno != ENOENT)
989 return log_error_errno(errno, "remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200990
991 break;
992
993 case TRUNCATE_DIRECTORY:
994 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +0200995 /* FIXME: we probably should use dir_cleanup() here
996 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +0200997 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Michal Schmidtf6479622014-11-28 18:50:43 +0100998 if (r < 0 && r != -ENOENT)
999 return log_error_errno(r, "rm_rf(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001000
1001 break;
1002 }
1003
1004 return 0;
1005}
1006
Michal Schmidta0896122011-12-15 21:32:50 +01001007static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +01001008 int r = 0;
1009
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001010 assert(i);
1011
1012 switch (i->type) {
1013
1014 case CREATE_FILE:
1015 case TRUNCATE_FILE:
1016 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +02001017 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +01001018 case CREATE_SYMLINK:
1019 case CREATE_CHAR_DEVICE:
1020 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001021 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001022 case IGNORE_DIRECTORY_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001023 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001024 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +01001025 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001026 case WRITE_FILE:
Lennart Poettering849958d2014-06-10 23:02:40 +02001027 case COPY_FILES:
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001028 case SET_XATTR:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001029 break;
1030
1031 case REMOVE_PATH:
1032 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +01001033 case RECURSIVE_REMOVE_PATH:
1034 r = glob_item(i, remove_item_instance);
1035 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001036 }
1037
Michal Schmidt99e68c02011-12-15 23:45:26 +01001038 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001039}
1040
Michal Sekletar78a92a52013-01-18 16:13:08 +01001041static int clean_item_instance(Item *i, const char* instance) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +02001042 _cleanup_closedir_ DIR *d = NULL;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001043 struct stat s, ps;
1044 bool mountpoint;
1045 int r;
1046 usec_t cutoff, n;
1047
1048 assert(i);
1049
1050 if (!i->age_set)
1051 return 0;
1052
1053 n = now(CLOCK_REALTIME);
1054 if (n < i->age)
1055 return 0;
1056
1057 cutoff = n - i->age;
1058
1059 d = opendir(instance);
1060 if (!d) {
1061 if (errno == ENOENT || errno == ENOTDIR)
1062 return 0;
1063
Michal Schmidt56f64d92014-11-28 19:29:59 +01001064 log_error_errno(errno, "Failed to open directory %s: %m", i->path);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001065 return -errno;
1066 }
1067
Michal Schmidt4a62c712014-11-28 19:57:32 +01001068 if (fstat(dirfd(d), &s) < 0)
1069 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001070
1071 if (!S_ISDIR(s.st_mode)) {
1072 log_error("%s is not a directory.", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001073 return -ENOTDIR;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001074 }
1075
Michal Schmidt4a62c712014-11-28 19:57:32 +01001076 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0)
1077 return log_error_errno(errno, "stat(%s/..) failed: %m", i->path);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001078
1079 mountpoint = s.st_dev != ps.st_dev ||
1080 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
1081
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001082 r = dir_cleanup(i, instance, d, &s, cutoff, s.st_dev, mountpoint,
1083 MAX_DEPTH, i->keep_first_level);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001084 return r;
1085}
1086
1087static int clean_item(Item *i) {
1088 int r = 0;
1089
1090 assert(i);
1091
1092 switch (i->type) {
1093 case CREATE_DIRECTORY:
1094 case TRUNCATE_DIRECTORY:
1095 case IGNORE_PATH:
Lennart Poettering849958d2014-06-10 23:02:40 +02001096 case COPY_FILES:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001097 clean_item_instance(i, i->path);
1098 break;
1099 case IGNORE_DIRECTORY_PATH:
1100 r = glob_item(i, clean_item_instance);
1101 break;
1102 default:
1103 break;
1104 }
1105
1106 return r;
1107}
1108
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001109static int process_item(Item *i) {
1110 int r, q, p;
Zbigniew Jędrzejewski-Szmek9348f0e2014-10-01 07:33:22 -05001111 _cleanup_free_ char *prefix = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001112
1113 assert(i);
1114
Lennart Poettering1910cd02014-06-11 01:37:35 +02001115 if (i->done)
1116 return 0;
1117
1118 i->done = true;
1119
Zbigniew Jędrzejewski-Szmek9348f0e2014-10-01 07:33:22 -05001120 prefix = malloc(strlen(i->path) + 1);
1121 if (!prefix)
1122 return log_oom();
1123
Lennart Poettering1910cd02014-06-11 01:37:35 +02001124 PATH_FOREACH_PREFIX(prefix, i->path) {
1125 Item *j;
1126
1127 j = hashmap_get(items, prefix);
1128 if (j)
1129 process_item(j);
1130 }
1131
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001132 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +01001133 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001134 p = arg_clean ? clean_item(i) : 0;
1135
1136 if (r < 0)
1137 return r;
1138
1139 if (q < 0)
1140 return q;
1141
1142 return p;
1143}
1144
1145static void item_free(Item *i) {
Lennart Poettering753615e2014-06-12 23:07:17 +02001146
1147 if (!i)
1148 return;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001149
1150 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +01001151 free(i->argument);
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001152 strv_free(i->xattrs);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001153 free(i);
1154}
1155
Lennart Poettering14bf2c92013-10-14 04:59:26 +02001156DEFINE_TRIVIAL_CLEANUP_FUNC(Item*, item_free);
Maciej Wereskie2f2fb72013-07-19 15:43:12 +02001157
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001158static bool item_equal(Item *a, Item *b) {
1159 assert(a);
1160 assert(b);
1161
1162 if (!streq_ptr(a->path, b->path))
1163 return false;
1164
1165 if (a->type != b->type)
1166 return false;
1167
1168 if (a->uid_set != b->uid_set ||
1169 (a->uid_set && a->uid != b->uid))
1170 return false;
1171
1172 if (a->gid_set != b->gid_set ||
1173 (a->gid_set && a->gid != b->gid))
1174 return false;
1175
1176 if (a->mode_set != b->mode_set ||
1177 (a->mode_set && a->mode != b->mode))
1178 return false;
1179
1180 if (a->age_set != b->age_set ||
1181 (a->age_set && a->age != b->age))
1182 return false;
1183
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001184 if ((a->type == CREATE_FILE ||
1185 a->type == TRUNCATE_FILE ||
1186 a->type == WRITE_FILE ||
Lennart Poettering849958d2014-06-10 23:02:40 +02001187 a->type == CREATE_SYMLINK ||
1188 a->type == COPY_FILES) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +01001189 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +01001190 return false;
1191
1192 if ((a->type == CREATE_CHAR_DEVICE ||
1193 a->type == CREATE_BLOCK_DEVICE) &&
1194 a->major_minor != b->major_minor)
1195 return false;
1196
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001197 return true;
1198}
1199
Dave Reisnera2aced42013-07-24 11:10:05 -04001200static bool should_include_path(const char *path) {
1201 char **prefix;
1202
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001203 STRV_FOREACH(prefix, arg_exclude_prefixes)
Dave Reisner5c795112013-07-24 11:19:24 -04001204 if (path_startswith(path, *prefix))
1205 return false;
Dave Reisnera2aced42013-07-24 11:10:05 -04001206
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001207 STRV_FOREACH(prefix, arg_include_prefixes)
Dave Reisnera2aced42013-07-24 11:10:05 -04001208 if (path_startswith(path, *prefix))
1209 return true;
Dave Reisnera2aced42013-07-24 11:10:05 -04001210
Dave Reisner5c795112013-07-24 11:19:24 -04001211 /* no matches, so we should include this path only if we
1212 * have no whitelist at all */
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001213 return strv_length(arg_include_prefixes) == 0;
Dave Reisnera2aced42013-07-24 11:10:05 -04001214}
1215
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001216static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001217
1218 static const Specifier specifier_table[] = {
1219 { 'm', specifier_machine_id, NULL },
1220 { 'b', specifier_boot_id, NULL },
1221 { 'H', specifier_host_name, NULL },
1222 { 'v', specifier_kernel_release, NULL },
1223 {}
1224 };
1225
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001226 _cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
1227 _cleanup_(item_freep) Item *i = NULL;
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001228 Item *existing;
Michal Schmidt66ccd032011-12-15 21:31:14 +01001229 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001230 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001231 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +02001232
1233 assert(fname);
1234 assert(line >= 1);
1235 assert(buffer);
1236
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001237 r = sscanf(buffer,
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001238 "%ms %ms %ms %ms %ms %ms %n",
1239 &action,
Lennart Poettering1731e342013-09-17 11:02:02 -05001240 &path,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +01001241 &mode,
1242 &user,
1243 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +01001244 &age,
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001245 &n);
1246 if (r < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001247 log_error("[%s:%u] Syntax error.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001248 return -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +02001249 }
1250
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001251 if (isempty(action)) {
1252 log_error("[%s:%u] Command too short '%s'.", fname, line, action);
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001253 return -EINVAL;
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001254 }
1255
1256 if (strlen(action) > 1 && !in_charset(action+1, "!+")) {
1257 log_error("[%s:%u] Unknown modifiers in command '%s'", fname, line, action);
1258 return -EINVAL;
1259 }
1260
1261 if (strchr(action+1, '!') && !arg_boot)
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001262 return 0;
1263
1264 type = action[0];
1265
Lennart Poettering1731e342013-09-17 11:02:02 -05001266 i = new0(Item, 1);
1267 if (!i)
1268 return log_oom();
1269
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001270 i->force = !!strchr(action+1, '+');
1271
Lennart Poettering1731e342013-09-17 11:02:02 -05001272 r = specifier_printf(path, specifier_table, NULL, &i->path);
1273 if (r < 0) {
1274 log_error("[%s:%u] Failed to replace specifiers: %s", fname, line, path);
1275 return r;
1276 }
1277
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001278 if (n >= 0) {
1279 n += strspn(buffer+n, WHITESPACE);
1280 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
1281 i->argument = unquote(buffer+n, "\"");
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001282 if (!i->argument)
1283 return log_oom();
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001284 }
1285 }
1286
Lennart Poettering753615e2014-06-12 23:07:17 +02001287 switch (type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001288
Michal Schmidt777b87e2011-12-16 18:27:35 +01001289 case CREATE_FILE:
1290 case TRUNCATE_FILE:
1291 case CREATE_DIRECTORY:
1292 case TRUNCATE_DIRECTORY:
1293 case CREATE_FIFO:
1294 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001295 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001296 case REMOVE_PATH:
1297 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001298 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001299 case RELABEL_PATH:
1300 case RECURSIVE_RELABEL_PATH:
1301 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001302
1303 case CREATE_SYMLINK:
1304 if (!i->argument) {
Kay Sievers2f3b8732014-06-20 15:57:43 +02001305 i->argument = strappend("/usr/share/factory", i->path);
1306 if (!i->argument)
1307 return log_oom();
Lennart Poettering468d7262012-01-17 15:04:12 +01001308 }
1309 break;
1310
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001311 case WRITE_FILE:
1312 if (!i->argument) {
1313 log_error("[%s:%u] Write file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001314 return -EBADMSG;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001315 }
1316 break;
1317
Lennart Poettering849958d2014-06-10 23:02:40 +02001318 case COPY_FILES:
1319 if (!i->argument) {
Kay Sievers2f3b8732014-06-20 15:57:43 +02001320 i->argument = strappend("/usr/share/factory", i->path);
1321 if (!i->argument)
1322 return log_oom();
Lennart Poettering849958d2014-06-10 23:02:40 +02001323 }
1324
1325 if (!path_is_absolute(i->argument)) {
1326 log_error("[%s:%u] Source path is not absolute.", fname, line);
1327 return -EBADMSG;
1328 }
1329
1330 path_kill_slashes(i->argument);
1331 break;
1332
Lennart Poettering468d7262012-01-17 15:04:12 +01001333 case CREATE_CHAR_DEVICE:
1334 case CREATE_BLOCK_DEVICE: {
1335 unsigned major, minor;
1336
1337 if (!i->argument) {
1338 log_error("[%s:%u] Device file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001339 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001340 }
1341
1342 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1343 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 -04001344 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001345 }
1346
1347 i->major_minor = makedev(major, minor);
1348 break;
1349 }
1350
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001351 case SET_XATTR:
1352 if (!i->argument) {
1353 log_error("[%s:%u] Set extended attribute requires argument.", fname, line);
1354 return -EBADMSG;
1355 }
1356 r = get_xattrs_from_arg(i);
1357 if (r < 0)
1358 return r;
1359 break;
1360
Michal Schmidt777b87e2011-12-16 18:27:35 +01001361 default:
Lennart Poettering753615e2014-06-12 23:07:17 +02001362 log_error("[%s:%u] Unknown command type '%c'.", fname, line, type);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001363 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001364 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001365
Michal Schmidta8d88782011-12-15 23:11:07 +01001366 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001367
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001368 if (!path_is_absolute(i->path)) {
1369 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001370 return -EBADMSG;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001371 }
1372
1373 path_kill_slashes(i->path);
1374
Dave Reisnera2aced42013-07-24 11:10:05 -04001375 if (!should_include_path(i->path))
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001376 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001377
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001378 if (arg_root) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001379 char *p;
1380
1381 p = strappend(arg_root, i->path);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001382 if (!p)
1383 return log_oom();
1384
1385 free(i->path);
1386 i->path = p;
1387 }
1388
Lennart Poettering5008d582010-09-28 02:34:02 +02001389 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001390 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001391
Lennart Poetteringd05c5032012-07-16 12:34:54 +02001392 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001393 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001394 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001395 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001396 }
1397
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001398 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001399 }
1400
1401 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001402 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001403
Lennart Poettering4b678342011-07-23 01:17:59 +02001404 r = get_group_creds(&g, &i->gid);
1405 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001406 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001407 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001408 }
1409
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001410 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001411 }
1412
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001413 if (mode && !streq(mode, "-")) {
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001414 const char *mm = mode;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001415 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001416
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001417 if (*mm == '~') {
1418 i->mask_perms = true;
1419 mm++;
1420 }
1421
1422 if (sscanf(mm, "%o", &m) != 1) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001423 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001424 return -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001425 }
1426
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001427 i->mode = m;
1428 i->mode_set = true;
1429 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001430 i->mode =
1431 i->type == CREATE_DIRECTORY ||
1432 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001433
1434 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001435 const char *a = age;
1436
1437 if (*a == '~') {
1438 i->keep_first_level = true;
1439 a++;
1440 }
1441
Lennart Poettering7f602782013-04-02 20:38:16 +02001442 if (parse_sec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001443 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001444 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001445 }
1446
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001447 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001448 }
1449
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001450 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001451
Lennart Poettering468d7262012-01-17 15:04:12 +01001452 existing = hashmap_get(h, i->path);
1453 if (existing) {
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001454 if (i->type == SET_XATTR) {
1455 r = strv_extend_strv(&existing->xattrs, i->xattrs);
1456 if (r < 0)
1457 return log_oom();
1458 return 0;
1459 } else if (existing->type == SET_XATTR) {
1460 r = strv_extend_strv(&i->xattrs, existing->xattrs);
1461 if (r < 0)
1462 return log_oom();
1463 r = hashmap_replace(h, i->path, i);
1464 if (r < 0) {
1465 log_error("Failed to replace item for %s.", i->path);
1466 return r;
1467 }
1468 item_free(existing);
1469 } else {
1470 /* Two identical items are fine */
1471 if (!item_equal(existing, i))
1472 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1473 return 0;
1474 }
1475 } else {
1476 r = hashmap_put(h, i->path, i);
1477 if (r < 0) {
1478 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
1479 return r;
1480 }
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001481 }
1482
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001483 i = NULL; /* avoid cleanup */
Lennart Poettering5008d582010-09-28 02:34:02 +02001484
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001485 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001486}
1487
Zbigniew Jędrzejewski-Szmek601185b2014-08-02 11:12:21 -04001488static void help(void) {
Lennart Poettering522d4a42011-02-13 15:08:15 +01001489 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1490 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001491 " -h --help Show this help\n"
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001492 " --version Show package version\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001493 " --create Create marked files/directories\n"
1494 " --clean Clean up marked directories\n"
1495 " --remove Remove marked files/directories\n"
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001496 " --boot Execute actions only safe at boot\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001497 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n"
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001498 " --exclude-prefix=PATH Ignore rules that apply to paths with the specified prefix\n"
1499 " --root=PATH Operate on an alternate filesystem root\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001500 program_invocation_short_name);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001501}
1502
1503static int parse_argv(int argc, char *argv[]) {
1504
1505 enum {
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001506 ARG_VERSION = 0x100,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001507 ARG_CREATE,
1508 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001509 ARG_REMOVE,
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001510 ARG_BOOT,
Dave Reisner5c795112013-07-24 11:19:24 -04001511 ARG_PREFIX,
1512 ARG_EXCLUDE_PREFIX,
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001513 ARG_ROOT,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001514 };
1515
1516 static const struct option options[] = {
Dave Reisner5c795112013-07-24 11:19:24 -04001517 { "help", no_argument, NULL, 'h' },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001518 { "version", no_argument, NULL, ARG_VERSION },
Dave Reisner5c795112013-07-24 11:19:24 -04001519 { "create", no_argument, NULL, ARG_CREATE },
1520 { "clean", no_argument, NULL, ARG_CLEAN },
1521 { "remove", no_argument, NULL, ARG_REMOVE },
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001522 { "boot", no_argument, NULL, ARG_BOOT },
Dave Reisner5c795112013-07-24 11:19:24 -04001523 { "prefix", required_argument, NULL, ARG_PREFIX },
1524 { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX },
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001525 { "root", required_argument, NULL, ARG_ROOT },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001526 {}
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001527 };
1528
1529 int c;
1530
1531 assert(argc >= 0);
1532 assert(argv);
1533
Zbigniew Jędrzejewski-Szmek601185b2014-08-02 11:12:21 -04001534 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001535
1536 switch (c) {
1537
1538 case 'h':
Zbigniew Jędrzejewski-Szmek601185b2014-08-02 11:12:21 -04001539 help();
1540 return 0;
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001541
1542 case ARG_VERSION:
1543 puts(PACKAGE_STRING);
1544 puts(SYSTEMD_FEATURES);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001545 return 0;
1546
1547 case ARG_CREATE:
1548 arg_create = true;
1549 break;
1550
1551 case ARG_CLEAN:
1552 arg_clean = true;
1553 break;
1554
1555 case ARG_REMOVE:
1556 arg_remove = true;
1557 break;
1558
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001559 case ARG_BOOT:
1560 arg_boot = true;
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001561 break;
1562
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001563 case ARG_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001564 if (strv_push(&arg_include_prefixes, optarg) < 0)
Dave Reisnera2aced42013-07-24 11:10:05 -04001565 return log_oom();
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001566 break;
1567
Dave Reisner5c795112013-07-24 11:19:24 -04001568 case ARG_EXCLUDE_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001569 if (strv_push(&arg_exclude_prefixes, optarg) < 0)
Dave Reisner5c795112013-07-24 11:19:24 -04001570 return log_oom();
1571 break;
1572
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001573 case ARG_ROOT:
Lennart Poettering753615e2014-06-12 23:07:17 +02001574 free(arg_root);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001575 arg_root = path_make_absolute_cwd(optarg);
1576 if (!arg_root)
1577 return log_oom();
Lennart Poettering753615e2014-06-12 23:07:17 +02001578
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001579 path_kill_slashes(arg_root);
1580 break;
1581
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001582 case '?':
1583 return -EINVAL;
1584
1585 default:
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001586 assert_not_reached("Unhandled option");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001587 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001588
1589 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001590 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001591 return -EINVAL;
1592 }
1593
1594 return 1;
1595}
1596
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001597static int read_config_file(const char *fn, bool ignore_enoent) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001598 _cleanup_fclose_ FILE *f = NULL;
1599 char line[LINE_MAX];
Michal Sekletar78a92a52013-01-18 16:13:08 +01001600 Iterator iterator;
Lennart Poettering1731e342013-09-17 11:02:02 -05001601 unsigned v = 0;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001602 Item *i;
Lennart Poettering1731e342013-09-17 11:02:02 -05001603 int r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001604
1605 assert(fn);
1606
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001607 r = search_and_fopen_nulstr(fn, "re", arg_root, conf_file_dirs, &f);
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001608 if (r < 0) {
1609 if (ignore_enoent && r == -ENOENT)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001610 return 0;
1611
Michal Schmidt8d3d7072014-11-28 19:13:53 +01001612 return log_error_errno(r, "Failed to open '%s', ignoring: %m", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001613 }
1614
Lennart Poettering1731e342013-09-17 11:02:02 -05001615 FOREACH_LINE(line, f, break) {
1616 char *l;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001617 int k;
1618
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001619 v++;
1620
1621 l = strstrip(line);
1622 if (*l == '#' || *l == 0)
1623 continue;
1624
Lennart Poettering1731e342013-09-17 11:02:02 -05001625 k = parse_line(fn, v, l);
1626 if (k < 0 && r == 0)
1627 r = k;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001628 }
1629
Michal Sekletar78a92a52013-01-18 16:13:08 +01001630 /* we have to determine age parameter for each entry of type X */
1631 HASHMAP_FOREACH(i, globs, iterator) {
1632 Iterator iter;
1633 Item *j, *candidate_item = NULL;
1634
1635 if (i->type != IGNORE_DIRECTORY_PATH)
1636 continue;
1637
1638 HASHMAP_FOREACH(j, items, iter) {
1639 if (j->type != CREATE_DIRECTORY && j->type != TRUNCATE_DIRECTORY)
1640 continue;
1641
1642 if (path_equal(j->path, i->path)) {
1643 candidate_item = j;
1644 break;
1645 }
1646
1647 if ((!candidate_item && path_startswith(i->path, j->path)) ||
1648 (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
1649 candidate_item = j;
1650 }
1651
Richard Weinberger9ed2a352014-09-09 11:09:37 +02001652 if (candidate_item && candidate_item->age_set) {
Michal Sekletar78a92a52013-01-18 16:13:08 +01001653 i->age = candidate_item->age;
1654 i->age_set = true;
1655 }
1656 }
1657
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001658 if (ferror(f)) {
Michal Schmidt56f64d92014-11-28 19:29:59 +01001659 log_error_errno(errno, "Failed to read from file %s: %m", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001660 if (r == 0)
1661 r = -EIO;
1662 }
1663
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001664 return r;
1665}
1666
Lennart Poettering5008d582010-09-28 02:34:02 +02001667int main(int argc, char *argv[]) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001668 int r, k;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001669 Item *i;
1670 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001671
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001672 r = parse_argv(argc, argv);
1673 if (r <= 0)
Lennart Poettering753615e2014-06-12 23:07:17 +02001674 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001675
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001676 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001677 log_parse_environment();
1678 log_open();
1679
Lennart Poettering4c126262011-08-01 20:52:18 +02001680 umask(0022);
1681
WaLyong Chocc56faf2014-10-23 17:23:46 +09001682 mac_selinux_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001683
Michal Schmidtd5099ef2014-08-13 01:00:18 +02001684 items = hashmap_new(&string_hash_ops);
1685 globs = hashmap_new(&string_hash_ops);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001686
1687 if (!items || !globs) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001688 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001689 goto finish;
1690 }
1691
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001692 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001693
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001694 if (optind < argc) {
1695 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001696
Dave Reisner91256702012-06-08 22:31:19 -04001697 for (j = optind; j < argc; j++) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001698 k = read_config_file(argv[j], false);
1699 if (k < 0 && r == 0)
1700 r = k;
Dave Reisner91256702012-06-08 22:31:19 -04001701 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001702
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001703 } else {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001704 _cleanup_strv_free_ char **files = NULL;
1705 char **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001706
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001707 r = conf_files_list_nulstr(&files, ".conf", arg_root, conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001708 if (r < 0) {
Michal Schmidtda927ba2014-11-28 13:19:16 +01001709 log_error_errno(r, "Failed to enumerate tmpfiles.d files: %m");
Kay Sievers44143302011-04-28 23:51:24 +02001710 goto finish;
1711 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001712
Kay Sievers772f8372011-04-25 21:38:21 +02001713 STRV_FOREACH(f, files) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001714 k = read_config_file(*f, true);
1715 if (k < 0 && r == 0)
1716 r = k;
Lennart Poettering5008d582010-09-28 02:34:02 +02001717 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001718 }
1719
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001720 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001721 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001722
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001723 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001724 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001725
Lennart Poettering5008d582010-09-28 02:34:02 +02001726finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001727 while ((i = hashmap_steal_first(items)))
1728 item_free(i);
1729
Lennart Poettering17b90522011-02-14 21:55:06 +01001730 while ((i = hashmap_steal_first(globs)))
1731 item_free(i);
1732
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001733 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001734 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001735
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001736 free(arg_include_prefixes);
1737 free(arg_exclude_prefixes);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001738 free(arg_root);
Dave Reisnera2aced42013-07-24 11:10:05 -04001739
Lennart Poettering17b90522011-02-14 21:55:06 +01001740 set_free_free(unix_sockets);
1741
WaLyong Chocc56faf2014-10-23 17:23:46 +09001742 mac_selinux_finish();
Lennart Poettering29003cf2010-10-19 19:36:45 +02001743
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001744 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Lennart Poettering5008d582010-09-28 02:34:02 +02001745}