]>
Commit | Line | Data |
---|---|---|
ea733a2f GKH |
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> | |
32ff5bca | 30 | #include <sys/stat.h> |
10950dfe GKH |
31 | #include <sys/types.h> |
32 | #include <grp.h> | |
f61d732a KS |
33 | #include <net/if.h> |
34 | #include <sys/socket.h> | |
35 | #include <sys/ioctl.h> | |
36 | #include <linux/sockios.h> | |
10950dfe | 37 | #include <pwd.h> |
ea733a2f | 38 | |
c80da508 | 39 | #include "libsysfs/sysfs/libsysfs.h" |
ea733a2f | 40 | #include "udev.h" |
c81b35c0 | 41 | #include "udev_lib.h" |
ea733a2f | 42 | #include "udev_version.h" |
54988802 | 43 | #include "logging.h" |
ea733a2f | 44 | #include "namedev.h" |
8e41d35d | 45 | #include "udevdb.h" |
ea733a2f | 46 | |
9825617b HH |
47 | #include "selinux.h" |
48 | ||
707680b1 KS |
49 | /* |
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 | |
ea733a2f | 52 | */ |
30defadd | 53 | static int get_major_minor(struct sysfs_class_device *class_dev, struct udevice *udev) |
ea733a2f | 54 | { |
5d4754f1 | 55 | struct sysfs_attribute *attr = NULL; |
ea733a2f | 56 | |
5d4754f1 DS |
57 | attr = sysfs_get_classdev_attr(class_dev, "dev"); |
58 | if (attr == NULL) | |
bbbe503e | 59 | goto error; |
5d4754f1 | 60 | dbg("dev='%s'", attr->value); |
ea733a2f | 61 | |
5d4754f1 | 62 | if (sscanf(attr->value, "%u:%u", &udev->major, &udev->minor) != 2) |
bbbe503e | 63 | goto error; |
f7b4eca4 | 64 | dbg("found major=%d, minor=%d", udev->major, udev->minor); |
ea733a2f | 65 | |
bbbe503e KS |
66 | return 0; |
67 | error: | |
68 | return -1; | |
ea733a2f GKH |
69 | } |
70 | ||
3d150dfb KS |
71 | static int create_path(char *file) |
72 | { | |
73 | char p[NAME_SIZE]; | |
74 | char *pos; | |
75 | int retval; | |
76 | struct stat stats; | |
77 | ||
c472e3c8 | 78 | strfieldcpy(p, file); |
3d150dfb KS |
79 | pos = strchr(p+1, '/'); |
80 | while (1) { | |
81 | pos = strchr(pos+1, '/'); | |
82 | if (pos == NULL) | |
83 | break; | |
84 | *pos = 0x00; | |
85 | if (stat(p, &stats)) { | |
9825617b | 86 | selinux_setfscreatecon(p, S_IFDIR); |
3d150dfb | 87 | retval = mkdir(p, 0755); |
c5118442 | 88 | if (retval != 0) { |
3d150dfb KS |
89 | dbg("mkdir(%s) failed with error '%s'", |
90 | p, strerror(errno)); | |
91 | return retval; | |
92 | } | |
93 | dbg("created '%s'", p); | |
9825617b HH |
94 | } else { |
95 | selinux_setfilecon(p, S_IFDIR); | |
3d150dfb KS |
96 | } |
97 | *pos = '/'; | |
98 | } | |
99 | return 0; | |
100 | } | |
101 | ||
49747bdc | 102 | static int make_node(char *file, int major, int minor, unsigned int mode, uid_t uid, gid_t gid) |
50e5de03 | 103 | { |
49747bdc KS |
104 | struct stat stats; |
105 | int retval = 0; | |
106 | ||
107 | if (stat(file, &stats) != 0) | |
108 | goto create; | |
109 | ||
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); | |
9825617b | 114 | selinux_setfilecon(file,stats.st_mode); |
49747bdc KS |
115 | goto perms; |
116 | } | |
117 | ||
118 | if (unlink(file) != 0) | |
119 | dbg("unlink(%s) failed with error '%s'", file, strerror(errno)); | |
120 | else | |
121 | dbg("already present file '%s' unlinked", file); | |
50e5de03 | 122 | |
49747bdc | 123 | create: |
9825617b | 124 | selinux_setfscreatecon(file, mode); |
49747bdc | 125 | retval = mknod(file, mode, makedev(major, minor)); |
50e5de03 KS |
126 | if (retval != 0) { |
127 | dbg("mknod(%s, %#o, %u, %u) failed with error '%s'", | |
49747bdc | 128 | file, mode, major, minor, strerror(errno)); |
bbbe503e | 129 | goto exit; |
50e5de03 KS |
130 | } |
131 | ||
49747bdc KS |
132 | perms: |
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)); | |
bbbe503e | 136 | goto exit; |
50e5de03 KS |
137 | } |
138 | ||
139 | if (uid != 0 || gid != 0) { | |
49747bdc KS |
140 | dbg("chown(%s, %u, %u)", file, uid, gid); |
141 | if (chown(file, uid, gid) != 0) { | |
50e5de03 | 142 | dbg("chown(%s, %u, %u) failed with error '%s'", |
49747bdc | 143 | file, uid, gid, strerror(errno)); |
bbbe503e | 144 | goto exit; |
50e5de03 KS |
145 | } |
146 | } | |
147 | ||
bbbe503e KS |
148 | exit: |
149 | return retval; | |
50e5de03 KS |
150 | } |
151 | ||
7a947ce5 | 152 | static int create_node(struct udevice *udev) |
97853b4f | 153 | { |
9fe3f9a9 KS |
154 | char filename[NAME_SIZE]; |
155 | char linkname[NAME_SIZE]; | |
156 | char linktarget[NAME_SIZE]; | |
157 | char partitionname[NAME_SIZE]; | |
e308ebb7 GKH |
158 | uid_t uid = 0; |
159 | gid_t gid = 0; | |
3d150dfb KS |
160 | int i; |
161 | int tail; | |
ef672b3d KS |
162 | char *pos; |
163 | int len; | |
3d150dfb | 164 | |
c472e3c8 | 165 | strfieldcpy(filename, udev_root); |
7a947ce5 | 166 | strfieldcat(filename, udev->name); |
5840bc63 | 167 | |
7a947ce5 | 168 | switch (udev->type) { |
1331c889 | 169 | case 'b': |
7a947ce5 | 170 | udev->mode |= S_IFBLK; |
1331c889 GKH |
171 | break; |
172 | case 'c': | |
173 | case 'u': | |
7a947ce5 | 174 | udev->mode |= S_IFCHR; |
1331c889 GKH |
175 | break; |
176 | case 'p': | |
7a947ce5 | 177 | udev->mode |= S_IFIFO; |
1331c889 GKH |
178 | break; |
179 | default: | |
7a947ce5 | 180 | dbg("unknown node type %c\n", udev->type); |
1331c889 GKH |
181 | return -EINVAL; |
182 | } | |
0abf54fc | 183 | |
3d150dfb | 184 | /* create parent directories if needed */ |
7a947ce5 | 185 | if (strrchr(udev->name, '/')) |
3d150dfb | 186 | create_path(filename); |
218eae87 | 187 | |
7a947ce5 | 188 | if (udev->owner[0] != '\0') { |
c19a6b30 | 189 | char *endptr; |
7a947ce5 | 190 | unsigned long id = strtoul(udev->owner, &endptr, 10); |
765cbd97 | 191 | if (endptr[0] == '\0') |
c19a6b30 | 192 | uid = (uid_t) id; |
10950dfe | 193 | else { |
5895eb31 | 194 | struct passwd *pw; |
534c853d | 195 | |
7a947ce5 | 196 | pw = getpwnam(udev->owner); |
765cbd97 | 197 | if (pw == NULL) |
7a947ce5 | 198 | dbg("specified user unknown '%s'", udev->owner); |
10950dfe GKH |
199 | else |
200 | uid = pw->pw_uid; | |
201 | } | |
c19a6b30 KS |
202 | } |
203 | ||
7a947ce5 | 204 | if (udev->group[0] != '\0') { |
c19a6b30 | 205 | char *endptr; |
7a947ce5 | 206 | unsigned long id = strtoul(udev->group, &endptr, 10); |
765cbd97 | 207 | if (endptr[0] == '\0') |
c19a6b30 | 208 | gid = (gid_t) id; |
10950dfe | 209 | else { |
7a947ce5 | 210 | struct group *gr = getgrnam(udev->group); |
765cbd97 | 211 | if (gr == NULL) |
7a947ce5 | 212 | dbg("specified group unknown '%s'", udev->group); |
10950dfe GKH |
213 | else |
214 | gid = gr->gr_gid; | |
215 | } | |
c19a6b30 KS |
216 | } |
217 | ||
7a947ce5 | 218 | if (!udev->test_run) { |
50e5de03 | 219 | info("creating device node '%s'", filename); |
7a947ce5 | 220 | if (make_node(filename, udev->major, udev->minor, udev->mode, uid, gid) != 0) |
bbbe503e | 221 | goto error; |
ab2e5bd9 GKH |
222 | } else { |
223 | info("creating device node '%s', major = '%d', minor = '%d', " | |
224 | "mode = '%#o', uid = '%d', gid = '%d'", filename, | |
7a947ce5 | 225 | udev->major, udev->minor, (mode_t)udev->mode, uid, gid); |
ab2e5bd9 | 226 | } |
50e5de03 | 227 | |
bbbe503e | 228 | /* create all_partitions if requested */ |
7a947ce5 KS |
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++) { | |
c58e36c0 KS |
233 | strfieldcpy(partitionname, filename); |
234 | strintcat(partitionname, i); | |
7a947ce5 | 235 | make_node(partitionname, udev->major, udev->minor + i, udev->mode, uid, gid); |
ab2e5bd9 | 236 | } |
50e5de03 | 237 | } |
3d150dfb KS |
238 | } |
239 | ||
bbbe503e | 240 | /* create symlink(s) if requested */ |
7a947ce5 | 241 | foreach_strpart(udev->symlink, " ", pos, len) { |
17794d77 | 242 | strfieldcpymax(linkname, pos, len+1); |
9fe3f9a9 KS |
243 | strfieldcpy(filename, udev_root); |
244 | strfieldcat(filename, linkname); | |
7a947ce5 KS |
245 | dbg("symlink '%s' to node '%s' requested", filename, udev->name); |
246 | if (!udev->test_run) | |
9fe3f9a9 KS |
247 | if (strrchr(linkname, '/')) |
248 | create_path(filename); | |
249 | ||
250 | /* optimize relative link */ | |
251 | linktarget[0] = '\0'; | |
252 | i = 0; | |
253 | tail = 0; | |
7a947ce5 KS |
254 | while ((udev->name[i] == linkname[i]) && udev->name[i]) { |
255 | if (udev->name[i] == '/') | |
9fe3f9a9 KS |
256 | tail = i+1; |
257 | i++; | |
258 | } | |
259 | while (linkname[i] != '\0') { | |
260 | if (linkname[i] == '/') | |
261 | strfieldcat(linktarget, "../"); | |
262 | i++; | |
263 | } | |
3d150dfb | 264 | |
7a947ce5 | 265 | strfieldcat(linktarget, &udev->name[tail]); |
3d150dfb | 266 | |
9fe3f9a9 | 267 | dbg("symlink(%s, %s)", linktarget, filename); |
7a947ce5 | 268 | if (!udev->test_run) { |
9825617b | 269 | selinux_setfscreatecon(filename, S_IFLNK); |
49747bdc | 270 | unlink(filename); |
bbbe503e | 271 | if (symlink(linktarget, filename) != 0) |
9fe3f9a9 KS |
272 | dbg("symlink(%s, %s) failed with error '%s'", |
273 | linktarget, filename, strerror(errno)); | |
4763256c | 274 | } |
c19a6b30 KS |
275 | } |
276 | ||
bbbe503e KS |
277 | return 0; |
278 | error: | |
279 | return -1; | |
ea733a2f GKH |
280 | } |
281 | ||
7a947ce5 | 282 | static int rename_net_if(struct udevice *udev) |
f61d732a KS |
283 | { |
284 | int sk; | |
285 | struct ifreq ifr; | |
286 | int retval; | |
287 | ||
7a947ce5 KS |
288 | dbg("changing net interface name from '%s' to '%s'", udev->kernel_name, udev->name); |
289 | if (udev->test_run) | |
bbbe503e KS |
290 | return 0; |
291 | ||
f61d732a KS |
292 | sk = socket(PF_INET, SOCK_DGRAM, 0); |
293 | if (sk < 0) { | |
294 | dbg("error opening socket"); | |
295 | return -1; | |
296 | } | |
297 | ||
298 | memset(&ifr, 0x00, sizeof(struct ifreq)); | |
7a947ce5 KS |
299 | strfieldcpy(ifr.ifr_name, udev->kernel_name); |
300 | strfieldcpy(ifr.ifr_newname, udev->name); | |
f61d732a | 301 | |
f61d732a KS |
302 | retval = ioctl(sk, SIOCSIFNAME, &ifr); |
303 | if (retval != 0) | |
304 | dbg("error changing net interface name"); | |
4a539daf | 305 | close(sk); |
f61d732a KS |
306 | |
307 | return retval; | |
308 | } | |
309 | ||
7a947ce5 | 310 | int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev) |
ea733a2f | 311 | { |
bbbe503e | 312 | char *pos; |
707680b1 | 313 | int retval = 0; |
ea733a2f | 314 | |
7a947ce5 KS |
315 | if (udev->type == 'b' || udev->type == 'c') { |
316 | retval = get_major_minor(class_dev, udev); | |
f61d732a | 317 | if (retval != 0) { |
707680b1 | 318 | dbg("no dev-file found, do nothing"); |
5d24c6ca | 319 | return 0; |
f61d732a | 320 | } |
ea733a2f GKH |
321 | } |
322 | ||
7a947ce5 | 323 | if (namedev_name_device(udev, class_dev) != 0) |
30defadd GKH |
324 | goto exit; |
325 | ||
7a947ce5 | 326 | dbg("adding name='%s'", udev->name); |
bbbe503e | 327 | |
9825617b | 328 | selinux_init(); |
5d24c6ca KS |
329 | |
330 | if (udev->type == 'b' || udev->type == 'c') { | |
7a947ce5 | 331 | retval = create_node(udev); |
c4603a07 | 332 | if (retval != 0) |
bbbe503e | 333 | goto exit; |
9b28a52a | 334 | |
5d24c6ca KS |
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"); | |
f61d732a | 338 | |
5d24c6ca KS |
339 | /* use full path to the environment */ |
340 | snprintf(udev->devname, NAME_SIZE-1, "%s%s", udev_root, udev->name); | |
341 | ||
342 | } else if (udev->type == 'n') { | |
343 | /* look if we want to change the name of the netif */ | |
7a947ce5 KS |
344 | if (strcmp(udev->name, udev->kernel_name) != 0) { |
345 | retval = rename_net_if(udev); | |
c4603a07 | 346 | if (retval != 0) |
bbbe503e | 347 | goto exit; |
5d24c6ca KS |
348 | |
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 | |
bbbe503e | 352 | */ |
7a947ce5 | 353 | pos = strrchr(udev->devpath, '/'); |
bbbe503e KS |
354 | if (pos != NULL) { |
355 | pos[1] = '\0'; | |
7a947ce5 | 356 | strfieldcat(udev->devpath, udev->name); |
5d24c6ca | 357 | setenv("DEVPATH", udev->devpath, 1); |
bbbe503e | 358 | } |
9b28a52a | 359 | |
5d24c6ca KS |
360 | /* use netif name for the environment */ |
361 | strfieldcpy(udev->devname, udev->name); | |
362 | } | |
f61d732a | 363 | } |
ea733a2f GKH |
364 | |
365 | exit: | |
9825617b | 366 | selinux_restore(); |
30defadd | 367 | |
ea733a2f GKH |
368 | return retval; |
369 | } |