1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
9 #include "bus-common-errors.h"
10 #include "bus-error.h"
12 #include "cgroup-util.h"
13 #include "clean-ipc.h"
17 #include "format-util.h"
21 #include "logind-user.h"
23 #include "parse-util.h"
24 #include "path-util.h"
27 #include "stdio-util.h"
28 #include "string-table.h"
29 #include "unit-name.h"
30 #include "user-util.h"
33 int user_new(User **out, Manager *m, uid_t uid, gid_t gid, const char *name) {
34 _cleanup_(user_freep) User *u = NULL;
35 char lu[DECIMAL_STR_MAX(uid_t) + 1];
49 xsprintf(lu, UID_FMT, uid);
51 u->name = strdup(name);
55 if (asprintf(&u->state_file, "/run/systemd/users/"UID_FMT, uid) < 0)
58 if (asprintf(&u->runtime_path, "/run/user/"UID_FMT, uid) < 0)
61 r = slice_build_subslice(SPECIAL_USER_SLICE, lu, &u->slice);
65 r = unit_name_build("user", lu, ".service", &u->service);
69 r = hashmap_put(m->users, UID_TO_PTR(uid), u);
73 r = hashmap_put(m->user_units, u->slice, u);
77 r = hashmap_put(m->user_units, u->service, u);
86 User *user_free(User *u) {
91 LIST_REMOVE(gc_queue, u->manager->user_gc_queue, u);
94 session_free(u->sessions);
97 hashmap_remove_value(u->manager->user_units, u->service, u);
100 hashmap_remove_value(u->manager->user_units, u->slice, u);
102 hashmap_remove_value(u->manager->users, UID_TO_PTR(u->uid), u);
104 #if 0 /// elogind neither supports slice nor service jobs.
105 u->slice_job = mfree(u->slice_job);
106 u->service_job = mfree(u->service_job);
109 u->service = mfree(u->service);
110 u->slice = mfree(u->slice);
111 u->runtime_path = mfree(u->runtime_path);
112 u->state_file = mfree(u->state_file);
113 u->name = mfree(u->name);
118 static int user_save_internal(User *u) {
119 _cleanup_free_ char *temp_path = NULL;
120 _cleanup_fclose_ FILE *f = NULL;
124 assert(u->state_file);
126 r = mkdir_safe_label("/run/systemd/users", 0755, 0, 0, MKDIR_WARN_MODE);
130 r = fopen_temporary(u->state_file, &f, &temp_path);
134 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
135 (void) fchmod(fileno(f), 0644);
138 "# This is private data. Do not parse.\n"
142 user_state_to_string(user_get_state(u)));
144 /* LEGACY: no-one reads RUNTIME= anymore, drop it at some point */
146 fprintf(f, "RUNTIME=%s\n", u->runtime_path);
148 #if 0 /// elogind neither supports service nor slice jobs
150 fprintf(f, "SERVICE_JOB=%s\n", u->service_job);
153 fprintf(f, "SLICE_JOB=%s\n", u->slice_job);
157 fprintf(f, "DISPLAY=%s\n", u->display->id);
159 if (dual_timestamp_is_set(&u->timestamp))
161 "REALTIME="USEC_FMT"\n"
162 "MONOTONIC="USEC_FMT"\n",
163 u->timestamp.realtime,
164 u->timestamp.monotonic);
170 fputs("SESSIONS=", f);
172 LIST_FOREACH(sessions_by_user, i, u->sessions) {
181 fputs("\nSEATS=", f);
183 LIST_FOREACH(sessions_by_user, i, u->sessions) {
192 fputs(i->seat->id, f);
195 fputs("\nACTIVE_SESSIONS=", f);
197 LIST_FOREACH(sessions_by_user, i, u->sessions) {
198 if (!session_is_active(i))
209 fputs("\nONLINE_SESSIONS=", f);
211 LIST_FOREACH(sessions_by_user, i, u->sessions) {
212 if (session_get_state(i) == SESSION_CLOSING)
223 fputs("\nACTIVE_SEATS=", f);
225 LIST_FOREACH(sessions_by_user, i, u->sessions) {
226 if (!session_is_active(i) || !i->seat)
234 fputs(i->seat->id, f);
237 fputs("\nONLINE_SEATS=", f);
239 LIST_FOREACH(sessions_by_user, i, u->sessions) {
240 if (session_get_state(i) == SESSION_CLOSING || !i->seat)
248 fputs(i->seat->id, f);
253 r = fflush_and_check(f);
257 if (rename(temp_path, u->state_file) < 0) {
265 (void) unlink(u->state_file);
268 (void) unlink(temp_path);
270 return log_error_errno(r, "Failed to save user data %s: %m", u->state_file);
273 int user_save(User *u) {
279 return user_save_internal (u);
282 int user_load(User *u) {
283 _cleanup_free_ char *display = NULL, *realtime = NULL, *monotonic = NULL;
289 r = parse_env_file(NULL, u->state_file, NEWLINE,
290 #if 0 /// elogind neither supports service nor slice jobs
291 "SERVICE_JOB", &u->service_job,
292 "SLICE_JOB", &u->slice_job,
295 "REALTIME", &realtime,
296 "MONOTONIC", &monotonic,
302 return log_error_errno(r, "Failed to read %s: %m", u->state_file);
306 s = hashmap_get(u->manager->sessions, display);
308 if (s && s->display && display_is_local(s->display))
312 timestamp_deserialize(realtime, &u->timestamp.realtime);
314 timestamp_deserialize(monotonic, &u->timestamp.monotonic);
320 static int user_start_service(User *u) {
321 #if 0 /// elogind can not ask systemd via dbus to start user services
322 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
328 u->service_job = mfree(u->service_job);
330 r = manager_start_unit(
336 /* we don't fail due to this, let's try to continue */
337 log_error_errno(r, "Failed to start user service, ignoring: %s", bus_error_message(&error, r));
339 u->service_job = job;
343 hashmap_put(u->manager->user_units, u->service, u);
349 int user_start(User *u) {
354 if (u->started && !u->stopping)
358 * If u->stopping is set, the user is marked for removal and the slice
359 * and service stop-jobs are queued. We have to clear that flag before
360 * queing the start-jobs again. If they succeed, the user object can be
361 * re-used just fine (pid1 takes care of job-ordering and proper
362 * restart), but if they fail, we want to force another user_stop() so
363 * possibly pending units are stopped.
364 * Note that we don't clear u->started, as we have no clue what state
365 * the user is in on failure here. Hence, we pretend the user is
366 * running so it will be properly taken down by GC. However, we clearly
367 * return an error from user_start() in that case, so no further
368 * reference to the user is taken.
373 log_debug("Starting services for new user %s.", u->name);
375 /* Save the user data so far, because pam_systemd will read the
376 * XDG_RUNTIME_DIR out of it while starting up systemd --user.
377 * We need to do user_save_internal() because we have not
378 * "officially" started yet. */
379 user_save_internal(u);
381 /* Spawn user systemd */
382 r = user_start_service(u);
387 if (!dual_timestamp_is_set(&u->timestamp))
388 dual_timestamp_get(&u->timestamp);
389 user_send_signal(u, true);
393 /* Save new user data */
399 #if 0 /// UNNEEDED by elogind
400 static int user_stop_slice(User *u) {
401 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
407 r = manager_stop_unit(u->manager, u->slice, &error, &job);
409 log_error("Failed to stop user slice: %s", bus_error_message(&error, r));
419 static int user_stop_service(User *u) {
420 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
426 r = manager_stop_unit(u->manager, u->service, &error, &job);
428 log_error("Failed to stop user service: %s", bus_error_message(&error, r));
432 free_and_replace(u->service_job, job);
437 int user_stop(User *u, bool force) {
442 /* Stop jobs have already been queued */
448 LIST_FOREACH(sessions_by_user, s, u->sessions) {
449 k = session_stop(s, force);
455 #if 0 /// elogind does not support service or slice jobs
456 k = user_stop_service(u);
461 k = user_stop_slice(u);
470 #if 1 /// elogind must queue this user again
471 user_add_to_gc_queue(u);
476 int user_finalize(User *u) {
483 log_debug("User %s logged out.", u->name);
485 LIST_FOREACH(sessions_by_user, s, u->sessions) {
486 k = session_finalize(s);
491 /* Clean SysV + POSIX IPC objects, but only if this is not a system user. Background: in many setups cronjobs
492 * are run in full PAM and thus logind sessions, even if the code run doesn't belong to actual users but to
493 * system components. Since enable RemoveIPC= globally for all users, we need to be a bit careful with such
494 * cases, as we shouldn't accidentally remove a system service's IPC objects while it is running, just because
495 * a cronjob running as the same user just finished. Hence: exclude system users generally from IPC clean-up,
496 * and do it only for normal users. */
497 if (u->manager->remove_ipc && !uid_is_system(u->uid)) {
498 k = clean_ipc_by_uid(u->uid);
503 unlink(u->state_file);
504 user_add_to_gc_queue(u);
507 user_send_signal(u, false);
514 int user_get_idle_hint(User *u, dual_timestamp *t) {
516 bool idle_hint = true;
517 dual_timestamp ts = DUAL_TIMESTAMP_NULL;
521 LIST_FOREACH(sessions_by_user, s, u->sessions) {
525 ih = session_get_idle_hint(s, &k);
531 if (k.monotonic < ts.monotonic)
537 } else if (idle_hint) {
539 if (k.monotonic > ts.monotonic)
550 int user_check_linger_file(User *u) {
551 _cleanup_free_ char *cc = NULL;
554 cc = cescape(u->name);
558 p = strjoina("/var/lib/elogind/linger/", cc);
560 return access(p, F_OK) >= 0;
563 bool user_may_gc(User *u, bool drop_not_started) {
566 if (drop_not_started && !u->started)
572 if (user_check_linger_file(u) > 0)
575 #if 0 /// elogind neither supports service nor slice jobs
576 if (u->slice_job && manager_job_is_active(u->manager, u->slice_job))
579 if (u->service_job && manager_job_is_active(u->manager, u->service_job))
586 void user_add_to_gc_queue(User *u) {
592 LIST_PREPEND(gc_queue, u->manager->user_gc_queue, u);
593 u->in_gc_queue = true;
596 UserState user_get_state(User *u) {
604 #if 0 /// elogind neither supports service nor slice jobs.
605 if (!u->started || u->slice_job || u->service_job)
612 bool all_closing = true;
614 LIST_FOREACH(sessions_by_user, i, u->sessions) {
617 state = session_get_state(i);
618 if (state == SESSION_ACTIVE)
620 if (state != SESSION_CLOSING)
624 return all_closing ? USER_CLOSING : USER_ONLINE;
627 if (user_check_linger_file(u) > 0)
628 return USER_LINGERING;
633 int user_kill(User *u, int signo) {
634 #if 0 /// Without systemd unit support, elogind has to rely on its session system
637 return manager_kill_unit(u->manager, u->slice, KILL_ALL, signo, NULL);
644 LIST_FOREACH(sessions_by_user, s, u->sessions) {
645 int r = session_kill(s, KILL_ALL, signo);
646 if (res == 0 && r < 0)
654 static bool elect_display_filter(Session *s) {
655 /* Return true if the session is a candidate for the user’s ‘primary
656 * session’ or ‘display’. */
659 return (s->class == SESSION_USER && !s->stopping);
662 static int elect_display_compare(Session *s1, Session *s2) {
663 /* Indexed by SessionType. Lower numbers mean more preferred. */
664 const int type_ranks[_SESSION_TYPE_MAX] = {
665 [SESSION_UNSPECIFIED] = 0,
668 [SESSION_WAYLAND] = -3,
673 /* Calculate the partial order relationship between s1 and s2,
674 * returning < 0 if s1 is preferred as the user’s ‘primary session’,
675 * 0 if s1 and s2 are equally preferred or incomparable, or > 0 if s2
678 * s1 or s2 may be NULL. */
682 if ((s1 == NULL) != (s2 == NULL))
683 return (s1 == NULL) - (s2 == NULL);
685 if (s1->stopping != s2->stopping)
686 return s1->stopping - s2->stopping;
688 if ((s1->class != SESSION_USER) != (s2->class != SESSION_USER))
689 return (s1->class != SESSION_USER) - (s2->class != SESSION_USER);
691 if ((s1->type == _SESSION_TYPE_INVALID) != (s2->type == _SESSION_TYPE_INVALID))
692 return (s1->type == _SESSION_TYPE_INVALID) - (s2->type == _SESSION_TYPE_INVALID);
694 if (s1->type != s2->type)
695 return type_ranks[s1->type] - type_ranks[s2->type];
700 void user_elect_display(User *u) {
705 /* This elects a primary session for each user, which we call
706 * the "display". We try to keep the assignment stable, but we
707 * "upgrade" to better choices. */
708 log_debug("Electing new display for user %s", u->name);
710 LIST_FOREACH(sessions_by_user, s, u->sessions) {
711 if (!elect_display_filter(s)) {
712 log_debug("Ignoring session %s", s->id);
716 if (elect_display_compare(s, u->display) < 0) {
717 log_debug("Choosing session %s in preference to %s", s->id, u->display ? u->display->id : "-");
723 static const char* const user_state_table[_USER_STATE_MAX] = {
724 [USER_OFFLINE] = "offline",
725 [USER_OPENING] = "opening",
726 [USER_LINGERING] = "lingering",
727 [USER_ONLINE] = "online",
728 [USER_ACTIVE] = "active",
729 [USER_CLOSING] = "closing"
732 DEFINE_STRING_TABLE_LOOKUP(user_state, UserState);
734 int config_parse_tmpfs_size(
736 const char *filename,
739 unsigned section_line,
754 /* First, try to parse as percentage */
755 r = parse_percent(rvalue);
756 if (r > 0 && r < 100)
757 *sz = physical_memory_scale(r, 100U);
761 /* If the passed argument was not a percentage, or out of range, parse as byte size */
763 r = parse_size(rvalue, 1024, &k);
764 if (r < 0 || k <= 0 || (uint64_t) (size_t) k != k) {
765 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
769 *sz = PAGE_ALIGN((size_t) k);
775 int config_parse_compat_user_tasks_max(
777 const char *filename,
780 unsigned section_line,
792 log_syntax(unit, LOG_NOTICE, filename, line, 0,
793 "Support for option %s= has been removed.",
795 log_info("Hint: try creating /etc/elogind/system/user-.slice/50-limits.conf with:\n"