]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/user-runtime-dir.c
51b864c9f826f9e795816de044b092ca100f2db4
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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_debug_errno(r
, "Failed to remove runtime directory %s (before unmounting), ignoring: %m", runtime_path
);
100 /* Ignore cases where the directory isn't mounted, as that's quite possible, if we lacked the permissions to
102 r
= umount2(runtime_path
, MNT_DETACH
);
103 if (r
< 0 && !IN_SET(errno
, EINVAL
, ENOENT
))
104 log_debug_errno(errno
, "Failed to unmount user runtime directory %s, ignoring: %m", runtime_path
);
106 r
= rm_rf(runtime_path
, REMOVE_ROOT
);
107 if (r
< 0 && r
!= -ENOENT
)
108 return log_error_errno(r
, "Failed to remove runtime directory %s (after unmounting): %m", runtime_path
);
113 static int do_mount(const char *user
) {
114 char runtime_path
[sizeof("/run/user") + DECIMAL_STR_MAX(uid_t
)];
115 size_t runtime_dir_size
;
120 r
= get_user_creds(&user
, &uid
, &gid
, NULL
, NULL
);
122 return log_error_errno(r
,
123 r
== -ESRCH
? "No such user \"%s\"" :
124 r
== -ENOMSG
? "UID \"%s\" is invalid or has an invalid main group"
125 : "Failed to look up user \"%s\": %m",
128 xsprintf(runtime_path
, "/run/user/" UID_FMT
, uid
);
130 assert_se(gather_configuration(&runtime_dir_size
) == 0);
132 log_debug("Will mount %s owned by "UID_FMT
":"GID_FMT
, runtime_path
, uid
, gid
);
133 return user_mkdir_runtime_path(runtime_path
, uid
, gid
, runtime_dir_size
);
136 static int do_umount(const char *user
) {
137 char runtime_path
[sizeof("/run/user") + DECIMAL_STR_MAX(uid_t
)];
141 /* The user may be already removed. So, first try to parse the string by parse_uid(),
142 * and if it fails, fallback to get_user_creds().*/
143 if (parse_uid(user
, &uid
) < 0) {
144 r
= get_user_creds(&user
, &uid
, NULL
, NULL
, NULL
);
146 return log_error_errno(r
,
147 r
== -ESRCH
? "No such user \"%s\"" :
148 r
== -ENOMSG
? "UID \"%s\" is invalid or has an invalid main group"
149 : "Failed to look up user \"%s\": %m",
153 xsprintf(runtime_path
, "/run/user/" UID_FMT
, uid
);
155 log_debug("Will remove %s", runtime_path
);
156 return user_remove_runtime_path(runtime_path
);
159 int main(int argc
, char *argv
[]) {
162 log_parse_environment();
166 log_error("This program takes two arguments.");
169 if (!STR_IN_SET(argv
[1], "start", "stop")) {
170 log_error("First argument must be either \"start\" or \"stop\".");
176 if (streq(argv
[1], "start"))
177 r
= do_mount(argv
[2]);
178 else if (streq(argv
[1], "stop"))
179 r
= do_umount(argv
[2]);
181 assert_not_reached("Unknown verb!");
183 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;