blob: fb25b77b2bd2602b73ba9b6636b1e61b88807248 [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>
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 Poettering5008d582010-09-28 02:34:02 +020055
Andreas Jaeger01000472010-09-29 10:08:24 +020056/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020057 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020058 * properly owned directories beneath /tmp, /var/tmp, /run, which are
59 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020060
Michal Schmidt66ccd032011-12-15 21:31:14 +010061typedef enum ItemType {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010062 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020063 CREATE_FILE = 'f',
64 TRUNCATE_FILE = 'F',
Lennart Poettering31ed59c2012-01-18 16:39:04 +010065 WRITE_FILE = 'w',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020066 CREATE_DIRECTORY = 'd',
67 TRUNCATE_DIRECTORY = 'D',
Lennart Poetteringee17ee72011-07-12 03:56:56 +020068 CREATE_FIFO = 'p',
Lennart Poettering468d7262012-01-17 15:04:12 +010069 CREATE_SYMLINK = 'L',
70 CREATE_CHAR_DEVICE = 'c',
71 CREATE_BLOCK_DEVICE = 'b',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010072
73 /* These ones take globs */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020074 IGNORE_PATH = 'x',
Michal Sekletar78a92a52013-01-18 16:13:08 +010075 IGNORE_DIRECTORY_PATH = 'X',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020076 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010077 RECURSIVE_REMOVE_PATH = 'R',
Michal Schmidt777b87e2011-12-16 18:27:35 +010078 RELABEL_PATH = 'z',
Michal Schmidta8d88782011-12-15 23:11:07 +010079 RECURSIVE_RELABEL_PATH = 'Z'
Michal Schmidt66ccd032011-12-15 21:31:14 +010080} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020081
82typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +010083 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020084
85 char *path;
Lennart Poettering468d7262012-01-17 15:04:12 +010086 char *argument;
Lennart Poettering5008d582010-09-28 02:34:02 +020087 uid_t uid;
88 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020089 mode_t mode;
90 usec_t age;
91
Lennart Poettering468d7262012-01-17 15:04:12 +010092 dev_t major_minor;
93
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020094 bool uid_set:1;
95 bool gid_set:1;
96 bool mode_set:1;
97 bool age_set:1;
Lennart Poettering24f3a372012-06-20 09:05:50 +020098
99 bool keep_first_level:1;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200100} Item;
101
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100102static Hashmap *items = NULL, *globs = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100103static Set *unix_sockets = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200104
105static bool arg_create = false;
106static bool arg_clean = false;
107static bool arg_remove = false;
108
Dave Reisnera2aced42013-07-24 11:10:05 -0400109static char **include_prefixes = NULL;
Dave Reisner5c795112013-07-24 11:19:24 -0400110static char **exclude_prefixes = NULL;
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100111
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100112static const char conf_file_dirs[] =
113 "/etc/tmpfiles.d\0"
114 "/run/tmpfiles.d\0"
115 "/usr/local/lib/tmpfiles.d\0"
116 "/usr/lib/tmpfiles.d\0"
Lennart Poettering3f2afb22012-07-20 16:24:55 +0200117#ifdef HAVE_SPLIT_USR
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100118 "/lib/tmpfiles.d\0"
Lennart Poettering3f2afb22012-07-20 16:24:55 +0200119#endif
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100120 ;
Dave Reisner91256702012-06-08 22:31:19 -0400121
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200122#define MAX_DEPTH 256
123
Michal Schmidt66ccd032011-12-15 21:31:14 +0100124static bool needs_glob(ItemType t) {
Michal Sekletar78a92a52013-01-18 16:13:08 +0100125 return t == IGNORE_PATH || t == IGNORE_DIRECTORY_PATH || t == REMOVE_PATH || t == RECURSIVE_REMOVE_PATH || t == RELABEL_PATH || t == RECURSIVE_RELABEL_PATH;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100126}
127
128static struct Item* find_glob(Hashmap *h, const char *match) {
129 Item *j;
130 Iterator i;
131
132 HASHMAP_FOREACH(j, h, i)
133 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
134 return j;
135
136 return NULL;
137}
138
Lennart Poettering17b90522011-02-14 21:55:06 +0100139static void load_unix_sockets(void) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200140 _cleanup_fclose_ FILE *f = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100141 char line[LINE_MAX];
142
143 if (unix_sockets)
144 return;
145
146 /* We maintain a cache of the sockets we found in
147 * /proc/net/unix to speed things up a little. */
148
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100149 unix_sockets = set_new(string_hash_func, string_compare_func);
150 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100151 return;
152
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100153 f = fopen("/proc/net/unix", "re");
154 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100155 return;
156
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100157 /* Skip header */
158 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100159 goto fail;
160
161 for (;;) {
162 char *p, *s;
163 int k;
164
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100165 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100166 break;
167
168 truncate_nl(line);
169
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100170 p = strchr(line, ':');
171 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100172 continue;
173
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100174 if (strlen(p) < 37)
175 continue;
176
177 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100178 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100179 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100180 p += strspn(p, WHITESPACE);
181
182 if (*p != '/')
183 continue;
184
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100185 s = strdup(p);
186 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100187 goto fail;
188
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100189 path_kill_slashes(s);
190
Zbigniew Jędrzejewski-Szmekef422022013-04-22 23:12:15 -0400191 k = set_consume(unix_sockets, s);
192 if (k < 0 && k != -EEXIST)
193 goto fail;
Lennart Poettering17b90522011-02-14 21:55:06 +0100194 }
195
196 return;
197
198fail:
199 set_free_free(unix_sockets);
200 unix_sockets = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100201}
202
203static bool unix_socket_alive(const char *fn) {
204 assert(fn);
205
206 load_unix_sockets();
207
208 if (unix_sockets)
209 return !!set_get(unix_sockets, (char*) fn);
210
211 /* We don't know, so assume yes */
212 return true;
213}
214
Kay Sievers99d680a2013-03-13 13:12:36 +0100215static int dir_is_mount_point(DIR *d, const char *subdir) {
216 struct file_handle *h;
217 int mount_id_parent, mount_id;
218 int r_p, r;
219
220 h = alloca(MAX_HANDLE_SZ);
221
222 h->handle_bytes = MAX_HANDLE_SZ;
223 r_p = name_to_handle_at(dirfd(d), ".", h, &mount_id_parent, 0);
224 if (r_p < 0)
225 r_p = -errno;
226
227 h->handle_bytes = MAX_HANDLE_SZ;
228 r = name_to_handle_at(dirfd(d), subdir, h, &mount_id, 0);
229 if (r < 0)
230 r = -errno;
231
232 /* got no handle; make no assumptions, return error */
233 if (r_p < 0 && r < 0)
234 return r_p;
235
236 /* got both handles; if they differ, it is a mount point */
237 if (r_p >= 0 && r >= 0)
238 return mount_id_parent != mount_id;
239
240 /* got only one handle; assume different mount points if one
241 * of both queries was not supported by the filesystem */
242 if (r_p == -ENOSYS || r_p == -ENOTSUP || r == -ENOSYS || r == -ENOTSUP)
243 return true;
244
245 /* return error */
246 if (r_p < 0)
247 return r_p;
248 return r;
249}
250
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200251static int dir_cleanup(
Michal Sekletar78a92a52013-01-18 16:13:08 +0100252 Item *i,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200253 const char *p,
254 DIR *d,
255 const struct stat *ds,
256 usec_t cutoff,
257 dev_t rootdev,
258 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200259 int maxdepth,
260 bool keep_this_level)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200261{
262 struct dirent *dent;
263 struct timespec times[2];
264 bool deleted = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200265 int r = 0;
266
267 while ((dent = readdir(d))) {
268 struct stat s;
269 usec_t age;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200270 _cleanup_free_ char *sub_path = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200271
272 if (streq(dent->d_name, ".") ||
273 streq(dent->d_name, ".."))
274 continue;
275
276 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
277
278 if (errno != ENOENT) {
279 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
280 r = -errno;
281 }
282
283 continue;
284 }
285
286 /* Stay on the same filesystem */
287 if (s.st_dev != rootdev)
288 continue;
289
Kay Sievers99d680a2013-03-13 13:12:36 +0100290 /* Try to detect bind mounts of the same filesystem instance; they
291 * do not differ in device major/minors. This type of query is not
292 * supported on all kernels or filesystem types though. */
293 if (S_ISDIR(s.st_mode) && dir_is_mount_point(d, dent->d_name) > 0)
294 continue;
295
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200296 /* Do not delete read-only files owned by root */
297 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
298 continue;
299
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200300 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700301 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200302 goto finish;
303 }
304
305 /* Is there an item configured for this path? */
306 if (hashmap_get(items, sub_path))
307 continue;
308
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100309 if (find_glob(globs, sub_path))
310 continue;
311
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200312 if (S_ISDIR(s.st_mode)) {
313
314 if (mountpoint &&
315 streq(dent->d_name, "lost+found") &&
316 s.st_uid == 0)
317 continue;
318
319 if (maxdepth <= 0)
320 log_warning("Reached max depth on %s.", sub_path);
321 else {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200322 _cleanup_closedir_ DIR *sub_dir;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200323 int q;
324
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200325 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200326 if (sub_dir == NULL) {
327 if (errno != ENOENT) {
328 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
329 r = -errno;
330 }
331
332 continue;
333 }
334
Michal Sekletar78a92a52013-01-18 16:13:08 +0100335 q = dir_cleanup(i, sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200336
337 if (q < 0)
338 r = q;
339 }
340
Lennart Poettering24f3a372012-06-20 09:05:50 +0200341 /* Note: if you are wondering why we don't
342 * support the sticky bit for excluding
343 * directories from cleaning like we do it for
344 * other file system objects: well, the sticky
345 * bit already has a meaning for directories,
346 * so we don't want to overload that. */
347
348 if (keep_this_level)
349 continue;
350
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200351 /* Ignore ctime, we change it when deleting */
352 age = MAX(timespec_load(&s.st_mtim),
353 timespec_load(&s.st_atim));
354 if (age >= cutoff)
355 continue;
356
Lukas Nykryna6187d42013-03-01 18:29:59 +0100357 if (i->type != IGNORE_DIRECTORY_PATH || !streq(dent->d_name, p)) {
Michal Sekletar78a92a52013-01-18 16:13:08 +0100358 log_debug("rmdir '%s'\n", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200359
Michal Sekletar78a92a52013-01-18 16:13:08 +0100360 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
361 if (errno != ENOENT && errno != ENOTEMPTY) {
362 log_error("rmdir(%s): %m", sub_path);
363 r = -errno;
364 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200365 }
366 }
367
368 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100369 /* Skip files for which the sticky bit is
370 * set. These are semantics we define, and are
371 * unknown elsewhere. See XDG_RUNTIME_DIR
372 * specification for details. */
373 if (s.st_mode & S_ISVTX)
374 continue;
375
Lennart Poettering17b90522011-02-14 21:55:06 +0100376 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200377 if (streq(dent->d_name, ".journal") &&
378 s.st_uid == 0)
379 continue;
380
381 if (streq(dent->d_name, "aquota.user") ||
382 streq(dent->d_name, "aquota.group"))
383 continue;
384 }
385
Lennart Poettering17b90522011-02-14 21:55:06 +0100386 /* Ignore sockets that are listed in /proc/net/unix */
387 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
388 continue;
389
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100390 /* Ignore device nodes */
391 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
392 continue;
393
Lennart Poettering24f3a372012-06-20 09:05:50 +0200394 /* Keep files on this level around if this is
395 * requested */
396 if (keep_this_level)
397 continue;
398
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200399 age = MAX3(timespec_load(&s.st_mtim),
400 timespec_load(&s.st_atim),
401 timespec_load(&s.st_ctim));
402
403 if (age >= cutoff)
404 continue;
405
406 log_debug("unlink '%s'\n", sub_path);
407
408 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
409 if (errno != ENOENT) {
410 log_error("unlink(%s): %m", sub_path);
411 r = -errno;
412 }
413 }
414
415 deleted = true;
416 }
417 }
418
419finish:
420 if (deleted) {
421 /* Restore original directory timestamps */
422 times[0] = ds->st_atim;
423 times[1] = ds->st_mtim;
424
425 if (futimens(dirfd(d), times) < 0)
426 log_error("utimensat(%s): %m", p);
427 }
428
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200429 return r;
430}
431
Michal Schmidt062e01b2011-12-16 18:00:11 +0100432static int item_set_perms(Item *i, const char *path) {
433 /* not using i->path directly because it may be a glob */
434 if (i->mode_set)
435 if (chmod(path, i->mode) < 0) {
436 log_error("chmod(%s) failed: %m", path);
437 return -errno;
438 }
439
440 if (i->uid_set || i->gid_set)
441 if (chown(path,
442 i->uid_set ? i->uid : (uid_t) -1,
443 i->gid_set ? i->gid : (gid_t) -1) < 0) {
444
445 log_error("chown(%s) failed: %m", path);
446 return -errno;
447 }
448
Lennart Poetteringc9bc0762012-07-03 16:25:50 +0200449 return label_fix(path, false, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100450}
451
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400452static int write_one_file(Item *i, const char *path) {
453 int r, e, fd, flags;
454 struct stat st;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400455
456 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
457 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
458
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200459 RUN_WITH_UMASK(0) {
460 label_context_set(path, S_IFREG);
461 fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
462 e = errno;
463 label_context_clear();
464 errno = e;
465 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400466
467 if (fd < 0) {
468 if (i->type == WRITE_FILE && errno == ENOENT)
469 return 0;
470
471 log_error("Failed to create file %s: %m", path);
472 return -errno;
473 }
474
475 if (i->argument) {
476 ssize_t n;
477 size_t l;
Dave Reisner54693d92012-09-15 12:58:49 -0400478 _cleanup_free_ char *unescaped;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400479
Dave Reisner54693d92012-09-15 12:58:49 -0400480 unescaped = cunescape(i->argument);
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100481 if (unescaped == NULL) {
482 close_nointr_nofail(fd);
Dave Reisner54693d92012-09-15 12:58:49 -0400483 return log_oom();
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100484 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400485
Dave Reisner54693d92012-09-15 12:58:49 -0400486 l = strlen(unescaped);
487 n = write(fd, unescaped, l);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400488
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400489 if (n < 0 || (size_t) n < l) {
490 log_error("Failed to write file %s: %s", path, n < 0 ? strerror(-n) : "Short write");
491 close_nointr_nofail(fd);
492 return n < 0 ? n : -EIO;
493 }
494 }
495
Dave Reisner3612fbc2012-09-12 16:21:00 -0400496 close_nointr_nofail(fd);
497
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400498 if (stat(path, &st) < 0) {
499 log_error("stat(%s) failed: %m", path);
500 return -errno;
501 }
502
503 if (!S_ISREG(st.st_mode)) {
504 log_error("%s is not a file.", path);
505 return -EEXIST;
506 }
507
508 r = item_set_perms(i, path);
509 if (r < 0)
510 return r;
511
512 return 0;
513}
514
Michal Schmidt062e01b2011-12-16 18:00:11 +0100515static int recursive_relabel_children(Item *i, const char *path) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200516 _cleanup_closedir_ DIR *d;
Michal Schmidta8d88782011-12-15 23:11:07 +0100517 int ret = 0;
518
519 /* This returns the first error we run into, but nevertheless
520 * tries to go on */
521
522 d = opendir(path);
523 if (!d)
524 return errno == ENOENT ? 0 : -errno;
525
526 for (;;) {
Lennart Poettering7d5e9c02012-09-19 22:21:09 +0200527 struct dirent *de;
528 union dirent_storage buf;
Michal Schmidta8d88782011-12-15 23:11:07 +0100529 bool is_dir;
530 int r;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200531 _cleanup_free_ char *entry_path = NULL;
Michal Schmidta8d88782011-12-15 23:11:07 +0100532
Lennart Poettering7d5e9c02012-09-19 22:21:09 +0200533 r = readdir_r(d, &buf.de, &de);
Michal Schmidta8d88782011-12-15 23:11:07 +0100534 if (r != 0) {
535 if (ret == 0)
536 ret = -r;
537 break;
538 }
539
540 if (!de)
541 break;
542
543 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
544 continue;
545
546 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
547 if (ret == 0)
548 ret = -ENOMEM;
549 continue;
550 }
551
552 if (de->d_type == DT_UNKNOWN) {
553 struct stat st;
554
555 if (lstat(entry_path, &st) < 0) {
556 if (ret == 0 && errno != ENOENT)
557 ret = -errno;
Michal Schmidta8d88782011-12-15 23:11:07 +0100558 continue;
559 }
560
561 is_dir = S_ISDIR(st.st_mode);
562
563 } else
564 is_dir = de->d_type == DT_DIR;
565
Michal Schmidt062e01b2011-12-16 18:00:11 +0100566 r = item_set_perms(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100567 if (r < 0) {
568 if (ret == 0 && r != -ENOENT)
569 ret = r;
Michal Schmidta8d88782011-12-15 23:11:07 +0100570 continue;
571 }
572
573 if (is_dir) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100574 r = recursive_relabel_children(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100575 if (r < 0 && ret == 0)
576 ret = r;
577 }
Michal Schmidta8d88782011-12-15 23:11:07 +0100578 }
579
Michal Schmidta8d88782011-12-15 23:11:07 +0100580 return ret;
581}
582
583static int recursive_relabel(Item *i, const char *path) {
584 int r;
585 struct stat st;
586
Michal Schmidt062e01b2011-12-16 18:00:11 +0100587 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100588 if (r < 0)
589 return r;
590
591 if (lstat(path, &st) < 0)
592 return -errno;
593
594 if (S_ISDIR(st.st_mode))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100595 r = recursive_relabel_children(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100596
597 return r;
598}
599
Michal Schmidt99e68c02011-12-15 23:45:26 +0100600static int glob_item(Item *i, int (*action)(Item *, const char *)) {
601 int r = 0, k;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200602 _cleanup_globfree_ glob_t g = {};
Michal Schmidt99e68c02011-12-15 23:45:26 +0100603 char **fn;
604
Michal Schmidt99e68c02011-12-15 23:45:26 +0100605 errno = 0;
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400606 k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
607 if (k != 0)
Michal Schmidt99e68c02011-12-15 23:45:26 +0100608 if (k != GLOB_NOMATCH) {
Zbigniew Jędrzejewski-Szmek8333c772013-03-28 09:24:15 -0400609 if (errno > 0)
Michal Schmidt99e68c02011-12-15 23:45:26 +0100610 errno = EIO;
611
612 log_error("glob(%s) failed: %m", i->path);
613 return -errno;
614 }
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400615
616 STRV_FOREACH(fn, g.gl_pathv) {
617 k = action(i, *fn);
618 if (k < 0)
619 r = k;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100620 }
621
Michal Schmidt99e68c02011-12-15 23:45:26 +0100622 return r;
623}
624
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200625static int create_item(Item *i) {
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200626 int r, e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200627 struct stat st;
628
629 assert(i);
630
631 switch (i->type) {
632
633 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100634 case IGNORE_DIRECTORY_PATH:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200635 case REMOVE_PATH:
636 case RECURSIVE_REMOVE_PATH:
637 return 0;
638
639 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100640 case TRUNCATE_FILE:
Dave Reisner1845fdd2012-09-27 20:48:13 -0400641 r = write_one_file(i, i->path);
642 if (r < 0)
643 return r;
644 break;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400645 case WRITE_FILE:
646 r = glob_item(i, write_one_file);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100647 if (r < 0)
648 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200649
650 break;
651
652 case TRUNCATE_DIRECTORY:
653 case CREATE_DIRECTORY:
654
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200655 RUN_WITH_UMASK(0000) {
656 mkdir_parents_label(i->path, 0755);
657 r = mkdir(i->path, i->mode);
658 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200659
660 if (r < 0 && errno != EEXIST) {
661 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100662 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200663 }
664
665 if (stat(i->path, &st) < 0) {
666 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100667 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200668 }
669
670 if (!S_ISDIR(st.st_mode)) {
671 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100672 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200673 }
674
Michal Schmidt062e01b2011-12-16 18:00:11 +0100675 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100676 if (r < 0)
677 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200678
679 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200680
681 case CREATE_FIFO:
682
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200683 RUN_WITH_UMASK(0000) {
684 r = mkfifo(i->path, i->mode);
685 }
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200686
687 if (r < 0 && errno != EEXIST) {
688 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100689 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200690 }
691
692 if (stat(i->path, &st) < 0) {
693 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100694 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200695 }
696
697 if (!S_ISFIFO(st.st_mode)) {
698 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100699 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200700 }
701
Michal Schmidt062e01b2011-12-16 18:00:11 +0100702 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100703 if (r < 0)
704 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200705
706 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100707
Lennart Poettering468d7262012-01-17 15:04:12 +0100708 case CREATE_SYMLINK: {
709 char *x;
710
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200711 label_context_set(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100712 r = symlink(i->argument, i->path);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200713 e = errno;
714 label_context_clear();
715 errno = e;
716
Lennart Poettering468d7262012-01-17 15:04:12 +0100717 if (r < 0 && errno != EEXIST) {
718 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
719 return -errno;
720 }
721
722 r = readlink_malloc(i->path, &x);
723 if (r < 0) {
724 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
725 return -errno;
726 }
727
728 if (!streq(i->argument, x)) {
729 free(x);
730 log_error("%s is not the right symlinks.", i->path);
731 return -EEXIST;
732 }
733
734 free(x);
735 break;
736 }
737
738 case CREATE_BLOCK_DEVICE:
739 case CREATE_CHAR_DEVICE: {
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700740 mode_t file_type;
741
742 if (have_effective_cap(CAP_MKNOD) == 0) {
743 /* In a container we lack CAP_MKNOD. We
Anatol Pomozovab06eef2013-04-14 19:37:54 -0700744 shouldn't attempt to create the device node in
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700745 that case to avoid noise, and we don't support
746 virtualized devices in containers anyway. */
747
748 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
749 return 0;
750 }
751
752 file_type = (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +0100753
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200754 RUN_WITH_UMASK(0000) {
755 label_context_set(i->path, file_type);
756 r = mknod(i->path, i->mode | file_type, i->major_minor);
757 e = errno;
758 label_context_clear();
759 errno = e;
760 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100761
762 if (r < 0 && errno != EEXIST) {
763 log_error("Failed to create device node %s: %m", i->path);
764 return -errno;
765 }
766
767 if (stat(i->path, &st) < 0) {
768 log_error("stat(%s) failed: %m", i->path);
769 return -errno;
770 }
771
Michal Schmidte7aee752012-06-14 16:01:19 +0200772 if ((st.st_mode & S_IFMT) != file_type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100773 log_error("%s is not a device node.", i->path);
774 return -EEXIST;
775 }
776
777 r = item_set_perms(i, i->path);
778 if (r < 0)
779 return r;
780
781 break;
782 }
783
Michal Schmidt777b87e2011-12-16 18:27:35 +0100784 case RELABEL_PATH:
785
786 r = glob_item(i, item_set_perms);
787 if (r < 0)
Lennart Poettering96ca8192013-06-21 15:57:42 +0200788 return r;
Michal Schmidt777b87e2011-12-16 18:27:35 +0100789 break;
790
Michal Schmidta8d88782011-12-15 23:11:07 +0100791 case RECURSIVE_RELABEL_PATH:
792
793 r = glob_item(i, recursive_relabel);
794 if (r < 0)
795 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200796 }
797
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200798 log_debug("%s created successfully.", i->path);
799
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100800 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200801}
802
Michal Schmidta0896122011-12-15 21:32:50 +0100803static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200804 int r;
805
806 assert(i);
807
808 switch (i->type) {
809
810 case CREATE_FILE:
811 case TRUNCATE_FILE:
812 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200813 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100814 case CREATE_SYMLINK:
815 case CREATE_BLOCK_DEVICE:
816 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200817 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100818 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100819 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100820 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100821 case WRITE_FILE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200822 break;
823
824 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100825 if (remove(instance) < 0 && errno != ENOENT) {
826 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200827 return -errno;
828 }
829
830 break;
831
832 case TRUNCATE_DIRECTORY:
833 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +0200834 /* FIXME: we probably should use dir_cleanup() here
835 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +0200836 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Lennart Poettering468d7262012-01-17 15:04:12 +0100837 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100838 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200839 return r;
840 }
841
842 break;
843 }
844
845 return 0;
846}
847
Michal Schmidta0896122011-12-15 21:32:50 +0100848static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100849 int r = 0;
850
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100851 assert(i);
852
853 switch (i->type) {
854
855 case CREATE_FILE:
856 case TRUNCATE_FILE:
857 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200858 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100859 case CREATE_SYMLINK:
860 case CREATE_CHAR_DEVICE:
861 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100862 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100863 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100864 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100865 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100866 case WRITE_FILE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100867 break;
868
869 case REMOVE_PATH:
870 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100871 case RECURSIVE_REMOVE_PATH:
872 r = glob_item(i, remove_item_instance);
873 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100874 }
875
Michal Schmidt99e68c02011-12-15 23:45:26 +0100876 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100877}
878
Michal Sekletar78a92a52013-01-18 16:13:08 +0100879static int clean_item_instance(Item *i, const char* instance) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200880 _cleanup_closedir_ DIR *d = NULL;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100881 struct stat s, ps;
882 bool mountpoint;
883 int r;
884 usec_t cutoff, n;
885
886 assert(i);
887
888 if (!i->age_set)
889 return 0;
890
891 n = now(CLOCK_REALTIME);
892 if (n < i->age)
893 return 0;
894
895 cutoff = n - i->age;
896
897 d = opendir(instance);
898 if (!d) {
899 if (errno == ENOENT || errno == ENOTDIR)
900 return 0;
901
902 log_error("Failed to open directory %s: %m", i->path);
903 return -errno;
904 }
905
906 if (fstat(dirfd(d), &s) < 0) {
907 log_error("stat(%s) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500908 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100909 }
910
911 if (!S_ISDIR(s.st_mode)) {
912 log_error("%s is not a directory.", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500913 return -ENOTDIR;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100914 }
915
916 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
917 log_error("stat(%s/..) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500918 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100919 }
920
921 mountpoint = s.st_dev != ps.st_dev ||
922 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
923
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500924 r = dir_cleanup(i, instance, d, &s, cutoff, s.st_dev, mountpoint,
925 MAX_DEPTH, i->keep_first_level);
Michal Sekletar78a92a52013-01-18 16:13:08 +0100926 return r;
927}
928
929static int clean_item(Item *i) {
930 int r = 0;
931
932 assert(i);
933
934 switch (i->type) {
935 case CREATE_DIRECTORY:
936 case TRUNCATE_DIRECTORY:
937 case IGNORE_PATH:
938 clean_item_instance(i, i->path);
939 break;
940 case IGNORE_DIRECTORY_PATH:
941 r = glob_item(i, clean_item_instance);
942 break;
943 default:
944 break;
945 }
946
947 return r;
948}
949
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200950static int process_item(Item *i) {
951 int r, q, p;
952
953 assert(i);
954
955 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100956 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200957 p = arg_clean ? clean_item(i) : 0;
958
959 if (r < 0)
960 return r;
961
962 if (q < 0)
963 return q;
964
965 return p;
966}
967
968static void item_free(Item *i) {
969 assert(i);
970
971 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100972 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200973 free(i);
974}
975
Maciej Wereskie2f2fb72013-07-19 15:43:12 +0200976static inline void item_freep(Item **i) {
977 if (*i)
978 item_free(*i);
979}
980#define _cleanup_item_free_ _cleanup_(item_freep)
981
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200982static bool item_equal(Item *a, Item *b) {
983 assert(a);
984 assert(b);
985
986 if (!streq_ptr(a->path, b->path))
987 return false;
988
989 if (a->type != b->type)
990 return false;
991
992 if (a->uid_set != b->uid_set ||
993 (a->uid_set && a->uid != b->uid))
994 return false;
995
996 if (a->gid_set != b->gid_set ||
997 (a->gid_set && a->gid != b->gid))
998 return false;
999
1000 if (a->mode_set != b->mode_set ||
1001 (a->mode_set && a->mode != b->mode))
1002 return false;
1003
1004 if (a->age_set != b->age_set ||
1005 (a->age_set && a->age != b->age))
1006 return false;
1007
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001008 if ((a->type == CREATE_FILE ||
1009 a->type == TRUNCATE_FILE ||
1010 a->type == WRITE_FILE ||
1011 a->type == CREATE_SYMLINK) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +01001012 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +01001013 return false;
1014
1015 if ((a->type == CREATE_CHAR_DEVICE ||
1016 a->type == CREATE_BLOCK_DEVICE) &&
1017 a->major_minor != b->major_minor)
1018 return false;
1019
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001020 return true;
1021}
1022
Dave Reisnera2aced42013-07-24 11:10:05 -04001023static bool should_include_path(const char *path) {
1024 char **prefix;
1025
Dave Reisner5c795112013-07-24 11:19:24 -04001026 STRV_FOREACH(prefix, exclude_prefixes) {
1027 if (path_startswith(path, *prefix))
1028 return false;
1029 }
Dave Reisnera2aced42013-07-24 11:10:05 -04001030
1031 STRV_FOREACH(prefix, include_prefixes) {
1032 if (path_startswith(path, *prefix))
1033 return true;
1034 }
1035
Dave Reisner5c795112013-07-24 11:19:24 -04001036 /* no matches, so we should include this path only if we
1037 * have no whitelist at all */
1038 return strv_length(include_prefixes) == 0;
Dave Reisnera2aced42013-07-24 11:10:05 -04001039}
1040
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001041static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001042
1043 static const Specifier specifier_table[] = {
1044 { 'm', specifier_machine_id, NULL },
1045 { 'b', specifier_boot_id, NULL },
1046 { 'H', specifier_host_name, NULL },
1047 { 'v', specifier_kernel_release, NULL },
1048 {}
1049 };
1050
Maciej Wereskie2f2fb72013-07-19 15:43:12 +02001051 _cleanup_item_free_ Item *i = NULL;
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001052 Item *existing;
Harald Hoyer7fd1b192013-04-18 09:11:22 +02001053 _cleanup_free_ char
Lennart Poettering1731e342013-09-17 11:02:02 -05001054 *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +01001055 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001056 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001057 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +02001058
1059 assert(fname);
1060 assert(line >= 1);
1061 assert(buffer);
1062
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001063 r = sscanf(buffer,
1064 "%c %ms %ms %ms %ms %ms %n",
Michal Schmidt66ccd032011-12-15 21:31:14 +01001065 &type,
Lennart Poettering1731e342013-09-17 11:02:02 -05001066 &path,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +01001067 &mode,
1068 &user,
1069 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +01001070 &age,
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001071 &n);
1072 if (r < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001073 log_error("[%s:%u] Syntax error.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001074 return -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +02001075 }
1076
Lennart Poettering1731e342013-09-17 11:02:02 -05001077 i = new0(Item, 1);
1078 if (!i)
1079 return log_oom();
1080
1081 r = specifier_printf(path, specifier_table, NULL, &i->path);
1082 if (r < 0) {
1083 log_error("[%s:%u] Failed to replace specifiers: %s", fname, line, path);
1084 return r;
1085 }
1086
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001087 if (n >= 0) {
1088 n += strspn(buffer+n, WHITESPACE);
1089 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
1090 i->argument = unquote(buffer+n, "\"");
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001091 if (!i->argument)
1092 return log_oom();
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001093 }
1094 }
1095
Michal Schmidt777b87e2011-12-16 18:27:35 +01001096 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001097
Michal Schmidt777b87e2011-12-16 18:27:35 +01001098 case CREATE_FILE:
1099 case TRUNCATE_FILE:
1100 case CREATE_DIRECTORY:
1101 case TRUNCATE_DIRECTORY:
1102 case CREATE_FIFO:
1103 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001104 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001105 case REMOVE_PATH:
1106 case RECURSIVE_REMOVE_PATH:
1107 case RELABEL_PATH:
1108 case RECURSIVE_RELABEL_PATH:
1109 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001110
1111 case CREATE_SYMLINK:
1112 if (!i->argument) {
1113 log_error("[%s:%u] Symlink file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001114 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001115 }
1116 break;
1117
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001118 case WRITE_FILE:
1119 if (!i->argument) {
1120 log_error("[%s:%u] Write file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001121 return -EBADMSG;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001122 }
1123 break;
1124
Lennart Poettering468d7262012-01-17 15:04:12 +01001125 case CREATE_CHAR_DEVICE:
1126 case CREATE_BLOCK_DEVICE: {
1127 unsigned major, minor;
1128
1129 if (!i->argument) {
1130 log_error("[%s:%u] Device file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001131 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001132 }
1133
1134 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1135 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 -04001136 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001137 }
1138
1139 i->major_minor = makedev(major, minor);
1140 break;
1141 }
1142
Michal Schmidt777b87e2011-12-16 18:27:35 +01001143 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001144 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001145 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001146 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001147
Michal Schmidta8d88782011-12-15 23:11:07 +01001148 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001149
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001150 if (!path_is_absolute(i->path)) {
1151 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001152 return -EBADMSG;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001153 }
1154
1155 path_kill_slashes(i->path);
1156
Dave Reisnera2aced42013-07-24 11:10:05 -04001157 if (!should_include_path(i->path))
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001158 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001159
1160 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001161 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001162
Lennart Poetteringd05c5032012-07-16 12:34:54 +02001163 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001164 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001165 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001166 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001167 }
1168
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001169 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001170 }
1171
1172 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001173 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001174
Lennart Poettering4b678342011-07-23 01:17:59 +02001175 r = get_group_creds(&g, &i->gid);
1176 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001177 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001178 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001179 }
1180
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001181 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001182 }
1183
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001184 if (mode && !streq(mode, "-")) {
1185 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001186
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001187 if (sscanf(mode, "%o", &m) != 1) {
1188 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001189 return -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001190 }
1191
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001192 i->mode = m;
1193 i->mode_set = true;
1194 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001195 i->mode =
1196 i->type == CREATE_DIRECTORY ||
1197 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001198
1199 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001200 const char *a = age;
1201
1202 if (*a == '~') {
1203 i->keep_first_level = true;
1204 a++;
1205 }
1206
Lennart Poettering7f602782013-04-02 20:38:16 +02001207 if (parse_sec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001208 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001209 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001210 }
1211
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001212 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001213 }
1214
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001215 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001216
Lennart Poettering468d7262012-01-17 15:04:12 +01001217 existing = hashmap_get(h, i->path);
1218 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001219
1220 /* Two identical items are fine */
1221 if (!item_equal(existing, i))
1222 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1223
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001224 return 0;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001225 }
1226
Lennart Poettering468d7262012-01-17 15:04:12 +01001227 r = hashmap_put(h, i->path, i);
1228 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001229 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001230 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001231 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001232
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001233 i = NULL; /* avoid cleanup */
Lennart Poettering5008d582010-09-28 02:34:02 +02001234
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001235 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001236}
1237
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001238static int help(void) {
1239
Lennart Poettering522d4a42011-02-13 15:08:15 +01001240 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1241 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001242 " -h --help Show this help\n"
1243 " --create Create marked files/directories\n"
1244 " --clean Clean up marked directories\n"
1245 " --remove Remove marked files/directories\n"
1246 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n"
1247 " --exclude-prefix=PATH Ignore rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001248 program_invocation_short_name);
1249
1250 return 0;
1251}
1252
1253static int parse_argv(int argc, char *argv[]) {
1254
1255 enum {
1256 ARG_CREATE,
1257 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001258 ARG_REMOVE,
Dave Reisner5c795112013-07-24 11:19:24 -04001259 ARG_PREFIX,
1260 ARG_EXCLUDE_PREFIX,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001261 };
1262
1263 static const struct option options[] = {
Dave Reisner5c795112013-07-24 11:19:24 -04001264 { "help", no_argument, NULL, 'h' },
1265 { "create", no_argument, NULL, ARG_CREATE },
1266 { "clean", no_argument, NULL, ARG_CLEAN },
1267 { "remove", no_argument, NULL, ARG_REMOVE },
1268 { "prefix", required_argument, NULL, ARG_PREFIX },
1269 { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX },
1270 { NULL, 0, NULL, 0 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001271 };
1272
1273 int c;
1274
1275 assert(argc >= 0);
1276 assert(argv);
1277
1278 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1279
1280 switch (c) {
1281
1282 case 'h':
1283 help();
1284 return 0;
1285
1286 case ARG_CREATE:
1287 arg_create = true;
1288 break;
1289
1290 case ARG_CLEAN:
1291 arg_clean = true;
1292 break;
1293
1294 case ARG_REMOVE:
1295 arg_remove = true;
1296 break;
1297
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001298 case ARG_PREFIX:
Dave Reisnera2aced42013-07-24 11:10:05 -04001299 if (strv_extend(&include_prefixes, optarg) < 0)
1300 return log_oom();
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001301 break;
1302
Dave Reisner5c795112013-07-24 11:19:24 -04001303 case ARG_EXCLUDE_PREFIX:
1304 if (strv_extend(&exclude_prefixes, optarg) < 0)
1305 return log_oom();
1306 break;
1307
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001308 case '?':
1309 return -EINVAL;
1310
1311 default:
1312 log_error("Unknown option code %c", c);
1313 return -EINVAL;
1314 }
1315 }
1316
1317 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001318 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001319 return -EINVAL;
1320 }
1321
1322 return 1;
1323}
1324
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001325static int read_config_file(const char *fn, bool ignore_enoent) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001326 _cleanup_fclose_ FILE *f = NULL;
1327 char line[LINE_MAX];
Michal Sekletar78a92a52013-01-18 16:13:08 +01001328 Iterator iterator;
Lennart Poettering1731e342013-09-17 11:02:02 -05001329 unsigned v = 0;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001330 Item *i;
Lennart Poettering1731e342013-09-17 11:02:02 -05001331 int r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001332
1333 assert(fn);
1334
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001335 r = search_and_fopen_nulstr(fn, "re", conf_file_dirs, &f);
1336 if (r < 0) {
1337 if (ignore_enoent && r == -ENOENT)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001338 return 0;
1339
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001340 log_error("Failed to open '%s', ignoring: %s", fn, strerror(-r));
1341 return r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001342 }
1343
Lennart Poettering1731e342013-09-17 11:02:02 -05001344 FOREACH_LINE(line, f, break) {
1345 char *l;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001346 int k;
1347
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001348 v++;
1349
1350 l = strstrip(line);
1351 if (*l == '#' || *l == 0)
1352 continue;
1353
Lennart Poettering1731e342013-09-17 11:02:02 -05001354 k = parse_line(fn, v, l);
1355 if (k < 0 && r == 0)
1356 r = k;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001357 }
1358
Michal Sekletar78a92a52013-01-18 16:13:08 +01001359 /* we have to determine age parameter for each entry of type X */
1360 HASHMAP_FOREACH(i, globs, iterator) {
1361 Iterator iter;
1362 Item *j, *candidate_item = NULL;
1363
1364 if (i->type != IGNORE_DIRECTORY_PATH)
1365 continue;
1366
1367 HASHMAP_FOREACH(j, items, iter) {
1368 if (j->type != CREATE_DIRECTORY && j->type != TRUNCATE_DIRECTORY)
1369 continue;
1370
1371 if (path_equal(j->path, i->path)) {
1372 candidate_item = j;
1373 break;
1374 }
1375
1376 if ((!candidate_item && path_startswith(i->path, j->path)) ||
1377 (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
1378 candidate_item = j;
1379 }
1380
1381 if (candidate_item) {
1382 i->age = candidate_item->age;
1383 i->age_set = true;
1384 }
1385 }
1386
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001387 if (ferror(f)) {
1388 log_error("Failed to read from file %s: %m", fn);
1389 if (r == 0)
1390 r = -EIO;
1391 }
1392
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001393 return r;
1394}
1395
Lennart Poettering5008d582010-09-28 02:34:02 +02001396int main(int argc, char *argv[]) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001397 int r, k;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001398 Item *i;
1399 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001400
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001401 r = parse_argv(argc, argv);
1402 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001403 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1404
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001405 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001406 log_parse_environment();
1407 log_open();
1408
Lennart Poettering4c126262011-08-01 20:52:18 +02001409 umask(0022);
1410
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001411 label_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001412
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001413 items = hashmap_new(string_hash_func, string_compare_func);
1414 globs = hashmap_new(string_hash_func, string_compare_func);
1415
1416 if (!items || !globs) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001417 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001418 goto finish;
1419 }
1420
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001421 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001422
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001423 if (optind < argc) {
1424 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001425
Dave Reisner91256702012-06-08 22:31:19 -04001426 for (j = optind; j < argc; j++) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001427 k = read_config_file(argv[j], false);
1428 if (k < 0 && r == 0)
1429 r = k;
Dave Reisner91256702012-06-08 22:31:19 -04001430 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001431
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001432 } else {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001433 _cleanup_strv_free_ char **files = NULL;
1434 char **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001435
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001436 r = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001437 if (r < 0) {
Kay Sievers44143302011-04-28 23:51:24 +02001438 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
1439 goto finish;
1440 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001441
Kay Sievers772f8372011-04-25 21:38:21 +02001442 STRV_FOREACH(f, files) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001443 k = read_config_file(*f, true);
1444 if (k < 0 && r == 0)
1445 r = k;
Lennart Poettering5008d582010-09-28 02:34:02 +02001446 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001447 }
1448
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001449 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001450 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001451
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001452 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001453 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001454
Lennart Poettering5008d582010-09-28 02:34:02 +02001455finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001456 while ((i = hashmap_steal_first(items)))
1457 item_free(i);
1458
Lennart Poettering17b90522011-02-14 21:55:06 +01001459 while ((i = hashmap_steal_first(globs)))
1460 item_free(i);
1461
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001462 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001463 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001464
Dave Reisnera2aced42013-07-24 11:10:05 -04001465 strv_free(include_prefixes);
1466
Lennart Poettering17b90522011-02-14 21:55:06 +01001467 set_free_free(unix_sockets);
1468
Lennart Poettering29003cf2010-10-19 19:36:45 +02001469 label_finish();
1470
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001471 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Lennart Poettering5008d582010-09-28 02:34:02 +02001472}