chiark / gitweb /
volume_id: hpfs - read label and uuid
[elogind.git] / udev / udev_node.c
1 /*
2  * Copyright (C) 2004-2008 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 <fcntl.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <grp.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29
30 #include "udev.h"
31 #include "udev_rules.h"
32
33 #define TMP_FILE_EXT            ".udev-tmp"
34
35 int udev_node_mknod(struct udevice *udevice, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid)
36 {
37         char file_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
38         struct stat stats;
39         int preserve = 0;
40         int err = 0;
41
42         if (major(devt) != 0 && strcmp(udevice->dev->subsystem, "block") == 0)
43                 mode |= S_IFBLK;
44         else
45                 mode |= S_IFCHR;
46
47         if (lstat(file, &stats) == 0) {
48                 if (((stats.st_mode & S_IFMT) == (mode & S_IFMT)) && (stats.st_rdev == devt)) {
49                         info(udevice->udev, "preserve file '%s', because it has correct dev_t\n", file);
50                         preserve = 1;
51                         udev_selinux_lsetfilecon(udevice->udev, file, mode);
52                 } else {
53                         info(udevice->udev, "atomically replace existing file '%s'\n", file);
54                         util_strlcpy(file_tmp, file, sizeof(file_tmp));
55                         util_strlcat(file_tmp, TMP_FILE_EXT, sizeof(file_tmp));
56                         unlink(file_tmp);
57                         udev_selinux_setfscreatecon(udevice->udev, file_tmp, mode);
58                         err = mknod(file_tmp, mode, devt);
59                         udev_selinux_resetfscreatecon(udevice->udev);
60                         if (err != 0) {
61                                 err(udevice->udev, "mknod(%s, %#o, %u, %u) failed: %m\n",
62                                     file_tmp, mode, major(devt), minor(devt));
63                                 goto exit;
64                         }
65                         err = rename(file_tmp, file);
66                         if (err != 0) {
67                                 err(udevice->udev, "rename(%s, %s) failed: %m\n", file_tmp, file);
68                                 unlink(file_tmp);
69                         }
70                 }
71         } else {
72                 info(udevice->udev, "mknod(%s, %#o, (%u,%u))\n", file, mode, major(devt), minor(devt));
73                 udev_selinux_setfscreatecon(udevice->udev, file, mode);
74                 err = mknod(file, mode, devt);
75                 udev_selinux_resetfscreatecon(udevice->udev);
76                 if (err != 0) {
77                         err(udevice->udev, "mknod(%s, %#o, (%u,%u) failed: %m\n",
78                             file, mode, major(devt), minor(devt));
79                         goto exit;
80                 }
81         }
82
83         if (!preserve || stats.st_mode != mode) {
84                 info(udevice->udev, "chmod(%s, %#o)\n", file, mode);
85                 err = chmod(file, mode);
86                 if (err != 0) {
87                         err(udevice->udev, "chmod(%s, %#o) failed: %m\n", file, mode);
88                         goto exit;
89                 }
90         }
91
92         if (!preserve || stats.st_uid != uid || stats.st_gid != gid) {
93                 info(udevice->udev, "chown(%s, %u, %u)\n", file, uid, gid);
94                 err = chown(file, uid, gid);
95                 if (err != 0) {
96                         err(udevice->udev, "chown(%s, %u, %u) failed: %m\n", file, uid, gid);
97                         goto exit;
98                 }
99         }
100 exit:
101         return err;
102 }
103
104 static int node_symlink(struct udevice *udevice, const char *node, const char *slink)
105 {
106         struct stat stats;
107         char target[UTIL_PATH_SIZE] = "";
108         char slink_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
109         int i = 0;
110         int tail = 0;
111         int len;
112         int retval = 0;
113
114         /* use relative link */
115         while (node[i] && (node[i] == slink[i])) {
116                 if (node[i] == '/')
117                         tail = i+1;
118                 i++;
119         }
120         while (slink[i] != '\0') {
121                 if (slink[i] == '/')
122                         util_strlcat(target, "../", sizeof(target));
123                 i++;
124         }
125         util_strlcat(target, &node[tail], sizeof(target));
126
127         /* preserve link with correct target, do not replace node of other device */
128         if (lstat(slink, &stats) == 0) {
129                 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
130                         struct stat stats2;
131
132                         info(udevice->udev, "found existing node instead of symlink '%s'\n", slink);
133                         if (lstat(node, &stats2) == 0) {
134                                 if ((stats.st_mode & S_IFMT) == (stats2.st_mode & S_IFMT) &&
135                                     stats.st_rdev == stats2.st_rdev) {
136                                         info(udevice->udev, "replace device node '%s' with symlink to our node '%s'\n", slink, node);
137                                 } else {
138                                         err(udevice->udev, "device node '%s' already exists, link to '%s' will not overwrite it\n", slink, node);
139                                         goto exit;
140                                 }
141                         }
142                 } else if (S_ISLNK(stats.st_mode)) {
143                         char buf[UTIL_PATH_SIZE];
144
145                         info(udevice->udev, "found existing symlink '%s'\n", slink);
146                         len = readlink(slink, buf, sizeof(buf));
147                         if (len > 0) {
148                                 buf[len] = '\0';
149                                 if (strcmp(target, buf) == 0) {
150                                         info(udevice->udev, "preserve already existing symlink '%s' to '%s'\n", slink, target);
151                                         udev_selinux_lsetfilecon(udevice->udev, slink, S_IFLNK);
152                                         goto exit;
153                                 }
154                         }
155                 }
156         } else {
157                 info(udevice->udev, "creating symlink '%s' to '%s'\n", slink, target);
158                 udev_selinux_setfscreatecon(udevice->udev, slink, S_IFLNK);
159                 retval = symlink(target, slink);
160                 udev_selinux_resetfscreatecon(udevice->udev);
161                 if (retval == 0)
162                         goto exit;
163         }
164
165         info(udevice->udev, "atomically replace '%s'\n", slink);
166         util_strlcpy(slink_tmp, slink, sizeof(slink_tmp));
167         util_strlcat(slink_tmp, TMP_FILE_EXT, sizeof(slink_tmp));
168         unlink(slink_tmp);
169         udev_selinux_setfscreatecon(udevice->udev, slink, S_IFLNK);
170         retval = symlink(target, slink_tmp);
171         udev_selinux_resetfscreatecon(udevice->udev);
172         if (retval != 0) {
173                 err(udevice->udev, "symlink(%s, %s) failed: %m\n", target, slink_tmp);
174                 goto exit;
175         }
176         retval = rename(slink_tmp, slink);
177         if (retval != 0) {
178                 err(udevice->udev, "rename(%s, %s) failed: %m\n", slink_tmp, slink);
179                 unlink(slink_tmp);
180                 goto exit;
181         }
182 exit:
183         return retval;
184 }
185
186 static int update_link(struct udevice *udevice, const char *name)
187 {
188         LIST_HEAD(name_list);
189         char slink[UTIL_PATH_SIZE];
190         char node[UTIL_PATH_SIZE];
191         struct udevice *udevice_db;
192         struct name_entry *device;
193         char target[UTIL_PATH_SIZE] = "";
194         int count;
195         int priority = 0;
196         int rc = 0;
197
198         util_strlcpy(slink, udev_get_dev_path(udevice->udev), sizeof(slink));
199         util_strlcat(slink, "/", sizeof(slink));
200         util_strlcat(slink, name, sizeof(slink));
201
202         count = udev_db_get_devices_by_name(udevice->udev, name, &name_list);
203         info(udevice->udev, "found %i devices with name '%s'\n", count, name);
204
205         /* if we don't have a reference, delete it */
206         if (count <= 0) {
207                 info(udevice->udev, "no reference left, remove '%s'\n", name);
208                 if (!udevice->test_run) {
209                         unlink(slink);
210                         delete_path(udevice->udev, slink);
211                 }
212                 goto out;
213         }
214
215         /* find the device with the highest priority */
216         list_for_each_entry(device, &name_list, node) {
217                 info(udevice->udev, "found '%s' for '%s'\n", device->name, name);
218
219                 /* did we find ourself? we win, if we have the same priority */
220                 if (strcmp(udevice->dev->devpath, device->name) == 0) {
221                         info(udevice->udev, "compare (our own) priority of '%s' %i >= %i\n",
222                              udevice->dev->devpath, udevice->link_priority, priority);
223                         if (strcmp(udevice->name, name) == 0) {
224                                 info(udevice->udev, "'%s' is our device node, database inconsistent, skip link update\n", udevice->name);
225                         } else if (target[0] == '\0' || udevice->link_priority >= priority) {
226                                 priority = udevice->link_priority;
227                                 util_strlcpy(target, udevice->name, sizeof(target));
228                         }
229                         continue;
230                 }
231
232                 /* another device, read priority from database */
233                 udevice_db = udev_device_init(udevice->udev);
234                 if (udevice_db == NULL)
235                         continue;
236                 if (udev_db_get_device(udevice_db, device->name) == 0) {
237                         if (strcmp(udevice_db->name, name) == 0) {
238                                 info(udevice->udev, "'%s' is a device node of '%s', skip link update\n", udevice_db->name, device->name);
239                         } else {
240                                 info(udevice->udev, "compare priority of '%s' %i > %i\n",
241                                      udevice_db->dev->devpath, udevice_db->link_priority, priority);
242                                 if (target[0] == '\0' || udevice_db->link_priority > priority) {
243                                         priority = udevice_db->link_priority;
244                                         util_strlcpy(target, udevice_db->name, sizeof(target));
245                                 }
246                         }
247                 }
248                 udev_device_cleanup(udevice_db);
249         }
250         name_list_cleanup(udevice->udev, &name_list);
251
252         if (target[0] == '\0') {
253                 info(udevice->udev, "no current target for '%s' found\n", name);
254                 rc = 1;
255                 goto out;
256         }
257
258         /* create symlink to the target with the highest priority */
259         util_strlcpy(node, udev_get_dev_path(udevice->udev), sizeof(node));
260         util_strlcat(node, "/", sizeof(node));
261         util_strlcat(node, target, sizeof(node));
262         info(udevice->udev, "'%s' with target '%s' has the highest priority %i, create it\n", name, target, priority);
263         if (!udevice->test_run) {
264                 create_path(udevice->udev, slink);
265                 node_symlink(udevice, node, slink);
266         }
267 out:
268         return rc;
269 }
270
271 void udev_node_update_symlinks(struct udevice *udevice, struct udevice *udevice_old)
272 {
273         struct name_entry *name_loop;
274         char symlinks[UTIL_PATH_SIZE] = "";
275
276         list_for_each_entry(name_loop, &udevice->symlink_list, node) {
277                 info(udevice->udev, "update symlink '%s' of '%s'\n", name_loop->name, udevice->dev->devpath);
278                 update_link(udevice, name_loop->name);
279                 util_strlcat(symlinks, udev_get_dev_path(udevice->udev), sizeof(symlinks));
280                 util_strlcat(symlinks, "/", sizeof(symlinks));
281                 util_strlcat(symlinks, name_loop->name, sizeof(symlinks));
282                 util_strlcat(symlinks, " ", sizeof(symlinks));
283         }
284
285         /* export symlinks to environment */
286         util_remove_trailing_chars(symlinks, ' ');
287         if (symlinks[0] != '\0')
288                 setenv("DEVLINKS", symlinks, 1);
289
290         /* update possible left-over symlinks (device metadata changed) */
291         if (udevice_old != NULL) {
292                 struct name_entry *link_loop;
293                 struct name_entry *link_old_loop;
294                 int found;
295
296                 /* remove current symlinks from old list */
297                 list_for_each_entry(link_old_loop, &udevice_old->symlink_list, node) {
298                         found = 0;
299                         list_for_each_entry(link_loop, &udevice->symlink_list, node) {
300                                 if (strcmp(link_old_loop->name, link_loop->name) == 0) {
301                                         found = 1;
302                                         break;
303                                 }
304                         }
305                         if (!found) {
306                                 /* link does no longer belong to this device */
307                                 info(udevice->udev, "update old symlink '%s' no longer belonging to '%s'\n",
308                                      link_old_loop->name, udevice->dev->devpath);
309                                 update_link(udevice, link_old_loop->name);
310                         }
311                 }
312
313                 /*
314                  * if the node name has changed, delete the node,
315                  * or possibly restore a symlink of another device
316                  */
317                 if (strcmp(udevice->name, udevice_old->name) != 0)
318                         update_link(udevice, udevice_old->name);
319         }
320 }
321
322 int udev_node_add(struct udevice *udevice)
323 {
324         char filename[UTIL_PATH_SIZE];
325         uid_t uid;
326         gid_t gid;
327         int i;
328         int retval = 0;
329
330         util_strlcpy(filename, udev_get_dev_path(udevice->udev), sizeof(filename));
331         util_strlcat(filename, "/", sizeof(filename));
332         util_strlcat(filename, udevice->name, sizeof(filename));
333         create_path(udevice->udev, filename);
334
335         if (strcmp(udevice->owner, "root") == 0)
336                 uid = 0;
337         else {
338                 char *endptr;
339                 unsigned long id;
340
341                 id = strtoul(udevice->owner, &endptr, 10);
342                 if (endptr[0] == '\0')
343                         uid = (uid_t) id;
344                 else
345                         uid = lookup_user(udevice->udev, udevice->owner);
346         }
347
348         if (strcmp(udevice->group, "root") == 0)
349                 gid = 0;
350         else {
351                 char *endptr;
352                 unsigned long id;
353
354                 id = strtoul(udevice->group, &endptr, 10);
355                 if (endptr[0] == '\0')
356                         gid = (gid_t) id;
357                 else
358                         gid = lookup_group(udevice->udev, udevice->group);
359         }
360
361         info(udevice->udev, "creating device node '%s', major=%d, minor=%d, mode=%#o, uid=%d, gid=%d\n",
362              filename, major(udevice->devt), minor(udevice->devt), udevice->mode, uid, gid);
363
364         if (!udevice->test_run)
365                 if (udev_node_mknod(udevice, filename, udevice->devt, udevice->mode, uid, gid) != 0) {
366                         retval = -1;
367                         goto exit;
368                 }
369
370         setenv("DEVNAME", filename, 1);
371
372         /* create all_partitions if requested */
373         if (udevice->partitions) {
374                 char partitionname[UTIL_PATH_SIZE];
375                 char *attr;
376                 int range;
377
378                 /* take the maximum registered minor range */
379                 attr = sysfs_attr_get_value(udevice->udev, udevice->dev->devpath, "range");
380                 if (attr != NULL) {
381                         range = atoi(attr);
382                         if (range > 1)
383                                 udevice->partitions = range-1;
384                 }
385                 info(udevice->udev, "creating device partition nodes '%s[1-%i]'\n", filename, udevice->partitions);
386                 if (!udevice->test_run) {
387                         for (i = 1; i <= udevice->partitions; i++) {
388                                 dev_t part_devt;
389
390                                 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
391                                 partitionname[sizeof(partitionname)-1] = '\0';
392                                 part_devt = makedev(major(udevice->devt), minor(udevice->devt) + i);
393                                 udev_node_mknod(udevice, partitionname, part_devt, udevice->mode, uid, gid);
394                         }
395                 }
396         }
397 exit:
398         return retval;
399 }
400
401 int udev_node_remove(struct udevice *udevice)
402 {
403         char filename[UTIL_PATH_SIZE];
404         char partitionname[UTIL_PATH_SIZE];
405         struct stat stats;
406         int retval = 0;
407         int num;
408
409         util_strlcpy(filename, udev_get_dev_path(udevice->udev), sizeof(filename));
410         util_strlcat(filename, "/", sizeof(filename));
411         util_strlcat(filename, udevice->name, sizeof(filename));
412         if (stat(filename, &stats) != 0) {
413                 info(udevice->udev, "device node '%s' not found\n", filename);
414                 return 0;
415         }
416         if (udevice->devt && stats.st_rdev != udevice->devt) {
417                 info(udevice->udev, "device node '%s' points to a different device, skip removal\n", filename);
418                 return -1;
419         }
420
421         info(udevice->udev, "removing device node '%s'\n", filename);
422         if (!udevice->test_run)
423                 retval = unlink_secure(udevice->udev, filename);
424         if (retval)
425                 return retval;
426
427         setenv("DEVNAME", filename, 1);
428         num = udevice->partitions;
429         if (num > 0) {
430                 int i;
431
432                 info(udevice->udev, "removing all_partitions '%s[1-%i]'\n", filename, num);
433                 if (num > 255)
434                         return -1;
435                 for (i = 1; i <= num; i++) {
436                         snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
437                         partitionname[sizeof(partitionname)-1] = '\0';
438                         if (!udevice->test_run)
439                                 unlink_secure(udevice->udev, partitionname);
440                 }
441         }
442         delete_path(udevice->udev, filename);
443         return retval;
444 }