]>
Commit | Line | Data |
---|---|---|
20263082 LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2011 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <string.h> | |
23 | #include <unistd.h> | |
24 | #include <errno.h> | |
25 | ||
90821c93 | 26 | #include "logind-user.h" |
20263082 LP |
27 | #include "util.h" |
28 | #include "cgroup-util.h" | |
29 | #include "hashmap.h" | |
30 | #include "strv.h" | |
31 | ||
32 | User* user_new(Manager *m, uid_t uid, gid_t gid, const char *name) { | |
33 | User *u; | |
34 | ||
35 | assert(m); | |
36 | assert(name); | |
37 | ||
14c3baca | 38 | u = new0(User, 1); |
20263082 LP |
39 | if (!u) |
40 | return NULL; | |
41 | ||
42 | u->name = strdup(name); | |
43 | if (!u->name) { | |
44 | free(u); | |
45 | return NULL; | |
46 | } | |
47 | ||
98a28fef | 48 | if (asprintf(&u->state_file, "/run/systemd/users/%lu", (unsigned long) uid) < 0) { |
20263082 LP |
49 | free(u->name); |
50 | free(u); | |
51 | return NULL; | |
52 | } | |
53 | ||
54 | if (hashmap_put(m->users, ULONG_TO_PTR((unsigned long) uid), u) < 0) { | |
55 | free(u->state_file); | |
56 | free(u->name); | |
57 | free(u); | |
58 | return NULL; | |
59 | } | |
60 | ||
61 | u->manager = m; | |
62 | u->uid = uid; | |
63 | u->gid = gid; | |
64 | ||
65 | return u; | |
66 | } | |
67 | ||
68 | void user_free(User *u) { | |
69 | assert(u); | |
70 | ||
14c3baca LP |
71 | if (u->in_gc_queue) |
72 | LIST_REMOVE(User, gc_queue, u->manager->user_gc_queue, u); | |
73 | ||
20263082 LP |
74 | while (u->sessions) |
75 | session_free(u->sessions); | |
76 | ||
77 | free(u->cgroup_path); | |
78 | ||
79 | free(u->service); | |
80 | free(u->runtime_path); | |
81 | ||
82 | hashmap_remove(u->manager->users, ULONG_TO_PTR((unsigned long) u->uid)); | |
83 | ||
84 | free(u->name); | |
d2f92cdf | 85 | free(u->state_file); |
20263082 LP |
86 | free(u); |
87 | } | |
88 | ||
89 | int user_save(User *u) { | |
90 | FILE *f; | |
91 | int r; | |
14c3baca | 92 | char *temp_path; |
20263082 LP |
93 | |
94 | assert(u); | |
95 | assert(u->state_file); | |
96 | ||
98a28fef | 97 | r = safe_mkdir("/run/systemd/users", 0755, 0, 0); |
20263082 | 98 | if (r < 0) |
14c3baca | 99 | goto finish; |
20263082 | 100 | |
14c3baca LP |
101 | r = fopen_temporary(u->state_file, &f, &temp_path); |
102 | if (r < 0) | |
103 | goto finish; | |
104 | ||
105 | fchmod(fileno(f), 0644); | |
20263082 LP |
106 | |
107 | fprintf(f, | |
14c3baca | 108 | "# This is private data. Do not parse.\n" |
20263082 LP |
109 | "NAME=%s\n" |
110 | "STATE=%s\n", | |
111 | u->name, | |
112 | user_state_to_string(user_get_state(u))); | |
113 | ||
114 | if (u->cgroup_path) | |
115 | fprintf(f, | |
116 | "CGROUP=%s\n", | |
117 | u->cgroup_path); | |
118 | ||
119 | if (u->runtime_path) | |
120 | fprintf(f, | |
121 | "RUNTIME=%s\n", | |
122 | u->runtime_path); | |
123 | ||
124 | if (u->service) | |
125 | fprintf(f, | |
126 | "SERVICE=%s\n", | |
127 | u->service); | |
128 | ||
129 | if (u->display) | |
130 | fprintf(f, | |
131 | "DISPLAY=%s\n", | |
132 | u->display->id); | |
133 | ||
134 | fflush(f); | |
14c3baca LP |
135 | |
136 | if (ferror(f) || rename(temp_path, u->state_file) < 0) { | |
20263082 LP |
137 | r = -errno; |
138 | unlink(u->state_file); | |
14c3baca | 139 | unlink(temp_path); |
20263082 LP |
140 | } |
141 | ||
142 | fclose(f); | |
14c3baca LP |
143 | free(temp_path); |
144 | ||
145 | finish: | |
146 | if (r < 0) | |
147 | log_error("Failed to save user data for %s: %s", u->name, strerror(-r)); | |
148 | ||
20263082 LP |
149 | return r; |
150 | } | |
151 | ||
152 | int user_load(User *u) { | |
153 | int r; | |
154 | char *display = NULL; | |
98a28fef | 155 | Session *s = NULL; |
20263082 LP |
156 | |
157 | assert(u); | |
158 | ||
a185c5aa | 159 | r = parse_env_file(u->state_file, NEWLINE, |
20263082 LP |
160 | "CGROUP", &u->cgroup_path, |
161 | "RUNTIME", &u->runtime_path, | |
162 | "SERVICE", &u->service, | |
163 | "DISPLAY", &display, | |
164 | NULL); | |
165 | if (r < 0) { | |
166 | free(display); | |
167 | ||
168 | if (r == -ENOENT) | |
169 | return 0; | |
170 | ||
171 | log_error("Failed to read %s: %s", u->state_file, strerror(-r)); | |
172 | return r; | |
173 | } | |
174 | ||
98a28fef LP |
175 | if (display) { |
176 | s = hashmap_get(u->manager->sessions, display); | |
177 | free(display); | |
178 | } | |
20263082 LP |
179 | |
180 | if (s && s->display && x11_display_is_local(s->display)) | |
181 | u->display = s; | |
182 | ||
183 | return r; | |
184 | } | |
185 | ||
186 | static int user_mkdir_runtime_path(User *u) { | |
187 | char *p; | |
188 | int r; | |
189 | ||
190 | assert(u); | |
191 | ||
192 | r = safe_mkdir("/run/user", 0755, 0, 0); | |
193 | if (r < 0) { | |
194 | log_error("Failed to create /run/user: %s", strerror(-r)); | |
195 | return r; | |
196 | } | |
197 | ||
198 | if (!u->runtime_path) { | |
199 | p = strappend("/run/user/", u->name); | |
200 | ||
201 | if (!p) { | |
202 | log_error("Out of memory"); | |
203 | return -ENOMEM; | |
204 | } | |
205 | } else | |
206 | p = u->runtime_path; | |
207 | ||
208 | r = safe_mkdir(p, 0700, u->uid, u->gid); | |
209 | if (r < 0) { | |
210 | log_error("Failed to create runtime directory %s: %s", p, strerror(-r)); | |
211 | free(p); | |
212 | u->runtime_path = NULL; | |
213 | return r; | |
214 | } | |
215 | ||
216 | u->runtime_path = p; | |
217 | return 0; | |
218 | } | |
219 | ||
220 | static int user_create_cgroup(User *u) { | |
221 | char **k; | |
222 | char *p; | |
223 | int r; | |
224 | ||
225 | assert(u); | |
226 | ||
227 | if (!u->cgroup_path) { | |
228 | if (asprintf(&p, "%s/%s", u->manager->cgroup_path, u->name) < 0) { | |
229 | log_error("Out of memory"); | |
230 | return -ENOMEM; | |
231 | } | |
232 | } else | |
233 | p = u->cgroup_path; | |
234 | ||
235 | r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p); | |
236 | if (r < 0) { | |
237 | free(p); | |
238 | u->cgroup_path = NULL; | |
239 | log_error("Failed to create cgroup "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r)); | |
240 | return r; | |
241 | } | |
242 | ||
243 | u->cgroup_path = p; | |
244 | ||
245 | STRV_FOREACH(k, u->manager->controllers) { | |
246 | r = cg_create(*k, p); | |
247 | if (r < 0) | |
248 | log_warning("Failed to create cgroup %s:%s: %s", *k, p, strerror(-r)); | |
249 | } | |
250 | ||
251 | return 0; | |
252 | } | |
253 | ||
254 | static int user_start_service(User *u) { | |
255 | assert(u); | |
256 | ||
257 | return 0; | |
258 | } | |
259 | ||
260 | int user_start(User *u) { | |
261 | int r; | |
262 | ||
263 | assert(u); | |
264 | ||
9418f147 LP |
265 | if (u->started) |
266 | return 0; | |
267 | ||
ed18b08b LP |
268 | log_info("New user %s logged in.", u->name); |
269 | ||
20263082 LP |
270 | /* Make XDG_RUNTIME_DIR */ |
271 | r = user_mkdir_runtime_path(u); | |
272 | if (r < 0) | |
273 | return r; | |
274 | ||
275 | /* Create cgroup */ | |
276 | r = user_create_cgroup(u); | |
277 | if (r < 0) | |
278 | return r; | |
279 | ||
280 | /* Spawn user systemd */ | |
281 | r = user_start_service(u); | |
282 | if (r < 0) | |
283 | return r; | |
284 | ||
14c3baca LP |
285 | /* Save new user data */ |
286 | user_save(u); | |
287 | ||
20263082 LP |
288 | dual_timestamp_get(&u->timestamp); |
289 | ||
9418f147 LP |
290 | u->started = true; |
291 | ||
da119395 LP |
292 | user_send_signal(u, true); |
293 | ||
20263082 LP |
294 | return 0; |
295 | } | |
296 | ||
297 | static int user_stop_service(User *u) { | |
298 | assert(u); | |
299 | ||
300 | if (!u->service) | |
301 | return 0; | |
302 | ||
303 | return 0; | |
304 | } | |
305 | ||
306 | static int user_shall_kill(User *u) { | |
307 | assert(u); | |
308 | ||
ed18b08b LP |
309 | if (!u->manager->kill_user_processes) |
310 | return false; | |
311 | ||
312 | if (strv_contains(u->manager->kill_exclude_users, u->name)) | |
313 | return false; | |
314 | ||
315 | if (strv_isempty(u->manager->kill_only_users)) | |
316 | return true; | |
317 | ||
318 | return strv_contains(u->manager->kill_only_users, u->name); | |
20263082 LP |
319 | } |
320 | ||
321 | static int user_kill_cgroup(User *u) { | |
322 | int r; | |
323 | char **k; | |
324 | ||
325 | assert(u); | |
326 | ||
327 | if (!u->cgroup_path) | |
328 | return 0; | |
329 | ||
330 | cg_trim(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false); | |
331 | ||
332 | if (user_shall_kill(u)) { | |
333 | ||
334 | r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true); | |
335 | if (r < 0) | |
336 | log_error("Failed to kill user cgroup: %s", strerror(-r)); | |
337 | } else { | |
338 | ||
339 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true); | |
340 | if (r < 0) | |
341 | log_error("Failed to check user cgroup: %s", strerror(-r)); | |
342 | else if (r > 0) { | |
343 | r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path); | |
344 | if (r < 0) | |
345 | log_error("Failed to delete user cgroup: %s", strerror(-r)); | |
346 | } else | |
347 | r = -EBUSY; | |
348 | } | |
349 | ||
350 | STRV_FOREACH(k, u->manager->controllers) | |
351 | cg_trim(*k, u->cgroup_path, true); | |
352 | ||
353 | free(u->cgroup_path); | |
354 | u->cgroup_path = NULL; | |
355 | ||
356 | return r; | |
357 | } | |
358 | ||
359 | static int user_remove_runtime_path(User *u) { | |
360 | int r; | |
361 | ||
362 | assert(u); | |
363 | ||
364 | if (!u->runtime_path) | |
365 | return 0; | |
366 | ||
367 | r = rm_rf(u->runtime_path, false, true); | |
368 | if (r < 0) | |
369 | log_error("Failed to remove runtime directory %s: %s", u->runtime_path, strerror(-r)); | |
370 | ||
371 | free(u->runtime_path); | |
372 | u->runtime_path = NULL; | |
373 | ||
374 | return r; | |
375 | } | |
376 | ||
377 | int user_stop(User *u) { | |
378 | Session *s; | |
379 | int r = 0, k; | |
380 | assert(u); | |
381 | ||
ed18b08b LP |
382 | if (u->started) |
383 | log_info("User %s logged out.", u->name); | |
9418f147 | 384 | |
20263082 LP |
385 | LIST_FOREACH(sessions_by_user, s, u->sessions) { |
386 | k = session_stop(s); | |
387 | if (k < 0) | |
388 | r = k; | |
389 | } | |
390 | ||
391 | /* Kill systemd */ | |
392 | k = user_stop_service(u); | |
393 | if (k < 0) | |
394 | r = k; | |
395 | ||
396 | /* Kill cgroup */ | |
397 | k = user_kill_cgroup(u); | |
398 | if (k < 0) | |
399 | r = k; | |
400 | ||
401 | /* Kill XDG_RUNTIME_DIR */ | |
402 | k = user_remove_runtime_path(u); | |
403 | if (k < 0) | |
404 | r = k; | |
405 | ||
d2f92cdf LP |
406 | unlink(u->state_file); |
407 | user_add_to_gc_queue(u); | |
408 | ||
ed18b08b LP |
409 | if (u->started) |
410 | user_send_signal(u, false); | |
411 | ||
9418f147 LP |
412 | u->started = false; |
413 | ||
20263082 LP |
414 | return r; |
415 | } | |
416 | ||
a185c5aa LP |
417 | int user_get_idle_hint(User *u, dual_timestamp *t) { |
418 | Session *s; | |
419 | bool idle_hint = true; | |
420 | dual_timestamp ts = { 0, 0 }; | |
421 | ||
422 | assert(u); | |
423 | ||
424 | LIST_FOREACH(sessions_by_user, s, u->sessions) { | |
425 | dual_timestamp k; | |
426 | int ih; | |
427 | ||
428 | ih = session_get_idle_hint(s, &k); | |
429 | if (ih < 0) | |
430 | return ih; | |
431 | ||
432 | if (!ih) { | |
433 | if (!idle_hint) { | |
434 | if (k.monotonic < ts.monotonic) | |
435 | ts = k; | |
436 | } else { | |
437 | idle_hint = false; | |
438 | ts = k; | |
439 | } | |
440 | } else if (idle_hint) { | |
441 | ||
442 | if (k.monotonic > ts.monotonic) | |
443 | ts = k; | |
444 | } | |
445 | } | |
446 | ||
447 | if (t) | |
448 | *t = ts; | |
449 | ||
450 | return idle_hint; | |
451 | } | |
452 | ||
20263082 LP |
453 | int user_check_gc(User *u) { |
454 | int r; | |
455 | char *p; | |
456 | ||
457 | assert(u); | |
458 | ||
459 | if (u->sessions) | |
460 | return 1; | |
461 | ||
462 | if (asprintf(&p, "/var/lib/systemd/linger/%s", u->name) < 0) | |
463 | return -ENOMEM; | |
464 | ||
465 | r = access(p, F_OK) >= 0; | |
466 | free(p); | |
467 | ||
468 | if (r > 0) | |
469 | return 1; | |
470 | ||
471 | if (u->cgroup_path) { | |
472 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false); | |
473 | if (r < 0) | |
474 | return r; | |
475 | ||
476 | if (r <= 0) | |
477 | return 1; | |
478 | } | |
479 | ||
480 | return 0; | |
481 | } | |
482 | ||
14c3baca LP |
483 | void user_add_to_gc_queue(User *u) { |
484 | assert(u); | |
485 | ||
486 | if (u->in_gc_queue) | |
487 | return; | |
488 | ||
489 | LIST_PREPEND(User, gc_queue, u->manager->user_gc_queue, u); | |
490 | u->in_gc_queue = true; | |
491 | } | |
492 | ||
20263082 LP |
493 | UserState user_get_state(User *u) { |
494 | Session *i; | |
495 | ||
496 | assert(u); | |
497 | ||
498 | if (!u->sessions) | |
499 | return USER_LINGERING; | |
500 | ||
501 | LIST_FOREACH(sessions_by_user, i, u->sessions) | |
502 | if (session_is_active(i)) | |
503 | return USER_ACTIVE; | |
504 | ||
505 | return USER_ONLINE; | |
506 | } | |
507 | ||
508 | static const char* const user_state_table[_USER_STATE_MAX] = { | |
509 | [USER_OFFLINE] = "offline", | |
510 | [USER_LINGERING] = "lingering", | |
511 | [USER_ONLINE] = "online", | |
512 | [USER_ACTIVE] = "active" | |
513 | }; | |
514 | ||
515 | DEFINE_STRING_TABLE_LOOKUP(user_state, UserState); |