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

systemd / systemd / 28066554192

23 Jun 2026 10:04PM UTC coverage: 72.703% (-0.3%) from 72.966%
28066554192

push

github

bluca
test: skip fdstore tests if test-fdstore is not available

When the test suite is run in the "standalone" mode, the minimal
container might not contain the test-fdstore binary that's needed for a
couple of tests. Since installing systemd-tests into the minimal
container pulls in a lot of other dependencies, let's just skip the
affected tests instead to avoid this.

339059 of 466364 relevant lines covered (72.7%)

1294926.64 hits per line

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

87.92
/src/userdb/userwork.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <poll.h>
4

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

8
#include "alloc-util.h"
9
#include "argv-util.h"
10
#include "env-util.h"
11
#include "errno-util.h"
12
#include "fd-util.h"
13
#include "group-record.h"
14
#include "io-util.h"
15
#include "json-util.h"
16
#include "main-func.h"
17
#include "pidref.h"
18
#include "string-util.h"
19
#include "time-util.h"
20
#include "user-record.h"
21
#include "user-util.h"
22
#include "userdb.h"
23
#include "varlink-io.systemd.UserDatabase.h"
24
#include "varlink-util.h"
25

26
#define ITERATIONS_MAX 64U
27
#define RUNTIME_MAX_USEC (5 * USEC_PER_MINUTE)
28
#define PRESSURE_SLEEP_TIME_USEC (50 * USEC_PER_MSEC)
29
#define CONNECTION_IDLE_USEC (15 * USEC_PER_SEC)
30
#define LISTEN_IDLE_USEC (90 * USEC_PER_SEC)
31

32
typedef struct LookupParameters {
33
        const char *name;
34
        union {
35
                uid_t uid;
36
                gid_t gid;
37
        };
38
        const char *service;
39
        UserDBMatch match;
40
} LookupParameters;
41

42
static void lookup_parameters_done(LookupParameters *p) {
29,091✔
43
        assert(p);
29,091✔
44

45
        userdb_match_done(&p->match);
29,091✔
46
}
29,091✔
47

48
static int add_nss_service(sd_json_variant **v) {
8,876✔
49
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *status = NULL, *z = NULL;
17,750✔
50
        sd_id128_t mid;
8,876✔
51
        int r;
8,876✔
52

53
        assert(v);
8,876✔
54

55
        /* Patch in service field if it's missing. The assumption here is that this field is unset only for
56
         * NSS records */
57

58
        if (sd_json_variant_by_key(*v, "service"))
8,876✔
59
                return 0;
60

61
        r = sd_id128_get_machine(&mid);
8,874✔
62
        if (r < 0)
8,874✔
63
                return r;
64

65
        status = sd_json_variant_ref(sd_json_variant_by_key(*v, "status"));
8,874✔
66
        z = sd_json_variant_ref(sd_json_variant_by_key(status, SD_ID128_TO_STRING(mid)));
8,874✔
67

68
        if (sd_json_variant_by_key(z, "service"))
8,874✔
69
                return 0;
70

71
        r = sd_json_variant_set_field_string(&z, "service", "io.systemd.NameServiceSwitch");
8,643✔
72
        if (r < 0)
8,643✔
73
                return r;
74

75
        r = sd_json_variant_set_field(&status, SD_ID128_TO_STRING(mid), z);
8,643✔
76
        if (r < 0)
8,643✔
77
                return r;
78

79
        return sd_json_variant_set_field(v, "status", status);
8,643✔
80
}
81

