blob: 6745c23a1527ae0b0e3947c012bd919741605288 [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 Poettering265ffa12013-09-17 16:33:30 -050074 ADJUST_MODE = 'm',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010075
76 /* These ones take globs */
Lennart Poetteringcde684a2014-06-10 22:50:46 +020077 WRITE_FILE = 'w',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020078 IGNORE_PATH = 'x',
Michal Sekletar78a92a52013-01-18 16:13:08 +010079 IGNORE_DIRECTORY_PATH = 'X',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020080 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010081 RECURSIVE_REMOVE_PATH = 'R',
Michal Schmidt777b87e2011-12-16 18:27:35 +010082 RELABEL_PATH = 'z',
Michal Schmidta8d88782011-12-15 23:11:07 +010083 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 Poettering3b63d2d2010-10-18 22:38:41 +0200104} Item;
105
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100106static Hashmap *items = NULL, *globs = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100107static Set *unix_sockets = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200108
109static 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
Dave Reisnera2aced42013-07-24 11:10:05 -0400114static char **include_prefixes = NULL;
Dave Reisner5c795112013-07-24 11:19:24 -0400115static char **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
Michal Schmidt66ccd032011-12-15 21:31:14 +0100130static bool needs_glob(ItemType t) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200131 return IN_SET(t,
132 WRITE_FILE,
133 IGNORE_PATH,
134 IGNORE_DIRECTORY_PATH,
135 REMOVE_PATH,
136 RECURSIVE_REMOVE_PATH,
137 RELABEL_PATH,
138 RECURSIVE_RELABEL_PATH);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100139}
140
141static struct Item* find_glob(Hashmap *h, const char *match) {
142 Item *j;
143 Iterator i;
144
145 HASHMAP_FOREACH(j, h, i)
146 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
147 return j;
148
149 return NULL;
150}
151
Lennart Poettering17b90522011-02-14 21:55:06 +0100152static void load_unix_sockets(void) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200153 _cleanup_fclose_ FILE *f = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100154 char line[LINE_MAX];
155
156 if (unix_sockets)
157 return;
158
159 /* We maintain a cache of the sockets we found in
160 * /proc/net/unix to speed things up a little. */
161
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100162 unix_sockets = set_new(string_hash_func, string_compare_func);
163 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100164 return;
165
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100166 f = fopen("/proc/net/unix", "re");
167 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100168 return;
169
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100170 /* Skip header */
171 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100172 goto fail;
173
174 for (;;) {
175 char *p, *s;
176 int k;
177
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100178 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100179 break;
180
181 truncate_nl(line);
182
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100183 p = strchr(line, ':');
184 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100185 continue;
186
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100187 if (strlen(p) < 37)
188 continue;
189
190 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100191 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100192 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100193 p += strspn(p, WHITESPACE);
194
195 if (*p != '/')
196 continue;
197
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100198 s = strdup(p);
199 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100200 goto fail;
201
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100202 path_kill_slashes(s);
203
Zbigniew Jędrzejewski-Szmekef422022013-04-22 23:12:15 -0400204 k = set_consume(unix_sockets, s);
205 if (k < 0 && k != -EEXIST)
206 goto fail;
Lennart Poettering17b90522011-02-14 21:55:06 +0100207 }
208
209 return;
210
211fail:
212 set_free_free(unix_sockets);
213 unix_sockets = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100214}
215
216static bool unix_socket_alive(const char *fn) {
217 assert(fn);
218
219 load_unix_sockets();
220
221 if (unix_sockets)
222 return !!set_get(unix_sockets, (char*) fn);
223
224 /* We don't know, so assume yes */
225 return true;
226}
227
Kay Sievers99d680a2013-03-13 13:12:36 +0100228static int dir_is_mount_point(DIR *d, const char *subdir) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200229
230 union file_handle_union h = {
231 .handle.handle_bytes = MAX_HANDLE_SZ
232 };
233
Kay Sievers99d680a2013-03-13 13:12:36 +0100234 int mount_id_parent, mount_id;
235 int r_p, r;
236
Dave Reisner370c8602014-04-19 13:22:35 -0400237 r_p = name_to_handle_at(dirfd(d), ".", &h.handle, &mount_id_parent, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100238 if (r_p < 0)
239 r_p = -errno;
240
Dave Reisner370c8602014-04-19 13:22:35 -0400241 h.handle.handle_bytes = MAX_HANDLE_SZ;
242 r = name_to_handle_at(dirfd(d), subdir, &h.handle, &mount_id, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100243 if (r < 0)
244 r = -errno;
245
246 /* got no handle; make no assumptions, return error */
247 if (r_p < 0 && r < 0)
248 return r_p;
249
250 /* got both handles; if they differ, it is a mount point */
251 if (r_p >= 0 && r >= 0)
252 return mount_id_parent != mount_id;
253
254 /* got only one handle; assume different mount points if one
255 * of both queries was not supported by the filesystem */
256 if (r_p == -ENOSYS || r_p == -ENOTSUP || r == -ENOSYS || r == -ENOTSUP)
257 return true;
258
259 /* return error */
260 if (r_p < 0)
261 return r_p;
262 return r;
263}
264
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200265static int dir_cleanup(
Michal Sekletar78a92a52013-01-18 16:13:08 +0100266 Item *i,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200267 const char *p,
268 DIR *d,
269 const struct stat *ds,
270 usec_t cutoff,
271 dev_t rootdev,
272 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200273 int maxdepth,
Lennart Poettering265ffa12013-09-17 16:33:30 -0500274 bool keep_this_level) {
275
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200276 struct dirent *dent;
277 struct timespec times[2];
278 bool deleted = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200279 int r = 0;
280
281 while ((dent = readdir(d))) {
282 struct stat s;
283 usec_t age;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200284 _cleanup_free_ char *sub_path = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200285
286 if (streq(dent->d_name, ".") ||
287 streq(dent->d_name, ".."))
288 continue;
289
290 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
Kay Sieversca2f4172013-10-17 03:20:46 +0200291 if (errno == ENOENT)
292 continue;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200293
Kay Sieversca2f4172013-10-17 03:20:46 +0200294 /* FUSE, NFS mounts, SELinux might return EACCES */
295 if (errno == EACCES)
296 log_debug("stat(%s/%s) failed: %m", p, dent->d_name);
297 else
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200298 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
Kay Sieversca2f4172013-10-17 03:20:46 +0200299 r = -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200300 continue;
301 }
302
303 /* Stay on the same filesystem */
304 if (s.st_dev != rootdev)
305 continue;
306
Kay Sievers99d680a2013-03-13 13:12:36 +0100307 /* Try to detect bind mounts of the same filesystem instance; they
308 * do not differ in device major/minors. This type of query is not
309 * supported on all kernels or filesystem types though. */
310 if (S_ISDIR(s.st_mode) && dir_is_mount_point(d, dent->d_name) > 0)
311 continue;
312
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200313 /* Do not delete read-only files owned by root */
314 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
315 continue;
316
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200317 sub_path = strjoin(p, "/", dent->d_name, NULL);
318 if (!sub_path) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700319 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200320 goto finish;
321 }
322
323 /* Is there an item configured for this path? */
324 if (hashmap_get(items, sub_path))
325 continue;
326
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100327 if (find_glob(globs, sub_path))
328 continue;
329
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200330 if (S_ISDIR(s.st_mode)) {
331
332 if (mountpoint &&
333 streq(dent->d_name, "lost+found") &&
334 s.st_uid == 0)
335 continue;
336
337 if (maxdepth <= 0)
338 log_warning("Reached max depth on %s.", sub_path);
339 else {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200340 _cleanup_closedir_ DIR *sub_dir;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200341 int q;
342
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200343 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200344 if (!sub_dir) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200345 if (errno != ENOENT) {
346 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
347 r = -errno;
348 }
349
350 continue;
351 }
352
Michal Sekletar78a92a52013-01-18 16:13:08 +0100353 q = dir_cleanup(i, sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200354 if (q < 0)
355 r = q;
356 }
357
Lennart Poettering24f3a372012-06-20 09:05:50 +0200358 /* Note: if you are wondering why we don't
359 * support the sticky bit for excluding
360 * directories from cleaning like we do it for
361 * other file system objects: well, the sticky
362 * bit already has a meaning for directories,
363 * so we don't want to overload that. */
364
365 if (keep_this_level)
366 continue;
367
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200368 /* Ignore ctime, we change it when deleting */
369 age = MAX(timespec_load(&s.st_mtim),
370 timespec_load(&s.st_atim));
371 if (age >= cutoff)
372 continue;
373
Lukas Nykryna6187d42013-03-01 18:29:59 +0100374 if (i->type != IGNORE_DIRECTORY_PATH || !streq(dent->d_name, p)) {
Lennart Poettering9f6445e32013-12-24 16:39:37 +0100375 log_debug("rmdir '%s'", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200376
Michal Sekletar78a92a52013-01-18 16:13:08 +0100377 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
378 if (errno != ENOENT && errno != ENOTEMPTY) {
379 log_error("rmdir(%s): %m", sub_path);
380 r = -errno;
381 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200382 }
383 }
384
385 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100386 /* Skip files for which the sticky bit is
387 * set. These are semantics we define, and are
388 * unknown elsewhere. See XDG_RUNTIME_DIR
389 * specification for details. */
390 if (s.st_mode & S_ISVTX)
391 continue;
392
Lennart Poettering17b90522011-02-14 21:55:06 +0100393 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200394 if (streq(dent->d_name, ".journal") &&
395 s.st_uid == 0)
396 continue;
397
398 if (streq(dent->d_name, "aquota.user") ||
399 streq(dent->d_name, "aquota.group"))
400 continue;
401 }
402
Lennart Poettering17b90522011-02-14 21:55:06 +0100403 /* Ignore sockets that are listed in /proc/net/unix */
404 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
405 continue;
406
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100407 /* Ignore device nodes */
408 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
409 continue;
410
Lennart Poettering24f3a372012-06-20 09:05:50 +0200411 /* Keep files on this level around if this is
412 * requested */
413 if (keep_this_level)
414 continue;
415
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200416 age = MAX3(timespec_load(&s.st_mtim),
417 timespec_load(&s.st_atim),
418 timespec_load(&s.st_ctim));
419
420 if (age >= cutoff)
421 continue;
422
Lennart Poettering9f6445e32013-12-24 16:39:37 +0100423 log_debug("unlink '%s'", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200424
425 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
426 if (errno != ENOENT) {
427 log_error("unlink(%s): %m", sub_path);
428 r = -errno;
429 }
430 }
431
432 deleted = true;
433 }
434 }
435
436finish:
437 if (deleted) {
438 /* Restore original directory timestamps */
439 times[0] = ds->st_atim;
440 times[1] = ds->st_mtim;
441
442 if (futimens(dirfd(d), times) < 0)
443 log_error("utimensat(%s): %m", p);
444 }
445
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200446 return r;
447}
448
Lennart Poettering265ffa12013-09-17 16:33:30 -0500449static int item_set_perms_full(Item *i, const char *path, bool ignore_enoent) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200450 assert(i);
451 assert(path);
452
Michal Schmidt062e01b2011-12-16 18:00:11 +0100453 /* not using i->path directly because it may be a glob */
454 if (i->mode_set)
455 if (chmod(path, i->mode) < 0) {
Lennart Poettering265ffa12013-09-17 16:33:30 -0500456 if (errno != ENOENT || !ignore_enoent) {
457 log_error("chmod(%s) failed: %m", path);
458 return -errno;
459 }
Michal Schmidt062e01b2011-12-16 18:00:11 +0100460 }
461
462 if (i->uid_set || i->gid_set)
463 if (chown(path,
464 i->uid_set ? i->uid : (uid_t) -1,
465 i->gid_set ? i->gid : (gid_t) -1) < 0) {
466
Lennart Poettering265ffa12013-09-17 16:33:30 -0500467 if (errno != ENOENT || !ignore_enoent) {
468 log_error("chown(%s) failed: %m", path);
469 return -errno;
470 }
Michal Schmidt062e01b2011-12-16 18:00:11 +0100471 }
472
Lukas Nykrynf58ceb22014-01-09 18:00:50 +0100473 return label_fix(path, ignore_enoent, false);
Lennart Poettering265ffa12013-09-17 16:33:30 -0500474}
475
476static int item_set_perms(Item *i, const char *path) {
477 return item_set_perms_full(i, path, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100478}
479
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400480static int write_one_file(Item *i, const char *path) {
Lennart Poettering874f1942014-06-10 22:48:56 +0200481 int flags;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200482 int fd = -1;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400483 struct stat st;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200484 int r = 0;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400485
Lennart Poettering874f1942014-06-10 22:48:56 +0200486 assert(i);
487 assert(path);
488
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400489 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
490 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
491
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200492 RUN_WITH_UMASK(0) {
493 label_context_set(path, S_IFREG);
494 fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200495 label_context_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200496 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400497
498 if (fd < 0) {
499 if (i->type == WRITE_FILE && errno == ENOENT)
500 return 0;
501
502 log_error("Failed to create file %s: %m", path);
503 return -errno;
504 }
505
506 if (i->argument) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200507 _cleanup_free_ char *unescaped;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400508 ssize_t n;
509 size_t l;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400510
Dave Reisner54693d92012-09-15 12:58:49 -0400511 unescaped = cunescape(i->argument);
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100512 if (unescaped == NULL) {
Lennart Poettering03e334a2014-03-18 19:22:43 +0100513 safe_close(fd);
Dave Reisner54693d92012-09-15 12:58:49 -0400514 return log_oom();
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100515 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400516
Dave Reisner54693d92012-09-15 12:58:49 -0400517 l = strlen(unescaped);
518 n = write(fd, unescaped, l);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400519
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400520 if (n < 0 || (size_t) n < l) {
521 log_error("Failed to write file %s: %s", path, n < 0 ? strerror(-n) : "Short write");
Lennart Poettering03e334a2014-03-18 19:22:43 +0100522 safe_close(fd);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400523 return n < 0 ? n : -EIO;
524 }
525 }
526
Lennart Poettering03e334a2014-03-18 19:22:43 +0100527 safe_close(fd);
Dave Reisner3612fbc2012-09-12 16:21:00 -0400528
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400529 if (stat(path, &st) < 0) {
530 log_error("stat(%s) failed: %m", path);
531 return -errno;
532 }
533
534 if (!S_ISREG(st.st_mode)) {
535 log_error("%s is not a file.", path);
536 return -EEXIST;
537 }
538
539 r = item_set_perms(i, path);
540 if (r < 0)
541 return r;
542
543 return 0;
544}
545
Michal Schmidt062e01b2011-12-16 18:00:11 +0100546static int recursive_relabel_children(Item *i, const char *path) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200547 _cleanup_closedir_ DIR *d;
Michal Schmidta8d88782011-12-15 23:11:07 +0100548 int ret = 0;
549
550 /* This returns the first error we run into, but nevertheless
551 * tries to go on */
552
553 d = opendir(path);
554 if (!d)
555 return errno == ENOENT ? 0 : -errno;
556
557 for (;;) {
Lennart Poettering7d5e9c02012-09-19 22:21:09 +0200558 struct dirent *de;
Zbigniew Jędrzejewski-Szmek6cf487a2012-11-02 15:05:31 +0100559 bool dir;
Michal Schmidta8d88782011-12-15 23:11:07 +0100560 int r;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200561 _cleanup_free_ char *entry_path = NULL;
Michal Schmidta8d88782011-12-15 23:11:07 +0100562
Florian Weimerd78096b2013-12-19 12:26:07 +0100563 errno = 0;
564 de = readdir(d);
565 if (!de && errno != 0) {
Michal Schmidta8d88782011-12-15 23:11:07 +0100566 if (ret == 0)
Florian Weimerd78096b2013-12-19 12:26:07 +0100567 ret = -errno;
Michal Schmidta8d88782011-12-15 23:11:07 +0100568 break;
569 }
570
571 if (!de)
572 break;
573
574 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
575 continue;
576
577 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
578 if (ret == 0)
579 ret = -ENOMEM;
580 continue;
581 }
582
583 if (de->d_type == DT_UNKNOWN) {
Zbigniew Jędrzejewski-Szmek6cf487a2012-11-02 15:05:31 +0100584 r = is_dir(entry_path);
585 if (r < 0) {
Michal Schmidta8d88782011-12-15 23:11:07 +0100586 if (ret == 0 && errno != ENOENT)
587 ret = -errno;
Michal Schmidta8d88782011-12-15 23:11:07 +0100588 continue;
589 }
590
Zbigniew Jędrzejewski-Szmek6cf487a2012-11-02 15:05:31 +0100591 dir = r;
Michal Schmidta8d88782011-12-15 23:11:07 +0100592
593 } else
Zbigniew Jędrzejewski-Szmek6cf487a2012-11-02 15:05:31 +0100594 dir = de->d_type == DT_DIR;
Michal Schmidta8d88782011-12-15 23:11:07 +0100595
Michal Schmidt062e01b2011-12-16 18:00:11 +0100596 r = item_set_perms(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100597 if (r < 0) {
598 if (ret == 0 && r != -ENOENT)
599 ret = r;
Michal Schmidta8d88782011-12-15 23:11:07 +0100600 continue;
601 }
602
Zbigniew Jędrzejewski-Szmek6cf487a2012-11-02 15:05:31 +0100603 if (dir) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100604 r = recursive_relabel_children(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100605 if (r < 0 && ret == 0)
606 ret = r;
607 }
Michal Schmidta8d88782011-12-15 23:11:07 +0100608 }
609
Michal Schmidta8d88782011-12-15 23:11:07 +0100610 return ret;
611}
612
613static int recursive_relabel(Item *i, const char *path) {
614 int r;
615 struct stat st;
616
Michal Schmidt062e01b2011-12-16 18:00:11 +0100617 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100618 if (r < 0)
619 return r;
620
621 if (lstat(path, &st) < 0)
622 return -errno;
623
624 if (S_ISDIR(st.st_mode))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100625 r = recursive_relabel_children(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100626
627 return r;
628}
629
Michal Schmidt99e68c02011-12-15 23:45:26 +0100630static int glob_item(Item *i, int (*action)(Item *, const char *)) {
631 int r = 0, k;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200632 _cleanup_globfree_ glob_t g = {};
Michal Schmidt99e68c02011-12-15 23:45:26 +0100633 char **fn;
634
Michal Schmidt99e68c02011-12-15 23:45:26 +0100635 errno = 0;
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400636 k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
637 if (k != 0)
Michal Schmidt99e68c02011-12-15 23:45:26 +0100638 if (k != GLOB_NOMATCH) {
Zbigniew Jędrzejewski-Szmek8333c772013-03-28 09:24:15 -0400639 if (errno > 0)
Michal Schmidt99e68c02011-12-15 23:45:26 +0100640 errno = EIO;
641
642 log_error("glob(%s) failed: %m", i->path);
643 return -errno;
644 }
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400645
646 STRV_FOREACH(fn, g.gl_pathv) {
647 k = action(i, *fn);
648 if (k < 0)
649 r = k;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100650 }
651
Michal Schmidt99e68c02011-12-15 23:45:26 +0100652 return r;
653}
654
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200655static int create_item(Item *i) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200656 struct stat st;
Kay Sieversdf28bc02013-10-21 15:24:18 +0200657 int r = 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200658
659 assert(i);
660
661 switch (i->type) {
662
663 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100664 case IGNORE_DIRECTORY_PATH:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200665 case REMOVE_PATH:
666 case RECURSIVE_REMOVE_PATH:
667 return 0;
668
669 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100670 case TRUNCATE_FILE:
Dave Reisner1845fdd2012-09-27 20:48:13 -0400671 r = write_one_file(i, i->path);
672 if (r < 0)
673 return r;
674 break;
Lennart Poettering265ffa12013-09-17 16:33:30 -0500675
Lennart Poettering849958d2014-06-10 23:02:40 +0200676 case COPY_FILES:
677 r = copy_tree(i->argument, i->path);
678 if (r < 0) {
679 log_error("Failed to copy files: %s", strerror(-r));
680 return r;
681 }
682
683 r = item_set_perms(i, i->path);
684 if (r < 0)
685 return r;
686
687 break;
688
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400689 case WRITE_FILE:
690 r = glob_item(i, write_one_file);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100691 if (r < 0)
692 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200693
694 break;
695
Lennart Poettering265ffa12013-09-17 16:33:30 -0500696 case ADJUST_MODE:
697 r = item_set_perms_full(i, i->path, true);
698 if (r < 0)
699 return r;
700
701 break;
702
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200703 case TRUNCATE_DIRECTORY:
704 case CREATE_DIRECTORY:
705
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200706 RUN_WITH_UMASK(0000) {
707 mkdir_parents_label(i->path, 0755);
708 r = mkdir(i->path, i->mode);
709 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200710
711 if (r < 0 && errno != EEXIST) {
712 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100713 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200714 }
715
716 if (stat(i->path, &st) < 0) {
717 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100718 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200719 }
720
721 if (!S_ISDIR(st.st_mode)) {
722 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100723 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200724 }
725
Michal Schmidt062e01b2011-12-16 18:00:11 +0100726 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100727 if (r < 0)
728 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200729
730 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200731
732 case CREATE_FIFO:
733
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200734 RUN_WITH_UMASK(0000) {
735 r = mkfifo(i->path, i->mode);
736 }
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200737
738 if (r < 0 && errno != EEXIST) {
739 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100740 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200741 }
742
743 if (stat(i->path, &st) < 0) {
744 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100745 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200746 }
747
748 if (!S_ISFIFO(st.st_mode)) {
749 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100750 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200751 }
752
Michal Schmidt062e01b2011-12-16 18:00:11 +0100753 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100754 if (r < 0)
755 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200756
757 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100758
Lennart Poettering468d7262012-01-17 15:04:12 +0100759 case CREATE_SYMLINK: {
Lennart Poetteringe26da2d2014-02-19 17:52:41 +0100760 _cleanup_free_ char *x = NULL;
Lennart Poettering468d7262012-01-17 15:04:12 +0100761
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200762 label_context_set(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100763 r = symlink(i->argument, i->path);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200764 label_context_clear();
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200765
Lennart Poettering468d7262012-01-17 15:04:12 +0100766 if (r < 0 && errno != EEXIST) {
767 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
768 return -errno;
769 }
770
771 r = readlink_malloc(i->path, &x);
772 if (r < 0) {
773 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
774 return -errno;
775 }
776
777 if (!streq(i->argument, x)) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100778 log_error("%s is not the right symlinks.", i->path);
779 return -EEXIST;
780 }
781
Lennart Poettering468d7262012-01-17 15:04:12 +0100782 break;
783 }
784
785 case CREATE_BLOCK_DEVICE:
786 case CREATE_CHAR_DEVICE: {
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700787 mode_t file_type;
788
789 if (have_effective_cap(CAP_MKNOD) == 0) {
790 /* In a container we lack CAP_MKNOD. We
Anatol Pomozovab06eef2013-04-14 19:37:54 -0700791 shouldn't attempt to create the device node in
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700792 that case to avoid noise, and we don't support
793 virtualized devices in containers anyway. */
794
795 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
796 return 0;
797 }
798
799 file_type = (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +0100800
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200801 RUN_WITH_UMASK(0000) {
802 label_context_set(i->path, file_type);
803 r = mknod(i->path, i->mode | file_type, i->major_minor);
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200804 label_context_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200805 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100806
807 if (r < 0 && errno != EEXIST) {
808 log_error("Failed to create device node %s: %m", i->path);
809 return -errno;
810 }
811
812 if (stat(i->path, &st) < 0) {
813 log_error("stat(%s) failed: %m", i->path);
814 return -errno;
815 }
816
Michal Schmidte7aee752012-06-14 16:01:19 +0200817 if ((st.st_mode & S_IFMT) != file_type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100818 log_error("%s is not a device node.", i->path);
819 return -EEXIST;
820 }
821
822 r = item_set_perms(i, i->path);
823 if (r < 0)
824 return r;
825
826 break;
827 }
828
Michal Schmidt777b87e2011-12-16 18:27:35 +0100829 case RELABEL_PATH:
830
831 r = glob_item(i, item_set_perms);
832 if (r < 0)
Lennart Poettering96ca8192013-06-21 15:57:42 +0200833 return r;
Michal Schmidt777b87e2011-12-16 18:27:35 +0100834 break;
835
Michal Schmidta8d88782011-12-15 23:11:07 +0100836 case RECURSIVE_RELABEL_PATH:
837
838 r = glob_item(i, recursive_relabel);
839 if (r < 0)
840 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200841 }
842
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200843 log_debug("%s created successfully.", i->path);
844
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100845 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200846}
847
Michal Schmidta0896122011-12-15 21:32:50 +0100848static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200849 int r;
850
851 assert(i);
852
853 switch (i->type) {
854
855 case CREATE_FILE:
856 case TRUNCATE_FILE:
857 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200858 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100859 case CREATE_SYMLINK:
860 case CREATE_BLOCK_DEVICE:
861 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200862 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100863 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100864 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100865 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100866 case WRITE_FILE:
Lennart Poettering849958d2014-06-10 23:02:40 +0200867 case COPY_FILES:
Lennart Poettering265ffa12013-09-17 16:33:30 -0500868 case ADJUST_MODE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200869 break;
870
871 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100872 if (remove(instance) < 0 && errno != ENOENT) {
873 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200874 return -errno;
875 }
876
877 break;
878
879 case TRUNCATE_DIRECTORY:
880 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +0200881 /* FIXME: we probably should use dir_cleanup() here
882 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +0200883 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Lennart Poettering468d7262012-01-17 15:04:12 +0100884 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100885 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200886 return r;
887 }
888
889 break;
890 }
891
892 return 0;
893}
894
Michal Schmidta0896122011-12-15 21:32:50 +0100895static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100896 int r = 0;
897
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100898 assert(i);
899
900 switch (i->type) {
901
902 case CREATE_FILE:
903 case TRUNCATE_FILE:
904 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200905 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100906 case CREATE_SYMLINK:
907 case CREATE_CHAR_DEVICE:
908 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100909 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100910 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100911 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100912 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100913 case WRITE_FILE:
Lennart Poettering849958d2014-06-10 23:02:40 +0200914 case COPY_FILES:
Lennart Poettering265ffa12013-09-17 16:33:30 -0500915 case ADJUST_MODE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100916 break;
917
918 case REMOVE_PATH:
919 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100920 case RECURSIVE_REMOVE_PATH:
921 r = glob_item(i, remove_item_instance);
922 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100923 }
924
Michal Schmidt99e68c02011-12-15 23:45:26 +0100925 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100926}
927
Michal Sekletar78a92a52013-01-18 16:13:08 +0100928static int clean_item_instance(Item *i, const char* instance) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200929 _cleanup_closedir_ DIR *d = NULL;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100930 struct stat s, ps;
931 bool mountpoint;
932 int r;
933 usec_t cutoff, n;
934
935 assert(i);
936
937 if (!i->age_set)
938 return 0;
939
940 n = now(CLOCK_REALTIME);
941 if (n < i->age)
942 return 0;
943
944 cutoff = n - i->age;
945
946 d = opendir(instance);
947 if (!d) {
948 if (errno == ENOENT || errno == ENOTDIR)
949 return 0;
950
951 log_error("Failed to open directory %s: %m", i->path);
952 return -errno;
953 }
954
955 if (fstat(dirfd(d), &s) < 0) {
956 log_error("stat(%s) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500957 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100958 }
959
960 if (!S_ISDIR(s.st_mode)) {
961 log_error("%s is not a directory.", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500962 return -ENOTDIR;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100963 }
964
965 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
966 log_error("stat(%s/..) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500967 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100968 }
969
970 mountpoint = s.st_dev != ps.st_dev ||
971 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
972
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500973 r = dir_cleanup(i, instance, d, &s, cutoff, s.st_dev, mountpoint,
974 MAX_DEPTH, i->keep_first_level);
Michal Sekletar78a92a52013-01-18 16:13:08 +0100975 return r;
976}
977
978static int clean_item(Item *i) {
979 int r = 0;
980
981 assert(i);
982
983 switch (i->type) {
984 case CREATE_DIRECTORY:
985 case TRUNCATE_DIRECTORY:
986 case IGNORE_PATH:
Lennart Poettering849958d2014-06-10 23:02:40 +0200987 case COPY_FILES:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100988 clean_item_instance(i, i->path);
989 break;
990 case IGNORE_DIRECTORY_PATH:
991 r = glob_item(i, clean_item_instance);
992 break;
993 default:
994 break;
995 }
996
997 return r;
998}
999
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001000static int process_item(Item *i) {
1001 int r, q, p;
1002
1003 assert(i);
1004
1005 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +01001006 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001007 p = arg_clean ? clean_item(i) : 0;
1008
1009 if (r < 0)
1010 return r;
1011
1012 if (q < 0)
1013 return q;
1014
1015 return p;
1016}
1017
1018static void item_free(Item *i) {
1019 assert(i);
1020
1021 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +01001022 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001023 free(i);
1024}
1025
Lennart Poettering14bf2c92013-10-14 04:59:26 +02001026DEFINE_TRIVIAL_CLEANUP_FUNC(Item*, item_free);
Maciej Wereskie2f2fb72013-07-19 15:43:12 +02001027
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001028static bool item_equal(Item *a, Item *b) {
1029 assert(a);
1030 assert(b);
1031
1032 if (!streq_ptr(a->path, b->path))
1033 return false;
1034
1035 if (a->type != b->type)
1036 return false;
1037
1038 if (a->uid_set != b->uid_set ||
1039 (a->uid_set && a->uid != b->uid))
1040 return false;
1041
1042 if (a->gid_set != b->gid_set ||
1043 (a->gid_set && a->gid != b->gid))
1044 return false;
1045
1046 if (a->mode_set != b->mode_set ||
1047 (a->mode_set && a->mode != b->mode))
1048 return false;
1049
1050 if (a->age_set != b->age_set ||
1051 (a->age_set && a->age != b->age))
1052 return false;
1053
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001054 if ((a->type == CREATE_FILE ||
1055 a->type == TRUNCATE_FILE ||
1056 a->type == WRITE_FILE ||
Lennart Poettering849958d2014-06-10 23:02:40 +02001057 a->type == CREATE_SYMLINK ||
1058 a->type == COPY_FILES) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +01001059 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +01001060 return false;
1061
1062 if ((a->type == CREATE_CHAR_DEVICE ||
1063 a->type == CREATE_BLOCK_DEVICE) &&
1064 a->major_minor != b->major_minor)
1065 return false;
1066
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001067 return true;
1068}
1069
Dave Reisnera2aced42013-07-24 11:10:05 -04001070static bool should_include_path(const char *path) {
1071 char **prefix;
1072
Dave Reisner5c795112013-07-24 11:19:24 -04001073 STRV_FOREACH(prefix, exclude_prefixes) {
1074 if (path_startswith(path, *prefix))
1075 return false;
1076 }
Dave Reisnera2aced42013-07-24 11:10:05 -04001077
1078 STRV_FOREACH(prefix, include_prefixes) {
1079 if (path_startswith(path, *prefix))
1080 return true;
1081 }
1082
Dave Reisner5c795112013-07-24 11:19:24 -04001083 /* no matches, so we should include this path only if we
1084 * have no whitelist at all */
1085 return strv_length(include_prefixes) == 0;
Dave Reisnera2aced42013-07-24 11:10:05 -04001086}
1087
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001088static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001089
1090 static const Specifier specifier_table[] = {
1091 { 'm', specifier_machine_id, NULL },
1092 { 'b', specifier_boot_id, NULL },
1093 { 'H', specifier_host_name, NULL },
1094 { 'v', specifier_kernel_release, NULL },
1095 {}
1096 };
1097
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001098 _cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
1099 _cleanup_(item_freep) Item *i = NULL;
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001100 Item *existing;
Michal Schmidt66ccd032011-12-15 21:31:14 +01001101 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001102 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001103 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +02001104
1105 assert(fname);
1106 assert(line >= 1);
1107 assert(buffer);
1108
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001109 r = sscanf(buffer,
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001110 "%ms %ms %ms %ms %ms %ms %n",
1111 &action,
Lennart Poettering1731e342013-09-17 11:02:02 -05001112 &path,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +01001113 &mode,
1114 &user,
1115 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +01001116 &age,
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001117 &n);
1118 if (r < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001119 log_error("[%s:%u] Syntax error.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001120 return -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +02001121 }
1122
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001123 if (strlen(action) > 2 || (strlen(action) > 1 && action[1] != '!')) {
1124 log_error("[%s:%u] Unknown modifier '%s'", fname, line, action);
1125 return -EINVAL;
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001126 } else if (strlen(action) > 1 && !arg_boot)
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001127 return 0;
1128
1129 type = action[0];
1130
Lennart Poettering1731e342013-09-17 11:02:02 -05001131 i = new0(Item, 1);
1132 if (!i)
1133 return log_oom();
1134
1135 r = specifier_printf(path, specifier_table, NULL, &i->path);
1136 if (r < 0) {
1137 log_error("[%s:%u] Failed to replace specifiers: %s", fname, line, path);
1138 return r;
1139 }
1140
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001141 if (n >= 0) {
1142 n += strspn(buffer+n, WHITESPACE);
1143 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
1144 i->argument = unquote(buffer+n, "\"");
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001145 if (!i->argument)
1146 return log_oom();
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001147 }
1148 }
1149
Michal Schmidt777b87e2011-12-16 18:27:35 +01001150 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001151
Michal Schmidt777b87e2011-12-16 18:27:35 +01001152 case CREATE_FILE:
1153 case TRUNCATE_FILE:
1154 case CREATE_DIRECTORY:
1155 case TRUNCATE_DIRECTORY:
1156 case CREATE_FIFO:
1157 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001158 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001159 case REMOVE_PATH:
1160 case RECURSIVE_REMOVE_PATH:
1161 case RELABEL_PATH:
1162 case RECURSIVE_RELABEL_PATH:
Lennart Poettering265ffa12013-09-17 16:33:30 -05001163 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001164 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001165
1166 case CREATE_SYMLINK:
1167 if (!i->argument) {
1168 log_error("[%s:%u] Symlink file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001169 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001170 }
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001171
Lennart Poettering468d7262012-01-17 15:04:12 +01001172 break;
1173
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001174 case WRITE_FILE:
1175 if (!i->argument) {
1176 log_error("[%s:%u] Write file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001177 return -EBADMSG;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001178 }
1179 break;
1180
Lennart Poettering849958d2014-06-10 23:02:40 +02001181 case COPY_FILES:
1182 if (!i->argument) {
1183 log_error("[%s:%u] Copy files requires argument.", fname, line);
1184 return -EBADMSG;
1185 }
1186
1187 if (!path_is_absolute(i->argument)) {
1188 log_error("[%s:%u] Source path is not absolute.", fname, line);
1189 return -EBADMSG;
1190 }
1191
1192 path_kill_slashes(i->argument);
1193 break;
1194
Lennart Poettering468d7262012-01-17 15:04:12 +01001195 case CREATE_CHAR_DEVICE:
1196 case CREATE_BLOCK_DEVICE: {
1197 unsigned major, minor;
1198
1199 if (!i->argument) {
1200 log_error("[%s:%u] Device file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001201 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001202 }
1203
1204 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1205 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 -04001206 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001207 }
1208
1209 i->major_minor = makedev(major, minor);
1210 break;
1211 }
1212
Michal Schmidt777b87e2011-12-16 18:27:35 +01001213 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001214 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001215 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001216 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001217
Michal Schmidta8d88782011-12-15 23:11:07 +01001218 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001219
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001220 if (!path_is_absolute(i->path)) {
1221 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001222 return -EBADMSG;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001223 }
1224
1225 path_kill_slashes(i->path);
1226
Dave Reisnera2aced42013-07-24 11:10:05 -04001227 if (!should_include_path(i->path))
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001228 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001229
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001230 if (arg_root) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001231 char *p;
1232
1233 p = strappend(arg_root, i->path);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001234 if (!p)
1235 return log_oom();
1236
1237 free(i->path);
1238 i->path = p;
1239 }
1240
Lennart Poettering5008d582010-09-28 02:34:02 +02001241 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001242 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001243
Lennart Poetteringd05c5032012-07-16 12:34:54 +02001244 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001245 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001246 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001247 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001248 }
1249
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001250 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001251 }
1252
1253 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001254 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001255
Lennart Poettering4b678342011-07-23 01:17:59 +02001256 r = get_group_creds(&g, &i->gid);
1257 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001258 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001259 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001260 }
1261
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001262 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001263 }
1264
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001265 if (mode && !streq(mode, "-")) {
1266 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001267
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001268 if (sscanf(mode, "%o", &m) != 1) {
1269 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001270 return -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001271 }
1272
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001273 i->mode = m;
1274 i->mode_set = true;
1275 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001276 i->mode =
1277 i->type == CREATE_DIRECTORY ||
1278 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001279
1280 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001281 const char *a = age;
1282
1283 if (*a == '~') {
1284 i->keep_first_level = true;
1285 a++;
1286 }
1287
Lennart Poettering7f602782013-04-02 20:38:16 +02001288 if (parse_sec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001289 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001290 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001291 }
1292
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001293 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001294 }
1295
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001296 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001297
Lennart Poettering468d7262012-01-17 15:04:12 +01001298 existing = hashmap_get(h, i->path);
1299 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001300
1301 /* Two identical items are fine */
1302 if (!item_equal(existing, i))
1303 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1304
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001305 return 0;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001306 }
1307
Lennart Poettering468d7262012-01-17 15:04:12 +01001308 r = hashmap_put(h, i->path, i);
1309 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001310 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001311 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001312 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001313
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001314 i = NULL; /* avoid cleanup */
Lennart Poettering5008d582010-09-28 02:34:02 +02001315
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001316 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001317}
1318
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001319static int help(void) {
1320
Lennart Poettering522d4a42011-02-13 15:08:15 +01001321 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1322 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001323 " -h --help Show this help\n"
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001324 " --version Show package version\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001325 " --create Create marked files/directories\n"
1326 " --clean Clean up marked directories\n"
1327 " --remove Remove marked files/directories\n"
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001328 " --boot Execute actions only safe at boot\n"
Dave Reisner5c795112013-07-24 11:19:24 -04001329 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n"
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001330 " --exclude-prefix=PATH Ignore rules that apply to paths with the specified prefix\n"
1331 " --root=PATH Operate on an alternate filesystem root\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001332 program_invocation_short_name);
1333
1334 return 0;
1335}
1336
1337static int parse_argv(int argc, char *argv[]) {
1338
1339 enum {
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001340 ARG_VERSION = 0x100,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001341 ARG_CREATE,
1342 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001343 ARG_REMOVE,
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001344 ARG_BOOT,
Dave Reisner5c795112013-07-24 11:19:24 -04001345 ARG_PREFIX,
1346 ARG_EXCLUDE_PREFIX,
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001347 ARG_ROOT,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001348 };
1349
1350 static const struct option options[] = {
Dave Reisner5c795112013-07-24 11:19:24 -04001351 { "help", no_argument, NULL, 'h' },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001352 { "version", no_argument, NULL, ARG_VERSION },
Dave Reisner5c795112013-07-24 11:19:24 -04001353 { "create", no_argument, NULL, ARG_CREATE },
1354 { "clean", no_argument, NULL, ARG_CLEAN },
1355 { "remove", no_argument, NULL, ARG_REMOVE },
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001356 { "boot", no_argument, NULL, ARG_BOOT },
Dave Reisner5c795112013-07-24 11:19:24 -04001357 { "prefix", required_argument, NULL, ARG_PREFIX },
1358 { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX },
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001359 { "root", required_argument, NULL, ARG_ROOT },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001360 {}
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001361 };
1362
1363 int c;
1364
1365 assert(argc >= 0);
1366 assert(argv);
1367
1368 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1369
1370 switch (c) {
1371
1372 case 'h':
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001373 return help();
1374
1375 case ARG_VERSION:
1376 puts(PACKAGE_STRING);
1377 puts(SYSTEMD_FEATURES);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001378 return 0;
1379
1380 case ARG_CREATE:
1381 arg_create = true;
1382 break;
1383
1384 case ARG_CLEAN:
1385 arg_clean = true;
1386 break;
1387
1388 case ARG_REMOVE:
1389 arg_remove = true;
1390 break;
1391
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05001392 case ARG_BOOT:
1393 arg_boot = true;
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001394 break;
1395
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001396 case ARG_PREFIX:
Zbigniew Jędrzejewski-Szmek498f8a32014-01-30 21:40:27 -05001397 if (strv_push(&include_prefixes, optarg) < 0)
Dave Reisnera2aced42013-07-24 11:10:05 -04001398 return log_oom();
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001399 break;
1400
Dave Reisner5c795112013-07-24 11:19:24 -04001401 case ARG_EXCLUDE_PREFIX:
Zbigniew Jędrzejewski-Szmek498f8a32014-01-30 21:40:27 -05001402 if (strv_push(&exclude_prefixes, optarg) < 0)
Dave Reisner5c795112013-07-24 11:19:24 -04001403 return log_oom();
1404 break;
1405
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001406 case ARG_ROOT:
1407 arg_root = path_make_absolute_cwd(optarg);
1408 if (!arg_root)
1409 return log_oom();
1410 path_kill_slashes(arg_root);
1411 break;
1412
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001413 case '?':
1414 return -EINVAL;
1415
1416 default:
Lennart Poetteringeb9da372013-11-06 18:28:39 +01001417 assert_not_reached("Unhandled option");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001418 }
1419 }
1420
1421 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001422 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001423 return -EINVAL;
1424 }
1425
1426 return 1;
1427}
1428
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001429static int read_config_file(const char *fn, bool ignore_enoent) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001430 _cleanup_fclose_ FILE *f = NULL;
1431 char line[LINE_MAX];
Michal Sekletar78a92a52013-01-18 16:13:08 +01001432 Iterator iterator;
Lennart Poettering1731e342013-09-17 11:02:02 -05001433 unsigned v = 0;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001434 Item *i;
Lennart Poettering1731e342013-09-17 11:02:02 -05001435 int r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001436
1437 assert(fn);
1438
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001439 r = search_and_fopen_nulstr(fn, "re", arg_root, conf_file_dirs, &f);
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001440 if (r < 0) {
1441 if (ignore_enoent && r == -ENOENT)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001442 return 0;
1443
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001444 log_error("Failed to open '%s', ignoring: %s", fn, strerror(-r));
1445 return r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001446 }
1447
Lennart Poettering1731e342013-09-17 11:02:02 -05001448 FOREACH_LINE(line, f, break) {
1449 char *l;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001450 int k;
1451
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001452 v++;
1453
1454 l = strstrip(line);
1455 if (*l == '#' || *l == 0)
1456 continue;
1457
Lennart Poettering1731e342013-09-17 11:02:02 -05001458 k = parse_line(fn, v, l);
1459 if (k < 0 && r == 0)
1460 r = k;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001461 }
1462
Michal Sekletar78a92a52013-01-18 16:13:08 +01001463 /* we have to determine age parameter for each entry of type X */
1464 HASHMAP_FOREACH(i, globs, iterator) {
1465 Iterator iter;
1466 Item *j, *candidate_item = NULL;
1467
1468 if (i->type != IGNORE_DIRECTORY_PATH)
1469 continue;
1470
1471 HASHMAP_FOREACH(j, items, iter) {
1472 if (j->type != CREATE_DIRECTORY && j->type != TRUNCATE_DIRECTORY)
1473 continue;
1474
1475 if (path_equal(j->path, i->path)) {
1476 candidate_item = j;
1477 break;
1478 }
1479
1480 if ((!candidate_item && path_startswith(i->path, j->path)) ||
1481 (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
1482 candidate_item = j;
1483 }
1484
1485 if (candidate_item) {
1486 i->age = candidate_item->age;
1487 i->age_set = true;
1488 }
1489 }
1490
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001491 if (ferror(f)) {
1492 log_error("Failed to read from file %s: %m", fn);
1493 if (r == 0)
1494 r = -EIO;
1495 }
1496
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001497 return r;
1498}
1499
Lennart Poettering5008d582010-09-28 02:34:02 +02001500int main(int argc, char *argv[]) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001501 int r, k;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001502 Item *i;
1503 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001504
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001505 r = parse_argv(argc, argv);
1506 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001507 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1508
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001509 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001510 log_parse_environment();
1511 log_open();
1512
Lennart Poettering4c126262011-08-01 20:52:18 +02001513 umask(0022);
1514
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001515 label_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001516
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001517 items = hashmap_new(string_hash_func, string_compare_func);
1518 globs = hashmap_new(string_hash_func, string_compare_func);
1519
1520 if (!items || !globs) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001521 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001522 goto finish;
1523 }
1524
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001525 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001526
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001527 if (optind < argc) {
1528 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001529
Dave Reisner91256702012-06-08 22:31:19 -04001530 for (j = optind; j < argc; j++) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001531 k = read_config_file(argv[j], false);
1532 if (k < 0 && r == 0)
1533 r = k;
Dave Reisner91256702012-06-08 22:31:19 -04001534 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001535
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001536 } else {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001537 _cleanup_strv_free_ char **files = NULL;
1538 char **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001539
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001540 r = conf_files_list_nulstr(&files, ".conf", arg_root, conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001541 if (r < 0) {
Kay Sievers44143302011-04-28 23:51:24 +02001542 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
1543 goto finish;
1544 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001545
Kay Sievers772f8372011-04-25 21:38:21 +02001546 STRV_FOREACH(f, files) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001547 k = read_config_file(*f, true);
1548 if (k < 0 && r == 0)
1549 r = k;
Lennart Poettering5008d582010-09-28 02:34:02 +02001550 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001551 }
1552
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001553 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001554 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001555
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001556 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001557 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001558
Lennart Poettering5008d582010-09-28 02:34:02 +02001559finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001560 while ((i = hashmap_steal_first(items)))
1561 item_free(i);
1562
Lennart Poettering17b90522011-02-14 21:55:06 +01001563 while ((i = hashmap_steal_first(globs)))
1564 item_free(i);
1565
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001566 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001567 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001568
Zbigniew Jędrzejewski-Szmek498f8a32014-01-30 21:40:27 -05001569 free(include_prefixes);
1570 free(exclude_prefixes);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001571 free(arg_root);
Dave Reisnera2aced42013-07-24 11:10:05 -04001572
Lennart Poettering17b90522011-02-14 21:55:06 +01001573 set_free_free(unix_sockets);
1574
Lennart Poettering29003cf2010-10-19 19:36:45 +02001575 label_finish();
1576
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001577 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Lennart Poettering5008d582010-09-28 02:34:02 +02001578}