• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 15199265962

22 May 2025 09:40PM UTC coverage: 72.061% (-0.02%) from 72.079%
15199265962

push

github

bluca
tests: fix TEST-74-AUX-UTILS.varlinkctl.sh (#37562)

per Daan's explanation:
other subtests running as testuser apparently use systemd-run --user
--machine testuser@.host which turns user tracking in logind into "by
pin" mode. when the last pinning session exits it terminates the user.

299156 of 415145 relevant lines covered (72.06%)

703915.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.58
/src/login/user-runtime-dir.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <sys/mount.h>
4

5
#include "sd-bus.h"
6

7
#include "bus-error.h"
8
#include "bus-locator.h"
9
#include "devnum-util.h"
10
#include "errno-util.h"
11
#include "fd-util.h"
12
#include "format-util.h"
13
#include "fs-util.h"
14
#include "label-util.h"
15
#include "limits-util.h"
16
#include "log.h"
17
#include "main-func.h"
18
#include "missing_magic.h"
19
#include "missing_syscall.h"
20
#include "mkdir-label.h"
21
#include "mount-util.h"
22
#include "mountpoint-util.h"
23
#include "path-util.h"
24
#include "quota-util.h"
25
#include "rm-rf.h"
26
#include "set.h"
27
#include "smack-util.h"
28
#include "stat-util.h"
29
#include "stdio-util.h"
30
#include "string-util.h"
31
#include "strv.h"
32
#include "user-util.h"
33
#include "userdb.h"
34

35
static int acquire_runtime_dir_properties(uint64_t *ret_size, uint64_t *ret_inodes) {
173✔
36
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
37
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
173✔
38
        uint64_t size, inodes;
173✔
39
        int r;
173✔
40

41
        assert(ret_size);
173✔
42
        assert(ret_inodes);
173✔
43

44
        r = sd_bus_default_system(&bus);
173✔
45
        if (r < 0)
173✔
46
                return log_error_errno(r, "Failed to connect to system bus: %m");
×
47

48
        r = bus_get_property_trivial(bus, bus_login_mgr, "RuntimeDirectorySize", &error, 't', &size);
173✔
49
        if (r < 0) {
173✔
50
                log_warning_errno(r, "Failed to acquire runtime directory size, ignoring: %s", bus_error_message(&error, r));
×
51
                sd_bus_error_free(&error);
×
52

53
                size = physical_memory_scale(10U, 100U); /* 10% */
×
54
        }
55

56
        r = bus_get_property_trivial(bus, bus_login_mgr, "RuntimeDirectoryInodesMax", &error, 't', &inodes);
173✔
57
        if (r < 0) {
173✔
58
                log_warning_errno(r, "Failed to acquire number of inodes for runtime directory, ignoring: %s", bus_error_message(&error, r));
×
59
                sd_bus_error_free(&error);
×
60

61
                inodes = DIV_ROUND_UP(size, 4096);
×
62
        }
63

64
        *ret_size = size;
173✔
65
        *ret_inodes = inodes;
173✔
66

67
        return 0;
173✔
68
}
69

70
static int user_mkdir_runtime_path(
173✔
71
                const char *runtime_path,
72
                uid_t uid,
73
                gid_t gid,
74
                uint64_t runtime_dir_size,
75
                uint64_t runtime_dir_inodes) {
76

77
        int r;
173✔
78

79
        assert(runtime_path);
173✔
80
        assert(path_is_absolute(runtime_path));
173✔
81
        assert(uid_is_valid(uid));
173✔
82
        assert(gid_is_valid(gid));
173✔
83

84
        r = mkdir_safe_label("/run/user", 0755, 0, 0, MKDIR_WARN_MODE);
173✔
85
        if (r < 0)
173✔
86
                return log_error_errno(r, "Failed to create /run/user: %m");
×
87

88
        if (path_is_mount_point(runtime_path) > 0)
173✔
89
                log_debug("%s is already a mount point", runtime_path);
×
90
        else {
91
                char options[STRLEN("mode=0700,uid=,gid=,size=,nr_inodes=,smackfsroot=*")
173✔
92
                             + DECIMAL_STR_MAX(uid_t)
93
                             + DECIMAL_STR_MAX(gid_t)
94
                             + DECIMAL_STR_MAX(uint64_t)
95
                             + DECIMAL_STR_MAX(uint64_t)];
96

97
                xsprintf(options,
346✔
98
                         "mode=0700,uid=" UID_FMT ",gid=" GID_FMT ",size=%" PRIu64 ",nr_inodes=%" PRIu64 "%s",
99
                         uid, gid, runtime_dir_size, runtime_dir_inodes,
100
                         mac_smack_use() ? ",smackfsroot=*" : "");
101

102
                _cleanup_free_ char *d = strdup(runtime_path);
173✔
103
                if (!d)
173✔
104
                        return log_oom();
×
105

106
                r = mkdir_label(runtime_path, 0700);
173✔
107
                if (r < 0 && r != -EEXIST)
173✔
108
                        return log_error_errno(r, "Failed to create %s: %m", runtime_path);
×
109

110
                _cleanup_(rmdir_and_freep) char *destroy = TAKE_PTR(d); /* auto-destroy */
×
111

112
                r = mount_nofollow_verbose(LOG_DEBUG, "tmpfs", runtime_path, "tmpfs", MS_NODEV|MS_NOSUID, options);
173✔
113
                if (r < 0) {
173✔
114
                        if (!ERRNO_IS_PRIVILEGE(r))
×
115
                                return log_error_errno(r, "Failed to mount per-user tmpfs directory %s: %m", runtime_path);
×
116

117
                        log_debug_errno(r,
×
118
                                        "Failed to mount per-user tmpfs directory %s.\n"
119
                                        "Assuming containerized execution, ignoring: %m", runtime_path);
120

121
                        r = chmod_and_chown(runtime_path, 0700, uid, gid);
×
122
                        if (r < 0)
×
123
                                return log_error_errno(r, "Failed to change ownership and mode of \"%s\": %m", runtime_path);
×
124
                }
125

126
                destroy = mfree(destroy); /* deactivate auto-destroy */
173✔
127

128
                r = label_fix(runtime_path, 0);
173✔
129
                if (r < 0)
173✔
130
                        log_warning_errno(r, "Failed to fix label of \"%s\", ignoring: %m", runtime_path);
173✔
131
        }
132

133
        return 0;
134
}
135

136
static int do_mount(UserRecord *ur) {
173✔
137
        int r;
173✔
138

139
        assert(ur);
173✔
140

141
        if (!uid_is_valid(ur->uid) || !gid_is_valid(user_record_gid(ur)))
173✔
142
                return log_error_errno(SYNTHETIC_ERRNO(ENOMSG), "User '%s' lacks UID or GID, refusing.", ur->user_name);
×
143

144
        uint64_t runtime_dir_size, runtime_dir_inodes;
173✔
145
        r = acquire_runtime_dir_properties(&runtime_dir_size, &runtime_dir_inodes);
173✔
146
        if (r < 0)
173✔
147
                return r;
148

149
        char runtime_path[STRLEN("/run/user/") + DECIMAL_STR_MAX(uid_t)];
173✔
150
        xsprintf(runtime_path, "/run/user/" UID_FMT, ur->uid);
173✔
151

152
        log_debug("Will mount %s owned by "UID_FMT":"GID_FMT, runtime_path, ur->uid, user_record_gid(ur));
173✔
153
        return user_mkdir_runtime_path(runtime_path, ur->uid, user_record_gid(ur), runtime_dir_size, runtime_dir_inodes);
173✔
154
}
155

156
static int user_remove_runtime_path(const char *runtime_path) {
173✔
157
        int r;
173✔
158

159
        assert(runtime_path);
173✔
160
        assert(path_is_absolute(runtime_path));
173✔
161

162
        r = rm_rf(runtime_path, 0);
173✔
163
        if (r < 0)
173✔
164
                log_debug_errno(r, "Failed to remove runtime directory %s (before unmounting), ignoring: %m", runtime_path);
×
165

166
        /* Ignore cases where the directory isn't mounted, as that's quite possible, if we lacked the permissions to
167
         * mount something */
168
        r = RET_NERRNO(umount2(runtime_path, MNT_DETACH));
173✔
169
        if (r < 0 && !IN_SET(r, -EINVAL, -ENOENT))
×
170
                log_debug_errno(r, "Failed to unmount user runtime directory %s, ignoring: %m", runtime_path);
×
171

172
        r = rm_rf(runtime_path, REMOVE_ROOT);
173✔
173
        if (r < 0 && r != -ENOENT)
173✔
174
                return log_error_errno(r, "Failed to remove runtime directory %s (after unmounting): %m", runtime_path);
×
175

176
        return 0;
177
}
178

179
static int do_umount(const char *user) {
173✔
180
        char runtime_path[STRLEN("/run/user/") + DECIMAL_STR_MAX(uid_t)];
173✔
181
        uid_t uid;
173✔
182
        int r;
173✔
183

184
        /* The user may be already removed. So, first try to parse the string by parse_uid(),
185
         * and if it fails, fall back to get_user_creds(). */
186
        if (parse_uid(user, &uid) < 0) {
173✔
187
                r = get_user_creds(&user, &uid, NULL, NULL, NULL, 0);
×
188
                if (r < 0)
×
189
                        return log_error_errno(r,
×
190
                                               r == -ESRCH ? "No such user \"%s\"" :
191
                                               r == -ENOMSG ? "UID \"%s\" is invalid or has an invalid main group"
192
                                                            : "Failed to look up user \"%s\": %m",
193
                                               user);
194
        }
195

196
        xsprintf(runtime_path, "/run/user/" UID_FMT, uid);
173✔
197

198
        log_debug("Will remove %s", runtime_path);
173✔
199
        return user_remove_runtime_path(runtime_path);
173✔
200
}
201

202
static int apply_tmpfs_quota(
224✔
203
                char **paths,
204
                uid_t uid,
205
                uint64_t limit,
206
                uint32_t scale) {
207

208
        _cleanup_set_free_ Set *processed = NULL;
224✔
209
        int r;
224✔
210

211
        assert(uid_is_valid(uid));
224✔
212

213
        STRV_FOREACH(p, paths) {
560✔
214
                _cleanup_close_ int fd = open(*p, O_DIRECTORY|O_CLOEXEC);
672✔
215
                if (fd < 0) {
336✔
216
                        log_warning_errno(errno, "Failed to open '%s' in order to set quota, ignoring: %m", *p);
×
217
                        continue;
×
218
                }
219

220
                struct stat st;
336✔
221
                if (fstat(fd, &st) < 0) {
336✔
222
                        log_warning_errno(errno, "Failed to stat '%s' in order to set quota, ignoring: %m", *p);
×
223
                        continue;
×
224
                }
225

226
                /* Cover for bind mounted or symlinked /var/tmp/ + /tmp/ */
227
                if (set_contains(processed, DEVNUM_TO_PTR(st.st_dev))) {
336✔
228
                        log_debug("Not setting quota on '%s', since already processed.", *p);
×
229
                        continue;
×
230
                }
231

232
                /* Remember we already dealt with this fs, even if the subsequent operation fails, since
233
                 * there's no point in appyling quota twice, regardless if it succeeds or not. */
234
                if (set_ensure_put(&processed, /* hash_ops= */ NULL, DEVNUM_TO_PTR(st.st_dev)) < 0)
336✔
235
                        return log_oom();
×
236

237
                struct statfs sfs;
336✔
238
                if (fstatfs(fd, &sfs) < 0) {
336✔
239
                        log_warning_errno(errno, "Failed to statfs '%s' in order to set quota, ignoring: %m", *p);
×
240
                        continue;
×
241
                }
242

243
                if (!is_fs_type(&sfs, TMPFS_MAGIC)) {
336✔
244
                        log_debug("Not setting quota on '%s', since not tmpfs.", *p);
112✔
245
                        continue;
112✔
246
                }
247

248
                struct dqblk req;
224✔
249
                r = RET_NERRNO(quotactl_fd(fd, QCMD_FIXED(Q_GETQUOTA, USRQUOTA), uid, &req));
224✔
250
                if (r == -ESRCH)
126✔
251
                        zero(req);
×
252
                else if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) {
224✔
253
                        log_debug_errno(r, "No UID quota support on %s, not setting quota: %m", *p);
×
254
                        continue;
×
255
                } else if (ERRNO_IS_NEG_PRIVILEGE(r)) {
224✔
256
                        log_debug_errno(r, "Lacking privileges to query UID quota on %s, not setting quota: %m", *p);
126✔
257
                        continue;
126✔
258
                } else if (r < 0) {
98✔
259
                        log_warning_errno(r, "Failed to query disk quota on %s for UID " UID_FMT ", ignoring: %m", *p, uid);
×
260
                        continue;
×
261
                }
262

263
                uint64_t v =
196✔
264
                        (scale == 0) ? 0 :
98✔
265
                        (scale == UINT32_MAX) ? UINT64_MAX :
98✔
266
                        (uint64_t) ((double) (sfs.f_blocks * sfs.f_frsize) * scale / UINT32_MAX);
98✔
267

268
                v = MIN(v, limit);
98✔
269
                v /= QIF_DQBLKSIZE;
98✔
270

271
                if (FLAGS_SET(req.dqb_valid, QIF_BLIMITS) && v == req.dqb_bhardlimit) {
98✔
272
                        /* Shortcut things if everything is set up properly already */
273
                        log_debug("Configured quota on '%s' already matches the intended setting, not updating quota.", *p);
2✔
274
                        continue;
2✔
275
                }
276

277
                req.dqb_valid = QIF_BLIMITS;
96✔
278
                req.dqb_bsoftlimit = req.dqb_bhardlimit = v;
96✔
279

280
                r = RET_NERRNO(quotactl_fd(fd, QCMD_FIXED(Q_SETQUOTA, USRQUOTA), uid, &req));
96✔
281
                if (r == -ESRCH) {
×
282
                        log_debug_errno(r, "Not setting UID quota on %s since UID quota is not supported: %m", *p);
×
283
                        continue;
×
284
                } else if (ERRNO_IS_NEG_PRIVILEGE(r)) {
96✔
285
                        log_debug_errno(r, "Lacking privileges to set UID quota on %s, skipping: %m", *p);
×
286
                        continue;
×
287
                } else if (r < 0) {
96✔
288
                        log_warning_errno(r, "Failed to set disk quota limit to '%s' on %s for UID " UID_FMT ", ignoring: %m", FORMAT_BYTES(v), *p, uid);
×
289
                        continue;
×
290
                }
291

292
                log_info("Successfully configured disk quota for UID " UID_FMT " on %s to %s", uid, *p, FORMAT_BYTES(v * QIF_DQBLKSIZE));
96✔
293
        }
294

295
        return 0;
296
}
297

