blob: 48dc619d94f4e1717368d1c51b2bbbb82b874079 [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 Poetteringeb9da372013-11-06 18:28:39 +010055#include "build.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020056
Andreas Jaeger01000472010-09-29 10:08:24 +020057/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020058 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020059 * properly owned directories beneath /tmp, /var/tmp, /run, which are
60 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020061
Michal Schmidt66ccd032011-12-15 21:31:14 +010062typedef enum ItemType {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010063 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020064 CREATE_FILE = 'f',
65 TRUNCATE_FILE = 'F',
66 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 Poettering265ffa12013-09-17 16:33:30 -050072 ADJUST_MODE = 'm',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010073
74 /* These ones take globs */
Lennart Poetteringcde684a2014-06-10 22:50:46 +020075 WRITE_FILE = 'w',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020076 IGNORE_PATH = 'x',
Michal Sekletar78a92a52013-01-18 16:13:08 +010077 IGNORE_DIRECTORY_PATH = 'X',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020078 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010079 RECURSIVE_REMOVE_PATH = 'R',
Michal Schmidt777b87e2011-12-16 18:27:35 +010080 RELABEL_PATH = 'z',
Michal Schmidta8d88782011-12-15 23:11:07 +010081 RECURSIVE_RELABEL_PATH = 'Z'
Michal Schmidt66ccd032011-12-15 21:31:14 +010082} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020083
84typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +010085 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020086
87 char *path;
Lennart Poettering468d7262012-01-17 15:04:12 +010088 char *argument;
Lennart Poettering5008d582010-09-28 02:34:02 +020089 uid_t uid;
90 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020091 mode_t mode;
92 usec_t age;
93
Lennart Poettering468d7262012-01-17 15:04:12 +010094 dev_t major_minor;
95
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020096 bool uid_set:1;
97 bool gid_set:1;
98 bool mode_set:1;
99 bool age_set:1;
Lennart Poettering24f3a372012-06-20 09:05:50 +0200100
101 bool keep_first_level:1;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200102} Item;
103
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100104static Hashmap *items = NULL, *globs = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100105static Set *unix_sockets = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200106
107static bool arg_create = false;
108static bool arg_clean = false;
109static bool arg_remove = false;
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -0500110static bool arg_boot = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200111
Dave Reisnera2aced42013-07-24 11:10:05 -0400112static char **include_prefixes = NULL;
Dave Reisner5c795112013-07-24 11:19:24 -0400113static char **exclude_prefixes = NULL;
Michael Marineaucf9a4ab2014-03-13 21:32:13 -0700114static char *arg_root = NULL;
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100115
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100116static const char conf_file_dirs[] =
117 "/etc/tmpfiles.d\0"
118 "/run/tmpfiles.d\0"
119 "/usr/local/lib/tmpfiles.d\0"
120 "/usr/lib/tmpfiles.d\0"
Lennart Poettering3f2afb22012-07-20 16:24:55 +0200121#ifdef HAVE_SPLIT_USR
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100122 "/lib/tmpfiles.d\0"
Lennart Poettering3f2afb22012-07-20 16:24:55 +0200123#endif
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100124 ;
Dave Reisner91256702012-06-08 22:31:19 -0400125
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200126#define MAX_DEPTH 256
127
Michal Schmidt66ccd032011-12-15 21:31:14 +0100128static bool needs_glob(ItemType t) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200129 return IN_SET(t,
130 WRITE_FILE,
131 IGNORE_PATH,
132 IGNORE_DIRECTORY_PATH,
133 REMOVE_PATH,
134 RECURSIVE_REMOVE_PATH,
135 RELABEL_PATH,
136 RECURSIVE_RELABEL_PATH);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100137}
138
139static struct Item* find_glob(Hashmap *h, const char *match) {
140 Item *j;
141 Iterator i;
142
143 HASHMAP_FOREACH(j, h, i)
144 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
145 return j;
146
147 return NULL;
148}
149
Lennart Poettering17b90522011-02-14 21:55:06 +0100150static void load_unix_sockets(void) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200151 _cleanup_fclose_ FILE *f = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100152 char line[LINE_MAX];
153
154 if (unix_sockets)
155 return;
156
157 /* We maintain a cache of the sockets we found in
158 * /proc/net/unix to speed things up a little. */
159
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100160 unix_sockets = set_new(string_hash_func, string_compare_func);
161 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100162 return;
163
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100164 f = fopen("/proc/net/unix", "re");
165 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100166 return;
167
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100168 /* Skip header */
169 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100170 goto fail;
171
172 for (;;) {
173 char *p, *s;
174 int k;
175
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100176 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100177 break;
178
179 truncate_nl(line);
180
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100181 p = strchr(line, ':');
182 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100183 continue;
184
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100185 if (strlen(p) < 37)
186 continue;
187
188 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100189 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100190 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100191 p += strspn(p, WHITESPACE);
192
193 if (*p != '/')
194 continue;
195
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100196 s = strdup(p);
197 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100198 goto fail;
199
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100200 path_kill_slashes(s);
201
Zbigniew Jędrzejewski-Szmekef422022013-04-22 23:12:15 -0400202 k = set_consume(unix_sockets, s);
203 if (k < 0 && k != -EEXIST)
204 goto fail;
Lennart Poettering17b90522011-02-14 21:55:06 +0100205 }
206
207 return;
208
209fail:
210 set_free_free(unix_sockets);
211 unix_sockets = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100212}
213
214static bool unix_socket_alive(const char *fn) {
215 assert(fn);
216
217 load_unix_sockets();
218
219 if (unix_sockets)
220 return !!set_get(unix_sockets, (char*) fn);
221
222 /* We don't know, so assume yes */
223 return true;
224}
225
Kay Sievers99d680a2013-03-13 13:12:36 +0100226static int dir_is_mount_point(DIR *d, const char *subdir) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200227
228 union file_handle_union h = {
229 .handle.handle_bytes = MAX_HANDLE_SZ
230 };
231
Kay Sievers99d680a2013-03-13 13:12:36 +0100232 int mount_id_parent, mount_id;
233 int r_p, r;
234
Dave Reisner370c8602014-04-19 13:22:35 -0400235 r_p = name_to_handle_at(dirfd(d), ".", &h.handle, &mount_id_parent, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100236 if (r_p < 0)
237 r_p = -errno;
238
Dave Reisner370c8602014-04-19 13:22:35 -0400239 h.handle.handle_bytes = MAX_HANDLE_SZ;
240 r = name_to_handle_at(dirfd(d), subdir, &h.handle, &mount_id, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100241 if (r < 0)
242 r = -errno;
243
244 /* got no handle; make no assumptions, return error */
245 if (r_p < 0 && r < 0)
246 return r_p;
247
248 /* got both handles; if they differ, it is a mount point */
249 if (r_p >= 0 && r >= 0)
250 return mount_id_parent != mount_id;
251
252 /* got only one handle; assume different mount points if one
253 * of both queries was not supported by the filesystem */
254 if (r_p == -ENOSYS || r_p == -ENOTSUP || r == -ENOSYS || r == -ENOTSUP)
255 return true;
256
257 /* return error */
258 if (r_p < 0)
259 return r_p;
260 return r;
261}
262
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200263static int dir_cleanup(
Michal Sekletar78a92a52013-01-18 16:13:08 +0100264 Item *i,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200265 const char *p,
266 DIR *d,
267 const struct stat *ds,
268 usec_t cutoff,
269 dev_t rootdev,
270 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200271 int maxdepth,
Lennart Poettering265ffa12013-09-17 16:33:30 -0500272 bool keep_this_level) {
273
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200274 struct dirent *dent;
275 struct timespec times[2];
276 bool deleted = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200277 int r = 0;
278
279 while ((dent = readdir(d))) {
280 struct stat s;
281 usec_t age;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200282 _cleanup_free_ char *sub_path = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200283
284 if (streq(dent->d_name, ".") ||
285 streq(dent->d_name, ".."))
286 continue;
287
288 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
Kay Sieversca2f4172013-10-17 03:20:46 +0200289 if (errno == ENOENT)
290 continue;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200291
Kay Sieversca2f4172013-10-17 03:20:46 +0200292 /* FUSE, NFS mounts, SELinux might return EACCES */
293 if (errno == EACCES)
294 log_debug("stat(%s/%s) failed: %m", p, dent->d_name);
295 else
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200296 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
Kay Sieversca2f4172013-10-17 03:20:46 +0200297 r = -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200298 continue;
299 }
300
301 /* Stay on the same filesystem */
302 if (s.st_dev != rootdev)
303 continue;
304
Kay Sievers99d680a2013-03-13 13:12:36 +0100305 /* Try to detect bind mounts of the same filesystem instance; they
306 * do not differ in device major/minors. This type of query is not
307 * supported on all kernels or filesystem types though. */
308 if (S_ISDIR(s.st_mode) && dir_is_mount_point(d, dent->d_name) > 0)
309 continue;
310
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200311 /* Do not delete read-only files owned by root */
312 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
313 continue;
314
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200315 sub_path = strjoin(p, "/", dent->d_name, NULL);
316 if (!sub_path) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700317 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200318 goto finish;
319 }
320
321 /* Is there an item configured for this path? */
322 if (hashmap_get(items, sub_path))
323 continue;
324
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100325 if (find_glob(globs, sub_path))
326 continue;
327
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200328 if (S_ISDIR(s.st_mode)) {
329
330 if (mountpoint &&
331 streq(dent->d_name, "lost+found") &&
332 s.st_uid == 0)
333 continue;
334
335 if (maxdepth <= 0)
336 log_warning("Reached max depth on %s.", sub_path);
337 else {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200338 _cleanup_closedir_ DIR *sub_dir;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200339 int q;
340
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200341 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200342 if (!sub_dir) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200343 if (errno != ENOENT) {
344 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
345 r = -errno;
346 }
347
348 continue;
349 }
350
Michal Sekletar78a92a52013-01-18 16:13:08 +0100351 q = dir_cleanup(i, sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200352 if (q < 0)
353 r = q;
354 }
355
Lennart Poettering24f3a372012-06-20 09:05:50 +0200356 /* Note: if you are wondering why we don't
357 * support the sticky bit for excluding
358 * directories from cleaning like we do it for
359 * other file system objects: well, the sticky
360 * bit already has a meaning for directories,
361 * so we don't want to overload that. */
362
363 if (keep_this_level)
364 continue;
365
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200366 /* Ignore ctime, we change it when deleting */
367 age = MAX(timespec_load(&s.st_mtim),
368 timespec_load(&s.st_atim));
369 if (age >= cutoff)
370 continue;
371
Lukas Nykryna6187d42013-03-01 18:29:59 +0100372 if (i->type != IGNORE_DIRECTORY_PATH || !streq(dent->d_name, p)) {
Lennart Poettering9f6445e32013-12-24 16:39:37 +0100373 log_debug("rmdir '%s'", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200374
Michal Sekletar78a92a52013-01-18 16:13:08 +0100375 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
376 if (errno != ENOENT && errno != ENOTEMPTY) {
377 log_error("rmdir(%s): %m", sub_path);
378 r = -errno;
379 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200380 }
381 }
382
383 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100384 /* Skip files for which the sticky bit is
385 * set. These are semantics we define, and are
386 * unknown elsewhere. See XDG_RUNTIME_DIR
387 * specification for details. */
388 if (s.st_mode & S_ISVTX)
389 continue;
390
Lennart Poettering17b90522011-02-14 21:55:06 +0100391 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200392 if (streq(dent->d_name, ".journal") &&
393 s.st_uid == 0)
394 continue;
395
396 if (streq(dent->d_name, "aquota.user") ||
397 streq(dent->d_name, "aquota.group"))
398 continue;
399 }
400
Lennart Poettering17b90522011-02-14 21:55:06 +0100401 /* Ignore sockets that are listed in /proc/net/unix */
402 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
403 continue;
404
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100405 /* Ignore device nodes */
406 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
407 continue;
408
Lennart Poettering24f3a372012-06-20 09:05:50 +0200409 /* Keep files on this level around if this is
410 * requested */
411 if (keep_this_level)
412 continue;
413
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200414 age = MAX3(timespec_load(&s.st_mtim),
415 timespec_load(&s.st_atim),
416 timespec_load(&s.st_ctim));
417
418 if (age >= cutoff)
419 continue;
420
Lennart Poettering9f6445e32013-12-24 16:39:37 +0100421 log_debug("unlink '%s'", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200422
423 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
424 if (errno != ENOENT) {
425 log_error("unlink(%s): %m", sub_path);
426 r = -errno;
427 }
428 }
429
430 deleted = true;
431 }
432 }
433
434finish:
435 if (deleted) {
436 /* Restore original directory timestamps */
437 times[0] = ds->st_atim;
438 times[1] = ds->st_mtim;
439
440 if (futimens(dirfd(d), times) < 0)
441 log_error("utimensat(%s): %m", p);
442 }
443
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200444 return r;
445}
446
Lennart Poettering265ffa12013-09-17 16:33:30 -0500447static int item_set_perms_full(Item *i, const char *path, bool ignore_enoent) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200448 assert(i);
449 assert(path);
450
Michal Schmidt062e01b2011-12-16 18:00:11 +0100451 /* not using i->path directly because it may be a glob */
452 if (i->mode_set)
453 if (chmod(path, i->mode) < 0) {
Lennart Poettering265ffa12013-09-17 16:33:30 -0500454 if (errno != ENOENT || !ignore_enoent) {
455 log_error("chmod(%s) failed: %m", path);
456 return -errno;
457 }
Michal Schmidt062e01b2011-12-16 18:00:11 +0100458 }
459
460 if (i->uid_set || i->gid_set)
461 if (chown(path,
462 i->uid_set ? i->uid : (uid_t) -1,
463 i->gid_set ? i->gid : (gid_t) -1) < 0) {
464
Lennart Poettering265ffa12013-09-17 16:33:30 -0500465 if (errno != ENOENT || !ignore_enoent) {
466 log_error("chown(%s) failed: %m", path);
467 return -errno;
468 }
Michal Schmidt062e01b2011-12-16 18:00:11 +0100469 }
470
Lukas Nykrynf58ceb22014-01-09 18:00:50 +0100471 return label_fix(path, ignore_enoent, false);
Lennart Poettering265ffa12013-09-17 16:33:30 -0500472}
473
474static int item_set_perms(Item *i, const char *path) {
475 return item_set_perms_full(i, path, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100476}
477
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400478static int write_one_file(Item *i, const char *path) {
Lennart Poettering874f1942014-06-10 22:48:56 +0200479 int flags;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200480 int fd = -1;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400481 struct stat st;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200482 int r = 0;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400483
Lennart Poettering874f1942014-06-10 22:48:56 +0200484 assert(i);
485 assert(path);
486
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400487 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
488 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
489
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200490 RUN_WITH_UMASK(0) {
491 label_context_set(path, S_IFREG);
492 fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200493 label_context_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200494 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400495
496 if (fd < 0) {
497 if (i->type == WRITE_FILE && errno == ENOENT)
498 return 0;
499
500 log_error("Failed to create file %s: %m", path);
501 return -errno;
502 }
503
504 if (i->argument) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200505 _cleanup_free_ char *unescaped;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400506 ssize_t n;
507 size_t l;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400508
Dave Reisner54693d92012-09-15 12:58:49 -0400509 unescaped = cunescape(i->argument);
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100510 if (unescaped == NULL) {
Lennart Poettering03e334a2014-03-18 19:22:43 +0100511 safe_close(fd);
Dave Reisner54693d92012-09-15 12:58:49 -0400512 return log_oom();
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100513 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400514
Dave Reisner54693d92012-09-15 12:58:49 -0400515 l = strlen(unescaped);
516 n = write(fd, unescaped, l);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400517
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400518 if (n < 0 || (size_t) n < l) {
519 log_error("Failed to write file %s: %s", path, n < 0 ? strerror(-n) : "Short write");
Lennart Poettering03e334a2014-03-18 19:22:43 +0100520 safe_close(fd);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400521 return n < 0 ? n : -EIO;
522 }
523 }
524
Lennart Poettering03e334a2014-03-18 19:22:43 +0100525 safe_close(fd);
Dave Reisner3612fbc2012-09-12 16:21:00 -0400526
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400527 if (stat(path, &st) < 0) {
528 log_error("stat(%s) failed: %m", path);
529 return -errno;
530 }
531
532 if (!S_ISREG(st.st_mode)) {
533 log_error("%s is not a file.", path);
534 return -EEXIST;
535 }
536
537 r = item_set_perms(i, path);
538 if (r < 0)
539 return r;
540
541 return 0;
542}
543
Michal Schmidt062e01b2011-12-16 18:00:11 +0100544static int recursive_relabel_children(Item *i, const char *path) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200545 _cleanup_closedir_ DIR *d;
Michal Schmidta8d88782011-12-15 23:11:07 +0100546 int ret = 0;
547
548 /* This returns the first error we run into, but nevertheless
549 * tries to go on */
550
551 d = opendir(path);
552 if (!d)
553 return errno == ENOENT ? 0 : -errno;
554
555 for (;;) {
Lennart Poettering7d5e9c02012-09-19 22:21:09 +0200556 struct dirent *de;
Zbigniew Jędrzejewski-Szmek6cf487a2012-11-02 15:05:31 +0100557 bool dir;
Michal Schmidta8d88782011-12-15 23:11:07 +0100558 int r;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200559 _cleanup_free_ char *entry_path = NULL;
Michal Schmidta8d88782011-12-15 23:11:07 +0100560
Florian Weimerd78096b2013-12-19 12:26:07 +0100561 errno = 0;
562 de = readdir(d);
563 if (!de && errno != 0) {
Michal Schmidta8d88782011-12-15 23:11:07 +0100564 if (ret == 0)
Florian Weimerd78096b2013-12-19 12:26:07 +0100565 ret = -errno;
Michal Schmidta8d88782011-12-15 23:11:07 +0100566 break;
567 }
568
569 if (!de)
570 break;
571
572 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
573 continue;
574
575 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
576 if (ret == 0)
577 ret = -ENOMEM;
578 continue;
579 }
580
581 if (de->d_type == DT_UNKNOWN) {
Zbigniew Jędrzejewski-Szmek6cf487a2012-11-02 15:05:31 +0100582 r = is_dir(entry_path);
583 if (r < 0) {
Michal Schmidta8d88782011-12-15 23:11:07 +0100584 if (ret == 0 && errno != ENOENT)
585 ret = -errno;
Michal Schmidta8d88782011-12-15 23:11:07 +0100586 continue;
587 }
588
Zbigniew Jędrzejewski-Szmek6cf487a2012-11-02 15:05:31 +0100589 dir = r;
Michal Schmidta8d88782011-12-15 23:11:07 +0100590
591 } else
Zbigniew Jędrzejewski-Szmek6cf487a2012-11-02 15:05:31 +0100592 dir = de->d_type == DT_DIR;
Michal Schmidta8d88782011-12-15 23:11:07 +0100593
Michal Schmidt062e01b2011-12-16 18:00:11 +0100594 r = item_set_perms(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100595 if (r < 0) {
596 if (ret == 0 && r != -ENOENT)
597 ret = r;
Michal Schmidta8d88782011-12-15 23:11:07 +0100598 continue;
599 }
600
Zbigniew Jędrzejewski-Szmek6cf487a2012-11-02 15:05:31 +0100601 if (dir) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100602 r = recursive_relabel_children(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100603 if (r < 0 && ret == 0)
604 ret = r;
605 }
Michal Schmidta8d88782011-12-15 23:11:07 +0100606 }
607
Michal Schmidta8d88782011-12-15 23:11:07 +0100608 return ret;
609}
610
611static int recursive_relabel(Item *i, const char *path) {
612 int r;
613 struct stat st;
614
Michal Schmidt062e01b2011-12-16 18:00:11 +0100615 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100616 if (r < 0)
617 return r;
618
619 if (lstat(path, &st) < 0)
620 return -errno;
621
622 if (S_ISDIR(st.st_mode))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100623 r = recursive_relabel_children(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100624
625 return r;
626}
627
Michal Schmidt99e68c02011-12-15 23:45:26 +0100628static int glob_item(Item *i, int (*action)(Item *, const char *)) {
629 int r = 0, k;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200630 _cleanup_globfree_ glob_t g = {};
Michal Schmidt99e68c02011-12-15 23:45:26 +0100631 char **fn;
632
Michal Schmidt99e68c02011-12-15 23:45:26 +0100633 errno = 0;
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400634 k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
635 if (k != 0)
Michal Schmidt99e68c02011-12-15 23:45:26 +0100636 if (k != GLOB_NOMATCH) {
Zbigniew Jędrzejewski-Szmek8333c772013-03-28 09:24:15 -0400637 if (errno > 0)
Michal Schmidt99e68c02011-12-15 23:45:26 +0100638 errno = EIO;
639
640 log_error("glob(%s) failed: %m", i->path);
641 return -errno;
642 }
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400643
644 STRV_FOREACH(fn, g.gl_pathv) {
645 k = action(i, *fn);
646 if (k < 0)
647 r = k;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100648 }
649
Michal Schmidt99e68c02011-12-15 23:45:26 +0100650 return r;
651}
652
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200653static int create_item(Item *i) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200654 struct stat st;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200655 int r = 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200656
657 assert(i);
658
659 switch (i->type) {
660
661 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100662 case IGNORE_DIRECTORY_PATH:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200663 case REMOVE_PATH:
664 case RECURSIVE_REMOVE_PATH:
665 return 0;
666
667 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100668 case TRUNCATE_FILE:
Dave Reisner1845fdd2012-09-27 20:48:13 -0400669 r = write_one_file(i, i->path);
670 if (r < 0)
671 return r;
672 break;
Lennart Poettering265ffa12013-09-17 16:33:30 -0500673
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400674 case WRITE_FILE:
675 r = glob_item(i, write_one_file);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100676 if (r < 0)
677 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200678
679 break;
680
Lennart Poettering265ffa12013-09-17 16:33:30 -0500681 case ADJUST_MODE:
682 r = item_set_perms_full(i, i->path, true);
683 if (r < 0)
684 return r;
685
686 break;
687
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200688 case TRUNCATE_DIRECTORY:
689 case CREATE_DIRECTORY:
690
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200691 RUN_WITH_UMASK(0000) {
692 mkdir_parents_label(i->path, 0755);
693 r = mkdir(i->path, i->mode);
694 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200695
696 if (r < 0 && errno != EEXIST) {
697 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100698 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200699 }
700
701 if (stat(i->path, &st) < 0) {
702 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100703 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200704 }
705
706 if (!S_ISDIR(st.st_mode)) {
707 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100708 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200709 }
710
Michal Schmidt062e01b2011-12-16 18:00:11 +0100711 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100712 if (r < 0)
713 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200714
715 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200716
717 case CREATE_FIFO:
718
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200719 RUN_WITH_UMASK(0000) {
720 r = mkfifo(i->path, i->mode);
721 }
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200722
723 if (r < 0 && errno != EEXIST) {
724 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100725 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200726 }
727
728 if (stat(i->path, &st) < 0) {
729 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100730 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200731 }
732
733 if (!S_ISFIFO(st.st_mode)) {
734 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100735 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200736 }
737
Michal Schmidt062e01b2011-12-16 18:00:11 +0100738 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100739 if (r < 0)
740 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200741
742 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100743
Lennart Poettering468d7262012-01-17 15:04:12 +0100744 case CREATE_SYMLINK: {
Lennart Poetteringe26da2d2014-02-19 17:52:41 +0100745 _cleanup_free_ char *x = NULL;
Lennart Poettering468d7262012-01-17 15:04:12 +0100746
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200747 label_context_set(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100748 r = symlink(i->argument, i->path);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200749 label_context_clear();
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200750
Lennart Poettering468d7262012-01-17 15:04:12 +0100751 if (r < 0 && errno != EEXIST) {
752 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
753 return -errno;
754 }
755
756 r = readlink_malloc(i->path, &x);
757 if (r < 0) {
758 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
759 return -errno;
760 }
761
762 if (!streq(i->argument, x)) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100763 log_error("%s is not the right symlinks.", i->path);
764 return -EEXIST;
765 }
766
Lennart Poettering468d7262012-01-17 15:04:12 +0100767 break;
768 }
769
770 case CREATE_BLOCK_DEVICE:
771 case CREATE_CHAR_DEVICE: {
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700772 mode_t file_type;
773
774 if (have_effective_cap(CAP_MKNOD) == 0) {
775 /* In a container we lack CAP_MKNOD. We
Anatol Pomozovab06eef2013-04-14 19:37:54 -0700776 shouldn't attempt to create the device node in
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700777 that case to avoid noise, and we don't support
778 virtualized devices in containers anyway. */
779
780 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
781 return 0;
782 }
783
784 file_type = (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +0100785
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200786 RUN_WITH_UMASK(0000) {
787 label_context_set(i->path, file_type);
788 r = mknod(i->path, i->mode | file_type, i->major_minor);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200789 label_context_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200790 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100791
792 if (r < 0 && errno != EEXIST) {
793 log_error("Failed to create device node %s: %m", i->path);
794 return -errno;
795 }
796
797 if (stat(i->path, &st) < 0) {
798 log_error("stat(%s) failed: %m", i->path);
799 return -errno;
800 }
801
Michal Schmidte7aee752012-06-14 16:01:19 +0200802 if ((st.st_mode & S_IFMT) != file_type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100803 log_error("%s is not a device node.", i->path);
804 return -EEXIST;
805 }
806
807 r = item_set_perms(i, i->path);
808 if (r < 0)
809 return r;
810
811 break;
812 }
813
Michal Schmidt777b87e2011-12-16 18:27:35 +0100814 case RELABEL_PATH:
815
816 r = glob_item(i, item_set_perms);
817 if (r < 0)
Lennart Poettering96ca8192013-06-21 15:57:42 +0200818 return r;
Michal Schmidt777b87e2011-12-16 18:27:35 +0100819 break;
820
Michal Schmidta8d88782011-12-15 23:11:07 +0100821 case RECURSIVE_RELABEL_PATH:
822
823 r = glob_item(i, recursive_relabel);
824 if (r < 0)
825 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200826 }
827
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200828 log_debug("%s created successfully.", i->path);
829
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100830 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200831}
832
Michal Schmidta0896122011-12-15 21:32:50 +0100833static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200834 int r;
835
836 assert(i);
837
838 switch (i->type) {
839
840 case CREATE_FILE:
841 case TRUNCATE_FILE:
842 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200843 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100844 case CREATE_SYMLINK:
845 case CREATE_BLOCK_DEVICE:
846 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200847 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100848 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100849 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100850 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100851 case WRITE_FILE:
Lennart Poettering265ffa12013-09-17 16:33:30 -0500852 case ADJUST_MODE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200853 break;
854
855 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100856 if (remove(instance) < 0 && errno != ENOENT) {
857 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200858 return -errno;
859 }
860
861 break;
862
863 case TRUNCATE_DIRECTORY:
864 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +0200865 /* FIXME: we probably should use dir_cleanup() here
866 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +0200867 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Lennart Poettering468d7262012-01-17 15:04:12 +0100868 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100869 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200870 return r;
871 }
872
873 break;
874 }
875
876 return 0;
877}
878
Michal Schmidta0896122011-12-15 21:32:50 +0100879static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100880 int r = 0;
881
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100882 assert(i);
883
884 switch (i->type) {
885
886 case CREATE_FILE:
887 case TRUNCATE_FILE:
888 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200889 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100890 case CREATE_SYMLINK:
891 case CREATE_CHAR_DEVICE:
892 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100893 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100894 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100895 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100896 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100897 case WRITE_FILE:
Lennart Poettering265ffa12013-09-17 16:33:30 -0500898 case ADJUST_MODE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100899 break;
900
901 case REMOVE_PATH:
902 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100903 case RECURSIVE_REMOVE_PATH:
904 r = glob_item(i, remove_item_instance);
905 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100906 }
907
Michal Schmidt99e68c02011-12-15 23:45:26 +0100908 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100909}
910
Michal Sekletar78a92a52013-01-18 16:13:08 +0100911static int clean_item_instance(Item *i, const char* instance) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200912 _cleanup_closedir_ DIR *d = NULL;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100913 struct stat s, ps;
914 bool mountpoint;
915 int r;
916 usec_t cutoff, n;
917
918 assert(i);
919
920 if (!i->age_set)
921 return 0;
922
923 n = now(CLOCK_REALTIME);
924 if (n < i->age)
925 return 0;
926
927 cutoff = n - i->age;
928
929 d = opendir(instance);
930 if (!d) {
931 if (errno == ENOENT || errno == ENOTDIR)
932 return 0;
933
934 log_error("Failed to open directory %s: %m", i->path);
935 return -errno;
936 }
937
938 if (fstat(dirfd(d), &s) < 0) {
939 log_error("stat(%s) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500940 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100941 }
942
943 if (!S_ISDIR(s.st_mode)) {
944 log_error("%s is not a directory.", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500945 return -ENOTDIR;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100946 }
947
948 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
949 log_error("stat(%s/..) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500950 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100951 }
952
953 mountpoint = s.st_dev != ps.st_dev ||
954 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
955
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500956 r = dir_cleanup(i, instance, d, &s, cutoff, s.st_dev, mountpoint,
957 MAX_DEPTH, i->keep_first_level);
Michal Sekletar78a92a52013-01-18 16:13:08 +0100958 return r;
959}
960
961static int clean_item(Item *i) {
962 int r = 0;
963
964 assert(i);
965
966 switch (i->type) {
967 case CREATE_DIRECTORY:
968 case TRUNCATE_DIRECTORY:
969 case IGNORE_PATH:
970 clean_item_instance(i, i->path);
971 break;
972 case IGNORE_DIRECTORY_PATH:
973 r = glob_item(i, clean_item_instance);
974 break;
975 default:
976 break;
977 }
978
979 return r;
980}
981
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200982static int process_item(Item *i) {
983 int r, q, p;
984
985 assert(i);
986
987 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100988 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200989 p = arg_clean ? clean_item(i) : 0;
990
991 if (r < 0)
992 return r;
993
994 if (q < 0)
995 return q;
996
997 return p;
998}
999
1000static void item_free(Item *i) {
1001 assert(i);
1002
1003 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +01001004 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001005 free(i);
1006}
1007
Lennart Poettering14bf2c92013-10-14 04:59:26 +02001008DEFINE_TRIVIAL_CLEANUP_FUNC(Item*, item_free);
Maciej Wereskie2f2fb72013-07-19 15:43:12 +02001009
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001010static bool item_equal(Item *a, Item *b) {
1011 assert(a);
1012 assert(b);
1013
1014 if (!streq_ptr(a->path, b->path))
1015 return false;
1016
1017 if (a->type != b->type)
1018 return false;
1019
1020 if (a->uid_set != b->uid_set ||
1021 (a->uid_set && a->uid != b->uid))
1022 return false;
1023
1024 if (a->gid_set != b->gid_set ||
1025 (a->gid_set && a->gid != b->gid))
1026 return false;
1027
1028 if (a->mode_set != b->mode_set ||
1029 (a->mode_set && a->mode != b->mode))
1030 return false;
1031
1032 if (a->age_set != b->age_set ||
1033 (a->age_set && a->age != b->age))
1034 return false;
1035
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001036 if ((a->type == CREATE_FILE ||
1037 a->type == TRUNCATE_FILE ||
1038 a->type == WRITE_FILE ||
1039 a->type == CREATE_SYMLINK) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +01001040 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +01001041 return false;
1042
1043 if ((a->type == CREATE_CHAR_DEVICE ||
1044 a->type == CREATE_BLOCK_DEVICE) &&
1045 a->major_minor != b->major_minor)
1046 return false;
1047
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001048 return true;
1049}
1050
Dave Reisnera2aced42013-07-24 11:10:05 -04001051static bool should_include_path(const char *path) {
1052 char **prefix;
1053
Dave Reisner5c795112013-07-24 11:19:24 -04001054 STRV_FOREACH(prefix, exclude_prefixes) {
1055 if (path_startswith(path, *prefix))
1056 return false;
1057 }
Dave Reisnera2aced42013-07-24 11:10:05 -04001058
1059 STRV_FOREACH(prefix, include_prefixes) {
1060 if (path_startswith(path, *prefix))
1061 return true;
1062 }
1063
Dave Reisner5c795112013-07-24 11:19:24 -04001064 /* no matches, so we should include this path only if we
1065 * have no whitelist at all */
1066 return strv_length(include_prefixes) == 0;
Dave Reisnera2aced42013-07-24 11:10:05 -04001067}
1068
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001069static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001070
1071 static const Specifier specifier_table[] = {
1072 { 'm', specifier_machine_id, NULL },
1073 { 'b', specifier_boot_id, NULL },
1074 { 'H', specifier_host_name, NULL },
1075 { 'v', specifier_kernel_release, NULL },
1076 {}
1077 };
1078
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001079 _cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
1080 _cleanup_(item_freep) Item *i = NULL;
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001081 Item *existing;
Michal Schmidt66ccd032011-12-15 21:31:14 +01001082 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001083 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001084 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +02001085
1086 assert(fname);
1087 assert(line >= 1);
1088 assert(buffer);
1089
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001090 r = sscanf(buffer,
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001091 "%ms %ms %ms %ms %ms %ms %n",
1092 &action,
Lennart Poettering1731e342013-09-17 11:02:02 -05001093 &path,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +01001094 &mode,
1095 &user,
1096 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +01001097 &age,
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001098 &n);
1099 if (r < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001100 log_error("[%s:%u] Syntax error.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001101 return -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +02001102 }
1103
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001104 if (strlen(action) > 2 || (strlen(action) > 1 && action[1] != '!')) {
1105 log_error("[%s:%u] Unknown modifier '%s'", fname, line, action);
1106 return -EINVAL;
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001107 } else if (strlen(action) > 1 && !arg_boot)
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001108 return 0;
1109
1110 type = action[0];
1111
Lennart Poettering1731e342013-09-17 11:02:02 -05001112 i = new0(Item, 1);
1113 if (!i)
1114 return log_oom();
1115
1116 r = specifier_printf(path, specifier_table, NULL, &i->path);
1117 if (r < 0) {
1118 log_error("[%s:%u] Failed to replace specifiers: %s", fname, line, path);
1119 return r;
1120 }
1121
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001122 if (n >= 0) {
1123 n += strspn(buffer+n, WHITESPACE);
1124 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
1125 i->argument = unquote(buffer+n, "\"");
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001126 if (!i->argument)
1127 return log_oom();
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001128 }
1129 }
1130
Michal Schmidt777b87e2011-12-16 18:27:35 +01001131 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001132
Michal Schmidt777b87e2011-12-16 18:27:35 +01001133 case CREATE_FILE:
1134 case TRUNCATE_FILE:
1135 case CREATE_DIRECTORY:
1136 case TRUNCATE_DIRECTORY:
1137 case CREATE_FIFO:
1138 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001139 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001140 case REMOVE_PATH:
1141 case RECURSIVE_REMOVE_PATH:
1142 case RELABEL_PATH:
1143 case RECURSIVE_RELABEL_PATH:
Lennart Poettering265ffa12013-09-17 16:33:30 -05001144 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001145 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001146
1147 case CREATE_SYMLINK:
1148 if (!i->argument) {
1149 log_error("[%s:%u] Symlink file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001150 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001151 }
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001152
Lennart Poettering468d7262012-01-17 15:04:12 +01001153 break;
1154
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001155 case WRITE_FILE:
1156 if (!i->argument) {
1157 log_error("[%s:%u] Write file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001158 return -EBADMSG;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001159 }
1160 break;
1161
Lennart Poettering468d7262012-01-17 15:04:12 +01001162 case CREATE_CHAR_DEVICE:
1163 case CREATE_BLOCK_DEVICE: {
1164 unsigned major, minor;
1165
1166 if (!i->argument) {
1167 log_error("[%s:%u] Device file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001168 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001169 }
1170
1171 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1172 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 -04001173 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001174 }
1175
1176 i->major_minor = makedev(major, minor);
1177 break;
1178 }
1179
Michal Schmidt777b87e2011-12-16 18:27:35 +01001180 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001181 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001182 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001183 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001184
Michal Schmidta8d88782011-12-15 23:11:07 +01001185 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001186
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001187 if (!path_is_absolute(i->path)) {
1188 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001189 return -EBADMSG;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001190 }
1191
1192 path_kill_slashes(i->path);
1193
Dave Reisnera2aced42013-07-24 11:10:05 -04001194 if (!should_include_path(i->path))
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001195 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001196
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001197 if (arg_root) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001198 char *p;
1199
1200 p = strappend(arg_root, i->path);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001201 if (!p)
1202 return log_oom();
1203
1204 free(i->path);
1205 i->path = p;
1206 }
1207
Lennart Poettering5008d582010-09-28 02:34:02 +02001208 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001209 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001210
Lennart Poetteringd05c5032012-07-16 12:34:54 +02001211 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001212 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001213 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001214 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001215 }
1216
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001217 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001218 }
1219
1220 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001221 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001222
Lennart Poettering4b678342011-07-23 01:17:59 +02001223 r = get_group_creds(&g, &i->gid);
1224 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001225 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001226 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001227 }
1228
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001229 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001230 }
1231
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001232 if (mode && !streq(mode, "-")) {
1233 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001234
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001235 if (sscanf(mode, "%o", &m) != 1) {
1236 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001237 return -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001238 }
1239
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001240 i->mode = m;
1241 i->mode_set = true;
1242 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001243 i->mode =
1244 i->type == CREATE_DIRECTORY ||
1245 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001246
1247 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001248 const char *a = age;
1249
1250 if (*a == '~') {
1251 i->keep_first_level = true;
1252 a++;
1253 }
1254
Lennart Poettering7f602782013-04-02 20:38:16 +02001255 if (parse_sec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001256 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001257 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001258 }
1259
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001260 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001261 }
1262
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001263 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001264
Lennart Poettering468d7262012-01-17 15:04:12 +01001265 existing = hashmap_get(h, i->path);
1266 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001267
1268 /* Two identical items are fine */
1269 if (!item_equal(existing, i))
1270 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1271
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001272 return 0;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001273 }
1274
Lennart Poettering468d7262012-01-17 15:04:12 +01001275 r = hashmap_put(h, i->path, i);
1276 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001277 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001278 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001279 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001280
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001281 i = NULL; /* avoid cleanup */
Lennart Poettering5008d582010-09-28 02:34:02 +02001282
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001283 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001284}
1285
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001286static int help(void) {
1287
Lennart Poettering522d4a42011-02-13 15:08:15 +01001288 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1289 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001290 " -h --help Show this help\n"
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001291 " --version Show package version\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001292 " --create Create marked files/directories\n"
1293 " --clean Clean up marked directories\n"
1294 " --remove Remove marked files/directories\n"
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001295 " --boot Execute actions only safe at boot\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001296 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n"
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001297 " --exclude-prefix=PATH Ignore rules that apply to paths with the specified prefix\n"
1298 " --root=PATH Operate on an alternate filesystem root\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001299 program_invocation_short_name);
1300
1301 return 0;
1302}
1303
1304static int parse_argv(int argc, char *argv[]) {
1305
1306 enum {
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001307 ARG_VERSION = 0x100,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001308 ARG_CREATE,
1309 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001310 ARG_REMOVE,
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001311 ARG_BOOT,
Dave Reisner5c795112013-07-24 11:19:24 -04001312 ARG_PREFIX,
1313 ARG_EXCLUDE_PREFIX,
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001314 ARG_ROOT,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001315 };
1316
1317 static const struct option options[] = {
Dave Reisner5c795112013-07-24 11:19:24 -04001318 { "help", no_argument, NULL, 'h' },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001319 { "version", no_argument, NULL, ARG_VERSION },
Dave Reisner5c795112013-07-24 11:19:24 -04001320 { "create", no_argument, NULL, ARG_CREATE },
1321 { "clean", no_argument, NULL, ARG_CLEAN },
1322 { "remove", no_argument, NULL, ARG_REMOVE },
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001323 { "boot", no_argument, NULL, ARG_BOOT },
Dave Reisner5c795112013-07-24 11:19:24 -04001324 { "prefix", required_argument, NULL, ARG_PREFIX },
1325 { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX },
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001326 { "root", required_argument, NULL, ARG_ROOT },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001327 {}
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001328 };
1329
1330 int c;
1331
1332 assert(argc >= 0);
1333 assert(argv);
1334
1335 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1336
1337 switch (c) {
1338
1339 case 'h':
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001340 return help();
1341
1342 case ARG_VERSION:
1343 puts(PACKAGE_STRING);
1344 puts(SYSTEMD_FEATURES);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001345 return 0;
1346
1347 case ARG_CREATE:
1348 arg_create = true;
1349 break;
1350
1351 case ARG_CLEAN:
1352 arg_clean = true;
1353 break;
1354
1355 case ARG_REMOVE:
1356 arg_remove = true;
1357 break;
1358
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001359 case ARG_BOOT:
1360 arg_boot = true;
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001361 break;
1362
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001363 case ARG_PREFIX:
Zbigniew Jędrzejewski-Szmek498f8a32014-01-30 21:40:27 -05001364 if (strv_push(&include_prefixes, optarg) < 0)
Dave Reisnera2aced42013-07-24 11:10:05 -04001365 return log_oom();
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001366 break;
1367
Dave Reisner5c795112013-07-24 11:19:24 -04001368 case ARG_EXCLUDE_PREFIX:
Zbigniew Jędrzejewski-Szmek498f8a32014-01-30 21:40:27 -05001369 if (strv_push(&exclude_prefixes, optarg) < 0)
Dave Reisner5c795112013-07-24 11:19:24 -04001370 return log_oom();
1371 break;
1372
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001373 case ARG_ROOT:
1374 arg_root = path_make_absolute_cwd(optarg);
1375 if (!arg_root)
1376 return log_oom();
1377 path_kill_slashes(arg_root);
1378 break;
1379
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001380 case '?':
1381 return -EINVAL;
1382
1383 default:
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001384 assert_not_reached("Unhandled option");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001385 }
1386 }
1387
1388 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001389 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001390 return -EINVAL;
1391 }
1392
1393 return 1;
1394}
1395
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001396static int read_config_file(const char *fn, bool ignore_enoent) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001397 _cleanup_fclose_ FILE *f = NULL;
1398 char line[LINE_MAX];
Michal Sekletar78a92a52013-01-18 16:13:08 +01001399 Iterator iterator;
Lennart Poettering1731e342013-09-17 11:02:02 -05001400 unsigned v = 0;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001401 Item *i;
Lennart Poettering1731e342013-09-17 11:02:02 -05001402 int r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001403
1404 assert(fn);
1405
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001406 r = search_and_fopen_nulstr(fn, "re", arg_root, conf_file_dirs, &f);
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001407 if (r < 0) {
1408 if (ignore_enoent && r == -ENOENT)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001409 return 0;
1410
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001411 log_error("Failed to open '%s', ignoring: %s", fn, strerror(-r));
1412 return r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001413 }
1414
Lennart Poettering1731e342013-09-17 11:02:02 -05001415 FOREACH_LINE(line, f, break) {
1416 char *l;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001417 int k;
1418
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001419 v++;
1420
1421 l = strstrip(line);
1422 if (*l == '#' || *l == 0)
1423 continue;
1424
Lennart Poettering1731e342013-09-17 11:02:02 -05001425 k = parse_line(fn, v, l);
1426 if (k < 0 && r == 0)
1427 r = k;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001428 }
1429
Michal Sekletar78a92a52013-01-18 16:13:08 +01001430 /* we have to determine age parameter for each entry of type X */
1431 HASHMAP_FOREACH(i, globs, iterator) {
1432 Iterator iter;
1433 Item *j, *candidate_item = NULL;
1434
1435 if (i->type != IGNORE_DIRECTORY_PATH)
1436 continue;
1437
1438 HASHMAP_FOREACH(j, items, iter) {
1439 if (j->type != CREATE_DIRECTORY && j->type != TRUNCATE_DIRECTORY)
1440 continue;
1441
1442 if (path_equal(j->path, i->path)) {
1443 candidate_item = j;
1444 break;
1445 }
1446
1447 if ((!candidate_item && path_startswith(i->path, j->path)) ||
1448 (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
1449 candidate_item = j;
1450 }
1451
1452 if (candidate_item) {
1453 i->age = candidate_item->age;
1454 i->age_set = true;
1455 }
1456 }
1457
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001458 if (ferror(f)) {
1459 log_error("Failed to read from file %s: %m", fn);
1460 if (r == 0)
1461 r = -EIO;
1462 }
1463
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001464 return r;
1465}
1466
Lennart Poettering5008d582010-09-28 02:34:02 +02001467int main(int argc, char *argv[]) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001468 int r, k;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001469 Item *i;
1470 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001471
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001472 r = parse_argv(argc, argv);
1473 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001474 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1475
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001476 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001477 log_parse_environment();
1478 log_open();
1479
Lennart Poettering4c126262011-08-01 20:52:18 +02001480 umask(0022);
1481
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001482 label_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001483
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001484 items = hashmap_new(string_hash_func, string_compare_func);
1485 globs = hashmap_new(string_hash_func, string_compare_func);
1486
1487 if (!items || !globs) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001488 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001489 goto finish;
1490 }
1491
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001492 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001493
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001494 if (optind < argc) {
1495 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001496
Dave Reisner91256702012-06-08 22:31:19 -04001497 for (j = optind; j < argc; j++) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001498 k = read_config_file(argv[j], false);
1499 if (k < 0 && r == 0)
1500 r = k;
Dave Reisner91256702012-06-08 22:31:19 -04001501 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001502
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001503 } else {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001504 _cleanup_strv_free_ char **files = NULL;
1505 char **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001506
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001507 r = conf_files_list_nulstr(&files, ".conf", arg_root, conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001508 if (r < 0) {
Kay Sievers44143302011-04-28 23:51:24 +02001509 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
1510 goto finish;
1511 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001512
Kay Sievers772f8372011-04-25 21:38:21 +02001513 STRV_FOREACH(f, files) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001514 k = read_config_file(*f, true);
1515 if (k < 0 && r == 0)
1516 r = k;
Lennart Poettering5008d582010-09-28 02:34:02 +02001517 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001518 }
1519
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001520 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001521 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001522
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001523 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001524 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001525
Lennart Poettering5008d582010-09-28 02:34:02 +02001526finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001527 while ((i = hashmap_steal_first(items)))
1528 item_free(i);
1529
Lennart Poettering17b90522011-02-14 21:55:06 +01001530 while ((i = hashmap_steal_first(globs)))
1531 item_free(i);
1532
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001533 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001534 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001535
Zbigniew Jędrzejewski-Szmek498f8a32014-01-30 21:40:27 -05001536 free(include_prefixes);
1537 free(exclude_prefixes);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001538 free(arg_root);
Dave Reisnera2aced42013-07-24 11:10:05 -04001539
Lennart Poettering17b90522011-02-14 21:55:06 +01001540 set_free_free(unix_sockets);
1541
Lennart Poettering29003cf2010-10-19 19:36:45 +02001542 label_finish();
1543
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001544 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Lennart Poettering5008d582010-09-28 02:34:02 +02001545}