tmpfiles.c revision 601185b43da638b1c74153deae01dbd518680889
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering, Kay Sievers
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <dirent.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <getopt.h>
#include <stdbool.h>
#include <time.h>
#include <glob.h>
#include <fnmatch.h>
#include <sys/capability.h>
#include "log.h"
#include "util.h"
#include "macro.h"
#include "missing.h"
#include "mkdir.h"
#include "path-util.h"
#include "strv.h"
#include "label.h"
#include "set.h"
#include "conf-files.h"
#include "capability.h"
#include "specifier.h"
#include "build.h"
#include "copy.h"
/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
* them in the file system. This is intended to be used to create
* volatile and hence need to be recreated on bootup. */
typedef enum ItemType {
/* These ones take file names */
CREATE_FILE = 'f',
TRUNCATE_FILE = 'F',
CREATE_DIRECTORY = 'd',
TRUNCATE_DIRECTORY = 'D',
CREATE_FIFO = 'p',
CREATE_SYMLINK = 'L',
CREATE_CHAR_DEVICE = 'c',
CREATE_BLOCK_DEVICE = 'b',
COPY_FILES = 'C',
/* These ones take globs */
WRITE_FILE = 'w',
IGNORE_PATH = 'x',
IGNORE_DIRECTORY_PATH = 'X',
REMOVE_PATH = 'r',
RECURSIVE_REMOVE_PATH = 'R',
RELABEL_PATH = 'z',
RECURSIVE_RELABEL_PATH = 'Z',
} ItemType;
typedef struct Item {
char *path;
char *argument;
bool uid_set:1;
bool gid_set:1;
bool mode_set:1;
bool age_set:1;
bool mask_perms:1;
bool keep_first_level:1;
bool force:1;
bool done:1;
} Item;
static bool arg_create = false;
static bool arg_clean = false;
static bool arg_remove = false;
static bool arg_boot = false;
static char **arg_include_prefixes = NULL;
static char **arg_exclude_prefixes = NULL;
static const char conf_file_dirs[] =
"/etc/tmpfiles.d\0"
"/run/tmpfiles.d\0"
"/usr/local/lib/tmpfiles.d\0"
"/usr/lib/tmpfiles.d\0"
#ifdef HAVE_SPLIT_USR
"/lib/tmpfiles.d\0"
#endif
;
#define MAX_DEPTH 256
static bool needs_glob(ItemType t) {
return IN_SET(t,
}
Item *j;
Iterator i;
HASHMAP_FOREACH(j, h, i)
return j;
return NULL;
}
static void load_unix_sockets(void) {
if (unix_sockets)
return;
/* We maintain a cache of the sockets we found in
if (!unix_sockets)
return;
if (!f)
return;
/* Skip header */
goto fail;
for (;;) {
char *p, *s;
int k;
break;
if (!p)
continue;
if (strlen(p) < 37)
continue;
p += 37;
p += strspn(p, WHITESPACE);
p += strspn(p, WHITESPACE);
if (*p != '/')
continue;
s = strdup(p);
if (!s)
goto fail;
k = set_consume(unix_sockets, s);
if (k < 0 && k != -EEXIST)
goto fail;
}
return;
fail:
unix_sockets = NULL;
}
static bool unix_socket_alive(const char *fn) {
if (unix_sockets)
/* We don't know, so assume yes */
return true;
}
union file_handle_union h = {
};
int mount_id_parent, mount_id;
int r_p, r;
if (r_p < 0)
if (r < 0)
r = -errno;
/* got no handle; make no assumptions, return error */
if (r_p < 0 && r < 0)
return r_p;
/* got both handles; if they differ, it is a mount point */
if (r_p >= 0 && r >= 0)
return mount_id_parent != mount_id;
/* got only one handle; assume different mount points if one
* of both queries was not supported by the filesystem */
return true;
/* return error */
if (r_p < 0)
return r_p;
return r;
}
static int dir_cleanup(
Item *i,
const char *p,
DIR *d,
bool mountpoint,
int maxdepth,
bool keep_this_level) {
bool deleted = false;
int r = 0;
struct stat s;
continue;
continue;
/* FUSE, NFS mounts, SELinux might return EACCES */
else
r = -errno;
continue;
}
/* Stay on the same filesystem */
continue;
/* Try to detect bind mounts of the same filesystem instance; they
* supported on all kernels or filesystem types though. */
continue;
/* Do not delete read-only files owned by root */
continue;
if (!sub_path) {
r = log_oom();
goto finish;
}
/* Is there an item configured for this path? */
continue;
continue;
if (mountpoint &&
s.st_uid == 0)
continue;
if (maxdepth <= 0)
else {
int q;
if (!sub_dir) {
r = -errno;
}
continue;
}
if (q < 0)
r = q;
}
/* Note: if you are wondering why we don't
* support the sticky bit for excluding
* directories from cleaning like we do it for
* other file system objects: well, the sticky
* bit already has a meaning for directories,
* so we don't want to overload that. */
if (keep_this_level)
continue;
/* Ignore ctime, we change it when deleting */
timespec_load(&s.st_atim));
continue;
r = -errno;
}
}
}
} else {
/* Skip files for which the sticky bit is
* set. These are semantics we define, and are
* unknown elsewhere. See XDG_RUNTIME_DIR
* specification for details. */
continue;
s.st_uid == 0)
continue;
continue;
}
continue;
/* Ignore device nodes */
continue;
/* Keep files on this level around if this is
* requested */
if (keep_this_level)
continue;
timespec_load(&s.st_atim),
timespec_load(&s.st_ctim));
continue;
r = -errno;
}
}
deleted = true;
}
}
if (deleted) {
/* Restore original directory timestamps */
log_error("utimensat(%s): %m", p);
}
return r;
}
assert(i);
/* not using i->path directly because it may be a glob */
if (i->mode_set) {
if (i->mask_perms) {
m &= ~0111;
m &= ~0222;
m &= ~0444;
}
}
return -errno;
}
}
return -errno;
}
}
int flags, r = 0;
assert(i);
RUN_WITH_UMASK(0000) {
}
if (fd < 0) {
return 0;
return -errno;
}
if (i->argument) {
_cleanup_free_ char *unescaped;
ssize_t n;
size_t l;
if (!unescaped)
return log_oom();
if (n < 0 || (size_t) n < l) {
return n < 0 ? n : -EIO;
}
}
return -errno;
}
return -EEXIST;
}
r = item_set_perms(i, path);
if (r < 0)
return r;
return 0;
}
int r = 0;
assert(i);
/* This returns the first error we run into, but nevertheless
* tries to go on */
if (!d)
for (;;) {
_cleanup_free_ char *p = NULL;
int q;
errno = 0;
if (!de) {
if (errno != 0 && r == 0)
r = -errno;
break;
}
continue;
if (!p)
return -ENOMEM;
q = item_set_perms(i, p);
if (q < 0 && q != -ENOENT && r == 0)
r = q;
q = item_set_perms_children(i, p);
if (q < 0 && r == 0)
r = q;
}
}
return r;
}
int r, q;
assert(i);
r = item_set_perms(i, path);
if (r < 0)
return r;
q = item_set_perms_children(i, path);
if (q < 0 && r == 0)
r = q;
return r;
}
_cleanup_globfree_ glob_t g = {};
int r = 0, k;
char **fn;
errno = 0;
if (k != 0 && k != GLOB_NOMATCH) {
if (errno == 0)
return -errno;
}
if (k < 0 && r == 0)
r = k;
}
return r;
}
static int create_item(Item *i) {
int r = 0;
assert(i);
switch (i->type) {
case IGNORE_PATH:
case IGNORE_DIRECTORY_PATH:
case REMOVE_PATH:
case RECURSIVE_REMOVE_PATH:
return 0;
case CREATE_FILE:
case TRUNCATE_FILE:
r = write_one_file(i, i->path);
if (r < 0)
return r;
break;
case COPY_FILES:
if (r < 0) {
struct stat a, b;
if (r != -EEXIST) {
return -r;
}
return -errno;
}
return -errno;
}
return 0;
}
}
r = item_set_perms(i, i->path);
if (r < 0)
return r;
break;
case WRITE_FILE:
r = glob_item(i, write_one_file);
if (r < 0)
return r;
break;
case TRUNCATE_DIRECTORY:
case CREATE_DIRECTORY:
RUN_WITH_UMASK(0000) {
}
if (r < 0) {
if (r != -EEXIST) {
return r;
}
return -errno;
}
return 0;
}
}
r = item_set_perms(i, i->path);
if (r < 0)
return r;
break;
case CREATE_FIFO:
RUN_WITH_UMASK(0000) {
}
if (r < 0) {
return -errno;
}
return -errno;
}
if (i->force) {
RUN_WITH_UMASK(0000) {
}
if (r < 0) {
return r;
}
} else {
return 0;
}
}
}
r = item_set_perms(i, i->path);
if (r < 0)
return r;
break;
case CREATE_SYMLINK:
if (r < 0) {
_cleanup_free_ char *x = NULL;
return -errno;
}
r = readlink_malloc(i->path, &x);
if (i->force) {
if (r < 0) {
return r;
}
} else {
return 0;
}
}
}
break;
case CREATE_BLOCK_DEVICE:
case CREATE_CHAR_DEVICE: {
if (have_effective_cap(CAP_MKNOD) == 0) {
/* In a container we lack CAP_MKNOD. We
shouldn't attempt to create the device node in
that case to avoid noise, and we don't support
virtualized devices in containers anyway. */
return 0;
}
RUN_WITH_UMASK(0000) {
}
if (r < 0) {
log_debug("We lack permissions, possibly because of cgroup configuration; "
"skipping creation of device node %s.", i->path);
return 0;
}
return -errno;
}
return -errno;
}
if (i->force) {
RUN_WITH_UMASK(0000) {
}
if (r < 0) {
return r;
}
} else {
return 0;
}
}
}
r = item_set_perms(i, i->path);
if (r < 0)
return r;
break;
}
case ADJUST_MODE:
case RELABEL_PATH:
r = glob_item(i, item_set_perms);
if (r < 0)
return r;
break;
case RECURSIVE_RELABEL_PATH:
r = glob_item(i, item_set_perms_recursive);
if (r < 0)
return r;
break;
}
return 0;
}
int r;
assert(i);
switch (i->type) {
case CREATE_FILE:
case TRUNCATE_FILE:
case CREATE_DIRECTORY:
case CREATE_FIFO:
case CREATE_SYMLINK:
case CREATE_BLOCK_DEVICE:
case CREATE_CHAR_DEVICE:
case IGNORE_PATH:
case IGNORE_DIRECTORY_PATH:
case ADJUST_MODE:
case RELABEL_PATH:
case RECURSIVE_RELABEL_PATH:
case WRITE_FILE:
case COPY_FILES:
break;
case REMOVE_PATH:
return -errno;
}
break;
case TRUNCATE_DIRECTORY:
case RECURSIVE_REMOVE_PATH:
/* FIXME: we probably should use dir_cleanup() here
* instead of rm_rf() so that 'x' is honoured. */
if (r < 0 && r != -ENOENT) {
return r;
}
break;
}
return 0;
}
static int remove_item(Item *i) {
int r = 0;
assert(i);
switch (i->type) {
case CREATE_FILE:
case TRUNCATE_FILE:
case CREATE_DIRECTORY:
case CREATE_FIFO:
case CREATE_SYMLINK:
case CREATE_CHAR_DEVICE:
case CREATE_BLOCK_DEVICE:
case IGNORE_PATH:
case IGNORE_DIRECTORY_PATH:
case ADJUST_MODE:
case RELABEL_PATH:
case RECURSIVE_RELABEL_PATH:
case WRITE_FILE:
case COPY_FILES:
break;
case REMOVE_PATH:
case TRUNCATE_DIRECTORY:
case RECURSIVE_REMOVE_PATH:
r = glob_item(i, remove_item_instance);
break;
}
return r;
}
bool mountpoint;
int r;
assert(i);
if (!i->age_set)
return 0;
n = now(CLOCK_REALTIME);
if (n < i->age)
return 0;
if (!d) {
return 0;
return -errno;
}
return -errno;
}
return -ENOTDIR;
}
return -errno;
}
MAX_DEPTH, i->keep_first_level);
return r;
}
static int clean_item(Item *i) {
int r = 0;
assert(i);
switch (i->type) {
case CREATE_DIRECTORY:
case TRUNCATE_DIRECTORY:
case IGNORE_PATH:
case COPY_FILES:
clean_item_instance(i, i->path);
break;
case IGNORE_DIRECTORY_PATH:
r = glob_item(i, clean_item_instance);
break;
default:
break;
}
return r;
}
static int process_item(Item *i) {
int r, q, p;
assert(i);
if (i->done)
return 0;
i->done = true;
Item *j;
if (j)
process_item(j);
}
r = arg_create ? create_item(i) : 0;
q = arg_remove ? remove_item(i) : 0;
p = arg_clean ? clean_item(i) : 0;
if (r < 0)
return r;
if (q < 0)
return q;
return p;
}
if (!i)
return;
free(i);
}
assert(a);
assert(b);
return false;
return false;
return false;
return false;
return false;
return false;
if ((a->type == CREATE_FILE ||
a->type == TRUNCATE_FILE ||
a->type == WRITE_FILE ||
a->type == CREATE_SYMLINK ||
a->type == COPY_FILES) &&
return false;
if ((a->type == CREATE_CHAR_DEVICE ||
a->type == CREATE_BLOCK_DEVICE) &&
a->major_minor != b->major_minor)
return false;
return true;
}
static bool should_include_path(const char *path) {
char **prefix;
return false;
return true;
/* no matches, so we should include this path only if we
* have no whitelist at all */
return strv_length(arg_include_prefixes) == 0;
}
static const Specifier specifier_table[] = {
{}
};
_cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
char type;
Hashmap *h;
int r, n = -1;
"%ms %ms %ms %ms %ms %ms %n",
&action,
&path,
&mode,
&user,
&group,
&age,
&n);
if (r < 2) {
return -EIO;
}
return -EINVAL;
}
return -EINVAL;
}
return 0;
if (!i)
return log_oom();
if (r < 0) {
return r;
}
if (n >= 0) {
if (!i->argument)
return log_oom();
}
}
switch (type) {
case CREATE_FILE:
case TRUNCATE_FILE:
case CREATE_DIRECTORY:
case TRUNCATE_DIRECTORY:
case CREATE_FIFO:
case IGNORE_PATH:
case IGNORE_DIRECTORY_PATH:
case REMOVE_PATH:
case RECURSIVE_REMOVE_PATH:
case ADJUST_MODE:
case RELABEL_PATH:
case RECURSIVE_RELABEL_PATH:
break;
case CREATE_SYMLINK:
if (!i->argument) {
if (!i->argument)
return log_oom();
}
break;
case WRITE_FILE:
if (!i->argument) {
return -EBADMSG;
}
break;
case COPY_FILES:
if (!i->argument) {
if (!i->argument)
return log_oom();
}
if (!path_is_absolute(i->argument)) {
return -EBADMSG;
}
break;
case CREATE_CHAR_DEVICE:
case CREATE_BLOCK_DEVICE: {
if (!i->argument) {
return -EBADMSG;
}
return -EBADMSG;
}
break;
}
default:
return -EBADMSG;
}
if (!path_is_absolute(i->path)) {
return -EBADMSG;
}
path_kill_slashes(i->path);
if (!should_include_path(i->path))
return 0;
if (arg_root) {
char *p;
if (!p)
return log_oom();
i->path = p;
}
const char *u = user;
if (r < 0) {
return r;
}
i->uid_set = true;
}
const char *g = group;
r = get_group_creds(&g, &i->gid);
if (r < 0) {
return r;
}
i->gid_set = true;
}
unsigned m;
if (*mm == '~') {
i->mask_perms = true;
mm++;
}
return -ENOENT;
}
i->mode = m;
i->mode_set = true;
} else
i->mode =
i->type == CREATE_DIRECTORY ||
const char *a = age;
if (*a == '~') {
i->keep_first_level = true;
a++;
}
return -EBADMSG;
}
i->age_set = true;
}
if (existing) {
/* Two identical items are fine */
if (!item_equal(existing, i))
return 0;
}
r = hashmap_put(h, i->path, i);
if (r < 0) {
return r;
}
i = NULL; /* avoid cleanup */
return 0;
}
static void help(void) {
printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
"Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --create Create marked files/directories\n"
" --clean Clean up marked directories\n"
" --remove Remove marked files/directories\n"
" --boot Execute actions only safe at boot\n"
" --prefix=PATH Only apply rules that apply to paths with the specified prefix\n"
" --exclude-prefix=PATH Ignore rules that apply to paths with the specified prefix\n"
" --root=PATH Operate on an alternate filesystem root\n",
}
enum {
ARG_VERSION = 0x100,
};
{}
};
int c;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return 0;
case ARG_CREATE:
arg_create = true;
break;
case ARG_CLEAN:
arg_clean = true;
break;
case ARG_REMOVE:
arg_remove = true;
break;
case ARG_BOOT:
arg_boot = true;
break;
case ARG_PREFIX:
return log_oom();
break;
case ARG_EXCLUDE_PREFIX:
return log_oom();
break;
case ARG_ROOT:
if (!arg_root)
return log_oom();
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
log_error("You need to specify at least one of --clean, --create or --remove.");
return -EINVAL;
}
return 1;
}
unsigned v = 0;
Item *i;
int r;
if (r < 0) {
if (ignore_enoent && r == -ENOENT)
return 0;
return r;
}
FOREACH_LINE(line, f, break) {
char *l;
int k;
v++;
if (*l == '#' || *l == 0)
continue;
k = parse_line(fn, v, l);
if (k < 0 && r == 0)
r = k;
}
/* we have to determine age parameter for each entry of type X */
if (i->type != IGNORE_DIRECTORY_PATH)
continue;
continue;
candidate_item = j;
break;
}
(candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
candidate_item = j;
}
if (candidate_item) {
i->age_set = true;
}
}
if (ferror(f)) {
if (r == 0)
r = -EIO;
}
return r;
}
int r, k;
Item *i;
if (r <= 0)
goto finish;
log_open();
umask(0022);
r = log_oom();
goto finish;
}
r = 0;
int j;
k = read_config_file(argv[j], false);
if (k < 0 && r == 0)
r = k;
}
} else {
char **f;
if (r < 0) {
goto finish;
}
STRV_FOREACH(f, files) {
k = read_config_file(*f, true);
if (k < 0 && r == 0)
r = k;
}
}
process_item(i);
process_item(i);
while ((i = hashmap_steal_first(items)))
item_free(i);
while ((i = hashmap_steal_first(globs)))
item_free(i);
label_finish();
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}