blob: aebc4bb088bad5e99360fda92635a3281d085b86 [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 Poettering5008d582010-09-28 02:34:02 +020041
42#include "log.h"
43#include "util.h"
Kay Sievers49e942b2012-04-10 21:54:31 +020044#include "mkdir.h"
Kay Sievers9eb977d2012-05-07 21:36:12 +020045#include "path-util.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020046#include "strv.h"
47#include "label.h"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020048#include "set.h"
Kay Sievers2c210442012-05-07 18:55:45 +020049#include "conf-files.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020050
Andreas Jaeger01000472010-09-29 10:08:24 +020051/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020052 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020053 * properly owned directories beneath /tmp, /var/tmp, /run, which are
54 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020055
Michal Schmidt66ccd032011-12-15 21:31:14 +010056typedef enum ItemType {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010057 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020058 CREATE_FILE = 'f',
59 TRUNCATE_FILE = 'F',
Lennart Poettering31ed59c2012-01-18 16:39:04 +010060 WRITE_FILE = 'w',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020061 CREATE_DIRECTORY = 'd',
62 TRUNCATE_DIRECTORY = 'D',
Lennart Poetteringee17ee72011-07-12 03:56:56 +020063 CREATE_FIFO = 'p',
Lennart Poettering468d7262012-01-17 15:04:12 +010064 CREATE_SYMLINK = 'L',
65 CREATE_CHAR_DEVICE = 'c',
66 CREATE_BLOCK_DEVICE = 'b',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010067
68 /* These ones take globs */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020069 IGNORE_PATH = 'x',
70 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010071 RECURSIVE_REMOVE_PATH = 'R',
Michal Schmidt777b87e2011-12-16 18:27:35 +010072 RELABEL_PATH = 'z',
Michal Schmidta8d88782011-12-15 23:11:07 +010073 RECURSIVE_RELABEL_PATH = 'Z'
Michal Schmidt66ccd032011-12-15 21:31:14 +010074} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020075
76typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +010077 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020078
79 char *path;
Lennart Poettering468d7262012-01-17 15:04:12 +010080 char *argument;
Lennart Poettering5008d582010-09-28 02:34:02 +020081 uid_t uid;
82 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020083 mode_t mode;
84 usec_t age;
85
Lennart Poettering468d7262012-01-17 15:04:12 +010086 dev_t major_minor;
87
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020088 bool uid_set:1;
89 bool gid_set:1;
90 bool mode_set:1;
91 bool age_set:1;
92} Item;
93
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010094static Hashmap *items = NULL, *globs = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +010095static Set *unix_sockets = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020096
97static bool arg_create = false;
98static bool arg_clean = false;
99static bool arg_remove = false;
100
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100101static const char *arg_prefix = NULL;
102
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200103#define MAX_DEPTH 256
104
Michal Schmidt66ccd032011-12-15 21:31:14 +0100105static bool needs_glob(ItemType t) {
Michal Schmidt777b87e2011-12-16 18:27:35 +0100106 return t == IGNORE_PATH || t == REMOVE_PATH || t == RECURSIVE_REMOVE_PATH || t == RELABEL_PATH || t == RECURSIVE_RELABEL_PATH;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100107}
108
109static struct Item* find_glob(Hashmap *h, const char *match) {
110 Item *j;
111 Iterator i;
112
113 HASHMAP_FOREACH(j, h, i)
114 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
115 return j;
116
117 return NULL;
118}
119
Lennart Poettering17b90522011-02-14 21:55:06 +0100120static void load_unix_sockets(void) {
121 FILE *f = NULL;
122 char line[LINE_MAX];
123
124 if (unix_sockets)
125 return;
126
127 /* We maintain a cache of the sockets we found in
128 * /proc/net/unix to speed things up a little. */
129
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100130 unix_sockets = set_new(string_hash_func, string_compare_func);
131 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100132 return;
133
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100134 f = fopen("/proc/net/unix", "re");
135 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100136 return;
137
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100138 /* Skip header */
139 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100140 goto fail;
141
142 for (;;) {
143 char *p, *s;
144 int k;
145
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100146 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100147 break;
148
149 truncate_nl(line);
150
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100151 p = strchr(line, ':');
152 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100153 continue;
154
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100155 if (strlen(p) < 37)
156 continue;
157
158 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100159 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100160 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100161 p += strspn(p, WHITESPACE);
162
163 if (*p != '/')
164 continue;
165
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100166 s = strdup(p);
167 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100168 goto fail;
169
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100170 path_kill_slashes(s);
171
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100172 k = set_put(unix_sockets, s);
173 if (k < 0) {
Lennart Poettering17b90522011-02-14 21:55:06 +0100174 free(s);
175
176 if (k != -EEXIST)
177 goto fail;
178 }
179 }
180
Thomas Jarosch10d975f2011-10-05 22:30:49 +0200181 fclose(f);
Lennart Poettering17b90522011-02-14 21:55:06 +0100182 return;
183
184fail:
185 set_free_free(unix_sockets);
186 unix_sockets = NULL;
187
188 if (f)
189 fclose(f);
190}
191
192static bool unix_socket_alive(const char *fn) {
193 assert(fn);
194
195 load_unix_sockets();
196
197 if (unix_sockets)
198 return !!set_get(unix_sockets, (char*) fn);
199
200 /* We don't know, so assume yes */
201 return true;
202}
203
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200204static int dir_cleanup(
205 const char *p,
206 DIR *d,
207 const struct stat *ds,
208 usec_t cutoff,
209 dev_t rootdev,
210 bool mountpoint,
211 int maxdepth)
212{
213 struct dirent *dent;
214 struct timespec times[2];
215 bool deleted = false;
216 char *sub_path = NULL;
217 int r = 0;
218
219 while ((dent = readdir(d))) {
220 struct stat s;
221 usec_t age;
222
223 if (streq(dent->d_name, ".") ||
224 streq(dent->d_name, ".."))
225 continue;
226
227 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
228
229 if (errno != ENOENT) {
230 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
231 r = -errno;
232 }
233
234 continue;
235 }
236
237 /* Stay on the same filesystem */
238 if (s.st_dev != rootdev)
239 continue;
240
241 /* Do not delete read-only files owned by root */
242 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
243 continue;
244
245 free(sub_path);
246 sub_path = NULL;
247
248 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
249 log_error("Out of memory");
250 r = -ENOMEM;
251 goto finish;
252 }
253
254 /* Is there an item configured for this path? */
255 if (hashmap_get(items, sub_path))
256 continue;
257
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100258 if (find_glob(globs, sub_path))
259 continue;
260
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200261 if (S_ISDIR(s.st_mode)) {
262
263 if (mountpoint &&
264 streq(dent->d_name, "lost+found") &&
265 s.st_uid == 0)
266 continue;
267
268 if (maxdepth <= 0)
269 log_warning("Reached max depth on %s.", sub_path);
270 else {
271 DIR *sub_dir;
272 int q;
273
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200274 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200275 if (sub_dir == NULL) {
276 if (errno != ENOENT) {
277 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
278 r = -errno;
279 }
280
281 continue;
282 }
283
284 q = dir_cleanup(sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1);
285 closedir(sub_dir);
286
287 if (q < 0)
288 r = q;
289 }
290
291 /* Ignore ctime, we change it when deleting */
292 age = MAX(timespec_load(&s.st_mtim),
293 timespec_load(&s.st_atim));
294 if (age >= cutoff)
295 continue;
296
297 log_debug("rmdir '%s'\n", sub_path);
298
299 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
300 if (errno != ENOENT && errno != ENOTEMPTY) {
301 log_error("rmdir(%s): %m", sub_path);
302 r = -errno;
303 }
304 }
305
306 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100307 /* Skip files for which the sticky bit is
308 * set. These are semantics we define, and are
309 * unknown elsewhere. See XDG_RUNTIME_DIR
310 * specification for details. */
311 if (s.st_mode & S_ISVTX)
312 continue;
313
Lennart Poettering17b90522011-02-14 21:55:06 +0100314 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200315 if (streq(dent->d_name, ".journal") &&
316 s.st_uid == 0)
317 continue;
318
319 if (streq(dent->d_name, "aquota.user") ||
320 streq(dent->d_name, "aquota.group"))
321 continue;
322 }
323
Lennart Poettering17b90522011-02-14 21:55:06 +0100324 /* Ignore sockets that are listed in /proc/net/unix */
325 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
326 continue;
327
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100328 /* Ignore device nodes */
329 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
330 continue;
331
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200332 age = MAX3(timespec_load(&s.st_mtim),
333 timespec_load(&s.st_atim),
334 timespec_load(&s.st_ctim));
335
336 if (age >= cutoff)
337 continue;
338
339 log_debug("unlink '%s'\n", sub_path);
340
341 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
342 if (errno != ENOENT) {
343 log_error("unlink(%s): %m", sub_path);
344 r = -errno;
345 }
346 }
347
348 deleted = true;
349 }
350 }
351
352finish:
353 if (deleted) {
354 /* Restore original directory timestamps */
355 times[0] = ds->st_atim;
356 times[1] = ds->st_mtim;
357
358 if (futimens(dirfd(d), times) < 0)
359 log_error("utimensat(%s): %m", p);
360 }
361
362 free(sub_path);
363
364 return r;
365}
366
367static int clean_item(Item *i) {
368 DIR *d;
369 struct stat s, ps;
370 bool mountpoint;
371 int r;
372 usec_t cutoff, n;
373
374 assert(i);
375
376 if (i->type != CREATE_DIRECTORY &&
377 i->type != TRUNCATE_DIRECTORY &&
378 i->type != IGNORE_PATH)
379 return 0;
380
381 if (!i->age_set || i->age <= 0)
382 return 0;
383
384 n = now(CLOCK_REALTIME);
385 if (n < i->age)
386 return 0;
387
388 cutoff = n - i->age;
389
390 d = opendir(i->path);
391 if (!d) {
392 if (errno == ENOENT)
393 return 0;
394
395 log_error("Failed to open directory %s: %m", i->path);
396 return -errno;
397 }
398
399 if (fstat(dirfd(d), &s) < 0) {
400 log_error("stat(%s) failed: %m", i->path);
401 r = -errno;
402 goto finish;
403 }
404
405 if (!S_ISDIR(s.st_mode)) {
406 log_error("%s is not a directory.", i->path);
407 r = -ENOTDIR;
408 goto finish;
409 }
410
411 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
412 log_error("stat(%s/..) failed: %m", i->path);
413 r = -errno;
414 goto finish;
415 }
416
417 mountpoint = s.st_dev != ps.st_dev ||
418 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
419
420 r = dir_cleanup(i->path, d, &s, cutoff, s.st_dev, mountpoint, MAX_DEPTH);
421
422finish:
423 if (d)
424 closedir(d);
425
426 return r;
427}
428
Michal Schmidt062e01b2011-12-16 18:00:11 +0100429static int item_set_perms(Item *i, const char *path) {
430 /* not using i->path directly because it may be a glob */
431 if (i->mode_set)
432 if (chmod(path, i->mode) < 0) {
433 log_error("chmod(%s) failed: %m", path);
434 return -errno;
435 }
436
437 if (i->uid_set || i->gid_set)
438 if (chown(path,
439 i->uid_set ? i->uid : (uid_t) -1,
440 i->gid_set ? i->gid : (gid_t) -1) < 0) {
441
442 log_error("chown(%s) failed: %m", path);
443 return -errno;
444 }
445
446 return label_fix(path, false);
447}
448
449static int recursive_relabel_children(Item *i, const char *path) {
Michal Schmidta8d88782011-12-15 23:11:07 +0100450 DIR *d;
451 int ret = 0;
452
453 /* This returns the first error we run into, but nevertheless
454 * tries to go on */
455
456 d = opendir(path);
457 if (!d)
458 return errno == ENOENT ? 0 : -errno;
459
460 for (;;) {
461 struct dirent buf, *de;
462 bool is_dir;
463 int r;
464 char *entry_path;
465
466 r = readdir_r(d, &buf, &de);
467 if (r != 0) {
468 if (ret == 0)
469 ret = -r;
470 break;
471 }
472
473 if (!de)
474 break;
475
476 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
477 continue;
478
479 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
480 if (ret == 0)
481 ret = -ENOMEM;
482 continue;
483 }
484
485 if (de->d_type == DT_UNKNOWN) {
486 struct stat st;
487
488 if (lstat(entry_path, &st) < 0) {
489 if (ret == 0 && errno != ENOENT)
490 ret = -errno;
491 free(entry_path);
492 continue;
493 }
494
495 is_dir = S_ISDIR(st.st_mode);
496
497 } else
498 is_dir = de->d_type == DT_DIR;
499
Michal Schmidt062e01b2011-12-16 18:00:11 +0100500 r = item_set_perms(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100501 if (r < 0) {
502 if (ret == 0 && r != -ENOENT)
503 ret = r;
504 free(entry_path);
505 continue;
506 }
507
508 if (is_dir) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100509 r = recursive_relabel_children(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100510 if (r < 0 && ret == 0)
511 ret = r;
512 }
513
514 free(entry_path);
515 }
516
517 closedir(d);
518
519 return ret;
520}
521
522static int recursive_relabel(Item *i, const char *path) {
523 int r;
524 struct stat st;
525
Michal Schmidt062e01b2011-12-16 18:00:11 +0100526 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100527 if (r < 0)
528 return r;
529
530 if (lstat(path, &st) < 0)
531 return -errno;
532
533 if (S_ISDIR(st.st_mode))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100534 r = recursive_relabel_children(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100535
536 return r;
537}
538
Michal Schmidt99e68c02011-12-15 23:45:26 +0100539static int glob_item(Item *i, int (*action)(Item *, const char *)) {
540 int r = 0, k;
541 glob_t g;
542 char **fn;
543
544 zero(g);
545
546 errno = 0;
547 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
548
549 if (k != GLOB_NOMATCH) {
550 if (errno != 0)
551 errno = EIO;
552
553 log_error("glob(%s) failed: %m", i->path);
554 return -errno;
555 }
556 }
557
558 STRV_FOREACH(fn, g.gl_pathv)
559 if ((k = action(i, *fn)) < 0)
560 r = k;
561
562 globfree(&g);
563 return r;
564}
565
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200566static int create_item(Item *i) {
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200567 int r, e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200568 mode_t u;
569 struct stat st;
570
571 assert(i);
572
573 switch (i->type) {
574
575 case IGNORE_PATH:
576 case REMOVE_PATH:
577 case RECURSIVE_REMOVE_PATH:
578 return 0;
579
580 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100581 case TRUNCATE_FILE:
582 case WRITE_FILE: {
583 int fd, flags;
584
585 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
586 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200587
588 u = umask(0);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200589 label_context_set(i->path, S_IFREG);
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100590 fd = open(i->path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200591 e = errno;
592 label_context_clear();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200593 umask(u);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200594 errno = e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200595
596 if (fd < 0) {
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100597 if (i->type == WRITE_FILE && errno == ENOENT)
598 break;
599
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200600 log_error("Failed to create file %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100601 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200602 }
603
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100604 if (i->argument) {
605 ssize_t n;
606 size_t l;
607 struct iovec iovec[2];
608 static const char new_line = '\n';
609
610 l = strlen(i->argument);
611
612 zero(iovec);
613 iovec[0].iov_base = i->argument;
614 iovec[0].iov_len = l;
615
616 iovec[1].iov_base = (void*) &new_line;
617 iovec[1].iov_len = 1;
618
619 n = writev(fd, iovec, 2);
Lennart Poettering03ad1132012-05-15 14:34:33 +0200620
621 /* It's OK if we don't write the trailing
622 * newline, hence we check for l, instead of
623 * l+1 here. Files in /sys often refuse
624 * writing of the trailing newline. */
625 if (n < 0 || (size_t) n < l) {
626 log_error("Failed to write file %s: %s", i->path, n < 0 ? strerror(-n) : "Short write");
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100627 close_nointr_nofail(fd);
628 return n < 0 ? n : -EIO;
629 }
630 }
631
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100632 close_nointr_nofail(fd);
633
634 if (stat(i->path, &st) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200635 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100636 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200637 }
638
639 if (!S_ISREG(st.st_mode)) {
640 log_error("%s is not a file.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100641 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200642 }
643
Michal Schmidt062e01b2011-12-16 18:00:11 +0100644 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100645 if (r < 0)
646 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200647
648 break;
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100649 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200650
651 case TRUNCATE_DIRECTORY:
652 case CREATE_DIRECTORY:
653
654 u = umask(0);
Kay Sieversd2e54fa2012-05-31 12:40:20 +0200655 mkdir_parents_label(i->path, 0755);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200656 r = mkdir(i->path, i->mode);
657 umask(u);
658
659 if (r < 0 && errno != EEXIST) {
660 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100661 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200662 }
663
664 if (stat(i->path, &st) < 0) {
665 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100666 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200667 }
668
669 if (!S_ISDIR(st.st_mode)) {
670 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100671 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200672 }
673
Michal Schmidt062e01b2011-12-16 18:00:11 +0100674 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100675 if (r < 0)
676 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200677
678 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200679
680 case CREATE_FIFO:
681
682 u = umask(0);
683 r = mkfifo(i->path, i->mode);
684 umask(u);
685
686 if (r < 0 && errno != EEXIST) {
687 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100688 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200689 }
690
691 if (stat(i->path, &st) < 0) {
692 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100693 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200694 }
695
696 if (!S_ISFIFO(st.st_mode)) {
697 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100698 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200699 }
700
Michal Schmidt062e01b2011-12-16 18:00:11 +0100701 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100702 if (r < 0)
703 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200704
705 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100706
Lennart Poettering468d7262012-01-17 15:04:12 +0100707 case CREATE_SYMLINK: {
708 char *x;
709
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200710 label_context_set(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100711 r = symlink(i->argument, i->path);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200712 e = errno;
713 label_context_clear();
714 errno = e;
715
Lennart Poettering468d7262012-01-17 15:04:12 +0100716 if (r < 0 && errno != EEXIST) {
717 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
718 return -errno;
719 }
720
721 r = readlink_malloc(i->path, &x);
722 if (r < 0) {
723 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
724 return -errno;
725 }
726
727 if (!streq(i->argument, x)) {
728 free(x);
729 log_error("%s is not the right symlinks.", i->path);
730 return -EEXIST;
731 }
732
733 free(x);
734 break;
735 }
736
737 case CREATE_BLOCK_DEVICE:
738 case CREATE_CHAR_DEVICE: {
739
740 u = umask(0);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200741 label_context_set(i->path, CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +0100742 r = mknod(i->path, i->mode | (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR), i->major_minor);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200743 e = errno;
744 label_context_clear();
Lennart Poettering468d7262012-01-17 15:04:12 +0100745 umask(u);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200746 errno = e;
Lennart Poettering468d7262012-01-17 15:04:12 +0100747
748 if (r < 0 && errno != EEXIST) {
749 log_error("Failed to create device node %s: %m", i->path);
750 return -errno;
751 }
752
753 if (stat(i->path, &st) < 0) {
754 log_error("stat(%s) failed: %m", i->path);
755 return -errno;
756 }
757
758 if (i->type == CREATE_BLOCK_DEVICE ? !S_ISBLK(st.st_mode) : !S_ISCHR(st.st_mode)) {
759 log_error("%s is not a device node.", i->path);
760 return -EEXIST;
761 }
762
763 r = item_set_perms(i, i->path);
764 if (r < 0)
765 return r;
766
767 break;
768 }
769
Michal Schmidt777b87e2011-12-16 18:27:35 +0100770 case RELABEL_PATH:
771
772 r = glob_item(i, item_set_perms);
773 if (r < 0)
774 return 0;
775 break;
776
Michal Schmidta8d88782011-12-15 23:11:07 +0100777 case RECURSIVE_RELABEL_PATH:
778
779 r = glob_item(i, recursive_relabel);
780 if (r < 0)
781 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200782 }
783
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200784 log_debug("%s created successfully.", i->path);
785
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100786 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200787}
788
Michal Schmidta0896122011-12-15 21:32:50 +0100789static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200790 int r;
791
792 assert(i);
793
794 switch (i->type) {
795
796 case CREATE_FILE:
797 case TRUNCATE_FILE:
798 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200799 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100800 case CREATE_SYMLINK:
801 case CREATE_BLOCK_DEVICE:
802 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200803 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100804 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100805 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100806 case WRITE_FILE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200807 break;
808
809 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100810 if (remove(instance) < 0 && errno != ENOENT) {
811 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200812 return -errno;
813 }
814
815 break;
816
817 case TRUNCATE_DIRECTORY:
818 case RECURSIVE_REMOVE_PATH:
Lennart Poettering468d7262012-01-17 15:04:12 +0100819 r = rm_rf(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
820 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100821 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200822 return r;
823 }
824
825 break;
826 }
827
828 return 0;
829}
830
Michal Schmidta0896122011-12-15 21:32:50 +0100831static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100832 int r = 0;
833
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100834 assert(i);
835
836 switch (i->type) {
837
838 case CREATE_FILE:
839 case TRUNCATE_FILE:
840 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200841 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100842 case CREATE_SYMLINK:
843 case CREATE_CHAR_DEVICE:
844 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100845 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100846 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100847 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100848 case WRITE_FILE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100849 break;
850
851 case REMOVE_PATH:
852 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100853 case RECURSIVE_REMOVE_PATH:
854 r = glob_item(i, remove_item_instance);
855 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100856 }
857
Michal Schmidt99e68c02011-12-15 23:45:26 +0100858 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100859}
860
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200861static int process_item(Item *i) {
862 int r, q, p;
863
864 assert(i);
865
866 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100867 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200868 p = arg_clean ? clean_item(i) : 0;
869
870 if (r < 0)
871 return r;
872
873 if (q < 0)
874 return q;
875
876 return p;
877}
878
879static void item_free(Item *i) {
880 assert(i);
881
882 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100883 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200884 free(i);
885}
886
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200887static bool item_equal(Item *a, Item *b) {
888 assert(a);
889 assert(b);
890
891 if (!streq_ptr(a->path, b->path))
892 return false;
893
894 if (a->type != b->type)
895 return false;
896
897 if (a->uid_set != b->uid_set ||
898 (a->uid_set && a->uid != b->uid))
899 return false;
900
901 if (a->gid_set != b->gid_set ||
902 (a->gid_set && a->gid != b->gid))
903 return false;
904
905 if (a->mode_set != b->mode_set ||
906 (a->mode_set && a->mode != b->mode))
907 return false;
908
909 if (a->age_set != b->age_set ||
910 (a->age_set && a->age != b->age))
911 return false;
912
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100913 if ((a->type == CREATE_FILE ||
914 a->type == TRUNCATE_FILE ||
915 a->type == WRITE_FILE ||
916 a->type == CREATE_SYMLINK) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +0100917 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +0100918 return false;
919
920 if ((a->type == CREATE_CHAR_DEVICE ||
921 a->type == CREATE_BLOCK_DEVICE) &&
922 a->major_minor != b->major_minor)
923 return false;
924
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200925 return true;
926}
927
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100928static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200929 Item *i, *existing;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200930 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +0100931 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200932 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100933 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +0200934
935 assert(fname);
936 assert(line >= 1);
937 assert(buffer);
938
Lennart Poettering468d7262012-01-17 15:04:12 +0100939 i = new0(Item, 1);
940 if (!i) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200941 log_error("Out of memory");
942 return -ENOMEM;
943 }
944
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100945 if (sscanf(buffer,
946 "%c "
947 "%ms "
948 "%ms "
949 "%ms "
950 "%ms "
Lennart Poettering468d7262012-01-17 15:04:12 +0100951 "%ms "
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100952 "%n",
Michal Schmidt66ccd032011-12-15 21:31:14 +0100953 &type,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100954 &i->path,
955 &mode,
956 &user,
957 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +0100958 &age,
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100959 &n) < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200960 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200961 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +0200962 goto finish;
963 }
964
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100965 if (n >= 0) {
966 n += strspn(buffer+n, WHITESPACE);
967 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
968 i->argument = unquote(buffer+n, "\"");
969 if (!i->argument) {
970 log_error("Out of memory");
971 return -ENOMEM;
972 }
973 }
974 }
975
Michal Schmidt777b87e2011-12-16 18:27:35 +0100976 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100977
Michal Schmidt777b87e2011-12-16 18:27:35 +0100978 case CREATE_FILE:
979 case TRUNCATE_FILE:
980 case CREATE_DIRECTORY:
981 case TRUNCATE_DIRECTORY:
982 case CREATE_FIFO:
983 case IGNORE_PATH:
984 case REMOVE_PATH:
985 case RECURSIVE_REMOVE_PATH:
986 case RELABEL_PATH:
987 case RECURSIVE_RELABEL_PATH:
988 break;
Lennart Poettering468d7262012-01-17 15:04:12 +0100989
990 case CREATE_SYMLINK:
991 if (!i->argument) {
992 log_error("[%s:%u] Symlink file requires argument.", fname, line);
993 r = -EBADMSG;
994 goto finish;
995 }
996 break;
997
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100998 case WRITE_FILE:
999 if (!i->argument) {
1000 log_error("[%s:%u] Write file requires argument.", fname, line);
1001 r = -EBADMSG;
1002 goto finish;
1003 }
1004 break;
1005
Lennart Poettering468d7262012-01-17 15:04:12 +01001006 case CREATE_CHAR_DEVICE:
1007 case CREATE_BLOCK_DEVICE: {
1008 unsigned major, minor;
1009
1010 if (!i->argument) {
1011 log_error("[%s:%u] Device file requires argument.", fname, line);
1012 r = -EBADMSG;
1013 goto finish;
1014 }
1015
1016 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1017 log_error("[%s:%u] Can't parse device file major/minor '%s'.", fname, line, i->argument);
1018 r = -EBADMSG;
1019 goto finish;
1020 }
1021
1022 i->major_minor = makedev(major, minor);
1023 break;
1024 }
1025
Michal Schmidt777b87e2011-12-16 18:27:35 +01001026 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001027 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001028 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001029 goto finish;
1030 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001031
Michal Schmidta8d88782011-12-15 23:11:07 +01001032 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001033
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001034 if (!path_is_absolute(i->path)) {
1035 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
1036 r = -EBADMSG;
1037 goto finish;
1038 }
1039
1040 path_kill_slashes(i->path);
1041
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001042 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001043 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001044 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001045 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001046
1047 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001048 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001049
Lennart Poettering4b678342011-07-23 01:17:59 +02001050 r = get_user_creds(&u, &i->uid, NULL, NULL);
1051 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001052 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
1053 goto finish;
1054 }
1055
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001056 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001057 }
1058
1059 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001060 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001061
Lennart Poettering4b678342011-07-23 01:17:59 +02001062 r = get_group_creds(&g, &i->gid);
1063 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001064 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
1065 goto finish;
1066 }
1067
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001068 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001069 }
1070
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001071 if (mode && !streq(mode, "-")) {
1072 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001073
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001074 if (sscanf(mode, "%o", &m) != 1) {
1075 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
1076 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001077 goto finish;
1078 }
1079
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001080 i->mode = m;
1081 i->mode_set = true;
1082 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001083 i->mode =
1084 i->type == CREATE_DIRECTORY ||
1085 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001086
1087 if (age && !streq(age, "-")) {
1088 if (parse_usec(age, &i->age) < 0) {
1089 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
1090 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001091 goto finish;
1092 }
1093
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001094 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001095 }
1096
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001097 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001098
Lennart Poettering468d7262012-01-17 15:04:12 +01001099 existing = hashmap_get(h, i->path);
1100 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001101
1102 /* Two identical items are fine */
1103 if (!item_equal(existing, i))
1104 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1105
1106 r = 0;
1107 goto finish;
1108 }
1109
Lennart Poettering468d7262012-01-17 15:04:12 +01001110 r = hashmap_put(h, i->path, i);
1111 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001112 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001113 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001114 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001115
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001116 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001117 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001118
1119finish:
Lennart Poettering5008d582010-09-28 02:34:02 +02001120 free(user);
1121 free(group);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001122 free(mode);
1123 free(age);
Lennart Poettering5008d582010-09-28 02:34:02 +02001124
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001125 if (i)
1126 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001127
1128 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001129}
1130
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001131static int help(void) {
1132
Lennart Poettering522d4a42011-02-13 15:08:15 +01001133 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1134 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001135 " -h --help Show this help\n"
1136 " --create Create marked files/directories\n"
1137 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001138 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +01001139 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001140 program_invocation_short_name);
1141
1142 return 0;
1143}
1144
1145static int parse_argv(int argc, char *argv[]) {
1146
1147 enum {
1148 ARG_CREATE,
1149 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001150 ARG_REMOVE,
1151 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001152 };
1153
1154 static const struct option options[] = {
1155 { "help", no_argument, NULL, 'h' },
1156 { "create", no_argument, NULL, ARG_CREATE },
1157 { "clean", no_argument, NULL, ARG_CLEAN },
1158 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001159 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001160 { NULL, 0, NULL, 0 }
1161 };
1162
1163 int c;
1164
1165 assert(argc >= 0);
1166 assert(argv);
1167
1168 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1169
1170 switch (c) {
1171
1172 case 'h':
1173 help();
1174 return 0;
1175
1176 case ARG_CREATE:
1177 arg_create = true;
1178 break;
1179
1180 case ARG_CLEAN:
1181 arg_clean = true;
1182 break;
1183
1184 case ARG_REMOVE:
1185 arg_remove = true;
1186 break;
1187
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001188 case ARG_PREFIX:
1189 arg_prefix = optarg;
1190 break;
1191
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001192 case '?':
1193 return -EINVAL;
1194
1195 default:
1196 log_error("Unknown option code %c", c);
1197 return -EINVAL;
1198 }
1199 }
1200
1201 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001202 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001203 return -EINVAL;
1204 }
1205
1206 return 1;
1207}
1208
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001209static int read_config_file(const char *fn, bool ignore_enoent) {
1210 FILE *f;
1211 unsigned v = 0;
1212 int r = 0;
1213
1214 assert(fn);
1215
Lennart Poettering468d7262012-01-17 15:04:12 +01001216 f = fopen(fn, "re");
1217 if (!f) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001218
1219 if (ignore_enoent && errno == ENOENT)
1220 return 0;
1221
1222 log_error("Failed to open %s: %m", fn);
1223 return -errno;
1224 }
1225
Kay Sievers772f8372011-04-25 21:38:21 +02001226 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001227 for (;;) {
1228 char line[LINE_MAX], *l;
1229 int k;
1230
1231 if (!(fgets(line, sizeof(line), f)))
1232 break;
1233
1234 v++;
1235
1236 l = strstrip(line);
1237 if (*l == '#' || *l == 0)
1238 continue;
1239
1240 if ((k = parse_line(fn, v, l)) < 0)
1241 if (r == 0)
1242 r = k;
1243 }
1244
1245 if (ferror(f)) {
1246 log_error("Failed to read from file %s: %m", fn);
1247 if (r == 0)
1248 r = -EIO;
1249 }
1250
1251 fclose(f);
1252
1253 return r;
1254}
1255
Lennart Poettering5008d582010-09-28 02:34:02 +02001256int main(int argc, char *argv[]) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001257 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001258 Item *i;
1259 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001260
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001261 r = parse_argv(argc, argv);
1262 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001263 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1264
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001265 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001266 log_parse_environment();
1267 log_open();
1268
Lennart Poettering4c126262011-08-01 20:52:18 +02001269 umask(0022);
1270
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001271 label_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001272
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001273 items = hashmap_new(string_hash_func, string_compare_func);
1274 globs = hashmap_new(string_hash_func, string_compare_func);
1275
1276 if (!items || !globs) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001277 log_error("Out of memory");
1278 r = EXIT_FAILURE;
1279 goto finish;
1280 }
1281
Lennart Poettering5008d582010-09-28 02:34:02 +02001282 r = EXIT_SUCCESS;
1283
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001284 if (optind < argc) {
1285 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001286
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001287 for (j = optind; j < argc; j++)
1288 if (read_config_file(argv[j], false) < 0)
1289 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001290
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001291 } else {
Kay Sievers772f8372011-04-25 21:38:21 +02001292 char **files, **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001293
Kay Sievers44143302011-04-28 23:51:24 +02001294 r = conf_files_list(&files, ".conf",
Kay Sievers44143302011-04-28 23:51:24 +02001295 "/etc/tmpfiles.d",
Lennart Poetteringfc1a2e02012-03-14 14:25:05 +01001296 "/run/tmpfiles.d",
Kay Sievers223a3552011-04-30 20:31:33 +02001297 "/usr/local/lib/tmpfiles.d",
Kay Sievers44143302011-04-28 23:51:24 +02001298 "/usr/lib/tmpfiles.d",
1299 NULL);
1300 if (r < 0) {
Kay Sievers44143302011-04-28 23:51:24 +02001301 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
Michal Schmidta48f3d12012-04-17 22:53:35 +02001302 r = EXIT_FAILURE;
Kay Sievers44143302011-04-28 23:51:24 +02001303 goto finish;
1304 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001305
Kay Sievers772f8372011-04-25 21:38:21 +02001306 STRV_FOREACH(f, files) {
1307 if (read_config_file(*f, true) < 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001308 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001309 }
1310
Kay Sievers772f8372011-04-25 21:38:21 +02001311 strv_free(files);
Lennart Poettering5008d582010-09-28 02:34:02 +02001312 }
1313
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001314 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001315 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001316
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001317 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001318 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001319
Lennart Poettering5008d582010-09-28 02:34:02 +02001320finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001321 while ((i = hashmap_steal_first(items)))
1322 item_free(i);
1323
Lennart Poettering17b90522011-02-14 21:55:06 +01001324 while ((i = hashmap_steal_first(globs)))
1325 item_free(i);
1326
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001327 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001328 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001329
Lennart Poettering17b90522011-02-14 21:55:06 +01001330 set_free_free(unix_sockets);
1331
Lennart Poettering29003cf2010-10-19 19:36:45 +02001332 label_finish();
1333
Lennart Poettering5008d582010-09-28 02:34:02 +02001334 return r;
1335}