chiark / gitweb /
pam: set XDG_SEAT and XDG_VTNR when logging in
[elogind.git] / src / sd-login.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 <unistd.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <sys/inotify.h>
26
27 #include "util.h"
28 #include "cgroup-util.h"
29 #include "macro.h"
30 #include "sd-login.h"
31 #include "strv.h"
32
33 static int pid_get_cgroup(pid_t pid, char **root, char **cgroup) {
34         char *cg_process, *cg_init, *p;
35         int r;
36
37         if (pid == 0)
38                 pid = getpid();
39
40         if (pid <= 0)
41                 return -EINVAL;
42
43         r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, pid, &cg_process);
44         if (r < 0)
45                 return r;
46
47         r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &cg_init);
48         if (r < 0) {
49                 free(cg_process);
50                 return r;
51         }
52
53         if (endswith(cg_init, "/system"))
54                 cg_init[strlen(cg_init)-7] = 0;
55         else if (streq(cg_init, "/"))
56                 cg_init[0] = 0;
57
58         if (startswith(cg_process, cg_init))
59                 p = cg_process + strlen(cg_init);
60         else
61                 p = cg_process;
62
63         free(cg_init);
64
65         if (cgroup) {
66                 char* c;
67
68                 c = strdup(p);
69                 if (!c) {
70                         free(cg_process);
71                         return -ENOMEM;
72                 }
73
74                 *cgroup = c;
75         }
76
77         if (root) {
78                 cg_process[p-cg_process] = 0;
79                 *root = cg_process;
80         } else
81                 free(cg_process);
82
83         return 0;
84 }
85
86 _public_ int sd_pid_get_session(pid_t pid, char **session) {
87         int r;
88         char *cgroup, *p;
89
90         if (!session)
91                 return -EINVAL;
92
93         r = pid_get_cgroup(pid, NULL, &cgroup);
94         if (r < 0)
95                 return r;
96
97         if (!startswith(cgroup, "/user/")) {
98                 free(cgroup);
99                 return -ENOENT;
100         }
101
102         p = strchr(cgroup + 6, '/');
103         if (!p) {
104                 free(cgroup);
105                 return -ENOENT;
106         }
107
108         p++;
109         if (startswith(p, "shared/") || streq(p, "shared")) {
110                 free(cgroup);
111                 return -ENOENT;
112         }
113
114         p = strndup(p, strcspn(p, "/"));
115         free(cgroup);
116
117         if (!p)
118                 return -ENOMEM;
119
120         *session = p;
121         return 0;
122 }
123
124 _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
125         int r;
126         char *root, *cgroup, *p, *cc;
127         struct stat st;
128
129         if (!uid)
130                 return -EINVAL;
131
132         r = pid_get_cgroup(pid, &root, &cgroup);
133         if (r < 0)
134                 return r;
135
136         if (!startswith(cgroup, "/user/")) {
137                 free(cgroup);
138                 free(root);
139                 return -ENOENT;
140         }
141
142         p = strchr(cgroup + 6, '/');
143         if (!p) {
144                 free(cgroup);
145                 return -ENOENT;
146         }
147
148         p++;
149         p += strcspn(p, "/");
150         *p = 0;
151
152         r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, root, cgroup, &cc);
153         free(root);
154         free(cgroup);
155
156         if (r < 0)
157                 return -ENOMEM;
158
159         r = lstat(cc, &st);
160         free(cc);
161
162         if (r < 0)
163                 return -errno;
164
165         if (!S_ISDIR(st.st_mode))
166                 return -ENOTDIR;
167
168         *uid = st.st_uid;
169         return 0;
170 }
171
172 _public_ int sd_uid_get_state(uid_t uid, char**state) {
173         char *p, *s = NULL;
174         int r;
175
176         if (!state)
177                 return -EINVAL;
178
179         if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) uid) < 0)
180                 return -ENOMEM;
181
182         r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
183         free(p);
184
185         if (r == -ENOENT) {
186                 free(s);
187                 s = strdup("offline");
188                 if (!s)
189                         return -ENOMEM;
190
191                 *state = s;
192                 return 0;
193         } else if (r < 0) {
194                 free(s);
195                 return r;
196         } else if (!s)
197                 return -EIO;
198
199         *state = s;
200         return 0;
201 }
202
203 _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) {
204         char *p, *w, *t, *state, *s = NULL;
205         size_t l;
206         int r;
207         const char *variable;
208
209         if (!seat)
210                 return -EINVAL;
211
212         variable = require_active ? "ACTIVE_UID" : "UIDS";
213
214         p = strappend("/run/systemd/seats/", seat);
215         if (!p)
216                 return -ENOMEM;
217
218         r = parse_env_file(p, NEWLINE, variable, &s, NULL);
219         free(p);
220
221         if (r < 0) {
222                 free(s);
223                 return r;
224         }
225
226         if (!s)
227                 return -EIO;
228
229         if (asprintf(&t, "%lu", (unsigned long) uid) < 0) {
230                 free(s);
231                 return -ENOMEM;
232         }
233
234         FOREACH_WORD(w, l, s, state) {
235                 if (strncmp(t, w, l) == 0) {
236                         free(s);
237                         free(t);
238
239                         return 1;
240                 }
241         }
242
243         free(s);
244         free(t);
245
246         return 0;
247 }
248
249 static int uid_get_array(uid_t uid, const char *variable, char ***array) {
250         char *p, *s = NULL;
251         char **a;
252         int r;
253
254         if (!array)
255                 return -EINVAL;
256
257         if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) uid) < 0)
258                 return -ENOMEM;
259
260         r = parse_env_file(p, NEWLINE,
261                            variable, &s,
262                            NULL);
263         free(p);
264
265         if (r < 0) {
266                 free(s);
267
268                 if (r == -ENOENT) {
269                         *array = NULL;
270                         return 0;
271                 }
272
273                 return r;
274         }
275
276         if (!s) {
277                 *array = NULL;
278                 return 0;
279         }
280
281         a = strv_split(s, " ");
282         free(s);
283
284         if (!a)
285                 return -ENOMEM;
286
287         *array = a;
288         return 0;
289 }
290
291 _public_ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions) {
292         return uid_get_array(uid, require_active ? "ACTIVE_SESSIONS" : "SESSIONS", sessions);
293 }
294
295 _public_ int sd_uid_get_seats(uid_t uid, int require_active, char ***seats) {
296         return uid_get_array(uid, require_active ? "ACTIVE_SEATS" : "SEATS", seats);
297 }
298
299 _public_ int sd_session_is_active(const char *session) {
300         int r;
301         char *p, *s = NULL;
302
303         if (!session)
304                 return -EINVAL;
305
306         p = strappend("/run/systemd/sessions/", session);
307         if (!p)
308                 return -ENOMEM;
309
310         r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
311         free(p);
312
313         if (r < 0) {
314                 free(s);
315                 return r;
316         }
317
318         if (!s)
319                 return -EIO;
320
321         r = parse_boolean(s);
322         free(s);
323
324         return r;
325 }
326
327 _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
328         int r;
329         char *p, *s = NULL;
330
331         if (!session)
332                 return -EINVAL;
333         if (!uid)
334                 return -EINVAL;
335
336         p = strappend("/run/systemd/sessions/", session);
337         if (!p)
338                 return -ENOMEM;
339
340         r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
341         free(p);
342
343         if (r < 0) {
344                 free(s);
345                 return r;
346         }
347
348         if (!s)
349                 return -EIO;
350
351         r = parse_uid(s, uid);
352         free(s);
353
354         return r;
355 }
356
357 _public_ int sd_session_get_seat(const char *session, char **seat) {
358         char *p, *s = NULL;
359         int r;
360
361         if (!session)
362                 return -EINVAL;
363         if (!seat)
364                 return -EINVAL;
365
366         p = strappend("/run/systemd/sessions/", session);
367         if (!p)
368                 return -ENOMEM;
369
370         r = parse_env_file(p, NEWLINE, "SEAT", &s, NULL);
371         free(p);
372
373         if (r < 0) {
374                 free(s);
375                 return r;
376         }
377
378         if (isempty(s))
379                 return -ENOENT;
380
381         *seat = s;
382         return 0;
383 }
384
385 _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
386         char *p, *s = NULL, *t = NULL;
387         int r;
388
389         if (!seat)
390                 return -EINVAL;
391         if (!session && !uid)
392                 return -EINVAL;
393
394         p = strappend("/run/systemd/seats/", seat);
395         if (!p)
396                 return -ENOMEM;
397
398         r = parse_env_file(p, NEWLINE,
399                            "ACTIVE", &s,
400                            "ACTIVE_UID", &t,
401                            NULL);
402         free(p);
403
404         if (r < 0) {
405                 free(s);
406                 free(t);
407                 return r;
408         }
409
410         if (session && !s)  {
411                 free(t);
412                 return -ENOENT;
413         }
414
415         if (uid && !t) {
416                 free(s);
417                 return -ENOENT;
418         }
419
420         if (uid && t) {
421                 r = parse_uid(t, uid);
422                 if (r < 0) {
423                         free(t);
424                         free(s);
425                         return r;
426                 }
427         }
428
429         free(t);
430
431         if (session && s)
432                 *session = s;
433         else
434                 free(s);
435
436         return 0;
437 }
438
439 _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **uids, unsigned *n_uids) {
440         char *p, *s = NULL, *t = NULL, **a = NULL;
441         uid_t *b = NULL;
442         unsigned n = 0;
443         int r;
444
445         if (!seat)
446                 return -EINVAL;
447
448         if (!sessions && !uids)
449                 return -EINVAL;
450
451         p = strappend("/run/systemd/seats/", seat);
452         if (!p)
453                 return -ENOMEM;
454
455         r = parse_env_file(p, NEWLINE,
456                            "SESSIONS", &s,
457                            "ACTIVE_SESSIONS", &t,
458                            NULL);
459         free(p);
460
461         if (r < 0) {
462                 free(s);
463                 free(t);
464                 return r;
465         }
466
467         if (sessions && s) {
468                 a = strv_split(s, " ");
469                 if (!a) {
470                         free(s);
471                         free(t);
472                         return -ENOMEM;
473                 }
474         }
475
476         free(s);
477
478         if (uids && t) {
479                 char *w, *state;
480                 size_t l;
481                 unsigned i = 0;
482
483                 FOREACH_WORD(w, l, t, state)
484                         n++;
485
486                 b = new(uid_t, n);
487                 if (!b) {
488                         strv_free(a);
489                         return -ENOMEM;
490                 }
491
492                 FOREACH_WORD(w, l, t, state) {
493                         char *k;
494
495                         k = strndup(w, l);
496                         if (!k) {
497                                 free(t);
498                                 free(b);
499                                 return -ENOMEM;
500                         }
501
502                         r = parse_uid(k, b + i);
503                         free(k);
504                         if (r < 0)
505                                 continue;
506
507                         i++;
508                 }
509         }
510
511         free(t);
512
513         if (sessions)
514                 *sessions = a;
515
516         if (uids)
517                 *uids = b;
518
519         if (n_uids)
520                 *n_uids = n;
521
522         return 0;
523 }
524
525 _public_ int sd_seat_can_multi_session(const char *seat) {
526         char *p, *s = NULL;
527         int r;
528
529         if (!seat)
530                 return -EINVAL;
531
532         p = strappend("/run/systemd/seats/", seat);
533         if (!p)
534                 return -ENOMEM;
535
536         r = parse_env_file(p, NEWLINE,
537                            "IS_VTCONSOLE", &s,
538                            NULL);
539         free(p);
540
541         if (r < 0) {
542                 free(s);
543                 return r;
544         }
545
546         if (s) {
547                 r = parse_boolean(s);
548                 free(s);
549         } else
550                 r = 0;
551
552         return r;
553 }
554
555 _public_ int sd_get_seats(char ***seats) {
556
557         if (!seats)
558                 return -EINVAL;
559
560         return get_files_in_directory("/run/systemd/seats/", seats);
561 }
562
563 _public_ int sd_get_sessions(char ***sessions) {
564
565         if (!sessions)
566                 return -EINVAL;
567
568         return get_files_in_directory("/run/systemd/sessions/", sessions);
569 }
570
571 _public_ int sd_get_uids(uid_t **users) {
572         DIR *d;
573         int r = 0;
574         unsigned n = 0;
575         uid_t *l = NULL;
576
577         if (!users)
578                 return -EINVAL;
579
580         d = opendir("/run/systemd/users/");
581         for (;;) {
582                 struct dirent buffer, *de;
583                 int k;
584                 uid_t uid;
585
586                 k = readdir_r(d, &buffer, &de);
587                 if (k != 0) {
588                         r = -k;
589                         goto finish;
590                 }
591
592                 if (!de)
593                         break;
594
595                 dirent_ensure_type(d, de);
596
597                 if (!dirent_is_file(de))
598                         continue;
599
600                 k = parse_uid(de->d_name, &uid);
601                 if (k < 0)
602                         continue;
603
604                 if ((unsigned) r >= n) {
605                         uid_t *t;
606
607                         n = MAX(16, 2*r);
608                         t = realloc(l, sizeof(uid_t) * n);
609                         if (!t) {
610                                 r = -ENOMEM;
611                                 goto finish;
612                         }
613
614                         l = t;
615                 }
616
617                 assert((unsigned) r < n);
618                 l[r++] = uid;
619         }
620
621 finish:
622         if (d)
623                 closedir(d);
624
625         if (r >= 0)
626                 *users = l;
627         else
628                 free(l);
629
630         return r;
631 }
632
633 static inline int MONITOR_TO_FD(sd_login_monitor *m) {
634         return (int) (unsigned long) m - 1;
635 }
636
637 static inline sd_login_monitor* FD_TO_MONITOR(int fd) {
638         return (sd_login_monitor*) (unsigned long) (fd + 1);
639 }
640
641 _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
642         int fd, k;
643         bool good = false;
644
645         if (!m)
646                 return -EINVAL;
647
648         fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
649         if (fd < 0)
650                 return errno;
651
652         if (!category || streq(category, "seat")) {
653                 k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
654                 if (k < 0) {
655                         close_nointr_nofail(fd);
656                         return -errno;
657                 }
658
659                 good = true;
660         }
661
662         if (!category || streq(category, "session")) {
663                 k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
664                 if (k < 0) {
665                         close_nointr_nofail(fd);
666                         return -errno;
667                 }
668
669                 good = true;
670         }
671
672         if (!category || streq(category, "uid")) {
673                 k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
674                 if (k < 0) {
675                         close_nointr_nofail(fd);
676                         return -errno;
677                 }
678
679                 good = true;
680         }
681
682         if (!good) {
683                 close_nointr(fd);
684                 return -EINVAL;
685         }
686
687         *m = FD_TO_MONITOR(fd);
688         return 0;
689 }
690
691 _public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
692         int fd;
693
694         if (!m)
695                 return NULL;
696
697         fd = MONITOR_TO_FD(m);
698         close_nointr(fd);
699
700         return NULL;
701 }
702
703 _public_ int sd_login_monitor_flush(sd_login_monitor *m) {
704
705         if (!m)
706                 return -EINVAL;
707
708         return flush_fd(MONITOR_TO_FD(m));
709 }
710
711 _public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {
712
713         if (!m)
714                 return -EINVAL;
715
716         return MONITOR_TO_FD(m);
717 }