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