blob: 6e0c093a82c58174ccfeb44efad79e69e32d96e2 [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
Dave Reisner91256702012-06-08 22:31:19 -0400103static const char *conf_file_dirs[] = {
104 "/etc/tmpfiles.d",
105 "/run/tmpfiles.d",
106 "/usr/local/lib/tmpfiles.d",
107 "/usr/lib/tmpfiles.d",
108 NULL
109};
110
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200111#define MAX_DEPTH 256
112
Michal Schmidt66ccd032011-12-15 21:31:14 +0100113static bool needs_glob(ItemType t) {
Michal Schmidt777b87e2011-12-16 18:27:35 +0100114 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 +0100115}
116
117static struct Item* find_glob(Hashmap *h, const char *match) {
118 Item *j;
119 Iterator i;
120
121 HASHMAP_FOREACH(j, h, i)
122 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
123 return j;
124
125 return NULL;
126}
127
Lennart Poettering17b90522011-02-14 21:55:06 +0100128static void load_unix_sockets(void) {
129 FILE *f = NULL;
130 char line[LINE_MAX];
131
132 if (unix_sockets)
133 return;
134
135 /* We maintain a cache of the sockets we found in
136 * /proc/net/unix to speed things up a little. */
137
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100138 unix_sockets = set_new(string_hash_func, string_compare_func);
139 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100140 return;
141
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100142 f = fopen("/proc/net/unix", "re");
143 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100144 return;
145
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100146 /* Skip header */
147 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100148 goto fail;
149
150 for (;;) {
151 char *p, *s;
152 int k;
153
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100154 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100155 break;
156
157 truncate_nl(line);
158
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100159 p = strchr(line, ':');
160 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100161 continue;
162
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100163 if (strlen(p) < 37)
164 continue;
165
166 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100167 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100168 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100169 p += strspn(p, WHITESPACE);
170
171 if (*p != '/')
172 continue;
173
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100174 s = strdup(p);
175 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100176 goto fail;
177
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100178 path_kill_slashes(s);
179
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100180 k = set_put(unix_sockets, s);
181 if (k < 0) {
Lennart Poettering17b90522011-02-14 21:55:06 +0100182 free(s);
183
184 if (k != -EEXIST)
185 goto fail;
186 }
187 }
188
Thomas Jarosch10d975f2011-10-05 22:30:49 +0200189 fclose(f);
Lennart Poettering17b90522011-02-14 21:55:06 +0100190 return;
191
192fail:
193 set_free_free(unix_sockets);
194 unix_sockets = NULL;
195
196 if (f)
197 fclose(f);
198}
199
200static bool unix_socket_alive(const char *fn) {
201 assert(fn);
202
203 load_unix_sockets();
204
205 if (unix_sockets)
206 return !!set_get(unix_sockets, (char*) fn);
207
208 /* We don't know, so assume yes */
209 return true;
210}
211
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200212static int dir_cleanup(
213 const char *p,
214 DIR *d,
215 const struct stat *ds,
216 usec_t cutoff,
217 dev_t rootdev,
218 bool mountpoint,
219 int maxdepth)
220{
221 struct dirent *dent;
222 struct timespec times[2];
223 bool deleted = false;
224 char *sub_path = NULL;
225 int r = 0;
226
227 while ((dent = readdir(d))) {
228 struct stat s;
229 usec_t age;
230
231 if (streq(dent->d_name, ".") ||
232 streq(dent->d_name, ".."))
233 continue;
234
235 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
236
237 if (errno != ENOENT) {
238 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
239 r = -errno;
240 }
241
242 continue;
243 }
244
245 /* Stay on the same filesystem */
246 if (s.st_dev != rootdev)
247 continue;
248
249 /* Do not delete read-only files owned by root */
250 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
251 continue;
252
253 free(sub_path);
254 sub_path = NULL;
255
256 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
257 log_error("Out of memory");
258 r = -ENOMEM;
259 goto finish;
260 }
261
262 /* Is there an item configured for this path? */
263 if (hashmap_get(items, sub_path))
264 continue;
265
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100266 if (find_glob(globs, sub_path))
267 continue;
268
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200269 if (S_ISDIR(s.st_mode)) {
270
271 if (mountpoint &&
272 streq(dent->d_name, "lost+found") &&
273 s.st_uid == 0)
274 continue;
275
276 if (maxdepth <= 0)
277 log_warning("Reached max depth on %s.", sub_path);
278 else {
279 DIR *sub_dir;
280 int q;
281
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200282 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200283 if (sub_dir == NULL) {
284 if (errno != ENOENT) {
285 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
286 r = -errno;
287 }
288
289 continue;
290 }
291
292 q = dir_cleanup(sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1);
293 closedir(sub_dir);
294
295 if (q < 0)
296 r = q;
297 }
298
299 /* Ignore ctime, we change it when deleting */
300 age = MAX(timespec_load(&s.st_mtim),
301 timespec_load(&s.st_atim));
302 if (age >= cutoff)
303 continue;
304
305 log_debug("rmdir '%s'\n", sub_path);
306
307 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
308 if (errno != ENOENT && errno != ENOTEMPTY) {
309 log_error("rmdir(%s): %m", sub_path);
310 r = -errno;
311 }
312 }
313
314 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100315 /* Skip files for which the sticky bit is
316 * set. These are semantics we define, and are
317 * unknown elsewhere. See XDG_RUNTIME_DIR
318 * specification for details. */
319 if (s.st_mode & S_ISVTX)
320 continue;
321
Lennart Poettering17b90522011-02-14 21:55:06 +0100322 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200323 if (streq(dent->d_name, ".journal") &&
324 s.st_uid == 0)
325 continue;
326
327 if (streq(dent->d_name, "aquota.user") ||
328 streq(dent->d_name, "aquota.group"))
329 continue;
330 }
331
Lennart Poettering17b90522011-02-14 21:55:06 +0100332 /* Ignore sockets that are listed in /proc/net/unix */
333 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
334 continue;
335
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100336 /* Ignore device nodes */
337 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
338 continue;
339
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200340 age = MAX3(timespec_load(&s.st_mtim),
341 timespec_load(&s.st_atim),
342 timespec_load(&s.st_ctim));
343
344 if (age >= cutoff)
345 continue;
346
347 log_debug("unlink '%s'\n", sub_path);
348
349 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
350 if (errno != ENOENT) {
351 log_error("unlink(%s): %m", sub_path);
352 r = -errno;
353 }
354 }
355
356 deleted = true;
357 }
358 }
359
360finish:
361 if (deleted) {
362 /* Restore original directory timestamps */
363 times[0] = ds->st_atim;
364 times[1] = ds->st_mtim;
365
366 if (futimens(dirfd(d), times) < 0)
367 log_error("utimensat(%s): %m", p);
368 }
369
370 free(sub_path);
371
372 return r;
373}
374
375static int clean_item(Item *i) {
376 DIR *d;
377 struct stat s, ps;
378 bool mountpoint;
379 int r;
380 usec_t cutoff, n;
381
382 assert(i);
383
384 if (i->type != CREATE_DIRECTORY &&
385 i->type != TRUNCATE_DIRECTORY &&
386 i->type != IGNORE_PATH)
387 return 0;
388
389 if (!i->age_set || i->age <= 0)
390 return 0;
391
392 n = now(CLOCK_REALTIME);
393 if (n < i->age)
394 return 0;
395
396 cutoff = n - i->age;
397
398 d = opendir(i->path);
399 if (!d) {
400 if (errno == ENOENT)
401 return 0;
402
403 log_error("Failed to open directory %s: %m", i->path);
404 return -errno;
405 }
406
407 if (fstat(dirfd(d), &s) < 0) {
408 log_error("stat(%s) failed: %m", i->path);
409 r = -errno;
410 goto finish;
411 }
412
413 if (!S_ISDIR(s.st_mode)) {
414 log_error("%s is not a directory.", i->path);
415 r = -ENOTDIR;
416 goto finish;
417 }
418
419 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
420 log_error("stat(%s/..) failed: %m", i->path);
421 r = -errno;
422 goto finish;
423 }
424
425 mountpoint = s.st_dev != ps.st_dev ||
426 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
427
428 r = dir_cleanup(i->path, d, &s, cutoff, s.st_dev, mountpoint, MAX_DEPTH);
429
430finish:
431 if (d)
432 closedir(d);
433
434 return r;
435}
436
Michal Schmidt062e01b2011-12-16 18:00:11 +0100437static int item_set_perms(Item *i, const char *path) {
438 /* not using i->path directly because it may be a glob */
439 if (i->mode_set)
440 if (chmod(path, i->mode) < 0) {
441 log_error("chmod(%s) failed: %m", path);
442 return -errno;
443 }
444
445 if (i->uid_set || i->gid_set)
446 if (chown(path,
447 i->uid_set ? i->uid : (uid_t) -1,
448 i->gid_set ? i->gid : (gid_t) -1) < 0) {
449
450 log_error("chown(%s) failed: %m", path);
451 return -errno;
452 }
453
454 return label_fix(path, false);
455}
456
457static int recursive_relabel_children(Item *i, const char *path) {
Michal Schmidta8d88782011-12-15 23:11:07 +0100458 DIR *d;
459 int ret = 0;
460
461 /* This returns the first error we run into, but nevertheless
462 * tries to go on */
463
464 d = opendir(path);
465 if (!d)
466 return errno == ENOENT ? 0 : -errno;
467
468 for (;;) {
469 struct dirent buf, *de;
470 bool is_dir;
471 int r;
472 char *entry_path;
473
474 r = readdir_r(d, &buf, &de);
475 if (r != 0) {
476 if (ret == 0)
477 ret = -r;
478 break;
479 }
480
481 if (!de)
482 break;
483
484 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
485 continue;
486
487 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
488 if (ret == 0)
489 ret = -ENOMEM;
490 continue;
491 }
492
493 if (de->d_type == DT_UNKNOWN) {
494 struct stat st;
495
496 if (lstat(entry_path, &st) < 0) {
497 if (ret == 0 && errno != ENOENT)
498 ret = -errno;
499 free(entry_path);
500 continue;
501 }
502
503 is_dir = S_ISDIR(st.st_mode);
504
505 } else
506 is_dir = de->d_type == DT_DIR;
507
Michal Schmidt062e01b2011-12-16 18:00:11 +0100508 r = item_set_perms(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100509 if (r < 0) {
510 if (ret == 0 && r != -ENOENT)
511 ret = r;
512 free(entry_path);
513 continue;
514 }
515
516 if (is_dir) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100517 r = recursive_relabel_children(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100518 if (r < 0 && ret == 0)
519 ret = r;
520 }
521
522 free(entry_path);
523 }
524
525 closedir(d);
526
527 return ret;
528}
529
530static int recursive_relabel(Item *i, const char *path) {
531 int r;
532 struct stat st;
533
Michal Schmidt062e01b2011-12-16 18:00:11 +0100534 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100535 if (r < 0)
536 return r;
537
538 if (lstat(path, &st) < 0)
539 return -errno;
540
541 if (S_ISDIR(st.st_mode))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100542 r = recursive_relabel_children(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100543
544 return r;
545}
546
Michal Schmidt99e68c02011-12-15 23:45:26 +0100547static int glob_item(Item *i, int (*action)(Item *, const char *)) {
548 int r = 0, k;
549 glob_t g;
550 char **fn;
551
552 zero(g);
553
554 errno = 0;
555 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
556
557 if (k != GLOB_NOMATCH) {
558 if (errno != 0)
559 errno = EIO;
560
561 log_error("glob(%s) failed: %m", i->path);
562 return -errno;
563 }
564 }
565
566 STRV_FOREACH(fn, g.gl_pathv)
567 if ((k = action(i, *fn)) < 0)
568 r = k;
569
570 globfree(&g);
571 return r;
572}
573
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200574static int create_item(Item *i) {
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200575 int r, e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200576 mode_t u;
577 struct stat st;
578
579 assert(i);
580
581 switch (i->type) {
582
583 case IGNORE_PATH:
584 case REMOVE_PATH:
585 case RECURSIVE_REMOVE_PATH:
586 return 0;
587
588 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100589 case TRUNCATE_FILE:
590 case WRITE_FILE: {
591 int fd, flags;
592
593 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
594 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200595
596 u = umask(0);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200597 label_context_set(i->path, S_IFREG);
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100598 fd = open(i->path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200599 e = errno;
600 label_context_clear();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200601 umask(u);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200602 errno = e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200603
604 if (fd < 0) {
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100605 if (i->type == WRITE_FILE && errno == ENOENT)
606 break;
607
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200608 log_error("Failed to create file %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100609 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200610 }
611
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100612 if (i->argument) {
613 ssize_t n;
614 size_t l;
615 struct iovec iovec[2];
616 static const char new_line = '\n';
617
618 l = strlen(i->argument);
619
620 zero(iovec);
621 iovec[0].iov_base = i->argument;
622 iovec[0].iov_len = l;
623
624 iovec[1].iov_base = (void*) &new_line;
625 iovec[1].iov_len = 1;
626
627 n = writev(fd, iovec, 2);
Lennart Poettering03ad1132012-05-15 14:34:33 +0200628
629 /* It's OK if we don't write the trailing
630 * newline, hence we check for l, instead of
631 * l+1 here. Files in /sys often refuse
632 * writing of the trailing newline. */
633 if (n < 0 || (size_t) n < l) {
634 log_error("Failed to write file %s: %s", i->path, n < 0 ? strerror(-n) : "Short write");
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100635 close_nointr_nofail(fd);
636 return n < 0 ? n : -EIO;
637 }
638 }
639
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100640 close_nointr_nofail(fd);
641
642 if (stat(i->path, &st) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200643 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100644 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200645 }
646
647 if (!S_ISREG(st.st_mode)) {
648 log_error("%s is not a file.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100649 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200650 }
651
Michal Schmidt062e01b2011-12-16 18:00:11 +0100652 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100653 if (r < 0)
654 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200655
656 break;
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100657 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200658
659 case TRUNCATE_DIRECTORY:
660 case CREATE_DIRECTORY:
661
662 u = umask(0);
Kay Sieversd2e54fa2012-05-31 12:40:20 +0200663 mkdir_parents_label(i->path, 0755);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200664 r = mkdir(i->path, i->mode);
665 umask(u);
666
667 if (r < 0 && errno != EEXIST) {
668 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100669 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200670 }
671
672 if (stat(i->path, &st) < 0) {
673 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100674 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200675 }
676
677 if (!S_ISDIR(st.st_mode)) {
678 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100679 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200680 }
681
Michal Schmidt062e01b2011-12-16 18:00:11 +0100682 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100683 if (r < 0)
684 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200685
686 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200687
688 case CREATE_FIFO:
689
690 u = umask(0);
691 r = mkfifo(i->path, i->mode);
692 umask(u);
693
694 if (r < 0 && errno != EEXIST) {
695 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100696 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200697 }
698
699 if (stat(i->path, &st) < 0) {
700 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100701 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200702 }
703
704 if (!S_ISFIFO(st.st_mode)) {
705 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100706 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200707 }
708
Michal Schmidt062e01b2011-12-16 18:00:11 +0100709 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100710 if (r < 0)
711 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200712
713 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100714
Lennart Poettering468d7262012-01-17 15:04:12 +0100715 case CREATE_SYMLINK: {
716 char *x;
717
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200718 label_context_set(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100719 r = symlink(i->argument, i->path);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200720 e = errno;
721 label_context_clear();
722 errno = e;
723
Lennart Poettering468d7262012-01-17 15:04:12 +0100724 if (r < 0 && errno != EEXIST) {
725 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
726 return -errno;
727 }
728
729 r = readlink_malloc(i->path, &x);
730 if (r < 0) {
731 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
732 return -errno;
733 }
734
735 if (!streq(i->argument, x)) {
736 free(x);
737 log_error("%s is not the right symlinks.", i->path);
738 return -EEXIST;
739 }
740
741 free(x);
742 break;
743 }
744
745 case CREATE_BLOCK_DEVICE:
746 case CREATE_CHAR_DEVICE: {
Michal Schmidte7aee752012-06-14 16:01:19 +0200747 mode_t file_type = (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +0100748
749 u = umask(0);
Michal Schmidte7aee752012-06-14 16:01:19 +0200750 label_context_set(i->path, file_type);
751 r = mknod(i->path, i->mode | file_type, i->major_minor);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200752 e = errno;
753 label_context_clear();
Lennart Poettering468d7262012-01-17 15:04:12 +0100754 umask(u);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200755 errno = e;
Lennart Poettering468d7262012-01-17 15:04:12 +0100756
757 if (r < 0 && errno != EEXIST) {
758 log_error("Failed to create device node %s: %m", i->path);
759 return -errno;
760 }
761
762 if (stat(i->path, &st) < 0) {
763 log_error("stat(%s) failed: %m", i->path);
764 return -errno;
765 }
766
Michal Schmidte7aee752012-06-14 16:01:19 +0200767 if ((st.st_mode & S_IFMT) != file_type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100768 log_error("%s is not a device node.", i->path);
769 return -EEXIST;
770 }
771
772 r = item_set_perms(i, i->path);
773 if (r < 0)
774 return r;
775
776 break;
777 }
778
Michal Schmidt777b87e2011-12-16 18:27:35 +0100779 case RELABEL_PATH:
780
781 r = glob_item(i, item_set_perms);
782 if (r < 0)
783 return 0;
784 break;
785
Michal Schmidta8d88782011-12-15 23:11:07 +0100786 case RECURSIVE_RELABEL_PATH:
787
788 r = glob_item(i, recursive_relabel);
789 if (r < 0)
790 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200791 }
792
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200793 log_debug("%s created successfully.", i->path);
794
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100795 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200796}
797
Michal Schmidta0896122011-12-15 21:32:50 +0100798static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200799 int r;
800
801 assert(i);
802
803 switch (i->type) {
804
805 case CREATE_FILE:
806 case TRUNCATE_FILE:
807 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200808 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100809 case CREATE_SYMLINK:
810 case CREATE_BLOCK_DEVICE:
811 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200812 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100813 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100814 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100815 case WRITE_FILE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200816 break;
817
818 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100819 if (remove(instance) < 0 && errno != ENOENT) {
820 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200821 return -errno;
822 }
823
824 break;
825
826 case TRUNCATE_DIRECTORY:
827 case RECURSIVE_REMOVE_PATH:
Lennart Poettering468d7262012-01-17 15:04:12 +0100828 r = rm_rf(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
829 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100830 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200831 return r;
832 }
833
834 break;
835 }
836
837 return 0;
838}
839
Michal Schmidta0896122011-12-15 21:32:50 +0100840static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100841 int r = 0;
842
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100843 assert(i);
844
845 switch (i->type) {
846
847 case CREATE_FILE:
848 case TRUNCATE_FILE:
849 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200850 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100851 case CREATE_SYMLINK:
852 case CREATE_CHAR_DEVICE:
853 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100854 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100855 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100856 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100857 case WRITE_FILE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100858 break;
859
860 case REMOVE_PATH:
861 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100862 case RECURSIVE_REMOVE_PATH:
863 r = glob_item(i, remove_item_instance);
864 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100865 }
866
Michal Schmidt99e68c02011-12-15 23:45:26 +0100867 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100868}
869
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200870static int process_item(Item *i) {
871 int r, q, p;
872
873 assert(i);
874
875 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100876 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200877 p = arg_clean ? clean_item(i) : 0;
878
879 if (r < 0)
880 return r;
881
882 if (q < 0)
883 return q;
884
885 return p;
886}
887
888static void item_free(Item *i) {
889 assert(i);
890
891 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100892 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200893 free(i);
894}
895
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200896static bool item_equal(Item *a, Item *b) {
897 assert(a);
898 assert(b);
899
900 if (!streq_ptr(a->path, b->path))
901 return false;
902
903 if (a->type != b->type)
904 return false;
905
906 if (a->uid_set != b->uid_set ||
907 (a->uid_set && a->uid != b->uid))
908 return false;
909
910 if (a->gid_set != b->gid_set ||
911 (a->gid_set && a->gid != b->gid))
912 return false;
913
914 if (a->mode_set != b->mode_set ||
915 (a->mode_set && a->mode != b->mode))
916 return false;
917
918 if (a->age_set != b->age_set ||
919 (a->age_set && a->age != b->age))
920 return false;
921
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100922 if ((a->type == CREATE_FILE ||
923 a->type == TRUNCATE_FILE ||
924 a->type == WRITE_FILE ||
925 a->type == CREATE_SYMLINK) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +0100926 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +0100927 return false;
928
929 if ((a->type == CREATE_CHAR_DEVICE ||
930 a->type == CREATE_BLOCK_DEVICE) &&
931 a->major_minor != b->major_minor)
932 return false;
933
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200934 return true;
935}
936
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100937static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200938 Item *i, *existing;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200939 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +0100940 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200941 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100942 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +0200943
944 assert(fname);
945 assert(line >= 1);
946 assert(buffer);
947
Lennart Poettering468d7262012-01-17 15:04:12 +0100948 i = new0(Item, 1);
949 if (!i) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200950 log_error("Out of memory");
951 return -ENOMEM;
952 }
953
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100954 if (sscanf(buffer,
955 "%c "
956 "%ms "
957 "%ms "
958 "%ms "
959 "%ms "
Lennart Poettering468d7262012-01-17 15:04:12 +0100960 "%ms "
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100961 "%n",
Michal Schmidt66ccd032011-12-15 21:31:14 +0100962 &type,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100963 &i->path,
964 &mode,
965 &user,
966 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +0100967 &age,
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100968 &n) < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200969 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200970 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +0200971 goto finish;
972 }
973
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100974 if (n >= 0) {
975 n += strspn(buffer+n, WHITESPACE);
976 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
977 i->argument = unquote(buffer+n, "\"");
978 if (!i->argument) {
979 log_error("Out of memory");
980 return -ENOMEM;
981 }
982 }
983 }
984
Michal Schmidt777b87e2011-12-16 18:27:35 +0100985 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100986
Michal Schmidt777b87e2011-12-16 18:27:35 +0100987 case CREATE_FILE:
988 case TRUNCATE_FILE:
989 case CREATE_DIRECTORY:
990 case TRUNCATE_DIRECTORY:
991 case CREATE_FIFO:
992 case IGNORE_PATH:
993 case REMOVE_PATH:
994 case RECURSIVE_REMOVE_PATH:
995 case RELABEL_PATH:
996 case RECURSIVE_RELABEL_PATH:
997 break;
Lennart Poettering468d7262012-01-17 15:04:12 +0100998
999 case CREATE_SYMLINK:
1000 if (!i->argument) {
1001 log_error("[%s:%u] Symlink file requires argument.", fname, line);
1002 r = -EBADMSG;
1003 goto finish;
1004 }
1005 break;
1006
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001007 case WRITE_FILE:
1008 if (!i->argument) {
1009 log_error("[%s:%u] Write file requires argument.", fname, line);
1010 r = -EBADMSG;
1011 goto finish;
1012 }
1013 break;
1014
Lennart Poettering468d7262012-01-17 15:04:12 +01001015 case CREATE_CHAR_DEVICE:
1016 case CREATE_BLOCK_DEVICE: {
1017 unsigned major, minor;
1018
1019 if (!i->argument) {
1020 log_error("[%s:%u] Device file requires argument.", fname, line);
1021 r = -EBADMSG;
1022 goto finish;
1023 }
1024
1025 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1026 log_error("[%s:%u] Can't parse device file major/minor '%s'.", fname, line, i->argument);
1027 r = -EBADMSG;
1028 goto finish;
1029 }
1030
1031 i->major_minor = makedev(major, minor);
1032 break;
1033 }
1034
Michal Schmidt777b87e2011-12-16 18:27:35 +01001035 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001036 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001037 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001038 goto finish;
1039 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001040
Michal Schmidta8d88782011-12-15 23:11:07 +01001041 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001042
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001043 if (!path_is_absolute(i->path)) {
1044 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
1045 r = -EBADMSG;
1046 goto finish;
1047 }
1048
1049 path_kill_slashes(i->path);
1050
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001051 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001052 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001053 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001054 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001055
1056 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001057 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001058
Lennart Poettering4b678342011-07-23 01:17:59 +02001059 r = get_user_creds(&u, &i->uid, NULL, NULL);
1060 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001061 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
1062 goto finish;
1063 }
1064
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001065 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001066 }
1067
1068 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001069 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001070
Lennart Poettering4b678342011-07-23 01:17:59 +02001071 r = get_group_creds(&g, &i->gid);
1072 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001073 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
1074 goto finish;
1075 }
1076
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001077 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001078 }
1079
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001080 if (mode && !streq(mode, "-")) {
1081 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001082
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001083 if (sscanf(mode, "%o", &m) != 1) {
1084 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
1085 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001086 goto finish;
1087 }
1088
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001089 i->mode = m;
1090 i->mode_set = true;
1091 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001092 i->mode =
1093 i->type == CREATE_DIRECTORY ||
1094 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001095
1096 if (age && !streq(age, "-")) {
1097 if (parse_usec(age, &i->age) < 0) {
1098 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
1099 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001100 goto finish;
1101 }
1102
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001103 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001104 }
1105
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001106 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001107
Lennart Poettering468d7262012-01-17 15:04:12 +01001108 existing = hashmap_get(h, i->path);
1109 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001110
1111 /* Two identical items are fine */
1112 if (!item_equal(existing, i))
1113 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1114
1115 r = 0;
1116 goto finish;
1117 }
1118
Lennart Poettering468d7262012-01-17 15:04:12 +01001119 r = hashmap_put(h, i->path, i);
1120 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001121 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001122 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001123 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001124
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001125 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001126 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001127
1128finish:
Lennart Poettering5008d582010-09-28 02:34:02 +02001129 free(user);
1130 free(group);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001131 free(mode);
1132 free(age);
Lennart Poettering5008d582010-09-28 02:34:02 +02001133
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001134 if (i)
1135 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001136
1137 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001138}
1139
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001140static int help(void) {
1141
Lennart Poettering522d4a42011-02-13 15:08:15 +01001142 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1143 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001144 " -h --help Show this help\n"
1145 " --create Create marked files/directories\n"
1146 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001147 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +01001148 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001149 program_invocation_short_name);
1150
1151 return 0;
1152}
1153
1154static int parse_argv(int argc, char *argv[]) {
1155
1156 enum {
1157 ARG_CREATE,
1158 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001159 ARG_REMOVE,
1160 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001161 };
1162
1163 static const struct option options[] = {
1164 { "help", no_argument, NULL, 'h' },
1165 { "create", no_argument, NULL, ARG_CREATE },
1166 { "clean", no_argument, NULL, ARG_CLEAN },
1167 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001168 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001169 { NULL, 0, NULL, 0 }
1170 };
1171
1172 int c;
1173
1174 assert(argc >= 0);
1175 assert(argv);
1176
1177 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1178
1179 switch (c) {
1180
1181 case 'h':
1182 help();
1183 return 0;
1184
1185 case ARG_CREATE:
1186 arg_create = true;
1187 break;
1188
1189 case ARG_CLEAN:
1190 arg_clean = true;
1191 break;
1192
1193 case ARG_REMOVE:
1194 arg_remove = true;
1195 break;
1196
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001197 case ARG_PREFIX:
1198 arg_prefix = optarg;
1199 break;
1200
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001201 case '?':
1202 return -EINVAL;
1203
1204 default:
1205 log_error("Unknown option code %c", c);
1206 return -EINVAL;
1207 }
1208 }
1209
1210 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001211 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001212 return -EINVAL;
1213 }
1214
1215 return 1;
1216}
1217
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001218static int read_config_file(const char *fn, bool ignore_enoent) {
1219 FILE *f;
1220 unsigned v = 0;
1221 int r = 0;
1222
1223 assert(fn);
1224
Lennart Poettering468d7262012-01-17 15:04:12 +01001225 f = fopen(fn, "re");
1226 if (!f) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001227
1228 if (ignore_enoent && errno == ENOENT)
1229 return 0;
1230
1231 log_error("Failed to open %s: %m", fn);
1232 return -errno;
1233 }
1234
Kay Sievers772f8372011-04-25 21:38:21 +02001235 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001236 for (;;) {
1237 char line[LINE_MAX], *l;
1238 int k;
1239
1240 if (!(fgets(line, sizeof(line), f)))
1241 break;
1242
1243 v++;
1244
1245 l = strstrip(line);
1246 if (*l == '#' || *l == 0)
1247 continue;
1248
1249 if ((k = parse_line(fn, v, l)) < 0)
1250 if (r == 0)
1251 r = k;
1252 }
1253
1254 if (ferror(f)) {
1255 log_error("Failed to read from file %s: %m", fn);
1256 if (r == 0)
1257 r = -EIO;
1258 }
1259
1260 fclose(f);
1261
1262 return r;
1263}
1264
Dave Reisner91256702012-06-08 22:31:19 -04001265static char *resolve_fragment(const char *fragment, const char **search_paths) {
1266 const char **p;
1267 char *resolved_path;
1268
1269 if (is_path(fragment))
1270 return strdup(fragment);
1271
1272 STRV_FOREACH(p, search_paths) {
1273 resolved_path = join(*p, "/", fragment, NULL);
1274 if (resolved_path == NULL) {
1275 log_error("Out of memory");
1276 return NULL;
1277 }
1278
1279 if (access(resolved_path, F_OK) == 0)
1280 return resolved_path;
1281
1282 free(resolved_path);
1283 }
1284
Kay Sieversca2e8942012-06-10 19:21:50 +02001285 errno = ENOENT;
Dave Reisner91256702012-06-08 22:31:19 -04001286 return NULL;
1287}
1288
Lennart Poettering5008d582010-09-28 02:34:02 +02001289int main(int argc, char *argv[]) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001290 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001291 Item *i;
1292 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001293
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001294 r = parse_argv(argc, argv);
1295 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001296 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1297
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001298 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001299 log_parse_environment();
1300 log_open();
1301
Lennart Poettering4c126262011-08-01 20:52:18 +02001302 umask(0022);
1303
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001304 label_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001305
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001306 items = hashmap_new(string_hash_func, string_compare_func);
1307 globs = hashmap_new(string_hash_func, string_compare_func);
1308
1309 if (!items || !globs) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001310 log_error("Out of memory");
1311 r = EXIT_FAILURE;
1312 goto finish;
1313 }
1314
Lennart Poettering5008d582010-09-28 02:34:02 +02001315 r = EXIT_SUCCESS;
1316
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001317 if (optind < argc) {
1318 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001319
Dave Reisner91256702012-06-08 22:31:19 -04001320 for (j = optind; j < argc; j++) {
Kay Sieversca2e8942012-06-10 19:21:50 +02001321 char *fragment;
1322
1323 fragment = resolve_fragment(argv[j], conf_file_dirs);
1324 if (!fragment) {
Kay Sievers94f7a712012-06-10 19:31:39 +02001325 log_error("Failed to find a %s file: %m", argv[j]);
Kay Sieversca2e8942012-06-10 19:21:50 +02001326 r = EXIT_FAILURE;
1327 goto finish;
1328 }
Dave Reisner91256702012-06-08 22:31:19 -04001329 if (read_config_file(fragment, false) < 0)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001330 r = EXIT_FAILURE;
Dave Reisner91256702012-06-08 22:31:19 -04001331 free(fragment);
1332 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001333
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001334 } else {
Kay Sievers772f8372011-04-25 21:38:21 +02001335 char **files, **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001336
Dave Reisner91256702012-06-08 22:31:19 -04001337 r = conf_files_list_strv(&files, ".conf",
1338 (const char **)conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001339 if (r < 0) {
Kay Sievers44143302011-04-28 23:51:24 +02001340 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
Michal Schmidta48f3d12012-04-17 22:53:35 +02001341 r = EXIT_FAILURE;
Kay Sievers44143302011-04-28 23:51:24 +02001342 goto finish;
1343 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001344
Kay Sievers772f8372011-04-25 21:38:21 +02001345 STRV_FOREACH(f, files) {
1346 if (read_config_file(*f, true) < 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001347 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001348 }
1349
Kay Sievers772f8372011-04-25 21:38:21 +02001350 strv_free(files);
Lennart Poettering5008d582010-09-28 02:34:02 +02001351 }
1352
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001353 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001354 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001355
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001356 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001357 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001358
Lennart Poettering5008d582010-09-28 02:34:02 +02001359finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001360 while ((i = hashmap_steal_first(items)))
1361 item_free(i);
1362
Lennart Poettering17b90522011-02-14 21:55:06 +01001363 while ((i = hashmap_steal_first(globs)))
1364 item_free(i);
1365
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001366 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001367 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001368
Lennart Poettering17b90522011-02-14 21:55:06 +01001369 set_free_free(unix_sockets);
1370
Lennart Poettering29003cf2010-10-19 19:36:45 +02001371 label_finish();
1372
Lennart Poettering5008d582010-09-28 02:34:02 +02001373 return r;
1374}