82
static int build_user_json(sd_varlink *link, UserRecord *ur, sd_json_variant **ret) {
5,694✔
83
        _cleanup_(user_record_unrefp) UserRecord *stripped = NULL;
×
84
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
5,694✔
85
        UserRecordLoadFlags flags;
5,694✔
86
        uid_t peer_uid;
5,694✔
87
        bool trusted;
5,694✔
88
        int r;
5,694✔
89

90
        assert(ur);
5,694✔
91
        assert(ret);
5,694✔
92

93
        r = sd_varlink_get_peer_uid(link, &peer_uid);
5,694✔
94
        if (r < 0) {
5,694✔
95
                log_debug_errno(r, "Unable to query peer UID, ignoring: %m");
×
96
                trusted = false;
97
        } else
98
                trusted = peer_uid == 0 || peer_uid == ur->uid;
5,694✔
99

100
        flags = USER_RECORD_REQUIRE_REGULAR|USER_RECORD_ALLOW_PER_MACHINE|USER_RECORD_ALLOW_BINDING|USER_RECORD_STRIP_SECRET|USER_RECORD_ALLOW_STATUS|USER_RECORD_ALLOW_SIGNATURE|USER_RECORD_PERMISSIVE;
5,850✔
101
        if (trusted)
156✔
102
                flags |= USER_RECORD_ALLOW_PRIVILEGED;
103
        else
104
                flags |= USER_RECORD_STRIP_PRIVILEGED;
105

106
        r = user_record_clone(ur, flags, &stripped);
5,694✔
107
        if (r < 0)
5,694✔
108
                return r;
109

110
        stripped->incomplete =
11,388✔
111
                ur->incomplete ||
5,694✔
112
                (FLAGS_SET(ur->mask, USER_RECORD_PRIVILEGED) &&
5,694✔
113
                 !FLAGS_SET(stripped->mask, USER_RECORD_PRIVILEGED));
5,050✔
114

115
        v = sd_json_variant_ref(stripped->json);
5,694✔
116
        r = add_nss_service(&v);
5,694✔
117
        if (r < 0)
5,694✔
118
                return r;
119

120
        return sd_json_buildo(
5,694✔
121
                        ret,
122
                        SD_JSON_BUILD_PAIR_VARIANT("record", v),
123
                        SD_JSON_BUILD_PAIR_BOOLEAN("incomplete", stripped->incomplete));
124
}
125

126
static int userdb_flags_from_service(sd_varlink *link, const char *service, UserDBFlags *ret) {
44,538✔
127
        assert(link);
44,538✔
128
        assert(ret);
44,538✔
129

130
        if (streq_ptr(service, "io.systemd.NameServiceSwitch"))
44,538✔
131
                *ret = USERDB_NSS_ONLY|USERDB_AVOID_MULTIPLEXER;
56✔
132
        else if (streq_ptr(service, "io.systemd.DropIn"))
44,482✔
133
                *ret = USERDB_DROPIN_ONLY|USERDB_AVOID_MULTIPLEXER;
34,398✔
134
        else if (streq_ptr(service, "io.systemd.Multiplexer"))
10,084✔
135
                *ret = USERDB_AVOID_MULTIPLEXER;
10,084✔
136
        else
137
                return sd_varlink_error(link, "io.systemd.UserDatabase.BadService", NULL);
×
138

139
        return 0;
140
}
141

