]>
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
= unit_name_build("user-runtime-dir", lu
, ".service", &u
->runtime_dir_service
);
75 r
= hashmap_put(m
->users
, UID_TO_PTR(uid
), u
);
79 r
= hashmap_put(m
->user_units
, u
->slice
, u
);
83 r
= hashmap_put(m
->user_units
, u
->service
, u
);
87 r
= hashmap_put(m
->user_units
, u
->runtime_dir_service
, u
);
95 User
*user_free(User
*u
) {
100 LIST_REMOVE(gc_queue
, u
->manager
->user_gc_queue
, u
);
103 session_free(u
->sessions
);
106 hashmap_remove_value(u
->manager
->user_units
, u
->service
, u
);
108 if (u
->runtime_dir_service
)
109 hashmap_remove_value(u
->manager
->user_units
, u
->runtime_dir_service
, u
);
112 hashmap_remove_value(u
->manager
->user_units
, u
->slice
, u
);
114 hashmap_remove_value(u
->manager
->users
, UID_TO_PTR(u
->uid
), u
);
116 u
->service_job
= mfree(u
->service_job
);
118 u
->service
= mfree(u
->service
);
119 u
->runtime_dir_service
= mfree(u
->runtime_dir_service
);
120 u
->slice
= mfree(u
->slice
);
121 u
->runtime_path
= mfree(u
->runtime_path
);
122 u
->state_file
= mfree(u
->state_file
);
123 u
->name
= mfree(u
->name
);
128 static int user_save_internal(User
*u
) {
129 _cleanup_free_
char *temp_path
= NULL
;
130 _cleanup_fclose_
FILE *f
= NULL
;
134 assert(u
->state_file
);
136 r
= mkdir_safe_label("/run/systemd/users", 0755, 0, 0, MKDIR_WARN_MODE
);
140 r
= fopen_temporary(u
->state_file
, &f
, &temp_path
);
144 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
145 (void) fchmod(fileno(f
), 0644);
148 "# This is private data. Do not parse.\n"
150 "STATE=%s\n" /* friendly user-facing state */
151 "STOPPING=%s\n", /* low-level state */
153 user_state_to_string(user_get_state(u
)),
154 yes_no(u
->stopping
));
156 /* LEGACY: no-one reads RUNTIME= anymore, drop it at some point */
158 fprintf(f
, "RUNTIME=%s\n", u
->runtime_path
);
161 fprintf(f
, "SERVICE_JOB=%s\n", u
->service_job
);
164 fprintf(f
, "DISPLAY=%s\n", u
->display
->id
);
166 if (dual_timestamp_is_set(&u
->timestamp
))
168 "REALTIME="USEC_FMT
"\n"
169 "MONOTONIC="USEC_FMT
"\n",
170 u
->timestamp
.realtime
,
171 u
->timestamp
.monotonic
);
177 fputs("SESSIONS=", f
);
179 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
188 fputs("\nSEATS=", f
);
190 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
199 fputs(i
->seat
->id
, f
);
202 fputs("\nACTIVE_SESSIONS=", f
);
204 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
205 if (!session_is_active(i
))
216 fputs("\nONLINE_SESSIONS=", f
);
218 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
219 if (session_get_state(i
) == SESSION_CLOSING
)
230 fputs("\nACTIVE_SEATS=", f
);
232 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
233 if (!session_is_active(i
) || !i
->seat
)
241 fputs(i
->seat
->id
, f
);
244 fputs("\nONLINE_SEATS=", f
);
246 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
247 if (session_get_state(i
) == SESSION_CLOSING
|| !i
->seat
)
255 fputs(i
->seat
->id
, f
);
260 r
= fflush_and_check(f
);
264 if (rename(temp_path
, u
->state_file
) < 0) {
272 (void) unlink(u
->state_file
);
275 (void) unlink(temp_path
);
277 return log_error_errno(r
, "Failed to save user data %s: %m", u
->state_file
);
280 int user_save(User
*u
) {
286 return user_save_internal(u
);
289 int user_load(User
*u
) {
290 _cleanup_free_
char *realtime
= NULL
, *monotonic
= NULL
, *stopping
= NULL
;
295 r
= parse_env_file(NULL
, u
->state_file
, NEWLINE
,
296 "SERVICE_JOB", &u
->service_job
,
297 "STOPPING", &stopping
,
298 "REALTIME", &realtime
,
299 "MONOTONIC", &monotonic
,
304 return log_error_errno(r
, "Failed to read %s: %m", u
->state_file
);
307 r
= parse_boolean(stopping
);
309 log_debug_errno(r
, "Failed to parse 'STOPPING' boolean: %s", stopping
);
315 timestamp_deserialize(realtime
, &u
->timestamp
.realtime
);
317 timestamp_deserialize(monotonic
, &u
->timestamp
.monotonic
);
322 static void user_start_service(User
*u
) {
323 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
328 /* Start the service containing the "systemd --user" instance (user@.service). Note that we don't explicitly
329 * start the per-user slice or the systemd-runtime-dir@.service instance, as those are pulled in both by
330 * user@.service and the session scopes as dependencies. */
332 u
->service_job
= mfree(u
->service_job
);
334 r
= manager_start_unit(u
->manager
, u
->service
, &error
, &u
->service_job
);
336 log_warning_errno(r
, "Failed to start user service '%s', ignoring: %s", u
->service
, bus_error_message(&error
, r
));
339 int user_start(User
*u
) {
342 if (u
->started
&& !u
->stopping
)
345 /* If u->stopping is set, the user is marked for removal and service stop-jobs are queued. We have to clear
346 * that flag before queing the start-jobs again. If they succeed, the user object can be re-used just fine
347 * (pid1 takes care of job-ordering and proper restart), but if they fail, we want to force another user_stop()
348 * so possibly pending units are stopped. */
352 log_debug("Starting services for new user %s.", u
->name
);
354 /* Save the user data so far, because pam_systemd will read the XDG_RUNTIME_DIR out of it while starting up
355 * systemd --user. We need to do user_save_internal() because we have not "officially" started yet. */
356 user_save_internal(u
);
358 /* Start user@UID.service */
359 user_start_service(u
);
362 if (!dual_timestamp_is_set(&u
->timestamp
))
363 dual_timestamp_get(&u
->timestamp
);
364 user_send_signal(u
, true);
368 /* Save new user data */
374 static void user_stop_service(User
*u
) {
375 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
381 /* The reverse of user_start_service(). Note that we only stop user@UID.service here, and let StopWhenUnneeded=
382 * deal with the slice and the user-runtime-dir@.service instance. */
384 u
->service_job
= mfree(u
->service_job
);
386 r
= manager_stop_unit(u
->manager
, u
->service
, &error
, &u
->service_job
);
388 log_warning_errno(r
, "Failed to stop user service '%s', ignoring: %s", u
->service
, bus_error_message(&error
, r
));
391 int user_stop(User
*u
, bool force
) {
396 /* This is called whenever we begin with tearing down a user record. It's called in two cases: explicit API
397 * request to do so via the bus (in which case 'force' is true) and automatically due to GC, if there's no
398 * session left pinning it (in which case 'force' is false). Note that this just initiates tearing down of the
399 * user, the User object will remain in memory until user_finalize() is called, see below. */
404 if (u
->stopping
) { /* Stop jobs have already been queued */
409 LIST_FOREACH(sessions_by_user
, s
, u
->sessions
) {
412 k
= session_stop(s
, force
);
417 user_stop_service(u
);
426 int user_finalize(User
*u
) {
432 /* Called when the user is really ready to be freed, i.e. when all unit stop jobs and suchlike for it are
433 * done. This is called as a result of an earlier user_done() when all jobs are completed. */
436 log_debug("User %s logged out.", u
->name
);
438 LIST_FOREACH(sessions_by_user
, s
, u
->sessions
) {
439 k
= session_finalize(s
);
444 /* Clean SysV + POSIX IPC objects, but only if this is not a system user. Background: in many setups cronjobs
445 * are run in full PAM and thus logind sessions, even if the code run doesn't belong to actual users but to
446 * system components. Since enable RemoveIPC= globally for all users, we need to be a bit careful with such
447 * cases, as we shouldn't accidentally remove a system service's IPC objects while it is running, just because
448 * a cronjob running as the same user just finished. Hence: exclude system users generally from IPC clean-up,
449 * and do it only for normal users. */
450 if (u
->manager
->remove_ipc
&& !uid_is_system(u
->uid
)) {
451 k
= clean_ipc_by_uid(u
->uid
);
456 (void) unlink(u
->state_file
);
457 user_add_to_gc_queue(u
);
460 user_send_signal(u
, false);
467 int user_get_idle_hint(User
*u
, dual_timestamp
*t
) {
469 bool idle_hint
= true;
470 dual_timestamp ts
= DUAL_TIMESTAMP_NULL
;
474 LIST_FOREACH(sessions_by_user
, s
, u
->sessions
) {
478 ih
= session_get_idle_hint(s
, &k
);
484 if (k
.monotonic
< ts
.monotonic
)
490 } else if (idle_hint
) {
492 if (k
.monotonic
> ts
.monotonic
)
503 int user_check_linger_file(User
*u
) {
504 _cleanup_free_
char *cc
= NULL
;
507 cc
= cescape(u
->name
);
511 p
= strjoina("/var/lib/systemd/linger/", cc
);
513 return access(p
, F_OK
) >= 0;
516 bool user_may_gc(User
*u
, bool drop_not_started
) {
521 if (drop_not_started
&& !u
->started
)
527 if (user_check_linger_file(u
) > 0)
530 /* Check if our job is still pending */
531 if (u
->service_job
) {
532 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
534 r
= manager_job_is_active(u
->manager
, u
->service_job
, &error
);
536 log_debug_errno(r
, "Failed to determine whether job '%s' is pending, ignoring: %s", u
->service_job
, bus_error_message(&error
, r
));
541 /* Note that we don't care if the three units we manage for each user object are up or not, as we are managing
542 * their state rather than tracking it. */
547 void user_add_to_gc_queue(User
*u
) {
553 LIST_PREPEND(gc_queue
, u
->manager
->user_gc_queue
, u
);
554 u
->in_gc_queue
= true;
557 UserState
user_get_state(User
*u
) {
565 if (!u
->started
|| u
->service_job
)
569 bool all_closing
= true;
571 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
574 state
= session_get_state(i
);
575 if (state
== SESSION_ACTIVE
)
577 if (state
!= SESSION_CLOSING
)
581 return all_closing
? USER_CLOSING
: USER_ONLINE
;
584 if (user_check_linger_file(u
) > 0)
585 return USER_LINGERING
;
590 int user_kill(User
*u
, int signo
) {
593 return manager_kill_unit(u
->manager
, u
->slice
, KILL_ALL
, signo
, NULL
);
596 static bool elect_display_filter(Session
*s
) {
597 /* Return true if the session is a candidate for the user’s ‘primary session’ or ‘display’. */
600 return s
->class == SESSION_USER
&& s
->started
&& !s
->stopping
;
603 static int elect_display_compare(Session
*s1
, Session
*s2
) {
604 /* Indexed by SessionType. Lower numbers mean more preferred. */
605 const int type_ranks
[_SESSION_TYPE_MAX
] = {
606 [SESSION_UNSPECIFIED
] = 0,
609 [SESSION_WAYLAND
] = -3,
614 /* Calculate the partial order relationship between s1 and s2,
615 * returning < 0 if s1 is preferred as the user’s ‘primary session’,
616 * 0 if s1 and s2 are equally preferred or incomparable, or > 0 if s2
619 * s1 or s2 may be NULL. */
623 if ((s1
== NULL
) != (s2
== NULL
))
624 return (s1
== NULL
) - (s2
== NULL
);
626 if (s1
->stopping
!= s2
->stopping
)
627 return s1
->stopping
- s2
->stopping
;
629 if ((s1
->class != SESSION_USER
) != (s2
->class != SESSION_USER
))
630 return (s1
->class != SESSION_USER
) - (s2
->class != SESSION_USER
);
632 if ((s1
->type
== _SESSION_TYPE_INVALID
) != (s2
->type
== _SESSION_TYPE_INVALID
))
633 return (s1
->type
== _SESSION_TYPE_INVALID
) - (s2
->type
== _SESSION_TYPE_INVALID
);
635 if (s1
->type
!= s2
->type
)
636 return type_ranks
[s1
->type
] - type_ranks
[s2
->type
];
641 void user_elect_display(User
*u
) {
646 /* This elects a primary session for each user, which we call the "display". We try to keep the assignment
647 * stable, but we "upgrade" to better choices. */
648 log_debug("Electing new display for user %s", u
->name
);
650 LIST_FOREACH(sessions_by_user
, s
, u
->sessions
) {
651 if (!elect_display_filter(s
)) {
652 log_debug("Ignoring session %s", s
->id
);
656 if (elect_display_compare(s
, u
->display
) < 0) {
657 log_debug("Choosing session %s in preference to %s", s
->id
, u
->display
? u
->display
->id
: "-");
663 static const char* const user_state_table
[_USER_STATE_MAX
] = {
664 [USER_OFFLINE
] = "offline",
665 [USER_OPENING
] = "opening",
666 [USER_LINGERING
] = "lingering",
667 [USER_ONLINE
] = "online",
668 [USER_ACTIVE
] = "active",
669 [USER_CLOSING
] = "closing"
672 DEFINE_STRING_TABLE_LOOKUP(user_state
, UserState
);
674 int config_parse_tmpfs_size(
676 const char *filename
,
679 unsigned section_line
,
694 /* First, try to parse as percentage */
695 r
= parse_permille(rvalue
);
696 if (r
> 0 && r
< 1000)
697 *sz
= physical_memory_scale(r
, 1000U);
701 /* If the passed argument was not a percentage, or out of range, parse as byte size */
703 r
= parse_size(rvalue
, 1024, &k
);
704 if (r
>= 0 && (k
<= 0 || (uint64_t) (size_t) k
!= k
))
707 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value '%s', ignoring: %m", rvalue
);
711 *sz
= PAGE_ALIGN((size_t) k
);
717 int config_parse_compat_user_tasks_max(
719 const char *filename
,
722 unsigned section_line
,
734 log_syntax(unit
, LOG_NOTICE
, filename
, line
, 0,
735 "Support for option %s= has been removed.",
737 log_info("Hint: try creating /etc/systemd/system/user-.slice.d/50-limits.conf with:\n"