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