142
static int vl_method_get_user_record(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
9,818✔
143

144
        static const sd_json_dispatch_field dispatch_table[] = {
9,818✔
145
                { "uid",             _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uid_gid,            offsetof(LookupParameters, uid),                    0             },
146
                { "userName",        SD_JSON_VARIANT_STRING,        json_dispatch_const_user_group_name, offsetof(LookupParameters, name),                   SD_JSON_RELAX },
147
                { "service",         SD_JSON_VARIANT_STRING,        sd_json_dispatch_const_string,       offsetof(LookupParameters, service),                0             },
148
                { "fuzzyNames",      SD_JSON_VARIANT_ARRAY,         sd_json_dispatch_strv,               offsetof(LookupParameters, match.fuzzy_names),      0             },
149
                { "dispositionMask", SD_JSON_VARIANT_ARRAY,         json_dispatch_dispositions_mask,     offsetof(LookupParameters, match.disposition_mask), 0             },
150
                { "uidMin",          _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uid_gid,            offsetof(LookupParameters, match.uid_min),          0             },
151
                { "uidMax",          _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uid_gid,            offsetof(LookupParameters, match.uid_max),          0             },
152
                { "uuid",            SD_JSON_VARIANT_STRING,        sd_json_dispatch_id128,              offsetof(LookupParameters, match.uuid),             0             },
153
                {}
154
        };
155

156
        _cleanup_(user_record_unrefp) UserRecord *hr = NULL;
9,818✔
157
        _cleanup_(lookup_parameters_done) LookupParameters p = {
×
158
                .uid = UID_INVALID,
159
                .match = USERDB_MATCH_NULL,
160
        };
161
        UserDBFlags userdb_flags;
9,818✔
162
        int r;
9,818✔
163

164
        assert(parameters);
9,818✔
165

166
        r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
9,818✔
167
        if (r != 0)
9,818✔
168
                return r;
169

170
        r = userdb_flags_from_service(link, p.service, &userdb_flags);
9,818✔
171
        if (r != 0) /* return value of < 0 means error (as usual); > 0 means 'already processed and replied,
9,818✔
172
                     * we are done'; == 0 means 'not processed, caller should process now' */
173
                return r;
174

175
        r = sd_varlink_set_sentinel(link, "io.systemd.UserDatabase.NoRecordFound");
9,818✔
176
        if (r < 0)
9,818✔
177
                return r;
178

179
        /* Prefer lookup by name if both name and UID are specified, so NSS-backed records preserve
180
         * the requested lookup name as an alias before we check the UID below. */
181
        if (p.name)
9,818✔
182
                r = userdb_by_name(p.name, &p.match, userdb_flags, &hr);
4,717✔
183
        else if (uid_is_valid(p.uid))
5,101✔
184
                r = userdb_by_uid(p.uid, &p.match, userdb_flags, &hr);
5,092✔
185
        else {
186
                _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
9✔
187

188
                r = userdb_all(&p.match, userdb_flags, &iterator);
9✔
189
                if (IN_SET(r, -ESRCH, -ENOLINK))
9✔
190
                        /* We turn off Varlink lookups in various cases (e.g. in case we only enable DropIn
191
                         * backend) — this might make userdb_all return ENOLINK (which indicates that varlink
192
                         * was off and no other suitable source or entries were found). Let's hide this
193
                         * implementation detail and always return NoRecordFound in this case, since from a
194
                         * client's perspective it's irrelevant if there was no entry at all or just not on
195
                         * the service that the query was limited to. */
196
                        return 0;
197
                if (r < 0)
9✔
198
                        return r;
199

200
                for (;;) {
163✔
201
                        _cleanup_(user_record_unrefp) UserRecord *z = NULL;
9✔
202

203
                        r = userdb_iterator_get(iterator, &p.match, &z);
172✔
204
                        if (r == -ESRCH)
172✔
205
                                break;
206
                        if (r < 0)
163✔
207
                                return r;
208

209
                        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
163✔
210
                        r = build_user_json(link, z, &v);
163✔
211
                        if (r < 0)
163✔
212
                                return r;
213

214
                        r = sd_varlink_reply(link, v);
163✔
215
                        if (r < 0)
163✔
216
                                return r;
217
                }
218

219
                return 0;
9✔
220
        }
221
        if (r == -ESRCH)
9,809✔
222
                return 0;
223
        if (r == -ENOEXEC)
5,531✔
224
                return sd_varlink_error(link, "io.systemd.UserDatabase.NonMatchingRecordFound", NULL);
×
225
        if (r < 0) {
5,531✔
226
                log_debug_errno(r, "User lookup failed abnormally: %m");
×
227
                return sd_varlink_error(link, "io.systemd.UserDatabase.ServiceNotAvailable", NULL);
×
228
        }
229

230
        if ((uid_is_valid(p.uid) && hr->uid != p.uid) ||
5,531✔
231
            (p.name && !user_record_matches_user_name(hr, p.name)))
5,531✔
232
                return sd_varlink_error(link, "io.systemd.UserDatabase.ConflictingRecordFound", NULL);
×
233

234
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
9,818✔
235
        r = build_user_json(link, hr, &v);
5,531✔
236
        if (r < 0)
5,531✔
237
                return r;
238

239
        return sd_varlink_reply(link, v);
5,531✔
240
}
241