298
static int do_tmpfs_quota(UserRecord *ur) {
173✔
299
        int r;
173✔
300

301
        assert(ur);
173✔
302

303
        if (user_record_is_root(ur)) {
173✔
304
                log_debug("Not applying tmpfs quota to root user.");
61✔
305
                return 0;
61✔
306
        }
307

308
        if (!uid_is_valid(ur->uid))
112✔
309
                return log_error_errno(SYNTHETIC_ERRNO(ENOMSG), "User '%s' lacks UID, refusing.", ur->user_name);
×
310

311
        r = apply_tmpfs_quota(STRV_MAKE("/tmp", "/var/tmp"), ur->uid, ur->tmp_limit.limit, user_record_tmp_limit_scale(ur));
112✔
312
        if (r < 0)
112✔
313
                return r;
314

315
        r = apply_tmpfs_quota(STRV_MAKE("/dev/shm"), ur->uid, ur->dev_shm_limit.limit, user_record_dev_shm_limit_scale(ur));
112✔
316
        if (r < 0)
112✔
317
                return r;
×
318

319
        return 0;
320
}
321

322
static int run(int argc, char *argv[]) {
346✔
323
        int r;
346✔
324

325
        log_setup();
346✔
326

327
        if (argc != 3)
346✔
328
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
346✔
329
                                       "This program takes two arguments.");
