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

systemd / systemd / 14255132001

03 Apr 2025 10:15PM UTC coverage: 71.907% (-0.05%) from 71.955%
14255132001

push

github

bluca
mkosi: Fix arch build script version sed expression

Yours truly got rid of the _tag variable in the Arch Linux PKGBUILD
a while ago, so actually adapt the build script to that by changing
the pkgver= variable instead.

297094 of 413162 relevant lines covered (71.91%)

653278.23 hits per line

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

0.0
/src/mountfsd/mountwork.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <sys/mount.h>
4
#include <linux/loop.h>
5

6
#include "sd-daemon.h"
7
#include "sd-varlink.h"
8

9
#include "argv-util.h"
10
#include "bus-polkit.h"
11
#include "chase.h"
12
#include "discover-image.h"
13
#include "dissect-image.h"
14
#include "env-util.h"
15
#include "errno-util.h"
16
#include "fd-util.h"
17
#include "io-util.h"
18
#include "json-util.h"
19
#include "main-func.h"
20
#include "missing_syscall.h"
21
#include "namespace-util.h"
22
#include "nsresource.h"
23
#include "nulstr-util.h"
24
#include "os-util.h"
25
#include "process-util.h"
26
#include "stat-util.h"
27
#include "string-table.h"
28
#include "uid-classification.h"
29
#include "uid-range.h"
30
#include "user-util.h"
31
#include "varlink-io.systemd.MountFileSystem.h"
32
#include "varlink-util.h"
33

34
#define ITERATIONS_MAX 64U
35
#define RUNTIME_MAX_USEC (5 * USEC_PER_MINUTE)
36
#define PRESSURE_SLEEP_TIME_USEC (50 * USEC_PER_MSEC)
37
#define LISTEN_IDLE_USEC (90 * USEC_PER_SEC)
38

39
static const ImagePolicy image_policy_untrusted = {
40
        .n_policies = 2,
41
        .policies = {
42
                { PARTITION_ROOT,     PARTITION_POLICY_SIGNED|PARTITION_POLICY_ABSENT },
43
                { PARTITION_USR,      PARTITION_POLICY_SIGNED|PARTITION_POLICY_ABSENT },
44
        },
45
        .default_flags = PARTITION_POLICY_IGNORE,
46
};
47

48
static int json_dispatch_image_policy(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
×
49
        _cleanup_(image_policy_freep) ImagePolicy *q = NULL;
×
50
        ImagePolicy **p = ASSERT_PTR(userdata);
×
51
        int r;
×
52

53
        assert(p);
×
54

55
        if (sd_json_variant_is_null(variant)) {
×
56
                *p = image_policy_free(*p);
×
57
                return 0;
×
58
        }
59

60
        if (!sd_json_variant_is_string(variant))
×
61
                return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
×
62

63
        r = image_policy_from_string(sd_json_variant_string(variant), &q);
×
64
        if (r < 0)
×
65
                return json_log(variant, flags, r, "JSON field '%s' is not a valid image policy.", strna(name));
×
66

67
        image_policy_free(*p);
×
68
        *p = TAKE_PTR(q);
×
69
        return 0;
×
70
}
71

72
typedef struct MountImageParameters {
73
        unsigned image_fd_idx;
74
        unsigned userns_fd_idx;
75
        int read_only;
76
        int growfs;
77
        char *password;
78
        ImagePolicy *image_policy;
79
} MountImageParameters;
80

81
static void mount_image_parameters_done(MountImageParameters *p) {
×
82
        assert(p);
×
83

84
        p->password = erase_and_free(p->password);
×
85
        p->image_policy = image_policy_free(p->image_policy);
×
86
}
×
87

88
static int validate_image_fd(int fd, MountImageParameters *p) {
×
89
        int r, fl;
×
90

91
        assert(fd >= 0);
×
92
        assert(p);
×
93

94
        r = fd_verify_regular(fd);
×
95
        if (r < 0)
×
96
                return r;
97

98
        fl = fd_verify_safe_flags(fd);
×
99
        if (fl < 0)
×
100
                return log_debug_errno(fl, "Image file descriptor has unsafe flags set: %m");
×
101

102
        switch (fl & O_ACCMODE) {
×
103

104
        case O_RDONLY:
×
105
                p->read_only = true;
×
106
                break;
×
107

108
        case O_RDWR:
109
                break;
110

111
        default:
112
                return -EBADF;
113
        }
114

115
        return 0;
116
}
117

118
static int verify_trusted_image_fd_by_path(int fd) {
×
119
        _cleanup_free_ char *p = NULL;
×
120
        struct stat sta;
×
121
        int r;
×
122

123
        assert(fd >= 0);
×
124

125
        r = secure_getenv_bool("SYSTEMD_MOUNTFSD_TRUSTED_DIRECTORIES");
×
126
        if (r == -ENXIO)  {
×
127
                if (!DEFAULT_MOUNTFSD_TRUSTED_DIRECTORIES) {
×
128
                        log_debug("Trusted directory mechanism disabled at compile time.");
×
129
                        return false;
×
130
                }
131
        } else if (r < 0) {
×
132
                log_debug_errno(r, "Failed to parse $SYSTEMD_MOUNTFSD_TRUSTED_DIRECTORIES environment variable, not trusting any image.");
×
133
                return false;
×
134
        } else if (!r) {
×
135
                log_debug("Trusted directory mechanism disabled via $SYSTEMD_MOUNTFSD_TRUSTED_DIRECTORIES environment variable.");
×
136
                return false;
×
137
        }
138

139
        r = fd_get_path(fd, &p);
×
140
        if (r < 0)
×
141
                return log_debug_errno(r, "Failed to get path of passed image file descriptor: %m");
×
142
        if (fstat(fd, &sta) < 0)
×
143
                return log_debug_errno(errno, "Failed to stat() passed image file descriptor: %m");
×
144

145
        log_debug("Checking if image '%s' is in trusted directories.", p);
×
146

147
        for (ImageClass c = 0; c < _IMAGE_CLASS_MAX; c++)
×
148
                NULSTR_FOREACH(s, image_search_path[c]) {
×
149
                        _cleanup_close_ int dir_fd = -EBADF, inode_fd = -EBADF;
×
150
                        _cleanup_free_ char *q = NULL;
×
151
                        struct stat stb;
×
152
                        const char *e;
×
153

154
                        r = chase(s, NULL, CHASE_SAFE, &q, &dir_fd);
×
155
                        if (r == -ENOENT)
×
156
                                continue;
×
157
                        if (r < 0) {
×
158
                                log_warning_errno(r, "Failed to resolve search path '%s', ignoring: %m", s);
×
159
                                continue;
×
160
                        }
161

162
                        /* Check that the inode refers to a file immediately inside the image directory,
163
                         * i.e. not the image directory itself, and nothing further down the tree */
164
                        e = path_startswith(p, q);
×
165
                        if (isempty(e))
×
166
                                continue;
×
167

168
                        e += strspn(e, "/");
×
169
                        if (!filename_is_valid(e))
×
170
                                continue;
×
171

172
                        r = chaseat(dir_fd, e, CHASE_SAFE, NULL, &inode_fd);
×
173
                        if (r < 0)
×
174
                                return log_error_errno(r, "Couldn't verify that specified image '%s' is in search path '%s': %m", p, s);
×
175

176
                        if (fstat(inode_fd, &stb) < 0)
×
177
                                return log_error_errno(errno, "Failed to stat image file '%s/%s': %m", q, e);
×
178

179
                        if (stat_inode_same(&sta, &stb)) {
×
180
                                log_debug("Image '%s' is *in* trusted directories.", p);
×
181
                                return true; /* Yay */
×
182
                        }
183
                }
184

185
        log_debug("Image '%s' is *not* in trusted directories.", p);
×
186
        return false;
187
}
188

