chiark / gitweb /
store devpath with the usual leading slash
[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 <sys/stat.h>
29 #include <sys/types.h>
30
31 #include "udev.h"
32 #include "udev_rules.h"
33 #include "udev_selinux.h"
34
35
36 int udev_node_mknod(struct udevice *udev, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid)
37 {
38         struct stat stats;
39         int retval = 0;
40
41         if (major(devt) != 0 && strcmp(udev->dev->subsystem, "block") == 0)
42                 mode |= S_IFBLK;
43         else
44                 mode |= S_IFCHR;
45
46         if (stat(file, &stats) != 0)
47                 goto create;
48
49         /* preserve node with already correct numbers, to prevent changing the inode number */
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
56         if (unlink(file) != 0)
57                 err("unlink(%s) failed: %s", file, strerror(errno));
58         else
59                 dbg("already present file '%s' unlinked", file);
60
61 create:
62         selinux_setfscreatecon(file, udev->dev->kernel, mode);
63         retval = mknod(file, mode, devt);
64         selinux_resetfscreatecon();
65         if (retval != 0) {
66                 err("mknod(%s, %#o, %u, %u) failed: %s",
67                     file, mode, major(devt), minor(devt), strerror(errno));
68                 goto exit;
69         }
70
71 perms:
72         dbg("chmod(%s, %#o)", file, mode);
73         if (chmod(file, mode) != 0) {
74                 err("chmod(%s, %#o) failed: %s", file, mode, strerror(errno));
75                 goto exit;
76         }
77
78         if (uid != 0 || gid != 0) {
79                 dbg("chown(%s, %u, %u)", file, uid, gid);
80                 if (chown(file, uid, gid) != 0) {
81                         err("chown(%s, %u, %u) failed: %s",
82                             file, uid, gid, strerror(errno));
83                         goto exit;
84                 }
85         }
86
87 exit:
88         return retval;
89 }
90
91 static int node_symlink(const char *node, const char *slink)
92 {
93         char target[PATH_SIZE] = "";
94         char buf[PATH_SIZE];
95         int i = 0;
96         int tail = 0;
97         int len;
98
99         /* use relative link */
100         while (node[i] && (node[i] == slink[i])) {
101                 if (node[i] == '/')
102                         tail = i+1;
103                 i++;
104         }
105         while (slink[i] != '\0') {
106                 if (slink[i] == '/')
107                         strlcat(target, "../", sizeof(target));
108                 i++;
109         }
110         strlcat(target, &node[tail], sizeof(target));
111
112         /* look if symlink already exists */
113         len = readlink(slink, buf, sizeof(buf));
114         if (len > 0) {
115                 buf[len] = '\0';
116                 if (strcmp(target, buf) == 0) {
117                         info("preserving symlink '%s' to '%s'", slink, target);
118                         selinux_setfilecon(slink, NULL, S_IFLNK);
119                         goto exit;
120                 }
121                 info("link '%s' points to different target '%s', delete it", slink, buf);
122                 unlink(slink);
123         }
124
125         /* create link */
126         info("creating symlink '%s' to '%s'", slink, target);
127         selinux_setfscreatecon(slink, NULL, S_IFLNK);
128         if (symlink(target, slink) != 0)
129                 err("symlink(%s, %s) failed: %s", target, slink, strerror(errno));
130         selinux_resetfscreatecon();
131
132 exit:
133         return 0;
134 }
135
136 int udev_node_add(struct udevice *udev, struct udevice *udev_old)
137 {
138         char filename[PATH_SIZE];
139         uid_t uid;
140         gid_t gid;
141         int i;
142         int retval = 0;
143
144         snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
145         filename[sizeof(filename)-1] = '\0';
146
147         /* create parent directories if needed */
148         create_path(filename);
149
150         if (strcmp(udev->owner, "root") == 0)
151                 uid = 0;
152         else {
153                 char *endptr;
154                 unsigned long id;
155
156                 id = strtoul(udev->owner, &endptr, 10);
157                 if (endptr[0] == '\0')
158                         uid = (uid_t) id;
159                 else
160                         uid = lookup_user(udev->owner);
161         }
162
163         if (strcmp(udev->group, "root") == 0)
164                 gid = 0;
165         else {
166                 char *endptr;
167                 unsigned long id;
168
169                 id = strtoul(udev->group, &endptr, 10);
170                 if (endptr[0] == '\0')
171                         gid = (gid_t) id;
172                 else
173                         gid = lookup_group(udev->group);
174         }
175
176         info("creating device node '%s', major = '%d', minor = '%d', " "mode = '%#o', uid = '%d', gid = '%d'",
177              filename, major(udev->devt), minor(udev->devt), udev->mode, uid, gid);
178
179         if (!udev->test_run)
180                 if (udev_node_mknod(udev, filename, udev->devt, udev->mode, uid, gid) != 0) {
181                         retval = -1;
182                         goto exit;
183                 }
184
185         setenv("DEVNAME", filename, 1);
186
187         /* create all_partitions if requested */
188         if (udev->partitions) {
189                 char partitionname[PATH_SIZE];
190                 char *attr;
191                 int range;
192
193                 /* take the maximum registered minor range */
194                 attr = sysfs_attr_get_value(udev->dev->devpath, "range");
195                 if (attr) {
196                         range = atoi(attr);
197                         if (range > 1)
198                                 udev->partitions = range-1;
199                 }
200                 info("creating device partition nodes '%s[1-%i]'", filename, udev->partitions);
201                 if (!udev->test_run) {
202                         for (i = 1; i <= udev->partitions; i++) {
203                                 dev_t part_devt;
204
205                                 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
206                                 partitionname[sizeof(partitionname)-1] = '\0';
207                                 part_devt = makedev(major(udev->devt), minor(udev->devt) + i);
208                                 udev_node_mknod(udev, partitionname, part_devt, udev->mode, uid, gid);
209                         }
210                 }
211         }
212
213         /* create symlink(s) if requested */
214         if (!list_empty(&udev->symlink_list)) {
215                 struct name_entry *name_loop;
216                 char symlinks[PATH_SIZE] = "";
217
218                 list_for_each_entry(name_loop, &udev->symlink_list, node) {
219                         char slink[PATH_SIZE];
220
221                         strlcpy(slink, udev_root, sizeof(slink));
222                         strlcat(slink, "/", sizeof(slink));
223                         strlcat(slink, name_loop->name, sizeof(slink));
224
225                         info("creating symlink '%s' to node '%s'", slink, filename);
226                         if (!udev->test_run) {
227                                 create_path(slink);
228                                 node_symlink(filename, slink);
229                         }
230
231                         strlcat(symlinks, slink, sizeof(symlinks));
232                         strlcat(symlinks, " ", sizeof(symlinks));
233                 }
234
235                 remove_trailing_chars(symlinks, ' ');
236                 setenv("DEVLINKS", symlinks, 1);
237         }
238
239 exit:
240         selinux_exit();
241         return retval;
242 }
243
244 void udev_node_remove_symlinks(struct udevice *udev)
245 {
246         char filename[PATH_SIZE];
247         struct name_entry *name_loop;
248         struct stat stats;
249
250         if (!list_empty(&udev->symlink_list)) {
251                 char symlinks[PATH_SIZE] = "";
252
253                 list_for_each_entry(name_loop, &udev->symlink_list, node) {
254                         char devpath[PATH_SIZE];
255
256                         snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
257                         filename[sizeof(filename)-1] = '\0';
258
259                         if (stat(filename, &stats) != 0) {
260                                 dbg("symlink '%s' not found", filename);
261                                 continue;
262                         }
263                         if (udev->devt && stats.st_rdev != udev->devt) {
264                                 info("symlink '%s' points to a different device, skip removal", filename);
265                                 continue;
266                         }
267
268                         info("removing symlink '%s'", filename);
269                         if (!udev->test_run) {
270                                 unlink(filename);
271                                 delete_path(filename);
272                         }
273
274                         /* see if another device wants this symlink */
275                         if (udev_db_lookup_name(name_loop->name, devpath, sizeof(devpath)) == 0) {
276                                 struct udevice *old;
277
278                                 info("found overwritten symlink '%s' of '%s'", name_loop->name, devpath);
279                                 old = udev_device_init();
280                                 if (old != NULL) {
281                                         if (udev_db_get_device(old, devpath) == 0) {
282                                                 char slink[PATH_SIZE];
283                                                 char node[PATH_SIZE];
284
285                                                 strlcpy(slink, udev_root, sizeof(slink));
286                                                 strlcat(slink, "/", sizeof(slink));
287                                                 strlcat(slink, name_loop->name, sizeof(slink));
288                                                 strlcpy(node, udev_root, sizeof(node));
289                                                 strlcat(node, "/", sizeof(node));
290                                                 strlcat(node, old->name, sizeof(node));
291                                                 info("restore symlink '%s' to '%s'", slink, node);
292                                                 if (!udev->test_run)
293                                                         node_symlink(node, slink);
294                                         }
295                                         udev_device_cleanup(old);
296                                 }
297                         }
298
299                         strlcat(symlinks, filename, sizeof(symlinks));
300                         strlcat(symlinks, " ", sizeof(symlinks));
301                 }
302
303                 remove_trailing_chars(symlinks, ' ');
304                 if (symlinks[0] != '\0')
305                         setenv("DEVLINKS", symlinks, 1);
306         }
307 }
308
309 int udev_node_remove(struct udevice *udev)
310 {
311         char filename[PATH_SIZE];
312         char partitionname[PATH_SIZE];
313         struct stat stats;
314         int retval;
315         int num;
316
317         udev_node_remove_symlinks(udev);
318
319         snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
320         filename[sizeof(filename)-1] = '\0';
321         if (stat(filename, &stats) != 0) {
322                 dbg("device node '%s' not found", filename);
323                 return -1;
324         }
325         if (udev->devt && stats.st_rdev != udev->devt) {
326                 info("device node '%s' points to a different device, skip removal", filename);
327                 return -1;
328         }
329
330         info("removing device node '%s'", filename);
331         retval = unlink_secure(filename);
332         if (retval)
333                 return retval;
334
335         setenv("DEVNAME", filename, 1);
336         num = udev->partitions;
337         if (num > 0) {
338                 int i;
339
340                 info("removing all_partitions '%s[1-%i]'", filename, num);
341                 if (num > 255) {
342                         info("garbage from udev database, skip all_partitions removal");
343                         return -1;
344                 }
345                 for (i = 1; i <= num; i++) {
346                         snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
347                         partitionname[sizeof(partitionname)-1] = '\0';
348                         unlink_secure(partitionname);
349                 }
350         }
351         delete_path(filename);
352         return retval;
353 }