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