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