chiark / gitweb /
conf-parser: distinguish between multiple sections with the same name
[elogind.git] / src / udev / udev-node.c
1 /*
2  * Copyright (C) 2003-2013 Kay Sievers <kay@vrfy.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <stddef.h>
22 #include <stdbool.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <grp.h>
27 #include <dirent.h>
28 #include <sys/time.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31
32 #include "udev.h"
33 #include "smack-util.h"
34
35 static int node_symlink(struct udev_device *dev, const char *node, const char *slink)
36 {
37         struct stat stats;
38         char target[UTIL_PATH_SIZE];
39         char *s;
40         size_t l;
41         char slink_tmp[UTIL_PATH_SIZE + 32];
42         int i = 0;
43         int tail = 0;
44         int err = 0;
45
46         /* use relative link */
47         target[0] = '\0';
48         while (node[i] && (node[i] == slink[i])) {
49                 if (node[i] == '/')
50                         tail = i+1;
51                 i++;
52         }
53         s = target;
54         l = sizeof(target);
55         while (slink[i] != '\0') {
56                 if (slink[i] == '/')
57                         l = strpcpy(&s, l, "../");
58                 i++;
59         }
60         l = strscpy(s, l, &node[tail]);
61         if (l == 0) {
62                 err = -EINVAL;
63                 goto exit;
64         }
65
66         /* preserve link with correct target, do not replace node of other device */
67         if (lstat(slink, &stats) == 0) {
68                 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
69                         log_error("conflicting device node '%s' found, link to '%s' will not be created\n", slink, node);
70                         goto exit;
71                 } else if (S_ISLNK(stats.st_mode)) {
72                         char buf[UTIL_PATH_SIZE];
73                         int len;
74
75                         len = readlink(slink, buf, sizeof(buf));
76                         if (len > 0 && len < (int)sizeof(buf)) {
77                                 buf[len] = '\0';
78                                 if (streq(target, buf)) {
79                                         log_debug("preserve already existing symlink '%s' to '%s'\n", slink, target);
80                                         label_fix(slink, true, false);
81                                         utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
82                                         goto exit;
83                                 }
84                         }
85                 }
86         } else {
87                 log_debug("creating symlink '%s' to '%s'\n", slink, target);
88                 do {
89                         err = mkdir_parents_label(slink, 0755);
90                         if (err != 0 && err != -ENOENT)
91                                 break;
92                         label_context_set(slink, S_IFLNK);
93                         err = symlink(target, slink);
94                         if (err != 0)
95                                 err = -errno;
96                         label_context_clear();
97                 } while (err == -ENOENT);
98                 if (err == 0)
99                         goto exit;
100         }
101
102         log_debug("atomically replace '%s'\n", slink);
103         strscpyl(slink_tmp, sizeof(slink_tmp), slink, ".tmp-", udev_device_get_id_filename(dev), NULL);
104         unlink(slink_tmp);
105         do {
106                 err = mkdir_parents_label(slink_tmp, 0755);
107                 if (err != 0 && err != -ENOENT)
108                         break;
109                 label_context_set(slink_tmp, S_IFLNK);
110                 err = symlink(target, slink_tmp);
111                 if (err != 0)
112                         err = -errno;
113                 label_context_clear();
114         } while (err == -ENOENT);
115         if (err != 0) {
116                 log_error("symlink '%s' '%s' failed: %m\n", target, slink_tmp);
117                 goto exit;
118         }
119         err = rename(slink_tmp, slink);
120         if (err != 0) {
121                 log_error("rename '%s' '%s' failed: %m\n", slink_tmp, slink);
122                 unlink(slink_tmp);
123         }
124 exit:
125         return err;
126 }
127
128 /* find device node of device with highest priority */
129 static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize)
130 {
131         struct udev *udev = udev_device_get_udev(dev);
132         DIR *dir;
133         int priority = 0;
134         const char *target = NULL;
135
136         if (add) {
137                 priority = udev_device_get_devlink_priority(dev);
138                 strscpy(buf, bufsize, udev_device_get_devnode(dev));
139                 target = buf;
140         }
141
142         dir = opendir(stackdir);
143         if (dir == NULL)
144                 return target;
145         for (;;) {
146                 struct udev_device *dev_db;
147                 struct dirent *dent;
148
149                 dent = readdir(dir);
150                 if (dent == NULL || dent->d_name[0] == '\0')
151                         break;
152                 if (dent->d_name[0] == '.')
153                         continue;
154
155                 log_debug("found '%s' claiming '%s'\n", dent->d_name, stackdir);
156
157                 /* did we find ourself? */
158                 if (streq(dent->d_name, udev_device_get_id_filename(dev)))
159                         continue;
160
161                 dev_db = udev_device_new_from_device_id(udev, dent->d_name);
162                 if (dev_db != NULL) {
163                         const char *devnode;
164
165                         devnode = udev_device_get_devnode(dev_db);
166                         if (devnode != NULL) {
167                                 if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
168                                         log_debug("'%s' claims priority %i for '%s'\n",
169                                                   udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir);
170                                         priority = udev_device_get_devlink_priority(dev_db);
171                                         strscpy(buf, bufsize, devnode);
172                                         target = buf;
173                                 }
174                         }
175                         udev_device_unref(dev_db);
176                 }
177         }
178         closedir(dir);
179         return target;
180 }
181
182 /* manage "stack of names" with possibly specified device priorities */
183 static void link_update(struct udev_device *dev, const char *slink, bool add)
184 {
185         struct udev *udev = udev_device_get_udev(dev);
186         char name_enc[UTIL_PATH_SIZE];
187         char filename[UTIL_PATH_SIZE * 2];
188         char dirname[UTIL_PATH_SIZE];
189         const char *target;
190         char buf[UTIL_PATH_SIZE];
191
192         util_path_encode(slink + strlen("/dev"), name_enc, sizeof(name_enc));
193         strscpyl(dirname, sizeof(dirname), "/run/udev/links/", name_enc, NULL);
194         strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL);
195
196         if (!add && unlink(filename) == 0)
197                 rmdir(dirname);
198
199         target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
200         if (target == NULL) {
201                 log_debug("no reference left, remove '%s'\n", slink);
202                 if (unlink(slink) == 0)
203                         util_delete_path(udev, slink);
204         } else {
205                 log_debug("creating link '%s' to '%s'\n", slink, target);
206                 node_symlink(dev, target, slink);
207         }
208
209         if (add) {
210                 int err;
211
212                 do {
213                         int fd;
214
215                         err = mkdir_parents(filename, 0755);
216                         if (err != 0 && err != -ENOENT)
217                                 break;
218                         fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
219                         if (fd >= 0)
220                                 close(fd);
221                         else
222                                 err = -errno;
223                 } while (err == -ENOENT);
224         }
225 }
226
227 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
228 {
229         struct udev_list_entry *list_entry;
230
231         /* update possible left-over symlinks */
232         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
233                 const char *name = udev_list_entry_get_name(list_entry);
234                 struct udev_list_entry *list_entry_current;
235                 int found;
236
237                 /* check if old link name still belongs to this device */
238                 found = 0;
239                 udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
240                         const char *name_current = udev_list_entry_get_name(list_entry_current);
241
242                         if (streq(name, name_current)) {
243                                 found = 1;
244                                 break;
245                         }
246                 }
247                 if (found)
248                         continue;
249
250                 log_debug("update old name, '%s' no longer belonging to '%s'\n",
251                      name, udev_device_get_devpath(dev));
252                 link_update(dev, name, false);
253         }
254 }
255
256 static int node_permissions_apply(struct udev_device *dev, bool apply,
257                                   mode_t mode, uid_t uid, gid_t gid,
258                                   struct udev_list *seclabel_list) {
259         const char *devnode = udev_device_get_devnode(dev);
260         dev_t devnum = udev_device_get_devnum(dev);
261         struct stat stats;
262         struct udev_list_entry *entry;
263         int err = 0;
264
265         if (streq(udev_device_get_subsystem(dev), "block"))
266                 mode |= S_IFBLK;
267         else
268                 mode |= S_IFCHR;
269
270         if (lstat(devnode, &stats) != 0) {
271                 err = -errno;
272                 log_debug("can not stat() node '%s' (%m)\n", devnode);
273                 goto out;
274         }
275
276         if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
277                 err = -EEXIST;
278                 log_debug("found node '%s' with non-matching devnum %s, skip handling\n",
279                           udev_device_get_devnode(dev), udev_device_get_id_filename(dev));
280                 goto out;
281         }
282
283         if (apply) {
284                 bool selinux = false;
285                 bool smack = false;
286
287                 if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) {
288                         log_debug("set permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
289                         chmod(devnode, mode);
290                         chown(devnode, uid, gid);
291                 } else {
292                         log_debug("preserve permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
293                 }
294
295                 /* apply SECLABEL{$module}=$label */
296                 udev_list_entry_foreach(entry, udev_list_get_entry(seclabel_list)) {
297                         const char *name, *label;
298
299                         name = udev_list_entry_get_name(entry);
300                         label = udev_list_entry_get_value(entry);
301
302                         if (streq(name, "selinux")) {
303                                 selinux = true;
304                                 if (label_apply(devnode, label) < 0)
305                                         log_error("SECLABEL: failed to set SELinux label '%s'", label);
306                                 else
307                                         log_debug("SECLABEL: set SELinux label '%s'", label);
308
309                         } else if (streq(name, "smack")) {
310                                 smack = true;
311                                 if (smack_label_path(devnode, label) < 0)
312                                         log_error("SECLABEL: failed to set SMACK label '%s'", label);
313                                 else
314                                         log_debug("SECLABEL: set SMACK label '%s'", label);
315
316                         } else
317                                 log_error("SECLABEL: unknown subsystem, ignoring '%s'='%s'", name, label);
318                 }
319
320                 /* set the defaults */
321                 if (!selinux)
322                         label_fix(devnode, true, false);
323                 if (!smack)
324                         smack_label_path(devnode, NULL);
325         }
326
327         /* always update timestamp when we re-use the node, like on media change events */
328         utimensat(AT_FDCWD, devnode, NULL, 0);
329 out:
330         return err;
331 }
332
333 void udev_node_add(struct udev_device *dev, bool apply,
334                    mode_t mode, uid_t uid, gid_t gid,
335                    struct udev_list *seclabel_list) {
336         char filename[UTIL_PATH_SIZE];
337         struct udev_list_entry *list_entry;
338
339         log_debug("handling device node '%s', devnum=%s, mode=%#o, uid=%d, gid=%d\n",
340                   udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid);
341
342         if (node_permissions_apply(dev, apply, mode, uid, gid, seclabel_list) < 0)
343                 return;
344
345         /* always add /dev/{block,char}/$major:$minor */
346         snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
347                  streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
348                  major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
349         node_symlink(dev, udev_device_get_devnode(dev), filename);
350
351         /* create/update symlinks, add symlinks to name index */
352         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
353                         link_update(dev, udev_list_entry_get_name(list_entry), true);
354 }
355
356 void udev_node_remove(struct udev_device *dev)
357 {
358         struct udev_list_entry *list_entry;
359         char filename[UTIL_PATH_SIZE];
360
361         /* remove/update symlinks, remove symlinks from name index */
362         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
363                 link_update(dev, udev_list_entry_get_name(list_entry), false);
364
365         /* remove /dev/{block,char}/$major:$minor */
366         snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
367                  streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
368                  major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
369         unlink(filename);
370 }