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