chiark / gitweb /
util: user parse_uid() wherever applicable
[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         unsigned long ul;
331
332         if (!session)
333                 return -EINVAL;
334         if (!uid)
335                 return -EINVAL;
336
337         p = strappend("/run/systemd/sessions/", session);
338         if (!p)
339                 return -ENOMEM;
340
341         r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
342         free(p);
343
344         if (r < 0) {
345                 free(s);
346                 return r;
347         }
348
349         if (!s)
350                 return -EIO;
351
352         r = parse_uid(s, uid);
353         free(s);
354
355         return r;
356 }
357
358 _public_ int sd_session_get_seat(const char *session, char **seat) {
359         char *p, *s = NULL;
360         int r;
361
362         if (!session)
363                 return -EINVAL;
364         if (!seat)
365                 return -EINVAL;
366
367         p = strappend("/run/systemd/sessions/", session);
368         if (!p)
369                 return -ENOMEM;
370
371         r = parse_env_file(p, NEWLINE, "SEAT", &s, NULL);
372         free(p);
373
374         if (r < 0) {
375                 free(s);
376                 return r;
377         }
378
379         if (isempty(s))
380                 return -ENOENT;
381
382         *seat = s;
383         return 0;
384 }
385
386 _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
387         char *p, *s = NULL, *t = NULL;
388         int r;
389
390         if (!seat)
391                 return -EINVAL;
392         if (!session && !uid)
393                 return -EINVAL;
394
395         p = strappend("/run/systemd/seats/", seat);
396         if (!p)
397                 return -ENOMEM;
398
399         r = parse_env_file(p, NEWLINE,
400                            "ACTIVE", &s,
401                            "ACTIVE_UID", &t,
402                            NULL);
403         free(p);
404
405         if (r < 0) {
406                 free(s);
407                 free(t);
408                 return r;
409         }
410
411         if (session && !s)  {
412                 free(t);
413                 return -ENOENT;
414         }
415
416         if (uid && !t) {
417                 free(s);
418                 return -ENOENT;
419         }
420
421         if (uid && t) {
422                 r = parse_uid(t, uid);
423                 if (r < 0) {
424                         free(t);
425                         free(s);
426                         return r;
427                 }
428         }
429
430         free(t);
431
432         if (session && s)
433                 *session = s;
434         else
435                 free(s);
436
437         return 0;
438 }
439
440 _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **uids, unsigned *n_uids) {
441         char *p, *s = NULL, *t = NULL, **a = NULL;
442         uid_t *b = NULL;
443         unsigned n = 0;
444         int r;
445
446         if (!seat)
447                 return -EINVAL;
448
449         if (!sessions && !uids)
450                 return -EINVAL;
451
452         p = strappend("/run/systemd/seats/", seat);
453         if (!p)
454                 return -ENOMEM;
455
456         r = parse_env_file(p, NEWLINE,
457                            "SESSIONS", &s,
458                            "ACTIVE_SESSIONS", &t,
459                            NULL);
460         free(p);
461
462         if (r < 0) {
463                 free(s);
464                 free(t);
465                 return r;
466         }
467
468         if (sessions && s) {
469                 a = strv_split(s, " ");
470                 if (!a) {
471                         free(s);
472                         free(t);
473                         return -ENOMEM;
474                 }
475         }
476
477         free(s);
478
479         if (uids && t) {
480                 char *w, *state;
481                 size_t l;
482                 unsigned i = 0;
483
484                 FOREACH_WORD(w, l, t, state)
485                         n++;
486
487                 b = new(uid_t, n);
488                 if (!b) {
489                         strv_free(a);
490                         return -ENOMEM;
491                 }
492
493                 FOREACH_WORD(w, l, t, state) {
494                         char *k;
495
496                         k = strndup(w, l);
497                         if (!k) {
498                                 free(t);
499                                 free(b);
500                                 return -ENOMEM;
501                         }
502
503                         r = parse_uid(k, b + i);
504                         free(k);
505                         if (r < 0)
506                                 continue;
507
508                         i++;
509                 }
510         }
511
512         free(t);
513
514         if (sessions)
515                 *sessions = a;
516
517         if (uids)
518                 *uids = b;
519
520         if (n_uids)
521                 *n_uids = n;
522
523         return 0;
524 }
525
526 _public_ int sd_get_seats(char ***seats) {
527
528         if (!seats)
529                 return -EINVAL;
530
531         return get_files_in_directory("/run/systemd/seats/", seats);
532 }
533
534 _public_ int sd_get_sessions(char ***sessions) {
535
536         if (!sessions)
537                 return -EINVAL;
538
539         return get_files_in_directory("/run/systemd/sessions/", sessions);
540 }
541
542 _public_ int sd_get_uids(uid_t **users) {
543         DIR *d;
544         int r = 0;
545         unsigned n = 0;
546         uid_t *l = NULL;
547
548         if (!users)
549                 return -EINVAL;
550
551         d = opendir("/run/systemd/users/");
552         for (;;) {
553                 struct dirent buffer, *de;
554                 int k;
555                 uid_t uid;
556
557                 k = readdir_r(d, &buffer, &de);
558                 if (k != 0) {
559                         r = -k;
560                         goto finish;
561                 }
562
563                 if (!de)
564                         break;
565
566                 dirent_ensure_type(d, de);
567
568                 if (!dirent_is_file(de))
569                         continue;
570
571                 k = parse_uid(de->d_name, &uid);
572                 if (k < 0)
573                         continue;
574
575                 if ((unsigned) r >= n) {
576                         uid_t *t;
577
578                         n = MAX(16, 2*r);
579                         t = realloc(l, sizeof(uid_t) * n);
580                         if (!t) {
581                                 r = -ENOMEM;
582                                 goto finish;
583                         }
584
585                         l = t;
586                 }
587
588                 assert((unsigned) r < n);
589                 l[r++] = uid;
590         }
591
592 finish:
593         if (d)
594                 closedir(d);
595
596         if (r >= 0)
597                 *users = l;
598         else
599                 free(l);
600
601         return r;
602 }
603
604 static inline int MONITOR_TO_FD(sd_login_monitor *m) {
605         return (int) (unsigned long) m - 1;
606 }
607
608 static inline sd_login_monitor* FD_TO_MONITOR(int fd) {
609         return (sd_login_monitor*) (unsigned long) (fd + 1);
610 }
611
612 _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
613         const char *path;
614         int fd, k;
615         bool good = false;
616
617         if (!m)
618                 return -EINVAL;
619
620         fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
621         if (fd < 0)
622                 return errno;
623
624         if (!category || streq(category, "seat")) {
625                 k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
626                 if (k < 0) {
627                         close_nointr_nofail(fd);
628                         return -errno;
629                 }
630
631                 good = true;
632         }
633
634         if (!category || streq(category, "session")) {
635                 k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
636                 if (k < 0) {
637                         close_nointr_nofail(fd);
638                         return -errno;
639                 }
640
641                 good = true;
642         }
643
644         if (!category || streq(category, "uid")) {
645                 k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
646                 if (k < 0) {
647                         close_nointr_nofail(fd);
648                         return -errno;
649                 }
650
651                 good = true;
652         }
653
654         if (!good) {
655                 close_nointr(fd);
656                 return -EINVAL;
657         }
658
659         *m = FD_TO_MONITOR(fd);
660         return 0;
661 }
662
663 _public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
664         int fd;
665
666         if (!m)
667                 return NULL;
668
669         fd = MONITOR_TO_FD(m);
670         close_nointr(fd);
671
672         return NULL;
673 }
674
675 _public_ int sd_login_monitor_flush(sd_login_monitor *m) {
676
677         if (!m)
678                 return -EINVAL;
679
680         return flush_fd(MONITOR_TO_FD(m));
681 }
682
683 _public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {
684
685         if (!m)
686                 return -EINVAL;
687
688         return MONITOR_TO_FD(m);
689 }