chiark / gitweb /
f17c48a7b410dc72fdb6bd67144c1b3dc0ed3e9a
[elogind.git] / udev / udev-node.c
1 /*
2  * Copyright (C) 2003-2009 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/stat.h>
29 #include <sys/types.h>
30
31 #include "udev.h"
32
33 #define TMP_FILE_EXT            ".udev-tmp"
34
35 int udev_node_mknod(struct udev_device *dev, const char *file, dev_t devnum, mode_t mode, uid_t uid, gid_t gid)
36 {
37         struct udev *udev = udev_device_get_udev(dev);
38         struct stat stats;
39         int preserve = 0;
40         int err = 0;
41
42         if (major(devnum) == 0)
43                 devnum = udev_device_get_devnum(dev);
44
45         if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
46                 mode |= S_IFBLK;
47         else
48                 mode |= S_IFCHR;
49
50         if (file == NULL)
51                 file = udev_device_get_devnode(dev);
52
53         if (lstat(file, &stats) == 0) {
54                 if (((stats.st_mode & S_IFMT) == (mode & S_IFMT)) && (stats.st_rdev == devnum)) {
55                         info(udev, "preserve file '%s', because it has correct dev_t\n", file);
56                         preserve = 1;
57                         udev_selinux_lsetfilecon(udev, file, mode);
58                 } else {
59                         char file_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
60
61                         info(udev, "atomically replace existing file '%s'\n", file);
62                         util_strscpyl(file_tmp, sizeof(file_tmp), file, TMP_FILE_EXT, NULL);
63                         unlink(file_tmp);
64                         udev_selinux_setfscreatecon(udev, file_tmp, mode);
65                         err = mknod(file_tmp, mode, devnum);
66                         udev_selinux_resetfscreatecon(udev);
67                         if (err != 0) {
68                                 err(udev, "mknod(%s, %#o, %u, %u) failed: %m\n",
69                                     file_tmp, mode, major(devnum), minor(devnum));
70                                 goto exit;
71                         }
72                         err = rename(file_tmp, file);
73                         if (err != 0) {
74                                 err(udev, "rename(%s, %s) failed: %m\n", file_tmp, file);
75                                 unlink(file_tmp);
76                         }
77                 }
78         } else {
79                 info(udev, "mknod(%s, %#o, (%u,%u))\n", file, mode, major(devnum), minor(devnum));
80                 udev_selinux_setfscreatecon(udev, file, mode);
81                 err = mknod(file, mode, devnum);
82                 udev_selinux_resetfscreatecon(udev);
83                 if (err != 0) {
84                         err(udev, "mknod(%s, %#o, (%u,%u) failed: %m\n", file, mode, major(devnum), minor(devnum));
85                         goto exit;
86                 }
87         }
88
89         if (!preserve || stats.st_mode != mode) {
90                 info(udev, "chmod(%s, %#o)\n", file, mode);
91                 err = chmod(file, mode);
92                 if (err != 0) {
93                         err(udev, "chmod(%s, %#o) failed: %m\n", file, mode);
94                         goto exit;
95                 }
96         }
97
98         if (!preserve || stats.st_uid != uid || stats.st_gid != gid) {
99                 info(udev, "chown(%s, %u, %u)\n", file, uid, gid);
100                 err = chown(file, uid, gid);
101                 if (err != 0) {
102                         err(udev, "chown(%s, %u, %u) failed: %m\n", file, uid, gid);
103                         goto exit;
104                 }
105         }
106 exit:
107         return err;
108 }
109
110 static int node_symlink(struct udev *udev, const char *node, const char *slink)
111 {
112         struct stat stats;
113         char target[UTIL_PATH_SIZE];
114         char *s;
115         size_t l;
116         char slink_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
117         int i = 0;
118         int tail = 0;
119         int err = 0;
120
121         /* use relative link */
122         target[0] = '\0';
123         while (node[i] && (node[i] == slink[i])) {
124                 if (node[i] == '/')
125                         tail = i+1;
126                 i++;
127         }
128         s = target;
129         l = sizeof(target);
130         while (slink[i] != '\0') {
131                 if (slink[i] == '/')
132                         l = util_strpcpy(&s, l, "../");
133                 i++;
134         }
135         l = util_strscpy(s, l, &node[tail]);
136         if (l == 0) {
137                 err = -EINVAL;
138                 goto exit;
139         }
140
141         /* preserve link with correct target, do not replace node of other device */
142         if (lstat(slink, &stats) == 0) {
143                 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
144                         struct stat stats2;
145
146                         info(udev, "found existing node instead of symlink '%s'\n", slink);
147                         if (lstat(node, &stats2) == 0) {
148                                 if ((stats.st_mode & S_IFMT) == (stats2.st_mode & S_IFMT) &&
149                                     stats.st_rdev == stats2.st_rdev && stats.st_ino != stats2.st_ino) {
150                                         info(udev, "replace device node '%s' with symlink to our node '%s'\n",
151                                              slink, node);
152                                 } else {
153                                         err(udev, "device node '%s' already exists, "
154                                             "link to '%s' will not overwrite it\n",
155                                             slink, node);
156                                         goto exit;
157                                 }
158                         }
159                 } else if (S_ISLNK(stats.st_mode)) {
160                         char buf[UTIL_PATH_SIZE];
161                         int len;
162
163                         dbg(udev, "found existing symlink '%s'\n", slink);
164                         len = readlink(slink, buf, sizeof(buf));
165                         if (len > 0) {
166                                 buf[len] = '\0';
167                                 if (strcmp(target, buf) == 0) {
168                                         info(udev, "preserve already existing symlink '%s' to '%s'\n",
169                                              slink, target);
170                                         udev_selinux_lsetfilecon(udev, slink, S_IFLNK);
171                                         goto exit;
172                                 }
173                         }
174                 }
175         } else {
176                 info(udev, "creating symlink '%s' to '%s'\n", slink, target);
177                 udev_selinux_setfscreatecon(udev, slink, S_IFLNK);
178                 err = symlink(target, slink);
179                 udev_selinux_resetfscreatecon(udev);
180                 if (err == 0)
181                         goto exit;
182         }
183
184         info(udev, "atomically replace '%s'\n", slink);
185         util_strscpyl(slink_tmp, sizeof(slink_tmp), slink, TMP_FILE_EXT, NULL);
186         unlink(slink_tmp);
187         udev_selinux_setfscreatecon(udev, slink, S_IFLNK);
188         err = symlink(target, slink_tmp);
189         udev_selinux_resetfscreatecon(udev);
190         if (err != 0) {
191                 err(udev, "symlink(%s, %s) failed: %m\n", target, slink_tmp);
192                 goto exit;
193         }
194         err = rename(slink_tmp, slink);
195         if (err != 0) {
196                 err(udev, "rename(%s, %s) failed: %m\n", slink_tmp, slink);
197                 unlink(slink_tmp);
198         }
199 exit:
200         return err;
201 }
202
203 /* find device node of device with highest priority */
204 static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize)
205 {
206         struct udev *udev = udev_device_get_udev(dev);
207         DIR *dir;
208         int priority = 0;
209         const char *target = NULL;
210
211         if (add) {
212                 priority = udev_device_get_devlink_priority(dev);
213                 util_strscpy(buf, bufsize, udev_device_get_devnode(dev));
214                 target = buf;
215         }
216
217         dir = opendir(stackdir);
218         if (dir == NULL)
219                 return target;
220         for (;;) {
221                 struct udev_device *dev_db;
222                 struct dirent *dent;
223                 char devpath[UTIL_PATH_SIZE];
224                 char syspath[UTIL_PATH_SIZE];
225                 ssize_t len;
226
227                 dent = readdir(dir);
228                 if (dent == NULL || dent->d_name[0] == '\0')
229                         break;
230                 if (dent->d_name[0] == '.')
231                         continue;
232                 dbg(udev, "found '%s/%s'\n", stackdir, dent->d_name);
233                 len = readlinkat(dirfd(dir), dent->d_name, devpath, sizeof(devpath));
234                 if (len <= 0 || len == (ssize_t)sizeof(devpath))
235                         continue;
236                 devpath[len] = '\0';
237                 util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
238                 info(udev, "found '%s' claiming '%s'\n", syspath, stackdir);
239
240                 /* did we find ourself? */
241                 if (strcmp(udev_device_get_syspath(dev), syspath) == 0)
242                         continue;
243
244                 dev_db = udev_device_new_from_syspath(udev, syspath);
245                 if (dev_db != NULL) {
246                         const char *devnode;
247
248                         devnode = udev_device_get_devnode(dev_db);
249                         if (devnode != NULL) {
250                                 dbg(udev, "compare priority of '%s'(%i) > '%s'(%i)\n", target, priority,
251                                     udev_device_get_devnode(dev_db), udev_device_get_devlink_priority(dev_db));
252                                 if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
253                                         info(udev, "'%s' claims priority %i for '%s'\n",
254                                              syspath, udev_device_get_devlink_priority(dev_db), stackdir);
255                                         priority = udev_device_get_devlink_priority(dev_db);
256                                         util_strscpy(buf, bufsize, devnode);
257                                         target = buf;
258                                 }
259                         }
260                         udev_device_unref(dev_db);
261                 }
262         }
263         return target;
264 }
265
266 /* manage "stack of names" with possibly specified device priorities */
267 static void link_update(struct udev_device *dev, const char *slink, bool add)
268 {
269         struct udev *udev = udev_device_get_udev(dev);
270         char name_enc[UTIL_PATH_SIZE];
271         char filename[UTIL_PATH_SIZE * 2];
272         char dirname[UTIL_PATH_SIZE];
273         const char *target;
274         char buf[UTIL_PATH_SIZE];
275
276         dbg(udev, "update symlink '%s' of '%s'\n", slink, udev_device_get_syspath(dev));
277
278         util_path_encode(&slink[strlen(udev_get_dev_path(udev))+1], name_enc, sizeof(name_enc));
279         snprintf(dirname, sizeof(dirname), "%s/.udev/links/%s", udev_get_dev_path(udev), name_enc);
280         snprintf(filename, sizeof(filename), "%s/%c%u:%u", dirname,
281                  strcmp(udev_device_get_subsystem(dev), "block") == 0 ? 'b' : 'c',
282                  major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
283
284         if (!add) {
285                 dbg(udev, "removing index: '%s'\n", filename);
286                 unlink(filename);
287                 util_delete_path(udev, filename);
288         }
289
290         target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
291         if (target == NULL) {
292                 info(udev, "no reference left, remove '%s'\n", slink);
293                 unlink(slink);
294                 util_delete_path(udev, slink);
295         } else {
296                 util_create_path(udev, slink);
297                 info(udev, "creating link '%s' to '%s'\n", slink, target);
298                 node_symlink(udev, target, slink);
299         }
300
301         if (add) {
302                 dbg(udev, "creating index: '%s'\n", filename);
303                 util_create_path(udev, filename);
304                 symlink(udev_device_get_devpath(dev), filename);
305         }
306 }
307
308 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
309 {
310         struct udev *udev = udev_device_get_udev(dev);
311         struct udev_list_entry *list_entry;
312
313         /* update possible left-over symlinks */
314         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
315                 const char *name = udev_list_entry_get_name(list_entry);
316                 struct udev_list_entry *list_entry_current;
317                 int found;
318
319                 /* check if old link name still belongs to this device */
320                 found = 0;
321                 udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
322                         const char *name_current = udev_list_entry_get_name(list_entry_current);
323
324                         if (strcmp(name, name_current) == 0) {
325                                 found = 1;
326                                 break;
327                         }
328                 }
329                 if (found)
330                         continue;
331
332                 info(udev, "update old name, '%s' no longer belonging to '%s'\n",
333                      name, udev_device_get_devpath(dev));
334                 link_update(dev, name, 0);
335         }
336 }
337
338 int udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid)
339 {
340         struct udev *udev = udev_device_get_udev(dev);
341         int i;
342         int num;
343         struct udev_list_entry *list_entry;
344         int err = 0;
345
346         info(udev, "creating device node '%s', devnum=%d:%d, mode=%#o, uid=%d, gid=%d\n",
347              udev_device_get_devnode(dev),
348              major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)),
349              mode, uid, gid);
350
351         util_create_path(udev, udev_device_get_devnode(dev));
352         if (udev_node_mknod(dev, NULL, makedev(0,0), mode, uid, gid) != 0) {
353                 err = -1;
354                 goto exit;
355         }
356
357         /* create all_partitions if requested */
358         num = udev_device_get_num_fake_partitions(dev);
359         if (num > 0) {
360                 info(udev, "creating device partition nodes '%s[1-%i]'\n", udev_device_get_devnode(dev), num);
361                 for (i = 1; i <= num; i++) {
362                         char partitionname[UTIL_PATH_SIZE];
363                         dev_t part_devnum;
364
365                         snprintf(partitionname, sizeof(partitionname), "%s%d",
366                                  udev_device_get_devnode(dev), i);
367                         partitionname[sizeof(partitionname)-1] = '\0';
368                         part_devnum = makedev(major(udev_device_get_devnum(dev)),
369                                             minor(udev_device_get_devnum(dev)) + i);
370                         udev_node_mknod(dev, partitionname, part_devnum, mode, uid, gid);
371                 }
372         }
373
374         /* create/update symlinks, add symlinks to name index */
375         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
376                 if (udev_list_entry_get_flag(list_entry)) {
377                         /* simple unmanaged link name */
378                         util_create_path(udev, udev_list_entry_get_name(list_entry));
379                         node_symlink(udev, udev_device_get_devnode(dev), udev_list_entry_get_name(list_entry));
380                 } else {
381                         link_update(dev, udev_list_entry_get_name(list_entry), 1);
382                 }
383         }
384 exit:
385         return err;
386 }
387
388 int udev_node_remove(struct udev_device *dev)
389 {
390         struct udev *udev = udev_device_get_udev(dev);
391         struct udev_list_entry *list_entry;
392         const char *devnode;
393         char partitionname[UTIL_PATH_SIZE];
394         struct stat stats;
395         int err = 0;
396         int num;
397
398         /* remove,update symlinks, remove symlinks from name index */
399         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
400                 link_update(dev, udev_list_entry_get_name(list_entry), 0);
401
402         devnode = udev_device_get_devnode(dev);
403         if (devnode == NULL)
404                 return 0;
405         if (stat(devnode, &stats) != 0) {
406                 info(udev, "device node '%s' not found\n", devnode);
407                 return 0;
408         }
409         if (stats.st_rdev != udev_device_get_devnum(dev)) {
410                 info(udev, "device node '%s' points to a different device, skip removal\n", devnode);
411                 return -1;
412         }
413
414         info(udev, "removing device node '%s'\n", devnode);
415         err = util_unlink_secure(udev, devnode);
416         if (err)
417                 return err;
418
419         num = udev_device_get_num_fake_partitions(dev);
420         if (num > 0) {
421                 int i;
422
423                 info(udev, "removing all_partitions '%s[1-%i]'\n", devnode, num);
424                 if (num > 255)
425                         return -1;
426                 for (i = 1; i <= num; i++) {
427                         snprintf(partitionname, sizeof(partitionname), "%s%d", devnode, i);
428                         partitionname[sizeof(partitionname)-1] = '\0';
429                         util_unlink_secure(udev, partitionname);
430                 }
431         }
432         util_delete_path(udev, devnode);
433         return err;
434 }