blob: 8ec8252a7bf8f3c8b8e55d6f1e09e18e532b176c [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 Poettering849958d2014-06-10 23:02:40 +020056#include "copy.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020057
Andreas Jaeger01000472010-09-29 10:08:24 +020058/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020059 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020060 * properly owned directories beneath /tmp, /var/tmp, /run, which are
61 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020062
Michal Schmidt66ccd032011-12-15 21:31:14 +010063typedef enum ItemType {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010064 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020065 CREATE_FILE = 'f',
66 TRUNCATE_FILE = 'F',
67 CREATE_DIRECTORY = 'd',
68 TRUNCATE_DIRECTORY = 'D',
Lennart Poetteringee17ee72011-07-12 03:56:56 +020069 CREATE_FIFO = 'p',
Lennart Poettering468d7262012-01-17 15:04:12 +010070 CREATE_SYMLINK = 'L',
71 CREATE_CHAR_DEVICE = 'c',
72 CREATE_BLOCK_DEVICE = 'b',
Lennart Poettering849958d2014-06-10 23:02:40 +020073 COPY_FILES = 'C',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010074
75 /* These ones take globs */
Lennart Poetteringcde684a2014-06-10 22:50:46 +020076 WRITE_FILE = 'w',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020077 IGNORE_PATH = 'x',
Michal Sekletar78a92a52013-01-18 16:13:08 +010078 IGNORE_DIRECTORY_PATH = 'X',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020079 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010080 RECURSIVE_REMOVE_PATH = 'R',
Lennart Poetteringe73a03e2014-06-10 23:42:16 +020081 ADJUST_MODE = 'm', /* legacy, 'z' is identical to this */
Michal Schmidt777b87e2011-12-16 18:27:35 +010082 RELABEL_PATH = 'z',
Lennart Poetteringe73a03e2014-06-10 23:42:16 +020083 RECURSIVE_RELABEL_PATH = 'Z',
Michal Schmidt66ccd032011-12-15 21:31:14 +010084} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020085
86typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +010087 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020088
89 char *path;
Lennart Poettering468d7262012-01-17 15:04:12 +010090 char *argument;
Lennart Poettering5008d582010-09-28 02:34:02 +020091 uid_t uid;
92 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020093 mode_t mode;
94 usec_t age;
95
Lennart Poettering468d7262012-01-17 15:04:12 +010096 dev_t major_minor;
97
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020098 bool uid_set:1;
99 bool gid_set:1;
100 bool mode_set:1;
101 bool age_set:1;
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200102 bool mask_perms:1;
Lennart Poettering24f3a372012-06-20 09:05:50 +0200103
104 bool keep_first_level:1;
Lennart Poettering1910cd02014-06-11 01:37:35 +0200105
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200106 bool force:1;
107
Lennart Poettering1910cd02014-06-11 01:37:35 +0200108 bool done:1;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200109} Item;
110
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200111static bool arg_create = false;
112static bool arg_clean = false;
113static bool arg_remove = false;
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -0500114static bool arg_boot = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200115
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200116static char **arg_include_prefixes = NULL;
117static char **arg_exclude_prefixes = NULL;
Michael Marineaucf9a4ab2014-03-13 21:32:13 -0700118static char *arg_root = NULL;
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100119
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100120static const char conf_file_dirs[] =
121 "/etc/tmpfiles.d\0"
122 "/run/tmpfiles.d\0"
123 "/usr/local/lib/tmpfiles.d\0"
124 "/usr/lib/tmpfiles.d\0"
Lennart Poettering3f2afb22012-07-20 16:24:55 +0200125#ifdef HAVE_SPLIT_USR
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100126 "/lib/tmpfiles.d\0"
Lennart Poettering3f2afb22012-07-20 16:24:55 +0200127#endif
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100128 ;
Dave Reisner91256702012-06-08 22:31:19 -0400129
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200130#define MAX_DEPTH 256
131
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200132static Hashmap *items = NULL, *globs = NULL;
133static Set *unix_sockets = NULL;
134
Michal Schmidt66ccd032011-12-15 21:31:14 +0100135static bool needs_glob(ItemType t) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200136 return IN_SET(t,
137 WRITE_FILE,
138 IGNORE_PATH,
139 IGNORE_DIRECTORY_PATH,
140 REMOVE_PATH,
141 RECURSIVE_REMOVE_PATH,
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200142 ADJUST_MODE,
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200143 RELABEL_PATH,
144 RECURSIVE_RELABEL_PATH);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100145}
146
147static struct Item* find_glob(Hashmap *h, const char *match) {
148 Item *j;
149 Iterator i;
150
151 HASHMAP_FOREACH(j, h, i)
152 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
153 return j;
154
155 return NULL;
156}
157
Lennart Poettering17b90522011-02-14 21:55:06 +0100158static void load_unix_sockets(void) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200159 _cleanup_fclose_ FILE *f = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100160 char line[LINE_MAX];
161
162 if (unix_sockets)
163 return;
164
165 /* We maintain a cache of the sockets we found in
166 * /proc/net/unix to speed things up a little. */
167
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100168 unix_sockets = set_new(string_hash_func, string_compare_func);
169 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100170 return;
171
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100172 f = fopen("/proc/net/unix", "re");
173 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100174 return;
175
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100176 /* Skip header */
177 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100178 goto fail;
179
180 for (;;) {
181 char *p, *s;
182 int k;
183
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100184 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100185 break;
186
187 truncate_nl(line);
188
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100189 p = strchr(line, ':');
190 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100191 continue;
192
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100193 if (strlen(p) < 37)
194 continue;
195
196 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100197 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100198 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100199 p += strspn(p, WHITESPACE);
200
201 if (*p != '/')
202 continue;
203
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100204 s = strdup(p);
205 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100206 goto fail;
207
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100208 path_kill_slashes(s);
209
Zbigniew Jędrzejewski-Szmekef422022013-04-22 23:12:15 -0400210 k = set_consume(unix_sockets, s);
211 if (k < 0 && k != -EEXIST)
212 goto fail;
Lennart Poettering17b90522011-02-14 21:55:06 +0100213 }
214
215 return;
216
217fail:
218 set_free_free(unix_sockets);
219 unix_sockets = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100220}
221
222static bool unix_socket_alive(const char *fn) {
223 assert(fn);
224
225 load_unix_sockets();
226
227 if (unix_sockets)
228 return !!set_get(unix_sockets, (char*) fn);
229
230 /* We don't know, so assume yes */
231 return true;
232}
233
Kay Sievers99d680a2013-03-13 13:12:36 +0100234static int dir_is_mount_point(DIR *d, const char *subdir) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200235
236 union file_handle_union h = {
237 .handle.handle_bytes = MAX_HANDLE_SZ
238 };
239
Kay Sievers99d680a2013-03-13 13:12:36 +0100240 int mount_id_parent, mount_id;
241 int r_p, r;
242
Dave Reisner370c8602014-04-19 13:22:35 -0400243 r_p = name_to_handle_at(dirfd(d), ".", &h.handle, &mount_id_parent, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100244 if (r_p < 0)
245 r_p = -errno;
246
Dave Reisner370c8602014-04-19 13:22:35 -0400247 h.handle.handle_bytes = MAX_HANDLE_SZ;
248 r = name_to_handle_at(dirfd(d), subdir, &h.handle, &mount_id, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100249 if (r < 0)
250 r = -errno;
251
252 /* got no handle; make no assumptions, return error */
253 if (r_p < 0 && r < 0)
254 return r_p;
255
256 /* got both handles; if they differ, it is a mount point */
257 if (r_p >= 0 && r >= 0)
258 return mount_id_parent != mount_id;
259
260 /* got only one handle; assume different mount points if one
261 * of both queries was not supported by the filesystem */
262 if (r_p == -ENOSYS || r_p == -ENOTSUP || r == -ENOSYS || r == -ENOTSUP)
263 return true;
264
265 /* return error */
266 if (r_p < 0)
267 return r_p;
268 return r;
269}
270
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200271static int dir_cleanup(
Michal Sekletar78a92a52013-01-18 16:13:08 +0100272 Item *i,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200273 const char *p,
274 DIR *d,
275 const struct stat *ds,
276 usec_t cutoff,
277 dev_t rootdev,
278 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200279 int maxdepth,
Lennart Poettering265ffa12013-09-17 16:33:30 -0500280 bool keep_this_level) {
281
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200282 struct dirent *dent;
283 struct timespec times[2];
284 bool deleted = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200285 int r = 0;
286
287 while ((dent = readdir(d))) {
288 struct stat s;
289 usec_t age;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200290 _cleanup_free_ char *sub_path = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200291
292 if (streq(dent->d_name, ".") ||
293 streq(dent->d_name, ".."))
294 continue;
295
296 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
Kay Sieversca2f4172013-10-17 03:20:46 +0200297 if (errno == ENOENT)
298 continue;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200299
Kay Sieversca2f4172013-10-17 03:20:46 +0200300 /* FUSE, NFS mounts, SELinux might return EACCES */
301 if (errno == EACCES)
302 log_debug("stat(%s/%s) failed: %m", p, dent->d_name);
303 else
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200304 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
Kay Sieversca2f4172013-10-17 03:20:46 +0200305 r = -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200306 continue;
307 }
308
309 /* Stay on the same filesystem */
310 if (s.st_dev != rootdev)
311 continue;
312
Kay Sievers99d680a2013-03-13 13:12:36 +0100313 /* Try to detect bind mounts of the same filesystem instance; they
314 * do not differ in device major/minors. This type of query is not
315 * supported on all kernels or filesystem types though. */
316 if (S_ISDIR(s.st_mode) && dir_is_mount_point(d, dent->d_name) > 0)
317 continue;
318
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200319 /* Do not delete read-only files owned by root */
320 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
321 continue;
322
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200323 sub_path = strjoin(p, "/", dent->d_name, NULL);
324 if (!sub_path) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700325 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200326 goto finish;
327 }
328
329 /* Is there an item configured for this path? */
330 if (hashmap_get(items, sub_path))
331 continue;
332
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100333 if (find_glob(globs, sub_path))
334 continue;
335
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200336 if (S_ISDIR(s.st_mode)) {
337
338 if (mountpoint &&
339 streq(dent->d_name, "lost+found") &&
340 s.st_uid == 0)
341 continue;
342
343 if (maxdepth <= 0)
344 log_warning("Reached max depth on %s.", sub_path);
345 else {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200346 _cleanup_closedir_ DIR *sub_dir;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200347 int q;
348
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200349 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200350 if (!sub_dir) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200351 if (errno != ENOENT) {
352 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
353 r = -errno;
354 }
355
356 continue;
357 }
358
Michal Sekletar78a92a52013-01-18 16:13:08 +0100359 q = dir_cleanup(i, sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200360 if (q < 0)
361 r = q;
362 }
363
Lennart Poettering24f3a372012-06-20 09:05:50 +0200364 /* Note: if you are wondering why we don't
365 * support the sticky bit for excluding
366 * directories from cleaning like we do it for
367 * other file system objects: well, the sticky
368 * bit already has a meaning for directories,
369 * so we don't want to overload that. */
370
371 if (keep_this_level)
372 continue;
373
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200374 /* Ignore ctime, we change it when deleting */
375 age = MAX(timespec_load(&s.st_mtim),
376 timespec_load(&s.st_atim));
377 if (age >= cutoff)
378 continue;
379
Lukas Nykryna6187d42013-03-01 18:29:59 +0100380 if (i->type != IGNORE_DIRECTORY_PATH || !streq(dent->d_name, p)) {
Lennart Poettering9f6445e32013-12-24 16:39:37 +0100381 log_debug("rmdir '%s'", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200382
Michal Sekletar78a92a52013-01-18 16:13:08 +0100383 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
384 if (errno != ENOENT && errno != ENOTEMPTY) {
385 log_error("rmdir(%s): %m", sub_path);
386 r = -errno;
387 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200388 }
389 }
390
391 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100392 /* Skip files for which the sticky bit is
393 * set. These are semantics we define, and are
394 * unknown elsewhere. See XDG_RUNTIME_DIR
395 * specification for details. */
396 if (s.st_mode & S_ISVTX)
397 continue;
398
Lennart Poettering17b90522011-02-14 21:55:06 +0100399 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200400 if (streq(dent->d_name, ".journal") &&
401 s.st_uid == 0)
402 continue;
403
404 if (streq(dent->d_name, "aquota.user") ||
405 streq(dent->d_name, "aquota.group"))
406 continue;
407 }
408
Lennart Poettering17b90522011-02-14 21:55:06 +0100409 /* Ignore sockets that are listed in /proc/net/unix */
410 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
411 continue;
412
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100413 /* Ignore device nodes */
414 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
415 continue;
416
Lennart Poettering24f3a372012-06-20 09:05:50 +0200417 /* Keep files on this level around if this is
418 * requested */
419 if (keep_this_level)
420 continue;
421
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200422 age = MAX3(timespec_load(&s.st_mtim),
423 timespec_load(&s.st_atim),
424 timespec_load(&s.st_ctim));
425
426 if (age >= cutoff)
427 continue;
428
Lennart Poettering9f6445e32013-12-24 16:39:37 +0100429 log_debug("unlink '%s'", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200430
431 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
432 if (errno != ENOENT) {
433 log_error("unlink(%s): %m", sub_path);
434 r = -errno;
435 }
436 }
437
438 deleted = true;
439 }
440 }
441
442finish:
443 if (deleted) {
444 /* Restore original directory timestamps */
445 times[0] = ds->st_atim;
446 times[1] = ds->st_mtim;
447
448 if (futimens(dirfd(d), times) < 0)
449 log_error("utimensat(%s): %m", p);
450 }
451
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200452 return r;
453}
454
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200455static int item_set_perms(Item *i, const char *path) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200456 assert(i);
457 assert(path);
458
Michal Schmidt062e01b2011-12-16 18:00:11 +0100459 /* not using i->path directly because it may be a glob */
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200460 if (i->mode_set) {
461 mode_t m = i->mode;
462
463 if (i->mask_perms) {
464 struct stat st;
465
466 if (stat(path, &st) >= 0) {
467 if (!(st.st_mode & 0111))
468 m &= ~0111;
469 if (!(st.st_mode & 0222))
470 m &= ~0222;
471 if (!(st.st_mode & 0444))
472 m &= ~0444;
473 if (!S_ISDIR(st.st_mode))
474 m &= ~07000; /* remove sticky/sgid/suid bit, unless directory */
475 }
476 }
477
478 if (chmod(path, m) < 0) {
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200479 log_error("chmod(%s) failed: %m", path);
480 return -errno;
Michal Schmidt062e01b2011-12-16 18:00:11 +0100481 }
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200482 }
Michal Schmidt062e01b2011-12-16 18:00:11 +0100483
484 if (i->uid_set || i->gid_set)
485 if (chown(path,
486 i->uid_set ? i->uid : (uid_t) -1,
487 i->gid_set ? i->gid : (gid_t) -1) < 0) {
488
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200489 log_error("chown(%s) failed: %m", path);
490 return -errno;
Michal Schmidt062e01b2011-12-16 18:00:11 +0100491 }
492
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200493 return label_fix(path, false, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100494}
495
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400496static int write_one_file(Item *i, const char *path) {
Lennart Poettering874f1942014-06-10 22:48:56 +0200497 int flags;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200498 int fd = -1;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400499 struct stat st;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200500 int r = 0;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400501
Lennart Poettering874f1942014-06-10 22:48:56 +0200502 assert(i);
503 assert(path);
504
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400505 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
506 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
507
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200508 RUN_WITH_UMASK(0) {
509 label_context_set(path, S_IFREG);
510 fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200511 label_context_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200512 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400513
514 if (fd < 0) {
515 if (i->type == WRITE_FILE && errno == ENOENT)
516 return 0;
517
518 log_error("Failed to create file %s: %m", path);
519 return -errno;
520 }
521
522 if (i->argument) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200523 _cleanup_free_ char *unescaped;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400524 ssize_t n;
525 size_t l;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400526
Dave Reisner54693d92012-09-15 12:58:49 -0400527 unescaped = cunescape(i->argument);
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100528 if (unescaped == NULL) {
Lennart Poettering03e334a2014-03-18 19:22:43 +0100529 safe_close(fd);
Dave Reisner54693d92012-09-15 12:58:49 -0400530 return log_oom();
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100531 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400532
Dave Reisner54693d92012-09-15 12:58:49 -0400533 l = strlen(unescaped);
534 n = write(fd, unescaped, l);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400535
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400536 if (n < 0 || (size_t) n < l) {
537 log_error("Failed to write file %s: %s", path, n < 0 ? strerror(-n) : "Short write");
Lennart Poettering03e334a2014-03-18 19:22:43 +0100538 safe_close(fd);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400539 return n < 0 ? n : -EIO;
540 }
541 }
542
Lennart Poettering03e334a2014-03-18 19:22:43 +0100543 safe_close(fd);
Dave Reisner3612fbc2012-09-12 16:21:00 -0400544
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400545 if (stat(path, &st) < 0) {
546 log_error("stat(%s) failed: %m", path);
547 return -errno;
548 }
549
550 if (!S_ISREG(st.st_mode)) {
551 log_error("%s is not a file.", path);
552 return -EEXIST;
553 }
554
555 r = item_set_perms(i, path);
556 if (r < 0)
557 return r;
558
559 return 0;
560}
561
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200562static int item_set_perms_children(Item *i, const char *path) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200563 _cleanup_closedir_ DIR *d;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200564 int r = 0;
565
566 assert(i);
567 assert(path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100568
569 /* This returns the first error we run into, but nevertheless
570 * tries to go on */
571
572 d = opendir(path);
573 if (!d)
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200574 return errno == ENOENT || errno == ENOTDIR ? 0 : -errno;
Michal Schmidta8d88782011-12-15 23:11:07 +0100575
576 for (;;) {
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200577 _cleanup_free_ char *p = NULL;
Lennart Poettering7d5e9c02012-09-19 22:21:09 +0200578 struct dirent *de;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200579 int q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100580
Florian Weimerd78096b2013-12-19 12:26:07 +0100581 errno = 0;
582 de = readdir(d);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200583 if (!de) {
584 if (errno != 0 && r == 0)
585 r = -errno;
586
Michal Schmidta8d88782011-12-15 23:11:07 +0100587 break;
588 }
589
Michal Schmidta8d88782011-12-15 23:11:07 +0100590 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
591 continue;
592
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200593 p = strjoin(path, "/", de->d_name, NULL);
594 if (!p)
595 return -ENOMEM;
Michal Schmidta8d88782011-12-15 23:11:07 +0100596
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200597 q = item_set_perms(i, p);
598 if (q < 0 && q != -ENOENT && r == 0)
599 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100600
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200601 if (IN_SET(de->d_type, DT_UNKNOWN, DT_DIR)) {
602 q = item_set_perms_children(i, p);
603 if (q < 0 && r == 0)
604 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100605 }
Michal Schmidta8d88782011-12-15 23:11:07 +0100606 }
607
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200608 return r;
Michal Schmidta8d88782011-12-15 23:11:07 +0100609}
610
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200611static int item_set_perms_recursive(Item *i, const char *path) {
612 int r, q;
613
614 assert(i);
615 assert(path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100616
Michal Schmidt062e01b2011-12-16 18:00:11 +0100617 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100618 if (r < 0)
619 return r;
620
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200621 q = item_set_perms_children(i, path);
622 if (q < 0 && r == 0)
623 r = q;
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 *)) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200629 _cleanup_globfree_ glob_t g = {};
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200630 int r = 0, k;
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);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200635 if (k != 0 && k != GLOB_NOMATCH) {
636 if (errno == 0)
637 errno = EIO;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100638
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200639 log_error("glob(%s) failed: %m", i->path);
640 return -errno;
641 }
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400642
643 STRV_FOREACH(fn, g.gl_pathv) {
644 k = action(i, *fn);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200645 if (k < 0 && r == 0)
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400646 r = k;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100647 }
648
Michal Schmidt99e68c02011-12-15 23:45:26 +0100649 return r;
650}
651
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200652static int create_item(Item *i) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200653 struct stat st;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200654 int r = 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200655
656 assert(i);
657
658 switch (i->type) {
659
660 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100661 case IGNORE_DIRECTORY_PATH:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200662 case REMOVE_PATH:
663 case RECURSIVE_REMOVE_PATH:
664 return 0;
665
666 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100667 case TRUNCATE_FILE:
Dave Reisner1845fdd2012-09-27 20:48:13 -0400668 r = write_one_file(i, i->path);
669 if (r < 0)
670 return r;
671 break;
Lennart Poettering265ffa12013-09-17 16:33:30 -0500672
Lennart Poettering849958d2014-06-10 23:02:40 +0200673 case COPY_FILES:
674 r = copy_tree(i->argument, i->path);
675 if (r < 0) {
676 log_error("Failed to copy files: %s", strerror(-r));
677 return r;
678 }
679
680 r = item_set_perms(i, i->path);
681 if (r < 0)
682 return r;
683
684 break;
685
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400686 case WRITE_FILE:
687 r = glob_item(i, write_one_file);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100688 if (r < 0)
689 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200690
691 break;
692
693 case TRUNCATE_DIRECTORY:
694 case CREATE_DIRECTORY:
695
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200696 RUN_WITH_UMASK(0000) {
697 mkdir_parents_label(i->path, 0755);
698 r = mkdir(i->path, i->mode);
699 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200700
701 if (r < 0 && errno != EEXIST) {
702 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100703 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200704 }
705
706 if (stat(i->path, &st) < 0) {
707 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100708 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200709 }
710
711 if (!S_ISDIR(st.st_mode)) {
712 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100713 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200714 }
715
Michal Schmidt062e01b2011-12-16 18:00:11 +0100716 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100717 if (r < 0)
718 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200719
720 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200721
722 case CREATE_FIFO:
723
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200724 RUN_WITH_UMASK(0000) {
Lennart Poettering1554afa2014-06-17 23:50:22 +0200725 label_context_set(i->path, S_IFIFO);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200726 r = mkfifo(i->path, i->mode);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200727 label_context_clear();
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200728 }
729
Lennart Poettering1554afa2014-06-17 23:50:22 +0200730 if (r < 0) {
731 if (errno != EEXIST) {
732 log_error("Failed to create fifo %s: %m", i->path);
733 return -errno;
734 }
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200735
Lennart Poettering1554afa2014-06-17 23:50:22 +0200736 if (stat(i->path, &st) < 0) {
737 log_error("stat(%s) failed: %m", i->path);
738 return -errno;
739 }
740
741 if (!S_ISFIFO(st.st_mode)) {
742
743 if (i->force) {
744
745 RUN_WITH_UMASK(0000) {
746 label_context_set(i->path, S_IFIFO);
747 r = mkfifo_atomic(i->path, i->mode);
748 label_context_clear();
749 }
750
751 if (r < 0) {
752 log_error("Failed to create fifo %s: %s", i->path, strerror(-r));
753 return r;
754 }
755 } else {
756 log_debug("%s is not a fifo.", i->path);
757 return 0;
758 }
759 }
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200760 }
761
Michal Schmidt062e01b2011-12-16 18:00:11 +0100762 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100763 if (r < 0)
764 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200765
766 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100767
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200768 case CREATE_SYMLINK:
Lennart Poettering468d7262012-01-17 15:04:12 +0100769
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200770 label_context_set(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100771 r = symlink(i->argument, i->path);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200772 label_context_clear();
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200773
Lennart Poettering468d7262012-01-17 15:04:12 +0100774 if (r < 0) {
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200775 _cleanup_free_ char *x = NULL;
Lennart Poettering468d7262012-01-17 15:04:12 +0100776
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200777 if (errno != EEXIST) {
778 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
779 return -errno;
780 }
781
782 r = readlink_malloc(i->path, &x);
783 if (r < 0 || !streq(i->argument, x)) {
784
785 if (i->force) {
786 label_context_set(i->path, S_IFLNK);
787 r = symlink_atomic(i->argument, i->path);
788 label_context_clear();
789
790 if (r < 0) {
Lennart Poettering1554afa2014-06-17 23:50:22 +0200791 log_error("symlink(%s, %s) failed: %s", i->argument, i->path, strerror(-r));
792 return r;
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200793 }
Lennart Poettering1554afa2014-06-17 23:50:22 +0200794 } else {
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200795 log_debug("%s is not a symlink or does not point to the correct path.", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +0200796 return 0;
797 }
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200798 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100799 }
800
Lennart Poettering468d7262012-01-17 15:04:12 +0100801 break;
Lennart Poettering468d7262012-01-17 15:04:12 +0100802
803 case CREATE_BLOCK_DEVICE:
804 case CREATE_CHAR_DEVICE: {
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700805 mode_t file_type;
806
807 if (have_effective_cap(CAP_MKNOD) == 0) {
808 /* In a container we lack CAP_MKNOD. We
Anatol Pomozovab06eef2013-04-14 19:37:54 -0700809 shouldn't attempt to create the device node in
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700810 that case to avoid noise, and we don't support
811 virtualized devices in containers anyway. */
812
813 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
814 return 0;
815 }
816
Lennart Poettering1554afa2014-06-17 23:50:22 +0200817 file_type = i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR;
Lennart Poettering468d7262012-01-17 15:04:12 +0100818
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200819 RUN_WITH_UMASK(0000) {
820 label_context_set(i->path, file_type);
821 r = mknod(i->path, i->mode | file_type, i->major_minor);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200822 label_context_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200823 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100824
Kay Sievers6555ad82014-06-13 04:11:11 +0200825 if (r < 0) {
826 if (errno == EPERM) {
827 log_debug("We lack permissions, possibly because of cgroup configuration; "
828 "skipping creation of device node %s.", i->path);
829 return 0;
830 }
831
832 if (errno != EEXIST) {
833 log_error("Failed to create device node %s: %m", i->path);
834 return -errno;
835 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100836
Lennart Poettering1554afa2014-06-17 23:50:22 +0200837 if (stat(i->path, &st) < 0) {
838 log_error("stat(%s) failed: %m", i->path);
839 return -errno;
840 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100841
Lennart Poettering1554afa2014-06-17 23:50:22 +0200842 if ((st.st_mode & S_IFMT) != file_type) {
843
844 if (i->force) {
845
846 RUN_WITH_UMASK(0000) {
847 label_context_set(i->path, file_type);
848 r = mknod_atomic(i->path, i->mode | file_type, i->major_minor);
849 label_context_clear();
850 }
851
852 if (r < 0) {
853 log_error("Failed to create device node %s: %s", i->path, strerror(-r));
854 return r;
855 }
856 } else {
857 log_debug("%s is not a device node.", i->path);
858 return 0;
859 }
860 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100861 }
862
863 r = item_set_perms(i, i->path);
864 if (r < 0)
865 return r;
866
867 break;
868 }
869
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200870 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100871 case RELABEL_PATH:
872
873 r = glob_item(i, item_set_perms);
874 if (r < 0)
Lennart Poettering96ca8192013-06-21 15:57:42 +0200875 return r;
Michal Schmidt777b87e2011-12-16 18:27:35 +0100876 break;
877
Michal Schmidta8d88782011-12-15 23:11:07 +0100878 case RECURSIVE_RELABEL_PATH:
879
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200880 r = glob_item(i, item_set_perms_recursive);
Michal Schmidta8d88782011-12-15 23:11:07 +0100881 if (r < 0)
882 return r;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200883
884 break;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200885 }
886
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200887 log_debug("%s created successfully.", i->path);
888
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100889 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200890}
891
Michal Schmidta0896122011-12-15 21:32:50 +0100892static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200893 int r;
894
895 assert(i);
896
897 switch (i->type) {
898
899 case CREATE_FILE:
900 case TRUNCATE_FILE:
901 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200902 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100903 case CREATE_SYMLINK:
904 case CREATE_BLOCK_DEVICE:
905 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200906 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100907 case IGNORE_DIRECTORY_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200908 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100909 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100910 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100911 case WRITE_FILE:
Lennart Poettering849958d2014-06-10 23:02:40 +0200912 case COPY_FILES:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200913 break;
914
915 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100916 if (remove(instance) < 0 && errno != ENOENT) {
917 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200918 return -errno;
919 }
920
921 break;
922
923 case TRUNCATE_DIRECTORY:
924 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +0200925 /* FIXME: we probably should use dir_cleanup() here
926 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +0200927 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Lennart Poettering468d7262012-01-17 15:04:12 +0100928 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100929 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200930 return r;
931 }
932
933 break;
934 }
935
936 return 0;
937}
938
Michal Schmidta0896122011-12-15 21:32:50 +0100939static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100940 int r = 0;
941
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100942 assert(i);
943
944 switch (i->type) {
945
946 case CREATE_FILE:
947 case TRUNCATE_FILE:
948 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200949 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100950 case CREATE_SYMLINK:
951 case CREATE_CHAR_DEVICE:
952 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100953 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100954 case IGNORE_DIRECTORY_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200955 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100956 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100957 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100958 case WRITE_FILE:
Lennart Poettering849958d2014-06-10 23:02:40 +0200959 case COPY_FILES:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100960 break;
961
962 case REMOVE_PATH:
963 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100964 case RECURSIVE_REMOVE_PATH:
965 r = glob_item(i, remove_item_instance);
966 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100967 }
968
Michal Schmidt99e68c02011-12-15 23:45:26 +0100969 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100970}
971
Michal Sekletar78a92a52013-01-18 16:13:08 +0100972static int clean_item_instance(Item *i, const char* instance) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200973 _cleanup_closedir_ DIR *d = NULL;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100974 struct stat s, ps;
975 bool mountpoint;
976 int r;
977 usec_t cutoff, n;
978
979 assert(i);
980
981 if (!i->age_set)
982 return 0;
983
984 n = now(CLOCK_REALTIME);
985 if (n < i->age)
986 return 0;
987
988 cutoff = n - i->age;
989
990 d = opendir(instance);
991 if (!d) {
992 if (errno == ENOENT || errno == ENOTDIR)
993 return 0;
994
995 log_error("Failed to open directory %s: %m", i->path);
996 return -errno;
997 }
998
999 if (fstat(dirfd(d), &s) < 0) {
1000 log_error("stat(%s) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001001 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001002 }
1003
1004 if (!S_ISDIR(s.st_mode)) {
1005 log_error("%s is not a directory.", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001006 return -ENOTDIR;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001007 }
1008
1009 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
1010 log_error("stat(%s/..) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001011 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001012 }
1013
1014 mountpoint = s.st_dev != ps.st_dev ||
1015 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
1016
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001017 r = dir_cleanup(i, instance, d, &s, cutoff, s.st_dev, mountpoint,
1018 MAX_DEPTH, i->keep_first_level);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001019 return r;
1020}
1021
1022static int clean_item(Item *i) {
1023 int r = 0;
1024
1025 assert(i);
1026
1027 switch (i->type) {
1028 case CREATE_DIRECTORY:
1029 case TRUNCATE_DIRECTORY:
1030 case IGNORE_PATH:
Lennart Poettering849958d2014-06-10 23:02:40 +02001031 case COPY_FILES:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001032 clean_item_instance(i, i->path);
1033 break;
1034 case IGNORE_DIRECTORY_PATH:
1035 r = glob_item(i, clean_item_instance);
1036 break;
1037 default:
1038 break;
1039 }
1040
1041 return r;
1042}
1043
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001044static int process_item(Item *i) {
1045 int r, q, p;
Lennart Poettering1910cd02014-06-11 01:37:35 +02001046 char prefix[PATH_MAX];
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001047
1048 assert(i);
1049
Lennart Poettering1910cd02014-06-11 01:37:35 +02001050 if (i->done)
1051 return 0;
1052
1053 i->done = true;
1054
1055 PATH_FOREACH_PREFIX(prefix, i->path) {
1056 Item *j;
1057
1058 j = hashmap_get(items, prefix);
1059 if (j)
1060 process_item(j);
1061 }
1062
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001063 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +01001064 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001065 p = arg_clean ? clean_item(i) : 0;
1066
1067 if (r < 0)
1068 return r;
1069
1070 if (q < 0)
1071 return q;
1072
1073 return p;
1074}
1075
1076static void item_free(Item *i) {
Lennart Poettering753615e2014-06-12 23:07:17 +02001077
1078 if (!i)
1079 return;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001080
1081 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +01001082 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001083 free(i);
1084}
1085
Lennart Poettering14bf2c92013-10-14 04:59:26 +02001086DEFINE_TRIVIAL_CLEANUP_FUNC(Item*, item_free);
Maciej Wereskie2f2fb72013-07-19 15:43:12 +02001087
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001088static bool item_equal(Item *a, Item *b) {
1089 assert(a);
1090 assert(b);
1091
1092 if (!streq_ptr(a->path, b->path))
1093 return false;
1094
1095 if (a->type != b->type)
1096 return false;
1097
1098 if (a->uid_set != b->uid_set ||
1099 (a->uid_set && a->uid != b->uid))
1100 return false;
1101
1102 if (a->gid_set != b->gid_set ||
1103 (a->gid_set && a->gid != b->gid))
1104 return false;
1105
1106 if (a->mode_set != b->mode_set ||
1107 (a->mode_set && a->mode != b->mode))
1108 return false;
1109
1110 if (a->age_set != b->age_set ||
1111 (a->age_set && a->age != b->age))
1112 return false;
1113
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001114 if ((a->type == CREATE_FILE ||
1115 a->type == TRUNCATE_FILE ||
1116 a->type == WRITE_FILE ||
Lennart Poettering849958d2014-06-10 23:02:40 +02001117 a->type == CREATE_SYMLINK ||
1118 a->type == COPY_FILES) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +01001119 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +01001120 return false;
1121
1122 if ((a->type == CREATE_CHAR_DEVICE ||
1123 a->type == CREATE_BLOCK_DEVICE) &&
1124 a->major_minor != b->major_minor)
1125 return false;
1126
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001127 return true;
1128}
1129
Dave Reisnera2aced42013-07-24 11:10:05 -04001130static bool should_include_path(const char *path) {
1131 char **prefix;
1132
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001133 STRV_FOREACH(prefix, arg_exclude_prefixes)
Dave Reisner5c795112013-07-24 11:19:24 -04001134 if (path_startswith(path, *prefix))
1135 return false;
Dave Reisnera2aced42013-07-24 11:10:05 -04001136
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001137 STRV_FOREACH(prefix, arg_include_prefixes)
Dave Reisnera2aced42013-07-24 11:10:05 -04001138 if (path_startswith(path, *prefix))
1139 return true;
Dave Reisnera2aced42013-07-24 11:10:05 -04001140
Dave Reisner5c795112013-07-24 11:19:24 -04001141 /* no matches, so we should include this path only if we
1142 * have no whitelist at all */
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001143 return strv_length(arg_include_prefixes) == 0;
Dave Reisnera2aced42013-07-24 11:10:05 -04001144}
1145
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001146static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001147
1148 static const Specifier specifier_table[] = {
1149 { 'm', specifier_machine_id, NULL },
1150 { 'b', specifier_boot_id, NULL },
1151 { 'H', specifier_host_name, NULL },
1152 { 'v', specifier_kernel_release, NULL },
1153 {}
1154 };
1155
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001156 _cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
1157 _cleanup_(item_freep) Item *i = NULL;
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001158 Item *existing;
Michal Schmidt66ccd032011-12-15 21:31:14 +01001159 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001160 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001161 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +02001162
1163 assert(fname);
1164 assert(line >= 1);
1165 assert(buffer);
1166
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001167 r = sscanf(buffer,
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001168 "%ms %ms %ms %ms %ms %ms %n",
1169 &action,
Lennart Poettering1731e342013-09-17 11:02:02 -05001170 &path,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +01001171 &mode,
1172 &user,
1173 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +01001174 &age,
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001175 &n);
1176 if (r < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001177 log_error("[%s:%u] Syntax error.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001178 return -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +02001179 }
1180
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001181 if (isempty(action)) {
1182 log_error("[%s:%u] Command too short '%s'.", fname, line, action);
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001183 return -EINVAL;
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001184 }
1185
1186 if (strlen(action) > 1 && !in_charset(action+1, "!+")) {
1187 log_error("[%s:%u] Unknown modifiers in command '%s'", fname, line, action);
1188 return -EINVAL;
1189 }
1190
1191 if (strchr(action+1, '!') && !arg_boot)
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001192 return 0;
1193
1194 type = action[0];
1195
Lennart Poettering1731e342013-09-17 11:02:02 -05001196 i = new0(Item, 1);
1197 if (!i)
1198 return log_oom();
1199
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001200 i->force = !!strchr(action+1, '+');
1201
Lennart Poettering1731e342013-09-17 11:02:02 -05001202 r = specifier_printf(path, specifier_table, NULL, &i->path);
1203 if (r < 0) {
1204 log_error("[%s:%u] Failed to replace specifiers: %s", fname, line, path);
1205 return r;
1206 }
1207
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001208 if (n >= 0) {
1209 n += strspn(buffer+n, WHITESPACE);
1210 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
1211 i->argument = unquote(buffer+n, "\"");
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001212 if (!i->argument)
1213 return log_oom();
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001214 }
1215 }
1216
Lennart Poettering753615e2014-06-12 23:07:17 +02001217 switch (type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001218
Michal Schmidt777b87e2011-12-16 18:27:35 +01001219 case CREATE_FILE:
1220 case TRUNCATE_FILE:
1221 case CREATE_DIRECTORY:
1222 case TRUNCATE_DIRECTORY:
1223 case CREATE_FIFO:
1224 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001225 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001226 case REMOVE_PATH:
1227 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001228 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001229 case RELABEL_PATH:
1230 case RECURSIVE_RELABEL_PATH:
1231 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001232
1233 case CREATE_SYMLINK:
1234 if (!i->argument) {
1235 log_error("[%s:%u] Symlink file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001236 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001237 }
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001238
Lennart Poettering468d7262012-01-17 15:04:12 +01001239 break;
1240
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001241 case WRITE_FILE:
1242 if (!i->argument) {
1243 log_error("[%s:%u] Write file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001244 return -EBADMSG;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001245 }
1246 break;
1247
Lennart Poettering849958d2014-06-10 23:02:40 +02001248 case COPY_FILES:
1249 if (!i->argument) {
1250 log_error("[%s:%u] Copy files requires argument.", fname, line);
1251 return -EBADMSG;
1252 }
1253
1254 if (!path_is_absolute(i->argument)) {
1255 log_error("[%s:%u] Source path is not absolute.", fname, line);
1256 return -EBADMSG;
1257 }
1258
1259 path_kill_slashes(i->argument);
1260 break;
1261
Lennart Poettering468d7262012-01-17 15:04:12 +01001262 case CREATE_CHAR_DEVICE:
1263 case CREATE_BLOCK_DEVICE: {
1264 unsigned major, minor;
1265
1266 if (!i->argument) {
1267 log_error("[%s:%u] Device file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001268 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001269 }
1270
1271 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1272 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 -04001273 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001274 }
1275
1276 i->major_minor = makedev(major, minor);
1277 break;
1278 }
1279
Michal Schmidt777b87e2011-12-16 18:27:35 +01001280 default:
Lennart Poettering753615e2014-06-12 23:07:17 +02001281 log_error("[%s:%u] Unknown command type '%c'.", fname, line, type);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001282 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001283 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001284
Michal Schmidta8d88782011-12-15 23:11:07 +01001285 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001286
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001287 if (!path_is_absolute(i->path)) {
1288 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001289 return -EBADMSG;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001290 }
1291
1292 path_kill_slashes(i->path);
1293
Dave Reisnera2aced42013-07-24 11:10:05 -04001294 if (!should_include_path(i->path))
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001295 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001296
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001297 if (arg_root) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001298 char *p;
1299
1300 p = strappend(arg_root, i->path);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001301 if (!p)
1302 return log_oom();
1303
1304 free(i->path);
1305 i->path = p;
1306 }
1307
Lennart Poettering5008d582010-09-28 02:34:02 +02001308 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001309 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001310
Lennart Poetteringd05c5032012-07-16 12:34:54 +02001311 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001312 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001313 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001314 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001315 }
1316
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001317 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001318 }
1319
1320 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001321 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001322
Lennart Poettering4b678342011-07-23 01:17:59 +02001323 r = get_group_creds(&g, &i->gid);
1324 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001325 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001326 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001327 }
1328
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001329 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001330 }
1331
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001332 if (mode && !streq(mode, "-")) {
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001333 const char *mm = mode;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001334 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001335
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001336 if (*mm == '~') {
1337 i->mask_perms = true;
1338 mm++;
1339 }
1340
1341 if (sscanf(mm, "%o", &m) != 1) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001342 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001343 return -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001344 }
1345
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001346 i->mode = m;
1347 i->mode_set = true;
1348 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001349 i->mode =
1350 i->type == CREATE_DIRECTORY ||
1351 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001352
1353 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001354 const char *a = age;
1355
1356 if (*a == '~') {
1357 i->keep_first_level = true;
1358 a++;
1359 }
1360
Lennart Poettering7f602782013-04-02 20:38:16 +02001361 if (parse_sec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001362 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001363 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001364 }
1365
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001366 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001367 }
1368
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001369 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001370
Lennart Poettering468d7262012-01-17 15:04:12 +01001371 existing = hashmap_get(h, i->path);
1372 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001373
1374 /* Two identical items are fine */
1375 if (!item_equal(existing, i))
1376 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1377
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001378 return 0;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001379 }
1380
Lennart Poettering468d7262012-01-17 15:04:12 +01001381 r = hashmap_put(h, i->path, i);
1382 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001383 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001384 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001385 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001386
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001387 i = NULL; /* avoid cleanup */
Lennart Poettering5008d582010-09-28 02:34:02 +02001388
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001389 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001390}
1391
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001392static int help(void) {
1393
Lennart Poettering522d4a42011-02-13 15:08:15 +01001394 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1395 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001396 " -h --help Show this help\n"
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001397 " --version Show package version\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001398 " --create Create marked files/directories\n"
1399 " --clean Clean up marked directories\n"
1400 " --remove Remove marked files/directories\n"
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001401 " --boot Execute actions only safe at boot\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001402 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n"
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001403 " --exclude-prefix=PATH Ignore rules that apply to paths with the specified prefix\n"
1404 " --root=PATH Operate on an alternate filesystem root\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001405 program_invocation_short_name);
1406
1407 return 0;
1408}
1409
1410static int parse_argv(int argc, char *argv[]) {
1411
1412 enum {
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001413 ARG_VERSION = 0x100,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001414 ARG_CREATE,
1415 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001416 ARG_REMOVE,
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001417 ARG_BOOT,
Dave Reisner5c795112013-07-24 11:19:24 -04001418 ARG_PREFIX,
1419 ARG_EXCLUDE_PREFIX,
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001420 ARG_ROOT,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001421 };
1422
1423 static const struct option options[] = {
Dave Reisner5c795112013-07-24 11:19:24 -04001424 { "help", no_argument, NULL, 'h' },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001425 { "version", no_argument, NULL, ARG_VERSION },
Dave Reisner5c795112013-07-24 11:19:24 -04001426 { "create", no_argument, NULL, ARG_CREATE },
1427 { "clean", no_argument, NULL, ARG_CLEAN },
1428 { "remove", no_argument, NULL, ARG_REMOVE },
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001429 { "boot", no_argument, NULL, ARG_BOOT },
Dave Reisner5c795112013-07-24 11:19:24 -04001430 { "prefix", required_argument, NULL, ARG_PREFIX },
1431 { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX },
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001432 { "root", required_argument, NULL, ARG_ROOT },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001433 {}
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001434 };
1435
1436 int c;
1437
1438 assert(argc >= 0);
1439 assert(argv);
1440
1441 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1442
1443 switch (c) {
1444
1445 case 'h':
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001446 return help();
1447
1448 case ARG_VERSION:
1449 puts(PACKAGE_STRING);
1450 puts(SYSTEMD_FEATURES);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001451 return 0;
1452
1453 case ARG_CREATE:
1454 arg_create = true;
1455 break;
1456
1457 case ARG_CLEAN:
1458 arg_clean = true;
1459 break;
1460
1461 case ARG_REMOVE:
1462 arg_remove = true;
1463 break;
1464
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001465 case ARG_BOOT:
1466 arg_boot = true;
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001467 break;
1468
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001469 case ARG_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001470 if (strv_push(&arg_include_prefixes, optarg) < 0)
Dave Reisnera2aced42013-07-24 11:10:05 -04001471 return log_oom();
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001472 break;
1473
Dave Reisner5c795112013-07-24 11:19:24 -04001474 case ARG_EXCLUDE_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001475 if (strv_push(&arg_exclude_prefixes, optarg) < 0)
Dave Reisner5c795112013-07-24 11:19:24 -04001476 return log_oom();
1477 break;
1478
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001479 case ARG_ROOT:
Lennart Poettering753615e2014-06-12 23:07:17 +02001480 free(arg_root);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001481 arg_root = path_make_absolute_cwd(optarg);
1482 if (!arg_root)
1483 return log_oom();
Lennart Poettering753615e2014-06-12 23:07:17 +02001484
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001485 path_kill_slashes(arg_root);
1486 break;
1487
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001488 case '?':
1489 return -EINVAL;
1490
1491 default:
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001492 assert_not_reached("Unhandled option");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001493 }
1494 }
1495
1496 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001497 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001498 return -EINVAL;
1499 }
1500
1501 return 1;
1502}
1503
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001504static int read_config_file(const char *fn, bool ignore_enoent) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001505 _cleanup_fclose_ FILE *f = NULL;
1506 char line[LINE_MAX];
Michal Sekletar78a92a52013-01-18 16:13:08 +01001507 Iterator iterator;
Lennart Poettering1731e342013-09-17 11:02:02 -05001508 unsigned v = 0;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001509 Item *i;
Lennart Poettering1731e342013-09-17 11:02:02 -05001510 int r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001511
1512 assert(fn);
1513
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001514 r = search_and_fopen_nulstr(fn, "re", arg_root, conf_file_dirs, &f);
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001515 if (r < 0) {
1516 if (ignore_enoent && r == -ENOENT)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001517 return 0;
1518
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001519 log_error("Failed to open '%s', ignoring: %s", fn, strerror(-r));
1520 return r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001521 }
1522
Lennart Poettering1731e342013-09-17 11:02:02 -05001523 FOREACH_LINE(line, f, break) {
1524 char *l;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001525 int k;
1526
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001527 v++;
1528
1529 l = strstrip(line);
1530 if (*l == '#' || *l == 0)
1531 continue;
1532
Lennart Poettering1731e342013-09-17 11:02:02 -05001533 k = parse_line(fn, v, l);
1534 if (k < 0 && r == 0)
1535 r = k;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001536 }
1537
Michal Sekletar78a92a52013-01-18 16:13:08 +01001538 /* we have to determine age parameter for each entry of type X */
1539 HASHMAP_FOREACH(i, globs, iterator) {
1540 Iterator iter;
1541 Item *j, *candidate_item = NULL;
1542
1543 if (i->type != IGNORE_DIRECTORY_PATH)
1544 continue;
1545
1546 HASHMAP_FOREACH(j, items, iter) {
1547 if (j->type != CREATE_DIRECTORY && j->type != TRUNCATE_DIRECTORY)
1548 continue;
1549
1550 if (path_equal(j->path, i->path)) {
1551 candidate_item = j;
1552 break;
1553 }
1554
1555 if ((!candidate_item && path_startswith(i->path, j->path)) ||
1556 (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
1557 candidate_item = j;
1558 }
1559
1560 if (candidate_item) {
1561 i->age = candidate_item->age;
1562 i->age_set = true;
1563 }
1564 }
1565
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001566 if (ferror(f)) {
1567 log_error("Failed to read from file %s: %m", fn);
1568 if (r == 0)
1569 r = -EIO;
1570 }
1571
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001572 return r;
1573}
1574
Lennart Poettering5008d582010-09-28 02:34:02 +02001575int main(int argc, char *argv[]) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001576 int r, k;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001577 Item *i;
1578 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001579
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001580 r = parse_argv(argc, argv);
1581 if (r <= 0)
Lennart Poettering753615e2014-06-12 23:07:17 +02001582 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001583
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001584 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001585 log_parse_environment();
1586 log_open();
1587
Lennart Poettering4c126262011-08-01 20:52:18 +02001588 umask(0022);
1589
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001590 label_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001591
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001592 items = hashmap_new(string_hash_func, string_compare_func);
1593 globs = hashmap_new(string_hash_func, string_compare_func);
1594
1595 if (!items || !globs) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001596 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001597 goto finish;
1598 }
1599
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001600 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001601
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001602 if (optind < argc) {
1603 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001604
Dave Reisner91256702012-06-08 22:31:19 -04001605 for (j = optind; j < argc; j++) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001606 k = read_config_file(argv[j], false);
1607 if (k < 0 && r == 0)
1608 r = k;
Dave Reisner91256702012-06-08 22:31:19 -04001609 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001610
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001611 } else {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001612 _cleanup_strv_free_ char **files = NULL;
1613 char **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001614
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001615 r = conf_files_list_nulstr(&files, ".conf", arg_root, conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001616 if (r < 0) {
Kay Sievers44143302011-04-28 23:51:24 +02001617 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
1618 goto finish;
1619 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001620
Kay Sievers772f8372011-04-25 21:38:21 +02001621 STRV_FOREACH(f, files) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001622 k = read_config_file(*f, true);
1623 if (k < 0 && r == 0)
1624 r = k;
Lennart Poettering5008d582010-09-28 02:34:02 +02001625 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001626 }
1627
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001628 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001629 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001630
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001631 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001632 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001633
Lennart Poettering5008d582010-09-28 02:34:02 +02001634finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001635 while ((i = hashmap_steal_first(items)))
1636 item_free(i);
1637
Lennart Poettering17b90522011-02-14 21:55:06 +01001638 while ((i = hashmap_steal_first(globs)))
1639 item_free(i);
1640
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001641 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001642 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001643
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001644 free(arg_include_prefixes);
1645 free(arg_exclude_prefixes);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001646 free(arg_root);
Dave Reisnera2aced42013-07-24 11:10:05 -04001647
Lennart Poettering17b90522011-02-14 21:55:06 +01001648 set_free_free(unix_sockets);
1649
Lennart Poettering29003cf2010-10-19 19:36:45 +02001650 label_finish();
1651
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001652 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Lennart Poettering5008d582010-09-28 02:34:02 +02001653}