]>
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> | |
33 | #ifndef __KLIBC__ | |
34 | #include <pwd.h> | |
35 | #endif | |
ea733a2f GKH |
36 | |
37 | #include "udev.h" | |
38 | #include "udev_version.h" | |
7ac0feeb | 39 | #include "udev_dbus.h" |
54988802 | 40 | #include "logging.h" |
ea733a2f | 41 | #include "namedev.h" |
8e41d35d | 42 | #include "udevdb.h" |
ea733a2f | 43 | #include "libsysfs/libsysfs.h" |
10950dfe | 44 | #include "klibc_fixups.h" |
ea733a2f | 45 | |
ea733a2f GKH |
46 | /* |
47 | * Right now the major/minor of a device is stored in a file called | |
48 | * "dev" in sysfs. | |
49 | * The number is stored as: | |
727d1ba5 | 50 | * MM:mm |
ea733a2f GKH |
51 | * MM is the major |
52 | * mm is the minor | |
727d1ba5 | 53 | * The value is in decimal. |
ea733a2f | 54 | */ |
30defadd | 55 | static int get_major_minor(struct sysfs_class_device *class_dev, struct udevice *udev) |
ea733a2f | 56 | { |
ea256f90 | 57 | int retval = -ENODEV; |
5d4754f1 | 58 | struct sysfs_attribute *attr = NULL; |
ea733a2f | 59 | |
5d4754f1 DS |
60 | attr = sysfs_get_classdev_attr(class_dev, "dev"); |
61 | if (attr == NULL) | |
ea256f90 | 62 | goto exit; |
5d4754f1 | 63 | dbg("dev='%s'", attr->value); |
ea733a2f | 64 | |
5d4754f1 | 65 | if (sscanf(attr->value, "%u:%u", &udev->major, &udev->minor) != 2) |
ea256f90 | 66 | goto exit; |
f7b4eca4 | 67 | dbg("found major=%d, minor=%d", udev->major, udev->minor); |
ea733a2f GKH |
68 | |
69 | retval = 0; | |
ea256f90 | 70 | exit: |
ea733a2f GKH |
71 | return retval; |
72 | } | |
73 | ||
3d150dfb KS |
74 | static int create_path(char *file) |
75 | { | |
76 | char p[NAME_SIZE]; | |
77 | char *pos; | |
78 | int retval; | |
79 | struct stat stats; | |
80 | ||
81 | strncpy(p, file, sizeof(p)); | |
82 | pos = strchr(p+1, '/'); | |
83 | while (1) { | |
84 | pos = strchr(pos+1, '/'); | |
85 | if (pos == NULL) | |
86 | break; | |
87 | *pos = 0x00; | |
88 | if (stat(p, &stats)) { | |
89 | retval = mkdir(p, 0755); | |
c5118442 | 90 | if (retval != 0) { |
3d150dfb KS |
91 | dbg("mkdir(%s) failed with error '%s'", |
92 | p, strerror(errno)); | |
93 | return retval; | |
94 | } | |
95 | dbg("created '%s'", p); | |
96 | } | |
97 | *pos = '/'; | |
98 | } | |
99 | return 0; | |
100 | } | |
101 | ||
50e5de03 KS |
102 | static int make_node(char *filename, int major, int minor, unsigned int mode, uid_t uid, gid_t gid) |
103 | { | |
104 | int retval; | |
105 | ||
106 | retval = mknod(filename, mode, makedev(major, minor)); | |
107 | if (retval != 0) { | |
108 | dbg("mknod(%s, %#o, %u, %u) failed with error '%s'", | |
109 | filename, mode, major, minor, strerror(errno)); | |
110 | return retval; | |
111 | } | |
112 | ||
113 | dbg("chmod(%s, %#o)", filename, mode); | |
114 | retval = chmod(filename, mode); | |
115 | if (retval != 0) { | |
116 | dbg("chmod(%s, %#o) failed with error '%s'", | |
117 | filename, mode, strerror(errno)); | |
118 | return retval; | |
119 | } | |
120 | ||
121 | if (uid != 0 || gid != 0) { | |
122 | dbg("chown(%s, %u, %u)", filename, uid, gid); | |
123 | retval = chown(filename, uid, gid); | |
124 | if (retval != 0) { | |
125 | dbg("chown(%s, %u, %u) failed with error '%s'", | |
126 | filename, uid, gid, strerror(errno)); | |
127 | return retval; | |
128 | } | |
129 | } | |
130 | ||
131 | return 0; | |
132 | } | |
133 | ||
eb10f97f | 134 | static int create_node(struct udevice *dev, int fake) |
ea733a2f | 135 | { |
3f09184b | 136 | struct stat stats; |
ea733a2f | 137 | char filename[255]; |
3d150dfb | 138 | char linktarget[255]; |
50e5de03 | 139 | char partitionname[255]; |
4763256c KS |
140 | char *linkname; |
141 | char *symlinks; | |
ea733a2f | 142 | int retval = 0; |
e308ebb7 GKH |
143 | uid_t uid = 0; |
144 | gid_t gid = 0; | |
3d150dfb KS |
145 | int i; |
146 | int tail; | |
147 | ||
c056c514 | 148 | strncpy(filename, udev_root, sizeof(filename)); |
5840bc63 GKH |
149 | strncat(filename, dev->name, sizeof(filename)); |
150 | ||
151 | switch (dev->type) { | |
1331c889 | 152 | case 'b': |
5840bc63 | 153 | dev->mode |= S_IFBLK; |
1331c889 GKH |
154 | break; |
155 | case 'c': | |
156 | case 'u': | |
5840bc63 | 157 | dev->mode |= S_IFCHR; |
1331c889 GKH |
158 | break; |
159 | case 'p': | |
5840bc63 | 160 | dev->mode |= S_IFIFO; |
1331c889 GKH |
161 | break; |
162 | default: | |
5840bc63 | 163 | dbg("unknown node type %c\n", dev->type); |
1331c889 GKH |
164 | return -EINVAL; |
165 | } | |
0abf54fc | 166 | |
3d150dfb KS |
167 | /* create parent directories if needed */ |
168 | if (strrchr(dev->name, '/')) | |
169 | create_path(filename); | |
218eae87 | 170 | |
c5118442 | 171 | if (dev->owner[0] != '\0') { |
c19a6b30 KS |
172 | char *endptr; |
173 | unsigned long id = strtoul(dev->owner, &endptr, 10); | |
765cbd97 | 174 | if (endptr[0] == '\0') |
c19a6b30 | 175 | uid = (uid_t) id; |
10950dfe GKH |
176 | else { |
177 | struct passwd *pw = getpwnam(dev->owner); | |
765cbd97 KS |
178 | if (pw == NULL) |
179 | dbg("specified user unknown '%s'", dev->owner); | |
10950dfe GKH |
180 | else |
181 | uid = pw->pw_uid; | |
182 | } | |
c19a6b30 KS |
183 | } |
184 | ||
c5118442 | 185 | if (dev->group[0] != '\0') { |
c19a6b30 KS |
186 | char *endptr; |
187 | unsigned long id = strtoul(dev->group, &endptr, 10); | |
765cbd97 | 188 | if (endptr[0] == '\0') |
c19a6b30 | 189 | gid = (gid_t) id; |
10950dfe GKH |
190 | else { |
191 | struct group *gr = getgrnam(dev->group); | |
765cbd97 KS |
192 | if (gr == NULL) |
193 | dbg("specified group unknown '%s'", dev->group); | |
10950dfe GKH |
194 | else |
195 | gid = gr->gr_gid; | |
196 | } | |
c19a6b30 KS |
197 | } |
198 | ||
ab2e5bd9 | 199 | if (!fake) { |
50e5de03 KS |
200 | info("creating device node '%s'", filename); |
201 | make_node(filename, dev->major, dev->minor, dev->mode, uid, gid); | |
ab2e5bd9 GKH |
202 | } else { |
203 | info("creating device node '%s', major = '%d', minor = '%d', " | |
204 | "mode = '%#o', uid = '%d', gid = '%d'", filename, | |
205 | dev->major, dev->minor, (mode_t)dev->mode, uid, gid); | |
206 | } | |
50e5de03 KS |
207 | |
208 | /* create partitions if requested */ | |
209 | if (dev->partitions > 0) { | |
210 | info("creating device partition nodes '%s[1-%i]'", filename, dev->partitions); | |
ab2e5bd9 GKH |
211 | if (!fake) { |
212 | for (i = 1; i <= dev->partitions; i++) { | |
213 | sprintf(partitionname, "%s%i", filename, i); | |
214 | make_node(partitionname, dev->major, | |
215 | dev->minor + i, dev->mode, uid, gid); | |
216 | } | |
50e5de03 | 217 | } |
3d150dfb KS |
218 | } |
219 | ||
3d150dfb | 220 | /* create symlink if requested */ |
c5118442 | 221 | if (dev->symlink[0] != '\0') { |
4763256c KS |
222 | symlinks = dev->symlink; |
223 | while (1) { | |
224 | linkname = strsep(&symlinks, " "); | |
0529e2ed | 225 | if (linkname == NULL || linkname[0] == '\0') |
4763256c KS |
226 | break; |
227 | ||
228 | strncpy(filename, udev_root, sizeof(filename)); | |
229 | strncat(filename, linkname, sizeof(filename)); | |
230 | dbg("symlink '%s' to node '%s' requested", filename, dev->name); | |
eb10f97f GKH |
231 | if (!fake) |
232 | if (strrchr(linkname, '/')) | |
233 | create_path(filename); | |
4763256c KS |
234 | |
235 | /* optimize relative link */ | |
236 | linktarget[0] = '\0'; | |
237 | i = 0; | |
238 | tail = 0; | |
239 | while ((dev->name[i] == linkname[i]) && dev->name[i]) { | |
240 | if (dev->name[i] == '/') | |
241 | tail = i+1; | |
242 | i++; | |
243 | } | |
c5118442 | 244 | while (linkname[i] != '\0') { |
4763256c KS |
245 | if (linkname[i] == '/') |
246 | strcat(linktarget, "../"); | |
247 | i++; | |
248 | } | |
3d150dfb | 249 | |
c5118442 | 250 | if (linktarget[0] == '\0') |
4763256c KS |
251 | strcpy(linktarget, "./"); |
252 | strcat(linktarget, &dev->name[tail]); | |
3d150dfb | 253 | |
50e5de03 | 254 | /* unlink existing files to ensure that our symlink is created */ |
eb10f97f | 255 | if (!fake && (lstat(filename, &stats) == 0)) { |
3f09184b | 256 | if ((stats.st_mode & S_IFMT) != S_IFDIR) { |
257 | if (unlink(filename)) | |
258 | dbg("unlink(%s) failed with error '%s'", | |
259 | filename, strerror(errno)); | |
260 | } | |
261 | } | |
262 | ||
4763256c | 263 | dbg("symlink(%s, %s)", linktarget, filename); |
eb10f97f GKH |
264 | if (!fake) { |
265 | retval = symlink(linktarget, filename); | |
266 | if (retval != 0) | |
267 | dbg("symlink(%s, %s) failed with error '%s'", | |
268 | linktarget, filename, strerror(errno)); | |
269 | } | |
4763256c | 270 | } |
c19a6b30 KS |
271 | } |
272 | ||
ea733a2f GKH |
273 | return retval; |
274 | } | |
275 | ||
d7e954a4 | 276 | static struct sysfs_class_device *get_class_dev(char *device_name) |
ea733a2f GKH |
277 | { |
278 | char dev_path[SYSFS_PATH_MAX]; | |
63dde9f8 GKH |
279 | struct sysfs_class_device *class_dev = NULL; |
280 | ||
ea733a2f GKH |
281 | strcpy(dev_path, sysfs_path); |
282 | strcat(dev_path, device_name); | |
f7b4eca4 | 283 | dbg("looking at '%s'", dev_path); |
ea733a2f GKH |
284 | |
285 | /* open up the sysfs class device for this thing... */ | |
bcbe2d8e | 286 | class_dev = sysfs_open_class_device_path(dev_path); |
ea733a2f | 287 | if (class_dev == NULL) { |
bcbe2d8e | 288 | dbg ("sysfs_open_class_device_path failed"); |
63dde9f8 | 289 | goto exit; |
ea733a2f | 290 | } |
f7b4eca4 | 291 | dbg("class_dev->name='%s'", class_dev->name); |
ea733a2f | 292 | |
63dde9f8 | 293 | exit: |
ea733a2f GKH |
294 | return class_dev; |
295 | } | |
296 | ||
29fd7e67 GKH |
297 | /* wait for the "dev" file to show up in the directory in sysfs. |
298 | * If it doesn't happen in about 10 seconds, give up. | |
299 | */ | |
300 | #define SECONDS_TO_WAIT_FOR_DEV 10 | |
6089318c | 301 | static int sleep_for_dev(char *path) |
29fd7e67 GKH |
302 | { |
303 | char filename[SYSFS_PATH_MAX + 6]; | |
c332cfc7 RL |
304 | int loop = SECONDS_TO_WAIT_FOR_DEV; |
305 | int retval; | |
29fd7e67 GKH |
306 | |
307 | strcpy(filename, sysfs_path); | |
5840bc63 | 308 | strcat(filename, path); |
29fd7e67 GKH |
309 | strcat(filename, "/dev"); |
310 | ||
c332cfc7 RL |
311 | while (loop--) { |
312 | struct stat buf; | |
313 | ||
f7b4eca4 | 314 | dbg("looking for '%s'", filename); |
29fd7e67 | 315 | retval = stat(filename, &buf); |
c5118442 | 316 | if (retval == 0) |
29fd7e67 | 317 | goto exit; |
29fd7e67 | 318 | |
f7b4eca4 | 319 | /* sleep to give the kernel a chance to create the dev file */ |
29fd7e67 GKH |
320 | sleep(1); |
321 | } | |
322 | retval = -ENODEV; | |
323 | exit: | |
324 | return retval; | |
325 | } | |
326 | ||
eb10f97f | 327 | int udev_add_device(char *path, char *subsystem, int fake) |
ea733a2f | 328 | { |
30defadd | 329 | struct sysfs_class_device *class_dev = NULL; |
5840bc63 | 330 | struct udevice dev; |
ea733a2f GKH |
331 | int retval = -EINVAL; |
332 | ||
f3b04a2e GKH |
333 | memset(&dev, 0x00, sizeof(dev)); |
334 | ||
ea733a2f GKH |
335 | /* for now, the block layer is the only place where block devices are */ |
336 | if (strcmp(subsystem, "block") == 0) | |
5840bc63 | 337 | dev.type = 'b'; |
ea733a2f | 338 | else |
5840bc63 | 339 | dev.type = 'c'; |
ea733a2f | 340 | |
5840bc63 | 341 | retval = sleep_for_dev(path); |
c5118442 | 342 | if (retval != 0) |
29fd7e67 | 343 | goto exit; |
63dde9f8 | 344 | |
5840bc63 | 345 | class_dev = get_class_dev(path); |
ea733a2f GKH |
346 | if (class_dev == NULL) |
347 | goto exit; | |
348 | ||
30defadd | 349 | retval = get_major_minor(class_dev, &dev); |
c5118442 | 350 | if (retval != 0) { |
ca999860 | 351 | dbg("get_major_minor failed"); |
ea733a2f GKH |
352 | goto exit; |
353 | } | |
354 | ||
30defadd | 355 | retval = namedev_name_device(class_dev, &dev); |
c5118442 | 356 | if (retval != 0) |
30defadd GKH |
357 | goto exit; |
358 | ||
eb10f97f GKH |
359 | if (!fake) { |
360 | retval = udevdb_add_dev(path, &dev); | |
361 | if (retval != 0) | |
362 | dbg("udevdb_add_dev failed, but we are going to try " | |
363 | "to create the node anyway. But remove might not " | |
364 | "work properly for this device."); | |
ca999860 | 365 | |
eb10f97f | 366 | } |
f7b4eca4 | 367 | dbg("name='%s'", dev.name); |
eb10f97f | 368 | retval = create_node(&dev, fake); |
ea733a2f | 369 | |
eb10f97f | 370 | if ((retval == 0) && (!fake)) |
7ac0feeb | 371 | sysbus_send_create(&dev, path); |
5aebfbcb | 372 | |
ea733a2f | 373 | exit: |
30defadd GKH |
374 | if (class_dev) |
375 | sysfs_close_class_device(class_dev); | |
376 | ||
ea733a2f GKH |
377 | return retval; |
378 | } |