242
static int build_group_json(sd_varlink *link, GroupRecord *gr, sd_json_variant **ret) {
3,182✔
243
        _cleanup_(group_record_unrefp) GroupRecord *stripped = NULL;
×
244
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
3,182✔
245
        UserRecordLoadFlags flags;
3,182✔
246
        uid_t peer_uid;
3,182✔
247
        bool trusted;
3,182✔
248
        int r;
3,182✔
249

250
        assert(gr);
3,182✔
251
        assert(ret);
3,182✔
252

253
        r = sd_varlink_get_peer_uid(link, &peer_uid);
3,182✔
254
        if (r < 0) {
3,182✔
255
                log_debug_errno(r, "Unable to query peer UID, ignoring: %m");
×
256
                trusted = false;
257
        } else
258
                trusted = peer_uid == 0;
3,182✔
259

260
        flags = USER_RECORD_REQUIRE_REGULAR|USER_RECORD_ALLOW_PER_MACHINE|USER_RECORD_ALLOW_BINDING|USER_RECORD_STRIP_SECRET|USER_RECORD_ALLOW_STATUS|USER_RECORD_ALLOW_SIGNATURE|USER_RECORD_PERMISSIVE;
6,364✔
261
        if (trusted)
3,182✔
262
                flags |= USER_RECORD_ALLOW_PRIVILEGED;
263
        else
264
                flags |= USER_RECORD_STRIP_PRIVILEGED;
265

266
        r = group_record_clone(gr, flags, &stripped);
3,182✔
267
        if (r < 0)
3,182✔
268
                return r;
269

270
        stripped->incomplete =
6,364✔
271
                gr->incomplete ||
3,182✔
272
                (FLAGS_SET(gr->mask, USER_RECORD_PRIVILEGED) &&
3,182✔
273
                 !FLAGS_SET(stripped->mask, USER_RECORD_PRIVILEGED));
37✔
274

275
        v = sd_json_variant_ref(stripped->json);
3,182✔
276
        r = add_nss_service(&v);
3,182✔
277
        if (r < 0)
3,182✔
278
                return r;
279

280
        return sd_json_buildo(
3,182✔
281
                        ret,
282
                        SD_JSON_BUILD_PAIR_VARIANT("record", v),
283
                        SD_JSON_BUILD_PAIR_BOOLEAN("incomplete", stripped->incomplete));
284
}
285

286
static int vl_method_get_group_record(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
19,273✔
287

288
        static const sd_json_dispatch_field dispatch_table[] = {
19,273✔
289
                { "gid",             _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uid_gid,            offsetof(LookupParameters, gid),                    0             },
290
                { "groupName",       SD_JSON_VARIANT_STRING,        json_dispatch_const_user_group_name, offsetof(LookupParameters, name),                   SD_JSON_RELAX },
291
                { "service",         SD_JSON_VARIANT_STRING,        sd_json_dispatch_const_string,       offsetof(LookupParameters, service),                0             },
292
                { "fuzzyNames",      SD_JSON_VARIANT_ARRAY,         sd_json_dispatch_strv,               offsetof(LookupParameters, match.fuzzy_names),      0             },
293
                { "dispositionMask", SD_JSON_VARIANT_ARRAY,         json_dispatch_dispositions_mask,     offsetof(LookupParameters, match.disposition_mask), 0             },
294
                { "gidMin",          _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uid_gid,            offsetof(LookupParameters, match.gid_min),          0             },
295
                { "gidMax",          _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uid_gid,            offsetof(LookupParameters, match.gid_max),          0             },
296
                { "uuid",            SD_JSON_VARIANT_STRING,        sd_json_dispatch_id128,              offsetof(LookupParameters, match.uuid),             0             },
297
                {}
298
        };
299

300
        _cleanup_(group_record_unrefp) GroupRecord *g = NULL;
19,273✔
301
        _cleanup_(lookup_parameters_done) LookupParameters p = {
×
302
                .gid = GID_INVALID,
303
                .match = USERDB_MATCH_NULL,
304
        };
305
        UserDBFlags userdb_flags;
19,273✔
306
        int r;
19,273✔
307

308
        assert(parameters);
19,273✔
309

310
        r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
19,273✔
311
        if (r != 0)
19,273✔
312
                return r;
313

314
        r = userdb_flags_from_service(link, p.service, &userdb_flags);
19,273✔
315
        if (r != 0)
19,273✔
316
                return r;
317

318
        r = sd_varlink_set_sentinel(link, "io.systemd.UserDatabase.NoRecordFound");
19,273✔
319
        if (r < 0)
19,273✔
320
                return r;
321

322
        /* Prefer lookup by name if both name and GID are specified, so NSS-backed records preserve
323
         * the requested lookup name as an alias before we check the GID below. */
324
        if (p.name)
19,273✔
325
                r = groupdb_by_name(p.name, &p.match, userdb_flags, &g);
14,267✔
326
        else if (gid_is_valid(p.gid))
5,006✔
327
                r = groupdb_by_gid(p.gid, &p.match, userdb_flags, &g);
5,003✔
328
        else {
329
                _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
3✔
330

331
                r = groupdb_all(&p.match, userdb_flags, &iterator);
3✔
332
                if (IN_SET(r, -ESRCH, -ENOLINK))
3✔
333
                        return 0;
334
                if (r < 0)
3✔
335
                        return r;
336

337
                for (;;) {
183✔
338
                        _cleanup_(group_record_unrefp) GroupRecord *z = NULL;
3✔
339

340
                        r = groupdb_iterator_get(iterator, &p.match, &z);
186✔
341
                        if (r == -ESRCH)
186✔
342
                                break;
343
                        if (r < 0)
183✔
344
                                return r;
345

346
                        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
183✔
347
                        r = build_group_json(link, z, &v);
183✔
348
                        if (r < 0)
183✔
349
                                return r;
350

351
                        r = sd_varlink_reply(link, v);
183✔
352
                        if (r < 0)
183✔
353
                                return r;
354
                }
355

356
                return 0;
3✔
357
        }
358
        if (r == -ESRCH)
19,270✔
359
                return 0;
360
        if (r == -ENOEXEC)
2,999✔
361
                return sd_varlink_error(link, "io.systemd.UserDatabase.NonMatchingRecordFound", NULL);
×
362
        if (r < 0) {
2,999✔
363
                log_debug_errno(r, "Group lookup failed abnormally: %m");
×
364
                return sd_varlink_error(link, "io.systemd.UserDatabase.ServiceNotAvailable", NULL);
×
365
        }
366

367
        if ((gid_is_valid(p.gid) && g->gid != p.gid) ||
2,999✔
368
            (p.name && !group_record_matches_group_name(g, p.name)))
2,999✔
369
                return sd_varlink_error(link, "io.systemd.UserDatabase.ConflictingRecordFound", NULL);
×
370

371
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
19,273✔
372
        r = build_group_json(link, g, &v);
2,999✔
373
        if (r < 0)
2,999✔
374
                return r;
375

376
        return sd_varlink_reply(link, v);
2,999✔
377
}
378

