/* SPDX-License-Identifier: LGPL-2.1+ */
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering, Kay Sievers
- Copyright 2015 Zbigniew Jędrzejewski-Szmek
-
- systemd is free software; you can redistribute it and/or modify it
- 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 <errno.h>
#include <fcntl.h>
#include "missing.h"
#include "mkdir.h"
#include "mount-util.h"
+#include "pager.h"
#include "parse-util.h"
#include "path-lookup.h"
#include "path-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
+#include "terminal-util.h"
#include "umask-util.h"
#include "user-util.h"
#include "util.h"
_DIRECTORY_TYPE_MAX,
} DirectoryType;
+static bool arg_cat_config = false;
static bool arg_user = false;
static bool arg_create = false;
static bool arg_clean = false;
static bool arg_remove = false;
static bool arg_boot = false;
+static bool arg_no_pager = false;
static char **arg_include_prefixes = NULL;
static char **arg_exclude_prefixes = NULL;
{ 'U', specifier_user_id, NULL },
{ 'u', specifier_user_name, NULL },
{ 'h', specifier_user_home, NULL },
+
{ 't', specifier_directory, UINT_TO_PTR(DIRECTORY_RUNTIME) },
{ 'S', specifier_directory, UINT_TO_PTR(DIRECTORY_STATE) },
{ 'C', specifier_directory, UINT_TO_PTR(DIRECTORY_CACHE) },
{ 'L', specifier_directory, UINT_TO_PTR(DIRECTORY_LOGS) },
+ { 'T', specifier_tmp_dir, NULL },
+ { 'V', specifier_var_tmp_dir, NULL },
{}
};
goto fail;
}
- path_kill_slashes(s);
+ path_simplify(s, false);
r = set_consume(unix_sockets, s);
if (r < 0 && r != -EEXIST) {
static int fd_set_perms(Item *i, int fd, const struct stat *st) {
_cleanup_free_ char *path = NULL;
+ struct stat stbuf;
int r;
assert(i);
if (!i->mode_set && !i->uid_set && !i->gid_set)
goto shortcut;
+ if (!st) {
+ if (fstat(fd, &stbuf) < 0)
+ return log_error_errno(errno, "fstat(%s) failed: %m", path);
+ st = &stbuf;
+ }
+
if (hardlink_vulnerable(st)) {
log_error("Refusing to set permissions on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.", path);
return -EPERM;
if (m == (st->st_mode & 07777))
log_debug("\"%s\" has correct mode %o already.", path, st->st_mode);
else {
- char procfs_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
-
log_debug("Changing \"%s\" to mode %o.", path, m);
-
- /* fchmodat() still doesn't have AT_EMPTY_PATH flag. */
- xsprintf(procfs_path, "/proc/self/fd/%i", fd);
-
- if (chmod(procfs_path, m) < 0)
- return log_error_errno(errno, "chmod() of %s via %s failed: %m", path, procfs_path);
+ if (fchmod_opath(fd, m) < 0)
+ return log_error_errno(errno, "fchmod() of %s failed: %m", path);
}
}
}
}
shortcut:
- return label_fix(path, false, false);
+ return label_fix(path, 0);
}
static int path_set_perms(Item *i, const char *path) {
_cleanup_close_ int fd = -1;
- struct stat st;
+ struct stat stbuf, *st = NULL;
assert(i);
assert(path);
return r;
}
- if (fstat(fd, &st) < 0)
- return log_error_errno(errno, "Failed to fstat() file %s: %m", path);
+ if (i->type == EMPTY_DIRECTORY) {
+ /* FIXME: introduce fd_is_dir() helper ? */
+ if (fstat(fd, &stbuf) < 0)
+ return log_error_errno(errno, "Failed to fstat() file %s: %m", path);
+
+ if (!S_ISDIR(stbuf.st_mode)) {
+ log_error("'%s' already exists and is not a directory. ", path);
+ return -EEXIST;
+ }
+
+ st = &stbuf;
+ }
- return fd_set_perms(i, fd, &st);
+ return fd_set_perms(i, fd, st);
}
static int parse_xattrs_from_arg(Item *i) {
}
static int fd_set_xattrs(Item *i, int fd, const struct stat *st) {
- char procfs_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
+ char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
_cleanup_free_ char *path = NULL;
char **name, **value;
int r;
static int fd_set_acls(Item *item, int fd, const struct stat *st) {
int r = 0;
#if HAVE_ACL
- char procfs_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
+ char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
_cleanup_free_ char *path = NULL;
+ struct stat stbuf;
assert(item);
assert(fd);
- assert(st);
r = fd_get_path(fd, &path);
if (r < 0)
return r;
+ if (!st) {
+ if (fstat(fd, &stbuf) < 0)
+ return log_error_errno(errno, "fstat(%s) failed: %m", path);
+ st = &stbuf;
+ }
+
if (hardlink_vulnerable(st)) {
log_error("Refusing to set ACLs on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.", path);
return -EPERM;
if (item->acl_access)
r = path_set_acl(procfs_path, path, ACL_TYPE_ACCESS, item->acl_access, item->force);
- if (r == 0 && item->acl_default)
+ /* set only default acls to folders */
+ if (r == 0 && item->acl_default && S_ISDIR(st->st_mode))
r = path_set_acl(procfs_path, path, ACL_TYPE_DEFAULT, item->acl_default, item->force);
if (r > 0)
static int path_set_acls(Item *item, const char *path) {
int r = 0;
-#ifdef HAVE_ACL
+#if HAVE_ACL
_cleanup_close_ int fd = -1;
- struct stat st;
assert(item);
assert(path);
if (fd < 0)
return log_error_errno(errno, "Adjusting ACL of %s failed: %m", path);
- if (fstat(fd, &st) < 0)
- return log_error_errno(errno, "Failed to fstat() file %s: %m", path);
-
- r = fd_set_acls(item, fd, &st);
- #endif
- return r;
- }
+ r = fd_set_acls(item, fd, NULL);
+#endif
+ return r;
+}
#define ATTRIBUTES_ALL \
(FS_NOATIME_FL | \
}
static int fd_set_attribute(Item *item, int fd, const struct stat *st) {
- char procfs_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
_cleanup_close_ int procfs_fd = -1;
_cleanup_free_ char *path = NULL;
+ struct stat stbuf;
unsigned f;
int r;
if (r < 0)
return r;
+ if (!st) {
+ if (fstat(fd, &stbuf) < 0)
+ return log_error_errno(errno, "fstat(%s) failed: %m", path);
+ st = &stbuf;
+ }
+
/* Issuing the file attribute ioctls on device nodes is not
* safe, as that will be delivered to the drivers, not the
* file system containing the device node. */
if (!S_ISDIR(st->st_mode))
f &= ~FS_DIRSYNC_FL;
- xsprintf(procfs_path, "/proc/self/fd/%i", fd);
-
- procfs_fd = open(procfs_path, O_RDONLY|O_CLOEXEC|O_NOATIME);
+ procfs_fd = fd_reopen(fd, O_RDONLY|O_CLOEXEC|O_NOATIME);
if (procfs_fd < 0)
return -errno;
static int path_set_attribute(Item *item, const char *path) {
_cleanup_close_ int fd = -1;
- struct stat st;
if (!item->attribute_set || item->attribute_mask == 0)
return 0;
if (fd < 0)
return log_error_errno(errno, "Cannot open '%s': %m", path);
- if (fstat(fd, &st) < 0)
- return log_error_errno(errno, "Cannot stat '%s': %m", path);
-
- return fd_set_attribute(item, fd, &st);
+ return fd_set_attribute(item, fd, NULL);
}
static int write_one_file(Item *i, const char *path) {
RUN_WITH_UMASK(0000) {
mac_selinux_create_file_prepare(path, S_IFREG);
- fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode);
+ fd = open(path, flags|O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode);
mac_selinux_create_file_clear();
}
fd = safe_close(fd);
-done:
+ done:
if (stat(path, &st) < 0)
return log_error_errno(errno, "stat(%s) failed: %m", path);
typedef int (*action_t)(Item *, const char *);
typedef int (*fdaction_t)(Item *, int fd, const struct stat *st);
-static int item_do(Item *i, int fd, const struct stat *st, fdaction_t action) {
+static int item_do(Item *i, int fd, fdaction_t action) {
+ struct stat st;
int r = 0, q;
assert(i);
assert(fd >= 0);
- assert(st);
+
+ if (fstat(fd, &st) < 0) {
+ r = -errno;
+ goto finish;
+ }
/* This returns the first error we run into, but nevertheless
* tries to go on */
- r = action(i, fd, st);
+ r = action(i, fd, &st);
- if (S_ISDIR(st->st_mode)) {
- char procfs_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
+ if (S_ISDIR(st.st_mode)) {
+ char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
_cleanup_closedir_ DIR *d = NULL;
struct dirent *de;
}
FOREACH_DIRENT_ALL(de, d, q = -errno; goto finish) {
- struct stat de_st;
int de_fd;
if (dot_or_dot_dot(de->d_name))
continue;
de_fd = openat(fd, de->d_name, O_NOFOLLOW|O_CLOEXEC|O_PATH);
- if (de_fd >= 0 && fstat(de_fd, &de_st) >= 0)
+ if (de_fd >= 0)
/* pass ownership of dirent fd over */
- q = item_do(i, de_fd, &de_st, action);
+ q = item_do(i, de_fd, action);
else
q = -errno;
STRV_FOREACH(fn, g.gl_pathv) {
_cleanup_close_ int fd = -1;
- struct stat st;
/* Make sure we won't trigger/follow file object (such as
* device nodes, automounts, ...) pointed out by 'fn' with
continue;
}
- if (fstat(fd, &st) < 0) {
- r = r ?: -errno;
- continue;
- }
-
- k = item_do(i, fd, &st, action);
+ k = item_do(i, fd, action);
if (k < 0 && r == 0)
r = k;
return r;
break;
- case COPY_FILES: {
-
+ case COPY_FILES:
RUN_WITH_UMASK(0000)
(void) mkdir_parents_label(i->path, 0755);
if (IN_SET(i->type, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA)) {
- if (btrfs_is_subvol(isempty(arg_root) ? "/" : arg_root) <= 0)
+ if (btrfs_is_subvol(empty_to_root(arg_root)) <= 0)
/* Don't create a subvolume unless the
* root directory is one, too. We do
_fallthrough_;
case EMPTY_DIRECTORY:
- r = path_set_perms(i, i->path);
+ r = glob_item(i, path_set_perms);
if (q < 0)
return q;
if (r < 0)
return r;
break;
- }
case CREATE_SYMLINK: {
RUN_WITH_UMASK(0000)
r = symlink_atomic(i->argument, i->path);
mac_selinux_create_file_clear();
- if (IN_SET(r, -EEXIST, -ENOTEMPTY)) {
+ if (IN_SET(r, -EISDIR, -EEXIST, -ENOTEMPTY)) {
r = rm_rf(i->path, REMOVE_ROOT|REMOVE_PHYSICAL);
if (r < 0)
return log_error_errno(r, "rm -fr %s failed: %m", i->path);
return 0;
}
+static int patch_var_run(const char *fname, unsigned line, char **path) {
+ const char *k;
+ char *n;
+
+ assert(path);
+ assert(*path);
+
+ /* Optionally rewrites lines referencing /var/run/, to use /run/ instead. Why bother? tmpfiles merges lines in
+ * some cases and detects conflicts in others. If files/directories are specified through two equivalent lines
+ * this is problematic as neither case will be detected. Ideally we'd detect these cases by resolving symlinks
+ * early, but that's precisely not what we can do here as this code very likely is running very early on, at a
+ * time where the paths in question are not available yet, or even more importantly, our own tmpfiles rules
+ * might create the paths that are intermediary to the listed paths. We can't really cover the generic case,
+ * but the least we can do is cover the specific case of /var/run vs. /run, as /var/run is a legacy name for
+ * /run only, and we explicitly document that and require that on systemd systems the former is a symlink to
+ * the latter. Moreover files below this path are by far the primary usecase for tmpfiles.d/. */
+
+ k = path_startswith(*path, "/var/run/");
+ if (isempty(k)) /* Don't complain about other paths than /var/run, and not about /var/run itself either. */
+ return 0;
+
+ n = strjoin("/run/", k);
+ if (!n)
+ return log_oom();
+
+ /* Also log about this briefly. We do so at LOG_NOTICE level, as we fixed up the situation automatically, hence
+ * there's no immediate need for action by the user. However, in the interest of making things less confusing
+ * to the user, let's still inform the user that these snippets should really be updated. */
+
+ log_notice("[%s:%u] Line references path below legacy directory /var/run/, updating %s → %s; please update the tmpfiles.d/ drop-in file accordingly.", fname, line, *path, n);
+
+ free(*path);
+ *path = n;
+
+ return 0;
+}
+
static int parse_line(const char *fname, unsigned line, const char *buffer, bool *invalid_config) {
_cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
/* invalid quoting and such or an unknown specifier */
*invalid_config = true;
return log_error_errno(r, "[%s:%u] Failed to parse line: %m", fname, line);
- }
-
- else if (r < 2) {
+ } else if (r < 2) {
*invalid_config = true;
log_error("[%s:%u] Syntax error.", fname, line);
return -EIO;
return log_error_errno(r, "[%s:%u] Failed to replace specifiers: %s", fname, line, path);
}
+ r = patch_var_run(fname, line, &i.path);
+ if (r < 0)
+ return r;
+
switch (i.type) {
case CREATE_DIRECTORY:
return -EBADMSG;
}
- path_kill_slashes(i.argument);
+ path_simplify(i.argument, false);
break;
case CREATE_CHAR_DEVICE:
return -EBADMSG;
}
- path_kill_slashes(i.path);
+ path_simplify(i.path, false);
if (!should_include_path(i.path))
return 0;
return 0;
}
+static int cat_config(char **config_dirs, char **args) {
+ _cleanup_strv_free_ char **files = NULL;
+ int r;
+
+ r = conf_files_list_with_replacement(arg_root, config_dirs, arg_replace, &files, NULL);
+ if (r < 0)
+ return r;
+
+ return cat_files(NULL, files, 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"
" --user Execute user configuration\n"
" --version Show package version\n"
+ " --cat-config Show configuration files\n"
" --create Create marked files/directories\n"
" --clean Clean up marked directories\n"
" --remove Remove marked files/directories\n"
" --exclude-prefix=PATH Ignore rules with the specified prefix\n"
" --root=PATH Operate on an alternate filesystem root\n"
" --replace=PATH Treat arguments as replacement for PATH\n"
+ " --no-pager Do not pipe output into a pager\n"
, program_invocation_short_name);
}
enum {
ARG_VERSION = 0x100,
+ ARG_CAT_CONFIG,
ARG_USER,
ARG_CREATE,
ARG_CLEAN,
ARG_EXCLUDE_PREFIX,
ARG_ROOT,
ARG_REPLACE,
+ ARG_NO_PAGER,
};
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "user", no_argument, NULL, ARG_USER },
{ "version", no_argument, NULL, ARG_VERSION },
+ { "cat-config", no_argument, NULL, ARG_CAT_CONFIG },
{ "create", no_argument, NULL, ARG_CREATE },
{ "clean", no_argument, NULL, ARG_CLEAN },
{ "remove", no_argument, NULL, ARG_REMOVE },
{ "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX },
{ "root", required_argument, NULL, ARG_ROOT },
{ "replace", required_argument, NULL, ARG_REPLACE },
+ { "no-pager", no_argument, NULL, ARG_NO_PAGER },
{}
};
case ARG_VERSION:
return version();
+ case ARG_CAT_CONFIG:
+ arg_cat_config = true;
+ break;
+
case ARG_USER:
arg_user = true;
break;
arg_replace = optarg;
break;
+ case ARG_NO_PAGER:
+ arg_no_pager = true;
+ break;
+
case '?':
return -EINVAL;
assert_not_reached("Unhandled option");
}
- if (!arg_clean && !arg_create && !arg_remove) {
+ if (!arg_clean && !arg_create && !arg_remove && !arg_cat_config) {
log_error("You need to specify at least one of --clean, --create or --remove.");
return -EINVAL;
}
+ if (arg_replace && arg_cat_config) {
+ log_error("Option --replace= is not supported with --cat-config");
+ return -EINVAL;
+ }
+
if (arg_replace && optind >= argc) {
log_error("When --replace= is given, some configuration items must be specified");
return -EINVAL;
char **f;
int r;
- r = conf_files_list_strv(&files, ".conf", arg_root, 0, (const char* const*) config_dirs);
+ r = conf_files_list_with_replacement(arg_root, config_dirs, arg_replace, &files, &p);
if (r < 0)
- return log_error_errno(r, "Failed to enumerate tmpfiles.d files: %m");
-
- if (arg_replace) {
- r = conf_files_insert(&files, arg_root, config_dirs, arg_replace);
- if (r < 0)
- return log_error_errno(r, "Failed to extend tmpfiles.d file list: %m");
-
- p = path_join(arg_root, arg_replace, NULL);
- if (!p)
- return log_oom();
- }
+ return r;
STRV_FOREACH(f, files)
if (p && path_equal(*f, p)) {
}
int main(int argc, char *argv[]) {
- int r, k;
+ int r, k, r_process = 0;
ItemArray *a;
Iterator iterator;
_cleanup_strv_free_ char **config_dirs = NULL;
log_parse_environment();
log_open();
- umask(0022);
-
- mac_selinux_init();
-
- items = ordered_hashmap_new(&string_hash_ops);
- globs = ordered_hashmap_new(&string_hash_ops);
-
- if (!items || !globs) {
- r = log_oom();
- goto finish;
- }
-
- r = 0;
-
if (arg_user) {
r = user_config_paths(&config_dirs);
if (r < 0) {
t = strv_join(config_dirs, "\n\t");
if (t)
- log_debug("Looking for configuration files in (higher priority first:\n\t%s", t);
+ log_debug("Looking for configuration files in (higher priority first):\n\t%s", t);
+ }
+
+ if (arg_cat_config) {
+ (void) pager_open(arg_no_pager, false);
+
+ r = cat_config(config_dirs, argv + optind);
+ goto finish;
+ }
+
+ umask(0022);
+
+ mac_selinux_init();
+
+ items = ordered_hashmap_new(&string_hash_ops);
+ globs = ordered_hashmap_new(&string_hash_ops);
+
+ if (!items || !globs) {
+ r = log_oom();
+ goto finish;
}
/* If command line arguments are specified along with --replace, read all
if (r < 0)
goto finish;
-
-
/* The non-globbing ones usually create things, hence we apply
* them first */
ORDERED_HASHMAP_FOREACH(a, items, iterator) {
k = process_item_array(a);
- if (k < 0 && r == 0)
- r = k;
+ if (k < 0 && r_process == 0)
+ r_process = k;
}
/* The globbing ones usually alter things, hence we apply them
* second. */
ORDERED_HASHMAP_FOREACH(a, globs, iterator) {
k = process_item_array(a);
- if (k < 0 && r == 0)
- r = k;
+ if (k < 0 && r_process == 0)
+ r_process = k;
}
finish:
+ pager_close();
+
ordered_hashmap_free_with_destructor(items, item_array_free);
ordered_hashmap_free_with_destructor(globs, item_array_free);
mac_selinux_finish();
- if (r < 0)
+ if (r < 0 || ERRNO_IS_RESOURCE(-r_process))
return EXIT_FAILURE;
else if (invalid_config)
return EX_DATAERR;
+ else if (r_process < 0)
+ return EX_CANTCREAT;
else
return EXIT_SUCCESS;
}