189
static int determine_image_policy(
×
190
                int image_fd,
191
                bool trusted,
192
                ImagePolicy *client_policy,
193
                ImagePolicy **ret) {
194

195
        _cleanup_(image_policy_freep) ImagePolicy *envvar_policy = NULL;
×
196
        const ImagePolicy *default_policy;
×
197
        const char *envvar, *e;
×
198
        int r;
×
199

200
        assert(image_fd >= 0);
×
201
        assert(ret);
×
202

203
        if (trusted) {
×
204
                envvar = "SYSTEMD_MOUNTFSD_IMAGE_POLICY_TRUSTED";
205
                default_policy = &image_policy_allow;
206
        } else {
207
                envvar = "SYSTEMD_MOUNTFSD_IMAGE_POLICY_UNTRUSTED";
×
208
                default_policy = &image_policy_untrusted;
×
209
        }
210

211
        e = secure_getenv(envvar);
×
212
        if (e) {
×
213
                r = image_policy_from_string(e, &envvar_policy);
×
214
                if (r < 0)
×
215
                        return log_error_errno(r, "Failed to parse image policy supplied via $%s: %m", envvar);
×
216

217
                default_policy = envvar_policy;
×
218
        }
219

220
        return image_policy_intersect(default_policy, client_policy, ret);
×
221
}
222

223
static int validate_userns(sd_varlink *link, int *userns_fd) {
×
224
        int r;
×
225

226
        assert(link);
×
227
        assert(userns_fd);
×
228

229
        if (*userns_fd < 0)
×
230
                return 0;
231

232
        r = fd_verify_safe_flags(*userns_fd);
×
233
        if (r < 0)
×
234
                return log_debug_errno(r, "User namespace file descriptor has unsafe flags set: %m");
×
235

236
        r = fd_is_namespace(*userns_fd, NAMESPACE_USER);
×
237
        if (r < 0)
×
238
                return r;
239
        if (r == 0)
×
240
                return sd_varlink_error_invalid_parameter_name(link, "userNamespaceFileDescriptor");
×
241

242
        /* Our own host user namespace? Then close the fd, and handle it as if none was specified. */
243
        r = is_our_namespace(*userns_fd, NAMESPACE_USER);
×
244
        if (r < 0)
×
245
                return log_debug_errno(r, "Failed to determine if user namespace provided by client is our own.");
×
246
        if (r > 0) {
×
247
                log_debug("User namespace provided by client is our own.");
×
248
                *userns_fd = safe_close(*userns_fd);
×
249
        }
250

251
        return 0;
252
}
253

