chiark / gitweb /
logind: send dbus signals when sessions/users/seats come and go
[elogind.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         user_send_signal(u, true);
284
285         return 0;
286 }
287
288 static int user_stop_service(User *u) {
289         assert(u);
290
291         if (!u->service)
292                 return 0;
293
294         return 0;
295 }
296
297 static int user_shall_kill(User *u) {
298         assert(u);
299
300         return u->manager->kill_user_processes;
301 }
302
303 static int user_kill_cgroup(User *u) {
304         int r;
305         char **k;
306
307         assert(u);
308
309         if (!u->cgroup_path)
310                 return 0;
311
312         cg_trim(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false);
313
314         if (user_shall_kill(u)) {
315
316                 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
317                 if (r < 0)
318                         log_error("Failed to kill user cgroup: %s", strerror(-r));
319         } else {
320
321                 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
322                 if (r < 0)
323                         log_error("Failed to check user cgroup: %s", strerror(-r));
324                 else if (r > 0) {
325                         r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path);
326                         if (r < 0)
327                                 log_error("Failed to delete user cgroup: %s", strerror(-r));
328                 } else
329                         r = -EBUSY;
330         }
331
332         STRV_FOREACH(k, u->manager->controllers)
333                 cg_trim(*k, u->cgroup_path, true);
334
335         free(u->cgroup_path);
336         u->cgroup_path = NULL;
337
338         return r;
339 }
340
341 static int user_remove_runtime_path(User *u) {
342         int r;
343
344         assert(u);
345
346         if (!u->runtime_path)
347                 return 0;
348
349         r = rm_rf(u->runtime_path, false, true);
350         if (r < 0)
351                 log_error("Failed to remove runtime directory %s: %s", u->runtime_path, strerror(-r));
352
353         free(u->runtime_path);
354         u->runtime_path = NULL;
355
356         return r;
357 }
358
359 int user_stop(User *u) {
360         Session *s;
361         int r = 0, k;
362         assert(u);
363
364         LIST_FOREACH(sessions_by_user, s, u->sessions) {
365                 k = session_stop(s);
366                 if (k < 0)
367                         r = k;
368         }
369
370         user_send_signal(u, false);
371
372         /* Kill systemd */
373         k = user_stop_service(u);
374         if (k < 0)
375                 r = k;
376
377         /* Kill cgroup */
378         k = user_kill_cgroup(u);
379         if (k < 0)
380                 r = k;
381
382         /* Kill XDG_RUNTIME_DIR */
383         k = user_remove_runtime_path(u);
384         if (k < 0)
385                 r = k;
386
387         unlink(u->state_file);
388         user_add_to_gc_queue(u);
389
390         return r;
391 }
392
393 int user_get_idle_hint(User *u, dual_timestamp *t) {
394         Session *s;
395         bool idle_hint = true;
396         dual_timestamp ts = { 0, 0 };
397
398         assert(u);
399
400         LIST_FOREACH(sessions_by_user, s, u->sessions) {
401                 dual_timestamp k;
402                 int ih;
403
404                 ih = session_get_idle_hint(s, &k);
405                 if (ih < 0)
406                         return ih;
407
408                 if (!ih) {
409                         if (!idle_hint) {
410                                 if (k.monotonic < ts.monotonic)
411                                         ts = k;
412                         } else {
413                                 idle_hint = false;
414                                 ts = k;
415                         }
416                 } else if (idle_hint) {
417
418                         if (k.monotonic > ts.monotonic)
419                                 ts = k;
420                 }
421         }
422
423         if (t)
424                 *t = ts;
425
426         return idle_hint;
427 }
428
429 int user_check_gc(User *u) {
430         int r;
431         char *p;
432
433         assert(u);
434
435         if (u->sessions)
436                 return 1;
437
438         if (asprintf(&p, "/var/lib/systemd/linger/%s", u->name) < 0)
439                 return -ENOMEM;
440
441         r = access(p, F_OK) >= 0;
442         free(p);
443
444         if (r > 0)
445                 return 1;
446
447         if (u->cgroup_path) {
448                 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false);
449                 if (r < 0)
450                         return r;
451
452                 if (r <= 0)
453                         return 1;
454         }
455
456         return 0;
457 }
458
459 void user_add_to_gc_queue(User *u) {
460         assert(u);
461
462         if (u->in_gc_queue)
463                 return;
464
465         LIST_PREPEND(User, gc_queue, u->manager->user_gc_queue, u);
466         u->in_gc_queue = true;
467 }
468
469 UserState user_get_state(User *u) {
470         Session *i;
471
472         assert(u);
473
474         if (!u->sessions)
475                 return USER_LINGERING;
476
477         LIST_FOREACH(sessions_by_user, i, u->sessions)
478                 if (session_is_active(i))
479                         return USER_ACTIVE;
480
481         return USER_ONLINE;
482 }
483
484 static const char* const user_state_table[_USER_STATE_MAX] = {
485         [USER_OFFLINE] = "offline",
486         [USER_LINGERING] = "lingering",
487         [USER_ONLINE] = "online",
488         [USER_ACTIVE] = "active"
489 };
490
491 DEFINE_STRING_TABLE_LOOKUP(user_state, UserState);