]>
Commit | Line | Data |
---|---|---|
2232cac8 GKH |
1 | /* |
2 | * namedev.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 <stddef.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <stdio.h> | |
28 | #include <fcntl.h> | |
29 | #include <ctype.h> | |
30 | #include <unistd.h> | |
31 | #include <errno.h> | |
c27e6911 | 32 | #include <sys/wait.h> |
2232cac8 GKH |
33 | |
34 | #include "list.h" | |
35 | #include "udev.h" | |
36 | #include "udev_version.h" | |
37 | #include "namedev.h" | |
185a35a4 | 38 | #include "libsysfs/libsysfs.h" |
2023350e | 39 | #include "klibc_fixups.h" |
2232cac8 | 40 | |
19feb351 | 41 | LIST_HEAD(config_device_list); |
61219c75 | 42 | LIST_HEAD(perm_device_list); |
2232cac8 | 43 | |
9f1da361 KS |
44 | /* compare string with pattern (supports * ? [0-9] [!A-Z]) */ |
45 | static int strcmp_pattern(const char *p, const char *s) | |
c124eafa | 46 | { |
9f1da361 KS |
47 | if (*s == '\0') { |
48 | while (*p == '*') | |
49 | p++; | |
50 | return (*p != '\0'); | |
51 | } | |
52 | switch (*p) { | |
53 | case '[': | |
54 | { | |
55 | int not = 0; | |
56 | p++; | |
57 | if (*p == '!') { | |
58 | not = 1; | |
59 | p++; | |
60 | } | |
61 | while (*p && (*p != ']')) { | |
62 | int match = 0; | |
63 | if (p[1] == '-') { | |
64 | if ((*s >= *p) && (*s <= p[2])) | |
65 | match = 1; | |
66 | p += 3; | |
67 | } else { | |
68 | match = (*p == *s); | |
69 | p++; | |
70 | } | |
71 | if (match ^ not) { | |
72 | while (*p && (*p != ']')) | |
73 | p++; | |
74 | return strcmp_pattern(p+1, s+1); | |
75 | } | |
76 | } | |
77 | } | |
78 | break; | |
79 | case '*': | |
80 | if (strcmp_pattern(p, s+1)) | |
81 | return strcmp_pattern(p+1, s); | |
82 | return 0; | |
83 | case '\0': | |
84 | if (*s == '\0') { | |
85 | return 0; | |
86 | } | |
87 | break; | |
88 | default: | |
89 | if ((*p == *s) || (*p == '?')) | |
90 | return strcmp_pattern(p+1, s+1); | |
91 | break; | |
92 | } | |
93 | return 1; | |
c124eafa KS |
94 | } |
95 | ||
2232cac8 GKH |
96 | #define copy_var(a, b, var) \ |
97 | if (b->var) \ | |
469c7cff | 98 | a->var = b->var; |
2232cac8 GKH |
99 | |
100 | #define copy_string(a, b, var) \ | |
101 | if (strlen(b->var)) \ | |
469c7cff | 102 | strcpy(a->var, b->var); |
2232cac8 | 103 | |
19feb351 | 104 | int add_config_dev(struct config_device *new_dev) |
2232cac8 GKH |
105 | { |
106 | struct list_head *tmp; | |
107 | struct config_device *tmp_dev; | |
108 | ||
09e52d51 | 109 | /* update the values if we already have the device */ |
2232cac8 GKH |
110 | list_for_each(tmp, &config_device_list) { |
111 | struct config_device *dev = list_entry(tmp, struct config_device, node); | |
9f1da361 | 112 | if (strcmp_pattern(new_dev->name, dev->name)) |
c124eafa | 113 | continue; |
137af0cc GKH |
114 | if (strncmp(dev->bus, new_dev->bus, sizeof(dev->name))) |
115 | continue; | |
09e52d51 | 116 | copy_var(dev, new_dev, type); |
09e52d51 KS |
117 | copy_string(dev, new_dev, bus); |
118 | copy_string(dev, new_dev, sysfs_file); | |
119 | copy_string(dev, new_dev, sysfs_value); | |
120 | copy_string(dev, new_dev, id); | |
121 | copy_string(dev, new_dev, place); | |
122 | copy_string(dev, new_dev, kernel_name); | |
137af0cc | 123 | copy_string(dev, new_dev, exec_program); |
09e52d51 | 124 | return 0; |
2232cac8 GKH |
125 | } |
126 | ||
8f43a65e | 127 | /* not found, add new structure to the device list */ |
2232cac8 GKH |
128 | tmp_dev = malloc(sizeof(*tmp_dev)); |
129 | if (!tmp_dev) | |
130 | return -ENOMEM; | |
131 | memcpy(tmp_dev, new_dev, sizeof(*tmp_dev)); | |
8cf7ebe8 | 132 | list_add_tail(&tmp_dev->node, &config_device_list); |
19feb351 | 133 | //dump_config_dev(tmp_dev); |
469c7cff GKH |
134 | return 0; |
135 | } | |
2232cac8 | 136 | |
61219c75 GKH |
137 | int add_perm_dev(struct perm_device *new_dev) |
138 | { | |
139 | struct list_head *tmp; | |
140 | struct perm_device *tmp_dev; | |
141 | ||
142 | /* update the values if we already have the device */ | |
143 | list_for_each(tmp, &perm_device_list) { | |
144 | struct perm_device *dev = list_entry(tmp, struct perm_device, node); | |
145 | if (strcmp_pattern(new_dev->name, dev->name)) | |
146 | continue; | |
147 | copy_var(dev, new_dev, mode); | |
148 | copy_string(dev, new_dev, owner); | |
149 | copy_string(dev, new_dev, group); | |
150 | return 0; | |
151 | } | |
152 | ||
153 | /* not found, add new structure to the perm list */ | |
154 | tmp_dev = malloc(sizeof(*tmp_dev)); | |
155 | if (!tmp_dev) | |
156 | return -ENOMEM; | |
157 | memcpy(tmp_dev, new_dev, sizeof(*tmp_dev)); | |
158 | list_add_tail(&tmp_dev->node, &perm_device_list); | |
159 | //dump_perm_dev(tmp_dev); | |
160 | return 0; | |
161 | } | |
162 | ||
163 | static struct perm_device *find_perm(char *name) | |
164 | { | |
165 | struct list_head *tmp; | |
166 | struct perm_device *perm = NULL; | |
167 | ||
168 | list_for_each(tmp, &perm_device_list) { | |
169 | perm = list_entry(tmp, struct perm_device, node); | |
170 | if (strcmp_pattern(perm->name, name)) | |
171 | continue; | |
172 | return perm; | |
173 | } | |
174 | return NULL; | |
175 | } | |
176 | ||
c2405f50 | 177 | static mode_t get_default_mode(struct sysfs_class_device *class_dev) |
185a35a4 | 178 | { |
89571022 GKH |
179 | mode_t mode = 0600; /* default to owner rw only */ |
180 | ||
181 | if (strlen(default_mode_str) != 0) { | |
182 | mode = strtol(default_mode_str, NULL, 8); | |
183 | } | |
184 | return mode; | |
185a35a4 GKH |
185 | } |
186 | ||
f3b04a2e GKH |
187 | static void build_kernel_number(struct sysfs_class_device *class_dev, struct udevice *udev) |
188 | { | |
189 | char *dig; | |
190 | ||
191 | /* FIXME, figure out how to handle stuff like sdaj which will not work right now. */ | |
192 | dig = class_dev->name + strlen(class_dev->name); | |
193 | while (isdigit(*(dig-1))) | |
194 | dig--; | |
195 | strfieldcpy(udev->kernel_number, dig); | |
f7b4eca4 | 196 | dbg("kernel_number='%s'", udev->kernel_number); |
f3b04a2e GKH |
197 | } |
198 | ||
199 | static void apply_format(struct udevice *udev, unsigned char *string) | |
200 | { | |
201 | char name[NAME_SIZE]; | |
202 | char *pos; | |
203 | ||
204 | while (1) { | |
205 | pos = strchr(string, '%'); | |
206 | ||
207 | if (pos) { | |
208 | strfieldcpy(name, pos+2); | |
209 | *pos = 0x00; | |
210 | switch (pos[1]) { | |
211 | case 'b': | |
212 | if (strlen(udev->bus_id) == 0) | |
213 | break; | |
6968d494 | 214 | strcat(pos, udev->bus_id); |
f3b04a2e GKH |
215 | dbg("substitute bus_id '%s'", udev->bus_id); |
216 | break; | |
217 | case 'n': | |
218 | if (strlen(udev->kernel_number) == 0) | |
219 | break; | |
220 | strcat(pos, udev->kernel_number); | |
221 | dbg("substitute kernel number '%s'", udev->kernel_number); | |
222 | break; | |
5c6f0f14 AB |
223 | case 'D': |
224 | if (strlen(udev->kernel_number) == 0) { | |
225 | strcat(pos, "disk"); | |
226 | break; | |
227 | } | |
228 | strcat(pos, "part"); | |
229 | strcat(pos, udev->kernel_number); | |
230 | dbg("substitute kernel number '%s'", udev->kernel_number); | |
231 | break; | |
f3b04a2e GKH |
232 | case 'm': |
233 | sprintf(pos, "%u", udev->minor); | |
234 | dbg("substitute minor number '%u'", udev->minor); | |
235 | break; | |
236 | case 'M': | |
237 | sprintf(pos, "%u", udev->major); | |
238 | dbg("substitute major number '%u'", udev->major); | |
239 | break; | |
240 | case 'c': | |
241 | if (strlen(udev->callout_value) == 0) | |
242 | break; | |
243 | strcat(pos, udev->callout_value); | |
244 | dbg("substitute callout output '%s'", udev->callout_value); | |
245 | break; | |
246 | default: | |
247 | dbg("unknown substitution type '%%%c'", pos[1]); | |
248 | break; | |
249 | } | |
250 | strcat(string, name); | |
251 | } else | |
252 | break; | |
253 | } | |
254 | } | |
255 | ||
19dc5d4c | 256 | |
c27e6911 PM |
257 | static int exec_callout(struct config_device *dev, char *value, int len) |
258 | { | |
259 | int retval; | |
260 | int res; | |
261 | int status; | |
262 | int fds[2]; | |
263 | pid_t pid; | |
264 | int value_set = 0; | |
265 | char buffer[256]; | |
bc434511 KS |
266 | char *arg; |
267 | char *args[CALLOUT_MAXARG]; | |
268 | int i; | |
c27e6911 | 269 | |
8f43a65e | 270 | dbg("callout to '%s'", dev->exec_program); |
c27e6911 PM |
271 | retval = pipe(fds); |
272 | if (retval != 0) { | |
273 | dbg("pipe failed"); | |
274 | return -1; | |
275 | } | |
276 | pid = fork(); | |
277 | if (pid == -1) { | |
278 | dbg("fork failed"); | |
279 | return -1; | |
280 | } | |
281 | ||
282 | if (pid == 0) { | |
8f43a65e | 283 | /* child */ |
c27e6911 PM |
284 | close(STDOUT_FILENO); |
285 | dup(fds[1]); /* dup write side of pipe to STDOUT */ | |
bc434511 KS |
286 | if (strchr(dev->exec_program, ' ')) { |
287 | /* callout with arguments */ | |
288 | arg = dev->exec_program; | |
289 | for (i=0; i < CALLOUT_MAXARG-1; i++) { | |
290 | args[i] = strsep(&arg, " "); | |
291 | if (args[i] == NULL) | |
292 | break; | |
293 | } | |
294 | if (args[i]) { | |
53dc383e | 295 | dbg("too many args - %d", i); |
bc434511 KS |
296 | args[i] = NULL; |
297 | } | |
298 | retval = execve(args[0], args, main_envp); | |
299 | } else { | |
300 | retval = execve(dev->exec_program, main_argv, main_envp); | |
301 | } | |
c27e6911 PM |
302 | if (retval != 0) { |
303 | dbg("child execve failed"); | |
304 | exit(1); | |
305 | } | |
306 | return -1; /* avoid compiler warning */ | |
307 | } else { | |
8f43a65e | 308 | /* parent reads from fds[0] */ |
c27e6911 PM |
309 | close(fds[1]); |
310 | retval = 0; | |
311 | while (1) { | |
312 | res = read(fds[0], buffer, sizeof(buffer) - 1); | |
313 | if (res <= 0) | |
314 | break; | |
315 | buffer[res] = '\0'; | |
316 | if (res > len) { | |
f7b4eca4 | 317 | dbg("callout len %d too short", len); |
c27e6911 PM |
318 | retval = -1; |
319 | } | |
320 | if (value_set) { | |
321 | dbg("callout value already set"); | |
322 | retval = -1; | |
323 | } else { | |
324 | value_set = 1; | |
325 | strncpy(value, buffer, len); | |
326 | } | |
327 | } | |
bc434511 | 328 | dbg("callout returned '%s'", value); |
c27e6911 PM |
329 | close(fds[0]); |
330 | res = wait(&status); | |
331 | if (res < 0) { | |
332 | dbg("wait failed result %d", res); | |
333 | retval = -1; | |
334 | } | |
335 | ||
1e959a4b | 336 | #ifndef __KLIBC__ |
c27e6911 PM |
337 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) { |
338 | dbg("callout program status 0x%x", status); | |
339 | retval = -1; | |
340 | } | |
1e959a4b | 341 | #endif |
c27e6911 PM |
342 | } |
343 | return retval; | |
344 | } | |
345 | ||
137af0cc | 346 | static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
120d45d0 GKH |
347 | { |
348 | struct config_device *dev; | |
349 | struct list_head *tmp; | |
120d45d0 GKH |
350 | |
351 | list_for_each(tmp, &config_device_list) { | |
352 | dev = list_entry(tmp, struct config_device, node); | |
353 | if (dev->type != CALLOUT) | |
354 | continue; | |
f3b04a2e | 355 | |
137af0cc | 356 | if (sysfs_device) { |
19feb351 | 357 | dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); |
137af0cc GKH |
358 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) |
359 | continue; | |
360 | } | |
361 | ||
f3b04a2e GKH |
362 | /* substitute anything that needs to be in the program name */ |
363 | apply_format(udev, dev->exec_program); | |
364 | if (exec_callout(dev, udev->callout_value, NAME_SIZE)) | |
120d45d0 | 365 | continue; |
9f1da361 | 366 | if (strcmp_pattern(dev->id, udev->callout_value) != 0) |
120d45d0 | 367 | continue; |
70033702 | 368 | strfieldcpy(udev->name, dev->name); |
61219c75 GKH |
369 | dbg("callout returned matching value '%s', '%s' becomes '%s'", |
370 | dev->id, class_dev->name, udev->name); | |
120d45d0 GKH |
371 | return 0; |
372 | } | |
373 | return -ENODEV; | |
374 | } | |
375 | ||
ca1cc0fe GKH |
376 | static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
377 | { | |
378 | struct sysfs_attribute *tmpattr = NULL; | |
379 | struct config_device *dev; | |
380 | struct list_head *tmp; | |
ca1cc0fe GKH |
381 | |
382 | list_for_each(tmp, &config_device_list) { | |
383 | dev = list_entry(tmp, struct config_device, node); | |
384 | if (dev->type != LABEL) | |
385 | continue; | |
386 | ||
137af0cc | 387 | if (sysfs_device) { |
19feb351 | 388 | dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); |
137af0cc GKH |
389 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) |
390 | continue; | |
391 | } | |
392 | ||
19feb351 | 393 | dbg("look for device attribute '%s'", dev->sysfs_file); |
ca1cc0fe GKH |
394 | /* try to find the attribute in the class device directory */ |
395 | tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file); | |
396 | if (tmpattr) | |
397 | goto label_found; | |
398 | ||
399 | /* look in the class device directory if present */ | |
400 | if (sysfs_device) { | |
401 | tmpattr = sysfs_get_device_attr(sysfs_device, dev->sysfs_file); | |
402 | if (tmpattr) | |
403 | goto label_found; | |
404 | } | |
405 | ||
406 | continue; | |
407 | ||
408 | label_found: | |
409 | tmpattr->value[strlen(tmpattr->value)-1] = 0x00; | |
19feb351 | 410 | dbg("compare attribute '%s' value '%s' with '%s'", |
8f43a65e | 411 | dev->sysfs_file, tmpattr->value, dev->sysfs_value); |
ca1cc0fe GKH |
412 | if (strcmp(dev->sysfs_value, tmpattr->value) != 0) |
413 | continue; | |
414 | ||
70033702 | 415 | strfieldcpy(udev->name, dev->name); |
61219c75 GKH |
416 | dbg("found matching attribute '%s', '%s' becomes '%s' ", |
417 | dev->sysfs_file, class_dev->name, udev->name); | |
ca1cc0fe GKH |
418 | |
419 | return 0; | |
420 | } | |
421 | return -ENODEV; | |
422 | } | |
423 | ||
424 | static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) | |
425 | { | |
426 | struct config_device *dev; | |
427 | struct list_head *tmp; | |
428 | char path[SYSFS_PATH_MAX]; | |
429 | int found; | |
430 | char *temp = NULL; | |
431 | ||
432 | /* we have to have a sysfs device for NUMBER to work */ | |
433 | if (!sysfs_device) | |
434 | return -ENODEV; | |
435 | ||
436 | list_for_each(tmp, &config_device_list) { | |
437 | dev = list_entry(tmp, struct config_device, node); | |
438 | if (dev->type != NUMBER) | |
439 | continue; | |
440 | ||
19feb351 | 441 | dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); |
137af0cc GKH |
442 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) |
443 | continue; | |
444 | ||
ca1cc0fe | 445 | found = 0; |
70033702 | 446 | strfieldcpy(path, sysfs_device->path); |
ca1cc0fe | 447 | temp = strrchr(path, '/'); |
19feb351 | 448 | dbg("search '%s' in '%s', path='%s'", dev->id, temp, path); |
ca1cc0fe GKH |
449 | if (strstr(temp, dev->id) != NULL) { |
450 | found = 1; | |
451 | } else { | |
452 | *temp = 0x00; | |
453 | temp = strrchr(path, '/'); | |
19feb351 | 454 | dbg("search '%s' in '%s', path='%s'", dev->id, temp, path); |
ca1cc0fe GKH |
455 | if (strstr(temp, dev->id) != NULL) |
456 | found = 1; | |
457 | } | |
458 | if (!found) | |
459 | continue; | |
70033702 | 460 | strfieldcpy(udev->name, dev->name); |
61219c75 GKH |
461 | dbg("found matching id '%s', '%s' becomes '%s'", |
462 | dev->id, class_dev->name, udev->name); | |
ca1cc0fe GKH |
463 | return 0; |
464 | } | |
465 | return -ENODEV; | |
466 | } | |
467 | ||
8c51bbfe GKH |
468 | static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
469 | { | |
470 | struct config_device *dev; | |
471 | struct list_head *tmp; | |
472 | char path[SYSFS_PATH_MAX]; | |
473 | int found; | |
474 | char *temp = NULL; | |
475 | ||
476 | /* we have to have a sysfs device for TOPOLOGY to work */ | |
477 | if (!sysfs_device) | |
478 | return -ENODEV; | |
479 | ||
480 | list_for_each(tmp, &config_device_list) { | |
481 | dev = list_entry(tmp, struct config_device, node); | |
482 | if (dev->type != TOPOLOGY) | |
483 | continue; | |
484 | ||
19feb351 | 485 | dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); |
137af0cc GKH |
486 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) |
487 | continue; | |
488 | ||
20ff86bd | 489 | found = 0; |
70033702 | 490 | strfieldcpy(path, sysfs_device->path); |
8c51bbfe | 491 | temp = strrchr(path, '/'); |
19feb351 | 492 | dbg("search '%s' in '%s', path='%s'", dev->place, temp, path); |
8c51bbfe GKH |
493 | if (strstr(temp, dev->place) != NULL) { |
494 | found = 1; | |
495 | } else { | |
496 | *temp = 0x00; | |
497 | temp = strrchr(path, '/'); | |
19feb351 | 498 | dbg("search '%s' in '%s', path='%s'", dev->place, temp, path); |
8c51bbfe GKH |
499 | if (strstr(temp, dev->place) != NULL) |
500 | found = 1; | |
501 | } | |
502 | if (!found) | |
503 | continue; | |
504 | ||
70033702 | 505 | strfieldcpy(udev->name, dev->name); |
61219c75 GKH |
506 | dbg("found matching place '%s', '%s' becomes '%s'", |
507 | dev->place, class_dev->name, udev->name); | |
8c51bbfe GKH |
508 | return 0; |
509 | } | |
510 | return -ENODEV; | |
511 | } | |
512 | ||
137af0cc | 513 | static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
120d45d0 GKH |
514 | { |
515 | struct config_device *dev; | |
516 | struct list_head *tmp; | |
517 | ||
518 | list_for_each(tmp, &config_device_list) { | |
519 | dev = list_entry(tmp, struct config_device, node); | |
520 | if (dev->type != REPLACE) | |
521 | continue; | |
522 | ||
19feb351 | 523 | dbg("compare name '%s' with '%s'", dev->kernel_name, class_dev->name); |
9f1da361 | 524 | if (strcmp_pattern(dev->kernel_name, class_dev->name) != 0) |
120d45d0 GKH |
525 | continue; |
526 | ||
70033702 | 527 | strfieldcpy(udev->name, dev->name); |
61219c75 | 528 | dbg("found name, '%s' becomes '%s'", dev->kernel_name, udev->name); |
120d45d0 GKH |
529 | |
530 | return 0; | |
531 | } | |
532 | return -ENODEV; | |
533 | } | |
534 | ||
09e52d51 KS |
535 | static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev) |
536 | { | |
61219c75 | 537 | /* heh, this is pretty simple... */ |
09e52d51 | 538 | strfieldcpy(udev->name, class_dev->name); |
09e52d51 KS |
539 | } |
540 | ||
5c6f0fb0 | 541 | int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev) |
185a35a4 | 542 | { |
7bd22a78 GKH |
543 | struct sysfs_device *sysfs_device = NULL; |
544 | struct sysfs_class_device *class_dev_parent = NULL; | |
185a35a4 | 545 | int retval = 0; |
7bd22a78 | 546 | char *temp = NULL; |
61219c75 | 547 | struct perm_device *perm; |
185a35a4 | 548 | |
9d496c74 | 549 | udev->mode = 0; |
7bd22a78 GKH |
550 | |
551 | /* find the sysfs_device for this class device */ | |
8a0c11d3 | 552 | /* Wouldn't it really be nice if libsysfs could do this for us? */ |
03e64c8f | 553 | if (class_dev->sysdevice) { |
7bd22a78 GKH |
554 | sysfs_device = class_dev->sysdevice; |
555 | } else { | |
556 | /* bah, let's go backwards up a level to see if the device is there, | |
557 | * as block partitions don't point to the physical device. Need to fix that | |
558 | * up in the kernel... | |
559 | */ | |
560 | if (strstr(class_dev->path, "block")) { | |
19feb351 | 561 | dbg("looking at block device"); |
7bd22a78 GKH |
562 | if (isdigit(class_dev->path[strlen(class_dev->path)-1])) { |
563 | char path[SYSFS_PATH_MAX]; | |
564 | ||
19feb351 | 565 | dbg("really is a partition"); |
70033702 | 566 | strfieldcpy(path, class_dev->path); |
7bd22a78 GKH |
567 | temp = strrchr(path, '/'); |
568 | *temp = 0x00; | |
19feb351 | 569 | dbg("looking for a class device at '%s'", path); |
7bd22a78 GKH |
570 | class_dev_parent = sysfs_open_class_device(path); |
571 | if (class_dev_parent == NULL) { | |
572 | dbg("sysfs_open_class_device at '%s' failed", path); | |
573 | } else { | |
19feb351 | 574 | dbg("class_dev_parent->name='%s'", class_dev_parent->name); |
7bd22a78 GKH |
575 | if (class_dev_parent->sysdevice) |
576 | sysfs_device = class_dev_parent->sysdevice; | |
577 | } | |
578 | } | |
579 | } | |
580 | } | |
581 | ||
582 | if (sysfs_device) { | |
19feb351 GKH |
583 | dbg("sysfs_device->path='%s'", sysfs_device->path); |
584 | dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id); | |
585 | dbg("sysfs_device->bus='%s'", sysfs_device->bus); | |
f3b04a2e | 586 | strfieldcpy(udev->bus_id, sysfs_device->bus_id); |
03e64c8f | 587 | } else { |
19feb351 | 588 | dbg("class_dev->name = '%s'", class_dev->name); |
03e64c8f | 589 | } |
120d45d0 | 590 | |
f3b04a2e GKH |
591 | build_kernel_number(class_dev, udev); |
592 | ||
120d45d0 | 593 | /* rules are looked at in priority order */ |
137af0cc | 594 | retval = do_callout(class_dev, udev, sysfs_device); |
120d45d0 | 595 | if (retval == 0) |
09e52d51 | 596 | goto found; |
120d45d0 | 597 | |
ca1cc0fe GKH |
598 | retval = do_label(class_dev, udev, sysfs_device); |
599 | if (retval == 0) | |
09e52d51 | 600 | goto found; |
ca1cc0fe GKH |
601 | |
602 | retval = do_number(class_dev, udev, sysfs_device); | |
603 | if (retval == 0) | |
09e52d51 | 604 | goto found; |
ca1cc0fe | 605 | |
8c51bbfe GKH |
606 | retval = do_topology(class_dev, udev, sysfs_device); |
607 | if (retval == 0) | |
09e52d51 | 608 | goto found; |
8c51bbfe | 609 | |
137af0cc | 610 | retval = do_replace(class_dev, udev, sysfs_device); |
120d45d0 | 611 | if (retval == 0) |
09e52d51 | 612 | goto found; |
120d45d0 | 613 | |
09e52d51 KS |
614 | do_kernelname(class_dev, udev); |
615 | goto done; | |
c2405f50 | 616 | |
09e52d51 | 617 | found: |
98b88dbf | 618 | /* substitute placeholder in NAME */ |
f3b04a2e | 619 | apply_format(udev, udev->name); |
98b88dbf | 620 | |
09e52d51 | 621 | done: |
61219c75 GKH |
622 | perm = find_perm(udev->name); |
623 | if (perm) { | |
624 | udev->mode = perm->mode; | |
625 | strfieldcpy(udev->owner, perm->owner); | |
626 | strfieldcpy(udev->group, perm->group); | |
627 | } else { | |
628 | /* no matching perms found :( */ | |
9d496c74 GKH |
629 | udev->mode = get_default_mode(class_dev); |
630 | udev->owner[0] = 0x00; | |
631 | udev->group[0] = 0x00; | |
615e05f8 | 632 | } |
61219c75 GKH |
633 | dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o", |
634 | udev->name, udev->owner, udev->group, udev->mode); | |
7bd22a78 GKH |
635 | |
636 | if (class_dev_parent) | |
637 | sysfs_close_class_device(class_dev_parent); | |
638 | ||
120d45d0 | 639 | return 0; |
185a35a4 GKH |
640 | } |
641 | ||
2232cac8 GKH |
642 | int namedev_init(void) |
643 | { | |
644 | int retval; | |
185a35a4 | 645 | |
e8baccca | 646 | retval = namedev_init_rules(); |
2232cac8 GKH |
647 | if (retval) |
648 | return retval; | |
649 | ||
650 | retval = namedev_init_permissions(); | |
651 | if (retval) | |
652 | return retval; | |
653 | ||
19feb351 | 654 | dump_config_dev_list(); |
61219c75 | 655 | dump_perm_dev_list(); |
2232cac8 GKH |
656 | return retval; |
657 | } |