chiark / gitweb /
vol_id: README update
[elogind.git] / udev_node.c
1 /*
2  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
3  * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
4  *
5  *      This program is free software; you can redistribute it and/or modify it
6  *      under the terms of the GNU General Public License as published by the
7  *      Free Software Foundation version 2 of the License.
8  * 
9  *      This program is distributed in the hope that it will be useful, but
10  *      WITHOUT ANY WARRANTY; without even the implied warranty of
11  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *      General Public License for more details.
13  * 
14  *      You should have received a copy of the GNU General Public License along
15  *      with this program; if not, write to the Free Software Foundation, Inc.,
16  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <stddef.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <grp.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31
32 #include "udev.h"
33 #include "udev_rules.h"
34 #include "udev_selinux.h"
35
36
37 int udev_node_mknod(struct udevice *udev, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid)
38 {
39         struct stat stats;
40         int retval = 0;
41
42         if (major(devt) != 0 && strcmp(udev->dev->subsystem, "block") == 0)
43                 mode |= S_IFBLK;
44         else
45                 mode |= S_IFCHR;
46
47         if (lstat(file, &stats) != 0)
48                 goto create;
49
50         /* preserve node with already correct numbers, to prevent changing the inode number */
51         if ((stats.st_mode & S_IFMT) == (mode & S_IFMT) && (stats.st_rdev == devt)) {
52                 info("preserve file '%s', because it has correct dev_t", file);
53                 selinux_setfilecon(file, udev->dev->kernel, stats.st_mode);
54                 goto perms;
55         }
56
57         if (unlink(file) != 0)
58                 err("unlink(%s) failed: %s", file, strerror(errno));
59         else
60                 dbg("already present file '%s' unlinked", file);
61
62 create:
63         selinux_setfscreatecon(file, udev->dev->kernel, mode);
64         retval = mknod(file, mode, devt);
65         selinux_resetfscreatecon();
66         if (retval != 0) {
67                 err("mknod(%s, %#o, %u, %u) failed: %s",
68                     file, mode, major(devt), minor(devt), strerror(errno));
69                 goto exit;
70         }
71
72 perms:
73         dbg("chmod(%s, %#o)", file, mode);
74         if (chmod(file, mode) != 0) {
75                 err("chmod(%s, %#o) failed: %s", file, mode, strerror(errno));
76                 goto exit;
77         }
78
79         if (uid != 0 || gid != 0) {
80                 dbg("chown(%s, %u, %u)", file, uid, gid);
81                 if (chown(file, uid, gid) != 0) {
82                         err("chown(%s, %u, %u) failed: %s",
83                             file, uid, gid, strerror(errno));
84                         goto exit;
85                 }
86         }
87
88 exit:
89         return retval;
90 }
91
92 static int node_symlink(const char *node, const char *slink)
93 {
94         char target[PATH_SIZE] = "";
95         char buf[PATH_SIZE];
96         int i = 0;
97         int tail = 0;
98         int len;
99
100         /* use relative link */
101         while (node[i] && (node[i] == slink[i])) {
102                 if (node[i] == '/')
103                         tail = i+1;
104                 i++;
105         }
106         while (slink[i] != '\0') {
107                 if (slink[i] == '/')
108                         strlcat(target, "../", sizeof(target));
109                 i++;
110         }
111         strlcat(target, &node[tail], sizeof(target));
112
113         /* look if symlink already exists */
114         len = readlink(slink, buf, sizeof(buf));
115         if (len > 0) {
116                 buf[len] = '\0';
117                 if (strcmp(target, buf) == 0) {
118                         info("preserve already existing symlink '%s' to '%s'", slink, target);
119                         selinux_setfilecon(slink, NULL, S_IFLNK);
120                         goto exit;
121                 }
122                 info("link '%s' points to different target '%s', delete it", slink, buf);
123                 unlink(slink);
124         }
125
126         /* create link */
127         info("creating symlink '%s' to '%s'", slink, target);
128         selinux_setfscreatecon(slink, NULL, S_IFLNK);
129         if (symlink(target, slink) != 0)
130                 err("symlink(%s, %s) failed: %s", target, slink, strerror(errno));
131         selinux_resetfscreatecon();
132
133 exit:
134         return 0;
135 }
136
137 static int update_link(struct udevice *udev, const char *name)
138 {
139         LIST_HEAD(name_list);
140         char slink[PATH_SIZE];
141         char node[PATH_SIZE];
142         struct udevice *udev_db;
143         struct name_entry *device;
144         char target[PATH_MAX] = "";
145         int count;
146         int priority = 0;
147         int rc = 0;
148
149         strlcpy(slink, udev_root, sizeof(slink));
150         strlcat(slink, "/", sizeof(slink));
151         strlcat(slink, name, sizeof(slink));
152
153         count = udev_db_get_devices_by_name(name, &name_list);
154         info("found %i devices with name '%s'", count, name);
155
156         /* if we don't have a reference, delete it */
157         if (count <= 0) {
158                 info("no reference left, remove '%s'", name);
159                 if (!udev->test_run) {
160                         unlink(slink);
161                         delete_path(slink);
162                 }
163                 goto out;
164         }
165
166         /* find the device with the highest priority */
167         list_for_each_entry(device, &name_list, node) {
168                 info("found '%s' for '%s'", device->name, name);
169
170                 /* did we find ourself? we win, if we have the same priority */
171                 if (strcmp(udev->dev->devpath, device->name) == 0) {
172                         info("compare (our own) priority of '%s' %i >= %i",
173                              udev->dev->devpath, udev->link_priority, priority);
174                         if (target[0] == '\0' || udev->link_priority >= priority) {
175                                 priority = udev->link_priority;
176                                 strlcpy(target, udev->name, sizeof(target));
177                         }
178                         continue;
179                 }
180
181                 /* or something else, then read priority from database */
182                 udev_db = udev_device_init(NULL);
183                 if (udev_db == NULL)
184                         continue;
185                 if (udev_db_get_device(udev_db, device->name) == 0) {
186                         info("compare priority of '%s' %i > %i",
187                              udev_db->dev->devpath, udev_db->link_priority, priority);
188                         if (target[0] == '\0' || udev_db->link_priority > priority) {
189                                 priority = udev_db->link_priority;
190                                 strlcpy(target, udev_db->name, sizeof(target));
191                         }
192                 }
193                 udev_device_cleanup(udev_db);
194         }
195         name_list_cleanup(&name_list);
196
197         if (target[0] == '\0') {
198                 err("missing target for '%s'", name);
199                 rc = -1;
200                 goto out;
201         }
202
203         /* create symlink to the target with the highest priority */
204         strlcpy(node, udev_root, sizeof(node));
205         strlcat(node, "/", sizeof(node));
206         strlcat(node, target, sizeof(node));
207         info("'%s' with target '%s' has the highest priority %i, create it", name, target, priority);
208         if (!udev->test_run) {
209                 create_path(slink);
210                 node_symlink(node, slink);
211         }
212 out:
213         return rc;
214 }
215
216 void udev_node_update_symlinks(struct udevice *udev, struct udevice *udev_old)
217 {
218         struct name_entry *name_loop;
219         char symlinks[PATH_SIZE] = "";
220
221         list_for_each_entry(name_loop, &udev->symlink_list, node) {
222                 info("update symlink '%s' of '%s'", name_loop->name, udev->dev->devpath);
223                 update_link(udev, name_loop->name);
224                 strlcat(symlinks, udev_root, sizeof(symlinks));
225                 strlcat(symlinks, "/", sizeof(symlinks));
226                 strlcat(symlinks, name_loop->name, sizeof(symlinks));
227                 strlcat(symlinks, " ", sizeof(symlinks));
228         }
229
230         /* export symlinks to environment */
231         remove_trailing_chars(symlinks, ' ');
232         if (symlinks[0] != '\0')
233                 setenv("DEVLINKS", symlinks, 1);
234
235         /* update possible left-over symlinks (device metadata changed) */
236         if (udev_old != NULL) {
237                 struct name_entry *link_loop;
238                 struct name_entry *link_old_loop;
239                 int found;
240
241                 /* remove current symlinks from old list */
242                 list_for_each_entry(link_old_loop, &udev_old->symlink_list, node) {
243                         found = 0;
244                         list_for_each_entry(link_loop, &udev->symlink_list, node) {
245                                 if (strcmp(link_old_loop->name, link_loop->name) == 0) {
246                                         found = 1;
247                                         break;
248                                 }
249                         }
250                         if (!found) {
251                                 /* link does no longer belong to this device */
252                                 info("update old symlink '%s' no longer belonging to '%s'",
253                                      link_old_loop->name, udev->dev->devpath);
254                                 update_link(udev, link_old_loop->name);
255                         }
256                 }
257
258                 /*
259                  * if the node name has changed, delete the node,
260                  * or possibly restore a symlink of another device
261                  */
262                 if (strcmp(udev->name, udev_old->name) != 0)
263                         update_link(udev, udev_old->name);
264         }
265 }
266
267 int udev_node_add(struct udevice *udev)
268 {
269         char filename[PATH_SIZE];
270         uid_t uid;
271         gid_t gid;
272         int i;
273         int retval = 0;
274
275         strlcpy(filename, udev_root, sizeof(filename));
276         strlcat(filename, "/", sizeof(filename));
277         strlcat(filename, udev->name, sizeof(filename));
278         create_path(filename);
279
280         if (strcmp(udev->owner, "root") == 0)
281                 uid = 0;
282         else {
283                 char *endptr;
284                 unsigned long id;
285
286                 id = strtoul(udev->owner, &endptr, 10);
287                 if (endptr[0] == '\0')
288                         uid = (uid_t) id;
289                 else
290                         uid = lookup_user(udev->owner);
291         }
292
293         if (strcmp(udev->group, "root") == 0)
294                 gid = 0;
295         else {
296                 char *endptr;
297                 unsigned long id;
298
299                 id = strtoul(udev->group, &endptr, 10);
300                 if (endptr[0] == '\0')
301                         gid = (gid_t) id;
302                 else
303                         gid = lookup_group(udev->group);
304         }
305
306         info("creating device node '%s', major = '%d', minor = '%d', " "mode = '%#o', uid = '%d', gid = '%d'",
307              filename, major(udev->devt), minor(udev->devt), udev->mode, uid, gid);
308
309         if (!udev->test_run)
310                 if (udev_node_mknod(udev, filename, udev->devt, udev->mode, uid, gid) != 0) {
311                         retval = -1;
312                         goto exit;
313                 }
314
315         setenv("DEVNAME", filename, 1);
316
317         /* create all_partitions if requested */
318         if (udev->partitions) {
319                 char partitionname[PATH_SIZE];
320                 char *attr;
321                 int range;
322
323                 /* take the maximum registered minor range */
324                 attr = sysfs_attr_get_value(udev->dev->devpath, "range");
325                 if (attr) {
326                         range = atoi(attr);
327                         if (range > 1)
328                                 udev->partitions = range-1;
329                 }
330                 info("creating device partition nodes '%s[1-%i]'", filename, udev->partitions);
331                 if (!udev->test_run) {
332                         for (i = 1; i <= udev->partitions; i++) {
333                                 dev_t part_devt;
334
335                                 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
336                                 partitionname[sizeof(partitionname)-1] = '\0';
337                                 part_devt = makedev(major(udev->devt), minor(udev->devt) + i);
338                                 udev_node_mknod(udev, partitionname, part_devt, udev->mode, uid, gid);
339                         }
340                 }
341         }
342 exit:
343         return retval;
344 }
345
346 int udev_node_remove(struct udevice *udev)
347 {
348         char filename[PATH_SIZE];
349         char partitionname[PATH_SIZE];
350         struct stat stats;
351         int retval = 0;
352         int num;
353
354         strlcpy(filename, udev_root, sizeof(filename));
355         strlcat(filename, "/", sizeof(filename));
356         strlcat(filename, udev->name, sizeof(filename));
357         if (stat(filename, &stats) != 0) {
358                 dbg("device node '%s' not found", filename);
359                 return -1;
360         }
361         if (udev->devt && stats.st_rdev != udev->devt) {
362                 info("device node '%s' points to a different device, skip removal", filename);
363                 return -1;
364         }
365
366         info("removing device node '%s'", filename);
367         if (!udev->test_run)
368                 retval = unlink_secure(filename);
369         if (retval)
370                 return retval;
371
372         setenv("DEVNAME", filename, 1);
373         num = udev->partitions;
374         if (num > 0) {
375                 int i;
376
377                 info("removing all_partitions '%s[1-%i]'", filename, num);
378                 if (num > 255)
379                         return -1;
380                 for (i = 1; i <= num; i++) {
381                         snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
382                         partitionname[sizeof(partitionname)-1] = '\0';
383                         if (!udev->test_run)
384                                 unlink_secure(partitionname);
385                 }
386         }
387         delete_path(filename);
388         return retval;
389 }