379
typedef struct MembershipLookupParameters {
380
        const char *user_name;
381
        const char *group_name;
382
        const char *service;
383
} MembershipLookupParameters;
384

385
static int vl_method_get_memberships(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
15,447✔
386
        static const sd_json_dispatch_field dispatch_table[] = {
15,447✔
387
                { "userName",  SD_JSON_VARIANT_STRING, json_dispatch_const_user_group_name, offsetof(MembershipLookupParameters, user_name),  SD_JSON_RELAX },
388
                { "groupName", SD_JSON_VARIANT_STRING, json_dispatch_const_user_group_name, offsetof(MembershipLookupParameters, group_name), SD_JSON_RELAX },
389
                { "service",   SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string,       offsetof(MembershipLookupParameters, service),    0             },
390
                {}
391
        };
392

393
        _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
15,447✔
394
        MembershipLookupParameters p = {};
15,447✔
395
        UserDBFlags userdb_flags;
15,447✔
396
        int r;
15,447✔
397

398
        assert(parameters);
15,447✔
399

400
        r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
15,447✔
401
        if (r != 0)
15,447✔
402
                return r;
403

404
        r = userdb_flags_from_service(link, p.service, &userdb_flags);
15,447✔
405
        if (r != 0)
15,447✔
406
                return r;
407

408
        r = sd_varlink_set_sentinel(link, "io.systemd.UserDatabase.NoRecordFound");
15,447✔
409
        if (r < 0)
15,447✔
410
                return r;
411

412
        if (p.group_name)
15,447✔
413
                r = membershipdb_by_group(p.group_name, userdb_flags, &iterator);
13,174✔
414
        else if (p.user_name)
2,273✔
415
                r = membershipdb_by_user(p.user_name, userdb_flags, &iterator);
2,268✔
416
        else
417
                r = membershipdb_all(userdb_flags, &iterator);
5✔
418
        if (IN_SET(r, -ESRCH, -ENOLINK))
15,447✔
419
                return 0;
420
        if (r < 0)
115✔
421
                return r;
422

423
        for (;;) {
276✔
424
                _cleanup_free_ char *user_name = NULL, *group_name = NULL;
163✔
425

426
                r = membershipdb_iterator_get(iterator, &user_name, &group_name);
276✔
427
                if (r == -ESRCH)
276✔
428
                        break;
429
                if (r < 0)
163✔
430
                        return r;
431

432
                /* If both group + user are specified do a-posteriori filtering */
433
                if (p.group_name && p.user_name && !streq(group_name, p.group_name))
163✔
434
                        continue;
×
435

436
                r = sd_varlink_replybo(
163✔
437
                                link,
438
                                SD_JSON_BUILD_PAIR_STRING("userName", user_name),
439
                                SD_JSON_BUILD_PAIR_STRING("groupName", group_name));
440
                if (r < 0)
163✔
441
                        return r;
442
        }
443

444
        return 0;
113✔
445
}
446

