]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-user.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[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_full_errno(sd_bus_error_has_name(&error, BUS_ERROR_UNIT_MASKED) ? LOG_DEBUG : LOG_WARNING, r,
361 "Failed to start user service '%s', ignoring: %s", u->service, bus_error_message(&error, r));
362 }
363
364 int user_start(User *u) {
365 assert(u);
366
367 if (u->started && !u->stopping)
368 return 0;
369
370 /* If u->stopping is set, the user is marked for removal and service stop-jobs are queued. We have to clear
371 * that flag before queing the start-jobs again. If they succeed, the user object can be re-used just fine
372 * (pid1 takes care of job-ordering and proper restart), but if they fail, we want to force another user_stop()
373 * so possibly pending units are stopped. */
374 u->stopping = false;
375
376 if (!u->started)
377 log_debug("Starting services for new user %s.", u->name);
378
379 /* Save the user data so far, because pam_systemd will read the XDG_RUNTIME_DIR out of it while starting up
380 * systemd --user. We need to do user_save_internal() because we have not "officially" started yet. */
381 user_save_internal(u);
382
383 /* Start user@UID.service */
384 user_start_service(u);
385
386 if (!u->started) {
387 if (!dual_timestamp_is_set(&u->timestamp))
388 dual_timestamp_get(&u->timestamp);
389 user_send_signal(u, true);
390 u->started = true;
391 }
392
393 /* Save new user data */
394 user_save(u);
395
396 return 0;
397 }
398
399 static void user_stop_service(User *u) {
400 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
401 int r;
402
403 assert(u);
404 assert(u->service);
405
406 /* The reverse of user_start_service(). Note that we only stop user@UID.service here, and let StopWhenUnneeded=
407 * deal with the slice and the user-runtime-dir@.service instance. */
408
409 u->service_job = mfree(u->service_job);
410
411 r = manager_stop_unit(u->manager, u->service, &error, &u->service_job);
412 if (r < 0)
413 log_warning_errno(r, "Failed to stop user service '%s', ignoring: %s", u->service, bus_error_message(&error, r));
414 }
415
416 int user_stop(User *u, bool force) {
417 Session *s;
418 int r = 0;
419 assert(u);
420
421 /* This is called whenever we begin with tearing down a user record. It's called in two cases: explicit API
422 * request to do so via the bus (in which case 'force' is true) and automatically due to GC, if there's no
423 * session left pinning it (in which case 'force' is false). Note that this just initiates tearing down of the
424 * user, the User object will remain in memory until user_finalize() is called, see below. */
425
426 if (!u->started)
427 return 0;
428
429 if (u->stopping) { /* Stop jobs have already been queued */
430 user_save(u);
431 return 0;
432 }
433
434 LIST_FOREACH(sessions_by_user, s, u->sessions) {
435 int k;
436
437 k = session_stop(s, force);
438 if (k < 0)
439 r = k;
440 }
441
442 user_stop_service(u);
443
444 u->stopping = true;
445
446 user_save(u);
447
448 return r;
449 }
450
451 int user_finalize(User *u) {
452 Session *s;
453 int r = 0, k;
454
455 assert(u);
456
457 /* Called when the user is really ready to be freed, i.e. when all unit stop jobs and suchlike for it are
458 * done. This is called as a result of an earlier user_done() when all jobs are completed. */
459
460 if (u->started)
461 log_debug("User %s logged out.", u->name);
462
463 LIST_FOREACH(sessions_by_user, s, u->sessions) {
464 k = session_finalize(s);
465 if (k < 0)
466 r = k;
467 }
468
469 /* Clean SysV + POSIX IPC objects, but only if this is not a system user. Background: in many setups cronjobs
470 * are run in full PAM and thus logind sessions, even if the code run doesn't belong to actual users but to
471 * system components. Since enable RemoveIPC= globally for all users, we need to be a bit careful with such
472 * cases, as we shouldn't accidentally remove a system service's IPC objects while it is running, just because
473 * a cronjob running as the same user just finished. Hence: exclude system users generally from IPC clean-up,
474 * and do it only for normal users. */
475 if (u->manager->remove_ipc && !uid_is_system(u->uid)) {
476 k = clean_ipc_by_uid(u->uid);
477 if (k < 0)
478 r = k;
479 }
480
481 (void) unlink(u->state_file);
482 user_add_to_gc_queue(u);
483
484 if (u->started) {
485 user_send_signal(u, false);
486 u->started = false;
487 }
488
489 return r;
490 }
491
492 int user_get_idle_hint(User *u, dual_timestamp *t) {
493 Session *s;
494 bool idle_hint = true;
495 dual_timestamp ts = DUAL_TIMESTAMP_NULL;
496
497 assert(u);
498
499 LIST_FOREACH(sessions_by_user, s, u->sessions) {
500 dual_timestamp k;
501 int ih;
502
503 ih = session_get_idle_hint(s, &k);
504 if (ih < 0)
505 return ih;
506
507 if (!ih) {
508 if (!idle_hint) {
509 if (k.monotonic < ts.monotonic)
510 ts = k;
511 } else {
512 idle_hint = false;
513 ts = k;
514 }
515 } else if (idle_hint) {
516
517 if (k.monotonic > ts.monotonic)
518 ts = k;
519 }
520 }
521
522 if (t)
523 *t = ts;
524
525 return idle_hint;
526 }
527
528 int user_check_linger_file(User *u) {
529 _cleanup_free_ char *cc = NULL;
530 char *p = NULL;
531
532 cc = cescape(u->name);
533 if (!cc)
534 return -ENOMEM;
535
536 p = strjoina("/var/lib/systemd/linger/", cc);
537 if (access(p, F_OK) < 0) {
538 if (errno != ENOENT)
539 return -errno;
540
541 return false;
542 }
543
544 return true;
545 }
546
547 static bool user_unit_active(User *u) {
548 const char *i;
549 int r;
550
551 assert(u->service);
552 assert(u->runtime_dir_service);
553 assert(u->slice);
554
555 FOREACH_STRING(i, u->service, u->runtime_dir_service, u->slice) {
556 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
557
558 r = manager_unit_is_active(u->manager, i, &error);
559 if (r < 0)
560 log_debug_errno(r, "Failed to determine whether unit '%s' is active, ignoring: %s", u->service, bus_error_message(&error, r));
561 if (r != 0)
562 return true;
563 }
564
565 return false;
566 }
567
568 bool user_may_gc(User *u, bool drop_not_started) {
569 int r;
570
571 assert(u);
572
573 if (drop_not_started && !u->started)
574 return true;
575
576 if (u->sessions)
577 return false;
578
579 if (u->last_session_timestamp != USEC_INFINITY) {
580 /* All sessions have been closed. Let's see if we shall leave the user record around for a bit */
581
582 if (u->manager->user_stop_delay == USEC_INFINITY)
583 return false; /* Leave it around forever! */
584 if (u->manager->user_stop_delay > 0 &&
585 now(CLOCK_MONOTONIC) < usec_add(u->last_session_timestamp, u->manager->user_stop_delay))
586 return false; /* Leave it around for a bit longer. */
587 }
588
589 /* Is this a user that shall stay around forever ("linger")? Before we say "no" to GC'ing for lingering users, let's check
590 * if any of the three units that we maintain for this user is still around. If none of them is,
591 * there's no need to keep this user around even if lingering is enabled. */
592 if (user_check_linger_file(u) > 0 && user_unit_active(u))
593 return false;
594
595 /* Check if our job is still pending */
596 if (u->service_job) {
597 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
598
599 r = manager_job_is_active(u->manager, u->service_job, &error);
600 if (r < 0)
601 log_debug_errno(r, "Failed to determine whether job '%s' is pending, ignoring: %s", u->service_job, bus_error_message(&error, r));
602 if (r != 0)
603 return false;
604 }
605
606 /* Note that we don't care if the three units we manage for each user object are up or not, as we are managing
607 * their state rather than tracking it. */
608
609 return true;
610 }
611
612 void user_add_to_gc_queue(User *u) {
613 assert(u);
614
615 if (u->in_gc_queue)
616 return;
617
618 LIST_PREPEND(gc_queue, u->manager->user_gc_queue, u);
619 u->in_gc_queue = true;
620 }
621
622 UserState user_get_state(User *u) {
623 Session *i;
624
625 assert(u);
626
627 if (u->stopping)
628 return USER_CLOSING;
629
630 if (!u->started || u->service_job)
631 return USER_OPENING;
632
633 if (u->sessions) {
634 bool all_closing = true;
635
636 LIST_FOREACH(sessions_by_user, i, u->sessions) {
637 SessionState state;
638
639 state = session_get_state(i);
640 if (state == SESSION_ACTIVE)
641 return USER_ACTIVE;
642 if (state != SESSION_CLOSING)
643 all_closing = false;
644 }
645
646 return all_closing ? USER_CLOSING : USER_ONLINE;
647 }
648
649 if (user_check_linger_file(u) > 0 && user_unit_active(u))
650 return USER_LINGERING;
651
652 return USER_CLOSING;
653 }
654
655 int user_kill(User *u, int signo) {
656 assert(u);
657
658 return manager_kill_unit(u->manager, u->slice, KILL_ALL, signo, NULL);
659 }
660
661 static bool elect_display_filter(Session *s) {
662 /* Return true if the session is a candidate for the user’s ‘primary session’ or ‘display’. */
663 assert(s);
664
665 return s->class == SESSION_USER && s->started && !s->stopping;
666 }
667
668 static int elect_display_compare(Session *s1, Session *s2) {
669 /* Indexed by SessionType. Lower numbers mean more preferred. */
670 const int type_ranks[_SESSION_TYPE_MAX] = {
671 [SESSION_UNSPECIFIED] = 0,
672 [SESSION_TTY] = -2,
673 [SESSION_X11] = -3,
674 [SESSION_WAYLAND] = -3,
675 [SESSION_MIR] = -3,
676 [SESSION_WEB] = -1,
677 };
678
679 /* Calculate the partial order relationship between s1 and s2,
680 * returning < 0 if s1 is preferred as the user’s ‘primary session’,
681 * 0 if s1 and s2 are equally preferred or incomparable, or > 0 if s2
682 * is preferred.
683 *
684 * s1 or s2 may be NULL. */
685 if (!s1 && !s2)
686 return 0;
687
688 if ((s1 == NULL) != (s2 == NULL))
689 return (s1 == NULL) - (s2 == NULL);
690
691 if (s1->stopping != s2->stopping)
692 return s1->stopping - s2->stopping;
693
694 if ((s1->class != SESSION_USER) != (s2->class != SESSION_USER))
695 return (s1->class != SESSION_USER) - (s2->class != SESSION_USER);
696
697 if ((s1->type == _SESSION_TYPE_INVALID) != (s2->type == _SESSION_TYPE_INVALID))
698 return (s1->type == _SESSION_TYPE_INVALID) - (s2->type == _SESSION_TYPE_INVALID);
699
700 if (s1->type != s2->type)
701 return type_ranks[s1->type] - type_ranks[s2->type];
702
703 return 0;
704 }
705
706 void user_elect_display(User *u) {
707 Session *s;
708
709 assert(u);
710
711 /* This elects a primary session for each user, which we call the "display". We try to keep the assignment
712 * stable, but we "upgrade" to better choices. */
713 log_debug("Electing new display for user %s", u->name);
714
715 LIST_FOREACH(sessions_by_user, s, u->sessions) {
716 if (!elect_display_filter(s)) {
717 log_debug("Ignoring session %s", s->id);
718 continue;
719 }
720
721 if (elect_display_compare(s, u->display) < 0) {
722 log_debug("Choosing session %s in preference to %s", s->id, u->display ? u->display->id : "-");
723 u->display = s;
724 }
725 }
726 }
727
728 static int user_stop_timeout_callback(sd_event_source *es, uint64_t usec, void *userdata) {
729 User *u = userdata;
730
731 assert(u);
732 user_add_to_gc_queue(u);
733
734 return 0;
735 }
736
737 void user_update_last_session_timer(User *u) {
738 int r;
739
740 assert(u);
741
742 if (u->sessions) {
743 /* There are sessions, turn off the timer */
744 u->last_session_timestamp = USEC_INFINITY;
745 u->timer_event_source = sd_event_source_unref(u->timer_event_source);
746 return;
747 }
748
749 if (u->last_session_timestamp != USEC_INFINITY)
750 return; /* Timer already started */
751
752 u->last_session_timestamp = now(CLOCK_MONOTONIC);
753
754 assert(!u->timer_event_source);
755
756 if (u->manager->user_stop_delay == 0 || u->manager->user_stop_delay == USEC_INFINITY)
757 return;
758
759 if (sd_event_get_state(u->manager->event) == SD_EVENT_FINISHED) {
760 log_debug("Not allocating user stop timeout, since we are already exiting.");
761 return;
762 }
763
764 r = sd_event_add_time(u->manager->event,
765 &u->timer_event_source,
766 CLOCK_MONOTONIC,
767 usec_add(u->last_session_timestamp, u->manager->user_stop_delay), 0,
768 user_stop_timeout_callback, u);
769 if (r < 0)
770 log_warning_errno(r, "Failed to enqueue user stop event source, ignoring: %m");
771
772 if (DEBUG_LOGGING) {
773 char s[FORMAT_TIMESPAN_MAX];
774
775 log_debug("Last session of user '%s' logged out, terminating user context in %s.",
776 u->name,
777 format_timespan(s, sizeof(s), u->manager->user_stop_delay, USEC_PER_MSEC));
778 }
779 }
780
781 static const char* const user_state_table[_USER_STATE_MAX] = {
782 [USER_OFFLINE] = "offline",
783 [USER_OPENING] = "opening",
784 [USER_LINGERING] = "lingering",
785 [USER_ONLINE] = "online",
786 [USER_ACTIVE] = "active",
787 [USER_CLOSING] = "closing"
788 };
789
790 DEFINE_STRING_TABLE_LOOKUP(user_state, UserState);
791
792 int config_parse_tmpfs_size(
793 const char* unit,
794 const char *filename,
795 unsigned line,
796 const char *section,
797 unsigned section_line,
798 const char *lvalue,
799 int ltype,
800 const char *rvalue,
801 void *data,
802 void *userdata) {
803
804 uint64_t *sz = data;
805 int r;
806
807 assert(filename);
808 assert(lvalue);
809 assert(rvalue);
810 assert(data);
811
812 /* First, try to parse as percentage */
813 r = parse_permille(rvalue);
814 if (r > 0 && r < 1000)
815 *sz = physical_memory_scale(r, 1000U);
816 else {
817 uint64_t k;
818
819 /* If the passed argument was not a percentage, or out of range, parse as byte size */
820
821 r = parse_size(rvalue, 1024, &k);
822 if (r >= 0 && (k <= 0 || (uint64_t) (size_t) k != k))
823 r = -ERANGE;
824 if (r < 0) {
825 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value '%s', ignoring: %m", rvalue);
826 return 0;
827 }
828
829 *sz = PAGE_ALIGN((size_t) k);
830 }
831
832 return 0;
833 }
834
835 int config_parse_compat_user_tasks_max(
836 const char *unit,
837 const char *filename,
838 unsigned line,
839 const char *section,
840 unsigned section_line,
841 const char *lvalue,
842 int ltype,
843 const char *rvalue,
844 void *data,
845 void *userdata) {
846
847 assert(filename);
848 assert(lvalue);
849 assert(rvalue);
850 assert(data);
851
852 log_syntax(unit, LOG_NOTICE, filename, line, 0,
853 "Support for option %s= has been removed.",
854 lvalue);
855 log_info("Hint: try creating /etc/systemd/system/user-.slice.d/50-limits.conf with:\n"
856 " [Slice]\n"
857 " TasksMax=%s",
858 rvalue);
859 return 0;
860 }