blob: b93d8988fc8573bc4befaf5c60ade7f94b54335e [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"
Kay Sievers49e942b2012-04-10 21:54:31 +020046#include "mkdir.h"
Kay Sievers9eb977d2012-05-07 21:36:12 +020047#include "path-util.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020048#include "strv.h"
49#include "label.h"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020050#include "set.h"
Kay Sievers2c210442012-05-07 18:55:45 +020051#include "conf-files.h"
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -070052#include "capability.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020053
Andreas Jaeger01000472010-09-29 10:08:24 +020054/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020055 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020056 * properly owned directories beneath /tmp, /var/tmp, /run, which are
57 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020058
Michal Schmidt66ccd032011-12-15 21:31:14 +010059typedef enum ItemType {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010060 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020061 CREATE_FILE = 'f',
62 TRUNCATE_FILE = 'F',
Lennart Poettering31ed59c2012-01-18 16:39:04 +010063 WRITE_FILE = 'w',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020064 CREATE_DIRECTORY = 'd',
65 TRUNCATE_DIRECTORY = 'D',
Lennart Poetteringee17ee72011-07-12 03:56:56 +020066 CREATE_FIFO = 'p',
Lennart Poettering468d7262012-01-17 15:04:12 +010067 CREATE_SYMLINK = 'L',
68 CREATE_CHAR_DEVICE = 'c',
69 CREATE_BLOCK_DEVICE = 'b',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010070
71 /* These ones take globs */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020072 IGNORE_PATH = 'x',
Michal Sekletar78a92a52013-01-18 16:13:08 +010073 IGNORE_DIRECTORY_PATH = 'X',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020074 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010075 RECURSIVE_REMOVE_PATH = 'R',
Michal Schmidt777b87e2011-12-16 18:27:35 +010076 RELABEL_PATH = 'z',
Michal Schmidta8d88782011-12-15 23:11:07 +010077 RECURSIVE_RELABEL_PATH = 'Z'
Michal Schmidt66ccd032011-12-15 21:31:14 +010078} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020079
80typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +010081 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020082
83 char *path;
Lennart Poettering468d7262012-01-17 15:04:12 +010084 char *argument;
Lennart Poettering5008d582010-09-28 02:34:02 +020085 uid_t uid;
86 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020087 mode_t mode;
88 usec_t age;
89
Lennart Poettering468d7262012-01-17 15:04:12 +010090 dev_t major_minor;
91
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020092 bool uid_set:1;
93 bool gid_set:1;
94 bool mode_set:1;
95 bool age_set:1;
Lennart Poettering24f3a372012-06-20 09:05:50 +020096
97 bool keep_first_level:1;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020098} Item;
99
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100100static Hashmap *items = NULL, *globs = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100101static Set *unix_sockets = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200102
103static bool arg_create = false;
104static bool arg_clean = false;
105static bool arg_remove = false;
106
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100107static const char *arg_prefix = NULL;
108
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100109static const char conf_file_dirs[] =
110 "/etc/tmpfiles.d\0"
111 "/run/tmpfiles.d\0"
112 "/usr/local/lib/tmpfiles.d\0"
113 "/usr/lib/tmpfiles.d\0"
Lennart Poettering3f2afb22012-07-20 16:24:55 +0200114#ifdef HAVE_SPLIT_USR
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100115 "/lib/tmpfiles.d\0"
Lennart Poettering3f2afb22012-07-20 16:24:55 +0200116#endif
Lennart Poetteringfabe5c02013-02-11 23:48:36 +0100117 ;
Dave Reisner91256702012-06-08 22:31:19 -0400118
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200119#define MAX_DEPTH 256
120
Michal Schmidt66ccd032011-12-15 21:31:14 +0100121static bool needs_glob(ItemType t) {
Michal Sekletar78a92a52013-01-18 16:13:08 +0100122 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 +0100123}
124
125static struct Item* find_glob(Hashmap *h, const char *match) {
126 Item *j;
127 Iterator i;
128
129 HASHMAP_FOREACH(j, h, i)
130 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
131 return j;
132
133 return NULL;
134}
135
Lennart Poettering17b90522011-02-14 21:55:06 +0100136static void load_unix_sockets(void) {
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500137 FILE _cleanup_fclose_ *f = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100138 char line[LINE_MAX];
139
140 if (unix_sockets)
141 return;
142
143 /* We maintain a cache of the sockets we found in
144 * /proc/net/unix to speed things up a little. */
145
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100146 unix_sockets = set_new(string_hash_func, string_compare_func);
147 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100148 return;
149
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100150 f = fopen("/proc/net/unix", "re");
151 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100152 return;
153
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100154 /* Skip header */
155 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100156 goto fail;
157
158 for (;;) {
159 char *p, *s;
160 int k;
161
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100162 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100163 break;
164
165 truncate_nl(line);
166
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100167 p = strchr(line, ':');
168 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100169 continue;
170
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100171 if (strlen(p) < 37)
172 continue;
173
174 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100175 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100176 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100177 p += strspn(p, WHITESPACE);
178
179 if (*p != '/')
180 continue;
181
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100182 s = strdup(p);
183 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100184 goto fail;
185
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100186 path_kill_slashes(s);
187
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100188 k = set_put(unix_sockets, s);
189 if (k < 0) {
Lennart Poettering17b90522011-02-14 21:55:06 +0100190 free(s);
191
192 if (k != -EEXIST)
193 goto fail;
194 }
195 }
196
197 return;
198
199fail:
200 set_free_free(unix_sockets);
201 unix_sockets = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100202}
203
204static bool unix_socket_alive(const char *fn) {
205 assert(fn);
206
207 load_unix_sockets();
208
209 if (unix_sockets)
210 return !!set_get(unix_sockets, (char*) fn);
211
212 /* We don't know, so assume yes */
213 return true;
214}
215
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200216static int dir_cleanup(
Michal Sekletar78a92a52013-01-18 16:13:08 +0100217 Item *i,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200218 const char *p,
219 DIR *d,
220 const struct stat *ds,
221 usec_t cutoff,
222 dev_t rootdev,
223 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200224 int maxdepth,
225 bool keep_this_level)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200226{
227 struct dirent *dent;
228 struct timespec times[2];
229 bool deleted = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200230 int r = 0;
231
232 while ((dent = readdir(d))) {
233 struct stat s;
234 usec_t age;
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500235 char _cleanup_free_ *sub_path = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200236
237 if (streq(dent->d_name, ".") ||
238 streq(dent->d_name, ".."))
239 continue;
240
241 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
242
243 if (errno != ENOENT) {
244 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
245 r = -errno;
246 }
247
248 continue;
249 }
250
251 /* Stay on the same filesystem */
252 if (s.st_dev != rootdev)
253 continue;
254
255 /* Do not delete read-only files owned by root */
256 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
257 continue;
258
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200259 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700260 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200261 goto finish;
262 }
263
264 /* Is there an item configured for this path? */
265 if (hashmap_get(items, sub_path))
266 continue;
267
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100268 if (find_glob(globs, sub_path))
269 continue;
270
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200271 if (S_ISDIR(s.st_mode)) {
272
273 if (mountpoint &&
274 streq(dent->d_name, "lost+found") &&
275 s.st_uid == 0)
276 continue;
277
278 if (maxdepth <= 0)
279 log_warning("Reached max depth on %s.", sub_path);
280 else {
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500281 DIR _cleanup_closedir_ *sub_dir;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200282 int q;
283
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200284 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200285 if (sub_dir == NULL) {
286 if (errno != ENOENT) {
287 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
288 r = -errno;
289 }
290
291 continue;
292 }
293
Michal Sekletar78a92a52013-01-18 16:13:08 +0100294 q = dir_cleanup(i, sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200295
296 if (q < 0)
297 r = q;
298 }
299
Lennart Poettering24f3a372012-06-20 09:05:50 +0200300 /* Note: if you are wondering why we don't
301 * support the sticky bit for excluding
302 * directories from cleaning like we do it for
303 * other file system objects: well, the sticky
304 * bit already has a meaning for directories,
305 * so we don't want to overload that. */
306
307 if (keep_this_level)
308 continue;
309
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200310 /* Ignore ctime, we change it when deleting */
311 age = MAX(timespec_load(&s.st_mtim),
312 timespec_load(&s.st_atim));
313 if (age >= cutoff)
314 continue;
315
Lukas Nykryna6187d42013-03-01 18:29:59 +0100316 if (i->type != IGNORE_DIRECTORY_PATH || !streq(dent->d_name, p)) {
Michal Sekletar78a92a52013-01-18 16:13:08 +0100317 log_debug("rmdir '%s'\n", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200318
Michal Sekletar78a92a52013-01-18 16:13:08 +0100319 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
320 if (errno != ENOENT && errno != ENOTEMPTY) {
321 log_error("rmdir(%s): %m", sub_path);
322 r = -errno;
323 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200324 }
325 }
326
327 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100328 /* Skip files for which the sticky bit is
329 * set. These are semantics we define, and are
330 * unknown elsewhere. See XDG_RUNTIME_DIR
331 * specification for details. */
332 if (s.st_mode & S_ISVTX)
333 continue;
334
Lennart Poettering17b90522011-02-14 21:55:06 +0100335 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200336 if (streq(dent->d_name, ".journal") &&
337 s.st_uid == 0)
338 continue;
339
340 if (streq(dent->d_name, "aquota.user") ||
341 streq(dent->d_name, "aquota.group"))
342 continue;
343 }
344
Lennart Poettering17b90522011-02-14 21:55:06 +0100345 /* Ignore sockets that are listed in /proc/net/unix */
346 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
347 continue;
348
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100349 /* Ignore device nodes */
350 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
351 continue;
352
Lennart Poettering24f3a372012-06-20 09:05:50 +0200353 /* Keep files on this level around if this is
354 * requested */
355 if (keep_this_level)
356 continue;
357
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200358 age = MAX3(timespec_load(&s.st_mtim),
359 timespec_load(&s.st_atim),
360 timespec_load(&s.st_ctim));
361
362 if (age >= cutoff)
363 continue;
364
365 log_debug("unlink '%s'\n", sub_path);
366
367 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
368 if (errno != ENOENT) {
369 log_error("unlink(%s): %m", sub_path);
370 r = -errno;
371 }
372 }
373
374 deleted = true;
375 }
376 }
377
378finish:
379 if (deleted) {
380 /* Restore original directory timestamps */
381 times[0] = ds->st_atim;
382 times[1] = ds->st_mtim;
383
384 if (futimens(dirfd(d), times) < 0)
385 log_error("utimensat(%s): %m", p);
386 }
387
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200388 return r;
389}
390
Michal Schmidt062e01b2011-12-16 18:00:11 +0100391static int item_set_perms(Item *i, const char *path) {
392 /* not using i->path directly because it may be a glob */
393 if (i->mode_set)
394 if (chmod(path, i->mode) < 0) {
395 log_error("chmod(%s) failed: %m", path);
396 return -errno;
397 }
398
399 if (i->uid_set || i->gid_set)
400 if (chown(path,
401 i->uid_set ? i->uid : (uid_t) -1,
402 i->gid_set ? i->gid : (gid_t) -1) < 0) {
403
404 log_error("chown(%s) failed: %m", path);
405 return -errno;
406 }
407
Lennart Poetteringc9bc0762012-07-03 16:25:50 +0200408 return label_fix(path, false, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100409}
410
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400411static int write_one_file(Item *i, const char *path) {
412 int r, e, fd, flags;
413 struct stat st;
414 mode_t u;
415
416 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
417 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
418
419 u = umask(0);
420 label_context_set(path, S_IFREG);
421 fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
422 e = errno;
423 label_context_clear();
424 umask(u);
425 errno = e;
426
427 if (fd < 0) {
428 if (i->type == WRITE_FILE && errno == ENOENT)
429 return 0;
430
431 log_error("Failed to create file %s: %m", path);
432 return -errno;
433 }
434
435 if (i->argument) {
436 ssize_t n;
437 size_t l;
Dave Reisner54693d92012-09-15 12:58:49 -0400438 _cleanup_free_ char *unescaped;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400439
Dave Reisner54693d92012-09-15 12:58:49 -0400440 unescaped = cunescape(i->argument);
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100441 if (unescaped == NULL) {
442 close_nointr_nofail(fd);
Dave Reisner54693d92012-09-15 12:58:49 -0400443 return log_oom();
Thomas Jarosch3785ba62012-12-25 13:46:46 +0100444 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400445
Dave Reisner54693d92012-09-15 12:58:49 -0400446 l = strlen(unescaped);
447 n = write(fd, unescaped, l);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400448
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400449 if (n < 0 || (size_t) n < l) {
450 log_error("Failed to write file %s: %s", path, n < 0 ? strerror(-n) : "Short write");
451 close_nointr_nofail(fd);
452 return n < 0 ? n : -EIO;
453 }
454 }
455
Dave Reisner3612fbc2012-09-12 16:21:00 -0400456 close_nointr_nofail(fd);
457
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400458 if (stat(path, &st) < 0) {
459 log_error("stat(%s) failed: %m", path);
460 return -errno;
461 }
462
463 if (!S_ISREG(st.st_mode)) {
464 log_error("%s is not a file.", path);
465 return -EEXIST;
466 }
467
468 r = item_set_perms(i, path);
469 if (r < 0)
470 return r;
471
472 return 0;
473}
474
Michal Schmidt062e01b2011-12-16 18:00:11 +0100475static int recursive_relabel_children(Item *i, const char *path) {
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500476 DIR _cleanup_closedir_ *d;
Michal Schmidta8d88782011-12-15 23:11:07 +0100477 int ret = 0;
478
479 /* This returns the first error we run into, but nevertheless
480 * tries to go on */
481
482 d = opendir(path);
483 if (!d)
484 return errno == ENOENT ? 0 : -errno;
485
486 for (;;) {
Lennart Poettering7d5e9c02012-09-19 22:21:09 +0200487 struct dirent *de;
488 union dirent_storage buf;
Michal Schmidta8d88782011-12-15 23:11:07 +0100489 bool is_dir;
490 int r;
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500491 char _cleanup_free_ *entry_path = NULL;
Michal Schmidta8d88782011-12-15 23:11:07 +0100492
Lennart Poettering7d5e9c02012-09-19 22:21:09 +0200493 r = readdir_r(d, &buf.de, &de);
Michal Schmidta8d88782011-12-15 23:11:07 +0100494 if (r != 0) {
495 if (ret == 0)
496 ret = -r;
497 break;
498 }
499
500 if (!de)
501 break;
502
503 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
504 continue;
505
506 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
507 if (ret == 0)
508 ret = -ENOMEM;
509 continue;
510 }
511
512 if (de->d_type == DT_UNKNOWN) {
513 struct stat st;
514
515 if (lstat(entry_path, &st) < 0) {
516 if (ret == 0 && errno != ENOENT)
517 ret = -errno;
Michal Schmidta8d88782011-12-15 23:11:07 +0100518 continue;
519 }
520
521 is_dir = S_ISDIR(st.st_mode);
522
523 } else
524 is_dir = de->d_type == DT_DIR;
525
Michal Schmidt062e01b2011-12-16 18:00:11 +0100526 r = item_set_perms(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100527 if (r < 0) {
528 if (ret == 0 && r != -ENOENT)
529 ret = r;
Michal Schmidta8d88782011-12-15 23:11:07 +0100530 continue;
531 }
532
533 if (is_dir) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100534 r = recursive_relabel_children(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100535 if (r < 0 && ret == 0)
536 ret = r;
537 }
Michal Schmidta8d88782011-12-15 23:11:07 +0100538 }
539
Michal Schmidta8d88782011-12-15 23:11:07 +0100540 return ret;
541}
542
543static int recursive_relabel(Item *i, const char *path) {
544 int r;
545 struct stat st;
546
Michal Schmidt062e01b2011-12-16 18:00:11 +0100547 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100548 if (r < 0)
549 return r;
550
551 if (lstat(path, &st) < 0)
552 return -errno;
553
554 if (S_ISDIR(st.st_mode))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100555 r = recursive_relabel_children(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100556
557 return r;
558}
559
Michal Schmidt99e68c02011-12-15 23:45:26 +0100560static int glob_item(Item *i, int (*action)(Item *, const char *)) {
561 int r = 0, k;
562 glob_t g;
563 char **fn;
564
565 zero(g);
566
567 errno = 0;
568 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
569
570 if (k != GLOB_NOMATCH) {
571 if (errno != 0)
572 errno = EIO;
573
574 log_error("glob(%s) failed: %m", i->path);
575 return -errno;
576 }
577 }
578
579 STRV_FOREACH(fn, g.gl_pathv)
580 if ((k = action(i, *fn)) < 0)
581 r = k;
582
583 globfree(&g);
584 return r;
585}
586
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200587static int create_item(Item *i) {
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200588 int r, e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200589 mode_t u;
590 struct stat st;
591
592 assert(i);
593
594 switch (i->type) {
595
596 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100597 case IGNORE_DIRECTORY_PATH:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200598 case REMOVE_PATH:
599 case RECURSIVE_REMOVE_PATH:
600 return 0;
601
602 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100603 case TRUNCATE_FILE:
Dave Reisner1845fdd2012-09-27 20:48:13 -0400604 r = write_one_file(i, i->path);
605 if (r < 0)
606 return r;
607 break;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400608 case WRITE_FILE:
609 r = glob_item(i, write_one_file);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100610 if (r < 0)
611 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200612
613 break;
614
615 case TRUNCATE_DIRECTORY:
616 case CREATE_DIRECTORY:
617
618 u = umask(0);
Kay Sieversd2e54fa2012-05-31 12:40:20 +0200619 mkdir_parents_label(i->path, 0755);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200620 r = mkdir(i->path, i->mode);
621 umask(u);
622
623 if (r < 0 && errno != EEXIST) {
624 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100625 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200626 }
627
628 if (stat(i->path, &st) < 0) {
629 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100630 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200631 }
632
633 if (!S_ISDIR(st.st_mode)) {
634 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100635 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200636 }
637
Michal Schmidt062e01b2011-12-16 18:00:11 +0100638 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100639 if (r < 0)
640 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200641
642 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200643
644 case CREATE_FIFO:
645
646 u = umask(0);
647 r = mkfifo(i->path, i->mode);
648 umask(u);
649
650 if (r < 0 && errno != EEXIST) {
651 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100652 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200653 }
654
655 if (stat(i->path, &st) < 0) {
656 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100657 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200658 }
659
660 if (!S_ISFIFO(st.st_mode)) {
661 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100662 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200663 }
664
Michal Schmidt062e01b2011-12-16 18:00:11 +0100665 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100666 if (r < 0)
667 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200668
669 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100670
Lennart Poettering468d7262012-01-17 15:04:12 +0100671 case CREATE_SYMLINK: {
672 char *x;
673
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200674 label_context_set(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100675 r = symlink(i->argument, i->path);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200676 e = errno;
677 label_context_clear();
678 errno = e;
679
Lennart Poettering468d7262012-01-17 15:04:12 +0100680 if (r < 0 && errno != EEXIST) {
681 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
682 return -errno;
683 }
684
685 r = readlink_malloc(i->path, &x);
686 if (r < 0) {
687 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
688 return -errno;
689 }
690
691 if (!streq(i->argument, x)) {
692 free(x);
693 log_error("%s is not the right symlinks.", i->path);
694 return -EEXIST;
695 }
696
697 free(x);
698 break;
699 }
700
701 case CREATE_BLOCK_DEVICE:
702 case CREATE_CHAR_DEVICE: {
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700703 mode_t file_type;
704
705 if (have_effective_cap(CAP_MKNOD) == 0) {
706 /* In a container we lack CAP_MKNOD. We
707 shouldnt attempt to create the device node in
708 that case to avoid noise, and we don't support
709 virtualized devices in containers anyway. */
710
711 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
712 return 0;
713 }
714
715 file_type = (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +0100716
717 u = umask(0);
Michal Schmidte7aee752012-06-14 16:01:19 +0200718 label_context_set(i->path, file_type);
719 r = mknod(i->path, i->mode | file_type, i->major_minor);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200720 e = errno;
721 label_context_clear();
Lennart Poettering468d7262012-01-17 15:04:12 +0100722 umask(u);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200723 errno = e;
Lennart Poettering468d7262012-01-17 15:04:12 +0100724
725 if (r < 0 && errno != EEXIST) {
726 log_error("Failed to create device node %s: %m", i->path);
727 return -errno;
728 }
729
730 if (stat(i->path, &st) < 0) {
731 log_error("stat(%s) failed: %m", i->path);
732 return -errno;
733 }
734
Michal Schmidte7aee752012-06-14 16:01:19 +0200735 if ((st.st_mode & S_IFMT) != file_type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100736 log_error("%s is not a device node.", i->path);
737 return -EEXIST;
738 }
739
740 r = item_set_perms(i, i->path);
741 if (r < 0)
742 return r;
743
744 break;
745 }
746
Michal Schmidt777b87e2011-12-16 18:27:35 +0100747 case RELABEL_PATH:
748
749 r = glob_item(i, item_set_perms);
750 if (r < 0)
751 return 0;
752 break;
753
Michal Schmidta8d88782011-12-15 23:11:07 +0100754 case RECURSIVE_RELABEL_PATH:
755
756 r = glob_item(i, recursive_relabel);
757 if (r < 0)
758 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200759 }
760
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200761 log_debug("%s created successfully.", i->path);
762
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100763 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200764}
765
Michal Schmidta0896122011-12-15 21:32:50 +0100766static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200767 int r;
768
769 assert(i);
770
771 switch (i->type) {
772
773 case CREATE_FILE:
774 case TRUNCATE_FILE:
775 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200776 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100777 case CREATE_SYMLINK:
778 case CREATE_BLOCK_DEVICE:
779 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200780 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100781 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100782 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100783 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100784 case WRITE_FILE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200785 break;
786
787 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100788 if (remove(instance) < 0 && errno != ENOENT) {
789 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200790 return -errno;
791 }
792
793 break;
794
795 case TRUNCATE_DIRECTORY:
796 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +0200797 /* FIXME: we probably should use dir_cleanup() here
798 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +0200799 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Lennart Poettering468d7262012-01-17 15:04:12 +0100800 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100801 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200802 return r;
803 }
804
805 break;
806 }
807
808 return 0;
809}
810
Michal Schmidta0896122011-12-15 21:32:50 +0100811static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100812 int r = 0;
813
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100814 assert(i);
815
816 switch (i->type) {
817
818 case CREATE_FILE:
819 case TRUNCATE_FILE:
820 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200821 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100822 case CREATE_SYMLINK:
823 case CREATE_CHAR_DEVICE:
824 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100825 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +0100826 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100827 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100828 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100829 case WRITE_FILE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100830 break;
831
832 case REMOVE_PATH:
833 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100834 case RECURSIVE_REMOVE_PATH:
835 r = glob_item(i, remove_item_instance);
836 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100837 }
838
Michal Schmidt99e68c02011-12-15 23:45:26 +0100839 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100840}
841
Michal Sekletar78a92a52013-01-18 16:13:08 +0100842static int clean_item_instance(Item *i, const char* instance) {
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500843 DIR _cleanup_closedir_ *d = NULL;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100844 struct stat s, ps;
845 bool mountpoint;
846 int r;
847 usec_t cutoff, n;
848
849 assert(i);
850
851 if (!i->age_set)
852 return 0;
853
854 n = now(CLOCK_REALTIME);
855 if (n < i->age)
856 return 0;
857
858 cutoff = n - i->age;
859
860 d = opendir(instance);
861 if (!d) {
862 if (errno == ENOENT || errno == ENOTDIR)
863 return 0;
864
865 log_error("Failed to open directory %s: %m", i->path);
866 return -errno;
867 }
868
869 if (fstat(dirfd(d), &s) < 0) {
870 log_error("stat(%s) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500871 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100872 }
873
874 if (!S_ISDIR(s.st_mode)) {
875 log_error("%s is not a directory.", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500876 return -ENOTDIR;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100877 }
878
879 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
880 log_error("stat(%s/..) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500881 return -errno;
Michal Sekletar78a92a52013-01-18 16:13:08 +0100882 }
883
884 mountpoint = s.st_dev != ps.st_dev ||
885 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
886
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500887 r = dir_cleanup(i, instance, d, &s, cutoff, s.st_dev, mountpoint,
888 MAX_DEPTH, i->keep_first_level);
Michal Sekletar78a92a52013-01-18 16:13:08 +0100889 return r;
890}
891
892static int clean_item(Item *i) {
893 int r = 0;
894
895 assert(i);
896
897 switch (i->type) {
898 case CREATE_DIRECTORY:
899 case TRUNCATE_DIRECTORY:
900 case IGNORE_PATH:
901 clean_item_instance(i, i->path);
902 break;
903 case IGNORE_DIRECTORY_PATH:
904 r = glob_item(i, clean_item_instance);
905 break;
906 default:
907 break;
908 }
909
910 return r;
911}
912
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200913static int process_item(Item *i) {
914 int r, q, p;
915
916 assert(i);
917
918 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100919 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200920 p = arg_clean ? clean_item(i) : 0;
921
922 if (r < 0)
923 return r;
924
925 if (q < 0)
926 return q;
927
928 return p;
929}
930
931static void item_free(Item *i) {
932 assert(i);
933
934 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100935 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200936 free(i);
937}
938
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200939static bool item_equal(Item *a, Item *b) {
940 assert(a);
941 assert(b);
942
943 if (!streq_ptr(a->path, b->path))
944 return false;
945
946 if (a->type != b->type)
947 return false;
948
949 if (a->uid_set != b->uid_set ||
950 (a->uid_set && a->uid != b->uid))
951 return false;
952
953 if (a->gid_set != b->gid_set ||
954 (a->gid_set && a->gid != b->gid))
955 return false;
956
957 if (a->mode_set != b->mode_set ||
958 (a->mode_set && a->mode != b->mode))
959 return false;
960
961 if (a->age_set != b->age_set ||
962 (a->age_set && a->age != b->age))
963 return false;
964
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100965 if ((a->type == CREATE_FILE ||
966 a->type == TRUNCATE_FILE ||
967 a->type == WRITE_FILE ||
968 a->type == CREATE_SYMLINK) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +0100969 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +0100970 return false;
971
972 if ((a->type == CREATE_CHAR_DEVICE ||
973 a->type == CREATE_BLOCK_DEVICE) &&
974 a->major_minor != b->major_minor)
975 return false;
976
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200977 return true;
978}
979
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100980static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200981 Item *i, *existing;
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500982 char _cleanup_free_
983 *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +0100984 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200985 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100986 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +0200987
988 assert(fname);
989 assert(line >= 1);
990 assert(buffer);
991
Lennart Poettering468d7262012-01-17 15:04:12 +0100992 i = new0(Item, 1);
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700993 if (!i)
994 return log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200995
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -0500996 r = sscanf(buffer,
997 "%c %ms %ms %ms %ms %ms %n",
Michal Schmidt66ccd032011-12-15 21:31:14 +0100998 &type,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100999 &i->path,
1000 &mode,
1001 &user,
1002 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +01001003 &age,
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001004 &n);
1005 if (r < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001006 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001007 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +02001008 goto finish;
1009 }
1010
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001011 if (n >= 0) {
1012 n += strspn(buffer+n, WHITESPACE);
1013 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
1014 i->argument = unquote(buffer+n, "\"");
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001015 if (!i->argument)
1016 return log_oom();
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001017 }
1018 }
1019
Michal Schmidt777b87e2011-12-16 18:27:35 +01001020 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001021
Michal Schmidt777b87e2011-12-16 18:27:35 +01001022 case CREATE_FILE:
1023 case TRUNCATE_FILE:
1024 case CREATE_DIRECTORY:
1025 case TRUNCATE_DIRECTORY:
1026 case CREATE_FIFO:
1027 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001028 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001029 case REMOVE_PATH:
1030 case RECURSIVE_REMOVE_PATH:
1031 case RELABEL_PATH:
1032 case RECURSIVE_RELABEL_PATH:
1033 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001034
1035 case CREATE_SYMLINK:
1036 if (!i->argument) {
1037 log_error("[%s:%u] Symlink file requires argument.", fname, line);
1038 r = -EBADMSG;
1039 goto finish;
1040 }
1041 break;
1042
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001043 case WRITE_FILE:
1044 if (!i->argument) {
1045 log_error("[%s:%u] Write file requires argument.", fname, line);
1046 r = -EBADMSG;
1047 goto finish;
1048 }
1049 break;
1050
Lennart Poettering468d7262012-01-17 15:04:12 +01001051 case CREATE_CHAR_DEVICE:
1052 case CREATE_BLOCK_DEVICE: {
1053 unsigned major, minor;
1054
1055 if (!i->argument) {
1056 log_error("[%s:%u] Device file requires argument.", fname, line);
1057 r = -EBADMSG;
1058 goto finish;
1059 }
1060
1061 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1062 log_error("[%s:%u] Can't parse device file major/minor '%s'.", fname, line, i->argument);
1063 r = -EBADMSG;
1064 goto finish;
1065 }
1066
1067 i->major_minor = makedev(major, minor);
1068 break;
1069 }
1070
Michal Schmidt777b87e2011-12-16 18:27:35 +01001071 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001072 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001073 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001074 goto finish;
1075 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001076
Michal Schmidta8d88782011-12-15 23:11:07 +01001077 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001078
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001079 if (!path_is_absolute(i->path)) {
1080 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
1081 r = -EBADMSG;
1082 goto finish;
1083 }
1084
1085 path_kill_slashes(i->path);
1086
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001087 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001088 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001089 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001090 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001091
1092 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001093 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001094
Lennart Poetteringd05c5032012-07-16 12:34:54 +02001095 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001096 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001097 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
1098 goto finish;
1099 }
1100
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001101 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001102 }
1103
1104 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001105 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001106
Lennart Poettering4b678342011-07-23 01:17:59 +02001107 r = get_group_creds(&g, &i->gid);
1108 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001109 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
1110 goto finish;
1111 }
1112
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001113 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001114 }
1115
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001116 if (mode && !streq(mode, "-")) {
1117 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001118
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001119 if (sscanf(mode, "%o", &m) != 1) {
1120 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
1121 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001122 goto finish;
1123 }
1124
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001125 i->mode = m;
1126 i->mode_set = true;
1127 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001128 i->mode =
1129 i->type == CREATE_DIRECTORY ||
1130 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001131
1132 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001133 const char *a = age;
1134
1135 if (*a == '~') {
1136 i->keep_first_level = true;
1137 a++;
1138 }
1139
1140 if (parse_usec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001141 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
1142 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001143 goto finish;
1144 }
1145
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001146 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001147 }
1148
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001149 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001150
Lennart Poettering468d7262012-01-17 15:04:12 +01001151 existing = hashmap_get(h, i->path);
1152 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001153
1154 /* Two identical items are fine */
1155 if (!item_equal(existing, i))
1156 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1157
1158 r = 0;
1159 goto finish;
1160 }
1161
Lennart Poettering468d7262012-01-17 15:04:12 +01001162 r = hashmap_put(h, i->path, i);
1163 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001164 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001165 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001166 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001167
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001168 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001169 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001170
1171finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001172 if (i)
1173 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001174
1175 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001176}
1177
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001178static int help(void) {
1179
Lennart Poettering522d4a42011-02-13 15:08:15 +01001180 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1181 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001182 " -h --help Show this help\n"
1183 " --create Create marked files/directories\n"
1184 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001185 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +01001186 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001187 program_invocation_short_name);
1188
1189 return 0;
1190}
1191
1192static int parse_argv(int argc, char *argv[]) {
1193
1194 enum {
1195 ARG_CREATE,
1196 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001197 ARG_REMOVE,
1198 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001199 };
1200
1201 static const struct option options[] = {
1202 { "help", no_argument, NULL, 'h' },
1203 { "create", no_argument, NULL, ARG_CREATE },
1204 { "clean", no_argument, NULL, ARG_CLEAN },
1205 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001206 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001207 { NULL, 0, NULL, 0 }
1208 };
1209
1210 int c;
1211
1212 assert(argc >= 0);
1213 assert(argv);
1214
1215 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1216
1217 switch (c) {
1218
1219 case 'h':
1220 help();
1221 return 0;
1222
1223 case ARG_CREATE:
1224 arg_create = true;
1225 break;
1226
1227 case ARG_CLEAN:
1228 arg_clean = true;
1229 break;
1230
1231 case ARG_REMOVE:
1232 arg_remove = true;
1233 break;
1234
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001235 case ARG_PREFIX:
1236 arg_prefix = optarg;
1237 break;
1238
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001239 case '?':
1240 return -EINVAL;
1241
1242 default:
1243 log_error("Unknown option code %c", c);
1244 return -EINVAL;
1245 }
1246 }
1247
1248 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001249 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001250 return -EINVAL;
1251 }
1252
1253 return 1;
1254}
1255
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001256static int read_config_file(const char *fn, bool ignore_enoent) {
1257 FILE *f;
1258 unsigned v = 0;
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001259 int r;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001260 Iterator iterator;
1261 Item *i;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001262
1263 assert(fn);
1264
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001265 r = search_and_fopen_nulstr(fn, "re", conf_file_dirs, &f);
1266 if (r < 0) {
1267 if (ignore_enoent && r == -ENOENT)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001268 return 0;
1269
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001270 log_error("Failed to open '%s', ignoring: %s", fn, strerror(-r));
1271 return r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001272 }
1273
Kay Sievers772f8372011-04-25 21:38:21 +02001274 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001275 for (;;) {
1276 char line[LINE_MAX], *l;
1277 int k;
1278
1279 if (!(fgets(line, sizeof(line), f)))
1280 break;
1281
1282 v++;
1283
1284 l = strstrip(line);
1285 if (*l == '#' || *l == 0)
1286 continue;
1287
1288 if ((k = parse_line(fn, v, l)) < 0)
1289 if (r == 0)
1290 r = k;
1291 }
1292
Michal Sekletar78a92a52013-01-18 16:13:08 +01001293 /* we have to determine age parameter for each entry of type X */
1294 HASHMAP_FOREACH(i, globs, iterator) {
1295 Iterator iter;
1296 Item *j, *candidate_item = NULL;
1297
1298 if (i->type != IGNORE_DIRECTORY_PATH)
1299 continue;
1300
1301 HASHMAP_FOREACH(j, items, iter) {
1302 if (j->type != CREATE_DIRECTORY && j->type != TRUNCATE_DIRECTORY)
1303 continue;
1304
1305 if (path_equal(j->path, i->path)) {
1306 candidate_item = j;
1307 break;
1308 }
1309
1310 if ((!candidate_item && path_startswith(i->path, j->path)) ||
1311 (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
1312 candidate_item = j;
1313 }
1314
1315 if (candidate_item) {
1316 i->age = candidate_item->age;
1317 i->age_set = true;
1318 }
1319 }
1320
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001321 if (ferror(f)) {
1322 log_error("Failed to read from file %s: %m", fn);
1323 if (r == 0)
1324 r = -EIO;
1325 }
1326
1327 fclose(f);
1328
1329 return r;
1330}
1331
Lennart Poettering5008d582010-09-28 02:34:02 +02001332int main(int argc, char *argv[]) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001333 int r, k;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001334 Item *i;
1335 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001336
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001337 r = parse_argv(argc, argv);
1338 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001339 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1340
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001341 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001342 log_parse_environment();
1343 log_open();
1344
Lennart Poettering4c126262011-08-01 20:52:18 +02001345 umask(0022);
1346
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001347 label_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001348
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001349 items = hashmap_new(string_hash_func, string_compare_func);
1350 globs = hashmap_new(string_hash_func, string_compare_func);
1351
1352 if (!items || !globs) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001353 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001354 goto finish;
1355 }
1356
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001357 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001358
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001359 if (optind < argc) {
1360 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001361
Dave Reisner91256702012-06-08 22:31:19 -04001362 for (j = optind; j < argc; j++) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001363 k = read_config_file(argv[j], false);
1364 if (k < 0 && r == 0)
1365 r = k;
Dave Reisner91256702012-06-08 22:31:19 -04001366 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001367
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001368 } else {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001369 _cleanup_strv_free_ char **files = NULL;
1370 char **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001371
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001372 r = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001373 if (r < 0) {
Kay Sievers44143302011-04-28 23:51:24 +02001374 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
1375 goto finish;
1376 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001377
Kay Sievers772f8372011-04-25 21:38:21 +02001378 STRV_FOREACH(f, files) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001379 k = read_config_file(*f, true);
1380 if (k < 0 && r == 0)
1381 r = k;
Lennart Poettering5008d582010-09-28 02:34:02 +02001382 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001383 }
1384
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001385 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001386 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001387
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001388 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001389 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001390
Lennart Poettering5008d582010-09-28 02:34:02 +02001391finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001392 while ((i = hashmap_steal_first(items)))
1393 item_free(i);
1394
Lennart Poettering17b90522011-02-14 21:55:06 +01001395 while ((i = hashmap_steal_first(globs)))
1396 item_free(i);
1397
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001398 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001399 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001400
Lennart Poettering17b90522011-02-14 21:55:06 +01001401 set_free_free(unix_sockets);
1402
Lennart Poettering29003cf2010-10-19 19:36:45 +02001403 label_finish();
1404
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01001405 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Lennart Poettering5008d582010-09-28 02:34:02 +02001406}