447
static int process_connection(sd_varlink_server *server, int _fd) {
44,546✔
448
        _cleanup_close_ int fd = TAKE_FD(_fd); /* always take possession */
44,546✔
449
        _cleanup_(sd_varlink_close_unrefp) sd_varlink *vl = NULL;
44,546✔
450
        int r;
44,546✔
451

452
        assert(server);
44,546✔
453
        assert(fd >= 0);
44,546✔
454

455
        r = sd_varlink_server_add_connection(server, fd, &vl);
44,546✔
456
        if (r < 0)
44,546✔
457
                return log_error_errno(r, "Failed to add connection: %m");
×
458

459
        TAKE_FD(fd);
44,546✔
460
        vl = sd_varlink_ref(vl);
44,546✔
461

462
        for (;;) {
410,790✔
463
                r = sd_varlink_process(vl);
410,790✔
464
                if (r == -ENOTCONN) {
410,790✔
465
                        log_debug("Connection terminated.");
44,546✔
466
                        break;
467
                }
468
                if (r < 0)
366,244✔
469
                        return log_error_errno(r, "Failed to process connection: %m");
×
470
                if (r > 0)
366,244✔
471
                        continue;
311,856✔
472

473
                r = sd_varlink_wait(vl, CONNECTION_IDLE_USEC);
54,388✔
474
                if (r < 0)
54,388✔
475
                        return log_error_errno(r, "Failed to wait for connection events: %m");
×
476
                if (r == 0)
54,388✔
477
                        break;
478
        }
479

480
        return 0;
481
}
482