254
static int vl_method_mount_image(
×
255
                sd_varlink *link,
256
                sd_json_variant *parameters,
257
                sd_varlink_method_flags_t flags,
258
                void *userdata) {
259

260
        static const sd_json_dispatch_field dispatch_table[] = {
×
261
                { "imageFileDescriptor",         SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint,      offsetof(MountImageParameters, image_fd_idx),  SD_JSON_MANDATORY },
262
                { "userNamespaceFileDescriptor", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint,      offsetof(MountImageParameters, userns_fd_idx), 0 },
263
                { "readOnly",                    SD_JSON_VARIANT_BOOLEAN,  sd_json_dispatch_tristate,  offsetof(MountImageParameters, read_only),     0 },
264
                { "growFileSystems",             SD_JSON_VARIANT_BOOLEAN,  sd_json_dispatch_tristate,  offsetof(MountImageParameters, growfs),        0 },
265
                { "password",                    SD_JSON_VARIANT_STRING,   sd_json_dispatch_string,    offsetof(MountImageParameters, password),      0 },
266
                { "imagePolicy",                 SD_JSON_VARIANT_STRING,   json_dispatch_image_policy, offsetof(MountImageParameters, image_policy),  0 },
267
                VARLINK_DISPATCH_POLKIT_FIELD,
268
                {}
269
        };
270

271
        _cleanup_(verity_settings_done) VeritySettings verity = VERITY_SETTINGS_DEFAULT;
×
272
        _cleanup_(mount_image_parameters_done) MountImageParameters p = {
×
273
                .image_fd_idx = UINT_MAX,
274
                .userns_fd_idx = UINT_MAX,
275
                .read_only = -1,
276
                .growfs = -1,
277
        };
278
        _cleanup_(dissected_image_unrefp) DissectedImage *di = NULL;
×
279
        _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
×
280
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *aj = NULL;
×
281
        _cleanup_close_ int image_fd = -EBADF, userns_fd = -EBADF;
×
282
        _cleanup_(image_policy_freep) ImagePolicy *use_policy = NULL;
×
283
        Hashmap **polkit_registry = ASSERT_PTR(userdata);
×
284
        _cleanup_free_ char *ps = NULL;
×
285
        bool image_is_trusted = false;
×
286
        int r;
×
287

288
        assert(link);
×
289
        assert(parameters);
×
290

291
        sd_json_variant_sensitive(parameters); /* might contain passwords */
×
292

293
        r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
×
294
        if (r != 0)
×
295
                return r;
296

297
        if (p.image_fd_idx != UINT_MAX) {
×
298
                image_fd = sd_varlink_peek_dup_fd(link, p.image_fd_idx);
×
299
                if (image_fd < 0)
×
300
                        return log_debug_errno(image_fd, "Failed to peek image fd from client: %m");
×
301
        }
302

303
        if (p.userns_fd_idx != UINT_MAX) {
×
304
                userns_fd = sd_varlink_peek_dup_fd(link, p.userns_fd_idx);
×
305
                if (userns_fd < 0)
×
306
                        return log_debug_errno(userns_fd, "Failed to peek user namespace fd from client: %m");
×
307
        }
308

309
        r = validate_image_fd(image_fd, &p);
×
310
        if (r < 0)
×
311
                return r;
312

313
        r = validate_userns(link, &userns_fd);
×
314
        if (r != 0)
×
315
                return r;
316

317
        r = verify_trusted_image_fd_by_path(image_fd);
×
318
        if (r < 0)
×
319
                return r;
320
        image_is_trusted = r;
×
321

322
        const char *polkit_details[] = {
×
323
                "read_only", one_zero(p.read_only > 0),
×
324
                NULL,
325
        };
326

327
        const char *polkit_action, *polkit_untrusted_action;
×
328
        PolkitFlags polkit_flags;
×
329
        if (userns_fd < 0) {
×
330
                /* Mount into the host user namespace */
331
                polkit_action = "io.systemd.mount-file-system.mount-image";
332
                polkit_untrusted_action = "io.systemd.mount-file-system.mount-untrusted-image";
333
                polkit_flags = 0;
334
        } else {
335
                /* Mount into a private user namespace */
336
                polkit_action = "io.systemd.mount-file-system.mount-image-privately";
×
337
                polkit_untrusted_action = "io.systemd.mount-file-system.mount-untrusted-image-privately";
×
338

339
                /* If polkit is not around, let's allow mounting authenticated images by default */
340
                polkit_flags = POLKIT_DEFAULT_ALLOW;
×
341
        }
342

343
        /* Let's definitely acquire the regular action privilege, for mounting properly signed images */
344
        r = varlink_verify_polkit_async_full(
×
345
                        link,
346
                        /* bus= */ NULL,
347
                        polkit_action,
348
                        polkit_details,
349
                        /* good_user= */ UID_INVALID,
350
                        polkit_flags,
351
                        polkit_registry);
352
        if (r <= 0)
×
353
                return r;
354

355
        /* Generate the common dissection directory here. We are not going to use it, but the clients might,
356
         * and they likely are unprivileged, hence cannot create it themselves. Hence let's just create it
357
         * here, if it is missing. */
358
        r = get_common_dissect_directory(NULL);
×
359
        if (r < 0)
×
360
                return r;
361

362
        r = loop_device_make(
×
363
                        image_fd,
364
                        p.read_only == 0 ? O_RDONLY : O_RDWR,
×
365
                        0,
366
                        UINT64_MAX,
367
                        UINT32_MAX,
368
                        LO_FLAGS_PARTSCAN,
369
                        LOCK_EX,
370
                        &loop);
371
        if (r < 0)
×
372
                return r;
373

374
        DissectImageFlags dissect_flags =
×
375
                (p.read_only == 0 ? DISSECT_IMAGE_READ_ONLY : 0) |
×
376
                (p.growfs != 0 ? DISSECT_IMAGE_GROWFS : 0) |
×
377
                DISSECT_IMAGE_DISCARD_ANY |
378
                DISSECT_IMAGE_FSCK |
379
                DISSECT_IMAGE_ADD_PARTITION_DEVICES |
380
                DISSECT_IMAGE_PIN_PARTITION_DEVICES |
×
381
                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY;
382

383
        /* Let's see if we have acquired the privilege to mount untrusted images already */
384
        bool polkit_have_untrusted_action =
×
385
                varlink_has_polkit_action(link, polkit_untrusted_action, polkit_details, polkit_registry);
×
386

387
        for (;;) {
×
388
                use_policy = image_policy_free(use_policy);
×
389
                ps = mfree(ps);
×
390

391
                /* We use the image policy for trusted images if either the path is below a trusted
392
                 * directory, or if we have already acquired a PK authentication that tells us that untrusted
393
                 * images are OK */
394
                bool use_trusted_policy =
×
395
                        image_is_trusted ||
396
                        polkit_have_untrusted_action;
397

398
                r = determine_image_policy(
×
399
                                image_fd,
400
                                use_trusted_policy,
401
                                p.image_policy,
402
                                &use_policy);
403
                if (r < 0)
×
404
                        return r;
405

406
                r = image_policy_to_string(use_policy, /* simplify= */ true, &ps);
×
407
                if (r < 0)
×
408
                        return r;
409

410
                log_debug("Using image policy: %s", ps);
×
411

412
                r = dissect_loop_device(
×
413
                                loop,
414
                                &verity,
415
                                /* mount_options= */ NULL,
416
                                use_policy,
417
                                /* image_filter= */ NULL,
418
                                dissect_flags,
419
                                &di);
420
                if (r == -ENOPKG)
×
421
                        return sd_varlink_error(link, "io.systemd.MountFileSystem.IncompatibleImage", NULL);
×
422
                if (r == -ENOTUNIQ)
×
423
                        return sd_varlink_error(link, "io.systemd.MountFileSystem.MultipleRootPartitionsFound", NULL);
×
424
                if (r == -ENXIO)
×
425
                        return sd_varlink_error(link, "io.systemd.MountFileSystem.RootPartitionNotFound", NULL);
×
426
                if (r == -ERFKILL) {
×
427
                        /* The image policy refused this, let's retry after trying to get PolicyKit */
428

429
                        if (!polkit_have_untrusted_action) {
×
430
                                log_debug("Denied by image policy. Trying a stronger polkit authentication before continuing.");
×
431
                                r = varlink_verify_polkit_async_full(
×
432
                                                link,
433
                                                /* bus= */ NULL,
434
                                                polkit_untrusted_action,
435
                                                polkit_details,
436
                                                /* good_user= */ UID_INVALID,
437
                                                /* flags= */ 0,                   /* NB: the image cannot be authenticated, hence unless PK is around to allow this anyway, fail! */
438
                                                polkit_registry);
439
                                if (r <= 0 && !ERRNO_IS_NEG_PRIVILEGE(r))
×
440
                                        return r;
441
                                if (r > 0) {
×
442
                                        /* Try again, now that we know the client has enough privileges. */
443
                                        log_debug("Denied by image policy, retrying after polkit authentication.");
×
444
                                        polkit_have_untrusted_action = true;
×
445
                                        continue;
×
446
                                }
447
                        }
448

449
                        return sd_varlink_error(link, "io.systemd.MountFileSystem.DeniedByImagePolicy", NULL);
×
450
                }
451
                if (r < 0)
×
452
                        return r;
453

454
                /* Success */
455
                break;
×
456
        }
457

458
        r = dissected_image_load_verity_sig_partition(
×
459
                        di,
460
                        loop->fd,
×
461
                        &verity);
462
        if (r < 0)
×
463
                return r;
464

465
        r = dissected_image_guess_verity_roothash(
×
466
                        di,
467
                        &verity);
468
        if (r < 0)
×
469
                return r;
470

471
        r = dissected_image_decrypt(
×
472
                        di,
473
                        p.password,
×
474
                        &verity,
475
                        dissect_flags);
476
        if (r == -ENOKEY) /* new dm-verity userspace returns ENOKEY if the dm-verity signature key is not in
×
477
                           * key chain. That's great. */
478
                return sd_varlink_error(link, "io.systemd.MountFileSystem.KeyNotFound", NULL);
×
479
        if (r == -EBUSY) /* DM kernel subsystem is shit with returning useful errors hence we keep retrying
×
480
                          * under the assumption that some errors are transitional. Which the errors might
481
                          * not actually be. After all retries failed we return EBUSY. Let's turn that into a
482
                          * generic Verity error. It's not very helpful, could mean anything, but at least it
483
                          * gives client a clear idea that this has to do with Verity. */
484
                return sd_varlink_error(link, "io.systemd.MountFileSystem.VerityFailure", NULL);
×
485
        if (r < 0)
×
486
                return r;
487

488
        r = dissected_image_mount(
×
489
                        di,
490
                        /* where= */ NULL,
491
                        /* uid_shift= */ UID_INVALID,
492
                        /* uid_range= */ UID_INVALID,
493
                        userns_fd,
494
                        dissect_flags);
495
        if (r < 0)
×
496
                return r;
497

498
        for (PartitionDesignator d = 0; d < _PARTITION_DESIGNATOR_MAX; d++) {
×
499
                DissectedPartition *pp = di->partitions + d;
×
500
                int fd_idx;
×
501

502
                if (!pp->found)
×
503
                        continue;
×
504

505
                if (pp->fsmount_fd < 0)
×
506
                        continue;
×
507

508
                if (userns_fd >= 0) {
×
509
                        r = nsresource_add_mount(userns_fd, pp->fsmount_fd);
×
510
                        if (r < 0)
×
511
                                return r;
×
512
                }
513

514
                fd_idx = sd_varlink_push_fd(link, pp->fsmount_fd);
×
515
                if (fd_idx < 0)
×
516
                        return fd_idx;
517

518
                TAKE_FD(pp->fsmount_fd);
×
519

520
                const char *m = partition_mountpoint_to_string(d);
×
521
                _cleanup_strv_free_ char **l = NULL;
×
522
                if (!isempty(m)) {
×
523
                        l = strv_split_nulstr(m);
×
524
                        if (!l)
×
525
                                return log_oom_debug();
×
526
                }
527

528
                r = sd_json_variant_append_arraybo(
×
529
                                &aj,
530
                                SD_JSON_BUILD_PAIR("designator", SD_JSON_BUILD_STRING(partition_designator_to_string(d))),
531
                                SD_JSON_BUILD_PAIR("writable", SD_JSON_BUILD_BOOLEAN(pp->rw)),
532
                                SD_JSON_BUILD_PAIR("growFileSystem", SD_JSON_BUILD_BOOLEAN(pp->growfs)),
533
                                SD_JSON_BUILD_PAIR_CONDITION(pp->partno > 0, "partitionNumber", SD_JSON_BUILD_INTEGER(pp->partno)),
534
                                SD_JSON_BUILD_PAIR_CONDITION(pp->architecture > 0, "architecture", SD_JSON_BUILD_STRING(architecture_to_string(pp->architecture))),
535
                                SD_JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(pp->uuid), "partitionUuid", SD_JSON_BUILD_UUID(pp->uuid)),
536
                                SD_JSON_BUILD_PAIR("fileSystemType", SD_JSON_BUILD_STRING(dissected_partition_fstype(pp))),
537
                                SD_JSON_BUILD_PAIR_CONDITION(!!pp->label, "partitionLabel", SD_JSON_BUILD_STRING(pp->label)),
538
                                SD_JSON_BUILD_PAIR("size", SD_JSON_BUILD_INTEGER(pp->size)),
539
                                SD_JSON_BUILD_PAIR("offset", SD_JSON_BUILD_INTEGER(pp->offset)),
540
                                SD_JSON_BUILD_PAIR("mountFileDescriptor", SD_JSON_BUILD_INTEGER(fd_idx)),
541
                                JSON_BUILD_PAIR_STRV_NON_EMPTY("mountPoint", l));
542
                if (r < 0)
×
543
                        return r;
544
        }
