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