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