chiark / gitweb /
apply format char to variables exported by ENV
[elogind.git] / udev_node.c
1 /*
2  * udev-node.c - device node handling
3  *
4  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6  *
7  *      This program is free software; you can redistribute it and/or modify it
8  *      under the terms of the GNU General Public License as published by the
9  *      Free Software Foundation version 2 of the License.
10  * 
11  *      This program is distributed in the hope that it will be useful, but
12  *      WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *      General Public License for more details.
15  * 
16  *      You should have received a copy of the GNU General Public License along
17  *      with this program; if not, write to the Free Software Foundation, Inc.,
18  *      675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <stddef.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <grp.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32
33 #include "udev.h"
34 #include "udev_rules.h"
35 #include "udev_selinux.h"
36
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         struct stat stats;
41         int retval = 0;
42
43         if (major(devt) != 0 && strcmp(udev->dev->subsystem, "block") == 0)
44                 mode |= S_IFBLK;
45         else
46                 mode |= S_IFCHR;
47
48         if (stat(file, &stats) != 0)
49                 goto create;
50
51         /* preserve node with already correct numbers, to prevent changing the inode number */
52         if ((stats.st_mode & S_IFMT) == (mode & S_IFMT) && (stats.st_rdev == devt)) {
53                 info("preserve file '%s', because it has correct dev_t", file);
54                 selinux_setfilecon(file, udev->dev->kernel_name, stats.st_mode);
55                 goto perms;
56         }
57
58         if (unlink(file) != 0)
59                 err("unlink(%s) failed: %s", file, strerror(errno));
60         else
61                 dbg("already present file '%s' unlinked", file);
62
63 create:
64         selinux_setfscreatecon(file, udev->dev->kernel_name, mode);
65         retval = mknod(file, mode, devt);
66         selinux_resetfscreatecon();
67         if (retval != 0) {
68                 err("mknod(%s, %#o, %u, %u) failed: %s",
69                     file, mode, major(devt), minor(devt), strerror(errno));
70                 goto exit;
71         }
72
73 perms:
74         dbg("chmod(%s, %#o)", file, mode);
75         if (chmod(file, mode) != 0) {
76                 err("chmod(%s, %#o) failed: %s", file, mode, strerror(errno));
77                 goto exit;
78         }
79
80         if (uid != 0 || gid != 0) {
81                 dbg("chown(%s, %u, %u)", file, uid, gid);
82                 if (chown(file, uid, gid) != 0) {
83                         err("chown(%s, %u, %u) failed: %s",
84                             file, uid, gid, strerror(errno));
85                         goto exit;
86                 }
87         }
88
89 exit:
90         return retval;
91 }
92
93 int udev_node_add(struct udevice *udev, struct udevice *udev_old)
94 {
95         char filename[PATH_SIZE];
96         struct name_entry *name_loop;
97         uid_t uid;
98         gid_t gid;
99         int tail;
100         int i;
101         int retval = 0;
102
103         selinux_init();
104
105         snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
106         filename[sizeof(filename)-1] = '\0';
107
108         /* create parent directories if needed */
109         if (strchr(udev->name, '/'))
110                 create_path(filename);
111
112         if (strcmp(udev->owner, "root") == 0)
113                 uid = 0;
114         else {
115                 char *endptr;
116                 unsigned long id;
117
118                 id = strtoul(udev->owner, &endptr, 10);
119                 if (endptr[0] == '\0')
120                         uid = (uid_t) id;
121                 else
122                         uid = lookup_user(udev->owner);
123         }
124
125         if (strcmp(udev->group, "root") == 0)
126                 gid = 0;
127         else {
128                 char *endptr;
129                 unsigned long id;
130
131                 id = strtoul(udev->group, &endptr, 10);
132                 if (endptr[0] == '\0')
133                         gid = (gid_t) id;
134                 else
135                         gid = lookup_group(udev->group);
136         }
137
138         info("creating device node '%s', major = '%d', minor = '%d', " "mode = '%#o', uid = '%d', gid = '%d'",
139              filename, major(udev->devt), minor(udev->devt), udev->mode, uid, gid);
140
141         if (!udev->test_run)
142                 if (udev_node_mknod(udev, filename, udev->devt, udev->mode, uid, gid) != 0) {
143                         retval = -1;
144                         goto exit;
145                 }
146
147         setenv("DEVNAME", filename, 1);
148
149         /* create all_partitions if requested */
150         if (udev->partitions) {
151                 char partitionname[PATH_SIZE];
152                 char *attr;
153                 int range;
154
155                 /* take the maximum registered minor range */
156                 attr = sysfs_attr_get_value(udev->dev->devpath, "range");
157                 if (attr) {
158                         range = atoi(attr);
159                         if (range > 1)
160                                 udev->partitions = range-1;
161                 }
162                 info("creating device partition nodes '%s[1-%i]'", filename, udev->partitions);
163                 if (!udev->test_run) {
164                         for (i = 1; i <= udev->partitions; i++) {
165                                 dev_t part_devt;
166
167                                 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
168                                 partitionname[sizeof(partitionname)-1] = '\0';
169                                 part_devt = makedev(major(udev->devt), minor(udev->devt) + i);
170                                 udev_node_mknod(udev, partitionname, part_devt, udev->mode, uid, gid);
171                         }
172                 }
173         }
174
175         /* create symlink(s) if requested */
176         if (!list_empty(&udev->symlink_list)) {
177                 char symlinks[512] = "";
178
179                 list_for_each_entry(name_loop, &udev->symlink_list, node) {
180                         char linktarget[PATH_SIZE];
181
182                         snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
183                         filename[sizeof(filename)-1] = '\0';
184
185                         dbg("symlink '%s' to node '%s' requested", filename, udev->name);
186                         if (!udev->test_run)
187                                 if (strchr(filename, '/'))
188                                         create_path(filename);
189
190                         /* optimize relative link */
191                         linktarget[0] = '\0';
192                         i = 0;
193                         tail = 0;
194                         while (udev->name[i] && (udev->name[i] == name_loop->name[i])) {
195                                 if (udev->name[i] == '/')
196                                         tail = i+1;
197                                 i++;
198                         }
199                         while (name_loop->name[i] != '\0') {
200                                 if (name_loop->name[i] == '/')
201                                         strlcat(linktarget, "../", sizeof(linktarget));
202                                 i++;
203                         }
204
205                         strlcat(linktarget, &udev->name[tail], sizeof(linktarget));
206
207                         info("creating symlink '%s' to '%s'", filename, linktarget);
208                         if (!udev->test_run) {
209                                 unlink(filename);
210                                 selinux_setfscreatecon(filename, NULL, S_IFLNK);
211                                 if (symlink(linktarget, filename) != 0)
212                                         err("symlink(%s, %s) failed: %s", linktarget, filename, strerror(errno));
213                                 selinux_resetfscreatecon();
214                         }
215
216                         strlcat(symlinks, filename, sizeof(symlinks));
217                         strlcat(symlinks, " ", sizeof(symlinks));
218                 }
219
220                 remove_trailing_chars(symlinks, ' ');
221                 setenv("DEVLINKS", symlinks, 1);
222         }
223
224 exit:
225         selinux_exit();
226         return retval;
227 }
228
229 void udev_node_remove_symlinks(struct udevice *udev)
230 {
231         char filename[PATH_SIZE];
232         struct name_entry *name_loop;
233         struct stat stats;
234
235         if (!list_empty(&udev->symlink_list)) {
236                 char symlinks[512] = "";
237
238                 list_for_each_entry(name_loop, &udev->symlink_list, node) {
239                         snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
240                         filename[sizeof(filename)-1] = '\0';
241
242                         if (stat(filename, &stats) != 0) {
243                                 dbg("symlink '%s' not found", filename);
244                                 continue;
245                         }
246                         if (udev->devt && stats.st_rdev != udev->devt) {
247                                 info("symlink '%s' points to a different device, skip removal", filename);
248                                 continue;
249                         }
250
251                         info("removing symlink '%s'", filename);
252                         unlink(filename);
253
254                         if (strchr(filename, '/'))
255                                 delete_path(filename);
256
257                         strlcat(symlinks, filename, sizeof(symlinks));
258                         strlcat(symlinks, " ", sizeof(symlinks));
259                 }
260
261                 remove_trailing_chars(symlinks, ' ');
262                 if (symlinks[0] != '\0')
263                         setenv("DEVLINKS", symlinks, 1);
264         }
265 }
266
267 int udev_node_remove(struct udevice *udev)
268 {
269         char filename[PATH_SIZE];
270         char partitionname[PATH_SIZE];
271         struct stat stats;
272         int retval;
273         int num;
274
275         udev_node_remove_symlinks(udev);
276
277         snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
278         filename[sizeof(filename)-1] = '\0';
279
280         if (stat(filename, &stats) != 0) {
281                 dbg("device node '%s' not found", filename);
282                 return -1;
283         }
284         if (udev->devt && stats.st_rdev != udev->devt) {
285                 info("device node '%s' points to a different device, skip removal", filename);
286                 return -1;
287         }
288
289         info("removing device node '%s'", filename);
290         retval = unlink_secure(filename);
291         if (retval)
292                 return retval;
293
294         setenv("DEVNAME", filename, 1);
295
296         num = udev->partitions;
297         if (num > 0) {
298                 int i;
299
300                 info("removing all_partitions '%s[1-%i]'", filename, num);
301                 if (num > 255) {
302                         info("garbage from udev database, skip all_partitions removal");
303                         return -1;
304                 }
305                 for (i = 1; i <= num; i++) {
306                         snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
307                         partitionname[sizeof(partitionname)-1] = '\0';
308                         unlink_secure(partitionname);
309                 }
310         }
311
312         if (strchr(udev->name, '/'))
313                 delete_path(filename);
314
315         return retval;
316 }