chiark / gitweb /
build-sys: add a makefile target to run all tests through valgrind
[elogind.git] / src / udev / udev-builtin-uaccess.c
1 /*
2  * manage device node user ACL
3  *
4  * Copyright 2010-2012 Kay Sievers <kay@vrfy.org>
5  * Copyright 2010 Lennart Poettering
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <dirent.h>
30 #include <getopt.h>
31
32 #include <systemd/sd-login.h>
33 #include "logind-acl.h"
34 #include "udev.h"
35 #include "util.h"
36
37 static int builtin_uaccess(struct udev_device *dev, int argc, char *argv[], bool test)
38 {
39         int r;
40         const char *path = NULL, *seat;
41         bool changed_acl = false;
42         uid_t uid;
43
44         log_set_target(LOG_TARGET_AUTO);
45         log_parse_environment();
46         log_open();
47
48         umask(0022);
49
50         /* don't muck around with ACLs when the system is not running systemd */
51         if (!logind_running())
52                 return 0;
53
54         path = udev_device_get_devnode(dev);
55         seat = udev_device_get_property_value(dev, "ID_SEAT");
56         if (!seat)
57                 seat = "seat0";
58
59         r = sd_seat_get_active(seat, NULL, &uid);
60         if (r == -ENOENT) {
61                 /* No active session on this seat */
62                 r = 0;
63                 goto finish;
64         } else if (r < 0) {
65                 log_error("Failed to determine active user on seat %s.", seat);
66                 goto finish;
67         }
68
69         r = devnode_acl(path, true, false, 0, true, uid);
70         if (r < 0) {
71                 log_error("Failed to apply ACL on %s: %s", path, strerror(-r));
72                 goto finish;
73         }
74
75         changed_acl = true;
76         r = 0;
77
78 finish:
79         if (path && !changed_acl) {
80                 int k;
81
82                 /* Better be safe than sorry and reset ACL */
83                 k = devnode_acl(path, true, false, 0, false, 0);
84                 if (k < 0) {
85                         log_error("Failed to apply ACL on %s: %s", path, strerror(-k));
86                         if (r >= 0)
87                                 r = k;
88                 }
89         }
90
91         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
92 }
93
94 const struct udev_builtin udev_builtin_uaccess = {
95         .name = "uaccess",
96         .cmd = builtin_uaccess,
97         .help = "manage device node user ACL",
98 };