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