483
static int run(int argc, char *argv[]) {
701✔
484
        usec_t start_time, listen_idle_usec, last_busy_usec = USEC_INFINITY;
701✔
485
        _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *server = NULL;
701✔
486
        _cleanup_(pidref_done) PidRef parent = PIDREF_NULL;
×
487
        unsigned n_iterations = 0;
701✔
488
        int m, listen_fd, r;
701✔
489

490
        log_setup();
701✔
491

492
        m = sd_listen_fds(false);
701✔
493
        if (m < 0)
701✔
494
                return log_error_errno(m, "Failed to determine number of listening fds: %m");
×
495
        if (m == 0)
701✔
496
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No socket to listen on received.");
×
497
        if (m > 1)
701✔
498
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Worker can only listen on a single socket at a time.");
×
499

500
        listen_fd = SD_LISTEN_FDS_START;
701✔
501

502
        r = fd_nonblock(listen_fd, false);
701✔
503
        if (r < 0)
701✔
504
                return log_error_errno(r, "Failed to turn off non-blocking mode for listening socket: %m");
×
505

506
        r = varlink_server_new(&server, 0, NULL);
701✔
507
        if (r < 0)
701✔
508
                return log_error_errno(r, "Failed to allocate varlink server: %m");
×
509

510
        r = sd_varlink_server_add_interface(server, &vl_interface_io_systemd_UserDatabase);
701✔
511
        if (r < 0)
701✔
512
                return log_error_errno(r, "Failed to add UserDatabase interface to varlink server: %m");
×
513

514
        r = sd_varlink_server_bind_method_many(
701✔
515
                        server,
516
                        "io.systemd.UserDatabase.GetUserRecord",  vl_method_get_user_record,
517
                        "io.systemd.UserDatabase.GetGroupRecord", vl_method_get_group_record,
518
                        "io.systemd.UserDatabase.GetMemberships", vl_method_get_memberships);
519
        if (r < 0)
701✔
520
                return log_error_errno(r, "Failed to bind methods: %m");
×
521

522
        r = getenv_bool("USERDB_FIXED_WORKER");
701✔
523
        if (r < 0)
701✔
524
                return log_error_errno(r, "Failed to parse USERDB_FIXED_WORKER: %m");
×
525
        listen_idle_usec = r ? USEC_INFINITY : LISTEN_IDLE_USEC;
701✔
526

527
        r = userdb_block_nss_systemd(true);
701✔
528
        if (r < 0)
701✔
529
                return log_error_errno(r, "Failed to disable userdb NSS compatibility: %m");
×
530

531
        r = pidref_set_parent(&parent);
701✔
532
        if (r < 0)
701✔
533
                return log_error_errno(r, "Failed to acquire pidfd of parent process: %m");
×
534
        if (parent.pid == 1) /* We got reparented away from userdbd? */
701✔
535
                return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Parent already died, exiting.");
×
536

537
        start_time = now(CLOCK_MONOTONIC);
701✔
538

539
        for (;;) {
45,557✔
540
                _cleanup_close_ int fd = -EBADF;
701✔
541
                usec_t n;
45,557✔
542

543
                /* Exit the worker in regular intervals, to flush out all memory use */
544
                if (n_iterations++ > ITERATIONS_MAX) {
45,557✔
545
                        log_debug("Exiting worker, processed %u iterations, that's enough.", n_iterations);
677✔
546
                        break;
547
                }
548

549
                n = now(CLOCK_MONOTONIC);
44,880✔
550
                if (n >= usec_add(start_time, RUNTIME_MAX_USEC)) {
89,760✔
551
                        log_debug("Exiting worker, ran for %s, that's enough.",
32✔
552
                                  FORMAT_TIMESPAN(usec_sub_unsigned(n, start_time), 0));
553
                        break;
16✔
554
                }
555

556
                if (last_busy_usec == USEC_INFINITY)
44,864✔
557
                        last_busy_usec = n;
558
                else if (listen_idle_usec != USEC_INFINITY && n >= usec_add(last_busy_usec, listen_idle_usec)) {
337✔
559
                        log_debug("Exiting worker, been idle for %s.",
16✔
560
                                  FORMAT_TIMESPAN(usec_sub_unsigned(n, last_busy_usec), 0));
561
                        break;
8✔
562
                }
563

564
                (void) rename_process("systemd-userwork: waiting...");
44,856✔
565
                fd = RET_NERRNO(accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC));
44,856✔
566
                (void) rename_process("systemd-userwork: processing...");
44,856✔
567

568
                if (fd == -EAGAIN)
44,856✔
569
                        continue; /* The listening socket has SO_RECVTIMEO set, hence a timeout is expected
310✔
570
                                   * after a while, let's check if it's time to exit though. */
571
                if (fd == -EINTR)
44,546✔
572
                        continue; /* Might be that somebody attached via strace, let's just continue in that
×
573
                                   * case */
574
                if (fd < 0)
44,546✔
575
                        return log_error_errno(fd, "Failed to accept() from listening socket: %m");
×
576

577
                if (now(CLOCK_MONOTONIC) <= usec_add(n, PRESSURE_SLEEP_TIME_USEC)) {
44,546✔
578
                        /* We only slept a very short time? If so, let's see if there are more sockets
579
                         * pending, and if so, let's ask our parent for more workers */
580

581
                        r = fd_wait_for_event(listen_fd, POLLIN, 0);
35,355✔
582
                        if (r < 0)
35,355✔
583
                                return log_error_errno(r, "Failed to test for POLLIN on listening socket: %m");
×
584

585
                        if (FLAGS_SET(r, POLLIN)) {
35,355✔
586
                                r = pidref_kill(&parent, SIGUSR2);
146✔
587
                                if (r == -ESRCH)
146✔
588
                                        return log_error_errno(r, "Parent already died?");
×
589
                                if (r < 0)
146✔
590
                                        return log_error_errno(r, "Failed to send SIGUSR2 signal to parent: %m");
×
591
                        }
592
                }
593

594
                (void) process_connection(server, TAKE_FD(fd));
44,546✔
595
                last_busy_usec = USEC_INFINITY;
44,546✔
596
        }
597

598
        return 0;
599
}
600

601
DEFINE_MAIN_FUNCTION(run);
701✔
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