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