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