545

546
        loop_device_relinquish(loop);
×
547

548
        return sd_varlink_replybo(
×
549
                        link,
550
                        SD_JSON_BUILD_PAIR("partitions", SD_JSON_BUILD_VARIANT(aj)),
551
                        SD_JSON_BUILD_PAIR("imagePolicy", SD_JSON_BUILD_STRING(ps)),
552
                        SD_JSON_BUILD_PAIR("imageSize", SD_JSON_BUILD_INTEGER(di->image_size)),
553
                        SD_JSON_BUILD_PAIR("sectorSize", SD_JSON_BUILD_INTEGER(di->sector_size)),
554
                        SD_JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(di->image_uuid), "imageUuid", SD_JSON_BUILD_UUID(di->image_uuid)));
555
}
556

557
typedef enum MountMapMode {
558
        MOUNT_MAP_AUTO = 0,     /* determine automatically from image and caller */
559
        MOUNT_MAP_ROOT,         /* map caller's UID to root in namespace (map 1 UID only) */
560
        MOUNT_MAP_FOREIGN,      /* map foreign UID range to base in namespace (map 64K) */
561
        MOUNT_MAP_IDENTITY,     /* apply identity mapping (map 64K) */
562
        _MOUNT_MAP_MODE_MAX,
563
        _MOUNT_MAP_MODE_INVALID = -EINVAL,
564
} MountMapMode;
565

