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