]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/logind-user.c
logind: implement idle hint logic
[thirdparty/systemd.git] / src / 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/user/%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 r = safe_mkdir("/run/systemd/user", 0755, 0, 0);
98 if (r < 0)
99 goto finish;
100
101 r = fopen_temporary(u->state_file, &f, &temp_path);
102 if (r < 0)
103 goto finish;
104
105 fchmod(fileno(f), 0644);
106
107 fprintf(f,
108 "# This is private data. Do not parse.\n"
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);
135
136 if (ferror(f) || rename(temp_path, u->state_file) < 0) {
137 r = -errno;
138 unlink(u->state_file);
139 unlink(temp_path);
140 }
141
142 fclose(f);
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
149 return r;
150 }
151
152 int user_load(User *u) {
153 int r;
154 char *display = NULL;
155 Session *s;
156
157 assert(u);
158
159 r = parse_env_file(u->state_file, NEWLINE,
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
175 s = hashmap_get(u->manager->sessions, display);
176 free(display);
177
178 if (s && s->display && x11_display_is_local(s->display))
179 u->display = s;
180
181 return r;
182 }
183
184 static int user_mkdir_runtime_path(User *u) {
185 char *p;
186 int r;
187
188 assert(u);
189
190 r = safe_mkdir("/run/user", 0755, 0, 0);
191 if (r < 0) {
192 log_error("Failed to create /run/user: %s", strerror(-r));
193 return r;
194 }
195
196 if (!u->runtime_path) {
197 p = strappend("/run/user/", u->name);
198
199 if (!p) {
200 log_error("Out of memory");
201 return -ENOMEM;
202 }
203 } else
204 p = u->runtime_path;
205
206 r = safe_mkdir(p, 0700, u->uid, u->gid);
207 if (r < 0) {
208 log_error("Failed to create runtime directory %s: %s", p, strerror(-r));
209 free(p);
210 u->runtime_path = NULL;
211 return r;
212 }
213
214 u->runtime_path = p;
215 return 0;
216 }
217
218 static int user_create_cgroup(User *u) {
219 char **k;
220 char *p;
221 int r;
222
223 assert(u);
224
225 if (!u->cgroup_path) {
226 if (asprintf(&p, "%s/%s", u->manager->cgroup_path, u->name) < 0) {
227 log_error("Out of memory");
228 return -ENOMEM;
229 }
230 } else
231 p = u->cgroup_path;
232
233 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p);
234 if (r < 0) {
235 free(p);
236 u->cgroup_path = NULL;
237 log_error("Failed to create cgroup "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
238 return r;
239 }
240
241 u->cgroup_path = p;
242
243 STRV_FOREACH(k, u->manager->controllers) {
244 r = cg_create(*k, p);
245 if (r < 0)
246 log_warning("Failed to create cgroup %s:%s: %s", *k, p, strerror(-r));
247 }
248
249 return 0;
250 }
251
252 static int user_start_service(User *u) {
253 assert(u);
254
255 return 0;
256 }
257
258 int user_start(User *u) {
259 int r;
260
261 assert(u);
262
263 /* Make XDG_RUNTIME_DIR */
264 r = user_mkdir_runtime_path(u);
265 if (r < 0)
266 return r;
267
268 /* Create cgroup */
269 r = user_create_cgroup(u);
270 if (r < 0)
271 return r;
272
273 /* Spawn user systemd */
274 r = user_start_service(u);
275 if (r < 0)
276 return r;
277
278 /* Save new user data */
279 user_save(u);
280
281 dual_timestamp_get(&u->timestamp);
282
283 return 0;
284 }
285
286 static int user_stop_service(User *u) {
287 assert(u);
288
289 if (!u->service)
290 return 0;
291
292 return 0;
293 }
294
295 static int user_shall_kill(User *u) {
296 assert(u);
297
298 return u->manager->kill_user_processes;
299 }
300
301 static int user_kill_cgroup(User *u) {
302 int r;
303 char **k;
304
305 assert(u);
306
307 if (!u->cgroup_path)
308 return 0;
309
310 cg_trim(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false);
311
312 if (user_shall_kill(u)) {
313
314 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
315 if (r < 0)
316 log_error("Failed to kill user cgroup: %s", strerror(-r));
317 } else {
318
319 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
320 if (r < 0)
321 log_error("Failed to check user cgroup: %s", strerror(-r));
322 else if (r > 0) {
323 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path);
324 if (r < 0)
325 log_error("Failed to delete user cgroup: %s", strerror(-r));
326 } else
327 r = -EBUSY;
328 }
329
330 STRV_FOREACH(k, u->manager->controllers)
331 cg_trim(*k, u->cgroup_path, true);
332
333 free(u->cgroup_path);
334 u->cgroup_path = NULL;
335
336 return r;
337 }
338
339 static int user_remove_runtime_path(User *u) {
340 int r;
341
342 assert(u);
343
344 if (!u->runtime_path)
345 return 0;
346
347 r = rm_rf(u->runtime_path, false, true);
348 if (r < 0)
349 log_error("Failed to remove runtime directory %s: %s", u->runtime_path, strerror(-r));
350
351 free(u->runtime_path);
352 u->runtime_path = NULL;
353
354 return r;
355 }
356
357 int user_stop(User *u) {
358 Session *s;
359 int r = 0, k;
360 assert(u);
361
362 LIST_FOREACH(sessions_by_user, s, u->sessions) {
363 k = session_stop(s);
364 if (k < 0)
365 r = k;
366 }
367
368 /* Kill systemd */
369 k = user_stop_service(u);
370 if (k < 0)
371 r = k;
372
373 /* Kill cgroup */
374 k = user_kill_cgroup(u);
375 if (k < 0)
376 r = k;
377
378 /* Kill XDG_RUNTIME_DIR */
379 k = user_remove_runtime_path(u);
380 if (k < 0)
381 r = k;
382
383 unlink(u->state_file);
384 user_add_to_gc_queue(u);
385
386 return r;
387 }
388
389 int user_get_idle_hint(User *u, dual_timestamp *t) {
390 Session *s;
391 bool idle_hint = true;
392 dual_timestamp ts = { 0, 0 };
393
394 assert(u);
395
396 LIST_FOREACH(sessions_by_user, s, u->sessions) {
397 dual_timestamp k;
398 int ih;
399
400 ih = session_get_idle_hint(s, &k);
401 if (ih < 0)
402 return ih;
403
404 if (!ih) {
405 if (!idle_hint) {
406 if (k.monotonic < ts.monotonic)
407 ts = k;
408 } else {
409 idle_hint = false;
410 ts = k;
411 }
412 } else if (idle_hint) {
413
414 if (k.monotonic > ts.monotonic)
415 ts = k;
416 }
417 }
418
419 if (t)
420 *t = ts;
421
422 return idle_hint;
423 }
424
425 int user_check_gc(User *u) {
426 int r;
427 char *p;
428
429 assert(u);
430
431 if (u->sessions)
432 return 1;
433
434 if (asprintf(&p, "/var/lib/systemd/linger/%s", u->name) < 0)
435 return -ENOMEM;
436
437 r = access(p, F_OK) >= 0;
438 free(p);
439
440 if (r > 0)
441 return 1;
442
443 if (u->cgroup_path) {
444 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false);
445 if (r < 0)
446 return r;
447
448 if (r <= 0)
449 return 1;
450 }
451
452 return 0;
453 }
454
455 void user_add_to_gc_queue(User *u) {
456 assert(u);
457
458 if (u->in_gc_queue)
459 return;
460
461 LIST_PREPEND(User, gc_queue, u->manager->user_gc_queue, u);
462 u->in_gc_queue = true;
463 }
464
465 UserState user_get_state(User *u) {
466 Session *i;
467
468 assert(u);
469
470 if (!u->sessions)
471 return USER_LINGERING;
472
473 LIST_FOREACH(sessions_by_user, i, u->sessions)
474 if (session_is_active(i))
475 return USER_ACTIVE;
476
477 return USER_ONLINE;
478 }
479
480 static const char* const user_state_table[_USER_STATE_MAX] = {
481 [USER_OFFLINE] = "offline",
482 [USER_LINGERING] = "lingering",
483 [USER_ONLINE] = "online",
484 [USER_ACTIVE] = "active"
485 };
486
487 DEFINE_STRING_TABLE_LOOKUP(user_state, UserState);