]>
git.ipfire.org Git - thirdparty/systemd.git/blob - udev_add.c
6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
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.
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.
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.
31 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
36 #include <linux/sockios.h>
39 #include "libsysfs/sysfs/libsysfs.h"
42 #include "udev_version.h"
50 * the major/minor of a device is stored in a file called "dev"
51 * The number is stored in decimal values in the format: M:m
53 static int get_major_minor(struct sysfs_class_device
*class_dev
, struct udevice
*udev
)
55 struct sysfs_attribute
*attr
= NULL
;
57 attr
= sysfs_get_classdev_attr(class_dev
, "dev");
60 dbg("dev='%s'", attr
->value
);
62 if (sscanf(attr
->value
, "%u:%u", &udev
->major
, &udev
->minor
) != 2)
64 dbg("found major=%d, minor=%d", udev
->major
, udev
->minor
);
71 static int create_path(char *file
)
79 pos
= strchr(p
+1, '/');
81 pos
= strchr(pos
+1, '/');
85 if (stat(p
, &stats
)) {
86 selinux_setfscreatecon(p
, S_IFDIR
);
87 retval
= mkdir(p
, 0755);
89 dbg("mkdir(%s) failed with error '%s'",
93 dbg("created '%s'", p
);
95 selinux_setfilecon(p
, S_IFDIR
);
102 static int make_node(char *file
, int major
, int minor
, unsigned int mode
, uid_t uid
, gid_t gid
)
107 if (stat(file
, &stats
) != 0)
110 /* preserve node with already correct numbers, to not change the inode number */
111 if (((stats
.st_mode
& S_IFMT
) == S_IFBLK
|| (stats
.st_mode
& S_IFMT
) == S_IFCHR
) &&
112 (stats
.st_rdev
== makedev(major
, minor
))) {
113 dbg("preserve file '%s', cause it has correct dev_t", file
);
114 selinux_setfilecon(file
,stats
.st_mode
);
118 if (unlink(file
) != 0)
119 dbg("unlink(%s) failed with error '%s'", file
, strerror(errno
));
121 dbg("already present file '%s' unlinked", file
);
124 selinux_setfscreatecon(file
, mode
);
125 retval
= mknod(file
, mode
, makedev(major
, minor
));
127 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
128 file
, mode
, major
, minor
, strerror(errno
));
133 dbg("chmod(%s, %#o)", file
, mode
);
134 if (chmod(file
, mode
) != 0) {
135 dbg("chmod(%s, %#o) failed with error '%s'", file
, mode
, strerror(errno
));
139 if (uid
!= 0 || gid
!= 0) {
140 dbg("chown(%s, %u, %u)", file
, uid
, gid
);
141 if (chown(file
, uid
, gid
) != 0) {
142 dbg("chown(%s, %u, %u) failed with error '%s'",
143 file
, uid
, gid
, strerror(errno
));
152 static int create_node(struct udevice
*udev
)
154 char filename
[NAME_SIZE
];
155 char linkname
[NAME_SIZE
];
156 char linktarget
[NAME_SIZE
];
157 char partitionname
[NAME_SIZE
];
165 strfieldcpy(filename
, udev_root
);
166 strfieldcat(filename
, udev
->name
);
168 switch (udev
->type
) {
170 udev
->mode
|= S_IFBLK
;
174 udev
->mode
|= S_IFCHR
;
177 udev
->mode
|= S_IFIFO
;
180 dbg("unknown node type %c\n", udev
->type
);
184 /* create parent directories if needed */
185 if (strrchr(udev
->name
, '/'))
186 create_path(filename
);
188 if (udev
->owner
[0] != '\0') {
190 unsigned long id
= strtoul(udev
->owner
, &endptr
, 10);
191 if (endptr
[0] == '\0')
196 pw
= getpwnam(udev
->owner
);
198 dbg("specified user unknown '%s'", udev
->owner
);
204 if (udev
->group
[0] != '\0') {
206 unsigned long id
= strtoul(udev
->group
, &endptr
, 10);
207 if (endptr
[0] == '\0')
210 struct group
*gr
= getgrnam(udev
->group
);
212 dbg("specified group unknown '%s'", udev
->group
);
218 if (!udev
->test_run
) {
219 info("creating device node '%s'", filename
);
220 if (make_node(filename
, udev
->major
, udev
->minor
, udev
->mode
, uid
, gid
) != 0)
223 info("creating device node '%s', major = '%d', minor = '%d', "
224 "mode = '%#o', uid = '%d', gid = '%d'", filename
,
225 udev
->major
, udev
->minor
, (mode_t
)udev
->mode
, uid
, gid
);
228 /* create all_partitions if requested */
229 if (udev
->partitions
> 0) {
230 info("creating device partition nodes '%s[1-%i]'", filename
, udev
->partitions
);
231 if (!udev
->test_run
) {
232 for (i
= 1; i
<= udev
->partitions
; i
++) {
233 strfieldcpy(partitionname
, filename
);
234 strintcat(partitionname
, i
);
235 make_node(partitionname
, udev
->major
, udev
->minor
+ i
, udev
->mode
, uid
, gid
);
240 /* create symlink(s) if requested */
241 foreach_strpart(udev
->symlink
, " ", pos
, len
) {
242 strfieldcpymax(linkname
, pos
, len
+1);
243 strfieldcpy(filename
, udev_root
);
244 strfieldcat(filename
, linkname
);
245 dbg("symlink '%s' to node '%s' requested", filename
, udev
->name
);
247 if (strrchr(linkname
, '/'))
248 create_path(filename
);
250 /* optimize relative link */
251 linktarget
[0] = '\0';
254 while ((udev
->name
[i
] == linkname
[i
]) && udev
->name
[i
]) {
255 if (udev
->name
[i
] == '/')
259 while (linkname
[i
] != '\0') {
260 if (linkname
[i
] == '/')
261 strfieldcat(linktarget
, "../");
265 strfieldcat(linktarget
, &udev
->name
[tail
]);
267 dbg("symlink(%s, %s)", linktarget
, filename
);
268 if (!udev
->test_run
) {
269 selinux_setfscreatecon(filename
, S_IFLNK
);
271 if (symlink(linktarget
, filename
) != 0)
272 dbg("symlink(%s, %s) failed with error '%s'",
273 linktarget
, filename
, strerror(errno
));
282 static int rename_net_if(struct udevice
*udev
)
288 dbg("changing net interface name from '%s' to '%s'", udev
->kernel_name
, udev
->name
);
292 sk
= socket(PF_INET
, SOCK_DGRAM
, 0);
294 dbg("error opening socket");
298 memset(&ifr
, 0x00, sizeof(struct ifreq
));
299 strfieldcpy(ifr
.ifr_name
, udev
->kernel_name
);
300 strfieldcpy(ifr
.ifr_newname
, udev
->name
);
302 retval
= ioctl(sk
, SIOCSIFNAME
, &ifr
);
304 dbg("error changing net interface name");
310 int udev_add_device(struct udevice
*udev
, struct sysfs_class_device
*class_dev
)
315 if (udev
->type
== 'b' || udev
->type
== 'c') {
316 retval
= get_major_minor(class_dev
, udev
);
318 dbg("no dev-file found, do nothing");
323 if (namedev_name_device(udev
, class_dev
) != 0)
326 dbg("adding name='%s'", udev
->name
);
330 if (udev
->type
== 'b' || udev
->type
== 'c') {
331 retval
= create_node(udev
);
335 if (udevdb_add_dev(udev
) != 0)
336 dbg("udevdb_add_dev failed, but we create the node anyway, "
337 "remove might not work for custom names");
339 /* use full path to the environment */
340 snprintf(udev
->devname
, NAME_SIZE
-1, "%s%s", udev_root
, udev
->name
);
342 } else if (udev
->type
== 'n') {
343 /* look if we want to change the name of the netif */
344 if (strcmp(udev
->name
, udev
->kernel_name
) != 0) {
345 retval
= rename_net_if(udev
);
349 /* we've changed the name, now fake the devpath,
350 * cause original kernel name sleeps with the fishes
351 * and we don't get any event from the kernel now
353 pos
= strrchr(udev
->devpath
, '/');
356 strfieldcat(udev
->devpath
, udev
->name
);
357 setenv("DEVPATH", udev
->devpath
, 1);
360 /* use netif name for the environment */
361 strfieldcpy(udev
->devname
, udev
->name
);