chiark / gitweb /
bf5feb1b2d61cc387f03db5c768db98692b17470
[elogind.git] / udev_add.c
1 /*
2  * udev-add.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7  *
8  *
9  *      This program is free software; you can redistribute it and/or modify it
10  *      under the terms of the GNU General Public License as published by the
11  *      Free Software Foundation version 2 of the License.
12  * 
13  *      This program is distributed in the hope that it will be useful, but
14  *      WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *      General Public License for more details.
17  * 
18  *      You should have received a copy of the GNU General Public License along
19  *      with this program; if not, write to the Free Software Foundation, Inc.,
20  *      675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <grp.h>
33 #include <net/if.h>
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
36 #include <linux/sockios.h>
37
38 #include "libsysfs/sysfs/libsysfs.h"
39 #include "udev_libc_wrapper.h"
40 #include "udev.h"
41 #include "udev_utils.h"
42 #include "udev_sysfs.h"
43 #include "udev_version.h"
44 #include "logging.h"
45 #include "namedev.h"
46 #include "udev_db.h"
47 #include "udev_selinux.h"
48
49
50 int udev_make_node(struct udevice *udev, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid)
51 {
52         struct stat stats;
53         int retval = 0;
54
55         if (stat(file, &stats) != 0)
56                 goto create;
57
58         /* preserve node with already correct numbers, to not change the inode number */
59         if (((stats.st_mode & S_IFMT) == S_IFBLK || (stats.st_mode & S_IFMT) == S_IFCHR) &&
60             (stats.st_rdev == devt)) {
61                 dbg("preserve file '%s', cause it has correct dev_t", file);
62                 selinux_setfilecon(file, udev->kernel_name, stats.st_mode);
63                 goto perms;
64         }
65
66         if (unlink(file) != 0)
67                 dbg("unlink(%s) failed with error '%s'", file, strerror(errno));
68         else
69                 dbg("already present file '%s' unlinked", file);
70
71 create:
72         switch (udev->type) {
73         case DEV_BLOCK:
74                 mode |= S_IFBLK;
75                 break;
76         case DEV_CLASS:
77                 mode |= S_IFCHR;
78                 break;
79         default:
80                 dbg("unknown node type %c\n", udev->type);
81                 return -EINVAL;
82         }
83
84         selinux_setfscreatecon(file, udev->kernel_name, mode);
85         retval = mknod(file, mode, devt);
86         selinux_resetfscreatecon();
87         if (retval != 0) {
88                 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
89                     file, mode, major(devt), minor(devt), strerror(errno));
90                 goto exit;
91         }
92
93 perms:
94         dbg("chmod(%s, %#o)", file, mode);
95         if (chmod(file, mode) != 0) {
96                 dbg("chmod(%s, %#o) failed with error '%s'", file, mode, strerror(errno));
97                 goto exit;
98         }
99
100         if (uid != 0 || gid != 0) {
101                 dbg("chown(%s, %u, %u)", file, uid, gid);
102                 if (chown(file, uid, gid) != 0) {
103                         dbg("chown(%s, %u, %u) failed with error '%s'",
104                             file, uid, gid, strerror(errno));
105                         goto exit;
106                 }
107         }
108
109 exit:
110         return retval;
111 }
112
113 static int create_node(struct udevice *udev, struct sysfs_class_device *class_dev)
114 {
115         char filename[PATH_SIZE];
116         char partitionname[PATH_SIZE];
117         struct name_entry *name_loop;
118         uid_t uid;
119         gid_t gid;
120         int tail;
121         int i;
122
123         snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
124         filename[sizeof(filename)-1] = '\0';
125
126         /* create parent directories if needed */
127         if (strchr(udev->name, '/'))
128                 create_path(filename);
129
130         if (strcmp(udev->owner, "root") == 0)
131                 uid = 0;
132         else {
133                 char *endptr;
134                 unsigned long id;
135
136                 id = strtoul(udev->owner, &endptr, 10);
137                 if (endptr[0] == '\0')
138                         uid = (uid_t) id;
139                 else
140                         uid = lookup_user(udev->owner);
141         }
142
143         if (strcmp(udev->group, "root") == 0)
144                 gid = 0;
145         else {
146                 char *endptr;
147                 unsigned long id;
148
149                 id = strtoul(udev->group, &endptr, 10);
150                 if (endptr[0] == '\0')
151                         gid = (gid_t) id;
152                 else
153                         gid = lookup_user(udev->group);
154         }
155
156         if (!udev->test_run) {
157                 info("creating device node '%s'", filename);
158                 if (udev_make_node(udev, filename, udev->devt, udev->mode, uid, gid) != 0)
159                         goto error;
160         } else {
161                 info("creating device node '%s', major = '%d', minor = '%d', "
162                      "mode = '%#o', uid = '%d', gid = '%d'", filename,
163                      major(udev->devt), minor(udev->devt), udev->mode, uid, gid);
164         }
165
166         /* create all_partitions if requested */
167         if (udev->partitions) {
168                 struct sysfs_attribute *attr;
169                 int range;
170
171                 /* take the maximum registered minor range */
172                 attr = sysfs_get_classdev_attr(class_dev, "range");
173                 if (attr) {
174                         range = atoi(attr->value);
175                         if (range > 1)
176                                 udev->partitions = range-1;
177                 }
178                 info("creating device partition nodes '%s[1-%i]'", filename, udev->partitions);
179                 if (!udev->test_run) {
180                         for (i = 1; i <= udev->partitions; i++) {
181                                 dev_t part_devt;
182
183                                 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
184                                 partitionname[sizeof(partitionname)-1] = '\0';
185                                 part_devt = makedev(major(udev->devt), minor(udev->devt)+1);
186                                 udev_make_node(udev, partitionname, part_devt, udev->mode, uid, gid);
187                         }
188                 }
189         }
190
191         /* create symlink(s) if requested */
192         list_for_each_entry(name_loop, &udev->symlink_list, node) {
193                 int retval;
194                 char linktarget[PATH_SIZE];
195
196                 snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
197                 filename[sizeof(filename)-1] = '\0';
198
199                 dbg("symlink '%s' to node '%s' requested", filename, udev->name);
200                 if (!udev->test_run)
201                         if (strchr(filename, '/'))
202                                 create_path(filename);
203
204                 /* optimize relative link */
205                 linktarget[0] = '\0';
206                 i = 0;
207                 tail = 0;
208                 while (udev->name[i] && (udev->name[i] == name_loop->name[i])) {
209                         if (udev->name[i] == '/')
210                                 tail = i+1;
211                         i++;
212                 }
213                 while (name_loop->name[i] != '\0') {
214                         if (name_loop->name[i] == '/')
215                                 strlcat(linktarget, "../", sizeof(linktarget));
216                         i++;
217                 }
218
219                 strlcat(linktarget, &udev->name[tail], sizeof(linktarget));
220
221                 dbg("symlink(%s, %s)", linktarget, filename);
222                 if (!udev->test_run) {
223                         unlink(filename);
224                         selinux_setfscreatecon(filename, udev->kernel_name, S_IFLNK);
225                         retval = symlink(linktarget, filename);
226                         selinux_resetfscreatecon();
227                         if (retval != 0)
228                                 dbg("symlink(%s, %s) failed with error '%s'",
229                                     linktarget, filename, strerror(errno));
230                 }
231         }
232
233         return 0;
234 error:
235         return -1;
236 }
237
238 static int rename_net_if(struct udevice *udev)
239 {
240         int sk;
241         struct ifreq ifr;
242         int retval;
243
244         dbg("changing net interface name from '%s' to '%s'", udev->kernel_name, udev->name);
245         if (udev->test_run)
246                 return 0;
247
248         sk = socket(PF_INET, SOCK_DGRAM, 0);
249         if (sk < 0) {
250                 dbg("error opening socket");
251                 return -1;
252         }
253
254         memset(&ifr, 0x00, sizeof(struct ifreq));
255         strlcpy(ifr.ifr_name, udev->kernel_name, IFNAMSIZ);
256         strlcpy(ifr.ifr_newname, udev->name, IFNAMSIZ);
257
258         retval = ioctl(sk, SIOCSIFNAME, &ifr);
259         if (retval != 0)
260                 dbg("error changing net interface name");
261         close(sk);
262
263         return retval;
264 }
265
266 int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev)
267 {
268         char *pos;
269         int retval = 0;
270
271         if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS) {
272                 udev->devt = get_devt(class_dev);
273                 if (!udev->devt) {
274                         dbg("no dev-file found, do nothing");
275                         return 0;
276                 }
277         }
278
279         if (namedev_name_device(udev, class_dev) != 0)
280                 return 0;
281
282         dbg("adding name='%s'", udev->name);
283
284         selinux_init();
285
286         if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS) {
287                 retval = create_node(udev, class_dev);
288                 if (retval != 0)
289                         goto exit;
290
291                 if (udev_db_add_device(udev) != 0)
292                         dbg("udev_db_add_dev failed, but we create the node anyway, "
293                             "remove might not work for custom names");
294
295                 /* use full path to the environment */
296                 snprintf(udev->devname, sizeof(udev->devname), "%s/%s", udev_root, udev->name);
297                 udev->devname[sizeof(udev->devname)-1] = '\0';
298
299         } else if (udev->type == DEV_NET) {
300                 /* look if we want to change the name of the netif */
301                 if (strcmp(udev->name, udev->kernel_name) != 0) {
302                         retval = rename_net_if(udev);
303                         if (retval != 0)
304                                 goto exit;
305
306                         /* we've changed the name, now fake the devpath, cause the
307                          * original kernel name sleeps with the fishes and we don't
308                          * get an event from the kernel with the new name
309                          */
310                         pos = strrchr(udev->devpath, '/');
311                         if (pos != NULL) {
312                                 pos[1] = '\0';
313                                 strlcat(udev->devpath, udev->name, sizeof(udev->devpath));
314                                 setenv("DEVPATH", udev->devpath, 1);
315                                 setenv("INTERFACE", udev->name, 1);
316                         }
317
318                         /* use netif name for the environment */
319                         strlcpy(udev->devname, udev->name, sizeof(udev->devname));
320                 }
321         }
322
323 exit:
324         selinux_exit();
325
326         return retval;
327 }