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