]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-user.c
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
**ret
, 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];
52 u
->name
= strdup(name
);
56 if (asprintf(&u
->state_file
, "/run/systemd/users/"UID_FMT
, uid
) < 0)
59 if (asprintf(&u
->runtime_path
, "/run/user/"UID_FMT
, uid
) < 0)
62 xsprintf(lu
, UID_FMT
, uid
);
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
);
87 User
*user_free(User
*u
) {
92 LIST_REMOVE(gc_queue
, u
->manager
->user_gc_queue
, u
);
95 session_free(u
->sessions
);
98 hashmap_remove_value(u
->manager
->user_units
, u
->service
, u
);
101 hashmap_remove_value(u
->manager
->user_units
, u
->slice
, u
);
103 hashmap_remove_value(u
->manager
->users
, UID_TO_PTR(u
->uid
), u
);
105 u
->slice_job
= mfree(u
->slice_job
);
106 u
->service_job
= mfree(u
->service_job
);
108 u
->service
= mfree(u
->service
);
109 u
->slice
= mfree(u
->slice
);
110 u
->runtime_path
= mfree(u
->runtime_path
);
111 u
->state_file
= mfree(u
->state_file
);
112 u
->name
= mfree(u
->name
);
117 static int user_save_internal(User
*u
) {
118 _cleanup_free_
char *temp_path
= NULL
;
119 _cleanup_fclose_
FILE *f
= NULL
;
123 assert(u
->state_file
);
125 r
= mkdir_safe_label("/run/systemd/users", 0755, 0, 0, MKDIR_WARN_MODE
);
129 r
= fopen_temporary(u
->state_file
, &f
, &temp_path
);
133 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
134 (void) fchmod(fileno(f
), 0644);
137 "# This is private data. Do not parse.\n"
139 "STATE=%s\n" /* friendly user-facing state */
140 "STOPPING=%s\n", /* low-level state */
142 user_state_to_string(user_get_state(u
)),
143 yes_no(u
->stopping
));
145 /* LEGACY: no-one reads RUNTIME= anymore, drop it at some point */
147 fprintf(f
, "RUNTIME=%s\n", u
->runtime_path
);
150 fprintf(f
, "SERVICE_JOB=%s\n", u
->service_job
);
153 fprintf(f
, "SLICE_JOB=%s\n", u
->slice_job
);
156 fprintf(f
, "DISPLAY=%s\n", u
->display
->id
);
158 if (dual_timestamp_is_set(&u
->timestamp
))
160 "REALTIME="USEC_FMT
"\n"
161 "MONOTONIC="USEC_FMT
"\n",
162 u
->timestamp
.realtime
,
163 u
->timestamp
.monotonic
);
169 fputs("SESSIONS=", f
);
171 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
180 fputs("\nSEATS=", f
);
182 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
191 fputs(i
->seat
->id
, f
);
194 fputs("\nACTIVE_SESSIONS=", f
);
196 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
197 if (!session_is_active(i
))
208 fputs("\nONLINE_SESSIONS=", f
);
210 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
211 if (session_get_state(i
) == SESSION_CLOSING
)
222 fputs("\nACTIVE_SEATS=", f
);
224 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
225 if (!session_is_active(i
) || !i
->seat
)
233 fputs(i
->seat
->id
, f
);
236 fputs("\nONLINE_SEATS=", f
);
238 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
239 if (session_get_state(i
) == SESSION_CLOSING
|| !i
->seat
)
247 fputs(i
->seat
->id
, f
);
252 r
= fflush_and_check(f
);
256 if (rename(temp_path
, u
->state_file
) < 0) {
264 (void) unlink(u
->state_file
);
267 (void) unlink(temp_path
);
269 return log_error_errno(r
, "Failed to save user data %s: %m", u
->state_file
);
272 int user_save(User
*u
) {
278 return user_save_internal(u
);
281 int user_load(User
*u
) {
282 _cleanup_free_
char *realtime
= NULL
, *monotonic
= NULL
, *stopping
= NULL
;
287 r
= parse_env_file(NULL
, u
->state_file
, NEWLINE
,
288 "SERVICE_JOB", &u
->service_job
,
289 "STOPPING", &stopping
,
290 "REALTIME", &realtime
,
291 "MONOTONIC", &monotonic
,
296 return log_error_errno(r
, "Failed to read %s: %m", u
->state_file
);
299 r
= parse_boolean(stopping
);
301 log_debug_errno(r
, "Failed to parse 'STOPPING' boolean: %s", stopping
);
307 timestamp_deserialize(realtime
, &u
->timestamp
.realtime
);
309 timestamp_deserialize(monotonic
, &u
->timestamp
.monotonic
);
314 static int user_start_service(User
*u
) {
315 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
321 u
->service_job
= mfree(u
->service_job
);
323 r
= manager_start_unit(
329 /* we don't fail due to this, let's try to continue */
330 log_error_errno(r
, "Failed to start user service, ignoring: %s", bus_error_message(&error
, r
));
332 u
->service_job
= job
;
337 int user_start(User
*u
) {
342 if (u
->started
&& !u
->stopping
)
346 * If u->stopping is set, the user is marked for removal and the slice
347 * and service stop-jobs are queued. We have to clear that flag before
348 * queing the start-jobs again. If they succeed, the user object can be
349 * re-used just fine (pid1 takes care of job-ordering and proper
350 * restart), but if they fail, we want to force another user_stop() so
351 * possibly pending units are stopped.
352 * Note that we don't clear u->started, as we have no clue what state
353 * the user is in on failure here. Hence, we pretend the user is
354 * running so it will be properly taken down by GC. However, we clearly
355 * return an error from user_start() in that case, so no further
356 * reference to the user is taken.
361 log_debug("Starting services for new user %s.", u
->name
);
363 /* Save the user data so far, because pam_systemd will read the
364 * XDG_RUNTIME_DIR out of it while starting up systemd --user.
365 * We need to do user_save_internal() because we have not
366 * "officially" started yet. */
367 user_save_internal(u
);
369 /* Spawn user systemd */
370 r
= user_start_service(u
);
375 if (!dual_timestamp_is_set(&u
->timestamp
))
376 dual_timestamp_get(&u
->timestamp
);
377 user_send_signal(u
, true);
381 /* Save new user data */
387 static int user_stop_slice(User
*u
) {
388 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
394 r
= manager_stop_unit(u
->manager
, u
->slice
, &error
, &job
);
396 return log_error_errno(r
, "Failed to stop user slice: %s", bus_error_message(&error
, r
));
398 return free_and_replace(u
->slice_job
, job
);
401 static int user_stop_service(User
*u
) {
402 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
408 r
= manager_stop_unit(u
->manager
, u
->service
, &error
, &job
);
410 return log_error_errno(r
, "Failed to stop user service: %s", bus_error_message(&error
, r
));
412 return free_and_replace(u
->service_job
, job
);
415 int user_stop(User
*u
, bool force
) {
420 /* Stop jobs have already been queued */
426 LIST_FOREACH(sessions_by_user
, s
, u
->sessions
) {
427 k
= session_stop(s
, force
);
433 k
= user_stop_service(u
);
438 k
= user_stop_slice(u
);
449 int user_finalize(User
*u
) {
456 log_debug("User %s logged out.", u
->name
);
458 LIST_FOREACH(sessions_by_user
, s
, u
->sessions
) {
459 k
= session_finalize(s
);
464 /* Clean SysV + POSIX IPC objects, but only if this is not a system user. Background: in many setups cronjobs
465 * are run in full PAM and thus logind sessions, even if the code run doesn't belong to actual users but to
466 * system components. Since enable RemoveIPC= globally for all users, we need to be a bit careful with such
467 * cases, as we shouldn't accidentally remove a system service's IPC objects while it is running, just because
468 * a cronjob running as the same user just finished. Hence: exclude system users generally from IPC clean-up,
469 * and do it only for normal users. */
470 if (u
->manager
->remove_ipc
&& !uid_is_system(u
->uid
)) {
471 k
= clean_ipc_by_uid(u
->uid
);
476 unlink(u
->state_file
);
477 user_add_to_gc_queue(u
);
480 user_send_signal(u
, false);
487 int user_get_idle_hint(User
*u
, dual_timestamp
*t
) {
489 bool idle_hint
= true;
490 dual_timestamp ts
= DUAL_TIMESTAMP_NULL
;
494 LIST_FOREACH(sessions_by_user
, s
, u
->sessions
) {
498 ih
= session_get_idle_hint(s
, &k
);
504 if (k
.monotonic
< ts
.monotonic
)
510 } else if (idle_hint
) {
512 if (k
.monotonic
> ts
.monotonic
)
523 int user_check_linger_file(User
*u
) {
524 _cleanup_free_
char *cc
= NULL
;
527 cc
= cescape(u
->name
);
531 p
= strjoina("/var/lib/systemd/linger/", cc
);
533 return access(p
, F_OK
) >= 0;
536 bool user_may_gc(User
*u
, bool drop_not_started
) {
541 if (drop_not_started
&& !u
->started
)
547 if (user_check_linger_file(u
) > 0)
550 /* Check if our job is still pending */
551 if (u
->service_job
) {
552 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
554 r
= manager_job_is_active(u
->manager
, u
->service_job
, &error
);
556 log_debug_errno(r
, "Failed to determine whether job '%s' is pending, ignoring: %s", u
->service_job
, bus_error_message(&error
, r
));
561 /* Note that we don't care if the three units we manage for each user object are up or not, as we are managing
562 * their state rather than tracking it. */
567 void user_add_to_gc_queue(User
*u
) {
573 LIST_PREPEND(gc_queue
, u
->manager
->user_gc_queue
, u
);
574 u
->in_gc_queue
= true;
577 UserState
user_get_state(User
*u
) {
585 if (!u
->started
|| u
->slice_job
|| u
->service_job
)
589 bool all_closing
= true;
591 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
594 state
= session_get_state(i
);
595 if (state
== SESSION_ACTIVE
)
597 if (state
!= SESSION_CLOSING
)
601 return all_closing
? USER_CLOSING
: USER_ONLINE
;
604 if (user_check_linger_file(u
) > 0)
605 return USER_LINGERING
;
610 int user_kill(User
*u
, int signo
) {
613 return manager_kill_unit(u
->manager
, u
->slice
, KILL_ALL
, signo
, NULL
);
616 static bool elect_display_filter(Session
*s
) {
617 /* Return true if the session is a candidate for the user’s ‘primary session’ or ‘display’. */
620 return s
->class == SESSION_USER
&& s
->started
&& !s
->stopping
;
623 static int elect_display_compare(Session
*s1
, Session
*s2
) {
624 /* Indexed by SessionType. Lower numbers mean more preferred. */
625 const int type_ranks
[_SESSION_TYPE_MAX
] = {
626 [SESSION_UNSPECIFIED
] = 0,
629 [SESSION_WAYLAND
] = -3,
634 /* Calculate the partial order relationship between s1 and s2,
635 * returning < 0 if s1 is preferred as the user’s ‘primary session’,
636 * 0 if s1 and s2 are equally preferred or incomparable, or > 0 if s2
639 * s1 or s2 may be NULL. */
643 if ((s1
== NULL
) != (s2
== NULL
))
644 return (s1
== NULL
) - (s2
== NULL
);
646 if (s1
->stopping
!= s2
->stopping
)
647 return s1
->stopping
- s2
->stopping
;
649 if ((s1
->class != SESSION_USER
) != (s2
->class != SESSION_USER
))
650 return (s1
->class != SESSION_USER
) - (s2
->class != SESSION_USER
);
652 if ((s1
->type
== _SESSION_TYPE_INVALID
) != (s2
->type
== _SESSION_TYPE_INVALID
))
653 return (s1
->type
== _SESSION_TYPE_INVALID
) - (s2
->type
== _SESSION_TYPE_INVALID
);
655 if (s1
->type
!= s2
->type
)
656 return type_ranks
[s1
->type
] - type_ranks
[s2
->type
];
661 void user_elect_display(User
*u
) {
666 /* This elects a primary session for each user, which we call the "display". We try to keep the assignment
667 * stable, but we "upgrade" to better choices. */
668 log_debug("Electing new display for user %s", u
->name
);
670 LIST_FOREACH(sessions_by_user
, s
, u
->sessions
) {
671 if (!elect_display_filter(s
)) {
672 log_debug("Ignoring session %s", s
->id
);
676 if (elect_display_compare(s
, u
->display
) < 0) {
677 log_debug("Choosing session %s in preference to %s", s
->id
, u
->display
? u
->display
->id
: "-");
683 static const char* const user_state_table
[_USER_STATE_MAX
] = {
684 [USER_OFFLINE
] = "offline",
685 [USER_OPENING
] = "opening",
686 [USER_LINGERING
] = "lingering",
687 [USER_ONLINE
] = "online",
688 [USER_ACTIVE
] = "active",
689 [USER_CLOSING
] = "closing"
692 DEFINE_STRING_TABLE_LOOKUP(user_state
, UserState
);
694 int config_parse_tmpfs_size(
696 const char *filename
,
699 unsigned section_line
,
714 /* First, try to parse as percentage */
715 r
= parse_permille(rvalue
);
716 if (r
> 0 && r
< 1000)
717 *sz
= physical_memory_scale(r
, 1000U);
721 /* If the passed argument was not a percentage, or out of range, parse as byte size */
723 r
= parse_size(rvalue
, 1024, &k
);
724 if (r
>= 0 && (k
<= 0 || (uint64_t) (size_t) k
!= k
))
727 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value '%s', ignoring: %m", rvalue
);
731 *sz
= PAGE_ALIGN((size_t) k
);
737 int config_parse_compat_user_tasks_max(
739 const char *filename
,
742 unsigned section_line
,
754 log_syntax(unit
, LOG_NOTICE
, filename
, line
, 0,
755 "Support for option %s= has been removed.",
757 log_info("Hint: try creating /etc/systemd/system/user-.slice.d/50-limits.conf with:\n"