blob: b80731d470eae158b2158dd58d45790cad48ab38 [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 Poettering24f3a372012-06-20 09:05:50 +0200102
103 bool keep_first_level:1;
Lennart Poettering1910cd02014-06-11 01:37:35 +0200104
105 bool done:1;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200106} Item;
107
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200108static bool arg_create = false;
109static bool arg_clean = false;
110static bool arg_remove = false;
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -0500111static bool arg_boot = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200112
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200113static char **arg_include_prefixes = NULL;
114static char **arg_exclude_prefixes = NULL;
Michael Marineaucf9a4ab2014-03-13 21:32:13 -0700115static char *arg_root = NULL;
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100116
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100117static const char conf_file_dirs[] =
118 "/etc/tmpfiles.d\0"
119 "/run/tmpfiles.d\0"
120 "/usr/local/lib/tmpfiles.d\0"
121 "/usr/lib/tmpfiles.d\0"
Lennart Poettering3f2afb22012-07-20 16:24:55 +0200122#ifdef HAVE_SPLIT_USR
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100123 "/lib/tmpfiles.d\0"
Lennart Poettering3f2afb22012-07-20 16:24:55 +0200124#endif
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100125 ;
Dave Reisner91256702012-06-08 22:31:19 -0400126
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200127#define MAX_DEPTH 256
128
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200129static Hashmap *items = NULL, *globs = NULL;
130static Set *unix_sockets = NULL;
131
Michal Schmidt66ccd032011-12-15 21:31:14 +0100132static bool needs_glob(ItemType t) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200133 return IN_SET(t,
134 WRITE_FILE,
135 IGNORE_PATH,
136 IGNORE_DIRECTORY_PATH,
137 REMOVE_PATH,
138 RECURSIVE_REMOVE_PATH,
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200139 ADJUST_MODE,
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200140 RELABEL_PATH,
141 RECURSIVE_RELABEL_PATH);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100142}
143
144static struct Item* find_glob(Hashmap *h, const char *match) {
145 Item *j;
146 Iterator i;
147
148 HASHMAP_FOREACH(j, h, i)
149 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
150 return j;
151
152 return NULL;
153}
154
Lennart Poettering17b90522011-02-14 21:55:06 +0100155static void load_unix_sockets(void) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200156 _cleanup_fclose_ FILE *f = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100157 char line[LINE_MAX];
158
159 if (unix_sockets)
160 return;
161
162 /* We maintain a cache of the sockets we found in
163 * /proc/net/unix to speed things up a little. */
164
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100165 unix_sockets = set_new(string_hash_func, string_compare_func);
166 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100167 return;
168
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100169 f = fopen("/proc/net/unix", "re");
170 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100171 return;
172
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100173 /* Skip header */
174 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100175 goto fail;
176
177 for (;;) {
178 char *p, *s;
179 int k;
180
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100181 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100182 break;
183
184 truncate_nl(line);
185
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100186 p = strchr(line, ':');
187 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100188 continue;
189
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100190 if (strlen(p) < 37)
191 continue;
192
193 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100194 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100195 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100196 p += strspn(p, WHITESPACE);
197
198 if (*p != '/')
199 continue;
200
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100201 s = strdup(p);
202 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100203 goto fail;
204
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100205 path_kill_slashes(s);
206
Zbigniew Jędrzejewski-Szmekef422022013-04-22 23:12:15 -0400207 k = set_consume(unix_sockets, s);
208 if (k < 0 && k != -EEXIST)
209 goto fail;
Lennart Poettering17b90522011-02-14 21:55:06 +0100210 }
211
212 return;
213
214fail:
215 set_free_free(unix_sockets);
216 unix_sockets = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100217}
218
219static bool unix_socket_alive(const char *fn) {
220 assert(fn);
221
222 load_unix_sockets();
223
224 if (unix_sockets)
225 return !!set_get(unix_sockets, (char*) fn);
226
227 /* We don't know, so assume yes */
228 return true;
229}
230
Kay Sievers99d680a2013-03-13 13:12:36 +0100231static int dir_is_mount_point(DIR *d, const char *subdir) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200232
233 union file_handle_union h = {
234 .handle.handle_bytes = MAX_HANDLE_SZ
235 };
236
Kay Sievers99d680a2013-03-13 13:12:36 +0100237 int mount_id_parent, mount_id;
238 int r_p, r;
239
Dave Reisner370c8602014-04-19 13:22:35 -0400240 r_p = name_to_handle_at(dirfd(d), ".", &h.handle, &mount_id_parent, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100241 if (r_p < 0)
242 r_p = -errno;
243
Dave Reisner370c8602014-04-19 13:22:35 -0400244 h.handle.handle_bytes = MAX_HANDLE_SZ;
245 r = name_to_handle_at(dirfd(d), subdir, &h.handle, &mount_id, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100246 if (r < 0)
247 r = -errno;
248
249 /* got no handle; make no assumptions, return error */
250 if (r_p < 0 && r < 0)
251 return r_p;
252
253 /* got both handles; if they differ, it is a mount point */
254 if (r_p >= 0 && r >= 0)
255 return mount_id_parent != mount_id;
256
257 /* got only one handle; assume different mount points if one
258 * of both queries was not supported by the filesystem */
259 if (r_p == -ENOSYS || r_p == -ENOTSUP || r == -ENOSYS || r == -ENOTSUP)
260 return true;
261
262 /* return error */
263 if (r_p < 0)
264 return r_p;
265 return r;
266}
267
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200268static int dir_cleanup(
Michal Sekletar78a92a52013-01-18 16:13:08 +0100269 Item *i,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200270 const char *p,
271 DIR *d,
272 const struct stat *ds,
273 usec_t cutoff,
274 dev_t rootdev,
275 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200276 int maxdepth,
Lennart Poettering265ffa12013-09-17 16:33:30 -0500277 bool keep_this_level) {
278
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200279 struct dirent *dent;
280 struct timespec times[2];
281 bool deleted = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200282 int r = 0;
283
284 while ((dent = readdir(d))) {
285 struct stat s;
286 usec_t age;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200287 _cleanup_free_ char *sub_path = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200288
289 if (streq(dent->d_name, ".") ||
290 streq(dent->d_name, ".."))
291 continue;
292
293 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
Kay Sieversca2f4172013-10-17 03:20:46 +0200294 if (errno == ENOENT)
295 continue;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200296
Kay Sieversca2f4172013-10-17 03:20:46 +0200297 /* FUSE, NFS mounts, SELinux might return EACCES */
298 if (errno == EACCES)
299 log_debug("stat(%s/%s) failed: %m", p, dent->d_name);
300 else
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200301 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
Kay Sieversca2f4172013-10-17 03:20:46 +0200302 r = -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200303 continue;
304 }
305
306 /* Stay on the same filesystem */
307 if (s.st_dev != rootdev)
308 continue;
309
Kay Sievers99d680a2013-03-13 13:12:36 +0100310 /* Try to detect bind mounts of the same filesystem instance; they
311 * do not differ in device major/minors. This type of query is not
312 * supported on all kernels or filesystem types though. */
313 if (S_ISDIR(s.st_mode) && dir_is_mount_point(d, dent->d_name) > 0)
314 continue;
315
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200316 /* Do not delete read-only files owned by root */
317 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
318 continue;
319
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200320 sub_path = strjoin(p, "/", dent->d_name, NULL);
321 if (!sub_path) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700322 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200323 goto finish;
324 }
325
326 /* Is there an item configured for this path? */
327 if (hashmap_get(items, sub_path))
328 continue;
329
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100330 if (find_glob(globs, sub_path))
331 continue;
332
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200333 if (S_ISDIR(s.st_mode)) {
334
335 if (mountpoint &&
336 streq(dent->d_name, "lost+found") &&
337 s.st_uid == 0)
338 continue;
339
340 if (maxdepth <= 0)
341 log_warning("Reached max depth on %s.", sub_path);
342 else {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200343 _cleanup_closedir_ DIR *sub_dir;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200344 int q;
345
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200346 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200347 if (!sub_dir) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200348 if (errno != ENOENT) {
349 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
350 r = -errno;
351 }
352
353 continue;
354 }
355
Michal Sekletar78a92a52013-01-18 16:13:08 +0100356 q = dir_cleanup(i, sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200357 if (q < 0)
358 r = q;
359 }
360
Lennart Poettering24f3a372012-06-20 09:05:50 +0200361 /* Note: if you are wondering why we don't
362 * support the sticky bit for excluding
363 * directories from cleaning like we do it for
364 * other file system objects: well, the sticky
365 * bit already has a meaning for directories,
366 * so we don't want to overload that. */
367
368 if (keep_this_level)
369 continue;
370
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200371 /* Ignore ctime, we change it when deleting */
372 age = MAX(timespec_load(&s.st_mtim),
373 timespec_load(&s.st_atim));
374 if (age >= cutoff)
375 continue;
376
Lukas Nykryna6187d42013-03-01 18:29:59 +0100377 if (i->type != IGNORE_DIRECTORY_PATH || !streq(dent->d_name, p)) {
Lennart Poettering9f6445e32013-12-24 16:39:37 +0100378 log_debug("rmdir '%s'", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200379
Michal Sekletar78a92a52013-01-18 16:13:08 +0100380 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
381 if (errno != ENOENT && errno != ENOTEMPTY) {
382 log_error("rmdir(%s): %m", sub_path);
383 r = -errno;
384 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200385 }
386 }
387
388 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100389 /* Skip files for which the sticky bit is
390 * set. These are semantics we define, and are
391 * unknown elsewhere. See XDG_RUNTIME_DIR
392 * specification for details. */
393 if (s.st_mode & S_ISVTX)
394 continue;
395
Lennart Poettering17b90522011-02-14 21:55:06 +0100396 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200397 if (streq(dent->d_name, ".journal") &&
398 s.st_uid == 0)
399 continue;
400
401 if (streq(dent->d_name, "aquota.user") ||
402 streq(dent->d_name, "aquota.group"))
403 continue;
404 }
405
Lennart Poettering17b90522011-02-14 21:55:06 +0100406 /* Ignore sockets that are listed in /proc/net/unix */
407 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
408 continue;
409
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100410 /* Ignore device nodes */
411 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
412 continue;
413
Lennart Poettering24f3a372012-06-20 09:05:50 +0200414 /* Keep files on this level around if this is
415 * requested */
416 if (keep_this_level)
417 continue;
418
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200419 age = MAX3(timespec_load(&s.st_mtim),
420 timespec_load(&s.st_atim),
421 timespec_load(&s.st_ctim));
422
423 if (age >= cutoff)
424 continue;
425
Lennart Poettering9f6445e32013-12-24 16:39:37 +0100426 log_debug("unlink '%s'", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200427
428 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
429 if (errno != ENOENT) {
430 log_error("unlink(%s): %m", sub_path);
431 r = -errno;
432 }
433 }
434
435 deleted = true;
436 }
437 }
438
439finish:
440 if (deleted) {
441 /* Restore original directory timestamps */
442 times[0] = ds->st_atim;
443 times[1] = ds->st_mtim;
444
445 if (futimens(dirfd(d), times) < 0)
446 log_error("utimensat(%s): %m", p);
447 }
448
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200449 return r;
450}
451
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200452static int item_set_perms(Item *i, const char *path) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200453 assert(i);
454 assert(path);
455
Michal Schmidt062e01b2011-12-16 18:00:11 +0100456 /* not using i->path directly because it may be a glob */
457 if (i->mode_set)
458 if (chmod(path, i->mode) < 0) {
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200459 log_error("chmod(%s) failed: %m", path);
460 return -errno;
Michal Schmidt062e01b2011-12-16 18:00:11 +0100461 }
462
463 if (i->uid_set || i->gid_set)
464 if (chown(path,
465 i->uid_set ? i->uid : (uid_t) -1,
466 i->gid_set ? i->gid : (gid_t) -1) < 0) {
467
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200468 log_error("chown(%s) failed: %m", path);
469 return -errno;
Michal Schmidt062e01b2011-12-16 18:00:11 +0100470 }
471
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200472 return label_fix(path, false, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100473}
474
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400475static int write_one_file(Item *i, const char *path) {
Lennart Poettering874f1942014-06-10 22:48:56 +0200476 int flags;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200477 int fd = -1;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400478 struct stat st;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200479 int r = 0;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400480
Lennart Poettering874f1942014-06-10 22:48:56 +0200481 assert(i);
482 assert(path);
483
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400484 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
485 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
486
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200487 RUN_WITH_UMASK(0) {
488 label_context_set(path, S_IFREG);
489 fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200490 label_context_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200491 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400492
493 if (fd < 0) {
494 if (i->type == WRITE_FILE && errno == ENOENT)
495 return 0;
496
497 log_error("Failed to create file %s: %m", path);
498 return -errno;
499 }
500
501 if (i->argument) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200502 _cleanup_free_ char *unescaped;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400503 ssize_t n;
504 size_t l;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400505
Dave Reisner54693d92012-09-15 12:58:49 -0400506 unescaped = cunescape(i->argument);
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100507 if (unescaped == NULL) {
Lennart Poettering03e334a2014-03-18 19:22:43 +0100508 safe_close(fd);
Dave Reisner54693d92012-09-15 12:58:49 -0400509 return log_oom();
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100510 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400511
Dave Reisner54693d92012-09-15 12:58:49 -0400512 l = strlen(unescaped);
513 n = write(fd, unescaped, l);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400514
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400515 if (n < 0 || (size_t) n < l) {
516 log_error("Failed to write file %s: %s", path, n < 0 ? strerror(-n) : "Short write");
Lennart Poettering03e334a2014-03-18 19:22:43 +0100517 safe_close(fd);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400518 return n < 0 ? n : -EIO;
519 }
520 }
521
Lennart Poettering03e334a2014-03-18 19:22:43 +0100522 safe_close(fd);
Dave Reisner3612fbc2012-09-12 16:21:00 -0400523
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400524 if (stat(path, &st) < 0) {
525 log_error("stat(%s) failed: %m", path);
526 return -errno;
527 }
528
529 if (!S_ISREG(st.st_mode)) {
530 log_error("%s is not a file.", path);
531 return -EEXIST;
532 }
533
534 r = item_set_perms(i, path);
535 if (r < 0)
536 return r;
537
538 return 0;
539}
540
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200541static int item_set_perms_children(Item *i, const char *path) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200542 _cleanup_closedir_ DIR *d;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200543 int r = 0;
544
545 assert(i);
546 assert(path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100547
548 /* This returns the first error we run into, but nevertheless
549 * tries to go on */
550
551 d = opendir(path);
552 if (!d)
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200553 return errno == ENOENT || errno == ENOTDIR ? 0 : -errno;
Michal Schmidta8d88782011-12-15 23:11:07 +0100554
555 for (;;) {
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200556 _cleanup_free_ char *p = NULL;
Lennart Poettering7d5e9c02012-09-19 22:21:09 +0200557 struct dirent *de;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200558 int q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100559
Florian Weimerd78096b2013-12-19 12:26:07 +0100560 errno = 0;
561 de = readdir(d);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200562 if (!de) {
563 if (errno != 0 && r == 0)
564 r = -errno;
565
Michal Schmidta8d88782011-12-15 23:11:07 +0100566 break;
567 }
568
Michal Schmidta8d88782011-12-15 23:11:07 +0100569 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
570 continue;
571
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200572 p = strjoin(path, "/", de->d_name, NULL);
573 if (!p)
574 return -ENOMEM;
Michal Schmidta8d88782011-12-15 23:11:07 +0100575
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200576 q = item_set_perms(i, p);
577 if (q < 0 && q != -ENOENT && r == 0)
578 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100579
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200580 if (IN_SET(de->d_type, DT_UNKNOWN, DT_DIR)) {
581 q = item_set_perms_children(i, p);
582 if (q < 0 && r == 0)
583 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100584 }
Michal Schmidta8d88782011-12-15 23:11:07 +0100585 }
586
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200587 return r;
Michal Schmidta8d88782011-12-15 23:11:07 +0100588}
589
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200590static int item_set_perms_recursive(Item *i, const char *path) {
591 int r, q;
592
593 assert(i);
594 assert(path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100595
Michal Schmidt062e01b2011-12-16 18:00:11 +0100596 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100597 if (r < 0)
598 return r;
599
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200600 q = item_set_perms_children(i, path);
601 if (q < 0 && r == 0)
602 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +0100603
604 return r;
605}
606
Michal Schmidt99e68c02011-12-15 23:45:26 +0100607static int glob_item(Item *i, int (*action)(Item *, const char *)) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200608 _cleanup_globfree_ glob_t g = {};
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200609 int r = 0, k;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100610 char **fn;
611
Michal Schmidt99e68c02011-12-15 23:45:26 +0100612 errno = 0;
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400613 k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200614 if (k != 0 && k != GLOB_NOMATCH) {
615 if (errno == 0)
616 errno = EIO;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100617
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200618 log_error("glob(%s) failed: %m", i->path);
619 return -errno;
620 }
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400621
622 STRV_FOREACH(fn, g.gl_pathv) {
623 k = action(i, *fn);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200624 if (k < 0 && r == 0)
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400625 r = k;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100626 }
627
Michal Schmidt99e68c02011-12-15 23:45:26 +0100628 return r;
629}
630
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200631static int create_item(Item *i) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200632 struct stat st;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200633 int r = 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200634
635 assert(i);
636
637 switch (i->type) {
638
639 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100640 case IGNORE_DIRECTORY_PATH:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200641 case REMOVE_PATH:
642 case RECURSIVE_REMOVE_PATH:
643 return 0;
644
645 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100646 case TRUNCATE_FILE:
Dave Reisner1845fdd2012-09-27 20:48:13 -0400647 r = write_one_file(i, i->path);
648 if (r < 0)
649 return r;
650 break;
Lennart Poettering265ffa12013-09-17 16:33:30 -0500651
Lennart Poettering849958d2014-06-10 23:02:40 +0200652 case COPY_FILES:
653 r = copy_tree(i->argument, i->path);
654 if (r < 0) {
655 log_error("Failed to copy files: %s", strerror(-r));
656 return r;
657 }
658
659 r = item_set_perms(i, i->path);
660 if (r < 0)
661 return r;
662
663 break;
664
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400665 case WRITE_FILE:
666 r = glob_item(i, write_one_file);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100667 if (r < 0)
668 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200669
670 break;
671
672 case TRUNCATE_DIRECTORY:
673 case CREATE_DIRECTORY:
674
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200675 RUN_WITH_UMASK(0000) {
676 mkdir_parents_label(i->path, 0755);
677 r = mkdir(i->path, i->mode);
678 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200679
680 if (r < 0 && errno != EEXIST) {
681 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100682 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200683 }
684
685 if (stat(i->path, &st) < 0) {
686 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100687 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200688 }
689
690 if (!S_ISDIR(st.st_mode)) {
691 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100692 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200693 }
694
Michal Schmidt062e01b2011-12-16 18:00:11 +0100695 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100696 if (r < 0)
697 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200698
699 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200700
701 case CREATE_FIFO:
702
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200703 RUN_WITH_UMASK(0000) {
704 r = mkfifo(i->path, i->mode);
705 }
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200706
707 if (r < 0 && errno != EEXIST) {
708 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100709 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200710 }
711
712 if (stat(i->path, &st) < 0) {
713 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100714 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200715 }
716
717 if (!S_ISFIFO(st.st_mode)) {
718 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100719 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200720 }
721
Michal Schmidt062e01b2011-12-16 18:00:11 +0100722 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100723 if (r < 0)
724 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200725
726 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100727
Lennart Poettering468d7262012-01-17 15:04:12 +0100728 case CREATE_SYMLINK: {
Lennart Poetteringe26da2d2014-02-19 17:52:41 +0100729 _cleanup_free_ char *x = NULL;
Lennart Poettering468d7262012-01-17 15:04:12 +0100730
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200731 label_context_set(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100732 r = symlink(i->argument, i->path);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200733 label_context_clear();
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200734
Lennart Poettering468d7262012-01-17 15:04:12 +0100735 if (r < 0 && errno != EEXIST) {
736 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
737 return -errno;
738 }
739
740 r = readlink_malloc(i->path, &x);
741 if (r < 0) {
742 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
743 return -errno;
744 }
745
746 if (!streq(i->argument, x)) {
Lennart Poettering9339db72014-06-11 00:12:21 +0200747 log_error("%s is not the right symlink.", i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100748 return -EEXIST;
749 }
750
Lennart Poettering468d7262012-01-17 15:04:12 +0100751 break;
752 }
753
754 case CREATE_BLOCK_DEVICE:
755 case CREATE_CHAR_DEVICE: {
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700756 mode_t file_type;
757
758 if (have_effective_cap(CAP_MKNOD) == 0) {
759 /* In a container we lack CAP_MKNOD. We
Anatol Pomozovab06eef2013-04-14 19:37:54 -0700760 shouldn't attempt to create the device node in
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700761 that case to avoid noise, and we don't support
762 virtualized devices in containers anyway. */
763
764 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
765 return 0;
766 }
767
768 file_type = (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +0100769
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200770 RUN_WITH_UMASK(0000) {
771 label_context_set(i->path, file_type);
772 r = mknod(i->path, i->mode | file_type, i->major_minor);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200773 label_context_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200774 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100775
776 if (r < 0 && errno != EEXIST) {
777 log_error("Failed to create device node %s: %m", i->path);
778 return -errno;
779 }
780
781 if (stat(i->path, &st) < 0) {
782 log_error("stat(%s) failed: %m", i->path);
783 return -errno;
784 }
785
Michal Schmidte7aee752012-06-14 16:01:19 +0200786 if ((st.st_mode & S_IFMT) != file_type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100787 log_error("%s is not a device node.", i->path);
788 return -EEXIST;
789 }
790
791 r = item_set_perms(i, i->path);
792 if (r < 0)
793 return r;
794
795 break;
796 }
797
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200798 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100799 case RELABEL_PATH:
800
801 r = glob_item(i, item_set_perms);
802 if (r < 0)
Lennart Poettering96ca8192013-06-21 15:57:42 +0200803 return r;
Michal Schmidt777b87e2011-12-16 18:27:35 +0100804 break;
805
Michal Schmidta8d88782011-12-15 23:11:07 +0100806 case RECURSIVE_RELABEL_PATH:
807
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200808 r = glob_item(i, item_set_perms_recursive);
Michal Schmidta8d88782011-12-15 23:11:07 +0100809 if (r < 0)
810 return r;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200811
812 break;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200813 }
814
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200815 log_debug("%s created successfully.", i->path);
816
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100817 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200818}
819
Michal Schmidta0896122011-12-15 21:32:50 +0100820static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200821 int r;
822
823 assert(i);
824
825 switch (i->type) {
826
827 case CREATE_FILE:
828 case TRUNCATE_FILE:
829 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200830 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100831 case CREATE_SYMLINK:
832 case CREATE_BLOCK_DEVICE:
833 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200834 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100835 case IGNORE_DIRECTORY_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200836 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100837 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100838 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100839 case WRITE_FILE:
Lennart Poettering849958d2014-06-10 23:02:40 +0200840 case COPY_FILES:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200841 break;
842
843 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100844 if (remove(instance) < 0 && errno != ENOENT) {
845 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200846 return -errno;
847 }
848
849 break;
850
851 case TRUNCATE_DIRECTORY:
852 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +0200853 /* FIXME: we probably should use dir_cleanup() here
854 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +0200855 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Lennart Poettering468d7262012-01-17 15:04:12 +0100856 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100857 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200858 return r;
859 }
860
861 break;
862 }
863
864 return 0;
865}
866
Michal Schmidta0896122011-12-15 21:32:50 +0100867static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100868 int r = 0;
869
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100870 assert(i);
871
872 switch (i->type) {
873
874 case CREATE_FILE:
875 case TRUNCATE_FILE:
876 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200877 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100878 case CREATE_SYMLINK:
879 case CREATE_CHAR_DEVICE:
880 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100881 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100882 case IGNORE_DIRECTORY_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200883 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100884 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100885 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100886 case WRITE_FILE:
Lennart Poettering849958d2014-06-10 23:02:40 +0200887 case COPY_FILES:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100888 break;
889
890 case REMOVE_PATH:
891 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100892 case RECURSIVE_REMOVE_PATH:
893 r = glob_item(i, remove_item_instance);
894 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100895 }
896
Michal Schmidt99e68c02011-12-15 23:45:26 +0100897 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100898}
899
Michal Sekletar78a92a52013-01-18 16:13:08 +0100900static int clean_item_instance(Item *i, const char* instance) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200901 _cleanup_closedir_ DIR *d = NULL;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100902 struct stat s, ps;
903 bool mountpoint;
904 int r;
905 usec_t cutoff, n;
906
907 assert(i);
908
909 if (!i->age_set)
910 return 0;
911
912 n = now(CLOCK_REALTIME);
913 if (n < i->age)
914 return 0;
915
916 cutoff = n - i->age;
917
918 d = opendir(instance);
919 if (!d) {
920 if (errno == ENOENT || errno == ENOTDIR)
921 return 0;
922
923 log_error("Failed to open directory %s: %m", i->path);
924 return -errno;
925 }
926
927 if (fstat(dirfd(d), &s) < 0) {
928 log_error("stat(%s) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500929 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100930 }
931
932 if (!S_ISDIR(s.st_mode)) {
933 log_error("%s is not a directory.", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500934 return -ENOTDIR;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100935 }
936
937 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
938 log_error("stat(%s/..) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500939 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100940 }
941
942 mountpoint = s.st_dev != ps.st_dev ||
943 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
944
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500945 r = dir_cleanup(i, instance, d, &s, cutoff, s.st_dev, mountpoint,
946 MAX_DEPTH, i->keep_first_level);
Michal Sekletar78a92a52013-01-18 16:13:08 +0100947 return r;
948}
949
950static int clean_item(Item *i) {
951 int r = 0;
952
953 assert(i);
954
955 switch (i->type) {
956 case CREATE_DIRECTORY:
957 case TRUNCATE_DIRECTORY:
958 case IGNORE_PATH:
Lennart Poettering849958d2014-06-10 23:02:40 +0200959 case COPY_FILES:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100960 clean_item_instance(i, i->path);
961 break;
962 case IGNORE_DIRECTORY_PATH:
963 r = glob_item(i, clean_item_instance);
964 break;
965 default:
966 break;
967 }
968
969 return r;
970}
971
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200972static int process_item(Item *i) {
973 int r, q, p;
Lennart Poettering1910cd02014-06-11 01:37:35 +0200974 char prefix[PATH_MAX];
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200975
976 assert(i);
977
Lennart Poettering1910cd02014-06-11 01:37:35 +0200978 if (i->done)
979 return 0;
980
981 i->done = true;
982
983 PATH_FOREACH_PREFIX(prefix, i->path) {
984 Item *j;
985
986 j = hashmap_get(items, prefix);
987 if (j)
988 process_item(j);
989 }
990
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200991 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100992 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200993 p = arg_clean ? clean_item(i) : 0;
994
995 if (r < 0)
996 return r;
997
998 if (q < 0)
999 return q;
1000
1001 return p;
1002}
1003
1004static void item_free(Item *i) {
1005 assert(i);
1006
1007 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +01001008 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001009 free(i);
1010}
1011
Lennart Poettering14bf2c92013-10-14 04:59:26 +02001012DEFINE_TRIVIAL_CLEANUP_FUNC(Item*, item_free);
Maciej Wereskie2f2fb72013-07-19 15:43:12 +02001013
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001014static bool item_equal(Item *a, Item *b) {
1015 assert(a);
1016 assert(b);
1017
1018 if (!streq_ptr(a->path, b->path))
1019 return false;
1020
1021 if (a->type != b->type)
1022 return false;
1023
1024 if (a->uid_set != b->uid_set ||
1025 (a->uid_set && a->uid != b->uid))
1026 return false;
1027
1028 if (a->gid_set != b->gid_set ||
1029 (a->gid_set && a->gid != b->gid))
1030 return false;
1031
1032 if (a->mode_set != b->mode_set ||
1033 (a->mode_set && a->mode != b->mode))
1034 return false;
1035
1036 if (a->age_set != b->age_set ||
1037 (a->age_set && a->age != b->age))
1038 return false;
1039
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001040 if ((a->type == CREATE_FILE ||
1041 a->type == TRUNCATE_FILE ||
1042 a->type == WRITE_FILE ||
Lennart Poettering849958d2014-06-10 23:02:40 +02001043 a->type == CREATE_SYMLINK ||
1044 a->type == COPY_FILES) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +01001045 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +01001046 return false;
1047
1048 if ((a->type == CREATE_CHAR_DEVICE ||
1049 a->type == CREATE_BLOCK_DEVICE) &&
1050 a->major_minor != b->major_minor)
1051 return false;
1052
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001053 return true;
1054}
1055
Dave Reisnera2aced42013-07-24 11:10:05 -04001056static bool should_include_path(const char *path) {
1057 char **prefix;
1058
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001059 STRV_FOREACH(prefix, arg_exclude_prefixes) {
Dave Reisner5c795112013-07-24 11:19:24 -04001060 if (path_startswith(path, *prefix))
1061 return false;
1062 }
Dave Reisnera2aced42013-07-24 11:10:05 -04001063
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001064 STRV_FOREACH(prefix, arg_include_prefixes) {
Dave Reisnera2aced42013-07-24 11:10:05 -04001065 if (path_startswith(path, *prefix))
1066 return true;
1067 }
1068
Dave Reisner5c795112013-07-24 11:19:24 -04001069 /* no matches, so we should include this path only if we
1070 * have no whitelist at all */
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001071 return strv_length(arg_include_prefixes) == 0;
Dave Reisnera2aced42013-07-24 11:10:05 -04001072}
1073
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001074static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001075
1076 static const Specifier specifier_table[] = {
1077 { 'm', specifier_machine_id, NULL },
1078 { 'b', specifier_boot_id, NULL },
1079 { 'H', specifier_host_name, NULL },
1080 { 'v', specifier_kernel_release, NULL },
1081 {}
1082 };
1083
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001084 _cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
1085 _cleanup_(item_freep) Item *i = NULL;
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001086 Item *existing;
Michal Schmidt66ccd032011-12-15 21:31:14 +01001087 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001088 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001089 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +02001090
1091 assert(fname);
1092 assert(line >= 1);
1093 assert(buffer);
1094
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001095 r = sscanf(buffer,
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001096 "%ms %ms %ms %ms %ms %ms %n",
1097 &action,
Lennart Poettering1731e342013-09-17 11:02:02 -05001098 &path,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +01001099 &mode,
1100 &user,
1101 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +01001102 &age,
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001103 &n);
1104 if (r < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001105 log_error("[%s:%u] Syntax error.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001106 return -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +02001107 }
1108
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001109 if (strlen(action) > 2 || (strlen(action) > 1 && action[1] != '!')) {
1110 log_error("[%s:%u] Unknown modifier '%s'", fname, line, action);
1111 return -EINVAL;
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001112 } else if (strlen(action) > 1 && !arg_boot)
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001113 return 0;
1114
1115 type = action[0];
1116
Lennart Poettering1731e342013-09-17 11:02:02 -05001117 i = new0(Item, 1);
1118 if (!i)
1119 return log_oom();
1120
1121 r = specifier_printf(path, specifier_table, NULL, &i->path);
1122 if (r < 0) {
1123 log_error("[%s:%u] Failed to replace specifiers: %s", fname, line, path);
1124 return r;
1125 }
1126
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001127 if (n >= 0) {
1128 n += strspn(buffer+n, WHITESPACE);
1129 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
1130 i->argument = unquote(buffer+n, "\"");
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001131 if (!i->argument)
1132 return log_oom();
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001133 }
1134 }
1135
Michal Schmidt777b87e2011-12-16 18:27:35 +01001136 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001137
Michal Schmidt777b87e2011-12-16 18:27:35 +01001138 case CREATE_FILE:
1139 case TRUNCATE_FILE:
1140 case CREATE_DIRECTORY:
1141 case TRUNCATE_DIRECTORY:
1142 case CREATE_FIFO:
1143 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001144 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001145 case REMOVE_PATH:
1146 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001147 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001148 case RELABEL_PATH:
1149 case RECURSIVE_RELABEL_PATH:
1150 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001151
1152 case CREATE_SYMLINK:
1153 if (!i->argument) {
1154 log_error("[%s:%u] Symlink file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001155 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001156 }
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001157
Lennart Poettering468d7262012-01-17 15:04:12 +01001158 break;
1159
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001160 case WRITE_FILE:
1161 if (!i->argument) {
1162 log_error("[%s:%u] Write file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001163 return -EBADMSG;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001164 }
1165 break;
1166
Lennart Poettering849958d2014-06-10 23:02:40 +02001167 case COPY_FILES:
1168 if (!i->argument) {
1169 log_error("[%s:%u] Copy files requires argument.", fname, line);
1170 return -EBADMSG;
1171 }
1172
1173 if (!path_is_absolute(i->argument)) {
1174 log_error("[%s:%u] Source path is not absolute.", fname, line);
1175 return -EBADMSG;
1176 }
1177
1178 path_kill_slashes(i->argument);
1179 break;
1180
Lennart Poettering468d7262012-01-17 15:04:12 +01001181 case CREATE_CHAR_DEVICE:
1182 case CREATE_BLOCK_DEVICE: {
1183 unsigned major, minor;
1184
1185 if (!i->argument) {
1186 log_error("[%s:%u] Device file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001187 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001188 }
1189
1190 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1191 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 -04001192 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001193 }
1194
1195 i->major_minor = makedev(major, minor);
1196 break;
1197 }
1198
Michal Schmidt777b87e2011-12-16 18:27:35 +01001199 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001200 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001201 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001202 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001203
Michal Schmidta8d88782011-12-15 23:11:07 +01001204 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001205
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001206 if (!path_is_absolute(i->path)) {
1207 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001208 return -EBADMSG;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001209 }
1210
1211 path_kill_slashes(i->path);
1212
Dave Reisnera2aced42013-07-24 11:10:05 -04001213 if (!should_include_path(i->path))
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001214 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001215
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001216 if (arg_root) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001217 char *p;
1218
1219 p = strappend(arg_root, i->path);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001220 if (!p)
1221 return log_oom();
1222
1223 free(i->path);
1224 i->path = p;
1225 }
1226
Lennart Poettering5008d582010-09-28 02:34:02 +02001227 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001228 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001229
Lennart Poetteringd05c5032012-07-16 12:34:54 +02001230 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001231 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001232 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001233 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001234 }
1235
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001236 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001237 }
1238
1239 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001240 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001241
Lennart Poettering4b678342011-07-23 01:17:59 +02001242 r = get_group_creds(&g, &i->gid);
1243 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001244 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001245 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001246 }
1247
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001248 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001249 }
1250
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001251 if (mode && !streq(mode, "-")) {
1252 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001253
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001254 if (sscanf(mode, "%o", &m) != 1) {
1255 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001256 return -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001257 }
1258
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001259 i->mode = m;
1260 i->mode_set = true;
1261 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001262 i->mode =
1263 i->type == CREATE_DIRECTORY ||
1264 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001265
1266 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001267 const char *a = age;
1268
1269 if (*a == '~') {
1270 i->keep_first_level = true;
1271 a++;
1272 }
1273
Lennart Poettering7f602782013-04-02 20:38:16 +02001274 if (parse_sec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001275 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001276 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001277 }
1278
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001279 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001280 }
1281
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001282 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001283
Lennart Poettering468d7262012-01-17 15:04:12 +01001284 existing = hashmap_get(h, i->path);
1285 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001286
1287 /* Two identical items are fine */
1288 if (!item_equal(existing, i))
1289 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1290
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001291 return 0;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001292 }
1293
Lennart Poettering468d7262012-01-17 15:04:12 +01001294 r = hashmap_put(h, i->path, i);
1295 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001296 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001297 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001298 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001299
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001300 i = NULL; /* avoid cleanup */
Lennart Poettering5008d582010-09-28 02:34:02 +02001301
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001302 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001303}
1304
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001305static int help(void) {
1306
Lennart Poettering522d4a42011-02-13 15:08:15 +01001307 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1308 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001309 " -h --help Show this help\n"
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001310 " --version Show package version\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001311 " --create Create marked files/directories\n"
1312 " --clean Clean up marked directories\n"
1313 " --remove Remove marked files/directories\n"
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001314 " --boot Execute actions only safe at boot\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001315 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n"
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001316 " --exclude-prefix=PATH Ignore rules that apply to paths with the specified prefix\n"
1317 " --root=PATH Operate on an alternate filesystem root\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001318 program_invocation_short_name);
1319
1320 return 0;
1321}
1322
1323static int parse_argv(int argc, char *argv[]) {
1324
1325 enum {
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001326 ARG_VERSION = 0x100,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001327 ARG_CREATE,
1328 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001329 ARG_REMOVE,
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001330 ARG_BOOT,
Dave Reisner5c795112013-07-24 11:19:24 -04001331 ARG_PREFIX,
1332 ARG_EXCLUDE_PREFIX,
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001333 ARG_ROOT,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001334 };
1335
1336 static const struct option options[] = {
Dave Reisner5c795112013-07-24 11:19:24 -04001337 { "help", no_argument, NULL, 'h' },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001338 { "version", no_argument, NULL, ARG_VERSION },
Dave Reisner5c795112013-07-24 11:19:24 -04001339 { "create", no_argument, NULL, ARG_CREATE },
1340 { "clean", no_argument, NULL, ARG_CLEAN },
1341 { "remove", no_argument, NULL, ARG_REMOVE },
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001342 { "boot", no_argument, NULL, ARG_BOOT },
Dave Reisner5c795112013-07-24 11:19:24 -04001343 { "prefix", required_argument, NULL, ARG_PREFIX },
1344 { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX },
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001345 { "root", required_argument, NULL, ARG_ROOT },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001346 {}
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001347 };
1348
1349 int c;
1350
1351 assert(argc >= 0);
1352 assert(argv);
1353
1354 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1355
1356 switch (c) {
1357
1358 case 'h':
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001359 return help();
1360
1361 case ARG_VERSION:
1362 puts(PACKAGE_STRING);
1363 puts(SYSTEMD_FEATURES);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001364 return 0;
1365
1366 case ARG_CREATE:
1367 arg_create = true;
1368 break;
1369
1370 case ARG_CLEAN:
1371 arg_clean = true;
1372 break;
1373
1374 case ARG_REMOVE:
1375 arg_remove = true;
1376 break;
1377
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001378 case ARG_BOOT:
1379 arg_boot = true;
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001380 break;
1381
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001382 case ARG_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001383 if (strv_push(&arg_include_prefixes, optarg) < 0)
Dave Reisnera2aced42013-07-24 11:10:05 -04001384 return log_oom();
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001385 break;
1386
Dave Reisner5c795112013-07-24 11:19:24 -04001387 case ARG_EXCLUDE_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001388 if (strv_push(&arg_exclude_prefixes, optarg) < 0)
Dave Reisner5c795112013-07-24 11:19:24 -04001389 return log_oom();
1390 break;
1391
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001392 case ARG_ROOT:
1393 arg_root = path_make_absolute_cwd(optarg);
1394 if (!arg_root)
1395 return log_oom();
1396 path_kill_slashes(arg_root);
1397 break;
1398
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001399 case '?':
1400 return -EINVAL;
1401
1402 default:
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001403 assert_not_reached("Unhandled option");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001404 }
1405 }
1406
1407 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001408 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001409 return -EINVAL;
1410 }
1411
1412 return 1;
1413}
1414
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001415static int read_config_file(const char *fn, bool ignore_enoent) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001416 _cleanup_fclose_ FILE *f = NULL;
1417 char line[LINE_MAX];
Michal Sekletar78a92a52013-01-18 16:13:08 +01001418 Iterator iterator;
Lennart Poettering1731e342013-09-17 11:02:02 -05001419 unsigned v = 0;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001420 Item *i;
Lennart Poettering1731e342013-09-17 11:02:02 -05001421 int r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001422
1423 assert(fn);
1424
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001425 r = search_and_fopen_nulstr(fn, "re", arg_root, conf_file_dirs, &f);
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001426 if (r < 0) {
1427 if (ignore_enoent && r == -ENOENT)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001428 return 0;
1429
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001430 log_error("Failed to open '%s', ignoring: %s", fn, strerror(-r));
1431 return r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001432 }
1433
Lennart Poettering1731e342013-09-17 11:02:02 -05001434 FOREACH_LINE(line, f, break) {
1435 char *l;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001436 int k;
1437
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001438 v++;
1439
1440 l = strstrip(line);
1441 if (*l == '#' || *l == 0)
1442 continue;
1443
Lennart Poettering1731e342013-09-17 11:02:02 -05001444 k = parse_line(fn, v, l);
1445 if (k < 0 && r == 0)
1446 r = k;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001447 }
1448
Michal Sekletar78a92a52013-01-18 16:13:08 +01001449 /* we have to determine age parameter for each entry of type X */
1450 HASHMAP_FOREACH(i, globs, iterator) {
1451 Iterator iter;
1452 Item *j, *candidate_item = NULL;
1453
1454 if (i->type != IGNORE_DIRECTORY_PATH)
1455 continue;
1456
1457 HASHMAP_FOREACH(j, items, iter) {
1458 if (j->type != CREATE_DIRECTORY && j->type != TRUNCATE_DIRECTORY)
1459 continue;
1460
1461 if (path_equal(j->path, i->path)) {
1462 candidate_item = j;
1463 break;
1464 }
1465
1466 if ((!candidate_item && path_startswith(i->path, j->path)) ||
1467 (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
1468 candidate_item = j;
1469 }
1470
1471 if (candidate_item) {
1472 i->age = candidate_item->age;
1473 i->age_set = true;
1474 }
1475 }
1476
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001477 if (ferror(f)) {
1478 log_error("Failed to read from file %s: %m", fn);
1479 if (r == 0)
1480 r = -EIO;
1481 }
1482
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001483 return r;
1484}
1485
Lennart Poettering5008d582010-09-28 02:34:02 +02001486int main(int argc, char *argv[]) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001487 int r, k;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001488 Item *i;
1489 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001490
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001491 r = parse_argv(argc, argv);
1492 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001493 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1494
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001495 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001496 log_parse_environment();
1497 log_open();
1498
Lennart Poettering4c126262011-08-01 20:52:18 +02001499 umask(0022);
1500
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001501 label_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001502
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001503 items = hashmap_new(string_hash_func, string_compare_func);
1504 globs = hashmap_new(string_hash_func, string_compare_func);
1505
1506 if (!items || !globs) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001507 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001508 goto finish;
1509 }
1510
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001511 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001512
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001513 if (optind < argc) {
1514 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001515
Dave Reisner91256702012-06-08 22:31:19 -04001516 for (j = optind; j < argc; j++) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001517 k = read_config_file(argv[j], false);
1518 if (k < 0 && r == 0)
1519 r = k;
Dave Reisner91256702012-06-08 22:31:19 -04001520 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001521
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001522 } else {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001523 _cleanup_strv_free_ char **files = NULL;
1524 char **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001525
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001526 r = conf_files_list_nulstr(&files, ".conf", arg_root, conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001527 if (r < 0) {
Kay Sievers44143302011-04-28 23:51:24 +02001528 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
1529 goto finish;
1530 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001531
Kay Sievers772f8372011-04-25 21:38:21 +02001532 STRV_FOREACH(f, files) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001533 k = read_config_file(*f, true);
1534 if (k < 0 && r == 0)
1535 r = k;
Lennart Poettering5008d582010-09-28 02:34:02 +02001536 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001537 }
1538
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001539 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001540 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001541
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001542 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001543 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001544
Lennart Poettering5008d582010-09-28 02:34:02 +02001545finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001546 while ((i = hashmap_steal_first(items)))
1547 item_free(i);
1548
Lennart Poettering17b90522011-02-14 21:55:06 +01001549 while ((i = hashmap_steal_first(globs)))
1550 item_free(i);
1551
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001552 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001553 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001554
Lennart Poettering7bc040f2014-06-11 01:26:28 +02001555 free(arg_include_prefixes);
1556 free(arg_exclude_prefixes);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001557 free(arg_root);
Dave Reisnera2aced42013-07-24 11:10:05 -04001558
Lennart Poettering17b90522011-02-14 21:55:06 +01001559 set_free_free(unix_sockets);
1560
Lennart Poettering29003cf2010-10-19 19:36:45 +02001561 label_finish();
1562
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001563 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Lennart Poettering5008d582010-09-28 02:34:02 +02001564}