chiark / gitweb /
remove old error message
[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         return retval;
241 }
242
243 void udev_node_remove_symlinks(struct udevice *udev)
244 {
245         char filename[PATH_SIZE];
246         struct name_entry *name_loop;
247         struct stat stats;
248
249         if (!list_empty(&udev->symlink_list)) {
250                 char symlinks[PATH_SIZE] = "";
251
252                 list_for_each_entry(name_loop, &udev->symlink_list, node) {
253                         char devpath[PATH_SIZE];
254
255                         snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
256                         filename[sizeof(filename)-1] = '\0';
257
258                         if (stat(filename, &stats) != 0) {
259                                 dbg("symlink '%s' not found", filename);
260                                 continue;
261                         }
262                         if (udev->devt && stats.st_rdev != udev->devt) {
263                                 info("symlink '%s' points to a different device, skip removal", filename);
264                                 continue;
265                         }
266
267                         info("removing symlink '%s'", filename);
268                         if (!udev->test_run) {
269                                 unlink(filename);
270                                 delete_path(filename);
271                         }
272
273                         /* see if another device wants this symlink */
274                         if (udev_db_lookup_name(name_loop->name, devpath, sizeof(devpath)) == 0) {
275                                 struct udevice *old;
276
277                                 info("found overwritten symlink '%s' of '%s'", name_loop->name, devpath);
278                                 old = udev_device_init();
279                                 if (old != NULL) {
280                                         if (udev_db_get_device(old, devpath) == 0) {
281                                                 char slink[PATH_SIZE];
282                                                 char node[PATH_SIZE];
283
284                                                 strlcpy(slink, udev_root, sizeof(slink));
285                                                 strlcat(slink, "/", sizeof(slink));
286                                                 strlcat(slink, name_loop->name, sizeof(slink));
287                                                 strlcpy(node, udev_root, sizeof(node));
288                                                 strlcat(node, "/", sizeof(node));
289                                                 strlcat(node, old->name, sizeof(node));
290                                                 info("restore symlink '%s' to '%s'", slink, node);
291                                                 if (!udev->test_run)
292                                                         node_symlink(node, slink);
293                                         }
294                                         udev_device_cleanup(old);
295                                 }
296                         }
297
298                         strlcat(symlinks, filename, sizeof(symlinks));
299                         strlcat(symlinks, " ", sizeof(symlinks));
300                 }
301
302                 remove_trailing_chars(symlinks, ' ');
303                 if (symlinks[0] != '\0')
304                         setenv("DEVLINKS", symlinks, 1);
305         }
306 }
307
308 int udev_node_remove(struct udevice *udev)
309 {
310         char filename[PATH_SIZE];
311         char partitionname[PATH_SIZE];
312         struct stat stats;
313         int retval;
314         int num;
315
316         udev_node_remove_symlinks(udev);
317
318         snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
319         filename[sizeof(filename)-1] = '\0';
320         if (stat(filename, &stats) != 0) {
321                 dbg("device node '%s' not found", filename);
322                 return -1;
323         }
324         if (udev->devt && stats.st_rdev != udev->devt) {
325                 info("device node '%s' points to a different device, skip removal", filename);
326                 return -1;
327         }
328
329         info("removing device node '%s'", filename);
330         retval = unlink_secure(filename);
331         if (retval)
332                 return retval;
333
334         setenv("DEVNAME", filename, 1);
335         num = udev->partitions;
336         if (num > 0) {
337                 int i;
338
339                 info("removing all_partitions '%s[1-%i]'", filename, num);
340                 if (num > 255)
341                         return -1;
342                 for (i = 1; i <= num; i++) {
343                         snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
344                         partitionname[sizeof(partitionname)-1] = '\0';
345                         unlink_secure(partitionname);
346                 }
347         }
348         delete_path(filename);
349         return retval;
350 }