blob: 614644a0abe59fadaf7bbf8eb378e6abf233f2ff [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 Poettering5008d582010-09-28 02:34:02 +020054
Andreas Jaeger01000472010-09-29 10:08:24 +020055/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020056 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020057 * properly owned directories beneath /tmp, /var/tmp, /run, which are
58 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020059
Michal Schmidt66ccd032011-12-15 21:31:14 +010060typedef enum ItemType {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010061 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020062 CREATE_FILE = 'f',
63 TRUNCATE_FILE = 'F',
Lennart Poettering31ed59c2012-01-18 16:39:04 +010064 WRITE_FILE = 'w',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020065 CREATE_DIRECTORY = 'd',
66 TRUNCATE_DIRECTORY = 'D',
Lennart Poetteringee17ee72011-07-12 03:56:56 +020067 CREATE_FIFO = 'p',
Lennart Poettering468d7262012-01-17 15:04:12 +010068 CREATE_SYMLINK = 'L',
69 CREATE_CHAR_DEVICE = 'c',
70 CREATE_BLOCK_DEVICE = 'b',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010071
72 /* These ones take globs */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020073 IGNORE_PATH = 'x',
Michal Sekletar78a92a52013-01-18 16:13:08 +010074 IGNORE_DIRECTORY_PATH = 'X',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020075 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010076 RECURSIVE_REMOVE_PATH = 'R',
Michal Schmidt777b87e2011-12-16 18:27:35 +010077 RELABEL_PATH = 'z',
Michal Schmidta8d88782011-12-15 23:11:07 +010078 RECURSIVE_RELABEL_PATH = 'Z'
Michal Schmidt66ccd032011-12-15 21:31:14 +010079} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020080
81typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +010082 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020083
84 char *path;
Lennart Poettering468d7262012-01-17 15:04:12 +010085 char *argument;
Lennart Poettering5008d582010-09-28 02:34:02 +020086 uid_t uid;
87 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020088 mode_t mode;
89 usec_t age;
90
Lennart Poettering468d7262012-01-17 15:04:12 +010091 dev_t major_minor;
92
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020093 bool uid_set:1;
94 bool gid_set:1;
95 bool mode_set:1;
96 bool age_set:1;
Lennart Poettering24f3a372012-06-20 09:05:50 +020097
98 bool keep_first_level:1;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020099} Item;
100
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100101static Hashmap *items = NULL, *globs = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100102static Set *unix_sockets = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200103
104static bool arg_create = false;
105static bool arg_clean = false;
106static bool arg_remove = false;
107
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100108static const char *arg_prefix = NULL;
109
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100110static const char conf_file_dirs[] =
111 "/etc/tmpfiles.d\0"
112 "/run/tmpfiles.d\0"
113 "/usr/local/lib/tmpfiles.d\0"
114 "/usr/lib/tmpfiles.d\0"
Lennart Poettering3f2afb22012-07-20 16:24:55 +0200115#ifdef HAVE_SPLIT_USR
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100116 "/lib/tmpfiles.d\0"
Lennart Poettering3f2afb22012-07-20 16:24:55 +0200117#endif
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100118 ;
Dave Reisner91256702012-06-08 22:31:19 -0400119
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200120#define MAX_DEPTH 256
121
Michal Schmidt66ccd032011-12-15 21:31:14 +0100122static bool needs_glob(ItemType t) {
Michal Sekletar78a92a52013-01-18 16:13:08 +0100123 return t == IGNORE_PATH || t == IGNORE_DIRECTORY_PATH || t == REMOVE_PATH || t == RECURSIVE_REMOVE_PATH || t == RELABEL_PATH || t == RECURSIVE_RELABEL_PATH;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100124}
125
126static struct Item* find_glob(Hashmap *h, const char *match) {
127 Item *j;
128 Iterator i;
129
130 HASHMAP_FOREACH(j, h, i)
131 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
132 return j;
133
134 return NULL;
135}
136
Lennart Poettering17b90522011-02-14 21:55:06 +0100137static void load_unix_sockets(void) {
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500138 FILE _cleanup_fclose_ *f = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100139 char line[LINE_MAX];
140
141 if (unix_sockets)
142 return;
143
144 /* We maintain a cache of the sockets we found in
145 * /proc/net/unix to speed things up a little. */
146
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100147 unix_sockets = set_new(string_hash_func, string_compare_func);
148 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100149 return;
150
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100151 f = fopen("/proc/net/unix", "re");
152 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100153 return;
154
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100155 /* Skip header */
156 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100157 goto fail;
158
159 for (;;) {
160 char *p, *s;
161 int k;
162
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100163 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100164 break;
165
166 truncate_nl(line);
167
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100168 p = strchr(line, ':');
169 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100170 continue;
171
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100172 if (strlen(p) < 37)
173 continue;
174
175 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100176 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100177 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100178 p += strspn(p, WHITESPACE);
179
180 if (*p != '/')
181 continue;
182
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100183 s = strdup(p);
184 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100185 goto fail;
186
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100187 path_kill_slashes(s);
188
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100189 k = set_put(unix_sockets, s);
190 if (k < 0) {
Lennart Poettering17b90522011-02-14 21:55:06 +0100191 free(s);
192
193 if (k != -EEXIST)
194 goto fail;
195 }
196 }
197
198 return;
199
200fail:
201 set_free_free(unix_sockets);
202 unix_sockets = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100203}
204
205static bool unix_socket_alive(const char *fn) {
206 assert(fn);
207
208 load_unix_sockets();
209
210 if (unix_sockets)
211 return !!set_get(unix_sockets, (char*) fn);
212
213 /* We don't know, so assume yes */
214 return true;
215}
216
Kay Sievers99d680a2013-03-13 13:12:36 +0100217static int dir_is_mount_point(DIR *d, const char *subdir) {
218 struct file_handle *h;
219 int mount_id_parent, mount_id;
220 int r_p, r;
221
222 h = alloca(MAX_HANDLE_SZ);
223
224 h->handle_bytes = MAX_HANDLE_SZ;
225 r_p = name_to_handle_at(dirfd(d), ".", h, &mount_id_parent, 0);
226 if (r_p < 0)
227 r_p = -errno;
228
229 h->handle_bytes = MAX_HANDLE_SZ;
230 r = name_to_handle_at(dirfd(d), subdir, h, &mount_id, 0);
231 if (r < 0)
232 r = -errno;
233
234 /* got no handle; make no assumptions, return error */
235 if (r_p < 0 && r < 0)
236 return r_p;
237
238 /* got both handles; if they differ, it is a mount point */
239 if (r_p >= 0 && r >= 0)
240 return mount_id_parent != mount_id;
241
242 /* got only one handle; assume different mount points if one
243 * of both queries was not supported by the filesystem */
244 if (r_p == -ENOSYS || r_p == -ENOTSUP || r == -ENOSYS || r == -ENOTSUP)
245 return true;
246
247 /* return error */
248 if (r_p < 0)
249 return r_p;
250 return r;
251}
252
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200253static int dir_cleanup(
Michal Sekletar78a92a52013-01-18 16:13:08 +0100254 Item *i,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200255 const char *p,
256 DIR *d,
257 const struct stat *ds,
258 usec_t cutoff,
259 dev_t rootdev,
260 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200261 int maxdepth,
262 bool keep_this_level)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200263{
264 struct dirent *dent;
265 struct timespec times[2];
266 bool deleted = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200267 int r = 0;
268
269 while ((dent = readdir(d))) {
270 struct stat s;
271 usec_t age;
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500272 char _cleanup_free_ *sub_path = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200273
274 if (streq(dent->d_name, ".") ||
275 streq(dent->d_name, ".."))
276 continue;
277
278 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
279
280 if (errno != ENOENT) {
281 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
282 r = -errno;
283 }
284
285 continue;
286 }
287
288 /* Stay on the same filesystem */
289 if (s.st_dev != rootdev)
290 continue;
291
Kay Sievers99d680a2013-03-13 13:12:36 +0100292 /* Try to detect bind mounts of the same filesystem instance; they
293 * do not differ in device major/minors. This type of query is not
294 * supported on all kernels or filesystem types though. */
295 if (S_ISDIR(s.st_mode) && dir_is_mount_point(d, dent->d_name) > 0)
296 continue;
297
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200298 /* Do not delete read-only files owned by root */
299 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
300 continue;
301
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200302 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700303 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200304 goto finish;
305 }
306
307 /* Is there an item configured for this path? */
308 if (hashmap_get(items, sub_path))
309 continue;
310
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100311 if (find_glob(globs, sub_path))
312 continue;
313
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200314 if (S_ISDIR(s.st_mode)) {
315
316 if (mountpoint &&
317 streq(dent->d_name, "lost+found") &&
318 s.st_uid == 0)
319 continue;
320
321 if (maxdepth <= 0)
322 log_warning("Reached max depth on %s.", sub_path);
323 else {
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500324 DIR _cleanup_closedir_ *sub_dir;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200325 int q;
326
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200327 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200328 if (sub_dir == NULL) {
329 if (errno != ENOENT) {
330 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
331 r = -errno;
332 }
333
334 continue;
335 }
336
Michal Sekletar78a92a52013-01-18 16:13:08 +0100337 q = dir_cleanup(i, sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200338
339 if (q < 0)
340 r = q;
341 }
342
Lennart Poettering24f3a372012-06-20 09:05:50 +0200343 /* Note: if you are wondering why we don't
344 * support the sticky bit for excluding
345 * directories from cleaning like we do it for
346 * other file system objects: well, the sticky
347 * bit already has a meaning for directories,
348 * so we don't want to overload that. */
349
350 if (keep_this_level)
351 continue;
352
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200353 /* Ignore ctime, we change it when deleting */
354 age = MAX(timespec_load(&s.st_mtim),
355 timespec_load(&s.st_atim));
356 if (age >= cutoff)
357 continue;
358
Lukas Nykryna6187d42013-03-01 18:29:59 +0100359 if (i->type != IGNORE_DIRECTORY_PATH || !streq(dent->d_name, p)) {
Michal Sekletar78a92a52013-01-18 16:13:08 +0100360 log_debug("rmdir '%s'\n", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200361
Michal Sekletar78a92a52013-01-18 16:13:08 +0100362 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
363 if (errno != ENOENT && errno != ENOTEMPTY) {
364 log_error("rmdir(%s): %m", sub_path);
365 r = -errno;
366 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200367 }
368 }
369
370 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100371 /* Skip files for which the sticky bit is
372 * set. These are semantics we define, and are
373 * unknown elsewhere. See XDG_RUNTIME_DIR
374 * specification for details. */
375 if (s.st_mode & S_ISVTX)
376 continue;
377
Lennart Poettering17b90522011-02-14 21:55:06 +0100378 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200379 if (streq(dent->d_name, ".journal") &&
380 s.st_uid == 0)
381 continue;
382
383 if (streq(dent->d_name, "aquota.user") ||
384 streq(dent->d_name, "aquota.group"))
385 continue;
386 }
387
Lennart Poettering17b90522011-02-14 21:55:06 +0100388 /* Ignore sockets that are listed in /proc/net/unix */
389 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
390 continue;
391
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100392 /* Ignore device nodes */
393 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
394 continue;
395
Lennart Poettering24f3a372012-06-20 09:05:50 +0200396 /* Keep files on this level around if this is
397 * requested */
398 if (keep_this_level)
399 continue;
400
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200401 age = MAX3(timespec_load(&s.st_mtim),
402 timespec_load(&s.st_atim),
403 timespec_load(&s.st_ctim));
404
405 if (age >= cutoff)
406 continue;
407
408 log_debug("unlink '%s'\n", sub_path);
409
410 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
411 if (errno != ENOENT) {
412 log_error("unlink(%s): %m", sub_path);
413 r = -errno;
414 }
415 }
416
417 deleted = true;
418 }
419 }
420
421finish:
422 if (deleted) {
423 /* Restore original directory timestamps */
424 times[0] = ds->st_atim;
425 times[1] = ds->st_mtim;
426
427 if (futimens(dirfd(d), times) < 0)
428 log_error("utimensat(%s): %m", p);
429 }
430
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200431 return r;
432}
433
Michal Schmidt062e01b2011-12-16 18:00:11 +0100434static int item_set_perms(Item *i, const char *path) {
435 /* not using i->path directly because it may be a glob */
436 if (i->mode_set)
437 if (chmod(path, i->mode) < 0) {
438 log_error("chmod(%s) failed: %m", path);
439 return -errno;
440 }
441
442 if (i->uid_set || i->gid_set)
443 if (chown(path,
444 i->uid_set ? i->uid : (uid_t) -1,
445 i->gid_set ? i->gid : (gid_t) -1) < 0) {
446
447 log_error("chown(%s) failed: %m", path);
448 return -errno;
449 }
450
Lennart Poetteringc9bc0762012-07-03 16:25:50 +0200451 return label_fix(path, false, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100452}
453
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400454static int write_one_file(Item *i, const char *path) {
455 int r, e, fd, flags;
456 struct stat st;
457 mode_t u;
458
459 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
460 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
461
462 u = umask(0);
463 label_context_set(path, S_IFREG);
464 fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
465 e = errno;
466 label_context_clear();
467 umask(u);
468 errno = e;
469
470 if (fd < 0) {
471 if (i->type == WRITE_FILE && errno == ENOENT)
472 return 0;
473
474 log_error("Failed to create file %s: %m", path);
475 return -errno;
476 }
477
478 if (i->argument) {
479 ssize_t n;
480 size_t l;
Dave Reisner54693d92012-09-15 12:58:49 -0400481 _cleanup_free_ char *unescaped;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400482
Dave Reisner54693d92012-09-15 12:58:49 -0400483 unescaped = cunescape(i->argument);
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100484 if (unescaped == NULL) {
485 close_nointr_nofail(fd);
Dave Reisner54693d92012-09-15 12:58:49 -0400486 return log_oom();
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100487 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400488
Dave Reisner54693d92012-09-15 12:58:49 -0400489 l = strlen(unescaped);
490 n = write(fd, unescaped, l);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400491
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400492 if (n < 0 || (size_t) n < l) {
493 log_error("Failed to write file %s: %s", path, n < 0 ? strerror(-n) : "Short write");
494 close_nointr_nofail(fd);
495 return n < 0 ? n : -EIO;
496 }
497 }
498
Dave Reisner3612fbc2012-09-12 16:21:00 -0400499 close_nointr_nofail(fd);
500
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400501 if (stat(path, &st) < 0) {
502 log_error("stat(%s) failed: %m", path);
503 return -errno;
504 }
505
506 if (!S_ISREG(st.st_mode)) {
507 log_error("%s is not a file.", path);
508 return -EEXIST;
509 }
510
511 r = item_set_perms(i, path);
512 if (r < 0)
513 return r;
514
515 return 0;
516}
517
Michal Schmidt062e01b2011-12-16 18:00:11 +0100518static int recursive_relabel_children(Item *i, const char *path) {
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500519 DIR _cleanup_closedir_ *d;
Michal Schmidta8d88782011-12-15 23:11:07 +0100520 int ret = 0;
521
522 /* This returns the first error we run into, but nevertheless
523 * tries to go on */
524
525 d = opendir(path);
526 if (!d)
527 return errno == ENOENT ? 0 : -errno;
528
529 for (;;) {
Lennart Poettering7d5e9c02012-09-19 22:21:09 +0200530 struct dirent *de;
531 union dirent_storage buf;
Michal Schmidta8d88782011-12-15 23:11:07 +0100532 bool is_dir;
533 int r;
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500534 char _cleanup_free_ *entry_path = NULL;
Michal Schmidta8d88782011-12-15 23:11:07 +0100535
Lennart Poettering7d5e9c02012-09-19 22:21:09 +0200536 r = readdir_r(d, &buf.de, &de);
Michal Schmidta8d88782011-12-15 23:11:07 +0100537 if (r != 0) {
538 if (ret == 0)
539 ret = -r;
540 break;
541 }
542
543 if (!de)
544 break;
545
546 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
547 continue;
548
549 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
550 if (ret == 0)
551 ret = -ENOMEM;
552 continue;
553 }
554
555 if (de->d_type == DT_UNKNOWN) {
556 struct stat st;
557
558 if (lstat(entry_path, &st) < 0) {
559 if (ret == 0 && errno != ENOENT)
560 ret = -errno;
Michal Schmidta8d88782011-12-15 23:11:07 +0100561 continue;
562 }
563
564 is_dir = S_ISDIR(st.st_mode);
565
566 } else
567 is_dir = de->d_type == DT_DIR;
568
Michal Schmidt062e01b2011-12-16 18:00:11 +0100569 r = item_set_perms(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100570 if (r < 0) {
571 if (ret == 0 && r != -ENOENT)
572 ret = r;
Michal Schmidta8d88782011-12-15 23:11:07 +0100573 continue;
574 }
575
576 if (is_dir) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100577 r = recursive_relabel_children(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100578 if (r < 0 && ret == 0)
579 ret = r;
580 }
Michal Schmidta8d88782011-12-15 23:11:07 +0100581 }
582
Michal Schmidta8d88782011-12-15 23:11:07 +0100583 return ret;
584}
585
586static int recursive_relabel(Item *i, const char *path) {
587 int r;
588 struct stat st;
589
Michal Schmidt062e01b2011-12-16 18:00:11 +0100590 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100591 if (r < 0)
592 return r;
593
594 if (lstat(path, &st) < 0)
595 return -errno;
596
597 if (S_ISDIR(st.st_mode))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100598 r = recursive_relabel_children(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100599
600 return r;
601}
602
Michal Schmidt99e68c02011-12-15 23:45:26 +0100603static int glob_item(Item *i, int (*action)(Item *, const char *)) {
604 int r = 0, k;
605 glob_t g;
606 char **fn;
607
608 zero(g);
609
610 errno = 0;
611 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
612
613 if (k != GLOB_NOMATCH) {
614 if (errno != 0)
615 errno = EIO;
616
617 log_error("glob(%s) failed: %m", i->path);
618 return -errno;
619 }
620 }
621
622 STRV_FOREACH(fn, g.gl_pathv)
623 if ((k = action(i, *fn)) < 0)
624 r = k;
625
626 globfree(&g);
627 return r;
628}
629
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200630static int create_item(Item *i) {
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200631 int r, e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200632 mode_t u;
633 struct stat st;
634
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;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400651 case WRITE_FILE:
652 r = glob_item(i, write_one_file);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100653 if (r < 0)
654 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200655
656 break;
657
658 case TRUNCATE_DIRECTORY:
659 case CREATE_DIRECTORY:
660
661 u = umask(0);
Kay Sieversd2e54fa2012-05-31 12:40:20 +0200662 mkdir_parents_label(i->path, 0755);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200663 r = mkdir(i->path, i->mode);
664 umask(u);
665
666 if (r < 0 && errno != EEXIST) {
667 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100668 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200669 }
670
671 if (stat(i->path, &st) < 0) {
672 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100673 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200674 }
675
676 if (!S_ISDIR(st.st_mode)) {
677 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100678 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200679 }
680
Michal Schmidt062e01b2011-12-16 18:00:11 +0100681 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100682 if (r < 0)
683 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200684
685 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200686
687 case CREATE_FIFO:
688
689 u = umask(0);
690 r = mkfifo(i->path, i->mode);
691 umask(u);
692
693 if (r < 0 && errno != EEXIST) {
694 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100695 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200696 }
697
698 if (stat(i->path, &st) < 0) {
699 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100700 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200701 }
702
703 if (!S_ISFIFO(st.st_mode)) {
704 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100705 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200706 }
707
Michal Schmidt062e01b2011-12-16 18:00:11 +0100708 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100709 if (r < 0)
710 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200711
712 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100713
Lennart Poettering468d7262012-01-17 15:04:12 +0100714 case CREATE_SYMLINK: {
715 char *x;
716
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200717 label_context_set(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100718 r = symlink(i->argument, i->path);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200719 e = errno;
720 label_context_clear();
721 errno = e;
722
Lennart Poettering468d7262012-01-17 15:04:12 +0100723 if (r < 0 && errno != EEXIST) {
724 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
725 return -errno;
726 }
727
728 r = readlink_malloc(i->path, &x);
729 if (r < 0) {
730 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
731 return -errno;
732 }
733
734 if (!streq(i->argument, x)) {
735 free(x);
736 log_error("%s is not the right symlinks.", i->path);
737 return -EEXIST;
738 }
739
740 free(x);
741 break;
742 }
743
744 case CREATE_BLOCK_DEVICE:
745 case CREATE_CHAR_DEVICE: {
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700746 mode_t file_type;
747
748 if (have_effective_cap(CAP_MKNOD) == 0) {
749 /* In a container we lack CAP_MKNOD. We
750 shouldnt attempt to create the device node in
751 that case to avoid noise, and we don't support
752 virtualized devices in containers anyway. */
753
754 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
755 return 0;
756 }
757
758 file_type = (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +0100759
760 u = umask(0);
Michal Schmidte7aee752012-06-14 16:01:19 +0200761 label_context_set(i->path, file_type);
762 r = mknod(i->path, i->mode | file_type, i->major_minor);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200763 e = errno;
764 label_context_clear();
Lennart Poettering468d7262012-01-17 15:04:12 +0100765 umask(u);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200766 errno = e;
Lennart Poettering468d7262012-01-17 15:04:12 +0100767
768 if (r < 0 && errno != EEXIST) {
769 log_error("Failed to create device node %s: %m", i->path);
770 return -errno;
771 }
772
773 if (stat(i->path, &st) < 0) {
774 log_error("stat(%s) failed: %m", i->path);
775 return -errno;
776 }
777
Michal Schmidte7aee752012-06-14 16:01:19 +0200778 if ((st.st_mode & S_IFMT) != file_type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100779 log_error("%s is not a device node.", i->path);
780 return -EEXIST;
781 }
782
783 r = item_set_perms(i, i->path);
784 if (r < 0)
785 return r;
786
787 break;
788 }
789
Michal Schmidt777b87e2011-12-16 18:27:35 +0100790 case RELABEL_PATH:
791
792 r = glob_item(i, item_set_perms);
793 if (r < 0)
794 return 0;
795 break;
796
Michal Schmidta8d88782011-12-15 23:11:07 +0100797 case RECURSIVE_RELABEL_PATH:
798
799 r = glob_item(i, recursive_relabel);
800 if (r < 0)
801 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200802 }
803
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200804 log_debug("%s created successfully.", i->path);
805
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100806 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200807}
808
Michal Schmidta0896122011-12-15 21:32:50 +0100809static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200810 int r;
811
812 assert(i);
813
814 switch (i->type) {
815
816 case CREATE_FILE:
817 case TRUNCATE_FILE:
818 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200819 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100820 case CREATE_SYMLINK:
821 case CREATE_BLOCK_DEVICE:
822 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200823 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100824 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100825 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100826 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100827 case WRITE_FILE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200828 break;
829
830 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100831 if (remove(instance) < 0 && errno != ENOENT) {
832 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200833 return -errno;
834 }
835
836 break;
837
838 case TRUNCATE_DIRECTORY:
839 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +0200840 /* FIXME: we probably should use dir_cleanup() here
841 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +0200842 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Lennart Poettering468d7262012-01-17 15:04:12 +0100843 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100844 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200845 return r;
846 }
847
848 break;
849 }
850
851 return 0;
852}
853
Michal Schmidta0896122011-12-15 21:32:50 +0100854static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100855 int r = 0;
856
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100857 assert(i);
858
859 switch (i->type) {
860
861 case CREATE_FILE:
862 case TRUNCATE_FILE:
863 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200864 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100865 case CREATE_SYMLINK:
866 case CREATE_CHAR_DEVICE:
867 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100868 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100869 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100870 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100871 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100872 case WRITE_FILE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100873 break;
874
875 case REMOVE_PATH:
876 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100877 case RECURSIVE_REMOVE_PATH:
878 r = glob_item(i, remove_item_instance);
879 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100880 }
881
Michal Schmidt99e68c02011-12-15 23:45:26 +0100882 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100883}
884
Michal Sekletar78a92a52013-01-18 16:13:08 +0100885static int clean_item_instance(Item *i, const char* instance) {
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500886 DIR _cleanup_closedir_ *d = NULL;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100887 struct stat s, ps;
888 bool mountpoint;
889 int r;
890 usec_t cutoff, n;
891
892 assert(i);
893
894 if (!i->age_set)
895 return 0;
896
897 n = now(CLOCK_REALTIME);
898 if (n < i->age)
899 return 0;
900
901 cutoff = n - i->age;
902
903 d = opendir(instance);
904 if (!d) {
905 if (errno == ENOENT || errno == ENOTDIR)
906 return 0;
907
908 log_error("Failed to open directory %s: %m", i->path);
909 return -errno;
910 }
911
912 if (fstat(dirfd(d), &s) < 0) {
913 log_error("stat(%s) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500914 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100915 }
916
917 if (!S_ISDIR(s.st_mode)) {
918 log_error("%s is not a directory.", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500919 return -ENOTDIR;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100920 }
921
922 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
923 log_error("stat(%s/..) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500924 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100925 }
926
927 mountpoint = s.st_dev != ps.st_dev ||
928 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
929
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500930 r = dir_cleanup(i, instance, d, &s, cutoff, s.st_dev, mountpoint,
931 MAX_DEPTH, i->keep_first_level);
Michal Sekletar78a92a52013-01-18 16:13:08 +0100932 return r;
933}
934
935static int clean_item(Item *i) {
936 int r = 0;
937
938 assert(i);
939
940 switch (i->type) {
941 case CREATE_DIRECTORY:
942 case TRUNCATE_DIRECTORY:
943 case IGNORE_PATH:
944 clean_item_instance(i, i->path);
945 break;
946 case IGNORE_DIRECTORY_PATH:
947 r = glob_item(i, clean_item_instance);
948 break;
949 default:
950 break;
951 }
952
953 return r;
954}
955
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200956static int process_item(Item *i) {
957 int r, q, p;
958
959 assert(i);
960
961 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100962 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200963 p = arg_clean ? clean_item(i) : 0;
964
965 if (r < 0)
966 return r;
967
968 if (q < 0)
969 return q;
970
971 return p;
972}
973
974static void item_free(Item *i) {
975 assert(i);
976
977 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100978 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200979 free(i);
980}
981
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200982static bool item_equal(Item *a, Item *b) {
983 assert(a);
984 assert(b);
985
986 if (!streq_ptr(a->path, b->path))
987 return false;
988
989 if (a->type != b->type)
990 return false;
991
992 if (a->uid_set != b->uid_set ||
993 (a->uid_set && a->uid != b->uid))
994 return false;
995
996 if (a->gid_set != b->gid_set ||
997 (a->gid_set && a->gid != b->gid))
998 return false;
999
1000 if (a->mode_set != b->mode_set ||
1001 (a->mode_set && a->mode != b->mode))
1002 return false;
1003
1004 if (a->age_set != b->age_set ||
1005 (a->age_set && a->age != b->age))
1006 return false;
1007
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001008 if ((a->type == CREATE_FILE ||
1009 a->type == TRUNCATE_FILE ||
1010 a->type == WRITE_FILE ||
1011 a->type == CREATE_SYMLINK) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +01001012 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +01001013 return false;
1014
1015 if ((a->type == CREATE_CHAR_DEVICE ||
1016 a->type == CREATE_BLOCK_DEVICE) &&
1017 a->major_minor != b->major_minor)
1018 return false;
1019
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001020 return true;
1021}
1022
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001023static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001024 Item *i, *existing;
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001025 char _cleanup_free_
1026 *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +01001027 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001028 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001029 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +02001030
1031 assert(fname);
1032 assert(line >= 1);
1033 assert(buffer);
1034
Lennart Poettering468d7262012-01-17 15:04:12 +01001035 i = new0(Item, 1);
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001036 if (!i)
1037 return log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001038
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001039 r = sscanf(buffer,
1040 "%c %ms %ms %ms %ms %ms %n",
Michal Schmidt66ccd032011-12-15 21:31:14 +01001041 &type,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +01001042 &i->path,
1043 &mode,
1044 &user,
1045 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +01001046 &age,
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001047 &n);
1048 if (r < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001049 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001050 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +02001051 goto finish;
1052 }
1053
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001054 if (n >= 0) {
1055 n += strspn(buffer+n, WHITESPACE);
1056 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
1057 i->argument = unquote(buffer+n, "\"");
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001058 if (!i->argument)
1059 return log_oom();
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001060 }
1061 }
1062
Michal Schmidt777b87e2011-12-16 18:27:35 +01001063 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001064
Michal Schmidt777b87e2011-12-16 18:27:35 +01001065 case CREATE_FILE:
1066 case TRUNCATE_FILE:
1067 case CREATE_DIRECTORY:
1068 case TRUNCATE_DIRECTORY:
1069 case CREATE_FIFO:
1070 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001071 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001072 case REMOVE_PATH:
1073 case RECURSIVE_REMOVE_PATH:
1074 case RELABEL_PATH:
1075 case RECURSIVE_RELABEL_PATH:
1076 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001077
1078 case CREATE_SYMLINK:
1079 if (!i->argument) {
1080 log_error("[%s:%u] Symlink file requires argument.", fname, line);
1081 r = -EBADMSG;
1082 goto finish;
1083 }
1084 break;
1085
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001086 case WRITE_FILE:
1087 if (!i->argument) {
1088 log_error("[%s:%u] Write file requires argument.", fname, line);
1089 r = -EBADMSG;
1090 goto finish;
1091 }
1092 break;
1093
Lennart Poettering468d7262012-01-17 15:04:12 +01001094 case CREATE_CHAR_DEVICE:
1095 case CREATE_BLOCK_DEVICE: {
1096 unsigned major, minor;
1097
1098 if (!i->argument) {
1099 log_error("[%s:%u] Device file requires argument.", fname, line);
1100 r = -EBADMSG;
1101 goto finish;
1102 }
1103
1104 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1105 log_error("[%s:%u] Can't parse device file major/minor '%s'.", fname, line, i->argument);
1106 r = -EBADMSG;
1107 goto finish;
1108 }
1109
1110 i->major_minor = makedev(major, minor);
1111 break;
1112 }
1113
Michal Schmidt777b87e2011-12-16 18:27:35 +01001114 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001115 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001116 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001117 goto finish;
1118 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001119
Michal Schmidta8d88782011-12-15 23:11:07 +01001120 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001121
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001122 if (!path_is_absolute(i->path)) {
1123 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
1124 r = -EBADMSG;
1125 goto finish;
1126 }
1127
1128 path_kill_slashes(i->path);
1129
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001130 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001131 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001132 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001133 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001134
1135 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001136 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001137
Lennart Poetteringd05c5032012-07-16 12:34:54 +02001138 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001139 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001140 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
1141 goto finish;
1142 }
1143
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001144 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001145 }
1146
1147 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001148 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001149
Lennart Poettering4b678342011-07-23 01:17:59 +02001150 r = get_group_creds(&g, &i->gid);
1151 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001152 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
1153 goto finish;
1154 }
1155
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001156 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001157 }
1158
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001159 if (mode && !streq(mode, "-")) {
1160 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001161
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001162 if (sscanf(mode, "%o", &m) != 1) {
1163 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
1164 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001165 goto finish;
1166 }
1167
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001168 i->mode = m;
1169 i->mode_set = true;
1170 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001171 i->mode =
1172 i->type == CREATE_DIRECTORY ||
1173 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001174
1175 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001176 const char *a = age;
1177
1178 if (*a == '~') {
1179 i->keep_first_level = true;
1180 a++;
1181 }
1182
1183 if (parse_usec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001184 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
1185 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001186 goto finish;
1187 }
1188
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001189 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001190 }
1191
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001192 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001193
Lennart Poettering468d7262012-01-17 15:04:12 +01001194 existing = hashmap_get(h, i->path);
1195 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001196
1197 /* Two identical items are fine */
1198 if (!item_equal(existing, i))
1199 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1200
1201 r = 0;
1202 goto finish;
1203 }
1204
Lennart Poettering468d7262012-01-17 15:04:12 +01001205 r = hashmap_put(h, i->path, i);
1206 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001207 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001208 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001209 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001210
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001211 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001212 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001213
1214finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001215 if (i)
1216 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001217
1218 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001219}
1220
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001221static int help(void) {
1222
Lennart Poettering522d4a42011-02-13 15:08:15 +01001223 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1224 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001225 " -h --help Show this help\n"
1226 " --create Create marked files/directories\n"
1227 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001228 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +01001229 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001230 program_invocation_short_name);
1231
1232 return 0;
1233}
1234
1235static int parse_argv(int argc, char *argv[]) {
1236
1237 enum {
1238 ARG_CREATE,
1239 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001240 ARG_REMOVE,
1241 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001242 };
1243
1244 static const struct option options[] = {
1245 { "help", no_argument, NULL, 'h' },
1246 { "create", no_argument, NULL, ARG_CREATE },
1247 { "clean", no_argument, NULL, ARG_CLEAN },
1248 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001249 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001250 { NULL, 0, NULL, 0 }
1251 };
1252
1253 int c;
1254
1255 assert(argc >= 0);
1256 assert(argv);
1257
1258 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1259
1260 switch (c) {
1261
1262 case 'h':
1263 help();
1264 return 0;
1265
1266 case ARG_CREATE:
1267 arg_create = true;
1268 break;
1269
1270 case ARG_CLEAN:
1271 arg_clean = true;
1272 break;
1273
1274 case ARG_REMOVE:
1275 arg_remove = true;
1276 break;
1277
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001278 case ARG_PREFIX:
1279 arg_prefix = optarg;
1280 break;
1281
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001282 case '?':
1283 return -EINVAL;
1284
1285 default:
1286 log_error("Unknown option code %c", c);
1287 return -EINVAL;
1288 }
1289 }
1290
1291 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001292 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001293 return -EINVAL;
1294 }
1295
1296 return 1;
1297}
1298
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001299static int read_config_file(const char *fn, bool ignore_enoent) {
1300 FILE *f;
1301 unsigned v = 0;
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001302 int r;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001303 Iterator iterator;
1304 Item *i;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001305
1306 assert(fn);
1307
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001308 r = search_and_fopen_nulstr(fn, "re", conf_file_dirs, &f);
1309 if (r < 0) {
1310 if (ignore_enoent && r == -ENOENT)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001311 return 0;
1312
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001313 log_error("Failed to open '%s', ignoring: %s", fn, strerror(-r));
1314 return r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001315 }
1316
Kay Sievers772f8372011-04-25 21:38:21 +02001317 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001318 for (;;) {
1319 char line[LINE_MAX], *l;
1320 int k;
1321
1322 if (!(fgets(line, sizeof(line), f)))
1323 break;
1324
1325 v++;
1326
1327 l = strstrip(line);
1328 if (*l == '#' || *l == 0)
1329 continue;
1330
1331 if ((k = parse_line(fn, v, l)) < 0)
1332 if (r == 0)
1333 r = k;
1334 }
1335
Michal Sekletar78a92a52013-01-18 16:13:08 +01001336 /* we have to determine age parameter for each entry of type X */
1337 HASHMAP_FOREACH(i, globs, iterator) {
1338 Iterator iter;
1339 Item *j, *candidate_item = NULL;
1340
1341 if (i->type != IGNORE_DIRECTORY_PATH)
1342 continue;
1343
1344 HASHMAP_FOREACH(j, items, iter) {
1345 if (j->type != CREATE_DIRECTORY && j->type != TRUNCATE_DIRECTORY)
1346 continue;
1347
1348 if (path_equal(j->path, i->path)) {
1349 candidate_item = j;
1350 break;
1351 }
1352
1353 if ((!candidate_item && path_startswith(i->path, j->path)) ||
1354 (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
1355 candidate_item = j;
1356 }
1357
1358 if (candidate_item) {
1359 i->age = candidate_item->age;
1360 i->age_set = true;
1361 }
1362 }
1363
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001364 if (ferror(f)) {
1365 log_error("Failed to read from file %s: %m", fn);
1366 if (r == 0)
1367 r = -EIO;
1368 }
1369
1370 fclose(f);
1371
1372 return r;
1373}
1374
Lennart Poettering5008d582010-09-28 02:34:02 +02001375int main(int argc, char *argv[]) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001376 int r, k;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001377 Item *i;
1378 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001379
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001380 r = parse_argv(argc, argv);
1381 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001382 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1383
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001384 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001385 log_parse_environment();
1386 log_open();
1387
Lennart Poettering4c126262011-08-01 20:52:18 +02001388 umask(0022);
1389
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001390 label_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001391
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001392 items = hashmap_new(string_hash_func, string_compare_func);
1393 globs = hashmap_new(string_hash_func, string_compare_func);
1394
1395 if (!items || !globs) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001396 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001397 goto finish;
1398 }
1399
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001400 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001401
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001402 if (optind < argc) {
1403 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001404
Dave Reisner91256702012-06-08 22:31:19 -04001405 for (j = optind; j < argc; j++) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001406 k = read_config_file(argv[j], false);
1407 if (k < 0 && r == 0)
1408 r = k;
Dave Reisner91256702012-06-08 22:31:19 -04001409 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001410
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001411 } else {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001412 _cleanup_strv_free_ char **files = NULL;
1413 char **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001414
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001415 r = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001416 if (r < 0) {
Kay Sievers44143302011-04-28 23:51:24 +02001417 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
1418 goto finish;
1419 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001420
Kay Sievers772f8372011-04-25 21:38:21 +02001421 STRV_FOREACH(f, files) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001422 k = read_config_file(*f, true);
1423 if (k < 0 && r == 0)
1424 r = k;
Lennart Poettering5008d582010-09-28 02:34:02 +02001425 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001426 }
1427
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001428 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001429 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001430
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001431 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001432 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001433
Lennart Poettering5008d582010-09-28 02:34:02 +02001434finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001435 while ((i = hashmap_steal_first(items)))
1436 item_free(i);
1437
Lennart Poettering17b90522011-02-14 21:55:06 +01001438 while ((i = hashmap_steal_first(globs)))
1439 item_free(i);
1440
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001441 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001442 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001443
Lennart Poettering17b90522011-02-14 21:55:06 +01001444 set_free_free(unix_sockets);
1445
Lennart Poettering29003cf2010-10-19 19:36:45 +02001446 label_finish();
1447
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001448 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Lennart Poettering5008d582010-09-28 02:34:02 +02001449}