330

331
        const char *verb = argv[1], *user = argv[2];
346✔
332

333
        if (!STR_IN_SET(verb, "start", "stop"))
346✔
334
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
335
                                       "First argument must be either \"start\" or \"stop\".");
336

337
        umask(0022);
346✔
338

339
        r = mac_init();
346✔
340
        if (r < 0)
346✔
341
                return r;
342

343
        if (streq(verb, "start")) {
346✔
344
                _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
173✔
345
                r = userdb_by_name(user, /* match= */ NULL, USERDB_PARSE_NUMERIC|USERDB_SUPPRESS_SHADOW, &ur);
173✔
346
                if (r == -ESRCH)
173✔
347
                        return log_error_errno(r, "User '%s' does not exist: %m", user);
×
348
                if (r < 0)
173✔
349
                        return log_error_errno(r, "Failed to resolve user '%s': %m", user);
×
350

351
                /* We do two things here: mount the per-user XDG_RUNTIME_DIR, and set up tmpfs quota on /tmp/
352
                 * and /dev/shm/. */
353

354
                r = 0;
173✔
355
                RET_GATHER(r, do_mount(ur));
173✔
356
                RET_GATHER(r, do_tmpfs_quota(ur));
173✔
357
                return r;
173✔
358
        }
359

360
        if (streq(verb, "stop"))
173✔
361
                return do_umount(user);
173✔
362

363
        assert_not_reached();
×
364
}
365

366
DEFINE_MAIN_FUNCTION(run);
346✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc