]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-user.c
Merge pull request #10976 from yuwata/typesafe-netlink-call
[thirdparty/systemd.git] / src / login / logind-user.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <stdio_ext.h>
7
8 #include "alloc-util.h"
9 #include "bus-common-errors.h"
10 #include "bus-error.h"
11 #include "bus-util.h"
12 #include "cgroup-util.h"
13 #include "clean-ipc.h"
14 #include "env-file.h"
15 #include "escape.h"
16 #include "fd-util.h"
17 #include "fileio.h"
18 #include "format-util.h"
19 #include "fs-util.h"
20 #include "hashmap.h"
21 #include "label.h"
22 #include "logind-user.h"
23 #include "mkdir.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 #include "rm-rf.h"
27 #include "serialize.h"
28 #include "special.h"
29 #include "stdio-util.h"
30 #include "string-table.h"
31 #include "strv.h"
32 #include "tmpfile-util.h"
33 #include "unit-name.h"
34 #include "user-util.h"
35 #include "util.h"
36
37 int user_new(User **ret,
38 Manager *m,
39 uid_t uid,
40 gid_t gid,
41 const char *name,
42 const char *home) {
43
44 _cleanup_(user_freep) User *u = NULL;
45 char lu[DECIMAL_STR_MAX(uid_t) + 1];
46 int r;
47
48 assert(ret);
49 assert(m);
50 assert(name);
51
52 u = new(User, 1);
53 if (!u)
54 return -ENOMEM;
55
56 *u = (User) {
57 .manager = m,
58 .uid = uid,
59 .gid = gid,
60 .last_session_timestamp = USEC_INFINITY,
61 };
62
63 u->name = strdup(name);
64 if (!u->name)
65 return -ENOMEM;
66
67 u->home = strdup(home);
68 if (!u->home)
69 return -ENOMEM;
70
71 if (asprintf(&u->state_file, "/run/systemd/users/"UID_FMT, uid) < 0)
72 return -ENOMEM;
73
74 if (asprintf(&u->runtime_path, "/run/user/"UID_FMT, uid) < 0)
75 return -ENOMEM;
76
77 xsprintf(lu, UID_FMT, uid);
78 r = slice_build_subslice(SPECIAL_USER_SLICE, lu, &u->slice);
79 if (r < 0)
80 return r;
81
82 r = unit_name_build("user", lu, ".service", &u->service);
83 if (r < 0)
84 return r;
85
86 r = unit_name_build("user-runtime-dir", lu, ".service", &u->runtime_dir_service);
87 if (r < 0)
88 return r;
89
90 r = hashmap_put(m->users, UID_TO_PTR(uid), u);
91 if (r < 0)
92 return r;
93
94 r = hashmap_put(m->user_units, u->slice, u);
95 if (r < 0)
96 return r;
97
98 r = hashmap_put(m->user_units, u->service, u);
99 if (r < 0)
100 return r;
101
102 r = hashmap_put(m->user_units, u->runtime_dir_service, u);
103 if (r < 0)
104 return r;
105
106 *ret = TAKE_PTR(u);
107 return 0;
108 }
109
110 User *user_free(User *u) {
111 if (!u)
112 return NULL;
113
114 if (u->in_gc_queue)
115 LIST_REMOVE(gc_queue, u->manager->user_gc_queue, u);
116
117 while (u->sessions)
118 session_free(u->sessions);
119
120 if (u->service)
121 hashmap_remove_value(u->manager->user_units, u->service, u);
122
123 if (u->runtime_dir_service)
124 hashmap_remove_value(u->manager->user_units, u->runtime_dir_service, u);
125
126 if (u->slice)
127 hashmap_remove_value(u->manager->user_units, u->slice, u);
128
129 hashmap_remove_value(u->manager->users, UID_TO_PTR(u->uid), u);
130
131 (void) sd_event_source_unref(u->timer_event_source);
132
133 u->service_job = mfree(u->service_job);
134
135 u->service = mfree(u->service);
136 u->runtime_dir_service = mfree(u->runtime_dir_service);
137 u->slice = mfree(u->slice);
138 u->runtime_path = mfree(u->runtime_path);
139 u->state_file = mfree(u->state_file);
140 u->name = mfree(u->name);
141 u->home = mfree(u->home);
142
143 return mfree(u);
144 }
145
146 static int user_save_internal(User *u) {
147 _cleanup_free_ char *temp_path = NULL;
148 _cleanup_fclose_ FILE *f = NULL;
149 int r;
150
151 assert(u);
152 assert(u->state_file);
153
154 r = mkdir_safe_label("/run/systemd/users", 0755, 0, 0, MKDIR_WARN_MODE);
155 if (r < 0)
156 goto fail;
157
158 r = fopen_temporary(u->state_file, &f, &temp_path);
159 if (r < 0)
160 goto fail;
161
162 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
163 (void) fchmod(fileno(f), 0644);
164
165 fprintf(f,
166 "# This is private data. Do not parse.\n"
167 "NAME=%s\n"
168 "STATE=%s\n" /* friendly user-facing state */
169 "STOPPING=%s\n", /* low-level state */
170 u->name,
171 user_state_to_string(user_get_state(u)),
172 yes_no(u->stopping));
173
174 /* LEGACY: no-one reads RUNTIME= anymore, drop it at some point */
175 if (u->runtime_path)
176 fprintf(f, "RUNTIME=%s\n", u->runtime_path);
177
178 if (u->service_job)
179 fprintf(f, "SERVICE_JOB=%s\n", u->service_job);
180
181 if (u->display)
182 fprintf(f, "DISPLAY=%s\n", u->display->id);
183
184 if (dual_timestamp_is_set(&u->timestamp))
185 fprintf(f,
186 "REALTIME="USEC_FMT"\n"
187 "MONOTONIC="USEC_FMT"\n",
188 u->timestamp.realtime,
189 u->timestamp.monotonic);
190
191 if (u->last_session_timestamp != USEC_INFINITY)
192 fprintf(f, "LAST_SESSION_TIMESTAMP=" USEC_FMT "\n",
193 u->last_session_timestamp);
194
195 if (u->sessions) {
196 Session *i;
197 bool first;
198
199 fputs("SESSIONS=", f);
200 first = true;
201 LIST_FOREACH(sessions_by_user, i, u->sessions) {
202 if (first)
203 first = false;
204 else
205 fputc(' ', f);
206
207 fputs(i->id, f);
208 }
209
210 fputs("\nSEATS=", f);
211 first = true;
212 LIST_FOREACH(sessions_by_user, i, u->sessions) {
213 if (!i->seat)
214 continue;
215
216 if (first)
217 first = false;
218 else
219 fputc(' ', f);
220
221 fputs(i->seat->id, f);
222 }
223
224 fputs("\nACTIVE_SESSIONS=", f);
225 first = true;
226 LIST_FOREACH(sessions_by_user, i, u->sessions) {
227 if (!session_is_active(i))
228 continue;
229
230 if (first)
231 first = false;
232 else
233 fputc(' ', f);
234
235 fputs(i->id, f);
236 }
237
238 fputs("\nONLINE_SESSIONS=", f);
239 first = true;
240 LIST_FOREACH(sessions_by_user, i, u->sessions) {
241 if (session_get_state(i) == SESSION_CLOSING)
242 continue;
243
244 if (first)
245 first = false;
246 else
247 fputc(' ', f);
248
249 fputs(i->id, f);
250 }
251
252 fputs("\nACTIVE_SEATS=", f);
253 first = true;
254 LIST_FOREACH(sessions_by_user, i, u->sessions) {
255 if (!session_is_active(i) || !i->seat)
256 continue;
257
258 if (first)
259 first = false;
260 else
261 fputc(' ', f);
262
263 fputs(i->seat->id, f);
264 }
265
266 fputs("\nONLINE_SEATS=", f);
267 first = true;
268 LIST_FOREACH(sessions_by_user, i, u->sessions) {
269 if (session_get_state(i) == SESSION_CLOSING || !i->seat)
270 continue;
271
272 if (first)
273 first = false;
274 else
275 fputc(' ', f);
276
277 fputs(i->seat->id, f);
278 }
279 fputc('\n', f);
280 }
281
282 r = fflush_and_check(f);
283 if (r < 0)
284 goto fail;
285
286 if (rename(temp_path, u->state_file) < 0) {
287 r = -errno;
288 goto fail;
289 }
290
291 return 0;
292
293 fail:
294 (void) unlink(u->state_file);
295
296 if (temp_path)
297 (void) unlink(temp_path);
298
299 return log_error_errno(r, "Failed to save user data %s: %m", u->state_file);
300 }
301
302 int user_save(User *u) {
303 assert(u);
304
305 if (!u->started)
306 return 0;
307
308 return user_save_internal(u);
309 }
310
311 int user_load(User *u) {
312 _cleanup_free_ char *realtime = NULL, *monotonic = NULL, *stopping = NULL, *last_session_timestamp = NULL;
313 int r;
314
315 assert(u);
316
317 r = parse_env_file(NULL, u->state_file,
318 "SERVICE_JOB", &u->service_job,
319 "STOPPING", &stopping,
320 "REALTIME", &realtime,
321 "MONOTONIC", &monotonic,
322 "LAST_SESSION_TIMESTAMP", &last_session_timestamp);
323 if (r == -ENOENT)
324 return 0;
325 if (r < 0)
326 return log_error_errno(r, "Failed to read %s: %m", u->state_file);
327
328 if (stopping) {
329 r = parse_boolean(stopping);
330 if (r < 0)
331 log_debug_errno(r, "Failed to parse 'STOPPING' boolean: %s", stopping);
332 else
333 u->stopping = r;
334 }
335
336 if (realtime)
337 (void) deserialize_usec(realtime, &u->timestamp.realtime);
338 if (monotonic)
339 (void) deserialize_usec(monotonic, &u->timestamp.monotonic);
340 if (last_session_timestamp)
341 (void) deserialize_usec(last_session_timestamp, &u->last_session_timestamp);
342
343 return 0;
344 }
345
346 static void user_start_service(User *u) {
347 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
348 int r;
349
350 assert(u);
351
352 /* Start the service containing the "systemd --user" instance (user@.service). Note that we don't explicitly
353 * start the per-user slice or the systemd-runtime-dir@.service instance, as those are pulled in both by
354 * user@.service and the session scopes as dependencies. */
355
356 u->service_job = mfree(u->service_job);
357
358 r = manager_start_unit(u->manager, u->service, &error, &u->service_job);
359 if (r < 0)
360 log_warning_errno(r, "Failed to start user service '%s', ignoring: %s", u->service, bus_error_message(&error, r));
361 }
362
363 int user_start(User *u) {
364 assert(u);
365
366 if (u->started && !u->stopping)
367 return 0;
368
369 /* If u->stopping is set, the user is marked for removal and service stop-jobs are queued. We have to clear
370 * that flag before queing the start-jobs again. If they succeed, the user object can be re-used just fine
371 * (pid1 takes care of job-ordering and proper restart), but if they fail, we want to force another user_stop()
372 * so possibly pending units are stopped. */
373 u->stopping = false;
374
375 if (!u->started)
376 log_debug("Starting services for new user %s.", u->name);
377
378 /* Save the user data so far, because pam_systemd will read the XDG_RUNTIME_DIR out of it while starting up
379 * systemd --user. We need to do user_save_internal() because we have not "officially" started yet. */
380 user_save_internal(u);
381
382 /* Start user@UID.service */
383 user_start_service(u);
384
385 if (!u->started) {
386 if (!dual_timestamp_is_set(&u->timestamp))
387 dual_timestamp_get(&u->timestamp);
388 user_send_signal(u, true);
389 u->started = true;
390 }
391
392 /* Save new user data */
393 user_save(u);
394
395 return 0;
396 }
397
398 static void user_stop_service(User *u) {
399 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
400 int r;
401
402 assert(u);
403 assert(u->service);
404
405 /* The reverse of user_start_service(). Note that we only stop user@UID.service here, and let StopWhenUnneeded=
406 * deal with the slice and the user-runtime-dir@.service instance. */
407
408 u->service_job = mfree(u->service_job);
409
410 r = manager_stop_unit(u->manager, u->service, &error, &u->service_job);
411 if (r < 0)
412 log_warning_errno(r, "Failed to stop user service '%s', ignoring: %s", u->service, bus_error_message(&error, r));
413 }
414
415 int user_stop(User *u, bool force) {
416 Session *s;
417 int r = 0;
418 assert(u);
419
420 /* This is called whenever we begin with tearing down a user record. It's called in two cases: explicit API
421 * request to do so via the bus (in which case 'force' is true) and automatically due to GC, if there's no
422 * session left pinning it (in which case 'force' is false). Note that this just initiates tearing down of the
423 * user, the User object will remain in memory until user_finalize() is called, see below. */
424
425 if (!u->started)
426 return 0;
427
428 if (u->stopping) { /* Stop jobs have already been queued */
429 user_save(u);
430 return 0;
431 }
432
433 LIST_FOREACH(sessions_by_user, s, u->sessions) {
434 int k;
435
436 k = session_stop(s, force);
437 if (k < 0)
438 r = k;
439 }
440
441 user_stop_service(u);
442
443 u->stopping = true;
444
445 user_save(u);
446
447 return r;
448 }
449
450 int user_finalize(User *u) {
451 Session *s;
452 int r = 0, k;
453
454 assert(u);
455
456 /* Called when the user is really ready to be freed, i.e. when all unit stop jobs and suchlike for it are
457 * done. This is called as a result of an earlier user_done() when all jobs are completed. */
458
459 if (u->started)
460 log_debug("User %s logged out.", u->name);
461
462 LIST_FOREACH(sessions_by_user, s, u->sessions) {
463 k = session_finalize(s);
464 if (k < 0)
465 r = k;
466 }
467
468 /* Clean SysV + POSIX IPC objects, but only if this is not a system user. Background: in many setups cronjobs
469 * are run in full PAM and thus logind sessions, even if the code run doesn't belong to actual users but to
470 * system components. Since enable RemoveIPC= globally for all users, we need to be a bit careful with such
471 * cases, as we shouldn't accidentally remove a system service's IPC objects while it is running, just because
472 * a cronjob running as the same user just finished. Hence: exclude system users generally from IPC clean-up,
473 * and do it only for normal users. */
474 if (u->manager->remove_ipc && !uid_is_system(u->uid)) {
475 k = clean_ipc_by_uid(u->uid);
476 if (k < 0)
477 r = k;
478 }
479
480 (void) unlink(u->state_file);
481 user_add_to_gc_queue(u);
482
483 if (u->started) {
484 user_send_signal(u, false);
485 u->started = false;
486 }
487
488 return r;
489 }
490
491 int user_get_idle_hint(User *u, dual_timestamp *t) {
492 Session *s;
493 bool idle_hint = true;
494 dual_timestamp ts = DUAL_TIMESTAMP_NULL;
495
496 assert(u);
497
498 LIST_FOREACH(sessions_by_user, s, u->sessions) {
499 dual_timestamp k;
500 int ih;
501
502 ih = session_get_idle_hint(s, &k);
503 if (ih < 0)
504 return ih;
505
506 if (!ih) {
507 if (!idle_hint) {
508 if (k.monotonic < ts.monotonic)
509 ts = k;
510 } else {
511 idle_hint = false;
512 ts = k;
513 }
514 } else if (idle_hint) {
515
516 if (k.monotonic > ts.monotonic)
517 ts = k;
518 }
519 }
520
521 if (t)
522 *t = ts;
523
524 return idle_hint;
525 }
526
527 int user_check_linger_file(User *u) {
528 _cleanup_free_ char *cc = NULL;
529 char *p = NULL;
530
531 cc = cescape(u->name);
532 if (!cc)
533 return -ENOMEM;
534
535 p = strjoina("/var/lib/systemd/linger/", cc);
536 if (access(p, F_OK) < 0) {
537 if (errno != ENOENT)
538 return -errno;
539
540 return false;
541 }
542
543 return true;
544 }
545
546 static bool user_unit_active(User *u) {
547 const char *i;
548 int r;
549
550 assert(u->service);
551 assert(u->runtime_dir_service);
552 assert(u->slice);
553
554 FOREACH_STRING(i, u->service, u->runtime_dir_service, u->slice) {
555 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
556
557 r = manager_unit_is_active(u->manager, i, &error);
558 if (r < 0)
559 log_debug_errno(r, "Failed to determine whether unit '%s' is active, ignoring: %s", u->service, bus_error_message(&error, r));
560 if (r != 0)
561 return true;
562 }
563
564 return false;
565 }
566
567 bool user_may_gc(User *u, bool drop_not_started) {
568 int r;
569
570 assert(u);
571
572 if (drop_not_started && !u->started)
573 return true;
574
575 if (u->sessions)
576 return false;
577
578 if (u->last_session_timestamp != USEC_INFINITY) {
579 /* All sessions have been closed. Let's see if we shall leave the user record around for a bit */
580
581 if (u->manager->user_stop_delay == USEC_INFINITY)
582 return false; /* Leave it around forever! */
583 if (u->manager->user_stop_delay > 0 &&
584 now(CLOCK_MONOTONIC) < usec_add(u->last_session_timestamp, u->manager->user_stop_delay))
585 return false; /* Leave it around for a bit longer. */
586 }
587
588 /* Is this a user that shall stay around forever ("linger")? Before we say "no" to GC'ing for lingering users, let's check
589 * if any of the three units that we maintain for this user is still around. If none of them is,
590 * there's no need to keep this user around even if lingering is enabled. */
591 if (user_check_linger_file(u) > 0 && user_unit_active(u))
592 return false;
593
594 /* Check if our job is still pending */
595 if (u->service_job) {
596 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
597
598 r = manager_job_is_active(u->manager, u->service_job, &error);
599 if (r < 0)
600 log_debug_errno(r, "Failed to determine whether job '%s' is pending, ignoring: %s", u->service_job, bus_error_message(&error, r));
601 if (r != 0)
602 return false;
603 }
604
605 /* Note that we don't care if the three units we manage for each user object are up or not, as we are managing
606 * their state rather than tracking it. */
607
608 return true;
609 }
610
611 void user_add_to_gc_queue(User *u) {
612 assert(u);
613
614 if (u->in_gc_queue)
615 return;
616
617 LIST_PREPEND(gc_queue, u->manager->user_gc_queue, u);
618 u->in_gc_queue = true;
619 }
620
621 UserState user_get_state(User *u) {
622 Session *i;
623
624 assert(u);
625
626 if (u->stopping)
627 return USER_CLOSING;
628
629 if (!u->started || u->service_job)
630 return USER_OPENING;
631
632 if (u->sessions) {
633 bool all_closing = true;
634
635 LIST_FOREACH(sessions_by_user, i, u->sessions) {
636 SessionState state;
637
638 state = session_get_state(i);
639 if (state == SESSION_ACTIVE)
640 return USER_ACTIVE;
641 if (state != SESSION_CLOSING)
642 all_closing = false;
643 }
644
645 return all_closing ? USER_CLOSING : USER_ONLINE;
646 }
647
648 if (user_check_linger_file(u) > 0 && user_unit_active(u))
649 return USER_LINGERING;
650
651 return USER_CLOSING;
652 }
653
654 int user_kill(User *u, int signo) {
655 assert(u);
656
657 return manager_kill_unit(u->manager, u->slice, KILL_ALL, signo, NULL);
658 }
659
660 static bool elect_display_filter(Session *s) {
661 /* Return true if the session is a candidate for the user’s ‘primary session’ or ‘display’. */
662 assert(s);
663
664 return s->class == SESSION_USER && s->started && !s->stopping;
665 }
666
667 static int elect_display_compare(Session *s1, Session *s2) {
668 /* Indexed by SessionType. Lower numbers mean more preferred. */
669 const int type_ranks[_SESSION_TYPE_MAX] = {
670 [SESSION_UNSPECIFIED] = 0,
671 [SESSION_TTY] = -2,
672 [SESSION_X11] = -3,
673 [SESSION_WAYLAND] = -3,
674 [SESSION_MIR] = -3,
675 [SESSION_WEB] = -1,
676 };
677
678 /* Calculate the partial order relationship between s1 and s2,
679 * returning < 0 if s1 is preferred as the user’s ‘primary session’,
680 * 0 if s1 and s2 are equally preferred or incomparable, or > 0 if s2
681 * is preferred.
682 *
683 * s1 or s2 may be NULL. */
684 if (!s1 && !s2)
685 return 0;
686
687 if ((s1 == NULL) != (s2 == NULL))
688 return (s1 == NULL) - (s2 == NULL);
689
690 if (s1->stopping != s2->stopping)
691 return s1->stopping - s2->stopping;
692
693 if ((s1->class != SESSION_USER) != (s2->class != SESSION_USER))
694 return (s1->class != SESSION_USER) - (s2->class != SESSION_USER);
695
696 if ((s1->type == _SESSION_TYPE_INVALID) != (s2->type == _SESSION_TYPE_INVALID))
697 return (s1->type == _SESSION_TYPE_INVALID) - (s2->type == _SESSION_TYPE_INVALID);
698
699 if (s1->type != s2->type)
700 return type_ranks[s1->type] - type_ranks[s2->type];
701
702 return 0;
703 }
704
705 void user_elect_display(User *u) {
706 Session *s;
707
708 assert(u);
709
710 /* This elects a primary session for each user, which we call the "display". We try to keep the assignment
711 * stable, but we "upgrade" to better choices. */
712 log_debug("Electing new display for user %s", u->name);
713
714 LIST_FOREACH(sessions_by_user, s, u->sessions) {
715 if (!elect_display_filter(s)) {
716 log_debug("Ignoring session %s", s->id);
717 continue;
718 }
719
720 if (elect_display_compare(s, u->display) < 0) {
721 log_debug("Choosing session %s in preference to %s", s->id, u->display ? u->display->id : "-");
722 u->display = s;
723 }
724 }
725 }
726
727 static int user_stop_timeout_callback(sd_event_source *es, uint64_t usec, void *userdata) {
728 User *u = userdata;
729
730 assert(u);
731 user_add_to_gc_queue(u);
732
733 return 0;
734 }
735
736 void user_update_last_session_timer(User *u) {
737 int r;
738
739 assert(u);
740
741 if (u->sessions) {
742 /* There are sessions, turn off the timer */
743 u->last_session_timestamp = USEC_INFINITY;
744 u->timer_event_source = sd_event_source_unref(u->timer_event_source);
745 return;
746 }
747
748 if (u->last_session_timestamp != USEC_INFINITY)
749 return; /* Timer already started */
750
751 u->last_session_timestamp = now(CLOCK_MONOTONIC);
752
753 assert(!u->timer_event_source);
754
755 if (u->manager->user_stop_delay == 0 || u->manager->user_stop_delay == USEC_INFINITY)
756 return;
757
758 if (sd_event_get_state(u->manager->event) == SD_EVENT_FINISHED) {
759 log_debug("Not allocating user stop timeout, since we are already exiting.");
760 return;
761 }
762
763 r = sd_event_add_time(u->manager->event,
764 &u->timer_event_source,
765 CLOCK_MONOTONIC,
766 usec_add(u->last_session_timestamp, u->manager->user_stop_delay), 0,
767 user_stop_timeout_callback, u);
768 if (r < 0)
769 log_warning_errno(r, "Failed to enqueue user stop event source, ignoring: %m");
770
771 if (DEBUG_LOGGING) {
772 char s[FORMAT_TIMESPAN_MAX];
773
774 log_debug("Last session of user '%s' logged out, terminating user context in %s.",
775 u->name,
776 format_timespan(s, sizeof(s), u->manager->user_stop_delay, USEC_PER_MSEC));
777 }
778 }
779
780 static const char* const user_state_table[_USER_STATE_MAX] = {
781 [USER_OFFLINE] = "offline",
782 [USER_OPENING] = "opening",
783 [USER_LINGERING] = "lingering",
784 [USER_ONLINE] = "online",
785 [USER_ACTIVE] = "active",
786 [USER_CLOSING] = "closing"
787 };
788
789 DEFINE_STRING_TABLE_LOOKUP(user_state, UserState);
790
791 int config_parse_tmpfs_size(
792 const char* unit,
793 const char *filename,
794 unsigned line,
795 const char *section,
796 unsigned section_line,
797 const char *lvalue,
798 int ltype,
799 const char *rvalue,
800 void *data,
801 void *userdata) {
802
803 uint64_t *sz = data;
804 int r;
805
806 assert(filename);
807 assert(lvalue);
808 assert(rvalue);
809 assert(data);
810
811 /* First, try to parse as percentage */
812 r = parse_permille(rvalue);
813 if (r > 0 && r < 1000)
814 *sz = physical_memory_scale(r, 1000U);
815 else {
816 uint64_t k;
817
818 /* If the passed argument was not a percentage, or out of range, parse as byte size */
819
820 r = parse_size(rvalue, 1024, &k);
821 if (r >= 0 && (k <= 0 || (uint64_t) (size_t) k != k))
822 r = -ERANGE;
823 if (r < 0) {
824 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value '%s', ignoring: %m", rvalue);
825 return 0;
826 }
827
828 *sz = PAGE_ALIGN((size_t) k);
829 }
830
831 return 0;
832 }
833
834 int config_parse_compat_user_tasks_max(
835 const char *unit,
836 const char *filename,
837 unsigned line,
838 const char *section,
839 unsigned section_line,
840 const char *lvalue,
841 int ltype,
842 const char *rvalue,
843 void *data,
844 void *userdata) {
845
846 assert(filename);
847 assert(lvalue);
848 assert(rvalue);
849 assert(data);
850
851 log_syntax(unit, LOG_NOTICE, filename, line, 0,
852 "Support for option %s= has been removed.",
853 lvalue);
854 log_info("Hint: try creating /etc/systemd/system/user-.slice.d/50-limits.conf with:\n"
855 " [Slice]\n"
856 " TasksMax=%s",
857 rvalue);
858 return 0;
859 }