1 /* SPDX-License-Identifier: LGPL-2.1+ */
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"
17 //#include "format-util.h"
18 //#include "fs-util.h"
19 //#include "hashmap.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"
32 /// Additional includes needed by elogind
33 #include "user-runtime-dir.h"
35 int user_new(User **out, Manager *m, uid_t uid, gid_t gid, const char *name) {
36 _cleanup_(user_freep) User *u = NULL;
37 char lu[DECIMAL_STR_MAX(uid_t) + 1];
51 xsprintf(lu, UID_FMT, uid);
53 u->name = strdup(name);
57 if (asprintf(&u->state_file, "/run/systemd/users/"UID_FMT, uid) < 0)
60 if (asprintf(&u->runtime_path, "/run/user/"UID_FMT, uid) < 0)
63 r = slice_build_subslice(SPECIAL_USER_SLICE, lu, &u->slice);
67 r = unit_name_build("user", lu, ".service", &u->service);
71 r = hashmap_put(m->users, UID_TO_PTR(uid), u);
75 r = hashmap_put(m->user_units, u->slice, u);
79 r = hashmap_put(m->user_units, u->service, u);
88 User *user_free(User *u) {
93 LIST_REMOVE(gc_queue, u->manager->user_gc_queue, u);
96 session_free(u->sessions);
99 hashmap_remove_value(u->manager->user_units, u->service, u);
102 hashmap_remove_value(u->manager->user_units, u->slice, u);
104 hashmap_remove_value(u->manager->users, UID_TO_PTR(u->uid), u);
106 #if 0 /// elogind neither supports slice nor service jobs.
107 u->slice_job = mfree(u->slice_job);
108 u->service_job = mfree(u->service_job);
111 u->service = mfree(u->service);
112 u->slice = mfree(u->slice);
113 u->runtime_path = mfree(u->runtime_path);
114 u->state_file = mfree(u->state_file);
115 u->name = mfree(u->name);
120 static int user_save_internal(User *u) {
121 _cleanup_free_ char *temp_path = NULL;
122 _cleanup_fclose_ FILE *f = NULL;
126 assert(u->state_file);
128 r = mkdir_safe_label("/run/systemd/users", 0755, 0, 0, MKDIR_WARN_MODE);
132 r = fopen_temporary(u->state_file, &f, &temp_path);
136 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
137 (void) fchmod(fileno(f), 0644);
140 "# This is private data. Do not parse.\n"
144 user_state_to_string(user_get_state(u)));
146 /* LEGACY: no-one reads RUNTIME= anymore, drop it at some point */
148 fprintf(f, "RUNTIME=%s\n", u->runtime_path);
150 #if 0 /// elogind neither supports service nor slice jobs
152 fprintf(f, "SERVICE_JOB=%s\n", u->service_job);
155 fprintf(f, "SLICE_JOB=%s\n", u->slice_job);
159 fprintf(f, "DISPLAY=%s\n", u->display->id);
161 if (dual_timestamp_is_set(&u->timestamp))
163 "REALTIME="USEC_FMT"\n"
164 "MONOTONIC="USEC_FMT"\n",
165 u->timestamp.realtime,
166 u->timestamp.monotonic);
172 fputs("SESSIONS=", f);
174 LIST_FOREACH(sessions_by_user, i, u->sessions) {
183 fputs("\nSEATS=", f);
185 LIST_FOREACH(sessions_by_user, i, u->sessions) {
194 fputs(i->seat->id, f);
197 fputs("\nACTIVE_SESSIONS=", f);
199 LIST_FOREACH(sessions_by_user, i, u->sessions) {
200 if (!session_is_active(i))
211 fputs("\nONLINE_SESSIONS=", f);
213 LIST_FOREACH(sessions_by_user, i, u->sessions) {
214 if (session_get_state(i) == SESSION_CLOSING)
225 fputs("\nACTIVE_SEATS=", f);
227 LIST_FOREACH(sessions_by_user, i, u->sessions) {
228 if (!session_is_active(i) || !i->seat)
236 fputs(i->seat->id, f);
239 fputs("\nONLINE_SEATS=", f);
241 LIST_FOREACH(sessions_by_user, i, u->sessions) {
242 if (session_get_state(i) == SESSION_CLOSING || !i->seat)
250 fputs(i->seat->id, f);
255 r = fflush_and_check(f);
259 if (rename(temp_path, u->state_file) < 0) {
267 (void) unlink(u->state_file);
270 (void) unlink(temp_path);
272 return log_error_errno(r, "Failed to save user data %s: %m", u->state_file);
275 int user_save(User *u) {
281 return user_save_internal (u);
284 int user_load(User *u) {
285 _cleanup_free_ char *display = NULL, *realtime = NULL, *monotonic = NULL;
291 r = parse_env_file(NULL, u->state_file, NEWLINE,
292 #if 0 /// elogind neither supports service nor slice jobs
293 "SERVICE_JOB", &u->service_job,
294 "SLICE_JOB", &u->slice_job,
297 "REALTIME", &realtime,
298 "MONOTONIC", &monotonic,
304 return log_error_errno(r, "Failed to read %s: %m", u->state_file);
308 s = hashmap_get(u->manager->sessions, display);
310 if (s && s->display && display_is_local(s->display))
314 timestamp_deserialize(realtime, &u->timestamp.realtime);
316 timestamp_deserialize(monotonic, &u->timestamp.monotonic);
321 static int user_start_service(User *u) {
322 #if 0 /// elogind can not ask systemd via dbus to start user services
323 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
329 u->service_job = mfree(u->service_job);
331 r = manager_start_unit(
337 /* we don't fail due to this, let's try to continue */
338 log_error_errno(r, "Failed to start user service, ignoring: %s", bus_error_message(&error, r));
340 u->service_job = job;
344 hashmap_put(u->manager->user_units, u->service, u);
350 int user_start(User *u) {
355 if (u->started && !u->stopping)
359 * If u->stopping is set, the user is marked for removal and the slice
360 * and service stop-jobs are queued. We have to clear that flag before
361 * queing the start-jobs again. If they succeed, the user object can be
362 * re-used just fine (pid1 takes care of job-ordering and proper
363 * restart), but if they fail, we want to force another user_stop() so
364 * possibly pending units are stopped.
365 * Note that we don't clear u->started, as we have no clue what state
366 * the user is in on failure here. Hence, we pretend the user is
367 * running so it will be properly taken down by GC. However, we clearly
368 * return an error from user_start() in that case, so no further
369 * reference to the user is taken.
373 #if 0 /// elogind has to prepare the XDG_RUNTIME_DIR by itself
375 log_debug("Starting services for new user %s.", u->name);
378 log_debug("Starting services for new user %s.", u->name);
379 r = user_runtime_dir("start", u);
385 /* Save the user data so far, because pam_systemd will read the
386 * XDG_RUNTIME_DIR out of it while starting up systemd --user.
387 * We need to do user_save_internal() because we have not
388 * "officially" started yet. */
389 user_save_internal(u);
391 /* Spawn user systemd */
392 r = user_start_service(u);
397 if (!dual_timestamp_is_set(&u->timestamp))
398 dual_timestamp_get(&u->timestamp);
399 user_send_signal(u, true);
403 /* Save new user data */
409 #if 0 /// UNNEEDED by elogind
410 static int user_stop_slice(User *u) {
411 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
417 r = manager_stop_unit(u->manager, u->slice, &error, &job);
419 log_error("Failed to stop user slice: %s", bus_error_message(&error, r));
429 static int user_stop_service(User *u) {
430 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
436 r = manager_stop_unit(u->manager, u->service, &error, &job);
438 log_error("Failed to stop user service: %s", bus_error_message(&error, r));
442 free_and_replace(u->service_job, job);
447 int user_stop(User *u, bool force) {
452 /* Stop jobs have already been queued */
455 #if 1 /// elogind must queue this user again
456 user_add_to_gc_queue(u);
461 LIST_FOREACH(sessions_by_user, s, u->sessions) {
462 k = session_stop(s, force);
468 #if 0 /// elogind does not support service or slice jobs
469 k = user_stop_service(u);
474 k = user_stop_slice(u);
483 #if 1 /// elogind must queue this user again
484 user_add_to_gc_queue(u);
490 int user_finalize(User *u) {
497 log_debug("User %s logged out.", u->name);
499 LIST_FOREACH(sessions_by_user, s, u->sessions) {
500 k = session_finalize(s);
505 #if 1 /// elogind has to remove the XDG_RUNTIME_DIR by itself
506 /* Kill XDG_RUNTIME_DIR */
507 k = user_runtime_dir("stop", u);
512 /* Clean SysV + POSIX IPC objects, but only if this is not a system user. Background: in many setups cronjobs
513 * are run in full PAM and thus logind sessions, even if the code run doesn't belong to actual users but to
514 * system components. Since enable RemoveIPC= globally for all users, we need to be a bit careful with such
515 * cases, as we shouldn't accidentally remove a system service's IPC objects while it is running, just because
516 * a cronjob running as the same user just finished. Hence: exclude system users generally from IPC clean-up,
517 * and do it only for normal users. */
518 if (u->manager->remove_ipc && !uid_is_system(u->uid)) {
519 k = clean_ipc_by_uid(u->uid);
524 unlink(u->state_file);
525 user_add_to_gc_queue(u);
528 user_send_signal(u, false);
535 int user_get_idle_hint(User *u, dual_timestamp *t) {
537 bool idle_hint = true;
538 dual_timestamp ts = DUAL_TIMESTAMP_NULL;
542 LIST_FOREACH(sessions_by_user, s, u->sessions) {
546 ih = session_get_idle_hint(s, &k);
552 if (k.monotonic < ts.monotonic)
558 } else if (idle_hint) {
560 if (k.monotonic > ts.monotonic)
571 int user_check_linger_file(User *u) {
572 _cleanup_free_ char *cc = NULL;
575 cc = cescape(u->name);
579 p = strjoina("/var/lib/elogind/linger/", cc);
581 return access(p, F_OK) >= 0;
584 bool user_may_gc(User *u, bool drop_not_started) {
587 if (drop_not_started && !u->started)
593 if (user_check_linger_file(u) > 0)
596 #if 0 /// elogind neither supports service nor slice jobs
597 if (u->slice_job && manager_job_is_active(u->manager, u->slice_job))
600 if (u->service_job && manager_job_is_active(u->manager, u->service_job))
607 void user_add_to_gc_queue(User *u) {
613 LIST_PREPEND(gc_queue, u->manager->user_gc_queue, u);
614 u->in_gc_queue = true;
617 UserState user_get_state(User *u) {
625 #if 0 /// elogind neither supports service nor slice jobs.
626 if (!u->started || u->slice_job || u->service_job)
633 bool all_closing = true;
635 LIST_FOREACH(sessions_by_user, i, u->sessions) {
638 state = session_get_state(i);
639 if (state == SESSION_ACTIVE)
641 if (state != SESSION_CLOSING)
645 return all_closing ? USER_CLOSING : USER_ONLINE;
648 if (user_check_linger_file(u) > 0)
649 return USER_LINGERING;
654 int user_kill(User *u, int signo) {
655 #if 0 /// Without systemd unit support, elogind has to rely on its session system
658 return manager_kill_unit(u->manager, u->slice, KILL_ALL, signo, NULL);
665 LIST_FOREACH(sessions_by_user, s, u->sessions) {
666 int r = session_kill(s, KILL_ALL, signo);
667 if (res == 0 && r < 0)
675 static bool elect_display_filter(Session *s) {
676 /* Return true if the session is a candidate for the user’s ‘primary
677 * session’ or ‘display’. */
680 return (s->class == SESSION_USER && !s->stopping);
683 static int elect_display_compare(Session *s1, Session *s2) {
684 /* Indexed by SessionType. Lower numbers mean more preferred. */
685 const int type_ranks[_SESSION_TYPE_MAX] = {
686 [SESSION_UNSPECIFIED] = 0,
689 [SESSION_WAYLAND] = -3,
694 /* Calculate the partial order relationship between s1 and s2,
695 * returning < 0 if s1 is preferred as the user’s ‘primary session’,
696 * 0 if s1 and s2 are equally preferred or incomparable, or > 0 if s2
699 * s1 or s2 may be NULL. */
703 if ((s1 == NULL) != (s2 == NULL))
704 return (s1 == NULL) - (s2 == NULL);
706 if (s1->stopping != s2->stopping)
707 return s1->stopping - s2->stopping;
709 if ((s1->class != SESSION_USER) != (s2->class != SESSION_USER))
710 return (s1->class != SESSION_USER) - (s2->class != SESSION_USER);
712 if ((s1->type == _SESSION_TYPE_INVALID) != (s2->type == _SESSION_TYPE_INVALID))
713 return (s1->type == _SESSION_TYPE_INVALID) - (s2->type == _SESSION_TYPE_INVALID);
715 if (s1->type != s2->type)
716 return type_ranks[s1->type] - type_ranks[s2->type];
721 void user_elect_display(User *u) {
726 /* This elects a primary session for each user, which we call
727 * the "display". We try to keep the assignment stable, but we
728 * "upgrade" to better choices. */
729 log_debug("Electing new display for user %s", u->name);
731 LIST_FOREACH(sessions_by_user, s, u->sessions) {
732 if (!elect_display_filter(s)) {
733 log_debug("Ignoring session %s", s->id);
737 if (elect_display_compare(s, u->display) < 0) {
738 log_debug("Choosing session %s in preference to %s", s->id, u->display ? u->display->id : "-");
744 static const char* const user_state_table[_USER_STATE_MAX] = {
745 [USER_OFFLINE] = "offline",
746 [USER_OPENING] = "opening",
747 [USER_LINGERING] = "lingering",
748 [USER_ONLINE] = "online",
749 [USER_ACTIVE] = "active",
750 [USER_CLOSING] = "closing"
753 DEFINE_STRING_TABLE_LOOKUP(user_state, UserState);
755 int config_parse_tmpfs_size(
757 const char *filename,
760 unsigned section_line,
775 /* First, try to parse as percentage */
776 r = parse_percent(rvalue);
777 if (r > 0 && r < 100)
778 *sz = physical_memory_scale(r, 100U);
782 /* If the passed argument was not a percentage, or out of range, parse as byte size */
784 r = parse_size(rvalue, 1024, &k);
785 if (r < 0 || k <= 0 || (uint64_t) (size_t) k != k) {
786 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
790 *sz = PAGE_ALIGN((size_t) k);
796 int config_parse_compat_user_tasks_max(
798 const char *filename,
801 unsigned section_line,
813 log_syntax(unit, LOG_NOTICE, filename, line, 0,
814 "Support for option %s= has been removed.",
816 log_info("Hint: try creating /etc/elogind/system/user-.slice/50-limits.conf with:\n"