1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 //#include <sys/mount.h>
10 //#include "mount-util.h"
11 //#include "path-util.h"
13 //#include "smack-util.h"
14 //#include "stdio-util.h"
15 //#include "string-util.h"
17 //#include "user-util.h"
19 static int gather_configuration(size_t *runtime_dir_size) {
23 manager_reset_config(&m);
25 r = manager_parse_config_file(&m);
27 log_warning_errno(r, "Failed to parse logind.conf: %m");
29 *runtime_dir_size = m.runtime_dir_size;
33 static int user_mkdir_runtime_path(const char *runtime_path, uid_t uid, gid_t gid, size_t runtime_dir_size) {
37 assert(path_is_absolute(runtime_path));
38 assert(uid_is_valid(uid));
39 assert(gid_is_valid(gid));
41 r = mkdir_safe_label("/run/user", 0755, 0, 0, MKDIR_WARN_MODE);
43 return log_error_errno(r, "Failed to create /run/user: %m");
45 if (path_is_mount_point(runtime_path, NULL, 0) >= 0)
46 log_debug("%s is already a mount point", runtime_path);
48 char options[sizeof("mode=0700,uid=,gid=,size=,smackfsroot=*")
49 + DECIMAL_STR_MAX(uid_t)
50 + DECIMAL_STR_MAX(gid_t)
51 + DECIMAL_STR_MAX(size_t)];
54 "mode=0700,uid=" UID_FMT ",gid=" GID_FMT ",size=%zu%s",
55 uid, gid, runtime_dir_size,
56 mac_smack_use() ? ",smackfsroot=*" : "");
58 (void) mkdir_label(runtime_path, 0700);
60 r = mount("tmpfs", runtime_path, "tmpfs", MS_NODEV|MS_NOSUID, options);
62 if (!IN_SET(errno, EPERM, EACCES)) {
63 r = log_error_errno(errno, "Failed to mount per-user tmpfs directory %s: %m", runtime_path);
67 log_debug_errno(errno, "Failed to mount per-user tmpfs directory %s.\n"
68 "Assuming containerized execution, ignoring: %m", runtime_path);
70 r = chmod_and_chown(runtime_path, 0700, uid, gid);
72 log_error_errno(r, "Failed to change ownership and mode of \"%s\": %m", runtime_path);
77 r = label_fix(runtime_path, 0);
79 log_warning_errno(r, "Failed to fix label of \"%s\", ignoring: %m", runtime_path);
85 /* Try to clean up, but ignore errors */
86 (void) rmdir(runtime_path);
90 static int user_remove_runtime_path(const char *runtime_path) {
94 assert(path_is_absolute(runtime_path));
96 r = rm_rf(runtime_path, 0);
98 log_error_errno(r, "Failed to remove runtime directory %s (before unmounting): %m", runtime_path);
100 /* Ignore cases where the directory isn't mounted, as that's
101 * quite possible, if we lacked the permissions to mount
103 r = umount2(runtime_path, MNT_DETACH);
104 if (r < 0 && !IN_SET(errno, EINVAL, ENOENT))
105 log_error_errno(errno, "Failed to unmount user runtime directory %s: %m", runtime_path);
107 r = rm_rf(runtime_path, REMOVE_ROOT);
109 log_error_errno(r, "Failed to remove runtime directory %s (after unmounting): %m", runtime_path);
114 static int do_mount(const char *runtime_path, uid_t uid, gid_t gid) {
115 size_t runtime_dir_size;
117 assert_se(gather_configuration(&runtime_dir_size) == 0);
119 log_debug("Will mount %s owned by "UID_FMT":"GID_FMT, runtime_path, uid, gid);
120 return user_mkdir_runtime_path(runtime_path, uid, gid, runtime_dir_size);
123 static int do_umount(const char *runtime_path) {
124 log_debug("Will remove %s", runtime_path);
125 return user_remove_runtime_path(runtime_path);
128 int main(int argc, char *argv[]) {
132 char runtime_path[sizeof("/run/user") + DECIMAL_STR_MAX(uid_t)];
135 log_parse_environment();
139 log_error("This program takes two arguments.");
142 if (!STR_IN_SET(argv[1], "start", "stop")) {
143 log_error("First argument must be either \"start\" or \"stop\".");
150 r = get_user_creds(&user, &uid, &gid, NULL, NULL);
153 r == -ESRCH ? "No such user \"%s\"" :
154 r == -ENOMSG ? "UID \"%s\" is invalid or has an invalid main group"
155 : "Failed to look up user \"%s\": %m",
160 xsprintf(runtime_path, "/run/user/" UID_FMT, uid);
162 if (streq(argv[1], "start"))
163 r = do_mount(runtime_path, uid, gid);
164 else if (streq(argv[1], "stop"))
165 r = do_umount(runtime_path);
167 assert_not_reached("Unknown verb!");
169 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;