chiark / gitweb /
don't report an error on overlong comment lines
[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 static int udev_node_symlink(struct udevice *udev, const char *linktarget, const char *filename)
94 {
95         char target[PATH_SIZE];
96         int len;
97
98         /* look if symlink already exists */
99         len = readlink(filename, target, sizeof(target));
100         if (len > 0) {
101                 target[len] = '\0';
102                 if (strcmp(linktarget, target) == 0) {
103                         info("preserving symlink '%s' to '%s'", filename, linktarget);
104                         selinux_setfilecon(filename, NULL, S_IFLNK);
105                         goto exit;
106                 } else {
107                         info("link '%s' points to different target '%s', delete it", filename, target);
108                         unlink(filename);
109                 }
110         }
111
112         /* create link */
113         info("creating symlink '%s' to '%s'", filename, linktarget);
114         selinux_setfscreatecon(filename, NULL, S_IFLNK);
115         if (symlink(linktarget, filename) != 0)
116                 err("symlink(%s, %s) failed: %s", linktarget, filename, strerror(errno));
117         selinux_resetfscreatecon();
118
119 exit:
120         return 0;
121 }
122
123 int udev_node_add(struct udevice *udev, struct udevice *udev_old)
124 {
125         char filename[PATH_SIZE];
126         struct name_entry *name_loop;
127         uid_t uid;
128         gid_t gid;
129         int tail;
130         int i;
131         int retval = 0;
132
133         selinux_init();
134
135         snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
136         filename[sizeof(filename)-1] = '\0';
137
138         /* create parent directories if needed */
139         if (strchr(udev->name, '/'))
140                 create_path(filename);
141
142         if (strcmp(udev->owner, "root") == 0)
143                 uid = 0;
144         else {
145                 char *endptr;
146                 unsigned long id;
147
148                 id = strtoul(udev->owner, &endptr, 10);
149                 if (endptr[0] == '\0')
150                         uid = (uid_t) id;
151                 else
152                         uid = lookup_user(udev->owner);
153         }
154
155         if (strcmp(udev->group, "root") == 0)
156                 gid = 0;
157         else {
158                 char *endptr;
159                 unsigned long id;
160
161                 id = strtoul(udev->group, &endptr, 10);
162                 if (endptr[0] == '\0')
163                         gid = (gid_t) id;
164                 else
165                         gid = lookup_group(udev->group);
166         }
167
168         info("creating device node '%s', major = '%d', minor = '%d', " "mode = '%#o', uid = '%d', gid = '%d'",
169              filename, major(udev->devt), minor(udev->devt), udev->mode, uid, gid);
170
171         if (!udev->test_run)
172                 if (udev_node_mknod(udev, filename, udev->devt, udev->mode, uid, gid) != 0) {
173                         retval = -1;
174                         goto exit;
175                 }
176
177         setenv("DEVNAME", filename, 1);
178
179         /* create all_partitions if requested */
180         if (udev->partitions) {
181                 char partitionname[PATH_SIZE];
182                 char *attr;
183                 int range;
184
185                 /* take the maximum registered minor range */
186                 attr = sysfs_attr_get_value(udev->dev->devpath, "range");
187                 if (attr) {
188                         range = atoi(attr);
189                         if (range > 1)
190                                 udev->partitions = range-1;
191                 }
192                 info("creating device partition nodes '%s[1-%i]'", filename, udev->partitions);
193                 if (!udev->test_run) {
194                         for (i = 1; i <= udev->partitions; i++) {
195                                 dev_t part_devt;
196
197                                 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
198                                 partitionname[sizeof(partitionname)-1] = '\0';
199                                 part_devt = makedev(major(udev->devt), minor(udev->devt) + i);
200                                 udev_node_mknod(udev, partitionname, part_devt, udev->mode, uid, gid);
201                         }
202                 }
203         }
204
205         /* create symlink(s) if requested */
206         if (!list_empty(&udev->symlink_list)) {
207                 char symlinks[512] = "";
208
209                 list_for_each_entry(name_loop, &udev->symlink_list, node) {
210                         char linktarget[PATH_SIZE];
211
212                         snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
213                         filename[sizeof(filename)-1] = '\0';
214
215                         dbg("symlink '%s' to node '%s' requested", filename, udev->name);
216                         if (!udev->test_run)
217                                 if (strchr(filename, '/'))
218                                         create_path(filename);
219
220                         /* optimize relative link */
221                         linktarget[0] = '\0';
222                         i = 0;
223                         tail = 0;
224                         while (udev->name[i] && (udev->name[i] == name_loop->name[i])) {
225                                 if (udev->name[i] == '/')
226                                         tail = i+1;
227                                 i++;
228                         }
229                         while (name_loop->name[i] != '\0') {
230                                 if (name_loop->name[i] == '/')
231                                         strlcat(linktarget, "../", sizeof(linktarget));
232                                 i++;
233                         }
234
235                         strlcat(linktarget, &udev->name[tail], sizeof(linktarget));
236
237                         info("creating symlink '%s' to '%s'", filename, linktarget);
238                         if (!udev->test_run)
239                                 udev_node_symlink(udev, linktarget, filename);
240
241                         strlcat(symlinks, filename, sizeof(symlinks));
242                         strlcat(symlinks, " ", sizeof(symlinks));
243                 }
244
245                 remove_trailing_chars(symlinks, ' ');
246                 setenv("DEVLINKS", symlinks, 1);
247         }
248
249 exit:
250         selinux_exit();
251         return retval;
252 }
253
254 void udev_node_remove_symlinks(struct udevice *udev)
255 {
256         char filename[PATH_SIZE];
257         struct name_entry *name_loop;
258         struct stat stats;
259
260         if (!list_empty(&udev->symlink_list)) {
261                 char symlinks[512] = "";
262
263                 list_for_each_entry(name_loop, &udev->symlink_list, node) {
264                         snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
265                         filename[sizeof(filename)-1] = '\0';
266
267                         if (stat(filename, &stats) != 0) {
268                                 dbg("symlink '%s' not found", filename);
269                                 continue;
270                         }
271                         if (udev->devt && stats.st_rdev != udev->devt) {
272                                 info("symlink '%s' points to a different device, skip removal", filename);
273                                 continue;
274                         }
275
276                         info("removing symlink '%s'", filename);
277                         if (!udev->test_run) {
278                                 unlink(filename);
279
280                                 if (strchr(filename, '/'))
281                                         delete_path(filename);
282                         }
283
284                         strlcat(symlinks, filename, sizeof(symlinks));
285                         strlcat(symlinks, " ", sizeof(symlinks));
286                 }
287
288                 remove_trailing_chars(symlinks, ' ');
289                 if (symlinks[0] != '\0')
290                         setenv("DEVLINKS", symlinks, 1);
291         }
292 }
293
294 int udev_node_remove(struct udevice *udev)
295 {
296         char filename[PATH_SIZE];
297         char partitionname[PATH_SIZE];
298         struct stat stats;
299         int retval;
300         int num;
301
302         udev_node_remove_symlinks(udev);
303
304         snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
305         filename[sizeof(filename)-1] = '\0';
306
307         if (stat(filename, &stats) != 0) {
308                 dbg("device node '%s' not found", filename);
309                 return -1;
310         }
311         if (udev->devt && stats.st_rdev != udev->devt) {
312                 info("device node '%s' points to a different device, skip removal", filename);
313                 return -1;
314         }
315
316         info("removing device node '%s'", filename);
317         retval = unlink_secure(filename);
318         if (retval)
319                 return retval;
320
321         setenv("DEVNAME", filename, 1);
322
323         num = udev->partitions;
324         if (num > 0) {
325                 int i;
326
327                 info("removing all_partitions '%s[1-%i]'", filename, num);
328                 if (num > 255) {
329                         info("garbage from udev database, skip all_partitions removal");
330                         return -1;
331                 }
332                 for (i = 1; i <= num; i++) {
333                         snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
334                         partitionname[sizeof(partitionname)-1] = '\0';
335                         unlink_secure(partitionname);
336                 }
337         }
338
339         if (strchr(udev->name, '/'))
340                 delete_path(filename);
341
342         return retval;
343 }