]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/logind-user.c
util-lib: split out all temporary file related calls into tmpfiles-util.c
[thirdparty/systemd.git] / src / login / logind-user.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
20263082 2
4f5dd394 3#include <errno.h>
20263082
LP
4#include <string.h>
5#include <unistd.h>
0d536673 6#include <stdio_ext.h>
20263082 7
b5efdb8a 8#include "alloc-util.h"
90558f31 9#include "bus-common-errors.h"
cc377381 10#include "bus-error.h"
4f5dd394 11#include "bus-util.h"
f5058264 12#include "cgroup-util.h"
66cdd0f2 13#include "clean-ipc.h"
4f5dd394 14#include "escape.h"
3ffd4af2 15#include "fd-util.h"
4f5dd394 16#include "fileio.h"
f97b34a6 17#include "format-util.h"
f4f15635 18#include "fs-util.h"
4f5dd394 19#include "hashmap.h"
9e281beb 20#include "label.h"
3ffd4af2 21#include "logind-user.h"
4f5dd394 22#include "mkdir.h"
6bedfcbb 23#include "parse-util.h"
4f5dd394
LP
24#include "path-util.h"
25#include "rm-rf.h"
d68c645b 26#include "serialize.h"
4f5dd394 27#include "special.h"
90558f31 28#include "stdio-util.h"
8b43440b 29#include "string-table.h"
4e5b605a 30#include "strv.h"
e4de7287 31#include "tmpfile-util.h"
4f5dd394 32#include "unit-name.h"
ee104e11 33#include "user-util.h"
4f5dd394 34#include "util.h"
20263082 35
d5ac9d06
LP
36int user_new(User **ret,
37 Manager *m,
38 uid_t uid,
39 gid_t gid,
40 const char *name,
41 const char *home) {
42
157f5057 43 _cleanup_(user_freep) User *u = NULL;
6230bf75 44 char lu[DECIMAL_STR_MAX(uid_t) + 1];
6230bf75 45 int r;
20263082 46
8c29a457 47 assert(ret);
20263082
LP
48 assert(m);
49 assert(name);
50
8c29a457 51 u = new(User, 1);
20263082 52 if (!u)
157f5057
DH
53 return -ENOMEM;
54
8c29a457
LP
55 *u = (User) {
56 .manager = m,
57 .uid = uid,
58 .gid = gid,
9afe9efb 59 .last_session_timestamp = USEC_INFINITY,
8c29a457 60 };
20263082
LP
61
62 u->name = strdup(name);
9444b1f2 63 if (!u->name)
157f5057 64 return -ENOMEM;
20263082 65
d5ac9d06
LP
66 u->home = strdup(home);
67 if (!u->home)
68 return -ENOMEM;
69
de0671ee 70 if (asprintf(&u->state_file, "/run/systemd/users/"UID_FMT, uid) < 0)
157f5057 71 return -ENOMEM;
20263082 72
f9e4283d 73 if (asprintf(&u->runtime_path, "/run/user/"UID_FMT, uid) < 0)
157f5057 74 return -ENOMEM;
f9e4283d 75
8c29a457 76 xsprintf(lu, UID_FMT, uid);
6230bf75
DH
77 r = slice_build_subslice(SPECIAL_USER_SLICE, lu, &u->slice);
78 if (r < 0)
157f5057 79 return r;
6230bf75 80
b690ef12
DH
81 r = unit_name_build("user", lu, ".service", &u->service);
82 if (r < 0)
83 return r;
84
25a1ab4e
LP
85 r = unit_name_build("user-runtime-dir", lu, ".service", &u->runtime_dir_service);
86 if (r < 0)
87 return r;
88
157f5057
DH
89 r = hashmap_put(m->users, UID_TO_PTR(uid), u);
90 if (r < 0)
91 return r;
20263082 92
157f5057
DH
93 r = hashmap_put(m->user_units, u->slice, u);
94 if (r < 0)
95 return r;
9444b1f2 96
b690ef12
DH
97 r = hashmap_put(m->user_units, u->service, u);
98 if (r < 0)
99 return r;
100
25a1ab4e
LP
101 r = hashmap_put(m->user_units, u->runtime_dir_service, u);
102 if (r < 0)
103 return r;
104
8c29a457 105 *ret = TAKE_PTR(u);
157f5057 106 return 0;
20263082
LP
107}
108
157f5057
DH
109User *user_free(User *u) {
110 if (!u)
111 return NULL;
20263082 112
14c3baca 113 if (u->in_gc_queue)
71fda00f 114 LIST_REMOVE(gc_queue, u->manager->user_gc_queue, u);
14c3baca 115
20263082
LP
116 while (u->sessions)
117 session_free(u->sessions);
118
157f5057
DH
119 if (u->service)
120 hashmap_remove_value(u->manager->user_units, u->service, u);
121
25a1ab4e
LP
122 if (u->runtime_dir_service)
123 hashmap_remove_value(u->manager->user_units, u->runtime_dir_service, u);
124
157f5057
DH
125 if (u->slice)
126 hashmap_remove_value(u->manager->user_units, u->slice, u);
fb6becb4 127
157f5057 128 hashmap_remove_value(u->manager->users, UID_TO_PTR(u->uid), u);
6230bf75 129
9afe9efb
LP
130 (void) sd_event_source_unref(u->timer_event_source);
131
157f5057 132 u->service_job = mfree(u->service_job);
fb6becb4 133
157f5057 134 u->service = mfree(u->service);
25a1ab4e 135 u->runtime_dir_service = mfree(u->runtime_dir_service);
157f5057
DH
136 u->slice = mfree(u->slice);
137 u->runtime_path = mfree(u->runtime_path);
138 u->state_file = mfree(u->state_file);
139 u->name = mfree(u->name);
d5ac9d06 140 u->home = mfree(u->home);
20263082 141
157f5057 142 return mfree(u);
20263082
LP
143}
144
71161305 145static int user_save_internal(User *u) {
9444b1f2
LP
146 _cleanup_free_ char *temp_path = NULL;
147 _cleanup_fclose_ FILE *f = NULL;
20263082
LP
148 int r;
149
150 assert(u);
151 assert(u->state_file);
152
37c1d5e9 153 r = mkdir_safe_label("/run/systemd/users", 0755, 0, 0, MKDIR_WARN_MODE);
20263082 154 if (r < 0)
dacd6cee 155 goto fail;
20263082 156
14c3baca
LP
157 r = fopen_temporary(u->state_file, &f, &temp_path);
158 if (r < 0)
dacd6cee 159 goto fail;
14c3baca 160
0d536673
LP
161 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
162 (void) fchmod(fileno(f), 0644);
20263082
LP
163
164 fprintf(f,
14c3baca 165 "# This is private data. Do not parse.\n"
20263082 166 "NAME=%s\n"
d865bc02
LP
167 "STATE=%s\n" /* friendly user-facing state */
168 "STOPPING=%s\n", /* low-level state */
20263082 169 u->name,
d865bc02
LP
170 user_state_to_string(user_get_state(u)),
171 yes_no(u->stopping));
20263082 172
f9e4283d 173 /* LEGACY: no-one reads RUNTIME= anymore, drop it at some point */
20263082 174 if (u->runtime_path)
9444b1f2 175 fprintf(f, "RUNTIME=%s\n", u->runtime_path);
20263082 176
fb6becb4
LP
177 if (u->service_job)
178 fprintf(f, "SERVICE_JOB=%s\n", u->service_job);
9444b1f2 179
20263082 180 if (u->display)
9444b1f2
LP
181 fprintf(f, "DISPLAY=%s\n", u->display->id);
182
183 if (dual_timestamp_is_set(&u->timestamp))
20263082 184 fprintf(f,
90b2de37
ZJS
185 "REALTIME="USEC_FMT"\n"
186 "MONOTONIC="USEC_FMT"\n",
187 u->timestamp.realtime,
188 u->timestamp.monotonic);
20263082 189
9afe9efb
LP
190 if (u->last_session_timestamp != USEC_INFINITY)
191 fprintf(f, "LAST_SESSION_TIMESTAMP=" USEC_FMT "\n",
192 u->last_session_timestamp);
193
034a2a52
LP
194 if (u->sessions) {
195 Session *i;
9b958eff 196 bool first;
034a2a52 197
0d536673 198 fputs("SESSIONS=", f);
9b958eff 199 first = true;
034a2a52 200 LIST_FOREACH(sessions_by_user, i, u->sessions) {
9b958eff
LP
201 if (first)
202 first = false;
203 else
0d536673 204 fputc(' ', f);
9b958eff 205
0d536673 206 fputs(i->id, f);
034a2a52
LP
207 }
208
0d536673 209 fputs("\nSEATS=", f);
9b958eff 210 first = true;
034a2a52 211 LIST_FOREACH(sessions_by_user, i, u->sessions) {
9b958eff
LP
212 if (!i->seat)
213 continue;
214
215 if (first)
216 first = false;
217 else
0d536673 218 fputc(' ', f);
9b958eff 219
0d536673 220 fputs(i->seat->id, f);
034a2a52
LP
221 }
222
0d536673 223 fputs("\nACTIVE_SESSIONS=", f);
9b958eff
LP
224 first = true;
225 LIST_FOREACH(sessions_by_user, i, u->sessions) {
226 if (!session_is_active(i))
227 continue;
228
229 if (first)
230 first = false;
231 else
0d536673 232 fputc(' ', f);
9b958eff 233
0d536673 234 fputs(i->id, f);
9b958eff 235 }
034a2a52 236
0d536673 237 fputs("\nONLINE_SESSIONS=", f);
2dc8f41a
CG
238 first = true;
239 LIST_FOREACH(sessions_by_user, i, u->sessions) {
240 if (session_get_state(i) == SESSION_CLOSING)
241 continue;
242
243 if (first)
244 first = false;
245 else
0d536673 246 fputc(' ', f);
2dc8f41a 247
0d536673 248 fputs(i->id, f);
2dc8f41a
CG
249 }
250
0d536673 251 fputs("\nACTIVE_SEATS=", f);
9b958eff 252 first = true;
034a2a52 253 LIST_FOREACH(sessions_by_user, i, u->sessions) {
9b958eff
LP
254 if (!session_is_active(i) || !i->seat)
255 continue;
256
257 if (first)
258 first = false;
259 else
0d536673 260 fputc(' ', f);
47acb2f1 261
0d536673 262 fputs(i->seat->id, f);
034a2a52 263 }
2dc8f41a 264
0d536673 265 fputs("\nONLINE_SEATS=", f);
2dc8f41a
CG
266 first = true;
267 LIST_FOREACH(sessions_by_user, i, u->sessions) {
268 if (session_get_state(i) == SESSION_CLOSING || !i->seat)
269 continue;
270
271 if (first)
272 first = false;
273 else
0d536673 274 fputc(' ', f);
2dc8f41a 275
0d536673 276 fputs(i->seat->id, f);
2dc8f41a 277 }
0d536673 278 fputc('\n', f);
034a2a52
LP
279 }
280
dacd6cee
LP
281 r = fflush_and_check(f);
282 if (r < 0)
283 goto fail;
14c3baca 284
dacd6cee 285 if (rename(temp_path, u->state_file) < 0) {
20263082 286 r = -errno;
dacd6cee 287 goto fail;
20263082
LP
288 }
289
dacd6cee
LP
290 return 0;
291
292fail:
293 (void) unlink(u->state_file);
294
295 if (temp_path)
296 (void) unlink(temp_path);
14c3baca 297
dacd6cee 298 return log_error_errno(r, "Failed to save user data %s: %m", u->state_file);
20263082
LP
299}
300
71161305
SM
301int user_save(User *u) {
302 assert(u);
303
304 if (!u->started)
305 return 0;
306
8c29a457 307 return user_save_internal(u);
71161305
SM
308}
309
20263082 310int user_load(User *u) {
9afe9efb 311 _cleanup_free_ char *realtime = NULL, *monotonic = NULL, *stopping = NULL, *last_session_timestamp = NULL;
9444b1f2 312 int r;
20263082
LP
313
314 assert(u);
315
aa8fbc74 316 r = parse_env_file(NULL, u->state_file,
9afe9efb
LP
317 "SERVICE_JOB", &u->service_job,
318 "STOPPING", &stopping,
319 "REALTIME", &realtime,
320 "MONOTONIC", &monotonic,
13df9c39 321 "LAST_SESSION_TIMESTAMP", &last_session_timestamp);
1c8280fd
LP
322 if (r == -ENOENT)
323 return 0;
324 if (r < 0)
d710aaf7 325 return log_error_errno(r, "Failed to read %s: %m", u->state_file);
20263082 326
d865bc02
LP
327 if (stopping) {
328 r = parse_boolean(stopping);
329 if (r < 0)
330 log_debug_errno(r, "Failed to parse 'STOPPING' boolean: %s", stopping);
331 else
332 u->stopping = r;
333 }
334
b895a735 335 if (realtime)
d68c645b 336 (void) deserialize_usec(realtime, &u->timestamp.realtime);
b895a735 337 if (monotonic)
d68c645b 338 (void) deserialize_usec(monotonic, &u->timestamp.monotonic);
9afe9efb 339 if (last_session_timestamp)
d68c645b 340 (void) deserialize_usec(last_session_timestamp, &u->last_session_timestamp);
9444b1f2 341
d865bc02 342 return 0;
20263082
LP
343}
344
25a1ab4e 345static void user_start_service(User *u) {
4afd3348 346 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
fb6becb4
LP
347 int r;
348
20263082
LP
349 assert(u);
350
25a1ab4e
LP
351 /* Start the service containing the "systemd --user" instance (user@.service). Note that we don't explicitly
352 * start the per-user slice or the systemd-runtime-dir@.service instance, as those are pulled in both by
353 * user@.service and the session scopes as dependencies. */
354
b690ef12 355 u->service_job = mfree(u->service_job);
fb6becb4 356
25a1ab4e 357 r = manager_start_unit(u->manager, u->service, &error, &u->service_job);
545a30a9 358 if (r < 0)
25a1ab4e 359 log_warning_errno(r, "Failed to start user service '%s', ignoring: %s", u->service, bus_error_message(&error, r));
20263082
LP
360}
361
362int user_start(User *u) {
20263082
LP
363 assert(u);
364
a832ab6f 365 if (u->started && !u->stopping)
9418f147
LP
366 return 0;
367
25a1ab4e
LP
368 /* If u->stopping is set, the user is marked for removal and service stop-jobs are queued. We have to clear
369 * that flag before queing the start-jobs again. If they succeed, the user object can be re-used just fine
370 * (pid1 takes care of job-ordering and proper restart), but if they fail, we want to force another user_stop()
371 * so possibly pending units are stopped. */
a832ab6f
DH
372 u->stopping = false;
373
a9f0f5e5 374 if (!u->started)
8b5c4d16 375 log_debug("Starting services for new user %s.", u->name);
a832ab6f 376
25a1ab4e
LP
377 /* Save the user data so far, because pam_systemd will read the XDG_RUNTIME_DIR out of it while starting up
378 * systemd --user. We need to do user_save_internal() because we have not "officially" started yet. */
71161305
SM
379 user_save_internal(u);
380
25a1ab4e
LP
381 /* Start user@UID.service */
382 user_start_service(u);
20263082 383
a832ab6f
DH
384 if (!u->started) {
385 if (!dual_timestamp_is_set(&u->timestamp))
386 dual_timestamp_get(&u->timestamp);
387 user_send_signal(u, true);
388 u->started = true;
389 }
9418f147 390
7f7bb946
LP
391 /* Save new user data */
392 user_save(u);
393
20263082
LP
394 return 0;
395}
396
25a1ab4e 397static void user_stop_service(User *u) {
4afd3348 398 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
fb6becb4 399 int r;
20263082 400
20263082 401 assert(u);
25a1ab4e 402 assert(u->service);
20263082 403
25a1ab4e
LP
404 /* The reverse of user_start_service(). Note that we only stop user@UID.service here, and let StopWhenUnneeded=
405 * deal with the slice and the user-runtime-dir@.service instance. */
20263082 406
25a1ab4e 407 u->service_job = mfree(u->service_job);
20263082 408
25a1ab4e 409 r = manager_stop_unit(u->manager, u->service, &error, &u->service_job);
fb2367ed 410 if (r < 0)
25a1ab4e 411 log_warning_errno(r, "Failed to stop user service '%s', ignoring: %s", u->service, bus_error_message(&error, r));
fb6becb4 412}
20263082 413
9bb69af4 414int user_stop(User *u, bool force) {
20263082 415 Session *s;
25a1ab4e 416 int r = 0;
20263082
LP
417 assert(u);
418
25a1ab4e
LP
419 /* This is called whenever we begin with tearing down a user record. It's called in two cases: explicit API
420 * request to do so via the bus (in which case 'force' is true) and automatically due to GC, if there's no
421 * session left pinning it (in which case 'force' is false). Note that this just initiates tearing down of the
422 * user, the User object will remain in memory until user_finalize() is called, see below. */
423
424 if (!u->started)
425 return 0;
426
427 if (u->stopping) { /* Stop jobs have already been queued */
b58b227a 428 user_save(u);
25a1ab4e 429 return 0;
b58b227a
DH
430 }
431
20263082 432 LIST_FOREACH(sessions_by_user, s, u->sessions) {
25a1ab4e
LP
433 int k;
434
9bb69af4 435 k = session_stop(s, force);
20263082
LP
436 if (k < 0)
437 r = k;
438 }
439
25a1ab4e 440 user_stop_service(u);
20263082 441
5f41d1f1
LP
442 u->stopping = true;
443
405e0255
LP
444 user_save(u);
445
446 return r;
447}
448
449int user_finalize(User *u) {
450 Session *s;
451 int r = 0, k;
452
453 assert(u);
454
25a1ab4e
LP
455 /* Called when the user is really ready to be freed, i.e. when all unit stop jobs and suchlike for it are
456 * done. This is called as a result of an earlier user_done() when all jobs are completed. */
457
405e0255
LP
458 if (u->started)
459 log_debug("User %s logged out.", u->name);
460
461 LIST_FOREACH(sessions_by_user, s, u->sessions) {
462 k = session_finalize(s);
463 if (k < 0)
464 r = k;
465 }
466
00d9ef85
LP
467 /* Clean SysV + POSIX IPC objects, but only if this is not a system user. Background: in many setups cronjobs
468 * are run in full PAM and thus logind sessions, even if the code run doesn't belong to actual users but to
469 * system components. Since enable RemoveIPC= globally for all users, we need to be a bit careful with such
470 * cases, as we shouldn't accidentally remove a system service's IPC objects while it is running, just because
471 * a cronjob running as the same user just finished. Hence: exclude system users generally from IPC clean-up,
472 * and do it only for normal users. */
ece877d4 473 if (u->manager->remove_ipc && !uid_is_system(u->uid)) {
00d9ef85 474 k = clean_ipc_by_uid(u->uid);
66cdd0f2
LP
475 if (k < 0)
476 r = k;
477 }
478
75bbdf47 479 (void) unlink(u->state_file);
d2f92cdf
LP
480 user_add_to_gc_queue(u);
481
405e0255 482 if (u->started) {
ed18b08b 483 user_send_signal(u, false);
405e0255
LP
484 u->started = false;
485 }
9418f147 486
20263082
LP
487 return r;
488}
489
a185c5aa
LP
490int user_get_idle_hint(User *u, dual_timestamp *t) {
491 Session *s;
492 bool idle_hint = true;
5cb14b37 493 dual_timestamp ts = DUAL_TIMESTAMP_NULL;
a185c5aa
LP
494
495 assert(u);
496
497 LIST_FOREACH(sessions_by_user, s, u->sessions) {
498 dual_timestamp k;
499 int ih;
500
501 ih = session_get_idle_hint(s, &k);
502 if (ih < 0)
503 return ih;
504
505 if (!ih) {
506 if (!idle_hint) {
507 if (k.monotonic < ts.monotonic)
508 ts = k;
509 } else {
510 idle_hint = false;
511 ts = k;
512 }
513 } else if (idle_hint) {
514
515 if (k.monotonic > ts.monotonic)
516 ts = k;
517 }
518 }
519
520 if (t)
521 *t = ts;
522
523 return idle_hint;
524}
525
3a9f7a30 526int user_check_linger_file(User *u) {
cc377381
LP
527 _cleanup_free_ char *cc = NULL;
528 char *p = NULL;
e96cd586 529
cc377381
LP
530 cc = cescape(u->name);
531 if (!cc)
e96cd586
LP
532 return -ENOMEM;
533
63c372cb 534 p = strjoina("/var/lib/systemd/linger/", cc);
6996df9b
LP
535 if (access(p, F_OK) < 0) {
536 if (errno != ENOENT)
537 return -errno;
e96cd586 538
6996df9b
LP
539 return false;
540 }
541
542 return true;
e96cd586
LP
543}
544
4e5b605a
LP
545static bool user_unit_active(User *u) {
546 const char *i;
547 int r;
548
549 assert(u->service);
550 assert(u->runtime_dir_service);
551 assert(u->slice);
552
553 FOREACH_STRING(i, u->service, u->runtime_dir_service, u->slice) {
554 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
555
556 r = manager_unit_is_active(u->manager, i, &error);
557 if (r < 0)
558 log_debug_errno(r, "Failed to determine whether unit '%s' is active, ignoring: %s", u->service, bus_error_message(&error, r));
559 if (r != 0)
560 return true;
561 }
562
563 return false;
564}
565
5c093a23 566bool user_may_gc(User *u, bool drop_not_started) {
bd26aee1
LP
567 int r;
568
20263082
LP
569 assert(u);
570
4a4b033f 571 if (drop_not_started && !u->started)
5c093a23 572 return true;
932e3ee7 573
20263082 574 if (u->sessions)
5c093a23 575 return false;
20263082 576
9afe9efb
LP
577 if (u->last_session_timestamp != USEC_INFINITY) {
578 /* All sessions have been closed. Let's see if we shall leave the user record around for a bit */
579
580 if (u->manager->user_stop_delay == USEC_INFINITY)
581 return false; /* Leave it around forever! */
582 if (u->manager->user_stop_delay > 0 &&
583 now(CLOCK_MONOTONIC) < usec_add(u->last_session_timestamp, u->manager->user_stop_delay))
584 return false; /* Leave it around for a bit longer. */
585 }
586
4e5b605a
LP
587 /* Is this a user that shall stay around forever ("linger")? Before we say "no" to GC'ing for lingering users, let's check
588 * if any of the three units that we maintain for this user is still around. If none of them is,
589 * there's no need to keep this user around even if lingering is enabled. */
590 if (user_check_linger_file(u) > 0 && user_unit_active(u))
5c093a23 591 return false;
20263082 592
bd26aee1
LP
593 /* Check if our job is still pending */
594 if (u->service_job) {
595 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
cc377381 596
bd26aee1
LP
597 r = manager_job_is_active(u->manager, u->service_job, &error);
598 if (r < 0)
599 log_debug_errno(r, "Failed to determine whether job '%s' is pending, ignoring: %s", u->service_job, bus_error_message(&error, r));
600 if (r != 0)
601 return false;
602 }
603
604 /* Note that we don't care if the three units we manage for each user object are up or not, as we are managing
605 * their state rather than tracking it. */
405e0255 606
5c093a23 607 return true;
20263082
LP
608}
609
14c3baca
LP
610void user_add_to_gc_queue(User *u) {
611 assert(u);
612
613 if (u->in_gc_queue)
614 return;
615
71fda00f 616 LIST_PREPEND(gc_queue, u->manager->user_gc_queue, u);
14c3baca
LP
617 u->in_gc_queue = true;
618}
619
20263082
LP
620UserState user_get_state(User *u) {
621 Session *i;
622
623 assert(u);
624
5f41d1f1
LP
625 if (u->stopping)
626 return USER_CLOSING;
627
25a1ab4e 628 if (!u->started || u->service_job)
405e0255 629 return USER_OPENING;
c9caad80 630
5f41d1f1
LP
631 if (u->sessions) {
632 bool all_closing = true;
633
634 LIST_FOREACH(sessions_by_user, i, u->sessions) {
00555a2e
DH
635 SessionState state;
636
637 state = session_get_state(i);
638 if (state == SESSION_ACTIVE)
5f41d1f1 639 return USER_ACTIVE;
00555a2e 640 if (state != SESSION_CLOSING)
5f41d1f1
LP
641 all_closing = false;
642 }
20263082 643
c9caad80 644 return all_closing ? USER_CLOSING : USER_ONLINE;
5f41d1f1 645 }
e96cd586 646
4e5b605a 647 if (user_check_linger_file(u) > 0 && user_unit_active(u))
e96cd586
LP
648 return USER_LINGERING;
649
650 return USER_CLOSING;
20263082
LP
651}
652
de07ab16 653int user_kill(User *u, int signo) {
de07ab16
LP
654 assert(u);
655
fb6becb4 656 return manager_kill_unit(u->manager, u->slice, KILL_ALL, signo, NULL);
de07ab16
LP
657}
658
cde40acc 659static bool elect_display_filter(Session *s) {
04857cd8 660 /* Return true if the session is a candidate for the user’s ‘primary session’ or ‘display’. */
7ffeb45c
PW
661 assert(s);
662
04857cd8 663 return s->class == SESSION_USER && s->started && !s->stopping;
7ffeb45c
PW
664}
665
cde40acc 666static int elect_display_compare(Session *s1, Session *s2) {
7ffeb45c
PW
667 /* Indexed by SessionType. Lower numbers mean more preferred. */
668 const int type_ranks[_SESSION_TYPE_MAX] = {
669 [SESSION_UNSPECIFIED] = 0,
670 [SESSION_TTY] = -2,
671 [SESSION_X11] = -3,
672 [SESSION_WAYLAND] = -3,
673 [SESSION_MIR] = -3,
674 [SESSION_WEB] = -1,
675 };
676
677 /* Calculate the partial order relationship between s1 and s2,
678 * returning < 0 if s1 is preferred as the user’s ‘primary session’,
679 * 0 if s1 and s2 are equally preferred or incomparable, or > 0 if s2
680 * is preferred.
681 *
682 * s1 or s2 may be NULL. */
b9460fdc
RC
683 if (!s1 && !s2)
684 return 0;
685
7ffeb45c
PW
686 if ((s1 == NULL) != (s2 == NULL))
687 return (s1 == NULL) - (s2 == NULL);
688
689 if (s1->stopping != s2->stopping)
690 return s1->stopping - s2->stopping;
691
692 if ((s1->class != SESSION_USER) != (s2->class != SESSION_USER))
693 return (s1->class != SESSION_USER) - (s2->class != SESSION_USER);
694
695 if ((s1->type == _SESSION_TYPE_INVALID) != (s2->type == _SESSION_TYPE_INVALID))
696 return (s1->type == _SESSION_TYPE_INVALID) - (s2->type == _SESSION_TYPE_INVALID);
697
698 if (s1->type != s2->type)
699 return type_ranks[s1->type] - type_ranks[s2->type];
700
701 return 0;
702}
703
952d3260 704void user_elect_display(User *u) {
7ffeb45c 705 Session *s;
952d3260
LP
706
707 assert(u);
708
04857cd8
LP
709 /* This elects a primary session for each user, which we call the "display". We try to keep the assignment
710 * stable, but we "upgrade" to better choices. */
7ffeb45c 711 log_debug("Electing new display for user %s", u->name);
952d3260
LP
712
713 LIST_FOREACH(sessions_by_user, s, u->sessions) {
7ffeb45c
PW
714 if (!elect_display_filter(s)) {
715 log_debug("Ignoring session %s", s->id);
952d3260 716 continue;
7ffeb45c 717 }
952d3260 718
7ffeb45c
PW
719 if (elect_display_compare(s, u->display) < 0) {
720 log_debug("Choosing session %s in preference to %s", s->id, u->display ? u->display->id : "-");
721 u->display = s;
722 }
e9e74f28 723 }
952d3260
LP
724}
725
9afe9efb
LP
726static int user_stop_timeout_callback(sd_event_source *es, uint64_t usec, void *userdata) {
727 User *u = userdata;
728
729 assert(u);
730 user_add_to_gc_queue(u);
731
732 return 0;
733}
734
735void user_update_last_session_timer(User *u) {
736 int r;
737
738 assert(u);
739
740 if (u->sessions) {
741 /* There are sessions, turn off the timer */
742 u->last_session_timestamp = USEC_INFINITY;
743 u->timer_event_source = sd_event_source_unref(u->timer_event_source);
744 return;
745 }
746
747 if (u->last_session_timestamp != USEC_INFINITY)
748 return; /* Timer already started */
749
750 u->last_session_timestamp = now(CLOCK_MONOTONIC);
751
752 assert(!u->timer_event_source);
753
754 if (u->manager->user_stop_delay == 0 || u->manager->user_stop_delay == USEC_INFINITY)
755 return;
756
757 if (sd_event_get_state(u->manager->event) == SD_EVENT_FINISHED) {
758 log_debug("Not allocating user stop timeout, since we are already exiting.");
759 return;
760 }
761
762 r = sd_event_add_time(u->manager->event,
763 &u->timer_event_source,
764 CLOCK_MONOTONIC,
765 usec_add(u->last_session_timestamp, u->manager->user_stop_delay), 0,
766 user_stop_timeout_callback, u);
767 if (r < 0)
768 log_warning_errno(r, "Failed to enqueue user stop event source, ignoring: %m");
769
770 if (DEBUG_LOGGING) {
771 char s[FORMAT_TIMESPAN_MAX];
772
773 log_debug("Last session of user '%s' logged out, terminating user context in %s.",
774 u->name,
775 format_timespan(s, sizeof(s), u->manager->user_stop_delay, USEC_PER_MSEC));
776 }
777}
778
20263082
LP
779static const char* const user_state_table[_USER_STATE_MAX] = {
780 [USER_OFFLINE] = "offline",
fb6becb4 781 [USER_OPENING] = "opening",
20263082
LP
782 [USER_LINGERING] = "lingering",
783 [USER_ONLINE] = "online",
e96cd586
LP
784 [USER_ACTIVE] = "active",
785 [USER_CLOSING] = "closing"
20263082
LP
786};
787
788DEFINE_STRING_TABLE_LOOKUP(user_state, UserState);
1c231f56
LP
789
790int config_parse_tmpfs_size(
791 const char* unit,
792 const char *filename,
793 unsigned line,
794 const char *section,
795 unsigned section_line,
796 const char *lvalue,
797 int ltype,
798 const char *rvalue,
799 void *data,
800 void *userdata) {
801
a7b46b7d 802 uint64_t *sz = data;
1c231f56
LP
803 int r;
804
805 assert(filename);
806 assert(lvalue);
807 assert(rvalue);
808 assert(data);
809
9184ca48 810 /* First, try to parse as percentage */
f806dfd3
LP
811 r = parse_permille(rvalue);
812 if (r > 0 && r < 1000)
813 *sz = physical_memory_scale(r, 1000U);
9184ca48 814 else {
59f448cf 815 uint64_t k;
1c231f56 816
9184ca48
LP
817 /* If the passed argument was not a percentage, or out of range, parse as byte size */
818
59f448cf 819 r = parse_size(rvalue, 1024, &k);
1e5f4e8b
YW
820 if (r >= 0 && (k <= 0 || (uint64_t) (size_t) k != k))
821 r = -ERANGE;
822 if (r < 0) {
823 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value '%s', ignoring: %m", rvalue);
1c231f56
LP
824 return 0;
825 }
826
59f448cf 827 *sz = PAGE_ALIGN((size_t) k);
1c231f56
LP
828 }
829
830 return 0;
831}
c06eec15 832
28414939
ZJS
833int config_parse_compat_user_tasks_max(
834 const char *unit,
c06eec15
LP
835 const char *filename,
836 unsigned line,
837 const char *section,
838 unsigned section_line,
839 const char *lvalue,
840 int ltype,
841 const char *rvalue,
842 void *data,
843 void *userdata) {
844
c06eec15
LP
845 assert(filename);
846 assert(lvalue);
847 assert(rvalue);
848 assert(data);
849
28414939
ZJS
850 log_syntax(unit, LOG_NOTICE, filename, line, 0,
851 "Support for option %s= has been removed.",
852 lvalue);
948f7ce4 853 log_info("Hint: try creating /etc/systemd/system/user-.slice.d/50-limits.conf with:\n"
28414939
ZJS
854 " [Slice]\n"
855 " TasksMax=%s",
856 rvalue);
c06eec15
LP
857 return 0;
858}