chiark / gitweb /
switch from udev keymaps to hwdb
[elogind.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 Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 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   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser 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 "util.h"
27 #include "mkdir.h"
28 #include "cgroup-util.h"
29 #include "hashmap.h"
30 #include "strv.h"
31 #include "fileio.h"
32 #include "special.h"
33 #include "unit-name.h"
34 #include "dbus-common.h"
35 #include "logind-user.h"
36
37 User* user_new(Manager *m, uid_t uid, gid_t gid, const char *name) {
38         User *u;
39
40         assert(m);
41         assert(name);
42
43         u = new0(User, 1);
44         if (!u)
45                 return NULL;
46
47         u->name = strdup(name);
48         if (!u->name)
49                 goto fail;
50
51         if (asprintf(&u->state_file, "/run/systemd/users/%lu", (unsigned long) uid) < 0)
52                 goto fail;
53
54         if (hashmap_put(m->users, ULONG_TO_PTR((unsigned long) uid), u) < 0)
55                 goto fail;
56
57         u->manager = m;
58         u->uid = uid;
59         u->gid = gid;
60
61         return u;
62
63 fail:
64         free(u->state_file);
65         free(u->name);
66         free(u);
67
68         return NULL;
69 }
70
71 void user_free(User *u) {
72         assert(u);
73
74         if (u->in_gc_queue)
75                 LIST_REMOVE(User, gc_queue, u->manager->user_gc_queue, u);
76
77         while (u->sessions)
78                 session_free(u->sessions);
79
80         if (u->slice) {
81                 hashmap_remove(u->manager->user_units, u->slice);
82                 free(u->slice);
83         }
84
85         if (u->service) {
86                 hashmap_remove(u->manager->user_units, u->service);
87                 free(u->service);
88         }
89
90         free(u->slice_job);
91         free(u->service_job);
92
93         free(u->runtime_path);
94
95         hashmap_remove(u->manager->users, ULONG_TO_PTR((unsigned long) u->uid));
96
97         free(u->name);
98         free(u->state_file);
99         free(u);
100 }
101
102 int user_save(User *u) {
103         _cleanup_free_ char *temp_path = NULL;
104         _cleanup_fclose_ FILE *f = NULL;
105         int r;
106
107         assert(u);
108         assert(u->state_file);
109
110         if (!u->started)
111                 return 0;
112
113         r = mkdir_safe_label("/run/systemd/users", 0755, 0, 0);
114         if (r < 0)
115                 goto finish;
116
117         r = fopen_temporary(u->state_file, &f, &temp_path);
118         if (r < 0)
119                 goto finish;
120
121         fchmod(fileno(f), 0644);
122
123         fprintf(f,
124                 "# This is private data. Do not parse.\n"
125                 "NAME=%s\n"
126                 "STATE=%s\n",
127                 u->name,
128                 user_state_to_string(user_get_state(u)));
129
130         if (u->runtime_path)
131                 fprintf(f, "RUNTIME=%s\n", u->runtime_path);
132
133         if (u->service)
134                 fprintf(f, "SERVICE=%s\n", u->service);
135         if (u->service_job)
136                 fprintf(f, "SERVICE_JOB=%s\n", u->service_job);
137
138         if (u->slice)
139                 fprintf(f, "SLICE=%s\n", u->slice);
140         if (u->slice_job)
141                 fprintf(f, "SLICE_JOB=%s\n", u->slice_job);
142
143         if (u->display)
144                 fprintf(f, "DISPLAY=%s\n", u->display->id);
145
146         if (dual_timestamp_is_set(&u->timestamp))
147                 fprintf(f,
148                         "REALTIME=%llu\n"
149                         "MONOTONIC=%llu\n",
150                         (unsigned long long) u->timestamp.realtime,
151                         (unsigned long long) u->timestamp.monotonic);
152
153         if (u->sessions) {
154                 Session *i;
155                 bool first;
156
157                 fputs("SESSIONS=", f);
158                 first = true;
159                 LIST_FOREACH(sessions_by_user, i, u->sessions) {
160                         if (first)
161                                 first = false;
162                         else
163                                 fputc(' ', f);
164
165                         fputs(i->id, f);
166                 }
167
168                 fputs("\nSEATS=", f);
169                 first = true;
170                 LIST_FOREACH(sessions_by_user, i, u->sessions) {
171                         if (!i->seat)
172                                 continue;
173
174                         if (first)
175                                 first = false;
176                         else
177                                 fputc(' ', f);
178
179                         fputs(i->seat->id, f);
180                 }
181
182                 fputs("\nACTIVE_SESSIONS=", f);
183                 first = true;
184                 LIST_FOREACH(sessions_by_user, i, u->sessions) {
185                         if (!session_is_active(i))
186                                 continue;
187
188                         if (first)
189                                 first = false;
190                         else
191                                 fputc(' ', f);
192
193                         fputs(i->id, f);
194                 }
195
196                 fputs("\nONLINE_SESSIONS=", f);
197                 first = true;
198                 LIST_FOREACH(sessions_by_user, i, u->sessions) {
199                         if (session_get_state(i) == SESSION_CLOSING)
200                                 continue;
201
202                         if (first)
203                                 first = false;
204                         else
205                                 fputc(' ', f);
206
207                         fputs(i->id, f);
208                 }
209
210                 fputs("\nACTIVE_SEATS=", f);
211                 first = true;
212                 LIST_FOREACH(sessions_by_user, i, u->sessions) {
213                         if (!session_is_active(i) || !i->seat)
214                                 continue;
215
216                         if (first)
217                                 first = false;
218                         else
219                                 fputc(' ', f);
220
221                         fputs(i->seat->id, f);
222                 }
223
224                 fputs("\nONLINE_SEATS=", f);
225                 first = true;
226                 LIST_FOREACH(sessions_by_user, i, u->sessions) {
227                         if (session_get_state(i) == SESSION_CLOSING || !i->seat)
228                                 continue;
229
230                         if (first)
231                                 first = false;
232                         else
233                                 fputc(' ', f);
234
235                         fputs(i->seat->id, f);
236                 }
237                 fputc('\n', f);
238         }
239
240         fflush(f);
241
242         if (ferror(f) || rename(temp_path, u->state_file) < 0) {
243                 r = -errno;
244                 unlink(u->state_file);
245                 unlink(temp_path);
246         }
247
248 finish:
249         if (r < 0)
250                 log_error("Failed to save user data for %s: %s", u->name, strerror(-r));
251
252         return r;
253 }
254
255 int user_load(User *u) {
256         _cleanup_free_ char *display = NULL, *realtime = NULL, *monotonic = NULL;
257         Session *s = NULL;
258         int r;
259
260         assert(u);
261
262         r = parse_env_file(u->state_file, NEWLINE,
263                            "RUNTIME",     &u->runtime_path,
264                            "SERVICE",     &u->service,
265                            "SERVICE_JOB", &u->service_job,
266                            "SLICE",       &u->slice,
267                            "SLICE_JOB",   &u->slice_job,
268                            "DISPLAY",     &display,
269                            "REALTIME",    &realtime,
270                            "MONOTONIC",   &monotonic,
271                            NULL);
272         if (r < 0) {
273                 if (r == -ENOENT)
274                         return 0;
275
276                 log_error("Failed to read %s: %s", u->state_file, strerror(-r));
277                 return r;
278         }
279
280         if (display)
281                 s = hashmap_get(u->manager->sessions, display);
282
283         if (s && s->display && display_is_local(s->display))
284                 u->display = s;
285
286         if (realtime) {
287                 unsigned long long l;
288                 if (sscanf(realtime, "%llu", &l) > 0)
289                         u->timestamp.realtime = l;
290         }
291
292         if (monotonic) {
293                 unsigned long long l;
294                 if (sscanf(monotonic, "%llu", &l) > 0)
295                         u->timestamp.monotonic = l;
296         }
297
298         return r;
299 }
300
301 static int user_mkdir_runtime_path(User *u) {
302         char *p;
303         int r;
304
305         assert(u);
306
307         r = mkdir_safe_label("/run/user", 0755, 0, 0);
308         if (r < 0) {
309                 log_error("Failed to create /run/user: %s", strerror(-r));
310                 return r;
311         }
312
313         if (!u->runtime_path) {
314                 if (asprintf(&p, "/run/user/%lu", (unsigned long) u->uid) < 0)
315                         return log_oom();
316         } else
317                 p = u->runtime_path;
318
319         r = mkdir_safe_label(p, 0700, u->uid, u->gid);
320         if (r < 0) {
321                 log_error("Failed to create runtime directory %s: %s", p, strerror(-r));
322                 free(p);
323                 u->runtime_path = NULL;
324                 return r;
325         }
326
327         u->runtime_path = p;
328         return 0;
329 }
330
331 static int user_start_slice(User *u) {
332         DBusError error;
333         char *job;
334         int r;
335
336         assert(u);
337
338         dbus_error_init(&error);
339
340         if (!u->slice) {
341                 char lu[DECIMAL_STR_MAX(unsigned long) + 1], *slice;
342                 sprintf(lu, "%lu", (unsigned long) u->uid);
343
344                 r = build_subslice(SPECIAL_USER_SLICE, lu, &slice);
345                 if (r < 0)
346                         return r;
347
348                 r = manager_start_unit(u->manager, slice, &error, &job);
349                 if (r < 0) {
350                         log_error("Failed to start user slice: %s", bus_error(&error, r));
351                         dbus_error_free(&error);
352
353                         free(slice);
354                 } else {
355                         u->slice = slice;
356
357                         free(u->slice_job);
358                         u->slice_job = job;
359                 }
360         }
361
362         if (u->slice)
363                 hashmap_put(u->manager->user_units, u->slice, u);
364
365         return 0;
366 }
367
368 static int user_start_service(User *u) {
369         DBusError error;
370         char *job;
371         int r;
372
373         assert(u);
374
375         dbus_error_init(&error);
376
377         if (!u->service) {
378                 char lu[DECIMAL_STR_MAX(unsigned long) + 1], *service;
379                 sprintf(lu, "%lu", (unsigned long) u->uid);
380
381                 service = unit_name_build("user", lu, ".service");
382                 if (!service)
383                         return log_oom();
384
385                 r = manager_start_unit(u->manager, service, &error, &job);
386                 if (r < 0) {
387                         log_error("Failed to start user service: %s", bus_error(&error, r));
388                         dbus_error_free(&error);
389
390                         free(service);
391                 } else {
392                         u->service = service;
393
394                         free(u->service_job);
395                         u->service_job = job;
396                 }
397         }
398
399         if (u->service)
400                 hashmap_put(u->manager->user_units, u->service, u);
401
402         return 0;
403 }
404
405 int user_start(User *u) {
406         int r;
407
408         assert(u);
409
410         if (u->started)
411                 return 0;
412
413         log_debug("New user %s logged in.", u->name);
414
415         /* Make XDG_RUNTIME_DIR */
416         r = user_mkdir_runtime_path(u);
417         if (r < 0)
418                 return r;
419
420         /* Create cgroup */
421         r = user_start_slice(u);
422         if (r < 0)
423                 return r;
424
425         /* Spawn user systemd */
426         r = user_start_service(u);
427         if (r < 0)
428                 return r;
429
430         if (!dual_timestamp_is_set(&u->timestamp))
431                 dual_timestamp_get(&u->timestamp);
432
433         u->started = true;
434
435         /* Save new user data */
436         user_save(u);
437
438         user_send_signal(u, true);
439
440         return 0;
441 }
442
443 static int user_stop_slice(User *u) {
444         DBusError error;
445         char *job;
446         int r;
447
448         assert(u);
449
450         dbus_error_init(&error);
451
452         if (!u->slice)
453                 return 0;
454
455         r = manager_stop_unit(u->manager, u->slice, &error, &job);
456         if (r < 0) {
457                 log_error("Failed to stop user slice: %s", bus_error(&error, r));
458                 dbus_error_free(&error);
459                 return r;
460         }
461
462         free(u->slice_job);
463         u->slice_job = job;
464
465         return r;
466 }
467
468 static int user_stop_service(User *u) {
469         DBusError error;
470         char *job;
471         int r;
472
473         assert(u);
474
475         dbus_error_init(&error);
476
477         if (!u->service)
478                 return 0;
479
480         r = manager_stop_unit(u->manager, u->service, &error, &job);
481         if (r < 0) {
482                 log_error("Failed to stop user service: %s", bus_error(&error, r));
483                 dbus_error_free(&error);
484                 return r;
485         }
486
487         free(u->service_job);
488         u->service_job = job;
489
490         return r;
491 }
492
493 /* static int user_shall_kill(User *u) { */
494 /*         assert(u); */
495
496 /*         if (!u->manager->kill_user_processes) */
497 /*                 return false; */
498
499 /*         if (strv_contains(u->manager->kill_exclude_users, u->name)) */
500 /*                 return false; */
501
502 /*         if (strv_isempty(u->manager->kill_only_users)) */
503 /*                 return true; */
504
505 /*         return strv_contains(u->manager->kill_only_users, u->name); */
506 /* } */
507
508 static int user_remove_runtime_path(User *u) {
509         int r;
510
511         assert(u);
512
513         if (!u->runtime_path)
514                 return 0;
515
516         r = rm_rf(u->runtime_path, false, true, false);
517         if (r < 0)
518                 log_error("Failed to remove runtime directory %s: %s", u->runtime_path, strerror(-r));
519
520         free(u->runtime_path);
521         u->runtime_path = NULL;
522
523         return r;
524 }
525
526 int user_stop(User *u) {
527         Session *s;
528         int r = 0, k;
529         assert(u);
530
531         if (u->started)
532                 log_debug("User %s logged out.", u->name);
533
534         LIST_FOREACH(sessions_by_user, s, u->sessions) {
535                 k = session_stop(s);
536                 if (k < 0)
537                         r = k;
538         }
539
540         /* Kill systemd */
541         k = user_stop_service(u);
542         if (k < 0)
543                 r = k;
544
545         /* Kill cgroup */
546         k = user_stop_slice(u);
547         if (k < 0)
548                 r = k;
549
550         /* Kill XDG_RUNTIME_DIR */
551         k = user_remove_runtime_path(u);
552         if (k < 0)
553                 r = k;
554
555         unlink(u->state_file);
556         user_add_to_gc_queue(u);
557
558         if (u->started)
559                 user_send_signal(u, false);
560
561         u->started = false;
562
563         return r;
564 }
565
566 int user_get_idle_hint(User *u, dual_timestamp *t) {
567         Session *s;
568         bool idle_hint = true;
569         dual_timestamp ts = { 0, 0 };
570
571         assert(u);
572
573         LIST_FOREACH(sessions_by_user, s, u->sessions) {
574                 dual_timestamp k;
575                 int ih;
576
577                 ih = session_get_idle_hint(s, &k);
578                 if (ih < 0)
579                         return ih;
580
581                 if (!ih) {
582                         if (!idle_hint) {
583                                 if (k.monotonic < ts.monotonic)
584                                         ts = k;
585                         } else {
586                                 idle_hint = false;
587                                 ts = k;
588                         }
589                 } else if (idle_hint) {
590
591                         if (k.monotonic > ts.monotonic)
592                                 ts = k;
593                 }
594         }
595
596         if (t)
597                 *t = ts;
598
599         return idle_hint;
600 }
601
602 static int user_check_linger_file(User *u) {
603         char *p;
604         int r;
605
606         if (asprintf(&p, "/var/lib/systemd/linger/%s", u->name) < 0)
607                 return -ENOMEM;
608
609         r = access(p, F_OK) >= 0;
610         free(p);
611
612         return r;
613 }
614
615 int user_check_gc(User *u, bool drop_not_started) {
616         assert(u);
617
618         if (drop_not_started && !u->started)
619                 return 0;
620
621         if (u->sessions)
622                 return 1;
623
624         if (user_check_linger_file(u) > 0)
625                 return 1;
626
627         return 0;
628 }
629
630 void user_add_to_gc_queue(User *u) {
631         assert(u);
632
633         if (u->in_gc_queue)
634                 return;
635
636         LIST_PREPEND(User, gc_queue, u->manager->user_gc_queue, u);
637         u->in_gc_queue = true;
638 }
639
640 UserState user_get_state(User *u) {
641         Session *i;
642         bool all_closing = true;
643
644         assert(u);
645
646         if (u->slice_job || u->service_job)
647                 return u->started ? USER_OPENING : USER_CLOSING;
648
649         LIST_FOREACH(sessions_by_user, i, u->sessions) {
650                 if (session_is_active(i))
651                         return USER_ACTIVE;
652                 if (session_get_state(i) != SESSION_CLOSING)
653                         all_closing = false;
654         }
655
656         if (u->sessions)
657                 return all_closing ? USER_CLOSING : USER_ONLINE;
658
659         if (user_check_linger_file(u) > 0)
660                 return USER_LINGERING;
661
662         return USER_CLOSING;
663 }
664
665 int user_kill(User *u, int signo) {
666         assert(u);
667
668         if (!u->slice)
669                 return -ESRCH;
670
671         return manager_kill_unit(u->manager, u->slice, KILL_ALL, signo, NULL);
672 }
673
674 static const char* const user_state_table[_USER_STATE_MAX] = {
675         [USER_OFFLINE] = "offline",
676         [USER_OPENING] = "opening",
677         [USER_LINGERING] = "lingering",
678         [USER_ONLINE] = "online",
679         [USER_ACTIVE] = "active",
680         [USER_CLOSING] = "closing"
681 };
682
683 DEFINE_STRING_TABLE_LOOKUP(user_state, UserState);