blob: bb12dd0b5a3a37cd3bd12917df4fbba72e752483 [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
106 bool done:1;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200107} Item;
108
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200109static bool arg_create = false;
110static bool arg_clean = false;
111static bool arg_remove = false;
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -0500112static bool arg_boot = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200113
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200114static char **arg_include_prefixes = NULL;
115static char **arg_exclude_prefixes = NULL;
Michael Marineaucf9a4ab2014-03-13 21:32:13 -0700116static char *arg_root = NULL;
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100117
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100118static const char conf_file_dirs[] =
119 "/etc/tmpfiles.d\0"
120 "/run/tmpfiles.d\0"
121 "/usr/local/lib/tmpfiles.d\0"
122 "/usr/lib/tmpfiles.d\0"
Lennart Poettering3f2afb22012-07-20 16:24:55 +0200123#ifdef HAVE_SPLIT_USR
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100124 "/lib/tmpfiles.d\0"
Lennart Poettering3f2afb22012-07-20 16:24:55 +0200125#endif
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100126 ;
Dave Reisner91256702012-06-08 22:31:19 -0400127
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200128#define MAX_DEPTH 256
129
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200130static Hashmap *items = NULL, *globs = NULL;
131static Set *unix_sockets = NULL;
132
Michal Schmidt66ccd032011-12-15 21:31:14 +0100133static bool needs_glob(ItemType t) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200134 return IN_SET(t,
135 WRITE_FILE,
136 IGNORE_PATH,
137 IGNORE_DIRECTORY_PATH,
138 REMOVE_PATH,
139 RECURSIVE_REMOVE_PATH,
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200140 ADJUST_MODE,
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200141 RELABEL_PATH,
142 RECURSIVE_RELABEL_PATH);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100143}
144
145static struct Item* find_glob(Hashmap *h, const char *match) {
146 Item *j;
147 Iterator i;
148
149 HASHMAP_FOREACH(j, h, i)
150 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
151 return j;
152
153 return NULL;
154}
155
Lennart Poettering17b90522011-02-14 21:55:06 +0100156static void load_unix_sockets(void) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200157 _cleanup_fclose_ FILE *f = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100158 char line[LINE_MAX];
159
160 if (unix_sockets)
161 return;
162
163 /* We maintain a cache of the sockets we found in
164 * /proc/net/unix to speed things up a little. */
165
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100166 unix_sockets = set_new(string_hash_func, string_compare_func);
167 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100168 return;
169
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100170 f = fopen("/proc/net/unix", "re");
171 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100172 return;
173
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100174 /* Skip header */
175 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100176 goto fail;
177
178 for (;;) {
179 char *p, *s;
180 int k;
181
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100182 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100183 break;
184
185 truncate_nl(line);
186
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100187 p = strchr(line, ':');
188 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100189 continue;
190
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100191 if (strlen(p) < 37)
192 continue;
193
194 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100195 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100196 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100197 p += strspn(p, WHITESPACE);
198
199 if (*p != '/')
200 continue;
201
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100202 s = strdup(p);
203 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100204 goto fail;
205
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100206 path_kill_slashes(s);
207
Zbigniew Jędrzejewski-Szmekef422022013-04-22 23:12:15 -0400208 k = set_consume(unix_sockets, s);
209 if (k < 0 && k != -EEXIST)
210 goto fail;
Lennart Poettering17b90522011-02-14 21:55:06 +0100211 }
212
213 return;
214
215fail:
216 set_free_free(unix_sockets);
217 unix_sockets = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100218}
219
220static bool unix_socket_alive(const char *fn) {
221 assert(fn);
222
223 load_unix_sockets();
224
225 if (unix_sockets)
226 return !!set_get(unix_sockets, (char*) fn);
227
228 /* We don't know, so assume yes */
229 return true;
230}
231
Kay Sievers99d680a2013-03-13 13:12:36 +0100232static int dir_is_mount_point(DIR *d, const char *subdir) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200233
234 union file_handle_union h = {
235 .handle.handle_bytes = MAX_HANDLE_SZ
236 };
237
Kay Sievers99d680a2013-03-13 13:12:36 +0100238 int mount_id_parent, mount_id;
239 int r_p, r;
240
Dave Reisner370c8602014-04-19 13:22:35 -0400241 r_p = name_to_handle_at(dirfd(d), ".", &h.handle, &mount_id_parent, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100242 if (r_p < 0)
243 r_p = -errno;
244
Dave Reisner370c8602014-04-19 13:22:35 -0400245 h.handle.handle_bytes = MAX_HANDLE_SZ;
246 r = name_to_handle_at(dirfd(d), subdir, &h.handle, &mount_id, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100247 if (r < 0)
248 r = -errno;
249
250 /* got no handle; make no assumptions, return error */
251 if (r_p < 0 && r < 0)
252 return r_p;
253
254 /* got both handles; if they differ, it is a mount point */
255 if (r_p >= 0 && r >= 0)
256 return mount_id_parent != mount_id;
257
258 /* got only one handle; assume different mount points if one
259 * of both queries was not supported by the filesystem */
260 if (r_p == -ENOSYS || r_p == -ENOTSUP || r == -ENOSYS || r == -ENOTSUP)
261 return true;
262
263 /* return error */
264 if (r_p < 0)
265 return r_p;
266 return r;
267}
268
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200269static int dir_cleanup(
Michal Sekletar78a92a52013-01-18 16:13:08 +0100270 Item *i,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200271 const char *p,
272 DIR *d,
273 const struct stat *ds,
274 usec_t cutoff,
275 dev_t rootdev,
276 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200277 int maxdepth,
Lennart Poettering265ffa12013-09-17 16:33:30 -0500278 bool keep_this_level) {
279
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200280 struct dirent *dent;
281 struct timespec times[2];
282 bool deleted = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200283 int r = 0;
284
285 while ((dent = readdir(d))) {
286 struct stat s;
287 usec_t age;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200288 _cleanup_free_ char *sub_path = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200289
290 if (streq(dent->d_name, ".") ||
291 streq(dent->d_name, ".."))
292 continue;
293
294 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
Kay Sieversca2f4172013-10-17 03:20:46 +0200295 if (errno == ENOENT)
296 continue;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200297
Kay Sieversca2f4172013-10-17 03:20:46 +0200298 /* FUSE, NFS mounts, SELinux might return EACCES */
299 if (errno == EACCES)
300 log_debug("stat(%s/%s) failed: %m", p, dent->d_name);
301 else
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200302 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
Kay Sieversca2f4172013-10-17 03:20:46 +0200303 r = -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200304 continue;
305 }
306
307 /* Stay on the same filesystem */
308 if (s.st_dev != rootdev)
309 continue;
310
Kay Sievers99d680a2013-03-13 13:12:36 +0100311 /* Try to detect bind mounts of the same filesystem instance; they
312 * do not differ in device major/minors. This type of query is not
313 * supported on all kernels or filesystem types though. */
314 if (S_ISDIR(s.st_mode) && dir_is_mount_point(d, dent->d_name) > 0)
315 continue;
316
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200317 /* Do not delete read-only files owned by root */
318 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
319 continue;
320
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200321 sub_path = strjoin(p, "/", dent->d_name, NULL);
322 if (!sub_path) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700323 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200324 goto finish;
325 }
326
327 /* Is there an item configured for this path? */
328 if (hashmap_get(items, sub_path))
329 continue;
330
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100331 if (find_glob(globs, sub_path))
332 continue;
333
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200334 if (S_ISDIR(s.st_mode)) {
335
336 if (mountpoint &&
337 streq(dent->d_name, "lost+found") &&
338 s.st_uid == 0)
339 continue;
340
341 if (maxdepth <= 0)
342 log_warning("Reached max depth on %s.", sub_path);
343 else {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200344 _cleanup_closedir_ DIR *sub_dir;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200345 int q;
346
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200347 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200348 if (!sub_dir) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200349 if (errno != ENOENT) {
350 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
351 r = -errno;
352 }
353
354 continue;
355 }
356
Michal Sekletar78a92a52013-01-18 16:13:08 +0100357 q = dir_cleanup(i, sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200358 if (q < 0)
359 r = q;
360 }
361
Lennart Poettering24f3a372012-06-20 09:05:50 +0200362 /* Note: if you are wondering why we don't
363 * support the sticky bit for excluding
364 * directories from cleaning like we do it for
365 * other file system objects: well, the sticky
366 * bit already has a meaning for directories,
367 * so we don't want to overload that. */
368
369 if (keep_this_level)
370 continue;
371
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200372 /* Ignore ctime, we change it when deleting */
373 age = MAX(timespec_load(&s.st_mtim),
374 timespec_load(&s.st_atim));
375 if (age >= cutoff)
376 continue;
377
Lukas Nykryna6187d42013-03-01 18:29:59 +0100378 if (i->type != IGNORE_DIRECTORY_PATH || !streq(dent->d_name, p)) {
Lennart Poettering9f6445e32013-12-24 16:39:37 +0100379 log_debug("rmdir '%s'", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200380
Michal Sekletar78a92a52013-01-18 16:13:08 +0100381 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
382 if (errno != ENOENT && errno != ENOTEMPTY) {
383 log_error("rmdir(%s): %m", sub_path);
384 r = -errno;
385 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200386 }
387 }
388
389 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100390 /* Skip files for which the sticky bit is
391 * set. These are semantics we define, and are
392 * unknown elsewhere. See XDG_RUNTIME_DIR
393 * specification for details. */
394 if (s.st_mode & S_ISVTX)
395 continue;
396
Lennart Poettering17b90522011-02-14 21:55:06 +0100397 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200398 if (streq(dent->d_name, ".journal") &&
399 s.st_uid == 0)
400 continue;
401
402 if (streq(dent->d_name, "aquota.user") ||
403 streq(dent->d_name, "aquota.group"))
404 continue;
405 }
406
Lennart Poettering17b90522011-02-14 21:55:06 +0100407 /* Ignore sockets that are listed in /proc/net/unix */
408 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
409 continue;
410
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100411 /* Ignore device nodes */
412 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
413 continue;
414
Lennart Poettering24f3a372012-06-20 09:05:50 +0200415 /* Keep files on this level around if this is
416 * requested */
417 if (keep_this_level)
418 continue;
419
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200420 age = MAX3(timespec_load(&s.st_mtim),
421 timespec_load(&s.st_atim),
422 timespec_load(&s.st_ctim));
423
424 if (age >= cutoff)
425 continue;
426
Lennart Poettering9f6445e32013-12-24 16:39:37 +0100427 log_debug("unlink '%s'", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200428
429 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
430 if (errno != ENOENT) {
431 log_error("unlink(%s): %m", sub_path);
432 r = -errno;
433 }
434 }
435
436 deleted = true;
437 }
438 }
439
440finish:
441 if (deleted) {
442 /* Restore original directory timestamps */
443 times[0] = ds->st_atim;
444 times[1] = ds->st_mtim;
445
446 if (futimens(dirfd(d), times) < 0)
447 log_error("utimensat(%s): %m", p);
448 }
449
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200450 return r;
451}
452
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200453static int item_set_perms(Item *i, const char *path) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200454 assert(i);
455 assert(path);
456
Michal Schmidt062e01b2011-12-16 18:00:11 +0100457 /* not using i->path directly because it may be a glob */
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200458 if (i->mode_set) {
459 mode_t m = i->mode;
460
461 if (i->mask_perms) {
462 struct stat st;
463
464 if (stat(path, &st) >= 0) {
465 if (!(st.st_mode & 0111))
466 m &= ~0111;
467 if (!(st.st_mode & 0222))
468 m &= ~0222;
469 if (!(st.st_mode & 0444))
470 m &= ~0444;
471 if (!S_ISDIR(st.st_mode))
472 m &= ~07000; /* remove sticky/sgid/suid bit, unless directory */
473 }
474 }
475
476 if (chmod(path, m) < 0) {
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200477 log_error("chmod(%s) failed: %m", path);
478 return -errno;
Michal Schmidt062e01b2011-12-16 18:00:11 +0100479 }
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200480 }
Michal Schmidt062e01b2011-12-16 18:00:11 +0100481
482 if (i->uid_set || i->gid_set)
483 if (chown(path,
484 i->uid_set ? i->uid : (uid_t) -1,
485 i->gid_set ? i->gid : (gid_t) -1) < 0) {
486
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200487 log_error("chown(%s) failed: %m", path);
488 return -errno;
Michal Schmidt062e01b2011-12-16 18:00:11 +0100489 }
490
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200491 return label_fix(path, false, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100492}
493
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400494static int write_one_file(Item *i, const char *path) {
Lennart Poettering874f1942014-06-10 22:48:56 +0200495 int flags;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200496 int fd = -1;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400497 struct stat st;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200498 int r = 0;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400499
Lennart Poettering874f1942014-06-10 22:48:56 +0200500 assert(i);
501 assert(path);
502
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400503 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
504 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
505
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200506 RUN_WITH_UMASK(0) {
507 label_context_set(path, S_IFREG);
508 fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200509 label_context_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200510 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400511
512 if (fd < 0) {
513 if (i->type == WRITE_FILE && errno == ENOENT)
514 return 0;
515
516 log_error("Failed to create file %s: %m", path);
517 return -errno;
518 }
519
520 if (i->argument) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200521 _cleanup_free_ char *unescaped;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400522 ssize_t n;
523 size_t l;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400524
Dave Reisner54693d92012-09-15 12:58:49 -0400525 unescaped = cunescape(i->argument);
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100526 if (unescaped == NULL) {
Lennart Poettering03e334a2014-03-18 19:22:43 +0100527 safe_close(fd);
Dave Reisner54693d92012-09-15 12:58:49 -0400528 return log_oom();
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100529 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400530
Dave Reisner54693d92012-09-15 12:58:49 -0400531 l = strlen(unescaped);
532 n = write(fd, unescaped, l);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400533
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400534 if (n < 0 || (size_t) n < l) {
535 log_error("Failed to write file %s: %s", path, n < 0 ? strerror(-n) : "Short write");
Lennart Poettering03e334a2014-03-18 19:22:43 +0100536 safe_close(fd);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400537 return n < 0 ? n : -EIO;
538 }
539 }
540
Lennart Poettering03e334a2014-03-18 19:22:43 +0100541 safe_close(fd);
Dave Reisner3612fbc2012-09-12 16:21:00 -0400542
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400543 if (stat(path, &st) < 0) {
544 log_error("stat(%s) failed: %m", path);
545 return -errno;
546 }
547
548 if (!S_ISREG(st.st_mode)) {
549 log_error("%s is not a file.", path);
550 return -EEXIST;
551 }
552
553 r = item_set_perms(i, path);
554 if (r < 0)
555 return r;
556
557 return 0;
558}
559
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200560static int item_set_perms_children(Item *i, const char *path) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200561 _cleanup_closedir_ DIR *d;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200562 int r = 0;
563
564 assert(i);
565 assert(path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100566
567 /* This returns the first error we run into, but nevertheless
568 * tries to go on */
569
570 d = opendir(path);
571 if (!d)
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200572 return errno == ENOENT || errno == ENOTDIR ? 0 : -errno;
Michal Schmidta8d88782011-12-15 23:11:07 +0100573
574 for (;;) {
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200575 _cleanup_free_ char *p = NULL;
Lennart Poettering7d5e9c02012-09-19 22:21:09 +0200576 struct dirent *de;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200577 int q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100578
Florian Weimerd78096b2013-12-19 12:26:07 +0100579 errno = 0;
580 de = readdir(d);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200581 if (!de) {
582 if (errno != 0 && r == 0)
583 r = -errno;
584
Michal Schmidta8d88782011-12-15 23:11:07 +0100585 break;
586 }
587
Michal Schmidta8d88782011-12-15 23:11:07 +0100588 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
589 continue;
590
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200591 p = strjoin(path, "/", de->d_name, NULL);
592 if (!p)
593 return -ENOMEM;
Michal Schmidta8d88782011-12-15 23:11:07 +0100594
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200595 q = item_set_perms(i, p);
596 if (q < 0 && q != -ENOENT && r == 0)
597 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100598
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200599 if (IN_SET(de->d_type, DT_UNKNOWN, DT_DIR)) {
600 q = item_set_perms_children(i, p);
601 if (q < 0 && r == 0)
602 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100603 }
Michal Schmidta8d88782011-12-15 23:11:07 +0100604 }
605
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200606 return r;
Michal Schmidta8d88782011-12-15 23:11:07 +0100607}
608
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200609static int item_set_perms_recursive(Item *i, const char *path) {
610 int r, q;
611
612 assert(i);
613 assert(path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100614
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
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200619 q = item_set_perms_children(i, path);
620 if (q < 0 && r == 0)
621 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100622
623 return r;
624}
625
Michal Schmidt99e68c02011-12-15 23:45:26 +0100626static int glob_item(Item *i, int (*action)(Item *, const char *)) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200627 _cleanup_globfree_ glob_t g = {};
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200628 int r = 0, k;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100629 char **fn;
630
Michal Schmidt99e68c02011-12-15 23:45:26 +0100631 errno = 0;
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400632 k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200633 if (k != 0 && k != GLOB_NOMATCH) {
634 if (errno == 0)
635 errno = EIO;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100636
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200637 log_error("glob(%s) failed: %m", i->path);
638 return -errno;
639 }
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400640
641 STRV_FOREACH(fn, g.gl_pathv) {
642 k = action(i, *fn);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200643 if (k < 0 && r == 0)
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400644 r = k;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100645 }
646
Michal Schmidt99e68c02011-12-15 23:45:26 +0100647 return r;
648}
649
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200650static int create_item(Item *i) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200651 struct stat st;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200652 int r = 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200653
654 assert(i);
655
656 switch (i->type) {
657
658 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100659 case IGNORE_DIRECTORY_PATH:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200660 case REMOVE_PATH:
661 case RECURSIVE_REMOVE_PATH:
662 return 0;
663
664 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100665 case TRUNCATE_FILE:
Dave Reisner1845fdd2012-09-27 20:48:13 -0400666 r = write_one_file(i, i->path);
667 if (r < 0)
668 return r;
669 break;
Lennart Poettering265ffa12013-09-17 16:33:30 -0500670
Lennart Poettering849958d2014-06-10 23:02:40 +0200671 case COPY_FILES:
672 r = copy_tree(i->argument, i->path);
673 if (r < 0) {
674 log_error("Failed to copy files: %s", strerror(-r));
675 return r;
676 }
677
678 r = item_set_perms(i, i->path);
679 if (r < 0)
680 return r;
681
682 break;
683
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400684 case WRITE_FILE:
685 r = glob_item(i, write_one_file);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100686 if (r < 0)
687 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200688
689 break;
690
691 case TRUNCATE_DIRECTORY:
692 case CREATE_DIRECTORY:
693
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200694 RUN_WITH_UMASK(0000) {
695 mkdir_parents_label(i->path, 0755);
696 r = mkdir(i->path, i->mode);
697 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200698
699 if (r < 0 && errno != EEXIST) {
700 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100701 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200702 }
703
704 if (stat(i->path, &st) < 0) {
705 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100706 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200707 }
708
709 if (!S_ISDIR(st.st_mode)) {
710 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100711 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200712 }
713
Michal Schmidt062e01b2011-12-16 18:00:11 +0100714 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100715 if (r < 0)
716 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200717
718 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200719
720 case CREATE_FIFO:
721
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200722 RUN_WITH_UMASK(0000) {
723 r = mkfifo(i->path, i->mode);
724 }
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200725
726 if (r < 0 && errno != EEXIST) {
727 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100728 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200729 }
730
731 if (stat(i->path, &st) < 0) {
732 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100733 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200734 }
735
736 if (!S_ISFIFO(st.st_mode)) {
737 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100738 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200739 }
740
Michal Schmidt062e01b2011-12-16 18:00:11 +0100741 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100742 if (r < 0)
743 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200744
745 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100746
Lennart Poettering468d7262012-01-17 15:04:12 +0100747 case CREATE_SYMLINK: {
Lennart Poetteringe26da2d2014-02-19 17:52:41 +0100748 _cleanup_free_ char *x = NULL;
Lennart Poettering468d7262012-01-17 15:04:12 +0100749
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200750 label_context_set(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100751 r = symlink(i->argument, i->path);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200752 label_context_clear();
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200753
Lennart Poettering468d7262012-01-17 15:04:12 +0100754 if (r < 0 && errno != EEXIST) {
755 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
756 return -errno;
757 }
758
759 r = readlink_malloc(i->path, &x);
760 if (r < 0) {
761 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
762 return -errno;
763 }
764
765 if (!streq(i->argument, x)) {
Lennart Poettering9339db72014-06-11 00:12:21 +0200766 log_error("%s is not the right symlink.", i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100767 return -EEXIST;
768 }
769
Lennart Poettering468d7262012-01-17 15:04:12 +0100770 break;
771 }
772
773 case CREATE_BLOCK_DEVICE:
774 case CREATE_CHAR_DEVICE: {
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700775 mode_t file_type;
776
777 if (have_effective_cap(CAP_MKNOD) == 0) {
778 /* In a container we lack CAP_MKNOD. We
Anatol Pomozovab06eef2013-04-14 19:37:54 -0700779 shouldn't attempt to create the device node in
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700780 that case to avoid noise, and we don't support
781 virtualized devices in containers anyway. */
782
783 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
784 return 0;
785 }
786
787 file_type = (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +0100788
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200789 RUN_WITH_UMASK(0000) {
790 label_context_set(i->path, file_type);
791 r = mknod(i->path, i->mode | file_type, i->major_minor);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200792 label_context_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200793 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100794
795 if (r < 0 && errno != EEXIST) {
796 log_error("Failed to create device node %s: %m", i->path);
797 return -errno;
798 }
799
800 if (stat(i->path, &st) < 0) {
801 log_error("stat(%s) failed: %m", i->path);
802 return -errno;
803 }
804
Michal Schmidte7aee752012-06-14 16:01:19 +0200805 if ((st.st_mode & S_IFMT) != file_type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100806 log_error("%s is not a device node.", i->path);
807 return -EEXIST;
808 }
809
810 r = item_set_perms(i, i->path);
811 if (r < 0)
812 return r;
813
814 break;
815 }
816
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200817 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100818 case RELABEL_PATH:
819
820 r = glob_item(i, item_set_perms);
821 if (r < 0)
Lennart Poettering96ca8192013-06-21 15:57:42 +0200822 return r;
Michal Schmidt777b87e2011-12-16 18:27:35 +0100823 break;
824
Michal Schmidta8d88782011-12-15 23:11:07 +0100825 case RECURSIVE_RELABEL_PATH:
826
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200827 r = glob_item(i, item_set_perms_recursive);
Michal Schmidta8d88782011-12-15 23:11:07 +0100828 if (r < 0)
829 return r;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200830
831 break;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200832 }
833
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200834 log_debug("%s created successfully.", i->path);
835
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100836 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200837}
838
Michal Schmidta0896122011-12-15 21:32:50 +0100839static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200840 int r;
841
842 assert(i);
843
844 switch (i->type) {
845
846 case CREATE_FILE:
847 case TRUNCATE_FILE:
848 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200849 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100850 case CREATE_SYMLINK:
851 case CREATE_BLOCK_DEVICE:
852 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200853 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100854 case IGNORE_DIRECTORY_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200855 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100856 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100857 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100858 case WRITE_FILE:
Lennart Poettering849958d2014-06-10 23:02:40 +0200859 case COPY_FILES:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200860 break;
861
862 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100863 if (remove(instance) < 0 && errno != ENOENT) {
864 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200865 return -errno;
866 }
867
868 break;
869
870 case TRUNCATE_DIRECTORY:
871 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +0200872 /* FIXME: we probably should use dir_cleanup() here
873 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +0200874 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Lennart Poettering468d7262012-01-17 15:04:12 +0100875 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100876 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200877 return r;
878 }
879
880 break;
881 }
882
883 return 0;
884}
885
Michal Schmidta0896122011-12-15 21:32:50 +0100886static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100887 int r = 0;
888
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100889 assert(i);
890
891 switch (i->type) {
892
893 case CREATE_FILE:
894 case TRUNCATE_FILE:
895 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200896 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100897 case CREATE_SYMLINK:
898 case CREATE_CHAR_DEVICE:
899 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100900 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100901 case IGNORE_DIRECTORY_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200902 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100903 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100904 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100905 case WRITE_FILE:
Lennart Poettering849958d2014-06-10 23:02:40 +0200906 case COPY_FILES:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100907 break;
908
909 case REMOVE_PATH:
910 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100911 case RECURSIVE_REMOVE_PATH:
912 r = glob_item(i, remove_item_instance);
913 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100914 }
915
Michal Schmidt99e68c02011-12-15 23:45:26 +0100916 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100917}
918
Michal Sekletar78a92a52013-01-18 16:13:08 +0100919static int clean_item_instance(Item *i, const char* instance) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200920 _cleanup_closedir_ DIR *d = NULL;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100921 struct stat s, ps;
922 bool mountpoint;
923 int r;
924 usec_t cutoff, n;
925
926 assert(i);
927
928 if (!i->age_set)
929 return 0;
930
931 n = now(CLOCK_REALTIME);
932 if (n < i->age)
933 return 0;
934
935 cutoff = n - i->age;
936
937 d = opendir(instance);
938 if (!d) {
939 if (errno == ENOENT || errno == ENOTDIR)
940 return 0;
941
942 log_error("Failed to open directory %s: %m", i->path);
943 return -errno;
944 }
945
946 if (fstat(dirfd(d), &s) < 0) {
947 log_error("stat(%s) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500948 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100949 }
950
951 if (!S_ISDIR(s.st_mode)) {
952 log_error("%s is not a directory.", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500953 return -ENOTDIR;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100954 }
955
956 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
957 log_error("stat(%s/..) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500958 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100959 }
960
961 mountpoint = s.st_dev != ps.st_dev ||
962 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
963
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500964 r = dir_cleanup(i, instance, d, &s, cutoff, s.st_dev, mountpoint,
965 MAX_DEPTH, i->keep_first_level);
Michal Sekletar78a92a52013-01-18 16:13:08 +0100966 return r;
967}
968
969static int clean_item(Item *i) {
970 int r = 0;
971
972 assert(i);
973
974 switch (i->type) {
975 case CREATE_DIRECTORY:
976 case TRUNCATE_DIRECTORY:
977 case IGNORE_PATH:
Lennart Poettering849958d2014-06-10 23:02:40 +0200978 case COPY_FILES:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100979 clean_item_instance(i, i->path);
980 break;
981 case IGNORE_DIRECTORY_PATH:
982 r = glob_item(i, clean_item_instance);
983 break;
984 default:
985 break;
986 }
987
988 return r;
989}
990
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200991static int process_item(Item *i) {
992 int r, q, p;
Lennart Poettering1910cd02014-06-11 01:37:35 +0200993 char prefix[PATH_MAX];
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200994
995 assert(i);
996
Lennart Poettering1910cd02014-06-11 01:37:35 +0200997 if (i->done)
998 return 0;
999
1000 i->done = true;
1001
1002 PATH_FOREACH_PREFIX(prefix, i->path) {
1003 Item *j;
1004
1005 j = hashmap_get(items, prefix);
1006 if (j)
1007 process_item(j);
1008 }
1009
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001010 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +01001011 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001012 p = arg_clean ? clean_item(i) : 0;
1013
1014 if (r < 0)
1015 return r;
1016
1017 if (q < 0)
1018 return q;
1019
1020 return p;
1021}
1022
1023static void item_free(Item *i) {
1024 assert(i);
1025
1026 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +01001027 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001028 free(i);
1029}
1030
Lennart Poettering14bf2c92013-10-14 04:59:26 +02001031DEFINE_TRIVIAL_CLEANUP_FUNC(Item*, item_free);
Maciej Wereskie2f2fb72013-07-19 15:43:12 +02001032
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001033static bool item_equal(Item *a, Item *b) {
1034 assert(a);
1035 assert(b);
1036
1037 if (!streq_ptr(a->path, b->path))
1038 return false;
1039
1040 if (a->type != b->type)
1041 return false;
1042
1043 if (a->uid_set != b->uid_set ||
1044 (a->uid_set && a->uid != b->uid))
1045 return false;
1046
1047 if (a->gid_set != b->gid_set ||
1048 (a->gid_set && a->gid != b->gid))
1049 return false;
1050
1051 if (a->mode_set != b->mode_set ||
1052 (a->mode_set && a->mode != b->mode))
1053 return false;
1054
1055 if (a->age_set != b->age_set ||
1056 (a->age_set && a->age != b->age))
1057 return false;
1058
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001059 if ((a->type == CREATE_FILE ||
1060 a->type == TRUNCATE_FILE ||
1061 a->type == WRITE_FILE ||
Lennart Poettering849958d2014-06-10 23:02:40 +02001062 a->type == CREATE_SYMLINK ||
1063 a->type == COPY_FILES) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +01001064 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +01001065 return false;
1066
1067 if ((a->type == CREATE_CHAR_DEVICE ||
1068 a->type == CREATE_BLOCK_DEVICE) &&
1069 a->major_minor != b->major_minor)
1070 return false;
1071
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001072 return true;
1073}
1074
Dave Reisnera2aced42013-07-24 11:10:05 -04001075static bool should_include_path(const char *path) {
1076 char **prefix;
1077
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001078 STRV_FOREACH(prefix, arg_exclude_prefixes)
Dave Reisner5c795112013-07-24 11:19:24 -04001079 if (path_startswith(path, *prefix))
1080 return false;
Dave Reisnera2aced42013-07-24 11:10:05 -04001081
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001082 STRV_FOREACH(prefix, arg_include_prefixes)
Dave Reisnera2aced42013-07-24 11:10:05 -04001083 if (path_startswith(path, *prefix))
1084 return true;
Dave Reisnera2aced42013-07-24 11:10:05 -04001085
Dave Reisner5c795112013-07-24 11:19:24 -04001086 /* no matches, so we should include this path only if we
1087 * have no whitelist at all */
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001088 return strv_length(arg_include_prefixes) == 0;
Dave Reisnera2aced42013-07-24 11:10:05 -04001089}
1090
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001091static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001092
1093 static const Specifier specifier_table[] = {
1094 { 'm', specifier_machine_id, NULL },
1095 { 'b', specifier_boot_id, NULL },
1096 { 'H', specifier_host_name, NULL },
1097 { 'v', specifier_kernel_release, NULL },
1098 {}
1099 };
1100
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001101 _cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
1102 _cleanup_(item_freep) Item *i = NULL;
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001103 Item *existing;
Michal Schmidt66ccd032011-12-15 21:31:14 +01001104 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001105 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001106 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +02001107
1108 assert(fname);
1109 assert(line >= 1);
1110 assert(buffer);
1111
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001112 r = sscanf(buffer,
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001113 "%ms %ms %ms %ms %ms %ms %n",
1114 &action,
Lennart Poettering1731e342013-09-17 11:02:02 -05001115 &path,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +01001116 &mode,
1117 &user,
1118 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +01001119 &age,
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001120 &n);
1121 if (r < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001122 log_error("[%s:%u] Syntax error.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001123 return -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +02001124 }
1125
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001126 if (strlen(action) > 2 || (strlen(action) > 1 && action[1] != '!')) {
1127 log_error("[%s:%u] Unknown modifier '%s'", fname, line, action);
1128 return -EINVAL;
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001129 } else if (strlen(action) > 1 && !arg_boot)
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001130 return 0;
1131
1132 type = action[0];
1133
Lennart Poettering1731e342013-09-17 11:02:02 -05001134 i = new0(Item, 1);
1135 if (!i)
1136 return log_oom();
1137
1138 r = specifier_printf(path, specifier_table, NULL, &i->path);
1139 if (r < 0) {
1140 log_error("[%s:%u] Failed to replace specifiers: %s", fname, line, path);
1141 return r;
1142 }
1143
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001144 if (n >= 0) {
1145 n += strspn(buffer+n, WHITESPACE);
1146 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
1147 i->argument = unquote(buffer+n, "\"");
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001148 if (!i->argument)
1149 return log_oom();
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001150 }
1151 }
1152
Michal Schmidt777b87e2011-12-16 18:27:35 +01001153 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001154
Michal Schmidt777b87e2011-12-16 18:27:35 +01001155 case CREATE_FILE:
1156 case TRUNCATE_FILE:
1157 case CREATE_DIRECTORY:
1158 case TRUNCATE_DIRECTORY:
1159 case CREATE_FIFO:
1160 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001161 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001162 case REMOVE_PATH:
1163 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001164 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001165 case RELABEL_PATH:
1166 case RECURSIVE_RELABEL_PATH:
1167 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001168
1169 case CREATE_SYMLINK:
1170 if (!i->argument) {
1171 log_error("[%s:%u] Symlink file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001172 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001173 }
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001174
Lennart Poettering468d7262012-01-17 15:04:12 +01001175 break;
1176
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001177 case WRITE_FILE:
1178 if (!i->argument) {
1179 log_error("[%s:%u] Write file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001180 return -EBADMSG;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001181 }
1182 break;
1183
Lennart Poettering849958d2014-06-10 23:02:40 +02001184 case COPY_FILES:
1185 if (!i->argument) {
1186 log_error("[%s:%u] Copy files requires argument.", fname, line);
1187 return -EBADMSG;
1188 }
1189
1190 if (!path_is_absolute(i->argument)) {
1191 log_error("[%s:%u] Source path is not absolute.", fname, line);
1192 return -EBADMSG;
1193 }
1194
1195 path_kill_slashes(i->argument);
1196 break;
1197
Lennart Poettering468d7262012-01-17 15:04:12 +01001198 case CREATE_CHAR_DEVICE:
1199 case CREATE_BLOCK_DEVICE: {
1200 unsigned major, minor;
1201
1202 if (!i->argument) {
1203 log_error("[%s:%u] Device file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001204 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001205 }
1206
1207 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1208 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 -04001209 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001210 }
1211
1212 i->major_minor = makedev(major, minor);
1213 break;
1214 }
1215
Michal Schmidt777b87e2011-12-16 18:27:35 +01001216 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001217 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001218 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001219 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001220
Michal Schmidta8d88782011-12-15 23:11:07 +01001221 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001222
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001223 if (!path_is_absolute(i->path)) {
1224 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001225 return -EBADMSG;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001226 }
1227
1228 path_kill_slashes(i->path);
1229
Dave Reisnera2aced42013-07-24 11:10:05 -04001230 if (!should_include_path(i->path))
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001231 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001232
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001233 if (arg_root) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001234 char *p;
1235
1236 p = strappend(arg_root, i->path);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001237 if (!p)
1238 return log_oom();
1239
1240 free(i->path);
1241 i->path = p;
1242 }
1243
Lennart Poettering5008d582010-09-28 02:34:02 +02001244 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001245 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001246
Lennart Poetteringd05c5032012-07-16 12:34:54 +02001247 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001248 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001249 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001250 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001251 }
1252
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001253 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001254 }
1255
1256 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001257 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001258
Lennart Poettering4b678342011-07-23 01:17:59 +02001259 r = get_group_creds(&g, &i->gid);
1260 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001261 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001262 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001263 }
1264
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001265 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001266 }
1267
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001268 if (mode && !streq(mode, "-")) {
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001269 const char *mm = mode;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001270 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001271
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001272 if (*mm == '~') {
1273 i->mask_perms = true;
1274 mm++;
1275 }
1276
1277 if (sscanf(mm, "%o", &m) != 1) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001278 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001279 return -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001280 }
1281
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001282 i->mode = m;
1283 i->mode_set = true;
1284 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001285 i->mode =
1286 i->type == CREATE_DIRECTORY ||
1287 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001288
1289 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001290 const char *a = age;
1291
1292 if (*a == '~') {
1293 i->keep_first_level = true;
1294 a++;
1295 }
1296
Lennart Poettering7f602782013-04-02 20:38:16 +02001297 if (parse_sec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001298 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001299 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001300 }
1301
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001302 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001303 }
1304
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001305 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001306
Lennart Poettering468d7262012-01-17 15:04:12 +01001307 existing = hashmap_get(h, i->path);
1308 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001309
1310 /* Two identical items are fine */
1311 if (!item_equal(existing, i))
1312 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1313
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001314 return 0;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001315 }
1316
Lennart Poettering468d7262012-01-17 15:04:12 +01001317 r = hashmap_put(h, i->path, i);
1318 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001319 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001320 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001321 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001322
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001323 i = NULL; /* avoid cleanup */
Lennart Poettering5008d582010-09-28 02:34:02 +02001324
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001325 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001326}
1327
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001328static int help(void) {
1329
Lennart Poettering522d4a42011-02-13 15:08:15 +01001330 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1331 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001332 " -h --help Show this help\n"
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001333 " --version Show package version\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001334 " --create Create marked files/directories\n"
1335 " --clean Clean up marked directories\n"
1336 " --remove Remove marked files/directories\n"
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001337 " --boot Execute actions only safe at boot\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001338 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n"
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001339 " --exclude-prefix=PATH Ignore rules that apply to paths with the specified prefix\n"
1340 " --root=PATH Operate on an alternate filesystem root\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001341 program_invocation_short_name);
1342
1343 return 0;
1344}
1345
1346static int parse_argv(int argc, char *argv[]) {
1347
1348 enum {
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001349 ARG_VERSION = 0x100,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001350 ARG_CREATE,
1351 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001352 ARG_REMOVE,
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001353 ARG_BOOT,
Dave Reisner5c795112013-07-24 11:19:24 -04001354 ARG_PREFIX,
1355 ARG_EXCLUDE_PREFIX,
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001356 ARG_ROOT,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001357 };
1358
1359 static const struct option options[] = {
Dave Reisner5c795112013-07-24 11:19:24 -04001360 { "help", no_argument, NULL, 'h' },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001361 { "version", no_argument, NULL, ARG_VERSION },
Dave Reisner5c795112013-07-24 11:19:24 -04001362 { "create", no_argument, NULL, ARG_CREATE },
1363 { "clean", no_argument, NULL, ARG_CLEAN },
1364 { "remove", no_argument, NULL, ARG_REMOVE },
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001365 { "boot", no_argument, NULL, ARG_BOOT },
Dave Reisner5c795112013-07-24 11:19:24 -04001366 { "prefix", required_argument, NULL, ARG_PREFIX },
1367 { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX },
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001368 { "root", required_argument, NULL, ARG_ROOT },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001369 {}
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001370 };
1371
1372 int c;
1373
1374 assert(argc >= 0);
1375 assert(argv);
1376
1377 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1378
1379 switch (c) {
1380
1381 case 'h':
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001382 return help();
1383
1384 case ARG_VERSION:
1385 puts(PACKAGE_STRING);
1386 puts(SYSTEMD_FEATURES);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001387 return 0;
1388
1389 case ARG_CREATE:
1390 arg_create = true;
1391 break;
1392
1393 case ARG_CLEAN:
1394 arg_clean = true;
1395 break;
1396
1397 case ARG_REMOVE:
1398 arg_remove = true;
1399 break;
1400
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001401 case ARG_BOOT:
1402 arg_boot = true;
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001403 break;
1404
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001405 case ARG_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001406 if (strv_push(&arg_include_prefixes, optarg) < 0)
Dave Reisnera2aced42013-07-24 11:10:05 -04001407 return log_oom();
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001408 break;
1409
Dave Reisner5c795112013-07-24 11:19:24 -04001410 case ARG_EXCLUDE_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001411 if (strv_push(&arg_exclude_prefixes, optarg) < 0)
Dave Reisner5c795112013-07-24 11:19:24 -04001412 return log_oom();
1413 break;
1414
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001415 case ARG_ROOT:
1416 arg_root = path_make_absolute_cwd(optarg);
1417 if (!arg_root)
1418 return log_oom();
1419 path_kill_slashes(arg_root);
1420 break;
1421
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001422 case '?':
1423 return -EINVAL;
1424
1425 default:
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001426 assert_not_reached("Unhandled option");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001427 }
1428 }
1429
1430 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001431 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001432 return -EINVAL;
1433 }
1434
1435 return 1;
1436}
1437
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001438static int read_config_file(const char *fn, bool ignore_enoent) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001439 _cleanup_fclose_ FILE *f = NULL;
1440 char line[LINE_MAX];
Michal Sekletar78a92a52013-01-18 16:13:08 +01001441 Iterator iterator;
Lennart Poettering1731e342013-09-17 11:02:02 -05001442 unsigned v = 0;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001443 Item *i;
Lennart Poettering1731e342013-09-17 11:02:02 -05001444 int r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001445
1446 assert(fn);
1447
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001448 r = search_and_fopen_nulstr(fn, "re", arg_root, conf_file_dirs, &f);
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001449 if (r < 0) {
1450 if (ignore_enoent && r == -ENOENT)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001451 return 0;
1452
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001453 log_error("Failed to open '%s', ignoring: %s", fn, strerror(-r));
1454 return r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001455 }
1456
Lennart Poettering1731e342013-09-17 11:02:02 -05001457 FOREACH_LINE(line, f, break) {
1458 char *l;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001459 int k;
1460
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001461 v++;
1462
1463 l = strstrip(line);
1464 if (*l == '#' || *l == 0)
1465 continue;
1466
Lennart Poettering1731e342013-09-17 11:02:02 -05001467 k = parse_line(fn, v, l);
1468 if (k < 0 && r == 0)
1469 r = k;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001470 }
1471
Michal Sekletar78a92a52013-01-18 16:13:08 +01001472 /* we have to determine age parameter for each entry of type X */
1473 HASHMAP_FOREACH(i, globs, iterator) {
1474 Iterator iter;
1475 Item *j, *candidate_item = NULL;
1476
1477 if (i->type != IGNORE_DIRECTORY_PATH)
1478 continue;
1479
1480 HASHMAP_FOREACH(j, items, iter) {
1481 if (j->type != CREATE_DIRECTORY && j->type != TRUNCATE_DIRECTORY)
1482 continue;
1483
1484 if (path_equal(j->path, i->path)) {
1485 candidate_item = j;
1486 break;
1487 }
1488
1489 if ((!candidate_item && path_startswith(i->path, j->path)) ||
1490 (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
1491 candidate_item = j;
1492 }
1493
1494 if (candidate_item) {
1495 i->age = candidate_item->age;
1496 i->age_set = true;
1497 }
1498 }
1499
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001500 if (ferror(f)) {
1501 log_error("Failed to read from file %s: %m", fn);
1502 if (r == 0)
1503 r = -EIO;
1504 }
1505
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001506 return r;
1507}
1508
Lennart Poettering5008d582010-09-28 02:34:02 +02001509int main(int argc, char *argv[]) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001510 int r, k;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001511 Item *i;
1512 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001513
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001514 r = parse_argv(argc, argv);
1515 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001516 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1517
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001518 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001519 log_parse_environment();
1520 log_open();
1521
Lennart Poettering4c126262011-08-01 20:52:18 +02001522 umask(0022);
1523
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001524 label_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001525
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001526 items = hashmap_new(string_hash_func, string_compare_func);
1527 globs = hashmap_new(string_hash_func, string_compare_func);
1528
1529 if (!items || !globs) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001530 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001531 goto finish;
1532 }
1533
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001534 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001535
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001536 if (optind < argc) {
1537 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001538
Dave Reisner91256702012-06-08 22:31:19 -04001539 for (j = optind; j < argc; j++) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001540 k = read_config_file(argv[j], false);
1541 if (k < 0 && r == 0)
1542 r = k;
Dave Reisner91256702012-06-08 22:31:19 -04001543 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001544
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001545 } else {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001546 _cleanup_strv_free_ char **files = NULL;
1547 char **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001548
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001549 r = conf_files_list_nulstr(&files, ".conf", arg_root, conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001550 if (r < 0) {
Kay Sievers44143302011-04-28 23:51:24 +02001551 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
1552 goto finish;
1553 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001554
Kay Sievers772f8372011-04-25 21:38:21 +02001555 STRV_FOREACH(f, files) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001556 k = read_config_file(*f, true);
1557 if (k < 0 && r == 0)
1558 r = k;
Lennart Poettering5008d582010-09-28 02:34:02 +02001559 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001560 }
1561
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001562 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001563 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001564
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001565 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001566 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001567
Lennart Poettering5008d582010-09-28 02:34:02 +02001568finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001569 while ((i = hashmap_steal_first(items)))
1570 item_free(i);
1571
Lennart Poettering17b90522011-02-14 21:55:06 +01001572 while ((i = hashmap_steal_first(globs)))
1573 item_free(i);
1574
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001575 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001576 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001577
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001578 free(arg_include_prefixes);
1579 free(arg_exclude_prefixes);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001580 free(arg_root);
Dave Reisnera2aced42013-07-24 11:10:05 -04001581
Lennart Poettering17b90522011-02-14 21:55:06 +01001582 set_free_free(unix_sockets);
1583
Lennart Poettering29003cf2010-10-19 19:36:45 +02001584 label_finish();
1585
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001586 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Lennart Poettering5008d582010-09-28 02:34:02 +02001587}