566
static const char *const mount_map_mode_table[_MOUNT_MAP_MODE_MAX] = {
567
        [MOUNT_MAP_AUTO]     = "auto",
568
        [MOUNT_MAP_ROOT]     = "root",
569
        [MOUNT_MAP_FOREIGN]  = "foreign",
570
        [MOUNT_MAP_IDENTITY] = "identity",
571
};
572

573
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(mount_map_mode, MountMapMode);
×
574

575
typedef struct MountDirectoryParameters {
576
        MountMapMode mode;
577
        unsigned directory_fd_idx;
578
        unsigned userns_fd_idx;
579
        int read_only;
580
} MountDirectoryParameters;
581

582
typedef enum DirectoryOwnership {
583
        DIRECTORY_IS_ROOT_PEER_OWNED,  /* This is returned if the directory is owned by the root user and the peer is root */
584
        DIRECTORY_IS_ROOT_OWNED,       /* This is returned if the directory is owned by the root user (and the peer user is not root) */
585
        DIRECTORY_IS_PEER_OWNED,       /* This is returned if the directory is owned by the peer user (who is not root) */
586
        DIRECTORY_IS_FOREIGN_OWNED,    /* This is returned if the directory is owned by the foreign UID range */
587
        DIRECTORY_IS_OTHERWISE_OWNED,  /* This is returned if the directory is owned by something else */
588
        _DIRECTORY_OWNERSHIP_MAX,
589
        _DIRECTORY_OWNERSHIP_ERRNO_MAX = -ERRNO_MAX, /* Guarantee the whole negative errno range fits */
590
} DirectoryOwnership;
591

592
static MountMapMode default_mount_map_mode(DirectoryOwnership ownership) {
×
593
        /* Derives a suitable mapping mode from the ownership of the base tree */
594

595
        switch (ownership) {
×
596
        case DIRECTORY_IS_PEER_OWNED:
597
                return MOUNT_MAP_ROOT;     /* Map the peer's UID to root in the container */
598

599
        case DIRECTORY_IS_FOREIGN_OWNED:
×
600
                return MOUNT_MAP_FOREIGN;  /* Map the foreign UID range to the container's UID range */
×
601

602
        case DIRECTORY_IS_ROOT_PEER_OWNED:
×
603
        case DIRECTORY_IS_ROOT_OWNED:
604
        case DIRECTORY_IS_OTHERWISE_OWNED:
605
                return MOUNT_MAP_IDENTITY; /* Don't map */
×
606

607
        default:
×
608
                return _MOUNT_MAP_MODE_INVALID;
×
609
        }
610
}
611

612
static JSON_DISPATCH_ENUM_DEFINE(dispatch_mount_directory_mode, MountMapMode, mount_map_mode_from_string);
×
613

614
static DirectoryOwnership validate_directory_fd(int fd, uid_t peer_uid) {
×
615
        int r, fl;
×
616

617
        assert(fd >= 0);
×
618

619
        /* Checks if the specified directory fd looks sane. Returns a DirectoryOwnership that categorizes the
620
         * ownership situation in comparison to the peer's UID.
621
         *
622
         * Note one key difference to image validation (as implemented above): for regular files if the
623
         * client provided us with an open fd it implies the client has access, as well as what kind of
624
         * access (i.e. ro or rw). But for directories this doesn't work the same way, as directories are
625
         * always opened read-only only. Hence we use a different mechanism to validate access to them: we
626
         * check if the directory is owned by the peer UID or by the foreign UID range (in the latter case
627
         * one of the parent directories must be owned by the peer though). */
628

629
        struct stat st;
×
630
        if (fstat(fd, &st) < 0)
×
631
                return log_debug_errno(errno, "Failed to stat() directory fd: %m");
×
632

633
        r = stat_verify_directory(&st);
×
634
        if (r < 0)
×
635
                return r;
636

637
        fl = fd_verify_safe_flags_full(fd, O_DIRECTORY);
×
638
        if (fl < 0)
×
639
                return log_debug_errno(fl, "Directory file descriptor has unsafe flags set: %m");
×
640

641
        if (st.st_uid == 0) {
×
642
                if (peer_uid == 0) {
×
643
                        log_debug("Directory file descriptor points to root owned directory, who is also the peer.");
×
644
                        return DIRECTORY_IS_ROOT_PEER_OWNED;
×
645
                }
646
                log_debug("Directory file descriptor points to root owned directory.");
×
647
                return DIRECTORY_IS_ROOT_OWNED;
×
648
        }
649
        if (st.st_uid == peer_uid) {
×
650
                log_debug("Directory file descriptor points to peer owned directory.");
×
651
                return DIRECTORY_IS_PEER_OWNED;
×
652
        }
653

654
        /* For bind mounted directories we check if they are either owned by the client's UID, or by the
655
         * foreign UID set, but in that case the parent directory must be owned by the client's UID, or some
656
         * directory iteratively up the chain */
657

658
        _cleanup_close_ int parent_fd = -EBADF;
×
659
        unsigned n_level;
660
        for (n_level = 0; n_level < 16; n_level++) {
×
661
                /* Stop iteration if we find a directory up the tree that is neither owned by the user, nor is from the foreign UID range */
662
                if (!uid_is_foreign(st.st_uid) || !gid_is_foreign(st.st_gid)) {
×
663
                        log_debug("Directory file descriptor points to directory which itself or its parents is neither owned by foreign UID range nor by the user.");
×
664
                        return DIRECTORY_IS_OTHERWISE_OWNED;
×
665
                }
666

667
                /* If the peer is root, then it doesn't matter if we find a parent owned by root, let's shortcut things. */
668
                if (peer_uid == 0) {
×
669
                        log_debug("Directory file descriptor is owned by foreign UID range, and peer is root.");
×
670
                        return DIRECTORY_IS_FOREIGN_OWNED;
×
671
                }
672

673
                /* Go one level up */
674
                _cleanup_close_ int new_parent_fd = openat(fd, "..", O_DIRECTORY|O_PATH|O_CLOEXEC);
×
675
                if (new_parent_fd < 0)
×
676
                        return log_debug_errno(errno, "Failed to open parent directory of directory file descriptor: %m");
×
677

678
                struct stat new_st;
×
679
                if (fstat(new_parent_fd, &new_st) < 0)
×
680
                        return log_debug_errno(errno, "Failed to stat parent directory of directory file descriptor: %m");
×
681

682
                /* Safety check to see if we hit the root dir */
683
                if (stat_inode_same(&st, &new_st)) {
×
684
                        log_debug("Directory file descriptor is owned by foreign UID range, but didn't find parent directory that is owned by peer among ancestors.");
×
685
                        return DIRECTORY_IS_OTHERWISE_OWNED;
×
686
                }
687

688
                if (new_st.st_uid == peer_uid) { /* Parent inode is owned by the peer. That's good! Everything's fine. */
×
689
                        log_debug("Directory file descriptor is owned by foreign UID range, and ancestor is owned by peer.");
×
690
                        return DIRECTORY_IS_FOREIGN_OWNED;
×
691
                }
692

693
                close_and_replace(parent_fd, new_parent_fd);
×
694
                st = new_st;
×
695
        }
696

697
        log_debug("Failed to find peer owned parent directory after %u levels, refusing.", n_level);
×
698
        return DIRECTORY_IS_OTHERWISE_OWNED;
699
}
700

