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