blob: cb15133e5ed2a1639e5796d9273c7f182bf92864 [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
Dave Reisnera2aced42013-07-24 11:10:05 -0400108static char **include_prefixes = NULL;
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100109
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) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200138 _cleanup_fclose_ FILE *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
Zbigniew Jędrzejewski-Szmekef422022013-04-22 23:12:15 -0400189 k = set_consume(unix_sockets, s);
190 if (k < 0 && k != -EEXIST)
191 goto fail;
Lennart Poettering17b90522011-02-14 21:55:06 +0100192 }
193
194 return;
195
196fail:
197 set_free_free(unix_sockets);
198 unix_sockets = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100199}
200
201static bool unix_socket_alive(const char *fn) {
202 assert(fn);
203
204 load_unix_sockets();
205
206 if (unix_sockets)
207 return !!set_get(unix_sockets, (char*) fn);
208
209 /* We don't know, so assume yes */
210 return true;
211}
212
Kay Sievers99d680a2013-03-13 13:12:36 +0100213static int dir_is_mount_point(DIR *d, const char *subdir) {
214 struct file_handle *h;
215 int mount_id_parent, mount_id;
216 int r_p, r;
217
218 h = alloca(MAX_HANDLE_SZ);
219
220 h->handle_bytes = MAX_HANDLE_SZ;
221 r_p = name_to_handle_at(dirfd(d), ".", h, &mount_id_parent, 0);
222 if (r_p < 0)
223 r_p = -errno;
224
225 h->handle_bytes = MAX_HANDLE_SZ;
226 r = name_to_handle_at(dirfd(d), subdir, h, &mount_id, 0);
227 if (r < 0)
228 r = -errno;
229
230 /* got no handle; make no assumptions, return error */
231 if (r_p < 0 && r < 0)
232 return r_p;
233
234 /* got both handles; if they differ, it is a mount point */
235 if (r_p >= 0 && r >= 0)
236 return mount_id_parent != mount_id;
237
238 /* got only one handle; assume different mount points if one
239 * of both queries was not supported by the filesystem */
240 if (r_p == -ENOSYS || r_p == -ENOTSUP || r == -ENOSYS || r == -ENOTSUP)
241 return true;
242
243 /* return error */
244 if (r_p < 0)
245 return r_p;
246 return r;
247}
248
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200249static int dir_cleanup(
Michal Sekletar78a92a52013-01-18 16:13:08 +0100250 Item *i,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200251 const char *p,
252 DIR *d,
253 const struct stat *ds,
254 usec_t cutoff,
255 dev_t rootdev,
256 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200257 int maxdepth,
258 bool keep_this_level)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200259{
260 struct dirent *dent;
261 struct timespec times[2];
262 bool deleted = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200263 int r = 0;
264
265 while ((dent = readdir(d))) {
266 struct stat s;
267 usec_t age;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200268 _cleanup_free_ char *sub_path = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200269
270 if (streq(dent->d_name, ".") ||
271 streq(dent->d_name, ".."))
272 continue;
273
274 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
275
276 if (errno != ENOENT) {
277 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
278 r = -errno;
279 }
280
281 continue;
282 }
283
284 /* Stay on the same filesystem */
285 if (s.st_dev != rootdev)
286 continue;
287
Kay Sievers99d680a2013-03-13 13:12:36 +0100288 /* Try to detect bind mounts of the same filesystem instance; they
289 * do not differ in device major/minors. This type of query is not
290 * supported on all kernels or filesystem types though. */
291 if (S_ISDIR(s.st_mode) && dir_is_mount_point(d, dent->d_name) > 0)
292 continue;
293
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200294 /* Do not delete read-only files owned by root */
295 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
296 continue;
297
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200298 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700299 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200300 goto finish;
301 }
302
303 /* Is there an item configured for this path? */
304 if (hashmap_get(items, sub_path))
305 continue;
306
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100307 if (find_glob(globs, sub_path))
308 continue;
309
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200310 if (S_ISDIR(s.st_mode)) {
311
312 if (mountpoint &&
313 streq(dent->d_name, "lost+found") &&
314 s.st_uid == 0)
315 continue;
316
317 if (maxdepth <= 0)
318 log_warning("Reached max depth on %s.", sub_path);
319 else {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200320 _cleanup_closedir_ DIR *sub_dir;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200321 int q;
322
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200323 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200324 if (sub_dir == NULL) {
325 if (errno != ENOENT) {
326 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
327 r = -errno;
328 }
329
330 continue;
331 }
332
Michal Sekletar78a92a52013-01-18 16:13:08 +0100333 q = dir_cleanup(i, sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200334
335 if (q < 0)
336 r = q;
337 }
338
Lennart Poettering24f3a372012-06-20 09:05:50 +0200339 /* Note: if you are wondering why we don't
340 * support the sticky bit for excluding
341 * directories from cleaning like we do it for
342 * other file system objects: well, the sticky
343 * bit already has a meaning for directories,
344 * so we don't want to overload that. */
345
346 if (keep_this_level)
347 continue;
348
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200349 /* Ignore ctime, we change it when deleting */
350 age = MAX(timespec_load(&s.st_mtim),
351 timespec_load(&s.st_atim));
352 if (age >= cutoff)
353 continue;
354
Lukas Nykryna6187d42013-03-01 18:29:59 +0100355 if (i->type != IGNORE_DIRECTORY_PATH || !streq(dent->d_name, p)) {
Michal Sekletar78a92a52013-01-18 16:13:08 +0100356 log_debug("rmdir '%s'\n", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200357
Michal Sekletar78a92a52013-01-18 16:13:08 +0100358 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
359 if (errno != ENOENT && errno != ENOTEMPTY) {
360 log_error("rmdir(%s): %m", sub_path);
361 r = -errno;
362 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200363 }
364 }
365
366 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100367 /* Skip files for which the sticky bit is
368 * set. These are semantics we define, and are
369 * unknown elsewhere. See XDG_RUNTIME_DIR
370 * specification for details. */
371 if (s.st_mode & S_ISVTX)
372 continue;
373
Lennart Poettering17b90522011-02-14 21:55:06 +0100374 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200375 if (streq(dent->d_name, ".journal") &&
376 s.st_uid == 0)
377 continue;
378
379 if (streq(dent->d_name, "aquota.user") ||
380 streq(dent->d_name, "aquota.group"))
381 continue;
382 }
383
Lennart Poettering17b90522011-02-14 21:55:06 +0100384 /* Ignore sockets that are listed in /proc/net/unix */
385 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
386 continue;
387
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100388 /* Ignore device nodes */
389 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
390 continue;
391
Lennart Poettering24f3a372012-06-20 09:05:50 +0200392 /* Keep files on this level around if this is
393 * requested */
394 if (keep_this_level)
395 continue;
396
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200397 age = MAX3(timespec_load(&s.st_mtim),
398 timespec_load(&s.st_atim),
399 timespec_load(&s.st_ctim));
400
401 if (age >= cutoff)
402 continue;
403
404 log_debug("unlink '%s'\n", sub_path);
405
406 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
407 if (errno != ENOENT) {
408 log_error("unlink(%s): %m", sub_path);
409 r = -errno;
410 }
411 }
412
413 deleted = true;
414 }
415 }
416
417finish:
418 if (deleted) {
419 /* Restore original directory timestamps */
420 times[0] = ds->st_atim;
421 times[1] = ds->st_mtim;
422
423 if (futimens(dirfd(d), times) < 0)
424 log_error("utimensat(%s): %m", p);
425 }
426
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200427 return r;
428}
429
Michal Schmidt062e01b2011-12-16 18:00:11 +0100430static int item_set_perms(Item *i, const char *path) {
431 /* not using i->path directly because it may be a glob */
432 if (i->mode_set)
433 if (chmod(path, i->mode) < 0) {
434 log_error("chmod(%s) failed: %m", path);
435 return -errno;
436 }
437
438 if (i->uid_set || i->gid_set)
439 if (chown(path,
440 i->uid_set ? i->uid : (uid_t) -1,
441 i->gid_set ? i->gid : (gid_t) -1) < 0) {
442
443 log_error("chown(%s) failed: %m", path);
444 return -errno;
445 }
446
Lennart Poetteringc9bc0762012-07-03 16:25:50 +0200447 return label_fix(path, false, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100448}
449
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400450static int write_one_file(Item *i, const char *path) {
451 int r, e, fd, flags;
452 struct stat st;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400453
454 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
455 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
456
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200457 RUN_WITH_UMASK(0) {
458 label_context_set(path, S_IFREG);
459 fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
460 e = errno;
461 label_context_clear();
462 errno = e;
463 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400464
465 if (fd < 0) {
466 if (i->type == WRITE_FILE && errno == ENOENT)
467 return 0;
468
469 log_error("Failed to create file %s: %m", path);
470 return -errno;
471 }
472
473 if (i->argument) {
474 ssize_t n;
475 size_t l;
Dave Reisner54693d92012-09-15 12:58:49 -0400476 _cleanup_free_ char *unescaped;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400477
Dave Reisner54693d92012-09-15 12:58:49 -0400478 unescaped = cunescape(i->argument);
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100479 if (unescaped == NULL) {
480 close_nointr_nofail(fd);
Dave Reisner54693d92012-09-15 12:58:49 -0400481 return log_oom();
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100482 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400483
Dave Reisner54693d92012-09-15 12:58:49 -0400484 l = strlen(unescaped);
485 n = write(fd, unescaped, l);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400486
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400487 if (n < 0 || (size_t) n < l) {
488 log_error("Failed to write file %s: %s", path, n < 0 ? strerror(-n) : "Short write");
489 close_nointr_nofail(fd);
490 return n < 0 ? n : -EIO;
491 }
492 }
493
Dave Reisner3612fbc2012-09-12 16:21:00 -0400494 close_nointr_nofail(fd);
495
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400496 if (stat(path, &st) < 0) {
497 log_error("stat(%s) failed: %m", path);
498 return -errno;
499 }
500
501 if (!S_ISREG(st.st_mode)) {
502 log_error("%s is not a file.", path);
503 return -EEXIST;
504 }
505
506 r = item_set_perms(i, path);
507 if (r < 0)
508 return r;
509
510 return 0;
511}
512
Michal Schmidt062e01b2011-12-16 18:00:11 +0100513static int recursive_relabel_children(Item *i, const char *path) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200514 _cleanup_closedir_ DIR *d;
Michal Schmidta8d88782011-12-15 23:11:07 +0100515 int ret = 0;
516
517 /* This returns the first error we run into, but nevertheless
518 * tries to go on */
519
520 d = opendir(path);
521 if (!d)
522 return errno == ENOENT ? 0 : -errno;
523
524 for (;;) {
Lennart Poettering7d5e9c02012-09-19 22:21:09 +0200525 struct dirent *de;
526 union dirent_storage buf;
Michal Schmidta8d88782011-12-15 23:11:07 +0100527 bool is_dir;
528 int r;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200529 _cleanup_free_ char *entry_path = NULL;
Michal Schmidta8d88782011-12-15 23:11:07 +0100530
Lennart Poettering7d5e9c02012-09-19 22:21:09 +0200531 r = readdir_r(d, &buf.de, &de);
Michal Schmidta8d88782011-12-15 23:11:07 +0100532 if (r != 0) {
533 if (ret == 0)
534 ret = -r;
535 break;
536 }
537
538 if (!de)
539 break;
540
541 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
542 continue;
543
544 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
545 if (ret == 0)
546 ret = -ENOMEM;
547 continue;
548 }
549
550 if (de->d_type == DT_UNKNOWN) {
551 struct stat st;
552
553 if (lstat(entry_path, &st) < 0) {
554 if (ret == 0 && errno != ENOENT)
555 ret = -errno;
Michal Schmidta8d88782011-12-15 23:11:07 +0100556 continue;
557 }
558
559 is_dir = S_ISDIR(st.st_mode);
560
561 } else
562 is_dir = de->d_type == DT_DIR;
563
Michal Schmidt062e01b2011-12-16 18:00:11 +0100564 r = item_set_perms(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100565 if (r < 0) {
566 if (ret == 0 && r != -ENOENT)
567 ret = r;
Michal Schmidta8d88782011-12-15 23:11:07 +0100568 continue;
569 }
570
571 if (is_dir) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100572 r = recursive_relabel_children(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100573 if (r < 0 && ret == 0)
574 ret = r;
575 }
Michal Schmidta8d88782011-12-15 23:11:07 +0100576 }
577
Michal Schmidta8d88782011-12-15 23:11:07 +0100578 return ret;
579}
580
581static int recursive_relabel(Item *i, const char *path) {
582 int r;
583 struct stat st;
584
Michal Schmidt062e01b2011-12-16 18:00:11 +0100585 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100586 if (r < 0)
587 return r;
588
589 if (lstat(path, &st) < 0)
590 return -errno;
591
592 if (S_ISDIR(st.st_mode))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100593 r = recursive_relabel_children(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100594
595 return r;
596}
597
Michal Schmidt99e68c02011-12-15 23:45:26 +0100598static int glob_item(Item *i, int (*action)(Item *, const char *)) {
599 int r = 0, k;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200600 _cleanup_globfree_ glob_t g = {};
Michal Schmidt99e68c02011-12-15 23:45:26 +0100601 char **fn;
602
Michal Schmidt99e68c02011-12-15 23:45:26 +0100603 errno = 0;
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400604 k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
605 if (k != 0)
Michal Schmidt99e68c02011-12-15 23:45:26 +0100606 if (k != GLOB_NOMATCH) {
Zbigniew Jędrzejewski-Szmek8333c772013-03-28 09:24:15 -0400607 if (errno > 0)
Michal Schmidt99e68c02011-12-15 23:45:26 +0100608 errno = EIO;
609
610 log_error("glob(%s) failed: %m", i->path);
611 return -errno;
612 }
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -0400613
614 STRV_FOREACH(fn, g.gl_pathv) {
615 k = action(i, *fn);
616 if (k < 0)
617 r = k;
Michal Schmidt99e68c02011-12-15 23:45:26 +0100618 }
619
Michal Schmidt99e68c02011-12-15 23:45:26 +0100620 return r;
621}
622
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200623static int create_item(Item *i) {
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200624 int r, e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200625 struct stat st;
626
627 assert(i);
628
629 switch (i->type) {
630
631 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100632 case IGNORE_DIRECTORY_PATH:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200633 case REMOVE_PATH:
634 case RECURSIVE_REMOVE_PATH:
635 return 0;
636
637 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100638 case TRUNCATE_FILE:
Dave Reisner1845fdd2012-09-27 20:48:13 -0400639 r = write_one_file(i, i->path);
640 if (r < 0)
641 return r;
642 break;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400643 case WRITE_FILE:
644 r = glob_item(i, write_one_file);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100645 if (r < 0)
646 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200647
648 break;
649
650 case TRUNCATE_DIRECTORY:
651 case CREATE_DIRECTORY:
652
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200653 RUN_WITH_UMASK(0000) {
654 mkdir_parents_label(i->path, 0755);
655 r = mkdir(i->path, i->mode);
656 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200657
658 if (r < 0 && errno != EEXIST) {
659 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100660 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200661 }
662
663 if (stat(i->path, &st) < 0) {
664 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100665 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200666 }
667
668 if (!S_ISDIR(st.st_mode)) {
669 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100670 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200671 }
672
Michal Schmidt062e01b2011-12-16 18:00:11 +0100673 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100674 if (r < 0)
675 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200676
677 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200678
679 case CREATE_FIFO:
680
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200681 RUN_WITH_UMASK(0000) {
682 r = mkfifo(i->path, i->mode);
683 }
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200684
685 if (r < 0 && errno != EEXIST) {
686 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100687 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200688 }
689
690 if (stat(i->path, &st) < 0) {
691 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100692 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200693 }
694
695 if (!S_ISFIFO(st.st_mode)) {
696 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100697 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200698 }
699
Michal Schmidt062e01b2011-12-16 18:00:11 +0100700 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100701 if (r < 0)
702 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200703
704 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100705
Lennart Poettering468d7262012-01-17 15:04:12 +0100706 case CREATE_SYMLINK: {
707 char *x;
708
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200709 label_context_set(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100710 r = symlink(i->argument, i->path);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200711 e = errno;
712 label_context_clear();
713 errno = e;
714
Lennart Poettering468d7262012-01-17 15:04:12 +0100715 if (r < 0 && errno != EEXIST) {
716 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
717 return -errno;
718 }
719
720 r = readlink_malloc(i->path, &x);
721 if (r < 0) {
722 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
723 return -errno;
724 }
725
726 if (!streq(i->argument, x)) {
727 free(x);
728 log_error("%s is not the right symlinks.", i->path);
729 return -EEXIST;
730 }
731
732 free(x);
733 break;
734 }
735
736 case CREATE_BLOCK_DEVICE:
737 case CREATE_CHAR_DEVICE: {
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700738 mode_t file_type;
739
740 if (have_effective_cap(CAP_MKNOD) == 0) {
741 /* In a container we lack CAP_MKNOD. We
Anatol Pomozovab06eef2013-04-14 19:37:54 -0700742 shouldn't attempt to create the device node in
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700743 that case to avoid noise, and we don't support
744 virtualized devices in containers anyway. */
745
746 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
747 return 0;
748 }
749
750 file_type = (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +0100751
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200752 RUN_WITH_UMASK(0000) {
753 label_context_set(i->path, file_type);
754 r = mknod(i->path, i->mode | file_type, i->major_minor);
755 e = errno;
756 label_context_clear();
757 errno = e;
758 }
Lennart Poettering468d7262012-01-17 15:04:12 +0100759
760 if (r < 0 && errno != EEXIST) {
761 log_error("Failed to create device node %s: %m", i->path);
762 return -errno;
763 }
764
765 if (stat(i->path, &st) < 0) {
766 log_error("stat(%s) failed: %m", i->path);
767 return -errno;
768 }
769
Michal Schmidte7aee752012-06-14 16:01:19 +0200770 if ((st.st_mode & S_IFMT) != file_type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100771 log_error("%s is not a device node.", i->path);
772 return -EEXIST;
773 }
774
775 r = item_set_perms(i, i->path);
776 if (r < 0)
777 return r;
778
779 break;
780 }
781
Michal Schmidt777b87e2011-12-16 18:27:35 +0100782 case RELABEL_PATH:
783
784 r = glob_item(i, item_set_perms);
785 if (r < 0)
Lennart Poettering96ca8192013-06-21 15:57:42 +0200786 return r;
Michal Schmidt777b87e2011-12-16 18:27:35 +0100787 break;
788
Michal Schmidta8d88782011-12-15 23:11:07 +0100789 case RECURSIVE_RELABEL_PATH:
790
791 r = glob_item(i, recursive_relabel);
792 if (r < 0)
793 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200794 }
795
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200796 log_debug("%s created successfully.", i->path);
797
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100798 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200799}
800
Michal Schmidta0896122011-12-15 21:32:50 +0100801static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200802 int r;
803
804 assert(i);
805
806 switch (i->type) {
807
808 case CREATE_FILE:
809 case TRUNCATE_FILE:
810 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200811 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100812 case CREATE_SYMLINK:
813 case CREATE_BLOCK_DEVICE:
814 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200815 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100816 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100817 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100818 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100819 case WRITE_FILE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200820 break;
821
822 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100823 if (remove(instance) < 0 && errno != ENOENT) {
824 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200825 return -errno;
826 }
827
828 break;
829
830 case TRUNCATE_DIRECTORY:
831 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +0200832 /* FIXME: we probably should use dir_cleanup() here
833 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +0200834 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Lennart Poettering468d7262012-01-17 15:04:12 +0100835 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100836 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200837 return r;
838 }
839
840 break;
841 }
842
843 return 0;
844}
845
Michal Schmidta0896122011-12-15 21:32:50 +0100846static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100847 int r = 0;
848
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100849 assert(i);
850
851 switch (i->type) {
852
853 case CREATE_FILE:
854 case TRUNCATE_FILE:
855 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200856 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100857 case CREATE_SYMLINK:
858 case CREATE_CHAR_DEVICE:
859 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100860 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100861 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100862 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100863 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100864 case WRITE_FILE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100865 break;
866
867 case REMOVE_PATH:
868 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100869 case RECURSIVE_REMOVE_PATH:
870 r = glob_item(i, remove_item_instance);
871 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100872 }
873
Michal Schmidt99e68c02011-12-15 23:45:26 +0100874 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100875}
876
Michal Sekletar78a92a52013-01-18 16:13:08 +0100877static int clean_item_instance(Item *i, const char* instance) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200878 _cleanup_closedir_ DIR *d = NULL;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100879 struct stat s, ps;
880 bool mountpoint;
881 int r;
882 usec_t cutoff, n;
883
884 assert(i);
885
886 if (!i->age_set)
887 return 0;
888
889 n = now(CLOCK_REALTIME);
890 if (n < i->age)
891 return 0;
892
893 cutoff = n - i->age;
894
895 d = opendir(instance);
896 if (!d) {
897 if (errno == ENOENT || errno == ENOTDIR)
898 return 0;
899
900 log_error("Failed to open directory %s: %m", i->path);
901 return -errno;
902 }
903
904 if (fstat(dirfd(d), &s) < 0) {
905 log_error("stat(%s) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500906 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100907 }
908
909 if (!S_ISDIR(s.st_mode)) {
910 log_error("%s is not a directory.", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500911 return -ENOTDIR;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100912 }
913
914 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
915 log_error("stat(%s/..) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500916 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100917 }
918
919 mountpoint = s.st_dev != ps.st_dev ||
920 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
921
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500922 r = dir_cleanup(i, instance, d, &s, cutoff, s.st_dev, mountpoint,
923 MAX_DEPTH, i->keep_first_level);
Michal Sekletar78a92a52013-01-18 16:13:08 +0100924 return r;
925}
926
927static int clean_item(Item *i) {
928 int r = 0;
929
930 assert(i);
931
932 switch (i->type) {
933 case CREATE_DIRECTORY:
934 case TRUNCATE_DIRECTORY:
935 case IGNORE_PATH:
936 clean_item_instance(i, i->path);
937 break;
938 case IGNORE_DIRECTORY_PATH:
939 r = glob_item(i, clean_item_instance);
940 break;
941 default:
942 break;
943 }
944
945 return r;
946}
947
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200948static int process_item(Item *i) {
949 int r, q, p;
950
951 assert(i);
952
953 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100954 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200955 p = arg_clean ? clean_item(i) : 0;
956
957 if (r < 0)
958 return r;
959
960 if (q < 0)
961 return q;
962
963 return p;
964}
965
966static void item_free(Item *i) {
967 assert(i);
968
969 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100970 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200971 free(i);
972}
973
Maciej Wereskie2f2fb72013-07-19 15:43:12 +0200974static inline void item_freep(Item **i) {
975 if (*i)
976 item_free(*i);
977}
978#define _cleanup_item_free_ _cleanup_(item_freep)
979
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200980static bool item_equal(Item *a, Item *b) {
981 assert(a);
982 assert(b);
983
984 if (!streq_ptr(a->path, b->path))
985 return false;
986
987 if (a->type != b->type)
988 return false;
989
990 if (a->uid_set != b->uid_set ||
991 (a->uid_set && a->uid != b->uid))
992 return false;
993
994 if (a->gid_set != b->gid_set ||
995 (a->gid_set && a->gid != b->gid))
996 return false;
997
998 if (a->mode_set != b->mode_set ||
999 (a->mode_set && a->mode != b->mode))
1000 return false;
1001
1002 if (a->age_set != b->age_set ||
1003 (a->age_set && a->age != b->age))
1004 return false;
1005
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001006 if ((a->type == CREATE_FILE ||
1007 a->type == TRUNCATE_FILE ||
1008 a->type == WRITE_FILE ||
1009 a->type == CREATE_SYMLINK) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +01001010 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +01001011 return false;
1012
1013 if ((a->type == CREATE_CHAR_DEVICE ||
1014 a->type == CREATE_BLOCK_DEVICE) &&
1015 a->major_minor != b->major_minor)
1016 return false;
1017
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001018 return true;
1019}
1020
Dave Reisnera2aced42013-07-24 11:10:05 -04001021static bool should_include_path(const char *path) {
1022 char **prefix;
1023
1024 /* no explicit paths specified for inclusion, so everything is valid */
1025 if (strv_length(include_prefixes) == 0)
1026 return true;
1027
1028 STRV_FOREACH(prefix, include_prefixes) {
1029 if (path_startswith(path, *prefix))
1030 return true;
1031 }
1032
1033 return false;
1034}
1035
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001036static int parse_line(const char *fname, unsigned line, const char *buffer) {
Maciej Wereskie2f2fb72013-07-19 15:43:12 +02001037 _cleanup_item_free_ Item *i = NULL;
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001038 Item *existing;
Harald Hoyer7fd1b192013-04-18 09:11:22 +02001039 _cleanup_free_ char
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001040 *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +01001041 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001042 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001043 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +02001044
1045 assert(fname);
1046 assert(line >= 1);
1047 assert(buffer);
1048
Lennart Poettering468d7262012-01-17 15:04:12 +01001049 i = new0(Item, 1);
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001050 if (!i)
1051 return log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001052
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001053 r = sscanf(buffer,
1054 "%c %ms %ms %ms %ms %ms %n",
Michal Schmidt66ccd032011-12-15 21:31:14 +01001055 &type,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +01001056 &i->path,
1057 &mode,
1058 &user,
1059 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +01001060 &age,
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001061 &n);
1062 if (r < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001063 log_error("[%s:%u] Syntax error.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001064 return -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +02001065 }
1066
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001067 if (n >= 0) {
1068 n += strspn(buffer+n, WHITESPACE);
1069 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
1070 i->argument = unquote(buffer+n, "\"");
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001071 if (!i->argument)
1072 return log_oom();
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001073 }
1074 }
1075
Michal Schmidt777b87e2011-12-16 18:27:35 +01001076 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001077
Michal Schmidt777b87e2011-12-16 18:27:35 +01001078 case CREATE_FILE:
1079 case TRUNCATE_FILE:
1080 case CREATE_DIRECTORY:
1081 case TRUNCATE_DIRECTORY:
1082 case CREATE_FIFO:
1083 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001084 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001085 case REMOVE_PATH:
1086 case RECURSIVE_REMOVE_PATH:
1087 case RELABEL_PATH:
1088 case RECURSIVE_RELABEL_PATH:
1089 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001090
1091 case CREATE_SYMLINK:
1092 if (!i->argument) {
1093 log_error("[%s:%u] Symlink file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001094 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001095 }
1096 break;
1097
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001098 case WRITE_FILE:
1099 if (!i->argument) {
1100 log_error("[%s:%u] Write file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001101 return -EBADMSG;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001102 }
1103 break;
1104
Lennart Poettering468d7262012-01-17 15:04:12 +01001105 case CREATE_CHAR_DEVICE:
1106 case CREATE_BLOCK_DEVICE: {
1107 unsigned major, minor;
1108
1109 if (!i->argument) {
1110 log_error("[%s:%u] Device file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001111 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001112 }
1113
1114 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1115 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 -04001116 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001117 }
1118
1119 i->major_minor = makedev(major, minor);
1120 break;
1121 }
1122
Michal Schmidt777b87e2011-12-16 18:27:35 +01001123 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001124 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001125 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001126 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001127
Michal Schmidta8d88782011-12-15 23:11:07 +01001128 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001129
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001130 if (!path_is_absolute(i->path)) {
1131 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001132 return -EBADMSG;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001133 }
1134
1135 path_kill_slashes(i->path);
1136
Dave Reisnera2aced42013-07-24 11:10:05 -04001137 if (!should_include_path(i->path))
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001138 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001139
1140 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001141 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001142
Lennart Poetteringd05c5032012-07-16 12:34:54 +02001143 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001144 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001145 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001146 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001147 }
1148
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001149 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001150 }
1151
1152 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001153 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001154
Lennart Poettering4b678342011-07-23 01:17:59 +02001155 r = get_group_creds(&g, &i->gid);
1156 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001157 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001158 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001159 }
1160
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001161 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001162 }
1163
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001164 if (mode && !streq(mode, "-")) {
1165 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001166
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001167 if (sscanf(mode, "%o", &m) != 1) {
1168 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001169 return -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001170 }
1171
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001172 i->mode = m;
1173 i->mode_set = true;
1174 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001175 i->mode =
1176 i->type == CREATE_DIRECTORY ||
1177 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001178
1179 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001180 const char *a = age;
1181
1182 if (*a == '~') {
1183 i->keep_first_level = true;
1184 a++;
1185 }
1186
Lennart Poettering7f602782013-04-02 20:38:16 +02001187 if (parse_sec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001188 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001189 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001190 }
1191
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001192 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001193 }
1194
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001195 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001196
Lennart Poettering468d7262012-01-17 15:04:12 +01001197 existing = hashmap_get(h, i->path);
1198 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001199
1200 /* Two identical items are fine */
1201 if (!item_equal(existing, i))
1202 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1203
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001204 return 0;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001205 }
1206
Lennart Poettering468d7262012-01-17 15:04:12 +01001207 r = hashmap_put(h, i->path, i);
1208 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001209 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001210 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001211 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001212
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001213 i = NULL; /* avoid cleanup */
Lennart Poettering5008d582010-09-28 02:34:02 +02001214
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001215 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001216}
1217
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001218static int help(void) {
1219
Lennart Poettering522d4a42011-02-13 15:08:15 +01001220 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1221 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001222 " -h --help Show this help\n"
1223 " --create Create marked files/directories\n"
1224 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001225 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +01001226 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001227 program_invocation_short_name);
1228
1229 return 0;
1230}
1231
1232static int parse_argv(int argc, char *argv[]) {
1233
1234 enum {
1235 ARG_CREATE,
1236 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001237 ARG_REMOVE,
1238 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001239 };
1240
1241 static const struct option options[] = {
1242 { "help", no_argument, NULL, 'h' },
1243 { "create", no_argument, NULL, ARG_CREATE },
1244 { "clean", no_argument, NULL, ARG_CLEAN },
1245 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001246 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001247 { NULL, 0, NULL, 0 }
1248 };
1249
1250 int c;
1251
1252 assert(argc >= 0);
1253 assert(argv);
1254
1255 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1256
1257 switch (c) {
1258
1259 case 'h':
1260 help();
1261 return 0;
1262
1263 case ARG_CREATE:
1264 arg_create = true;
1265 break;
1266
1267 case ARG_CLEAN:
1268 arg_clean = true;
1269 break;
1270
1271 case ARG_REMOVE:
1272 arg_remove = true;
1273 break;
1274
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001275 case ARG_PREFIX:
Dave Reisnera2aced42013-07-24 11:10:05 -04001276 if (strv_extend(&include_prefixes, optarg) < 0)
1277 return log_oom();
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001278 break;
1279
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001280 case '?':
1281 return -EINVAL;
1282
1283 default:
1284 log_error("Unknown option code %c", c);
1285 return -EINVAL;
1286 }
1287 }
1288
1289 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001290 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001291 return -EINVAL;
1292 }
1293
1294 return 1;
1295}
1296
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001297static int read_config_file(const char *fn, bool ignore_enoent) {
1298 FILE *f;
1299 unsigned v = 0;
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001300 int r;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001301 Iterator iterator;
1302 Item *i;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001303
1304 assert(fn);
1305
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001306 r = search_and_fopen_nulstr(fn, "re", conf_file_dirs, &f);
1307 if (r < 0) {
1308 if (ignore_enoent && r == -ENOENT)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001309 return 0;
1310
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001311 log_error("Failed to open '%s', ignoring: %s", fn, strerror(-r));
1312 return r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001313 }
1314
Kay Sievers772f8372011-04-25 21:38:21 +02001315 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001316 for (;;) {
1317 char line[LINE_MAX], *l;
1318 int k;
1319
1320 if (!(fgets(line, sizeof(line), f)))
1321 break;
1322
1323 v++;
1324
1325 l = strstrip(line);
1326 if (*l == '#' || *l == 0)
1327 continue;
1328
1329 if ((k = parse_line(fn, v, l)) < 0)
1330 if (r == 0)
1331 r = k;
1332 }
1333
Michal Sekletar78a92a52013-01-18 16:13:08 +01001334 /* we have to determine age parameter for each entry of type X */
1335 HASHMAP_FOREACH(i, globs, iterator) {
1336 Iterator iter;
1337 Item *j, *candidate_item = NULL;
1338
1339 if (i->type != IGNORE_DIRECTORY_PATH)
1340 continue;
1341
1342 HASHMAP_FOREACH(j, items, iter) {
1343 if (j->type != CREATE_DIRECTORY && j->type != TRUNCATE_DIRECTORY)
1344 continue;
1345
1346 if (path_equal(j->path, i->path)) {
1347 candidate_item = j;
1348 break;
1349 }
1350
1351 if ((!candidate_item && path_startswith(i->path, j->path)) ||
1352 (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
1353 candidate_item = j;
1354 }
1355
1356 if (candidate_item) {
1357 i->age = candidate_item->age;
1358 i->age_set = true;
1359 }
1360 }
1361
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001362 if (ferror(f)) {
1363 log_error("Failed to read from file %s: %m", fn);
1364 if (r == 0)
1365 r = -EIO;
1366 }
1367
1368 fclose(f);
1369
1370 return r;
1371}
1372
Lennart Poettering5008d582010-09-28 02:34:02 +02001373int main(int argc, char *argv[]) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001374 int r, k;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001375 Item *i;
1376 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001377
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001378 r = parse_argv(argc, argv);
1379 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001380 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1381
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001382 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001383 log_parse_environment();
1384 log_open();
1385
Lennart Poettering4c126262011-08-01 20:52:18 +02001386 umask(0022);
1387
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001388 label_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001389
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001390 items = hashmap_new(string_hash_func, string_compare_func);
1391 globs = hashmap_new(string_hash_func, string_compare_func);
1392
1393 if (!items || !globs) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001394 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001395 goto finish;
1396 }
1397
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001398 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001399
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001400 if (optind < argc) {
1401 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001402
Dave Reisner91256702012-06-08 22:31:19 -04001403 for (j = optind; j < argc; j++) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001404 k = read_config_file(argv[j], false);
1405 if (k < 0 && r == 0)
1406 r = k;
Dave Reisner91256702012-06-08 22:31:19 -04001407 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001408
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001409 } else {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001410 _cleanup_strv_free_ char **files = NULL;
1411 char **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001412
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001413 r = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001414 if (r < 0) {
Kay Sievers44143302011-04-28 23:51:24 +02001415 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
1416 goto finish;
1417 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001418
Kay Sievers772f8372011-04-25 21:38:21 +02001419 STRV_FOREACH(f, files) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001420 k = read_config_file(*f, true);
1421 if (k < 0 && r == 0)
1422 r = k;
Lennart Poettering5008d582010-09-28 02:34:02 +02001423 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001424 }
1425
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001426 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001427 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001428
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001429 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001430 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001431
Lennart Poettering5008d582010-09-28 02:34:02 +02001432finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001433 while ((i = hashmap_steal_first(items)))
1434 item_free(i);
1435
Lennart Poettering17b90522011-02-14 21:55:06 +01001436 while ((i = hashmap_steal_first(globs)))
1437 item_free(i);
1438
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001439 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001440 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001441
Dave Reisnera2aced42013-07-24 11:10:05 -04001442 strv_free(include_prefixes);
1443
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}