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