1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "alloc-util.h"
8 #include "bus-common-errors.h"
10 #include "bus-locator.h"
12 #include "clean-ipc.h"
14 #include "errno-util.h"
17 #include "format-util.h"
20 #include "limits-util.h"
21 #include "logind-session.h"
23 #include "logind-dbus.h"
24 #include "logind-seat.h"
25 #include "logind-user.h"
26 #include "logind-user-dbus.h"
27 #include "mkdir-label.h"
28 #include "parse-util.h"
29 #include "percent-util.h"
30 #include "serialize.h"
32 #include "stdio-util.h"
33 #include "string-table.h"
34 #include "string-util.h"
36 #include "tmpfile-util.h"
37 #include "uid-classification.h"
38 #include "unit-name.h"
39 #include "user-record.h"
40 #include "user-util.h"
42 int user_new(Manager
*m
, UserRecord
*ur
, User
**ret
) {
43 _cleanup_(user_freep
) User
*u
= NULL
;
44 char lu
[DECIMAL_STR_MAX(uid_t
) + 1];
54 if (!uid_is_valid(ur
->uid
))
63 .user_record
= user_record_ref(ur
),
64 .last_session_timestamp
= USEC_INFINITY
,
65 .gc_mode
= USER_GC_BY_ANY
,
68 if (asprintf(&u
->state_file
, "/run/systemd/users/" UID_FMT
, ur
->uid
) < 0)
71 if (asprintf(&u
->runtime_path
, "/run/user/" UID_FMT
, ur
->uid
) < 0)
74 xsprintf(lu
, UID_FMT
, ur
->uid
);
75 r
= slice_build_subslice(SPECIAL_USER_SLICE
, lu
, &u
->slice
);
79 r
= unit_name_build("user-runtime-dir", lu
, ".service", &u
->runtime_dir_unit
);
83 r
= unit_name_build("user", lu
, ".service", &u
->service_manager_unit
);
87 r
= hashmap_put(m
->users
, UID_TO_PTR(ur
->uid
), u
);
91 r
= hashmap_put(m
->user_units
, u
->slice
, u
);
95 r
= hashmap_put(m
->user_units
, u
->runtime_dir_unit
, u
);
99 r
= hashmap_put(m
->user_units
, u
->service_manager_unit
, u
);
107 User
*user_free(User
*u
) {
112 LIST_REMOVE(gc_queue
, u
->manager
->user_gc_queue
, u
);
115 session_free(u
->sessions
);
117 sd_event_source_unref(u
->timer_event_source
);
119 if (u
->service_manager_unit
) {
120 (void) hashmap_remove_value(u
->manager
->user_units
, u
->service_manager_unit
, u
);
121 free(u
->service_manager_job
);
122 free(u
->service_manager_unit
);
125 if (u
->runtime_dir_unit
) {
126 (void) hashmap_remove_value(u
->manager
->user_units
, u
->runtime_dir_unit
, u
);
127 free(u
->runtime_dir_job
);
128 free(u
->runtime_dir_unit
);
132 (void) hashmap_remove_value(u
->manager
->user_units
, u
->slice
, u
);
136 (void) hashmap_remove_value(u
->manager
->users
, UID_TO_PTR(u
->user_record
->uid
), u
);
138 free(u
->runtime_path
);
141 user_record_unref(u
->user_record
);
146 static int user_save_internal(User
*u
) {
150 assert(u
->state_file
);
152 r
= mkdir_safe_label("/run/systemd/users", 0755, 0, 0, MKDIR_WARN_MODE
);
154 return log_error_errno(r
, "Failed to create /run/systemd/users/: %m");
156 _cleanup_(unlink_and_freep
) char *temp_path
= NULL
;
157 _cleanup_fclose_
FILE *f
= NULL
;
158 r
= fopen_tmpfile_linkable(u
->state_file
, O_WRONLY
|O_CLOEXEC
, &temp_path
, &f
);
160 return log_error_errno(r
, "Failed to create state file '%s': %m", u
->state_file
);
162 if (fchmod(fileno(f
), 0644) < 0)
163 return log_error_errno(errno
, "Failed to set access mode for state file '%s' to 0644: %m", u
->state_file
);
166 "# This is private data. Do not parse.\n"
168 "STATE=%s\n" /* friendly user-facing state */
169 "STOPPING=%s\n" /* low-level state */
171 u
->user_record
->user_name
,
172 user_state_to_string(user_get_state(u
)),
174 user_gc_mode_to_string(u
->gc_mode
));
176 /* LEGACY: no-one reads RUNTIME= anymore, drop it at some point */
177 env_file_fputs_assignment(f
, "RUNTIME=", u
->runtime_path
);
178 env_file_fputs_assignment(f
, "RUNTIME_DIR_JOB=", u
->runtime_dir_job
);
179 env_file_fputs_assignment(f
, "SERVICE_JOB=", u
->service_manager_job
);
181 env_file_fputs_assignment(f
, "DISPLAY=", u
->display
->id
);
183 if (dual_timestamp_is_set(&u
->timestamp
))
185 "REALTIME="USEC_FMT
"\n"
186 "MONOTONIC="USEC_FMT
"\n",
187 u
->timestamp
.realtime
,
188 u
->timestamp
.monotonic
);
190 if (u
->last_session_timestamp
!= USEC_INFINITY
)
191 fprintf(f
, "LAST_SESSION_TIMESTAMP=" USEC_FMT
"\n",
192 u
->last_session_timestamp
);
197 fputs("SESSIONS=", f
);
199 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
208 fputs("\nSEATS=", f
);
210 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
219 fputs(i
->seat
->id
, f
);
222 fputs("\nACTIVE_SESSIONS=", f
);
224 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
225 if (!session_is_active(i
))
236 fputs("\nONLINE_SESSIONS=", f
);
238 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
239 if (session_get_state(i
) == SESSION_CLOSING
)
250 fputs("\nACTIVE_SEATS=", f
);
252 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
253 if (!session_is_active(i
) || !i
->seat
)
261 fputs(i
->seat
->id
, f
);
264 fputs("\nONLINE_SEATS=", f
);
266 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
267 if (session_get_state(i
) == SESSION_CLOSING
|| !i
->seat
)
275 fputs(i
->seat
->id
, f
);
280 r
= flink_tmpfile(f
, temp_path
, u
->state_file
, LINK_TMPFILE_REPLACE
);
282 return log_error_errno(r
, "Failed to move '%s' into place: %m", u
->state_file
);
284 temp_path
= mfree(temp_path
); /* disarm auto-destroy: temporary file does not exist anymore */
288 int user_save(User
*u
) {
294 return user_save_internal(u
);
297 int user_load(User
*u
) {
298 _cleanup_free_
char *realtime
= NULL
, *monotonic
= NULL
, *stopping
= NULL
, *last_session_timestamp
= NULL
, *gc_mode
= NULL
;
303 r
= parse_env_file(NULL
, u
->state_file
,
304 "RUNTIME_DIR_JOB", &u
->runtime_dir_job
,
305 "SERVICE_JOB", &u
->service_manager_job
,
306 "STOPPING", &stopping
,
307 "REALTIME", &realtime
,
308 "MONOTONIC", &monotonic
,
309 "LAST_SESSION_TIMESTAMP", &last_session_timestamp
,
310 "GC_MODE", &gc_mode
);
314 return log_error_errno(r
, "Failed to read %s: %m", u
->state_file
);
317 r
= parse_boolean(stopping
);
319 log_debug_errno(r
, "Failed to parse 'STOPPING' boolean: %s", stopping
);
322 if (u
->stopping
&& !u
->runtime_dir_job
)
323 log_debug("User '%s' is stopping, but no job is being tracked.", u
->user_record
->user_name
);
328 (void) deserialize_usec(realtime
, &u
->timestamp
.realtime
);
330 (void) deserialize_usec(monotonic
, &u
->timestamp
.monotonic
);
331 if (last_session_timestamp
)
332 (void) deserialize_usec(last_session_timestamp
, &u
->last_session_timestamp
);
334 u
->gc_mode
= user_gc_mode_from_string(gc_mode
);
336 u
->gc_mode
= USER_GC_BY_PIN
;
341 static int user_start_runtime_dir(User
*u
) {
342 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
346 assert(!u
->stopping
);
348 assert(u
->runtime_dir_unit
);
350 u
->runtime_dir_job
= mfree(u
->runtime_dir_job
);
352 r
= manager_start_unit(u
->manager
, u
->runtime_dir_unit
, &error
, &u
->runtime_dir_job
);
354 return log_full_errno(sd_bus_error_has_name(&error
, BUS_ERROR_UNIT_MASKED
) ? LOG_DEBUG
: LOG_ERR
,
355 r
, "Failed to start user service '%s': %s",
356 u
->runtime_dir_unit
, bus_error_message(&error
, r
));
361 static bool user_wants_service_manager(const User
*u
) {
364 LIST_FOREACH(sessions_by_user
, s
, u
->sessions
)
365 if (SESSION_CLASS_WANTS_SERVICE_MANAGER(s
->class))
368 if (user_check_linger_file(u
) > 0)
374 int user_start_service_manager(User
*u
) {
375 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
379 assert(!u
->stopping
);
381 assert(u
->service_manager_unit
);
383 if (u
->service_manager_started
)
386 /* Only start user service manager if there's at least one session which wants it */
387 if (!user_wants_service_manager(u
))
390 u
->service_manager_job
= mfree(u
->service_manager_job
);
392 r
= manager_start_unit(u
->manager
, u
->service_manager_unit
, &error
, &u
->service_manager_job
);
394 if (sd_bus_error_has_name(&error
, BUS_ERROR_UNIT_MASKED
))
397 return log_error_errno(r
, "Failed to start user service '%s': %s",
398 u
->service_manager_unit
, bus_error_message(&error
, r
));
401 return (u
->service_manager_started
= true);
404 static int update_slice_callback(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
405 _cleanup_(user_record_unrefp
) UserRecord
*ur
= ASSERT_PTR(userdata
);
406 const sd_bus_error
*e
;
411 e
= sd_bus_message_get_error(m
);
413 r
= sd_bus_error_get_errno(e
);
415 "Failed to update slice of %s, ignoring: %s",
417 bus_error_message(e
, r
));
422 log_debug("Successfully set slice parameters of %s.", ur
->user_name
);
426 static int user_update_slice(User
*u
) {
427 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
432 if (u
->user_record
->tasks_max
== UINT64_MAX
&&
433 u
->user_record
->memory_high
== UINT64_MAX
&&
434 u
->user_record
->memory_max
== UINT64_MAX
&&
435 u
->user_record
->cpu_weight
== UINT64_MAX
&&
436 u
->user_record
->io_weight
== UINT64_MAX
)
439 r
= bus_message_new_method_call(u
->manager
->bus
, &m
, bus_systemd_mgr
, "SetUnitProperties");
441 return bus_log_create_error(r
);
443 r
= sd_bus_message_append(m
, "sb", u
->slice
, true);
445 return bus_log_create_error(r
);
447 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
449 return bus_log_create_error(r
);
455 { "TasksMax", u
->user_record
->tasks_max
},
456 { "MemoryMax", u
->user_record
->memory_max
},
457 { "MemoryHigh", u
->user_record
->memory_high
},
458 { "CPUWeight", u
->user_record
->cpu_weight
},
459 { "IOWeight", u
->user_record
->io_weight
},
462 FOREACH_ELEMENT(st
, settings
) {
463 if (st
->value
== UINT64_MAX
)
466 r
= sd_bus_message_append(m
, "(sv)", st
->name
, "t", st
->value
);
468 return bus_log_create_error(r
);
471 r
= sd_bus_message_close_container(m
);
473 return bus_log_create_error(r
);
475 r
= sd_bus_call_async(u
->manager
->bus
, NULL
, m
, update_slice_callback
, u
->user_record
, 0);
477 return log_error_errno(r
, "Failed to change user slice properties: %m");
479 /* Ref the user record pointer, so that the slot keeps it pinned */
480 user_record_ref(u
->user_record
);
485 int user_start(User
*u
) {
490 if (u
->service_manager_started
) {
491 /* Everything is up. No action needed. */
492 assert(u
->started
&& !u
->stopping
);
496 if (!u
->started
|| u
->stopping
) {
497 /* If u->stopping is set, the user is marked for removal and service stop-jobs are queued.
498 * We have to clear that flag before queueing the start-jobs again. If they succeed, the
499 * user object can be reused just fine (pid1 takes care of job-ordering and proper restart),
500 * but if they fail, we want to force another user_stop() so possibly pending units are
505 log_debug("Tracking new user %s.", u
->user_record
->user_name
);
507 /* Save the user data so far, because pam_systemd will read the XDG_RUNTIME_DIR out of it
508 * while starting up systemd --user. We need to do user_save_internal() because we have not
509 * "officially" started yet. */
510 user_save_internal(u
);
512 /* Set slice parameters */
513 (void) user_update_slice(u
);
515 (void) user_start_runtime_dir(u
);
518 /* Start user@UID.service if needed. */
519 r
= user_start_service_manager(u
);
524 if (!dual_timestamp_is_set(&u
->timestamp
))
525 dual_timestamp_now(&u
->timestamp
);
527 user_send_signal(u
, true);
531 /* Save new user data */
537 static void user_stop_service(User
*u
, bool force
) {
538 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
543 assert(u
->runtime_dir_unit
);
545 /* Note that we only stop user-runtime-dir@.service here, and let BindsTo= deal with the user@.service
546 * instance. However, we still need to clear service_manager_job here, so that if the stop is
547 * interrupted, the new sessions won't be confused by leftovers. */
549 u
->service_manager_job
= mfree(u
->service_manager_job
);
550 u
->service_manager_started
= false;
552 u
->runtime_dir_job
= mfree(u
->runtime_dir_job
);
554 r
= manager_stop_unit(u
->manager
, u
->runtime_dir_unit
, force
? "replace" : "fail", &error
, &u
->runtime_dir_job
);
556 log_warning_errno(r
, "Failed to stop user service '%s', ignoring: %s",
557 u
->runtime_dir_unit
, bus_error_message(&error
, r
));
560 int user_stop(User
*u
, bool force
) {
565 /* This is called whenever we begin with tearing down a user record. It's called in two cases: explicit API
566 * request to do so via the bus (in which case 'force' is true) and automatically due to GC, if there's no
567 * session left pinning it (in which case 'force' is false). Note that this just initiates tearing down of the
568 * user, the User object will remain in memory until user_finalize() is called, see below. */
573 if (u
->stopping
) { /* Stop jobs have already been queued */
578 LIST_FOREACH(sessions_by_user
, s
, u
->sessions
)
579 RET_GATHER(r
, session_stop(s
, force
));
581 user_stop_service(u
, force
);
590 int user_finalize(User
*u
) {
595 /* Called when the user is really ready to be freed, i.e. when all unit stop jobs and suchlike for it are
596 * done. This is called as a result of an earlier user_done() when all jobs are completed. */
599 log_debug("User %s exited.", u
->user_record
->user_name
);
601 LIST_FOREACH(sessions_by_user
, s
, u
->sessions
)
602 RET_GATHER(r
, session_finalize(s
));
604 /* Clean SysV + POSIX IPC objects, but only if this is not a system user. Background: in many setups cronjobs
605 * are run in full PAM and thus logind sessions, even if the code run doesn't belong to actual users but to
606 * system components. Since enable RemoveIPC= globally for all users, we need to be a bit careful with such
607 * cases, as we shouldn't accidentally remove a system service's IPC objects while it is running, just because
608 * a cronjob running as the same user just finished. Hence: exclude system users generally from IPC clean-up,
609 * and do it only for normal users. */
610 if (u
->manager
->remove_ipc
&& !uid_is_system(u
->user_record
->uid
))
611 RET_GATHER(r
, clean_ipc_by_uid(u
->user_record
->uid
));
613 (void) unlink(u
->state_file
);
614 user_add_to_gc_queue(u
);
617 user_send_signal(u
, false);
624 int user_get_idle_hint(User
*u
, dual_timestamp
*t
) {
625 bool idle_hint
= true;
626 dual_timestamp ts
= DUAL_TIMESTAMP_NULL
;
630 LIST_FOREACH(sessions_by_user
, s
, u
->sessions
) {
634 if (!SESSION_CLASS_CAN_IDLE(s
->class))
637 ih
= session_get_idle_hint(s
, &k
);
643 if (k
.monotonic
< ts
.monotonic
)
649 } else if (idle_hint
) {
651 if (k
.monotonic
> ts
.monotonic
)
662 int user_check_linger_file(const User
*u
) {
663 _cleanup_free_
char *cc
= NULL
;
667 assert(u
->user_record
);
669 cc
= cescape(u
->user_record
->user_name
);
673 p
= strjoina("/var/lib/systemd/linger/", cc
);
674 if (access(p
, F_OK
) < 0) {
684 static bool user_unit_active(User
*u
) {
688 assert(u
->runtime_dir_unit
);
689 assert(u
->service_manager_unit
);
691 FOREACH_STRING(i
, u
->slice
, u
->runtime_dir_unit
, u
->service_manager_unit
) {
692 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
694 r
= manager_unit_is_active(u
->manager
, i
, &error
);
696 log_debug_errno(r
, "Failed to determine whether unit '%s' is active, ignoring: %s", i
, bus_error_message(&error
, r
));
704 static usec_t
user_get_stop_delay(User
*u
) {
707 if (u
->user_record
->stop_delay_usec
!= UINT64_MAX
)
708 return u
->user_record
->stop_delay_usec
;
710 if (user_record_removable(u
->user_record
) > 0)
711 return 0; /* For removable users lower the stop delay to zero */
713 return u
->manager
->user_stop_delay
;
716 static bool user_pinned_by_sessions(User
*u
) {
719 /* Returns true if at least one session exists that shall keep the user tracking alive. That
720 * generally means one session that isn't the service manager still exists. */
722 switch (u
->gc_mode
) {
728 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
)
729 if (SESSION_CLASS_PIN_USER(i
->class))
735 assert_not_reached();
739 bool user_may_gc(User
*u
, bool drop_not_started
) {
744 if (drop_not_started
&& !u
->started
)
747 if (user_pinned_by_sessions(u
))
750 if (u
->last_session_timestamp
!= USEC_INFINITY
) {
751 usec_t user_stop_delay
;
753 /* All sessions have been closed. Let's see if we shall leave the user record around for a bit */
755 user_stop_delay
= user_get_stop_delay(u
);
757 if (user_stop_delay
== USEC_INFINITY
)
758 return false; /* Leave it around forever! */
759 if (user_stop_delay
> 0 &&
760 now(CLOCK_MONOTONIC
) < usec_add(u
->last_session_timestamp
, user_stop_delay
))
761 return false; /* Leave it around for a bit longer. */
764 /* Is this a user that shall stay around forever ("linger")? Before we say "no" to GC'ing for lingering users, let's check
765 * if any of the three units that we maintain for this user is still around. If none of them is,
766 * there's no need to keep this user around even if lingering is enabled. */
767 if (user_check_linger_file(u
) > 0 && user_unit_active(u
))
770 /* Check if our job is still pending */
772 FOREACH_ARGUMENT(j
, u
->runtime_dir_job
, u
->service_manager_job
) {
773 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
778 r
= manager_job_is_active(u
->manager
, j
, &error
);
780 log_debug_errno(r
, "Failed to determine whether job '%s' is pending, ignoring: %s",
781 j
, bus_error_message(&error
, r
));
786 /* Note that we don't care if the three units we manage for each user object are up or not, as we are
787 * managing their state rather than tracking it. */
792 void user_add_to_gc_queue(User
*u
) {
798 LIST_PREPEND(gc_queue
, u
->manager
->user_gc_queue
, u
);
799 u
->in_gc_queue
= true;
802 UserState
user_get_state(User
*u
) {
808 if (!u
->started
|| u
->runtime_dir_job
)
811 /* USER_GC_BY_PIN: Only pinning sessions count. None -> closing
812 * USER_GC_BY_ANY: 'manager' sessions also count. However, if lingering is enabled, 'lingering' state
813 * shall be preferred. 'online' if the manager is manually started by user. */
815 bool has_pinning
= false, all_closing
= true;
816 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
) {
817 bool pinned
= SESSION_CLASS_PIN_USER(i
->class);
819 if (u
->gc_mode
== USER_GC_BY_PIN
&& !pinned
)
822 has_pinning
= has_pinning
|| pinned
;
824 SessionState state
= session_get_state(i
);
825 if (state
== SESSION_ACTIVE
&& pinned
)
827 if (state
!= SESSION_CLOSING
)
831 if (!has_pinning
&& user_check_linger_file(u
) > 0 && user_unit_active(u
))
832 return USER_LINGERING
;
834 return all_closing
? USER_CLOSING
: USER_ONLINE
;
837 int user_kill(User
*u
, int signo
) {
840 return manager_kill_unit(u
->manager
, u
->slice
, KILL_ALL
, signo
, NULL
);
843 static bool elect_display_filter(Session
*s
) {
844 /* Return true if the session is a candidate for the user’s ‘primary session’ or ‘display’. */
847 return SESSION_CLASS_CAN_DISPLAY(s
->class) && s
->started
&& !s
->stopping
;
850 static int elect_display_compare(Session
*s1
, Session
*s2
) {
851 /* Indexed by SessionType. Lower numbers mean more preferred. */
852 static const int type_ranks
[_SESSION_TYPE_MAX
] = {
853 [SESSION_UNSPECIFIED
] = 0,
856 [SESSION_WAYLAND
] = -3,
861 /* Calculate the partial order relationship between s1 and s2,
862 * returning < 0 if s1 is preferred as the user’s ‘primary session’,
863 * 0 if s1 and s2 are equally preferred or incomparable, or > 0 if s2
866 * s1 or s2 may be NULL. */
870 if ((s1
== NULL
) != (s2
== NULL
))
871 return (s1
== NULL
) - (s2
== NULL
);
873 if (s1
->stopping
!= s2
->stopping
)
874 return s1
->stopping
- s2
->stopping
;
876 if ((s1
->class != SESSION_USER
) != (s2
->class != SESSION_USER
))
877 return (s1
->class != SESSION_USER
) - (s2
->class != SESSION_USER
);
879 if ((s1
->class != SESSION_USER_EARLY
) != (s2
->class != SESSION_USER_EARLY
))
880 return (s1
->class != SESSION_USER_EARLY
) - (s2
->class != SESSION_USER_EARLY
);
882 if ((s1
->type
== _SESSION_TYPE_INVALID
) != (s2
->type
== _SESSION_TYPE_INVALID
))
883 return (s1
->type
== _SESSION_TYPE_INVALID
) - (s2
->type
== _SESSION_TYPE_INVALID
);
885 if (s1
->type
!= s2
->type
)
886 return type_ranks
[s1
->type
] - type_ranks
[s2
->type
];
891 void user_elect_display(User
*u
) {
894 /* This elects a primary session for each user, which we call the "display". We try to keep the assignment
895 * stable, but we "upgrade" to better choices. */
896 log_debug("Electing new display for user %s", u
->user_record
->user_name
);
898 LIST_FOREACH(sessions_by_user
, s
, u
->sessions
) {
899 if (!elect_display_filter(s
)) {
900 log_debug("Ignoring session %s", s
->id
);
904 if (elect_display_compare(s
, u
->display
) < 0) {
905 log_debug("Choosing session %s in preference to %s", s
->id
, u
->display
? u
->display
->id
: "-");
911 static int user_stop_timeout_callback(sd_event_source
*es
, uint64_t usec
, void *userdata
) {
912 User
*u
= ASSERT_PTR(userdata
);
914 user_add_to_gc_queue(u
);
919 void user_update_last_session_timer(User
*u
) {
920 usec_t user_stop_delay
;
925 if (user_pinned_by_sessions(u
)) {
926 /* There are sessions, turn off the timer */
927 u
->last_session_timestamp
= USEC_INFINITY
;
928 u
->timer_event_source
= sd_event_source_unref(u
->timer_event_source
);
932 if (u
->last_session_timestamp
!= USEC_INFINITY
)
933 return; /* Timer already started */
935 u
->last_session_timestamp
= now(CLOCK_MONOTONIC
);
937 assert(!u
->timer_event_source
);
939 user_stop_delay
= user_get_stop_delay(u
);
940 if (!timestamp_is_set(user_stop_delay
))
943 if (sd_event_get_state(u
->manager
->event
) == SD_EVENT_FINISHED
) {
944 log_debug("Not allocating user stop timeout, since we are already exiting.");
948 r
= sd_event_add_time(u
->manager
->event
,
949 &u
->timer_event_source
,
951 usec_add(u
->last_session_timestamp
, user_stop_delay
), 0,
952 user_stop_timeout_callback
, u
);
954 log_warning_errno(r
, "Failed to enqueue user stop event source, ignoring: %m");
957 log_debug("Last session of user '%s' logged out, terminating user context in %s.",
958 u
->user_record
->user_name
,
959 FORMAT_TIMESPAN(user_stop_delay
, USEC_PER_MSEC
));
962 static const char* const user_state_table
[_USER_STATE_MAX
] = {
963 [USER_OFFLINE
] = "offline",
964 [USER_OPENING
] = "opening",
965 [USER_LINGERING
] = "lingering",
966 [USER_ONLINE
] = "online",
967 [USER_ACTIVE
] = "active",
968 [USER_CLOSING
] = "closing"
971 DEFINE_STRING_TABLE_LOOKUP(user_state
, UserState
);
973 static const char* const user_gc_mode_table
[_USER_GC_MODE_MAX
] = {
974 [USER_GC_BY_PIN
] = "pin",
975 [USER_GC_BY_ANY
] = "any",
978 DEFINE_STRING_TABLE_LOOKUP(user_gc_mode
, UserGCMode
);
980 int config_parse_tmpfs_size(
982 const char *filename
,
985 unsigned section_line
,
992 uint64_t *sz
= ASSERT_PTR(data
);
999 /* First, try to parse as percentage */
1000 r
= parse_permyriad(rvalue
);
1002 *sz
= physical_memory_scale(r
, 10000U);
1006 /* If the passed argument was not a percentage, or out of range, parse as byte size */
1008 r
= parse_size(rvalue
, 1024, &k
);
1009 if (r
>= 0 && (k
<= 0 || (uint64_t) (size_t) k
!= k
))
1012 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to parse size value '%s', ignoring: %m", rvalue
);
1016 *sz
= PAGE_ALIGN((size_t) k
);
1022 int config_parse_compat_user_tasks_max(
1024 const char *filename
,
1026 const char *section
,
1027 unsigned section_line
,
1038 log_syntax(unit
, LOG_NOTICE
, filename
, line
, 0,
1039 "Support for option %s= has been removed.",
1041 log_info("Hint: try creating /etc/systemd/system/user-.slice.d/50-limits.conf with:\n"