chiark / gitweb /
gudev: several minor introspection fixes
[elogind.git] / src / udev-node.c
1 /*
2  * Copyright (C) 2003-2010 Kay Sievers <kay.sievers@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
34 #define TMP_FILE_EXT                ".udev-tmp"
35
36 static int node_symlink(struct udev *udev, const char *node, const char *slink)
37 {
38         struct stat stats;
39         char target[UTIL_PATH_SIZE];
40         char *s;
41         size_t l;
42         char slink_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
43         int i = 0;
44         int tail = 0;
45         int err = 0;
46
47         /* use relative link */
48         target[0] = '\0';
49         while (node[i] && (node[i] == slink[i])) {
50                 if (node[i] == '/')
51                         tail = i+1;
52                 i++;
53         }
54         s = target;
55         l = sizeof(target);
56         while (slink[i] != '\0') {
57                 if (slink[i] == '/')
58                         l = util_strpcpy(&s, l, "../");
59                 i++;
60         }
61         l = util_strscpy(s, l, &node[tail]);
62         if (l == 0) {
63                 err = -EINVAL;
64                 goto exit;
65         }
66
67         /* preserve link with correct target, do not replace node of other device */
68         if (lstat(slink, &stats) == 0) {
69                 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
70                         struct stat stats2;
71
72                         info(udev, "found existing node instead of symlink '%s'\n", slink);
73                         if (lstat(node, &stats2) == 0) {
74                                 if ((stats.st_mode & S_IFMT) == (stats2.st_mode & S_IFMT) &&
75                                     stats.st_rdev == stats2.st_rdev && stats.st_ino != stats2.st_ino) {
76                                         info(udev, "replace device node '%s' with symlink to our node '%s'\n",
77                                              slink, node);
78                                 } else {
79                                         err(udev, "device node '%s' already exists, "
80                                             "link to '%s' will not overwrite it\n",
81                                             slink, node);
82                                         goto exit;
83                                 }
84                         }
85                 } else if (S_ISLNK(stats.st_mode)) {
86                         char buf[UTIL_PATH_SIZE];
87                         int len;
88
89                         dbg(udev, "found existing symlink '%s'\n", slink);
90                         len = readlink(slink, buf, sizeof(buf));
91                         if (len > 0 && len < (int)sizeof(buf)) {
92                                 buf[len] = '\0';
93                                 if (strcmp(target, buf) == 0) {
94                                         info(udev, "preserve already existing symlink '%s' to '%s'\n",
95                                              slink, target);
96                                         udev_selinux_lsetfilecon(udev, slink, S_IFLNK);
97                                         utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
98                                         goto exit;
99                                 }
100                         }
101                 }
102         } else {
103                 info(udev, "creating symlink '%s' to '%s'\n", slink, target);
104                 do {
105                         err = util_create_path_selinux(udev, slink);
106                         if (err != 0 && err != -ENOENT)
107                                 break;
108                         udev_selinux_setfscreatecon(udev, slink, S_IFLNK);
109                         err = symlink(target, slink);
110                         if (err != 0)
111                                 err = -errno;
112                         udev_selinux_resetfscreatecon(udev);
113                 } while (err == -ENOENT);
114                 if (err == 0)
115                         goto exit;
116         }
117
118         info(udev, "atomically replace '%s'\n", slink);
119         util_strscpyl(slink_tmp, sizeof(slink_tmp), slink, TMP_FILE_EXT, NULL);
120         unlink(slink_tmp);
121         do {
122                 err = util_create_path_selinux(udev, slink_tmp);
123                 if (err != 0 && err != -ENOENT)
124                         break;
125                 udev_selinux_setfscreatecon(udev, slink_tmp, S_IFLNK);
126                 err = symlink(target, slink_tmp);
127                 if (err != 0)
128                         err = -errno;
129                 udev_selinux_resetfscreatecon(udev);
130         } while (err == -ENOENT);
131         if (err != 0) {
132                 err(udev, "symlink '%s' '%s' failed: %m\n", target, slink_tmp);
133                 goto exit;
134         }
135         err = rename(slink_tmp, slink);
136         if (err != 0) {
137                 err(udev, "rename '%s' '%s' failed: %m\n", slink_tmp, slink);
138                 unlink(slink_tmp);
139         }
140 exit:
141         return err;
142 }
143
144 /* find device node of device with highest priority */
145 static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize)
146 {
147         struct udev *udev = udev_device_get_udev(dev);
148         DIR *dir;
149         int priority = 0;
150         const char *target = NULL;
151
152         if (add) {
153                 priority = udev_device_get_devlink_priority(dev);
154                 util_strscpy(buf, bufsize, udev_device_get_devnode(dev));
155                 target = buf;
156         }
157
158         dir = opendir(stackdir);
159         if (dir == NULL)
160                 return target;
161         for (;;) {
162                 struct udev_device *dev_db;
163                 struct dirent *dent;
164
165                 dent = readdir(dir);
166                 if (dent == NULL || dent->d_name[0] == '\0')
167                         break;
168                 if (dent->d_name[0] == '.')
169                         continue;
170
171                 info(udev, "found '%s' claiming '%s'\n", dent->d_name, stackdir);
172
173                 /* did we find ourself? */
174                 if (strcmp(dent->d_name, udev_device_get_id_filename(dev)) == 0)
175                         continue;
176
177                 dev_db = udev_device_new_from_id_filename(udev, dent->d_name);
178                 if (dev_db != NULL) {
179                         const char *devnode;
180
181                         devnode = udev_device_get_devnode(dev_db);
182                         if (devnode != NULL) {
183                                 dbg(udev, "compare priority of '%s'(%i) > '%s'(%i)\n", target, priority,
184                                     udev_device_get_devnode(dev_db), udev_device_get_devlink_priority(dev_db));
185                                 if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
186                                         info(udev, "'%s' claims priority %i for '%s'\n",
187                                              udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir);
188                                         priority = udev_device_get_devlink_priority(dev_db);
189                                         util_strscpy(buf, bufsize, devnode);
190                                         target = buf;
191                                 }
192                         }
193                         udev_device_unref(dev_db);
194                 }
195         }
196         closedir(dir);
197         return target;
198 }
199
200 /* manage "stack of names" with possibly specified device priorities */
201 static void link_update(struct udev_device *dev, const char *slink, bool add)
202 {
203         struct udev *udev = udev_device_get_udev(dev);
204         char name_enc[UTIL_PATH_SIZE];
205         char filename[UTIL_PATH_SIZE * 2];
206         char dirname[UTIL_PATH_SIZE];
207         const char *target;
208         char buf[UTIL_PATH_SIZE];
209
210         dbg(udev, "update symlink '%s' of '%s'\n", slink, udev_device_get_syspath(dev));
211
212         util_path_encode(&slink[strlen(udev_get_dev_path(udev))+1], name_enc, sizeof(name_enc));
213         util_strscpyl(dirname, sizeof(dirname), udev_get_run_path(udev), "/links/", name_enc, NULL);
214         util_strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL);
215
216         if (!add) {
217                 dbg(udev, "removing index: '%s'\n", filename);
218                 if (unlink(filename) == 0)
219                         rmdir(dirname);
220         }
221
222         target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
223         if (target == NULL) {
224                 info(udev, "no reference left, remove '%s'\n", slink);
225                 if (unlink(slink) == 0)
226                         util_delete_path(udev, slink);
227         } else {
228                 info(udev, "creating link '%s' to '%s'\n", slink, target);
229                 node_symlink(udev, target, slink);
230         }
231
232         if (add) {
233                 int err;
234
235                 dbg(udev, "creating index: '%s'\n", filename);
236                 do {
237                         int fd;
238
239                         err = util_create_path(udev, filename);
240                         if (err != 0 && err != -ENOENT)
241                                 break;
242                         fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
243                         if (fd >= 0)
244                                 close(fd);
245                         else
246                                 err = -errno;
247                 } while (err == -ENOENT);
248         }
249 }
250
251 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
252 {
253         struct udev *udev = udev_device_get_udev(dev);
254         struct udev_list_entry *list_entry;
255
256         /* update possible left-over symlinks */
257         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
258                 const char *name = udev_list_entry_get_name(list_entry);
259                 struct udev_list_entry *list_entry_current;
260                 int found;
261
262                 /* check if old link name still belongs to this device */
263                 found = 0;
264                 udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
265                         const char *name_current = udev_list_entry_get_name(list_entry_current);
266
267                         if (strcmp(name, name_current) == 0) {
268                                 found = 1;
269                                 break;
270                         }
271                 }
272                 if (found)
273                         continue;
274
275                 info(udev, "update old name, '%s' no longer belonging to '%s'\n",
276                      name, udev_device_get_devpath(dev));
277                 link_update(dev, name, 0);
278         }
279 }
280
281 static int node_fixup(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid)
282 {
283         struct udev *udev = udev_device_get_udev(dev);
284         const char *devnode = udev_device_get_devnode(dev);
285         dev_t devnum = udev_device_get_devnum(dev);
286         struct stat stats;
287         int err = 0;
288
289         if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
290                 mode |= S_IFBLK;
291         else
292                 mode |= S_IFCHR;
293
294         if (lstat(devnode, &stats) != 0) {
295                 err = -errno;
296                 info(udev, "can not stat() node '%s' (%m)\n", devnode);
297                 goto out;
298         }
299
300         if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
301                 err = -EEXIST;
302                 info(udev, "found node '%s' with non-matching devnum %s, skip handling\n",
303                      udev_device_get_devnode(dev), udev_device_get_id_filename(dev));
304                 goto out;
305         }
306
307         if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) {
308                 info(udev, "set permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
309                 chmod(devnode, mode);
310                 chown(devnode, uid, gid);
311         } else {
312                 info(udev, "preserve permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
313         }
314
315         /*
316          * Set initial selinux file context only on add events.
317          * We set the proper context on bootup (triger) or for newly
318          * added devices, but we don't change it later, in case
319          * something else has set a custom context in the meantime.
320          */
321         if (strcmp(udev_device_get_action(dev), "add") == 0)
322                 udev_selinux_lsetfilecon(udev, devnode, mode);
323
324         /* always update timestamp when we re-use the node, like on media change events */
325         utimensat(AT_FDCWD, devnode, NULL, 0);
326 out:
327         return err;
328 }
329
330 int udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid)
331 {
332         struct udev *udev = udev_device_get_udev(dev);
333         char filename[UTIL_PATH_SIZE];
334         struct udev_list_entry *list_entry;
335         int err = 0;
336
337         info(udev, "handling device node '%s', devnum=%s, mode=%#o, uid=%d, gid=%d\n",
338              udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid);
339
340         err = node_fixup(dev, mode, uid, gid);
341         if (err < 0)
342                 goto exit;
343
344         /* always add /dev/{block,char}/$major:$minor */
345         snprintf(filename, sizeof(filename), "%s/%s/%u:%u",
346                  udev_get_dev_path(udev),
347                  strcmp(udev_device_get_subsystem(dev), "block") == 0 ? "block" : "char",
348                  major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
349         node_symlink(udev, 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                 if (udev_list_entry_get_num(list_entry))
354                         /* simple unmanaged link name */
355                         node_symlink(udev, udev_device_get_devnode(dev), udev_list_entry_get_name(list_entry));
356                 else
357                         link_update(dev, udev_list_entry_get_name(list_entry), 1);
358         }
359 exit:
360         return err;
361 }
362
363 int udev_node_remove(struct udev_device *dev)
364 {
365         struct udev *udev = udev_device_get_udev(dev);
366         struct udev_list_entry *list_entry;
367         const char *devnode;
368         struct stat stats;
369         struct udev_device *dev_check;
370         char filename[UTIL_PATH_SIZE];
371         int err = 0;
372
373         /* remove/update symlinks, remove symlinks from name index */
374         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
375                 link_update(dev, udev_list_entry_get_name(list_entry), 0);
376
377         /* remove /dev/{block,char}/$major:$minor */
378         snprintf(filename, sizeof(filename), "%s/%s/%u:%u",
379                  udev_get_dev_path(udev),
380                  strcmp(udev_device_get_subsystem(dev), "block") == 0 ? "block" : "char",
381                  major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
382         unlink(filename);
383 out:
384         return err;
385 }