701
static int vl_method_mount_directory(
×
702
                sd_varlink *link,
703
                sd_json_variant *parameters,
704
                sd_varlink_method_flags_t flags,
705
                void *userdata) {
706

707
        static const sd_json_dispatch_field dispatch_table[] = {
×
708
                { "mode",                        SD_JSON_VARIANT_STRING,   dispatch_mount_directory_mode, offsetof(MountDirectoryParameters, mode),             0                 },
709
                { "directoryFileDescriptor",     SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint,         offsetof(MountDirectoryParameters, directory_fd_idx), SD_JSON_MANDATORY },
710
                { "userNamespaceFileDescriptor", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint,         offsetof(MountDirectoryParameters, userns_fd_idx),    0                 },
711
                { "readOnly",                    SD_JSON_VARIANT_BOOLEAN,  sd_json_dispatch_tristate,     offsetof(MountDirectoryParameters, read_only),        0                 },
712
                VARLINK_DISPATCH_POLKIT_FIELD,
713
                {}
714
        };
715

716
        MountDirectoryParameters p = {
×
717
                .mode = MOUNT_MAP_AUTO,
718
                .directory_fd_idx = UINT_MAX,
719
                .userns_fd_idx = UINT_MAX,
720
                .read_only = -1,
721
        };
722
        _cleanup_close_ int directory_fd = -EBADF, userns_fd = -EBADF;
×
723
        Hashmap **polkit_registry = ASSERT_PTR(userdata);
×
724
        int r;
×
725

726
        r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
×
727
        if (r != 0)
×
728
                return r;
729

730
        if (p.directory_fd_idx == UINT_MAX)
×
731
                return sd_varlink_error_invalid_parameter_name(link, "directoryFileDescriptor");
×
732

733
        directory_fd = sd_varlink_peek_dup_fd(link, p.directory_fd_idx);
×
734
        if (directory_fd < 0)
×
735
                return log_debug_errno(directory_fd, "Failed to peek directory fd from client: %m");
×
736

737
        if (p.userns_fd_idx != UINT_MAX) {
×
738
                userns_fd = sd_varlink_peek_dup_fd(link, p.userns_fd_idx);
×
739
                if (userns_fd < 0)
×
740
                        return log_debug_errno(userns_fd, "Failed to peek user namespace fd from client: %m");
×
741
        }
742

743
        uid_t peer_uid;
×
744
        r = sd_varlink_get_peer_uid(link, &peer_uid);
×
745
        if (r < 0)
×
746
                return log_debug_errno(r, "Failed to get client UID: %m");
×
747

748
        DirectoryOwnership owned_by = validate_directory_fd(directory_fd, peer_uid);
×
749
        if (owned_by < 0)
×
750
                return owned_by;
751

752
        r = validate_userns(link, &userns_fd);
×
753
        if (r != 0)
×
754
                return r;
755

756
        /* If no mode is specified, pick sensible default */
757
        if (p.mode <= 0) {
×
758
                p.mode = default_mount_map_mode(owned_by);
×
759
                assert(p.mode > 0);
×
760
        }
761

762
        _cleanup_free_ char *directory_path = NULL;
×
763
        (void) fd_get_path(directory_fd, &directory_path);
×
764

765
        log_debug("Mounting '%s' with mapping mode: %s", strna(directory_path), mount_map_mode_to_string(p.mode));
×
766

767
        const char *polkit_details[] = {
×
768
                "read_only", one_zero(p.read_only > 0),
×
769
                "directory", strna(directory_path),
×
770
                NULL,
771
        };
772

773
        const char *polkit_action, *polkit_untrusted_action;
×
774
        PolkitFlags polkit_flags;
×
775
        if (userns_fd < 0) {
×
776
                /* Mount into the host user namespace */
777
                polkit_action = "io.systemd.mount-file-system.mount-directory";
778
                polkit_untrusted_action = "io.systemd.mount-file-system.mount-untrusted-directory";
779
                polkit_flags = 0;
780
        } else {
781
                /* Mount into a private user namespace */
782
                polkit_action = "io.systemd.mount-file-system.mount-directory-privately";
×
783
                polkit_untrusted_action = "io.systemd.mount-file-system.mount-untrusted-directory-privately";
×
784

785
                /* If polkit is not around, let's allow mounting authenticated images by default */
786
                polkit_flags = POLKIT_DEFAULT_ALLOW;
×
787
        }
788

789
        /* We consider a directory "trusted" if it is owned by the peer or the foreign UID range */
790
        bool trusted_directory = IN_SET(owned_by, DIRECTORY_IS_ROOT_PEER_OWNED, DIRECTORY_IS_PEER_OWNED, DIRECTORY_IS_FOREIGN_OWNED);
×
791

792
        /* Let's definitely acquire the regular action privilege, for mounting properly signed images */
793
        r = varlink_verify_polkit_async_full(
×
794
                        link,
795
                        /* bus= */ NULL,
796
                        trusted_directory ? polkit_action : polkit_untrusted_action,
797
                        polkit_details,
798
                        /* good_user= */ UID_INVALID,
799
                        trusted_directory ? polkit_flags : 0,
800
                        polkit_registry);
801
        if (r <= 0)
×
802
                return r;
803

804
        /* Generate the common dissection directory here. We are not going to use it, but the clients might,
805
         * and they likely are unprivileged, hence cannot create it themselves. Hence let's just create it
806
         * here, if it is missing. */
807
        r = get_common_dissect_directory(NULL);
×
808
        if (r < 0)
×
809
                return r;
810

811
        _cleanup_close_ int mount_fd = open_tree(directory_fd, "", OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC|AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH);
×
812
        if (mount_fd < 0)
×
813
                return log_debug_errno(errno, "Failed to issue open_tree() of provided directory '%s': %m", strna(directory_path));
×
814

815
        if (p.read_only > 0 && mount_setattr(
×
816
                            mount_fd, "", AT_EMPTY_PATH,
817
                            &(struct mount_attr) {
×
818
                                    .attr_set = MOUNT_ATTR_RDONLY,
819
                            }, MOUNT_ATTR_SIZE_VER0) < 0)
820
                return log_debug_errno(errno, "Failed to enable read-only mode: %m");
×
821

822
        if (p.mode != MOUNT_MAP_IDENTITY) {
×
823
                uid_t start;
×
824

825
                if (userns_fd >= 0) {
×
826
                        _cleanup_(uid_range_freep) UIDRange *uid_range_outside = NULL, *uid_range_inside = NULL, *gid_range_outside = NULL, *gid_range_inside = NULL;
×
827
                        r = uid_range_load_userns_by_fd(userns_fd, UID_RANGE_USERNS_OUTSIDE, &uid_range_outside);
×
828
                        if (r < 0)
×
829
                                return log_debug_errno(r, "Failed to load outside UID range of provided userns: %m");
×
830
                        r = uid_range_load_userns_by_fd(userns_fd, UID_RANGE_USERNS_INSIDE, &uid_range_inside);
×
831
                        if (r < 0)
×
832
                                return log_debug_errno(r, "Failed to load inside UID range of provided userns: %m");
×
833
                        r = uid_range_load_userns_by_fd(userns_fd, GID_RANGE_USERNS_OUTSIDE, &gid_range_outside);
×
834
                        if (r < 0)
×
835
                                return log_debug_errno(r, "Failed to load outside GID range of provided userns: %m");
×
836
                        r = uid_range_load_userns_by_fd(userns_fd, GID_RANGE_USERNS_INSIDE, &gid_range_inside);
×
837
                        if (r < 0)
×
838
                                return log_debug_errno(r, "Failed to load inside GID range of provided userns: %m");
×
839

840
                        /* Be very strict for now */
841
                        if (!uid_range_equal(uid_range_outside, gid_range_outside) ||
×
842
                            !uid_range_equal(uid_range_inside, gid_range_inside) ||
×
843
                            uid_range_outside->n_entries != 1 ||
×
844
                            uid_range_outside->entries[0].nr != 0x10000 ||
×
845
                            uid_range_inside->n_entries != 1 ||
×
846
                            uid_range_inside->entries[0].start != 0 ||
×
847
                            uid_range_inside->entries[0].nr != 0x10000)
×
848
                                return sd_varlink_error_invalid_parameter_name(link, "userNamespaceFileDescriptor");
×
849

850
                        start = uid_range_outside->entries[0].start;
×
851
                } else
852
                        start = 0;
853

854
                _cleanup_free_ char *new_uid_map = NULL;
×
855
                switch (p.mode) {
×
856
                case MOUNT_MAP_ROOT:
×
857
                        r = strextendf(&new_uid_map, UID_FMT " " UID_FMT " " UID_FMT,
×
858
                                       peer_uid, start, (uid_t) 1);
859
                        break;
860
                case MOUNT_MAP_FOREIGN:
×
861
                        r = strextendf(&new_uid_map, UID_FMT " " UID_FMT " " UID_FMT,
×
862
                                       (uid_t) FOREIGN_UID_MIN, start, (uid_t) 0x10000);
863
                        break;
864
                default:
×
865
                        assert_not_reached();
×
866
                }
867
                if (r < 0)
×
868
                        return r;
869

870
                _cleanup_close_ int idmap_userns_fd = userns_acquire(new_uid_map, new_uid_map, /* setgroups_deny= */ true);
×
871
                if (idmap_userns_fd < 0)
×
872
                        return log_debug_errno(idmap_userns_fd, "Failed to acquire user namespace for id mapping: %m");
×
873

874
                if (mount_setattr(mount_fd, "", AT_EMPTY_PATH,
×
875
                                  &(struct mount_attr) {
×
876
                                          .attr_set = MOUNT_ATTR_IDMAP,
877
                                          .userns_fd = idmap_userns_fd,
878
                                          .propagation = MS_PRIVATE,
879
                                  }, MOUNT_ATTR_SIZE_VER0) < 0)
880
                        return log_debug_errno(errno, "Failed to enable id mapping: %m");
×
881
        }
882

883
        if (userns_fd >= 0) {
×
884
                r = nsresource_add_mount(userns_fd, mount_fd);
×
885
                if (r < 0)
×
886
                        return r;
887
        }
888

889
        int fd_idx = sd_varlink_push_fd(link, mount_fd);
×
890
        if (fd_idx < 0)
×
891
                return fd_idx;
892

893
        TAKE_FD(mount_fd);
×
894

895
        return sd_varlink_replybo(
×
896
                        link,
897
                        SD_JSON_BUILD_PAIR("mountFileDescriptor", SD_JSON_BUILD_INTEGER(fd_idx)));
898
}
899

