]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev_add.c
clean-up empty queue directories
[thirdparty/systemd.git] / udev_add.c
CommitLineData
ea733a2f
GKH
1/*
2 * udev-add.c
3 *
ea733a2f 4 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
e8d569b4 5 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
ea733a2f
GKH
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 <fcntl.h>
26#include <unistd.h>
27#include <errno.h>
32ff5bca 28#include <sys/stat.h>
10950dfe
GKH
29#include <sys/types.h>
30#include <grp.h>
f61d732a
KS
31#include <net/if.h>
32#include <sys/socket.h>
33#include <sys/ioctl.h>
34#include <linux/sockios.h>
ea733a2f 35
c80da508 36#include "libsysfs/sysfs/libsysfs.h"
57e1a277 37#include "udev_libc_wrapper.h"
ea733a2f 38#include "udev.h"
9af5bb2f 39#include "udev_utils.h"
ea733a2f 40#include "udev_version.h"
54988802 41#include "logging.h"
e5e322bc 42#include "udev_rules.h"
fbda4a34 43#include "udev_selinux.h"
9825617b 44
ea733a2f 45
7e720bd4 46int udev_make_node(struct udevice *udev, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid)
50e5de03 47{
49747bdc
KS
48 struct stat stats;
49 int retval = 0;
50
57663b36
KS
51 switch (udev->type) {
52 case DEV_BLOCK:
53 mode |= S_IFBLK;
54 break;
55 case DEV_CLASS:
56 mode |= S_IFCHR;
57 break;
58 default:
59 dbg("unknown node type %c\n", udev->type);
60 return -EINVAL;
61 }
62
49747bdc
KS
63 if (stat(file, &stats) != 0)
64 goto create;
65
66 /* preserve node with already correct numbers, to not change the inode number */
57663b36 67 if ((stats.st_mode & S_IFMT) == (mode & S_IFMT) && (stats.st_rdev == devt)) {
6b493a20 68 info("preserve file '%s', cause it has correct dev_t", file);
fbda4a34 69 selinux_setfilecon(file, udev->kernel_name, stats.st_mode);
49747bdc
KS
70 goto perms;
71 }
72
73 if (unlink(file) != 0)
df4e89bf 74 err("unlink(%s) failed: %s", file, strerror(errno));
49747bdc
KS
75 else
76 dbg("already present file '%s' unlinked", file);
50e5de03 77
49747bdc 78create:
fbda4a34 79 selinux_setfscreatecon(file, udev->kernel_name, mode);
7e720bd4 80 retval = mknod(file, mode, devt);
4d772639 81 selinux_resetfscreatecon();
50e5de03 82 if (retval != 0) {
ff3e4bed 83 err("mknod(%s, %#o, %u, %u) failed: %s",
7e720bd4 84 file, mode, major(devt), minor(devt), strerror(errno));
bbbe503e 85 goto exit;
50e5de03
KS
86 }
87
49747bdc
KS
88perms:
89 dbg("chmod(%s, %#o)", file, mode);
90 if (chmod(file, mode) != 0) {
df4e89bf 91 err("chmod(%s, %#o) failed: %s", file, mode, strerror(errno));
bbbe503e 92 goto exit;
50e5de03
KS
93 }
94
95 if (uid != 0 || gid != 0) {
49747bdc
KS
96 dbg("chown(%s, %u, %u)", file, uid, gid);
97 if (chown(file, uid, gid) != 0) {
df4e89bf 98 err("chown(%s, %u, %u) failed: %s",
49747bdc 99 file, uid, gid, strerror(errno));
bbbe503e 100 goto exit;
50e5de03
KS
101 }
102 }
103
bbbe503e
KS
104exit:
105 return retval;
50e5de03
KS
106}
107
6d564166 108static int create_node(struct udevice *udev, struct sysfs_class_device *class_dev)
97853b4f 109{
63f61c5c
KS
110 char filename[PATH_SIZE];
111 char partitionname[PATH_SIZE];
e48fc108 112 struct name_entry *name_loop;
783272f0
KS
113 uid_t uid;
114 gid_t gid;
3d150dfb 115 int tail;
e48fc108 116 int i;
3d150dfb 117
63f61c5c
KS
118 snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
119 filename[sizeof(filename)-1] = '\0';
5840bc63 120
3d150dfb 121 /* create parent directories if needed */
6a24dc74 122 if (strchr(udev->name, '/'))
3d150dfb 123 create_path(filename);
218eae87 124
783272f0
KS
125 if (strcmp(udev->owner, "root") == 0)
126 uid = 0;
127 else {
c19a6b30 128 char *endptr;
783272f0 129 unsigned long id;
6d564166 130
783272f0 131 id = strtoul(udev->owner, &endptr, 10);
765cbd97 132 if (endptr[0] == '\0')
c19a6b30 133 uid = (uid_t) id;
57e1a277
KS
134 else
135 uid = lookup_user(udev->owner);
c19a6b30
KS
136 }
137
783272f0
KS
138 if (strcmp(udev->group, "root") == 0)
139 gid = 0;
140 else {
c19a6b30 141 char *endptr;
783272f0 142 unsigned long id;
6d564166 143
783272f0 144 id = strtoul(udev->group, &endptr, 10);
765cbd97 145 if (endptr[0] == '\0')
c19a6b30 146 gid = (gid_t) id;
57e1a277 147 else
68c2c0b5 148 gid = lookup_group(udev->group);
c19a6b30
KS
149 }
150
7a947ce5 151 if (!udev->test_run) {
50e5de03 152 info("creating device node '%s'", filename);
7e720bd4 153 if (udev_make_node(udev, filename, udev->devt, udev->mode, uid, gid) != 0)
bbbe503e 154 goto error;
ab2e5bd9
GKH
155 } else {
156 info("creating device node '%s', major = '%d', minor = '%d', "
157 "mode = '%#o', uid = '%d', gid = '%d'", filename,
7e720bd4 158 major(udev->devt), minor(udev->devt), udev->mode, uid, gid);
ab2e5bd9 159 }
50e5de03 160
bbbe503e 161 /* create all_partitions if requested */
fd9efc00 162 if (udev->partitions) {
6d564166
KS
163 struct sysfs_attribute *attr;
164 int range;
165
166 /* take the maximum registered minor range */
167 attr = sysfs_get_classdev_attr(class_dev, "range");
168 if (attr) {
169 range = atoi(attr->value);
170 if (range > 1)
171 udev->partitions = range-1;
172 }
7a947ce5
KS
173 info("creating device partition nodes '%s[1-%i]'", filename, udev->partitions);
174 if (!udev->test_run) {
175 for (i = 1; i <= udev->partitions; i++) {
7e720bd4
KS
176 dev_t part_devt;
177
63f61c5c
KS
178 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
179 partitionname[sizeof(partitionname)-1] = '\0';
15139b8a 180 part_devt = makedev(major(udev->devt), minor(udev->devt) + i);
7e720bd4 181 udev_make_node(udev, partitionname, part_devt, udev->mode, uid, gid);
ab2e5bd9 182 }
50e5de03 183 }
3d150dfb
KS
184 }
185
bbbe503e 186 /* create symlink(s) if requested */
e48fc108 187 list_for_each_entry(name_loop, &udev->symlink_list, node) {
4d772639 188 int retval;
63f61c5c 189 char linktarget[PATH_SIZE];
2b41e68a 190
63f61c5c
KS
191 snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
192 filename[sizeof(filename)-1] = '\0';
2b41e68a 193
7a947ce5
KS
194 dbg("symlink '%s' to node '%s' requested", filename, udev->name);
195 if (!udev->test_run)
e48fc108 196 if (strchr(filename, '/'))
9fe3f9a9
KS
197 create_path(filename);
198
199 /* optimize relative link */
200 linktarget[0] = '\0';
201 i = 0;
202 tail = 0;
e48fc108 203 while (udev->name[i] && (udev->name[i] == name_loop->name[i])) {
7a947ce5 204 if (udev->name[i] == '/')
9fe3f9a9
KS
205 tail = i+1;
206 i++;
207 }
e48fc108
KS
208 while (name_loop->name[i] != '\0') {
209 if (name_loop->name[i] == '/')
63f61c5c 210 strlcat(linktarget, "../", sizeof(linktarget));
9fe3f9a9
KS
211 i++;
212 }
3d150dfb 213
63f61c5c 214 strlcat(linktarget, &udev->name[tail], sizeof(linktarget));
3d150dfb 215
40caaeec 216 info("creating symlink '%s' to '%s'", filename, linktarget);
7a947ce5 217 if (!udev->test_run) {
7eb92135 218 unlink(filename);
024780c2 219 selinux_setfscreatecon(filename, NULL, S_IFLNK);
4d772639
GKH
220 retval = symlink(linktarget, filename);
221 selinux_resetfscreatecon();
222 if (retval != 0)
df4e89bf 223 err("symlink(%s, %s) failed: %s",
9fe3f9a9 224 linktarget, filename, strerror(errno));
4763256c 225 }
c19a6b30
KS
226 }
227
bbbe503e
KS
228 return 0;
229error:
230 return -1;
ea733a2f
GKH
231}
232
7a947ce5 233static int rename_net_if(struct udevice *udev)
f61d732a
KS
234{
235 int sk;
236 struct ifreq ifr;
237 int retval;
238
6b493a20 239 info("changing net interface name from '%s' to '%s'", udev->kernel_name, udev->name);
7a947ce5 240 if (udev->test_run)
bbbe503e
KS
241 return 0;
242
f61d732a
KS
243 sk = socket(PF_INET, SOCK_DGRAM, 0);
244 if (sk < 0) {
df4e89bf 245 err("error opening socket: %s", strerror(errno));
f61d732a
KS
246 return -1;
247 }
248
249 memset(&ifr, 0x00, sizeof(struct ifreq));
63f61c5c
KS
250 strlcpy(ifr.ifr_name, udev->kernel_name, IFNAMSIZ);
251 strlcpy(ifr.ifr_newname, udev->name, IFNAMSIZ);
f61d732a 252
f61d732a
KS
253 retval = ioctl(sk, SIOCSIFNAME, &ifr);
254 if (retval != 0)
df4e89bf 255 err("error changing net interface name: %s", strerror(errno));
4a539daf 256 close(sk);
f61d732a
KS
257
258 return retval;
259}
260
7a947ce5 261int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev)
ea733a2f 262{
bbbe503e 263 char *pos;
707680b1 264 int retval = 0;
ea733a2f 265
7a947ce5 266 dbg("adding name='%s'", udev->name);
9825617b 267 selinux_init();
5d24c6ca 268
e6764498 269 if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS) {
6d564166 270 retval = create_node(udev, class_dev);
c4603a07 271 if (retval != 0)
bbbe503e 272 goto exit;
9b28a52a 273
02fa9ae5
KS
274 if (udev_db_add_device(udev) != 0)
275 dbg("udev_db_add_dev failed, but we create the node anyway, "
5d24c6ca 276 "remove might not work for custom names");
f61d732a 277
5d24c6ca 278 /* use full path to the environment */
63f61c5c
KS
279 snprintf(udev->devname, sizeof(udev->devname), "%s/%s", udev_root, udev->name);
280 udev->devname[sizeof(udev->devname)-1] = '\0';
5d24c6ca 281
e6764498 282 } else if (udev->type == DEV_NET) {
5d24c6ca 283 /* look if we want to change the name of the netif */
7a947ce5
KS
284 if (strcmp(udev->name, udev->kernel_name) != 0) {
285 retval = rename_net_if(udev);
c4603a07 286 if (retval != 0)
bbbe503e 287 goto exit;
5d24c6ca 288
c4456381 289 info("renamed netif to '%s'", udev->name);
eabfc973
KS
290 /* we've changed the name, now fake the devpath, cause the
291 * original kernel name sleeps with the fishes and we don't
292 * get an event from the kernel with the new name
bbbe503e 293 */
7a947ce5 294 pos = strrchr(udev->devpath, '/');
bbbe503e
KS
295 if (pos != NULL) {
296 pos[1] = '\0';
63f61c5c 297 strlcat(udev->devpath, udev->name, sizeof(udev->devpath));
c4456381 298 strlcpy(udev->kernel_name, udev->name, sizeof(udev->kernel_name));
5d24c6ca 299 setenv("DEVPATH", udev->devpath, 1);
eabfc973 300 setenv("INTERFACE", udev->name, 1);
bbbe503e 301 }
9b28a52a 302
5d24c6ca 303 /* use netif name for the environment */
63f61c5c 304 strlcpy(udev->devname, udev->name, sizeof(udev->devname));
5d24c6ca 305 }
f61d732a 306 }
ea733a2f
GKH
307
308exit:
4d772639 309 selinux_exit();
ea733a2f
GKH
310 return retval;
311}