]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/user-runtime-dir.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / login / user-runtime-dir.c
CommitLineData
a9f0f5e5
ZJS
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#include <stdint.h>
4#include <sys/mount.h>
5
07ee5adb
LP
6#include "sd-bus.h"
7
8#include "bus-error.h"
a9f0f5e5
ZJS
9#include "fs-util.h"
10#include "label.h"
5e332028 11#include "main-func.h"
a9f0f5e5 12#include "mkdir.h"
049af8ad 13#include "mountpoint-util.h"
a9f0f5e5
ZJS
14#include "path-util.h"
15#include "rm-rf.h"
81375d80 16#include "selinux-util.h"
a9f0f5e5
ZJS
17#include "smack-util.h"
18#include "stdio-util.h"
19#include "string-util.h"
20#include "strv.h"
21#include "user-util.h"
22
07ee5adb
LP
23static int acquire_runtime_dir_size(uint64_t *ret) {
24 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
92e31da1 25 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
a9f0f5e5
ZJS
26 int r;
27
07ee5adb
LP
28 r = sd_bus_default_system(&bus);
29 if (r < 0)
30 return log_error_errno(r, "Failed to connect to system bus: %m");
a9f0f5e5 31
07ee5adb 32 r = sd_bus_get_property_trivial(bus, "org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "RuntimeDirectorySize", &error, 't', ret);
a9f0f5e5 33 if (r < 0)
07ee5adb 34 return log_error_errno(r, "Failed to acquire runtime directory size: %s", bus_error_message(&error, r));
a9f0f5e5 35
a9f0f5e5
ZJS
36 return 0;
37}
38
07ee5adb
LP
39static int user_mkdir_runtime_path(
40 const char *runtime_path,
41 uid_t uid,
42 gid_t gid,
43 uint64_t runtime_dir_size) {
44
a9f0f5e5
ZJS
45 int r;
46
47 assert(runtime_path);
48 assert(path_is_absolute(runtime_path));
49 assert(uid_is_valid(uid));
50 assert(gid_is_valid(gid));
51
52 r = mkdir_safe_label("/run/user", 0755, 0, 0, MKDIR_WARN_MODE);
53 if (r < 0)
54 return log_error_errno(r, "Failed to create /run/user: %m");
55
56 if (path_is_mount_point(runtime_path, NULL, 0) >= 0)
57 log_debug("%s is already a mount point", runtime_path);
58 else {
59 char options[sizeof("mode=0700,uid=,gid=,size=,smackfsroot=*")
60 + DECIMAL_STR_MAX(uid_t)
61 + DECIMAL_STR_MAX(gid_t)
07ee5adb 62 + DECIMAL_STR_MAX(uint64_t)];
a9f0f5e5
ZJS
63
64 xsprintf(options,
07ee5adb 65 "mode=0700,uid=" UID_FMT ",gid=" GID_FMT ",size=%" PRIu64 "%s",
a9f0f5e5
ZJS
66 uid, gid, runtime_dir_size,
67 mac_smack_use() ? ",smackfsroot=*" : "");
68
69 (void) mkdir_label(runtime_path, 0700);
70
71 r = mount("tmpfs", runtime_path, "tmpfs", MS_NODEV|MS_NOSUID, options);
72 if (r < 0) {
73 if (!IN_SET(errno, EPERM, EACCES)) {
74 r = log_error_errno(errno, "Failed to mount per-user tmpfs directory %s: %m", runtime_path);
75 goto fail;
76 }
77
78 log_debug_errno(errno, "Failed to mount per-user tmpfs directory %s.\n"
79 "Assuming containerized execution, ignoring: %m", runtime_path);
80
81 r = chmod_and_chown(runtime_path, 0700, uid, gid);
82 if (r < 0) {
83 log_error_errno(r, "Failed to change ownership and mode of \"%s\": %m", runtime_path);
84 goto fail;
85 }
86 }
87
88 r = label_fix(runtime_path, 0);
89 if (r < 0)
90 log_warning_errno(r, "Failed to fix label of \"%s\", ignoring: %m", runtime_path);
91 }
92
93 return 0;
94
95fail:
96 /* Try to clean up, but ignore errors */
97 (void) rmdir(runtime_path);
98 return r;
99}
100
101static int user_remove_runtime_path(const char *runtime_path) {
102 int r;
103
104 assert(runtime_path);
105 assert(path_is_absolute(runtime_path));
106
107 r = rm_rf(runtime_path, 0);
108 if (r < 0)
3a13442b 109 log_debug_errno(r, "Failed to remove runtime directory %s (before unmounting), ignoring: %m", runtime_path);
a9f0f5e5 110
3a13442b
LP
111 /* Ignore cases where the directory isn't mounted, as that's quite possible, if we lacked the permissions to
112 * mount something */
a9f0f5e5
ZJS
113 r = umount2(runtime_path, MNT_DETACH);
114 if (r < 0 && !IN_SET(errno, EINVAL, ENOENT))
3a13442b 115 log_debug_errno(errno, "Failed to unmount user runtime directory %s, ignoring: %m", runtime_path);
a9f0f5e5
ZJS
116
117 r = rm_rf(runtime_path, REMOVE_ROOT);
3a13442b
LP
118 if (r < 0 && r != -ENOENT)
119 return log_error_errno(r, "Failed to remove runtime directory %s (after unmounting): %m", runtime_path);
a9f0f5e5 120
3a13442b 121 return 0;
a9f0f5e5
ZJS
122}
123
86d18f3b
YW
124static int do_mount(const char *user) {
125 char runtime_path[sizeof("/run/user") + DECIMAL_STR_MAX(uid_t)];
07ee5adb 126 uint64_t runtime_dir_size;
86d18f3b
YW
127 uid_t uid;
128 gid_t gid;
129 int r;
130
fafff8f1 131 r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0);
86d18f3b
YW
132 if (r < 0)
133 return log_error_errno(r,
134 r == -ESRCH ? "No such user \"%s\"" :
135 r == -ENOMSG ? "UID \"%s\" is invalid or has an invalid main group"
136 : "Failed to look up user \"%s\": %m",
137 user);
138
07ee5adb
LP
139 r = acquire_runtime_dir_size(&runtime_dir_size);
140 if (r < 0)
141 return r;
a9f0f5e5 142
07ee5adb 143 xsprintf(runtime_path, "/run/user/" UID_FMT, uid);
a9f0f5e5
ZJS
144
145 log_debug("Will mount %s owned by "UID_FMT":"GID_FMT, runtime_path, uid, gid);
146 return user_mkdir_runtime_path(runtime_path, uid, gid, runtime_dir_size);
147}
148
86d18f3b
YW
149static int do_umount(const char *user) {
150 char runtime_path[sizeof("/run/user") + DECIMAL_STR_MAX(uid_t)];
151 uid_t uid;
152 int r;
153
154 /* The user may be already removed. So, first try to parse the string by parse_uid(),
155 * and if it fails, fallback to get_user_creds().*/
156 if (parse_uid(user, &uid) < 0) {
fafff8f1 157 r = get_user_creds(&user, &uid, NULL, NULL, NULL, 0);
86d18f3b
YW
158 if (r < 0)
159 return log_error_errno(r,
160 r == -ESRCH ? "No such user \"%s\"" :
161 r == -ENOMSG ? "UID \"%s\" is invalid or has an invalid main group"
162 : "Failed to look up user \"%s\": %m",
163 user);
164 }
165
166 xsprintf(runtime_path, "/run/user/" UID_FMT, uid);
167
a9f0f5e5
ZJS
168 log_debug("Will remove %s", runtime_path);
169 return user_remove_runtime_path(runtime_path);
170}
171
cc639ee7 172static int run(int argc, char *argv[]) {
a9f0f5e5
ZJS
173 int r;
174
175 log_parse_environment();
176 log_open();
177
baaa35ad
ZJS
178 if (argc != 3)
179 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
180 "This program takes two arguments.");
181 if (!STR_IN_SET(argv[1], "start", "stop"))
182 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
183 "First argument must be either \"start\" or \"stop\".");
a9f0f5e5 184
81375d80 185 r = mac_selinux_init();
cc639ee7
ZJS
186 if (r < 0)
187 return log_error_errno(r, "Could not initialize labelling: %m\n");
81375d80 188
a9f0f5e5
ZJS
189 umask(0022);
190
a9f0f5e5 191 if (streq(argv[1], "start"))
cc639ee7
ZJS
192 return do_mount(argv[2]);
193 if (streq(argv[1], "stop"))
194 return do_umount(argv[2]);
195 assert_not_reached("Unknown verb!");
a9f0f5e5 196}
cc639ee7
ZJS
197
198DEFINE_MAIN_FUNCTION(run);