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