]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/userdb/userwork.c
Merge pull request #30284 from YHNdnzj/fstab-wantedby-defaultdeps
[thirdparty/systemd.git] / src / userdb / userwork.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
d093b62c
LP
2
3#include <poll.h>
4#include <sys/wait.h>
5
6#include "sd-daemon.h"
7
8#include "env-util.h"
9#include "fd-util.h"
d093b62c 10#include "group-record.h"
0f2d351f 11#include "io-util.h"
d093b62c
LP
12#include "main-func.h"
13#include "process-util.h"
14#include "strv.h"
15#include "time-util.h"
16#include "user-record-nss.h"
17#include "user-record.h"
18#include "user-util.h"
19#include "userdb.h"
20#include "varlink.h"
abef4a7b 21#include "varlink-io.systemd.UserDatabase.h"
d093b62c
LP
22
23#define ITERATIONS_MAX 64U
24#define RUNTIME_MAX_USEC (5 * USEC_PER_MINUTE)
25#define PRESSURE_SLEEP_TIME_USEC (50 * USEC_PER_MSEC)
26#define CONNECTION_IDLE_USEC (15 * USEC_PER_SEC)
27#define LISTEN_IDLE_USEC (90 * USEC_PER_SEC)
28
29typedef struct LookupParameters {
30 const char *user_name;
31 const char *group_name;
32 union {
33 uid_t uid;
34 gid_t gid;
35 };
36 const char *service;
37} LookupParameters;
38
39static int add_nss_service(JsonVariant **v) {
40 _cleanup_(json_variant_unrefp) JsonVariant *status = NULL, *z = NULL;
d093b62c
LP
41 sd_id128_t mid;
42 int r;
43
44 assert(v);
45
46 /* Patch in service field if it's missing. The assumption here is that this field is unset only for
47 * NSS records */
48
49 if (json_variant_by_key(*v, "service"))
50 return 0;
51
52 r = sd_id128_get_machine(&mid);
53 if (r < 0)
54 return r;
55
56 status = json_variant_ref(json_variant_by_key(*v, "status"));
85b55869 57 z = json_variant_ref(json_variant_by_key(status, SD_ID128_TO_STRING(mid)));
d093b62c
LP
58
59 if (json_variant_by_key(z, "service"))
60 return 0;
61
62 r = json_variant_set_field_string(&z, "service", "io.systemd.NameServiceSwitch");
63 if (r < 0)
64 return r;
65
85b55869 66 r = json_variant_set_field(&status, SD_ID128_TO_STRING(mid), z);
d093b62c
LP
67 if (r < 0)
68 return r;
69
70 return json_variant_set_field(v, "status", status);
71}
72
73static int build_user_json(Varlink *link, UserRecord *ur, JsonVariant **ret) {
74 _cleanup_(user_record_unrefp) UserRecord *stripped = NULL;
75 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
76 UserRecordLoadFlags flags;
77 uid_t peer_uid;
78 bool trusted;
79 int r;
80
81 assert(ur);
82 assert(ret);
83
84 r = varlink_get_peer_uid(link, &peer_uid);
85 if (r < 0) {
86 log_debug_errno(r, "Unable to query peer UID, ignoring: %m");
87 trusted = false;
88 } else
89 trusted = peer_uid == 0 || peer_uid == ur->uid;
90
bfc0cc1a 91 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;
d093b62c
LP
92 if (trusted)
93 flags |= USER_RECORD_ALLOW_PRIVILEGED;
94 else
95 flags |= USER_RECORD_STRIP_PRIVILEGED;
96
97 r = user_record_clone(ur, flags, &stripped);
98 if (r < 0)
99 return r;
100
101 stripped->incomplete =
102 ur->incomplete ||
103 (FLAGS_SET(ur->mask, USER_RECORD_PRIVILEGED) &&
104 !FLAGS_SET(stripped->mask, USER_RECORD_PRIVILEGED));
105
106 v = json_variant_ref(stripped->json);
107 r = add_nss_service(&v);
108 if (r < 0)
109 return r;
110
111 return json_build(ret, JSON_BUILD_OBJECT(
112 JSON_BUILD_PAIR("record", JSON_BUILD_VARIANT(v)),
113 JSON_BUILD_PAIR("incomplete", JSON_BUILD_BOOLEAN(stripped->incomplete))));
114}
115
134ff8f4
LP
116static int userdb_flags_from_service(Varlink *link, const char *service, UserDBFlags *ret) {
117 assert(link);
134ff8f4
LP
118 assert(ret);
119
120 if (streq_ptr(service, "io.systemd.NameServiceSwitch"))
121 *ret = USERDB_NSS_ONLY|USERDB_AVOID_MULTIPLEXER;
cf7c7512 122 else if (streq_ptr(service, "io.systemd.DropIn"))
8fbb1941 123 *ret = USERDB_DROPIN_ONLY|USERDB_AVOID_MULTIPLEXER;
134ff8f4
LP
124 else if (streq_ptr(service, "io.systemd.Multiplexer"))
125 *ret = USERDB_AVOID_MULTIPLEXER;
126 else
127 return varlink_error(link, "io.systemd.UserDatabase.BadService", NULL);
128
129 return 0;
130}
131
d093b62c
LP
132static int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) {
133
134 static const JsonDispatch dispatch_table[] = {
135 { "uid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(LookupParameters, uid), 0 },
136 { "userName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, user_name), 0 },
137 { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 },
138 {}
139 };
140
141 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
142 _cleanup_(user_record_unrefp) UserRecord *hr = NULL;
143 LookupParameters p = {
144 .uid = UID_INVALID,
145 };
134ff8f4 146 UserDBFlags userdb_flags;
d093b62c
LP
147 int r;
148
149 assert(parameters);
150
f1b622a0
LP
151 r = varlink_dispatch(link, parameters, dispatch_table, &p);
152 if (r != 0)
d093b62c
LP
153 return r;
154
134ff8f4 155 r = userdb_flags_from_service(link, p.service, &userdb_flags);
18e94a40
LP
156 if (r != 0) /* return value of < 0 means error (as usual); > 0 means 'already processed and replied,
157 * we are done'; == 0 means 'not processed, caller should process now' */
134ff8f4 158 return r;
d093b62c 159
134ff8f4
LP
160 if (uid_is_valid(p.uid))
161 r = userdb_by_uid(p.uid, userdb_flags, &hr);
162 else if (p.user_name)
163 r = userdb_by_name(p.user_name, userdb_flags, &hr);
164 else {
165 _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
166 _cleanup_(json_variant_unrefp) JsonVariant *last = NULL;
d093b62c 167
134ff8f4 168 r = userdb_all(userdb_flags, &iterator);
e908961d
LP
169 if (IN_SET(r, -ESRCH, -ENOLINK))
170 /* We turn off Varlink lookups in various cases (e.g. in case we only enable DropIn
171 * backend) — this might make userdb_all return ENOLINK (which indicates that varlink
172 * was off and no other suitable source or entries were found). Let's hide this
173 * implementation detail and always return NoRecordFound in this case, since from a
174 * client's perspective it's irrelevant if there was no entry at all or just not on
175 * the service that the query was limited to. */
176 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
134ff8f4
LP
177 if (r < 0)
178 return r;
d093b62c 179
134ff8f4
LP
180 for (;;) {
181 _cleanup_(user_record_unrefp) UserRecord *z = NULL;
d093b62c 182
134ff8f4
LP
183 r = userdb_iterator_get(iterator, &z);
184 if (r == -ESRCH)
185 break;
186 if (r < 0)
187 return r;
d093b62c 188
134ff8f4
LP
189 if (last) {
190 r = varlink_notify(link, last);
d093b62c 191 if (r < 0)
d093b62c 192 return r;
d093b62c 193
134ff8f4 194 last = json_variant_unref(last);
d093b62c
LP
195 }
196
134ff8f4 197 r = build_user_json(link, z, &last);
d093b62c
LP
198 if (r < 0)
199 return r;
134ff8f4 200 }
d093b62c 201
134ff8f4
LP
202 if (!last)
203 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
d093b62c 204
134ff8f4
LP
205 return varlink_reply(link, last);
206 }
d093b62c
LP
207 if (r == -ESRCH)
208 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
209 if (r < 0) {
210 log_debug_errno(r, "User lookup failed abnormally: %m");
211 return varlink_error(link, "io.systemd.UserDatabase.ServiceNotAvailable", NULL);
212 }
213
214 if ((uid_is_valid(p.uid) && hr->uid != p.uid) ||
215 (p.user_name && !streq(hr->user_name, p.user_name)))
216 return varlink_error(link, "io.systemd.UserDatabase.ConflictingRecordFound", NULL);
217
218 r = build_user_json(link, hr, &v);
219 if (r < 0)
220 return r;
221
222 return varlink_reply(link, v);
223}
224
225static int build_group_json(Varlink *link, GroupRecord *gr, JsonVariant **ret) {
226 _cleanup_(group_record_unrefp) GroupRecord *stripped = NULL;
227 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
228 UserRecordLoadFlags flags;
229 uid_t peer_uid;
230 bool trusted;
231 int r;
232
233 assert(gr);
234 assert(ret);
235
236 r = varlink_get_peer_uid(link, &peer_uid);
237 if (r < 0) {
238 log_debug_errno(r, "Unable to query peer UID, ignoring: %m");
239 trusted = false;
240 } else
241 trusted = peer_uid == 0;
242
bfc0cc1a 243 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;
d093b62c
LP
244 if (trusted)
245 flags |= USER_RECORD_ALLOW_PRIVILEGED;
246 else
247 flags |= USER_RECORD_STRIP_PRIVILEGED;
248
249 r = group_record_clone(gr, flags, &stripped);
250 if (r < 0)
251 return r;
252
253 stripped->incomplete =
254 gr->incomplete ||
255 (FLAGS_SET(gr->mask, USER_RECORD_PRIVILEGED) &&
256 !FLAGS_SET(stripped->mask, USER_RECORD_PRIVILEGED));
257
258 v = json_variant_ref(gr->json);
259 r = add_nss_service(&v);
260 if (r < 0)
261 return r;
262
263 return json_build(ret, JSON_BUILD_OBJECT(
264 JSON_BUILD_PAIR("record", JSON_BUILD_VARIANT(v)),
265 JSON_BUILD_PAIR("incomplete", JSON_BUILD_BOOLEAN(stripped->incomplete))));
266}
267
268static int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) {
269
270 static const JsonDispatch dispatch_table[] = {
271 { "gid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(LookupParameters, gid), 0 },
272 { "groupName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, group_name), 0 },
273 { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 },
274 {}
275 };
276
277 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
278 _cleanup_(group_record_unrefp) GroupRecord *g = NULL;
279 LookupParameters p = {
280 .gid = GID_INVALID,
281 };
134ff8f4 282 UserDBFlags userdb_flags;
d093b62c
LP
283 int r;
284
285 assert(parameters);
286
f1b622a0
LP
287 r = varlink_dispatch(link, parameters, dispatch_table, &p);
288 if (r != 0)
d093b62c
LP
289 return r;
290
134ff8f4 291 r = userdb_flags_from_service(link, p.service, &userdb_flags);
18e94a40 292 if (r != 0)
134ff8f4 293 return r;
d093b62c 294
134ff8f4
LP
295 if (gid_is_valid(p.gid))
296 r = groupdb_by_gid(p.gid, userdb_flags, &g);
297 else if (p.group_name)
298 r = groupdb_by_name(p.group_name, userdb_flags, &g);
299 else {
300 _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
301 _cleanup_(json_variant_unrefp) JsonVariant *last = NULL;
d093b62c 302
134ff8f4 303 r = groupdb_all(userdb_flags, &iterator);
e908961d
LP
304 if (IN_SET(r, -ESRCH, -ENOLINK))
305 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
134ff8f4
LP
306 if (r < 0)
307 return r;
d093b62c 308
134ff8f4
LP
309 for (;;) {
310 _cleanup_(group_record_unrefp) GroupRecord *z = NULL;
d093b62c 311
134ff8f4
LP
312 r = groupdb_iterator_get(iterator, &z);
313 if (r == -ESRCH)
314 break;
315 if (r < 0)
316 return r;
d093b62c 317
134ff8f4
LP
318 if (last) {
319 r = varlink_notify(link, last);
d093b62c 320 if (r < 0)
d093b62c 321 return r;
d093b62c 322
134ff8f4 323 last = json_variant_unref(last);
d093b62c
LP
324 }
325
134ff8f4 326 r = build_group_json(link, z, &last);
d093b62c
LP
327 if (r < 0)
328 return r;
134ff8f4 329 }
d093b62c 330
134ff8f4
LP
331 if (!last)
332 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
d093b62c 333
134ff8f4
LP
334 return varlink_reply(link, last);
335 }
d093b62c
LP
336 if (r == -ESRCH)
337 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
338 if (r < 0) {
339 log_debug_errno(r, "Group lookup failed abnormally: %m");
340 return varlink_error(link, "io.systemd.UserDatabase.ServiceNotAvailable", NULL);
341 }
342
343 if ((uid_is_valid(p.gid) && g->gid != p.gid) ||
344 (p.group_name && !streq(g->group_name, p.group_name)))
345 return varlink_error(link, "io.systemd.UserDatabase.ConflictingRecordFound", NULL);
346
347 r = build_group_json(link, g, &v);
348 if (r < 0)
349 return r;
350
351 return varlink_reply(link, v);
352}
353
354static int vl_method_get_memberships(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) {
355 static const JsonDispatch dispatch_table[] = {
404a12e1 356 { "userName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, user_name), 0 },
d093b62c 357 { "groupName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, group_name), 0 },
404a12e1 358 { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 },
d093b62c
LP
359 {}
360 };
361
134ff8f4
LP
362 _cleanup_free_ char *last_user_name = NULL, *last_group_name = NULL;
363 _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
d093b62c 364 LookupParameters p = {};
134ff8f4 365 UserDBFlags userdb_flags;
d093b62c
LP
366 int r;
367
368 assert(parameters);
369
f1b622a0
LP
370 r = varlink_dispatch(link, parameters, dispatch_table, &p);
371 if (r != 0)
d093b62c
LP
372 return r;
373
134ff8f4 374 r = userdb_flags_from_service(link, p.service, &userdb_flags);
18e94a40 375 if (r != 0)
134ff8f4 376 return r;
d093b62c 377
134ff8f4
LP
378 if (p.group_name)
379 r = membershipdb_by_group(p.group_name, userdb_flags, &iterator);
380 else if (p.user_name)
381 r = membershipdb_by_user(p.user_name, userdb_flags, &iterator);
382 else
383 r = membershipdb_all(userdb_flags, &iterator);
e908961d
LP
384 if (IN_SET(r, -ESRCH, -ENOLINK))
385 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
134ff8f4
LP
386 if (r < 0)
387 return r;
d093b62c 388
134ff8f4
LP
389 for (;;) {
390 _cleanup_free_ char *user_name = NULL, *group_name = NULL;
d093b62c 391
134ff8f4
LP
392 r = membershipdb_iterator_get(iterator, &user_name, &group_name);
393 if (r == -ESRCH)
394 break;
d093b62c
LP
395 if (r < 0)
396 return r;
397
134ff8f4
LP
398 /* If both group + user are specified do a-posteriori filtering */
399 if (p.group_name && p.user_name && !streq(group_name, p.group_name))
400 continue;
d093b62c 401
134ff8f4
LP
402 if (last_user_name) {
403 assert(last_group_name);
404
405 r = varlink_notifyb(link, JSON_BUILD_OBJECT(
406 JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(last_user_name)),
407 JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(last_group_name))));
d093b62c
LP
408 if (r < 0)
409 return r;
d093b62c
LP
410 }
411
6ac65492
YW
412 free_and_replace(last_user_name, user_name);
413 free_and_replace(last_group_name, group_name);
134ff8f4 414 }
d093b62c 415
134ff8f4
LP
416 if (!last_user_name) {
417 assert(!last_group_name);
418 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
d093b62c
LP
419 }
420
134ff8f4
LP
421 assert(last_group_name);
422
423 return varlink_replyb(link, JSON_BUILD_OBJECT(
424 JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(last_user_name)),
425 JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(last_group_name))));
d093b62c
LP
426}
427
523121d5 428static int process_connection(VarlinkServer *server, int _fd) {
d562667f 429 _cleanup_close_ int fd = TAKE_FD(_fd); /* always take possession */
d093b62c
LP
430 _cleanup_(varlink_close_unrefp) Varlink *vl = NULL;
431 int r;
432
433 r = varlink_server_add_connection(server, fd, &vl);
523121d5 434 if (r < 0)
d093b62c 435 return log_error_errno(r, "Failed to add connection: %m");
d093b62c 436
523121d5 437 TAKE_FD(fd);
d093b62c
LP
438 vl = varlink_ref(vl);
439
440 for (;;) {
441 r = varlink_process(vl);
442 if (r == -ENOTCONN) {
443 log_debug("Connection terminated.");
444 break;
445 }
446 if (r < 0)
447 return log_error_errno(r, "Failed to process connection: %m");
448 if (r > 0)
449 continue;
450
451 r = varlink_wait(vl, CONNECTION_IDLE_USEC);
452 if (r < 0)
453 return log_error_errno(r, "Failed to wait for connection events: %m");
454 if (r == 0)
455 break;
456 }
457
458 return 0;
459}
460
461static int run(int argc, char *argv[]) {
462 usec_t start_time, listen_idle_usec, last_busy_usec = USEC_INFINITY;
463 _cleanup_(varlink_server_unrefp) VarlinkServer *server = NULL;
7c695bea 464 _cleanup_(pidref_done) PidRef parent = PIDREF_NULL;
d093b62c
LP
465 unsigned n_iterations = 0;
466 int m, listen_fd, r;
467
d2acb93d 468 log_setup();
d093b62c
LP
469
470 m = sd_listen_fds(false);
471 if (m < 0)
472 return log_error_errno(m, "Failed to determine number of listening fds: %m");
473 if (m == 0)
474 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No socket to listen on received.");
475 if (m > 1)
476 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Worker can only listen on a single socket at a time.");
477
478 listen_fd = SD_LISTEN_FDS_START;
479
480 r = fd_nonblock(listen_fd, false);
481 if (r < 0)
482 return log_error_errno(r, "Failed to turn off non-blocking mode for listening socket: %m");
483
484 r = varlink_server_new(&server, 0);
485 if (r < 0)
486 return log_error_errno(r, "Failed to allocate server: %m");
487
abef4a7b
LP
488 r = varlink_server_add_interface(server, &vl_interface_io_systemd_UserDatabase);
489 if (r < 0)
490 return log_error_errno(r, "Failed to add UserDatabase interface to varlink server: %m");
491
d093b62c
LP
492 r = varlink_server_bind_method_many(
493 server,
494 "io.systemd.UserDatabase.GetUserRecord", vl_method_get_user_record,
495 "io.systemd.UserDatabase.GetGroupRecord", vl_method_get_group_record,
496 "io.systemd.UserDatabase.GetMemberships", vl_method_get_memberships);
497 if (r < 0)
498 return log_error_errno(r, "Failed to bind methods: %m");
499
500 r = getenv_bool("USERDB_FIXED_WORKER");
501 if (r < 0)
502 return log_error_errno(r, "Failed to parse USERDB_FIXED_WORKER: %m");
503 listen_idle_usec = r ? USEC_INFINITY : LISTEN_IDLE_USEC;
504
037b0a47
LP
505 r = userdb_block_nss_systemd(true);
506 if (r < 0)
d093b62c
LP
507 return log_error_errno(r, "Failed to disable userdb NSS compatibility: %m");
508
7c695bea
LP
509 r = pidref_set_parent(&parent);
510 if (r < 0)
511 return log_error_errno(r, "Failed to acquire pidfd of parent process: %m");
512 if (parent.pid == 1) /* We got reparented away from userdbd? */
513 return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Parent already died, exiting.");
514
d093b62c
LP
515 start_time = now(CLOCK_MONOTONIC);
516
517 for (;;) {
254d1313 518 _cleanup_close_ int fd = -EBADF;
d093b62c
LP
519 usec_t n;
520
521 /* Exit the worker in regular intervals, to flush out all memory use */
522 if (n_iterations++ > ITERATIONS_MAX) {
523 log_debug("Exiting worker, processed %u iterations, that's enough.", n_iterations);
524 break;
525 }
526
527 n = now(CLOCK_MONOTONIC);
528 if (n >= usec_add(start_time, RUNTIME_MAX_USEC)) {
e14db350 529 log_debug("Exiting worker, ran for %s, that's enough.",
5291f26d 530 FORMAT_TIMESPAN(usec_sub_unsigned(n, start_time), 0));
d093b62c
LP
531 break;
532 }
533
534 if (last_busy_usec == USEC_INFINITY)
535 last_busy_usec = n;
536 else if (listen_idle_usec != USEC_INFINITY && n >= usec_add(last_busy_usec, listen_idle_usec)) {
40fd0a77 537 log_debug("Exiting worker, been idle for %s.",
5291f26d 538 FORMAT_TIMESPAN(usec_sub_unsigned(n, last_busy_usec), 0));
d093b62c
LP
539 break;
540 }
541
542 (void) rename_process("systemd-userwork: waiting...");
7c248223 543 fd = RET_NERRNO(accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC));
d093b62c
LP
544 (void) rename_process("systemd-userwork: processing...");
545
546 if (fd == -EAGAIN)
e14db350 547 continue; /* The listening socket has SO_RECVTIMEO set, hence a timeout is expected
d093b62c
LP
548 * after a while, let's check if it's time to exit though. */
549 if (fd == -EINTR)
550 continue; /* Might be that somebody attached via strace, let's just continue in that
551 * case */
552 if (fd < 0)
553 return log_error_errno(fd, "Failed to accept() from listening socket: %m");
554
555 if (now(CLOCK_MONOTONIC) <= usec_add(n, PRESSURE_SLEEP_TIME_USEC)) {
d093b62c
LP
556 /* We only slept a very short time? If so, let's see if there are more sockets
557 * pending, and if so, let's ask our parent for more workers */
558
0f2d351f
LP
559 r = fd_wait_for_event(listen_fd, POLLIN, 0);
560 if (r < 0)
561 return log_error_errno(r, "Failed to test for POLLIN on listening socket: %m");
d093b62c 562
0f2d351f 563 if (FLAGS_SET(r, POLLIN)) {
7c695bea
LP
564 r = pidref_kill(&parent, SIGUSR2);
565 if (r == -ESRCH)
566 return log_error_errno(r, "Parent already died?");
567 if (r < 0)
568 return log_error_errno(r, "Failed to send SIGUSR2 signal to parent: %m");
d093b62c
LP
569 }
570 }
571
572 (void) process_connection(server, TAKE_FD(fd));
573 last_busy_usec = USEC_INFINITY;
574 }
575
576 return 0;
577}
578
579DEFINE_MAIN_FUNCTION(run);