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

systemd / systemd / 25409762285

05 May 2026 08:45PM UTC coverage: 72.658% (-0.02%) from 72.674%
25409762285

push

github

web-flow
Couple of coverity fixes (#41951)

0 of 11 new or added lines in 2 files covered. (0.0%)

2705 existing lines in 63 files now uncovered.

326249 of 449021 relevant lines covered (72.66%)

1212712.0 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) {
27,776✔
43
        assert(p);
27,776✔
44

45
        userdb_match_done(&p->match);
27,776✔
46
}
27,776✔
47

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

53
        assert(v);
8,111✔
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,111✔
59
                return 0;
60

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

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

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

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

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

79
        return sd_json_variant_set_field(v, "status", status);
7,860✔
80
}
81

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

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

93
        r = sd_varlink_get_peer_uid(link, &peer_uid);
5,148✔
94
        if (r < 0) {
5,148✔
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,148✔
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,287✔
101
        if (trusted)
139✔
102
                flags |= USER_RECORD_ALLOW_PRIVILEGED;
103
        else
104
                flags |= USER_RECORD_STRIP_PRIVILEGED;
105

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

110
        stripped->incomplete =
10,296✔
111
                ur->incomplete ||
5,148✔
112
                (FLAGS_SET(ur->mask, USER_RECORD_PRIVILEGED) &&
5,148✔
113
                 !FLAGS_SET(stripped->mask, USER_RECORD_PRIVILEGED));
4,452✔
114

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

120
        return sd_json_buildo(
5,148✔
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) {
42,613✔
127
        assert(link);
42,613✔
128
        assert(ret);
42,613✔
129

130
        if (streq_ptr(service, "io.systemd.NameServiceSwitch"))
42,613✔
131
                *ret = USERDB_NSS_ONLY|USERDB_AVOID_MULTIPLEXER;
55✔
132
        else if (streq_ptr(service, "io.systemd.DropIn"))
42,558✔
133
                *ret = USERDB_DROPIN_ONLY|USERDB_AVOID_MULTIPLEXER;
32,632✔
134
        else if (streq_ptr(service, "io.systemd.Multiplexer"))
9,926✔
135
                *ret = USERDB_AVOID_MULTIPLEXER;
9,926✔
136
        else
UNCOV
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,117✔
143

144
        static const sd_json_dispatch_field dispatch_table[] = {
9,117✔
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,117✔
157
        _cleanup_(lookup_parameters_done) LookupParameters p = {
×
158
                .uid = UID_INVALID,
159
                .match = USERDB_MATCH_NULL,
160
        };
161
        UserDBFlags userdb_flags;
9,117✔
162
        int r;
9,117✔
163

164
        assert(parameters);
9,117✔
165

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

170
        r = userdb_flags_from_service(link, p.service, &userdb_flags);
9,117✔
171
        if (r != 0) /* return value of < 0 means error (as usual); > 0 means 'already processed and replied,
9,117✔
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,117✔
176
        if (r < 0)
9,117✔
177
                return r;
178

179
        if (uid_is_valid(p.uid))
9,117✔
180
                r = userdb_by_uid(p.uid, &p.match, userdb_flags, &hr);
4,864✔
181
        else if (p.name)
4,253✔
182
                r = userdb_by_name(p.name, &p.match, userdb_flags, &hr);
4,243✔
183
        else {
184
                _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
10✔
185

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

198
                for (;;) {
243✔
199
                        _cleanup_(user_record_unrefp) UserRecord *z = NULL;
10✔
200

201
                        r = userdb_iterator_get(iterator, &p.match, &z);
253✔
202
                        if (r == -ESRCH)
253✔
203
                                break;
204
                        if (r < 0)
243✔
205
                                return r;
206

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

212
                        r = sd_varlink_reply(link, v);
243✔
213
                        if (r < 0)
243✔
214
                                return r;
215
                }
216

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

228
        if ((uid_is_valid(p.uid) && hr->uid != p.uid) ||
4,905✔
229
            (p.name && !user_record_matches_user_name(hr, p.name)))
4,905✔
230
                return sd_varlink_error(link, "io.systemd.UserDatabase.ConflictingRecordFound", NULL);
×
231

232
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
9,117✔
233
        r = build_user_json(link, hr, &v);
4,905✔
234
        if (r < 0)
4,905✔
235
                return r;
236

237
        return sd_varlink_reply(link, v);
4,905✔
238
}
239

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

248
        assert(gr);
2,963✔
249
        assert(ret);
2,963✔
250

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

258
        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,926✔
259
        if (trusted)
2,963✔
260
                flags |= USER_RECORD_ALLOW_PRIVILEGED;
261
        else
262
                flags |= USER_RECORD_STRIP_PRIVILEGED;
263

264
        r = group_record_clone(gr, flags, &stripped);
2,963✔
265
        if (r < 0)
2,963✔
266
                return r;
267

268
        stripped->incomplete =
5,926✔
269
                gr->incomplete ||
2,963✔
270
                (FLAGS_SET(gr->mask, USER_RECORD_PRIVILEGED) &&
2,963✔
271
                 !FLAGS_SET(stripped->mask, USER_RECORD_PRIVILEGED));
33✔
272

273
        v = sd_json_variant_ref(stripped->json);
2,963✔
274
        r = add_nss_service(&v);
2,963✔
275
        if (r < 0)
2,963✔
276
                return r;
277

278
        return sd_json_buildo(
2,963✔
279
                        ret,
280
                        SD_JSON_BUILD_PAIR_VARIANT("record", v),
281
                        SD_JSON_BUILD_PAIR_BOOLEAN("incomplete", stripped->incomplete));
282
}
283

284
static int vl_method_get_group_record(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
18,659✔
285

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

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

306
        assert(parameters);
18,659✔
307

308
        r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
18,659✔
309
        if (r != 0)
18,659✔
310
                return r;
311

312
        r = userdb_flags_from_service(link, p.service, &userdb_flags);
18,659✔
313
        if (r != 0)
18,659✔
314
                return r;
315

316
        r = sd_varlink_set_sentinel(link, "io.systemd.UserDatabase.NoRecordFound");
18,659✔
317
        if (r < 0)
18,659✔
318
                return r;
319

320
        if (gid_is_valid(p.gid))
18,659✔
321
                r = groupdb_by_gid(p.gid, &p.match, userdb_flags, &g);
4,726✔
322
        else if (p.name)
13,933✔
323
                r = groupdb_by_name(p.name, &p.match, userdb_flags, &g);
13,932✔
324
        else {
325
                _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
1✔
326

327
                r = groupdb_all(&p.match, userdb_flags, &iterator);
1✔
328
                if (IN_SET(r, -ESRCH, -ENOLINK))
1✔
329
                        return 0;
330
                if (r < 0)
1✔
331
                        return r;
332

333
                for (;;) {
59✔
334
                        _cleanup_(group_record_unrefp) GroupRecord *z = NULL;
1✔
335

336
                        r = groupdb_iterator_get(iterator, &p.match, &z);
60✔
337
                        if (r == -ESRCH)
60✔
338
                                break;
339
                        if (r < 0)
59✔
340
                                return r;
341

342
                        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
59✔
343
                        r = build_group_json(link, z, &v);
59✔
344
                        if (r < 0)
59✔
345
                                return r;
346

347
                        r = sd_varlink_reply(link, v);
59✔
348
                        if (r < 0)
59✔
349
                                return r;
350
                }
351

352
                return 0;
1✔
353
        }
354
        if (r == -ESRCH)
18,658✔
355
                return 0;
356
        if (r == -ENOEXEC)
2,904✔
357
                return sd_varlink_error(link, "io.systemd.UserDatabase.NonMatchingRecordFound", NULL);
×
358
        if (r < 0) {
2,904✔
359
                log_debug_errno(r, "Group lookup failed abnormally: %m");
×
360
                return sd_varlink_error(link, "io.systemd.UserDatabase.ServiceNotAvailable", NULL);
×
361
        }
362

363
        if ((uid_is_valid(p.gid) && g->gid != p.gid) ||
2,904✔
364
            (p.name && !group_record_matches_group_name(g, p.name)))
2,904✔
365
                return sd_varlink_error(link, "io.systemd.UserDatabase.ConflictingRecordFound", NULL);
×
366

367
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
18,659✔
368
        r = build_group_json(link, g, &v);
2,904✔
369
        if (r < 0)
2,904✔
370
                return r;
371

372
        return sd_varlink_reply(link, v);
2,904✔
373
}
374

375
typedef struct MembershipLookupParameters {
376
        const char *user_name;
377
        const char *group_name;
378
        const char *service;
379
} MembershipLookupParameters;
380

381
static int vl_method_get_memberships(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
14,837✔
382
        static const sd_json_dispatch_field dispatch_table[] = {
14,837✔
383
                { "userName",  SD_JSON_VARIANT_STRING, json_dispatch_const_user_group_name, offsetof(MembershipLookupParameters, user_name),  SD_JSON_RELAX },
384
                { "groupName", SD_JSON_VARIANT_STRING, json_dispatch_const_user_group_name, offsetof(MembershipLookupParameters, group_name), SD_JSON_RELAX },
385
                { "service",   SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string,       offsetof(MembershipLookupParameters, service),    0             },
386
                {}
387
        };
388

389
        _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
14,837✔
390
        MembershipLookupParameters p = {};
14,837✔
391
        UserDBFlags userdb_flags;
14,837✔
392
        int r;
14,837✔
393

394
        assert(parameters);
14,837✔
395

396
        r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
14,837✔
397
        if (r != 0)
14,837✔
398
                return r;
399

400
        r = userdb_flags_from_service(link, p.service, &userdb_flags);
14,837✔
401
        if (r != 0)
14,837✔
402
                return r;
403

404
        r = sd_varlink_set_sentinel(link, "io.systemd.UserDatabase.NoRecordFound");
14,837✔
405
        if (r < 0)
14,837✔
406
                return r;
407

408
        if (p.group_name)
14,837✔
409
                r = membershipdb_by_group(p.group_name, userdb_flags, &iterator);
12,639✔
410
        else if (p.user_name)
2,198✔
411
                r = membershipdb_by_user(p.user_name, userdb_flags, &iterator);
2,193✔
412
        else
413
                r = membershipdb_all(userdb_flags, &iterator);
5✔
414
        if (IN_SET(r, -ESRCH, -ENOLINK))
14,837✔
415
                return 0;
416
        if (r < 0)
121✔
417
                return r;
418

419
        for (;;) {
283✔
420
                _cleanup_free_ char *user_name = NULL, *group_name = NULL;
164✔
421

422
                r = membershipdb_iterator_get(iterator, &user_name, &group_name);
283✔
423
                if (r == -ESRCH)
283✔
424
                        break;
425
                if (r < 0)
164✔
426
                        return r;
427

428
                /* If both group + user are specified do a-posteriori filtering */
429
                if (p.group_name && p.user_name && !streq(group_name, p.group_name))
164✔
430
                        continue;
×
431

432
                r = sd_varlink_replybo(
164✔
433
                                link,
434
                                SD_JSON_BUILD_PAIR_STRING("userName", user_name),
435
                                SD_JSON_BUILD_PAIR_STRING("groupName", group_name));
436
                if (r < 0)
164✔
437
                        return r;
438
        }
439

440
        return 0;
119✔
441
}
442

443
static int process_connection(sd_varlink_server *server, int _fd) {
42,617✔
444
        _cleanup_close_ int fd = TAKE_FD(_fd); /* always take possession */
42,617✔
445
        _cleanup_(sd_varlink_close_unrefp) sd_varlink *vl = NULL;
42,617✔
446
        int r;
42,617✔
447

448
        assert(server);
42,617✔
449
        assert(fd >= 0);
42,617✔
450

451
        r = sd_varlink_server_add_connection(server, fd, &vl);
42,617✔
452
        if (r < 0)
42,617✔
453
                return log_error_errno(r, "Failed to add connection: %m");
×
454

455
        TAKE_FD(fd);
42,617✔
456
        vl = sd_varlink_ref(vl);
42,617✔
457

458
        for (;;) {
398,192✔
459
                r = sd_varlink_process(vl);
398,192✔
460
                if (r == -ENOTCONN) {
398,192✔
461
                        log_debug("Connection terminated.");
42,617✔
462
                        break;
463
                }
464
                if (r < 0)
355,575✔
465
                        return log_error_errno(r, "Failed to process connection: %m");
×
466
                if (r > 0)
355,575✔
467
                        continue;
298,329✔
468

469
                r = sd_varlink_wait(vl, CONNECTION_IDLE_USEC);
57,246✔
470
                if (r < 0)
57,246✔
471
                        return log_error_errno(r, "Failed to wait for connection events: %m");
×
472
                if (r == 0)
57,246✔
473
                        break;
474
        }
475

476
        return 0;
477
}
478

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

486
        log_setup();
669✔
487

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

496
        listen_fd = SD_LISTEN_FDS_START;
669✔
497

498
        r = fd_nonblock(listen_fd, false);
669✔
499
        if (r < 0)
669✔
500
                return log_error_errno(r, "Failed to turn off non-blocking mode for listening socket: %m");
×
501

502
        r = varlink_server_new(&server, 0, NULL);
669✔
503
        if (r < 0)
669✔
504
                return log_error_errno(r, "Failed to allocate varlink server: %m");
×
505

506
        r = sd_varlink_server_add_interface(server, &vl_interface_io_systemd_UserDatabase);
669✔
507
        if (r < 0)
669✔
508
                return log_error_errno(r, "Failed to add UserDatabase interface to varlink server: %m");
×
509

510
        r = sd_varlink_server_bind_method_many(
669✔
511
                        server,
512
                        "io.systemd.UserDatabase.GetUserRecord",  vl_method_get_user_record,
513
                        "io.systemd.UserDatabase.GetGroupRecord", vl_method_get_group_record,
514
                        "io.systemd.UserDatabase.GetMemberships", vl_method_get_memberships);
515
        if (r < 0)
669✔
516
                return log_error_errno(r, "Failed to bind methods: %m");
×
517

518
        r = getenv_bool("USERDB_FIXED_WORKER");
669✔
519
        if (r < 0)
669✔
520
                return log_error_errno(r, "Failed to parse USERDB_FIXED_WORKER: %m");
×
521
        listen_idle_usec = r ? USEC_INFINITY : LISTEN_IDLE_USEC;
669✔
522

523
        r = userdb_block_nss_systemd(true);
669✔
524
        if (r < 0)
669✔
525
                return log_error_errno(r, "Failed to disable userdb NSS compatibility: %m");
×
526

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

533
        start_time = now(CLOCK_MONOTONIC);
669✔
534

535
        for (;;) {
43,532✔
536
                _cleanup_close_ int fd = -EBADF;
669✔
537
                usec_t n;
43,532✔
538

539
                /* Exit the worker in regular intervals, to flush out all memory use */
540
                if (n_iterations++ > ITERATIONS_MAX) {
43,532✔
541
                        log_debug("Exiting worker, processed %u iterations, that's enough.", n_iterations);
648✔
542
                        break;
543
                }
544

545
                n = now(CLOCK_MONOTONIC);
42,884✔
546
                if (n >= usec_add(start_time, RUNTIME_MAX_USEC)) {
85,768✔
547
                        log_debug("Exiting worker, ran for %s, that's enough.",
22✔
548
                                  FORMAT_TIMESPAN(usec_sub_unsigned(n, start_time), 0));
549
                        break;
11✔
550
                }
551

552
                if (last_busy_usec == USEC_INFINITY)
42,873✔
553
                        last_busy_usec = n;
554
                else if (listen_idle_usec != USEC_INFINITY && n >= usec_add(last_busy_usec, listen_idle_usec)) {
282✔
555
                        log_debug("Exiting worker, been idle for %s.",
20✔
556
                                  FORMAT_TIMESPAN(usec_sub_unsigned(n, last_busy_usec), 0));
557
                        break;
10✔
558
                }
559

560
                (void) rename_process("systemd-userwork: waiting...");
42,863✔
561
                fd = RET_NERRNO(accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC));
42,863✔
562
                (void) rename_process("systemd-userwork: processing...");
42,863✔
563

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

573
                if (now(CLOCK_MONOTONIC) <= usec_add(n, PRESSURE_SLEEP_TIME_USEC)) {
42,617✔
574
                        /* We only slept a very short time? If so, let's see if there are more sockets
575
                         * pending, and if so, let's ask our parent for more workers */
576

577
                        r = fd_wait_for_event(listen_fd, POLLIN, 0);
33,980✔
578
                        if (r < 0)
33,980✔
579
                                return log_error_errno(r, "Failed to test for POLLIN on listening socket: %m");
×
580

581
                        if (FLAGS_SET(r, POLLIN)) {
33,980✔
582
                                r = pidref_kill(&parent, SIGUSR2);
117✔
583
                                if (r == -ESRCH)
117✔
584
                                        return log_error_errno(r, "Parent already died?");
×
585
                                if (r < 0)
117✔
586
                                        return log_error_errno(r, "Failed to send SIGUSR2 signal to parent: %m");
×
587
                        }
588
                }
589

590
                (void) process_connection(server, TAKE_FD(fd));
42,617✔
591
                last_busy_usec = USEC_INFINITY;
42,617✔
592
        }
593

594
        return 0;
595
}
596

597
DEFINE_MAIN_FUNCTION(run);
669✔
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