900
static int process_connection(sd_varlink_server *server, int _fd) {
×
901
        _cleanup_close_ int fd = TAKE_FD(_fd); /* always take possession */
×
902
        _cleanup_(sd_varlink_close_unrefp) sd_varlink *vl = NULL;
×
903
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
×
904
        int r;
×
905

906
        r = sd_event_new(&event);
×
907
        if (r < 0)
×
908
                return r;
909

910
        r = sd_varlink_server_attach_event(server, event, 0);
×
911
        if (r < 0)
×
912
                return log_error_errno(r, "Failed to attach Varlink server to event loop: %m");
×
913

914
        r = sd_varlink_server_add_connection(server, fd, &vl);
×
915
        if (r < 0)
×
916
                return log_error_errno(r, "Failed to add connection: %m");
×
917

918
        TAKE_FD(fd);
×
919
        vl = sd_varlink_ref(vl);
×
920

921
        r = sd_event_loop(event);
×
922
        if (r < 0)
×
923
                return log_error_errno(r, "Failed to run event loop: %m");
×
924

925
        r = sd_varlink_server_detach_event(server);
×
926
        if (r < 0)
×
927
                return log_error_errno(r, "Failed to detach Varlink server from event loop: %m");
×
928

929
        return 0;
930
}
931

932
static int run(int argc, char *argv[]) {
×
933
        usec_t start_time, listen_idle_usec, last_busy_usec = USEC_INFINITY;
×
934
        _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *server = NULL;
×
935
        _cleanup_hashmap_free_ Hashmap *polkit_registry = NULL;
×
936
        _cleanup_(pidref_done) PidRef parent = PIDREF_NULL;
×
937
        unsigned n_iterations = 0;
×
938
        int m, listen_fd, r;
×
939

940
        log_setup();
×
941

942
        m = sd_listen_fds(false);
×
943
        if (m < 0)
×
944
                return log_error_errno(m, "Failed to determine number of listening fds: %m");
×
945
        if (m == 0)
×
946
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No socket to listen on received.");
×
947
        if (m > 1)
×
948
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Worker can only listen on a single socket at a time.");
×
949

950
        listen_fd = SD_LISTEN_FDS_START;
×
951

952
        r = fd_nonblock(listen_fd, false);
×
953
        if (r < 0)
×
954
                return log_error_errno(r, "Failed to turn off non-blocking mode for listening socket: %m");
×
955

956
        r = varlink_server_new(&server,
×
957
                               SD_VARLINK_SERVER_INHERIT_USERDATA|
958
                               SD_VARLINK_SERVER_ALLOW_FD_PASSING_INPUT|SD_VARLINK_SERVER_ALLOW_FD_PASSING_OUTPUT,
959
                               &polkit_registry);
960
        if (r < 0)
×
961
                return log_error_errno(r, "Failed to allocate server: %m");
×
962

963
        r = sd_varlink_server_add_interface(server, &vl_interface_io_systemd_MountFileSystem);
×
964
        if (r < 0)
×
965
                return log_error_errno(r, "Failed to add MountFileSystem interface to varlink server: %m");
×
966

967
        r = sd_varlink_server_bind_method_many(
×
968
                        server,
969
                        "io.systemd.MountFileSystem.MountImage",     vl_method_mount_image,
970
                        "io.systemd.MountFileSystem.MountDirectory", vl_method_mount_directory);
971
        if (r < 0)
×
972
                return log_error_errno(r, "Failed to bind methods: %m");
×
973

974
        r = sd_varlink_server_set_exit_on_idle(server, true);
×
975
        if (r < 0)
×
976
                return log_error_errno(r, "Failed to enable exit-on-idle mode: %m");
×
977

978
        r = getenv_bool("MOUNTFS_FIXED_WORKER");
×
979
        if (r < 0)
×
980
                return log_error_errno(r, "Failed to parse MOUNTFSD_FIXED_WORKER: %m");
×
981
        listen_idle_usec = r ? USEC_INFINITY : LISTEN_IDLE_USEC;
×
982

983
        r = pidref_set_parent(&parent);
×
984
        if (r < 0)
×
985
                return log_error_errno(r, "Failed to acquire pidfd of parent process: %m");
×
986

987
        start_time = now(CLOCK_MONOTONIC);
×
988

989
        for (;;) {
×
990
                _cleanup_close_ int fd = -EBADF;
×
991
                usec_t n;
×
992

993
                /* Exit the worker in regular intervals, to flush out all memory use */
994
                if (n_iterations++ > ITERATIONS_MAX) {
×
995
                        log_debug("Exiting worker, processed %u iterations, that's enough.", n_iterations);
×
996
                        break;
997
                }
998

999
                n = now(CLOCK_MONOTONIC);
×
1000
                if (n >= usec_add(start_time, RUNTIME_MAX_USEC)) {
×
1001
                        log_debug("Exiting worker, ran for %s, that's enough.",
×
1002
                                  FORMAT_TIMESPAN(usec_sub_unsigned(n, start_time), 0));
1003
                        break;
×
1004
                }
1005

1006
                if (last_busy_usec == USEC_INFINITY)
×
1007
                        last_busy_usec = n;
1008
                else if (listen_idle_usec != USEC_INFINITY && n >= usec_add(last_busy_usec, listen_idle_usec)) {
×
1009
                        log_debug("Exiting worker, been idle for %s.",
×
1010
                                  FORMAT_TIMESPAN(usec_sub_unsigned(n, last_busy_usec), 0));
1011
                        break;
×
1012
                }
1013

1014
                (void) rename_process("systemd-mountwork: waiting...");
×
1015
                fd = RET_NERRNO(accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC));
×
1016
                (void) rename_process("systemd-mountwork: processing...");
×
1017

1018
                if (fd == -EAGAIN)
×
1019
                        continue; /* The listening socket has SO_RECVTIMEO set, hence a timeout is expected
×
1020
                                   * after a while, let's check if it's time to exit though. */
1021
                if (fd == -EINTR)
×
1022
                        continue; /* Might be that somebody attached via strace, let's just continue in that
×
1023
                                   * case */
1024
                if (fd < 0)
×
1025
                        return log_error_errno(fd, "Failed to accept() from listening socket: %m");
×
1026

1027
                if (now(CLOCK_MONOTONIC) <= usec_add(n, PRESSURE_SLEEP_TIME_USEC)) {
×
1028
                        /* We only slept a very short time? If so, let's see if there are more sockets
1029
                         * pending, and if so, let's ask our parent for more workers */
1030

1031
                        r = fd_wait_for_event(listen_fd, POLLIN, 0);
×
1032
                        if (r < 0)
×
1033
                                return log_error_errno(r, "Failed to test for POLLIN on listening socket: %m");
×
1034

1035
                        if (FLAGS_SET(r, POLLIN)) {
×
1036
                                r = pidref_kill(&parent, SIGUSR2);
×
1037
                                if (r == -ESRCH)
×
1038
                                        return log_error_errno(r, "Parent already died?");
×
1039
                                if (r < 0)
×
1040
                                        return log_error_errno(r, "Failed to send SIGUSR2 signal to parent. %m");
×
1041
                        }
1042
                }
1043

1044
                (void) process_connection(server, TAKE_FD(fd));
×
1045
                last_busy_usec = USEC_INFINITY;
×
1046
        }
1047

1048
        return 0;
1049
}
1050

1051
DEFINE_MAIN_FUNCTION(run);
×
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