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