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