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