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