]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-user.c
432d6092f54d4c5c72f81bc5c9cb049221f3ed34
[thirdparty/systemd.git] / src / login / logind-user.c
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
26 #include "logind-user.h"
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
38 u = new0(User, 1);
39 if (!u)
40 return NULL;
41
42 u->name = strdup(name);
43 if (!u->name) {
44 free(u);
45 return NULL;
46 }
47
48 if (asprintf(&u->state_file, "/run/systemd/users/%lu", (unsigned long) uid) < 0) {
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
71 if (u->in_gc_queue)
72 LIST_REMOVE(User, gc_queue, u->manager->user_gc_queue, u);
73
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);
85 free(u->state_file);
86 free(u);
87 }
88
89 int user_save(User *u) {
90 FILE *f;
91 int r;
92 char *temp_path;
93
94 assert(u);
95 assert(u->state_file);
96
97 if (!u->started)
98 return 0;
99
100 r = safe_mkdir("/run/systemd/users", 0755, 0, 0);
101 if (r < 0)
102 goto finish;
103
104 r = fopen_temporary(u->state_file, &f, &temp_path);
105 if (r < 0)
106 goto finish;
107
108 fchmod(fileno(f), 0644);
109
110 fprintf(f,
111 "# This is private data. Do not parse.\n"
112 "NAME=%s\n"
113 "STATE=%s\n",
114 u->name,
115 user_state_to_string(user_get_state(u)));
116
117 if (u->cgroup_path)
118 fprintf(f,
119 "CGROUP=%s\n",
120 u->cgroup_path);
121
122 if (u->runtime_path)
123 fprintf(f,
124 "RUNTIME=%s\n",
125 u->runtime_path);
126
127 if (u->service)
128 fprintf(f,
129 "SERVICE=%s\n",
130 u->service);
131
132 if (u->display)
133 fprintf(f,
134 "DISPLAY=%s\n",
135 u->display->id);
136
137 if (u->sessions) {
138 Session *i;
139
140 fputs("SESSIONS=", f);
141 LIST_FOREACH(sessions_by_user, i, u->sessions) {
142 fprintf(f,
143 "%s%c",
144 i->id,
145 i->sessions_by_user_next ? ' ' : '\n');
146 }
147
148 fputs("SEATS=", f);
149 LIST_FOREACH(sessions_by_user, i, u->sessions) {
150 if (i->seat)
151 fprintf(f,
152 "%s%c",
153 i->seat->id,
154 i->sessions_by_user_next ? ' ' : '\n');
155 }
156
157 fputs("ACTIVE_SESSIONS=", f);
158 LIST_FOREACH(sessions_by_user, i, u->sessions)
159 if (session_is_active(i))
160 fprintf(f,
161 "%lu%c",
162 (unsigned long) i->user->uid,
163 i->sessions_by_user_next ? ' ' : '\n');
164
165 fputs("ACTIVE_SEATS=", f);
166 LIST_FOREACH(sessions_by_user, i, u->sessions) {
167 if (session_is_active(i) && i->seat)
168 fprintf(f,
169 "%s%c",
170 i->seat->id,
171 i->sessions_by_user_next ? ' ' : '\n');
172 }
173 }
174
175 fflush(f);
176
177 if (ferror(f) || rename(temp_path, u->state_file) < 0) {
178 r = -errno;
179 unlink(u->state_file);
180 unlink(temp_path);
181 }
182
183 fclose(f);
184 free(temp_path);
185
186 finish:
187 if (r < 0)
188 log_error("Failed to save user data for %s: %s", u->name, strerror(-r));
189
190 return r;
191 }
192
193 int user_load(User *u) {
194 int r;
195 char *display = NULL;
196 Session *s = NULL;
197
198 assert(u);
199
200 r = parse_env_file(u->state_file, NEWLINE,
201 "CGROUP", &u->cgroup_path,
202 "RUNTIME", &u->runtime_path,
203 "SERVICE", &u->service,
204 "DISPLAY", &display,
205 NULL);
206 if (r < 0) {
207 free(display);
208
209 if (r == -ENOENT)
210 return 0;
211
212 log_error("Failed to read %s: %s", u->state_file, strerror(-r));
213 return r;
214 }
215
216 if (display) {
217 s = hashmap_get(u->manager->sessions, display);
218 free(display);
219 }
220
221 if (s && s->display && display_is_local(s->display))
222 u->display = s;
223
224 return r;
225 }
226
227 static int user_mkdir_runtime_path(User *u) {
228 char *p;
229 int r;
230
231 assert(u);
232
233 r = safe_mkdir("/run/user", 0755, 0, 0);
234 if (r < 0) {
235 log_error("Failed to create /run/user: %s", strerror(-r));
236 return r;
237 }
238
239 if (!u->runtime_path) {
240 p = strappend("/run/user/", u->name);
241
242 if (!p) {
243 log_error("Out of memory");
244 return -ENOMEM;
245 }
246 } else
247 p = u->runtime_path;
248
249 r = safe_mkdir(p, 0700, u->uid, u->gid);
250 if (r < 0) {
251 log_error("Failed to create runtime directory %s: %s", p, strerror(-r));
252 free(p);
253 u->runtime_path = NULL;
254 return r;
255 }
256
257 u->runtime_path = p;
258 return 0;
259 }
260
261 static int user_create_cgroup(User *u) {
262 char **k;
263 char *p;
264 int r;
265
266 assert(u);
267
268 if (!u->cgroup_path) {
269 if (asprintf(&p, "%s/%s", u->manager->cgroup_path, u->name) < 0) {
270 log_error("Out of memory");
271 return -ENOMEM;
272 }
273 } else
274 p = u->cgroup_path;
275
276 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p);
277 if (r < 0) {
278 log_error("Failed to create cgroup "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
279 free(p);
280 u->cgroup_path = NULL;
281 return r;
282 }
283
284 u->cgroup_path = p;
285
286 STRV_FOREACH(k, u->manager->controllers) {
287
288 if (strv_contains(u->manager->reset_controllers, *k))
289 continue;
290
291 r = cg_create(*k, p);
292 if (r < 0)
293 log_warning("Failed to create cgroup %s:%s: %s", *k, p, strerror(-r));
294 }
295
296 return 0;
297 }
298
299 static int user_start_service(User *u) {
300 assert(u);
301
302 return 0;
303 }
304
305 int user_start(User *u) {
306 int r;
307
308 assert(u);
309
310 if (u->started)
311 return 0;
312
313 log_debug("New user %s logged in.", u->name);
314
315 /* Make XDG_RUNTIME_DIR */
316 r = user_mkdir_runtime_path(u);
317 if (r < 0)
318 return r;
319
320 /* Create cgroup */
321 r = user_create_cgroup(u);
322 if (r < 0)
323 return r;
324
325 /* Spawn user systemd */
326 r = user_start_service(u);
327 if (r < 0)
328 return r;
329
330 dual_timestamp_get(&u->timestamp);
331
332 u->started = true;
333
334 /* Save new user data */
335 user_save(u);
336
337 user_send_signal(u, true);
338
339 return 0;
340 }
341
342 static int user_stop_service(User *u) {
343 assert(u);
344
345 if (!u->service)
346 return 0;
347
348 return 0;
349 }
350
351 static int user_shall_kill(User *u) {
352 assert(u);
353
354 if (!u->manager->kill_user_processes)
355 return false;
356
357 if (strv_contains(u->manager->kill_exclude_users, u->name))
358 return false;
359
360 if (strv_isempty(u->manager->kill_only_users))
361 return true;
362
363 return strv_contains(u->manager->kill_only_users, u->name);
364 }
365
366 static int user_terminate_cgroup(User *u) {
367 int r;
368 char **k;
369
370 assert(u);
371
372 if (!u->cgroup_path)
373 return 0;
374
375 cg_trim(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false);
376
377 if (user_shall_kill(u)) {
378
379 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
380 if (r < 0)
381 log_error("Failed to kill user cgroup: %s", strerror(-r));
382 } else {
383
384 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
385 if (r < 0)
386 log_error("Failed to check user cgroup: %s", strerror(-r));
387 else if (r > 0) {
388 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path);
389 if (r < 0)
390 log_error("Failed to delete user cgroup: %s", strerror(-r));
391 } else
392 r = -EBUSY;
393 }
394
395 STRV_FOREACH(k, u->manager->controllers)
396 cg_trim(*k, u->cgroup_path, true);
397
398 free(u->cgroup_path);
399 u->cgroup_path = NULL;
400
401 return r;
402 }
403
404 static int user_remove_runtime_path(User *u) {
405 int r;
406
407 assert(u);
408
409 if (!u->runtime_path)
410 return 0;
411
412 r = rm_rf(u->runtime_path, false, true, false);
413 if (r < 0)
414 log_error("Failed to remove runtime directory %s: %s", u->runtime_path, strerror(-r));
415
416 free(u->runtime_path);
417 u->runtime_path = NULL;
418
419 return r;
420 }
421
422 int user_stop(User *u) {
423 Session *s;
424 int r = 0, k;
425 assert(u);
426
427 if (u->started)
428 log_debug("User %s logged out.", u->name);
429
430 LIST_FOREACH(sessions_by_user, s, u->sessions) {
431 k = session_stop(s);
432 if (k < 0)
433 r = k;
434 }
435
436 /* Kill systemd */
437 k = user_stop_service(u);
438 if (k < 0)
439 r = k;
440
441 /* Kill cgroup */
442 k = user_terminate_cgroup(u);
443 if (k < 0)
444 r = k;
445
446 /* Kill XDG_RUNTIME_DIR */
447 k = user_remove_runtime_path(u);
448 if (k < 0)
449 r = k;
450
451 unlink(u->state_file);
452 user_add_to_gc_queue(u);
453
454 if (u->started)
455 user_send_signal(u, false);
456
457 u->started = false;
458
459 return r;
460 }
461
462 int user_get_idle_hint(User *u, dual_timestamp *t) {
463 Session *s;
464 bool idle_hint = true;
465 dual_timestamp ts = { 0, 0 };
466
467 assert(u);
468
469 LIST_FOREACH(sessions_by_user, s, u->sessions) {
470 dual_timestamp k;
471 int ih;
472
473 ih = session_get_idle_hint(s, &k);
474 if (ih < 0)
475 return ih;
476
477 if (!ih) {
478 if (!idle_hint) {
479 if (k.monotonic < ts.monotonic)
480 ts = k;
481 } else {
482 idle_hint = false;
483 ts = k;
484 }
485 } else if (idle_hint) {
486
487 if (k.monotonic > ts.monotonic)
488 ts = k;
489 }
490 }
491
492 if (t)
493 *t = ts;
494
495 return idle_hint;
496 }
497
498 int user_check_gc(User *u, bool drop_not_started) {
499 int r;
500 char *p;
501
502 assert(u);
503
504 if (drop_not_started && !u->started)
505 return 0;
506
507 if (u->sessions)
508 return 1;
509
510 if (asprintf(&p, "/var/lib/systemd/linger/%s", u->name) < 0)
511 return -ENOMEM;
512
513 r = access(p, F_OK) >= 0;
514 free(p);
515
516 if (r > 0)
517 return 1;
518
519 if (u->cgroup_path) {
520 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false);
521 if (r < 0)
522 return r;
523
524 if (r <= 0)
525 return 1;
526 }
527
528 return 0;
529 }
530
531 void user_add_to_gc_queue(User *u) {
532 assert(u);
533
534 if (u->in_gc_queue)
535 return;
536
537 LIST_PREPEND(User, gc_queue, u->manager->user_gc_queue, u);
538 u->in_gc_queue = true;
539 }
540
541 UserState user_get_state(User *u) {
542 Session *i;
543
544 assert(u);
545
546 if (!u->sessions)
547 return USER_LINGERING;
548
549 LIST_FOREACH(sessions_by_user, i, u->sessions)
550 if (session_is_active(i))
551 return USER_ACTIVE;
552
553 return USER_ONLINE;
554 }
555
556 int user_kill(User *u, int signo) {
557 int r = 0, q;
558 Set *pid_set = NULL;
559
560 assert(u);
561
562 if (!u->cgroup_path)
563 return -ESRCH;
564
565 pid_set = set_new(trivial_hash_func, trivial_compare_func);
566 if (!pid_set)
567 return -ENOMEM;
568
569 q = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, signo, false, true, false, pid_set);
570 if (q < 0)
571 if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
572 r = q;
573
574 if (pid_set)
575 set_free(pid_set);
576
577 return r;
578 }
579
580 static const char* const user_state_table[_USER_STATE_MAX] = {
581 [USER_OFFLINE] = "offline",
582 [USER_LINGERING] = "lingering",
583 [USER_ONLINE] = "online",
584 [USER_ACTIVE] = "active"
585 };
586
587 DEFINE_STRING_TABLE_LOOKUP(user_state, UserState);