chiark / gitweb /
nspawn: spawn shell under specified --user
[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         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         fflush(f);
138
139         if (ferror(f) || rename(temp_path, u->state_file) < 0) {
140                 r = -errno;
141                 unlink(u->state_file);
142                 unlink(temp_path);
143         }
144
145         fclose(f);
146         free(temp_path);
147
148 finish:
149         if (r < 0)
150                 log_error("Failed to save user data for %s: %s", u->name, strerror(-r));
151
152         return r;
153 }
154
155 int user_load(User *u) {
156         int r;
157         char *display = NULL;
158         Session *s = NULL;
159
160         assert(u);
161
162         r = parse_env_file(u->state_file, NEWLINE,
163                            "CGROUP", &u->cgroup_path,
164                            "RUNTIME", &u->runtime_path,
165                            "SERVICE", &u->service,
166                            "DISPLAY", &display,
167                            NULL);
168         if (r < 0) {
169                 free(display);
170
171                 if (r == -ENOENT)
172                         return 0;
173
174                 log_error("Failed to read %s: %s", u->state_file, strerror(-r));
175                 return r;
176         }
177
178         if (display) {
179                 s = hashmap_get(u->manager->sessions, display);
180                 free(display);
181         }
182
183         if (s && s->display && display_is_local(s->display))
184                 u->display = s;
185
186         return r;
187 }
188
189 static int user_mkdir_runtime_path(User *u) {
190         char *p;
191         int r;
192
193         assert(u);
194
195         r = safe_mkdir("/run/user", 0755, 0, 0);
196         if (r < 0) {
197                 log_error("Failed to create /run/user: %s", strerror(-r));
198                 return r;
199         }
200
201         if (!u->runtime_path) {
202                 p = strappend("/run/user/", u->name);
203
204                 if (!p) {
205                         log_error("Out of memory");
206                         return -ENOMEM;
207                 }
208         } else
209                 p = u->runtime_path;
210
211         r = safe_mkdir(p, 0700, u->uid, u->gid);
212         if (r < 0) {
213                 log_error("Failed to create runtime directory %s: %s", p, strerror(-r));
214                 free(p);
215                 u->runtime_path = NULL;
216                 return r;
217         }
218
219         u->runtime_path = p;
220         return 0;
221 }
222
223 static int user_create_cgroup(User *u) {
224         char **k;
225         char *p;
226         int r;
227
228         assert(u);
229
230         if (!u->cgroup_path) {
231                 if (asprintf(&p, "%s/%s", u->manager->cgroup_path, u->name) < 0) {
232                         log_error("Out of memory");
233                         return -ENOMEM;
234                 }
235         } else
236                 p = u->cgroup_path;
237
238         r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p);
239         if (r < 0) {
240                 log_error("Failed to create cgroup "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
241                 free(p);
242                 u->cgroup_path = NULL;
243                 return r;
244         }
245
246         u->cgroup_path = p;
247
248         STRV_FOREACH(k, u->manager->controllers) {
249
250                 if (strv_contains(u->manager->reset_controllers, *k))
251                         continue;
252
253                 r = cg_create(*k, p);
254                 if (r < 0)
255                         log_warning("Failed to create cgroup %s:%s: %s", *k, p, strerror(-r));
256         }
257
258         return 0;
259 }
260
261 static int user_start_service(User *u) {
262         assert(u);
263
264         return 0;
265 }
266
267 int user_start(User *u) {
268         int r;
269
270         assert(u);
271
272         if (u->started)
273                 return 0;
274
275         log_info("New user %s logged in.", u->name);
276
277         /* Make XDG_RUNTIME_DIR */
278         r = user_mkdir_runtime_path(u);
279         if (r < 0)
280                 return r;
281
282         /* Create cgroup */
283         r = user_create_cgroup(u);
284         if (r < 0)
285                 return r;
286
287         /* Spawn user systemd */
288         r = user_start_service(u);
289         if (r < 0)
290                 return r;
291
292         dual_timestamp_get(&u->timestamp);
293
294         u->started = true;
295
296         /* Save new user data */
297         user_save(u);
298
299         user_send_signal(u, true);
300
301         return 0;
302 }
303
304 static int user_stop_service(User *u) {
305         assert(u);
306
307         if (!u->service)
308                 return 0;
309
310         return 0;
311 }
312
313 static int user_shall_kill(User *u) {
314         assert(u);
315
316         if (!u->manager->kill_user_processes)
317                 return false;
318
319         if (strv_contains(u->manager->kill_exclude_users, u->name))
320                 return false;
321
322         if (strv_isempty(u->manager->kill_only_users))
323                 return true;
324
325         return strv_contains(u->manager->kill_only_users, u->name);
326 }
327
328 static int user_kill_cgroup(User *u) {
329         int r;
330         char **k;
331
332         assert(u);
333
334         if (!u->cgroup_path)
335                 return 0;
336
337         cg_trim(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false);
338
339         if (user_shall_kill(u)) {
340
341                 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
342                 if (r < 0)
343                         log_error("Failed to kill user cgroup: %s", strerror(-r));
344         } else {
345
346                 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
347                 if (r < 0)
348                         log_error("Failed to check user cgroup: %s", strerror(-r));
349                 else if (r > 0) {
350                         r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path);
351                         if (r < 0)
352                                 log_error("Failed to delete user cgroup: %s", strerror(-r));
353                 } else
354                         r = -EBUSY;
355         }
356
357         STRV_FOREACH(k, u->manager->controllers)
358                 cg_trim(*k, u->cgroup_path, true);
359
360         free(u->cgroup_path);
361         u->cgroup_path = NULL;
362
363         return r;
364 }
365
366 static int user_remove_runtime_path(User *u) {
367         int r;
368
369         assert(u);
370
371         if (!u->runtime_path)
372                 return 0;
373
374         r = rm_rf(u->runtime_path, false, true);
375         if (r < 0)
376                 log_error("Failed to remove runtime directory %s: %s", u->runtime_path, strerror(-r));
377
378         free(u->runtime_path);
379         u->runtime_path = NULL;
380
381         return r;
382 }
383
384 int user_stop(User *u) {
385         Session *s;
386         int r = 0, k;
387         assert(u);
388
389         if (u->started)
390                 log_info("User %s logged out.", u->name);
391
392         LIST_FOREACH(sessions_by_user, s, u->sessions) {
393                 k = session_stop(s);
394                 if (k < 0)
395                         r = k;
396         }
397
398         /* Kill systemd */
399         k = user_stop_service(u);
400         if (k < 0)
401                 r = k;
402
403         /* Kill cgroup */
404         k = user_kill_cgroup(u);
405         if (k < 0)
406                 r = k;
407
408         /* Kill XDG_RUNTIME_DIR */
409         k = user_remove_runtime_path(u);
410         if (k < 0)
411                 r = k;
412
413         unlink(u->state_file);
414         user_add_to_gc_queue(u);
415
416         if (u->started)
417                 user_send_signal(u, false);
418
419         u->started = false;
420
421         return r;
422 }
423
424 int user_get_idle_hint(User *u, dual_timestamp *t) {
425         Session *s;
426         bool idle_hint = true;
427         dual_timestamp ts = { 0, 0 };
428
429         assert(u);
430
431         LIST_FOREACH(sessions_by_user, s, u->sessions) {
432                 dual_timestamp k;
433                 int ih;
434
435                 ih = session_get_idle_hint(s, &k);
436                 if (ih < 0)
437                         return ih;
438
439                 if (!ih) {
440                         if (!idle_hint) {
441                                 if (k.monotonic < ts.monotonic)
442                                         ts = k;
443                         } else {
444                                 idle_hint = false;
445                                 ts = k;
446                         }
447                 } else if (idle_hint) {
448
449                         if (k.monotonic > ts.monotonic)
450                                 ts = k;
451                 }
452         }
453
454         if (t)
455                 *t = ts;
456
457         return idle_hint;
458 }
459
460 int user_check_gc(User *u, bool drop_not_started) {
461         int r;
462         char *p;
463
464         assert(u);
465
466         if (drop_not_started && !u->started)
467                 return 0;
468
469         if (u->sessions)
470                 return 1;
471
472         if (asprintf(&p, "/var/lib/systemd/linger/%s", u->name) < 0)
473                 return -ENOMEM;
474
475         r = access(p, F_OK) >= 0;
476         free(p);
477
478         if (r > 0)
479                 return 1;
480
481         if (u->cgroup_path) {
482                 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false);
483                 if (r < 0)
484                         return r;
485
486                 if (r <= 0)
487                         return 1;
488         }
489
490         return 0;
491 }
492
493 void user_add_to_gc_queue(User *u) {
494         assert(u);
495
496         if (u->in_gc_queue)
497                 return;
498
499         LIST_PREPEND(User, gc_queue, u->manager->user_gc_queue, u);
500         u->in_gc_queue = true;
501 }
502
503 UserState user_get_state(User *u) {
504         Session *i;
505
506         assert(u);
507
508         if (!u->sessions)
509                 return USER_LINGERING;
510
511         LIST_FOREACH(sessions_by_user, i, u->sessions)
512                 if (session_is_active(i))
513                         return USER_ACTIVE;
514
515         return USER_ONLINE;
516 }
517
518 static const char* const user_state_table[_USER_STATE_MAX] = {
519         [USER_OFFLINE] = "offline",
520         [USER_LINGERING] = "lingering",
521         [USER_ONLINE] = "online",
522         [USER_ACTIVE] = "active"
523 };
524
525 DEFINE_STRING_TABLE_LOOKUP(user_state, UserState);