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.
37 #include "udev_version.h"
39 #include "libsysfs/libsysfs.h"
40 #include "klibc_fixups.h"
42 LIST_HEAD(config_device_list);
43 LIST_HEAD(perm_device_list);
45 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
46 static int strcmp_pattern(const char *p, const char *s)
62 while (*p && (*p != ']')) {
65 if ((*s >= *p) && (*s <= p[2]))
73 while (*p && (*p != ']'))
75 return strcmp_pattern(p+1, s+1);
81 if (strcmp_pattern(p, s+1))
82 return strcmp_pattern(p+1, s);
90 if ((*p == *s) || (*p == '?'))
91 return strcmp_pattern(p+1, s+1);
97 #define copy_var(a, b, var) \
101 #define copy_string(a, b, var) \
102 if (strlen(b->var)) \
103 strcpy(a->var, b->var);
105 int add_config_dev(struct config_device *new_dev)
107 struct list_head *tmp;
108 struct config_device *tmp_dev;
110 /* update the values if we already have the device */
111 list_for_each(tmp, &config_device_list) {
112 struct config_device *dev = list_entry(tmp, struct config_device, node);
113 if (strcmp_pattern(new_dev->name, dev->name))
115 if (strncmp(dev->bus, new_dev->bus, sizeof(dev->name)))
117 copy_var(dev, new_dev, type);
118 copy_string(dev, new_dev, bus);
119 copy_string(dev, new_dev, sysfs_file);
120 copy_string(dev, new_dev, sysfs_value);
121 copy_string(dev, new_dev, id);
122 copy_string(dev, new_dev, place);
123 copy_string(dev, new_dev, kernel_name);
124 copy_string(dev, new_dev, exec_program);
125 copy_string(dev, new_dev, symlink);
129 /* not found, add new structure to the device list */
130 tmp_dev = malloc(sizeof(*tmp_dev));
133 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
134 list_add_tail(&tmp_dev->node, &config_device_list);
135 //dump_config_dev(tmp_dev);
139 int add_perm_dev(struct perm_device *new_dev)
141 struct list_head *tmp;
142 struct perm_device *tmp_dev;
144 /* update the values if we already have the device */
145 list_for_each(tmp, &perm_device_list) {
146 struct perm_device *dev = list_entry(tmp, struct perm_device, node);
147 if (strcmp_pattern(new_dev->name, dev->name))
149 copy_var(dev, new_dev, mode);
150 copy_string(dev, new_dev, owner);
151 copy_string(dev, new_dev, group);
155 /* not found, add new structure to the perm list */
156 tmp_dev = malloc(sizeof(*tmp_dev));
159 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
160 list_add_tail(&tmp_dev->node, &perm_device_list);
161 //dump_perm_dev(tmp_dev);
165 static struct perm_device *find_perm(char *name)
167 struct list_head *tmp;
168 struct perm_device *perm = NULL;
170 list_for_each(tmp, &perm_device_list) {
171 perm = list_entry(tmp, struct perm_device, node);
172 if (strcmp_pattern(perm->name, name))
179 static mode_t get_default_mode(struct sysfs_class_device *class_dev)
181 mode_t mode = 0600; /* default to owner rw only */
183 if (strlen(default_mode_str) != 0) {
184 mode = strtol(default_mode_str, NULL, 8);
189 static void apply_format(struct udevice *udev, unsigned char *string)
191 char name[NAME_SIZE];
192 char temp[NAME_SIZE];
201 pos = strchr(string, '%');
206 if (isdigit(tail[0])) {
207 num = (int) strtoul(&pos[1], &tail, 10);
209 dbg("format parsing error '%s'", pos+1);
213 strfieldcpy(name, tail+1);
217 if (strlen(udev->bus_id) == 0)
219 strcat(pos, udev->bus_id);
220 dbg("substitute bus_id '%s'", udev->bus_id);
223 if (strlen(udev->kernel_name) == 0)
225 strcat(pos, udev->kernel_name);
226 dbg("substitute kernel name '%s'", udev->kernel_name);
229 if (strlen(udev->kernel_number) == 0)
231 strcat(pos, udev->kernel_number);
232 dbg("substitute kernel number '%s'", udev->kernel_number);
235 if (strlen(udev->kernel_number) == 0) {
240 strcat(pos, udev->kernel_number);
241 dbg("substitute kernel number '%s'", udev->kernel_number);
244 sprintf(pos, "%u", udev->minor);
245 dbg("substitute minor number '%u'", udev->minor);
248 sprintf(pos, "%u", udev->major);
249 dbg("substitute major number '%u'", udev->major);
252 if (strlen(udev->callout_value) == 0)
255 /* get part of return string */
256 strncpy(temp, udev->callout_value, sizeof(temp));
260 pos3 = strsep(&pos2, " ");
262 dbg("requested part of callout string not found");
267 dbg("substitute partial callout output '%s'", pos3);
269 strcat(pos, udev->callout_value);
270 dbg("substitute callout output '%s'", udev->callout_value);
274 dbg("unknown substitution type '%%%c'", pos[1]);
277 strcat(string, name);
283 static struct bus_file {
287 { .bus = "scsi", .file = "vendor" },
288 { .bus = "usb", .file = "idVendor" },
292 #define SECONDS_TO_WAIT_FOR_FILE 10
293 static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device)
295 /* sleep until we see the file for this specific bus type show up this
296 * is needed because we can easily out-run the kernel in looking for
297 * these files before the paticular subsystem has created them in the
298 * sysfs tree properly.
300 * And people thought that the /sbin/hotplug event system was going to
301 * be slow, poo on you for arguing that before even testing it...
303 struct bus_file *b = &bus_files[0];
304 struct sysfs_attribute *tmpattr;
310 if (strcmp(sysfs_device->bus, b->bus) == 0) {
312 loop = SECONDS_TO_WAIT_FOR_FILE;
314 dbg("looking for file '%s' on bus '%s'", b->file, b->bus);
315 tmpattr = sysfs_get_device_attr(sysfs_device, b->file);
320 /* sleep to give the kernel a chance to create the file */
323 dbg("Timed out waiting for '%s' file, continuing on anyway...", b->file);
328 dbg("Did not find bus type '%s' on list of bus_id_files, contact greg@kroah.com", sysfs_device->bus);
330 return; /* here to prevent compiler warning... */
333 static int exec_callout(struct config_device *dev, char *value, int len)
343 char *args[CALLOUT_MAXARG];
346 dbg("callout to '%s'", dev->exec_program);
360 close(STDOUT_FILENO);
361 dup(fds[1]); /* dup write side of pipe to STDOUT */
362 if (strchr(dev->exec_program, ' ')) {
363 /* callout with arguments */
364 pos = dev->exec_program;
365 for (i=0; i < CALLOUT_MAXARG-1; i++) {
366 args[i] = strsep(&pos, " ");
371 dbg("too many args - %d", i);
374 retval = execve(args[0], args, main_envp);
376 retval = execve(dev->exec_program, main_argv, main_envp);
379 dbg("child execve failed");
382 return -1; /* avoid compiler warning */
384 /* parent reads from fds[0] */
388 res = read(fds[0], buffer, sizeof(buffer) - 1);
393 dbg("callout len %d too short", len);
397 dbg("callout value already set");
401 strncpy(value, buffer, len);
402 pos = value + strlen(value)-1;
405 dbg("callout returned '%s'", value);
411 dbg("wait failed result %d", res);
416 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
417 dbg("callout program status 0x%x", status);
425 static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
427 struct config_device *dev;
428 struct list_head *tmp;
430 list_for_each(tmp, &config_device_list) {
431 dev = list_entry(tmp, struct config_device, node);
432 if (dev->type != CALLOUT)
436 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
437 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
441 /* substitute anything that needs to be in the program name */
442 apply_format(udev, dev->exec_program);
443 if (exec_callout(dev, udev->callout_value, NAME_SIZE))
445 if (strcmp_pattern(dev->id, udev->callout_value) != 0)
447 strfieldcpy(udev->name, dev->name);
448 strfieldcpy(udev->symlink, dev->symlink);
449 dbg("callout returned matching value '%s', '%s' becomes '%s'",
450 dev->id, class_dev->name, udev->name);
456 static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
458 struct sysfs_attribute *tmpattr = NULL;
459 struct config_device *dev;
460 struct list_head *tmp;
463 list_for_each(tmp, &config_device_list) {
464 dev = list_entry(tmp, struct config_device, node);
465 if (dev->type != LABEL)
469 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
470 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
474 dbg("look for device attribute '%s'", dev->sysfs_file);
475 /* try to find the attribute in the class device directory */
476 tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file);
480 /* look in the class device directory if present */
482 tmpattr = sysfs_get_device_attr(sysfs_device, dev->sysfs_file);
490 c = tmpattr->value + strlen(tmpattr->value)-1;
493 dbg("compare attribute '%s' value '%s' with '%s'",
494 dev->sysfs_file, tmpattr->value, dev->sysfs_value);
495 if (strcmp_pattern(dev->sysfs_value, tmpattr->value) != 0)
498 strfieldcpy(udev->name, dev->name);
499 strfieldcpy(udev->symlink, dev->symlink);
500 dbg("found matching attribute '%s', '%s' becomes '%s' ",
501 dev->sysfs_file, class_dev->name, udev->name);
508 static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
510 struct config_device *dev;
511 struct list_head *tmp;
512 char path[SYSFS_PATH_MAX];
516 /* we have to have a sysfs device for NUMBER to work */
520 list_for_each(tmp, &config_device_list) {
521 dev = list_entry(tmp, struct config_device, node);
522 if (dev->type != NUMBER)
525 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
526 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
530 strfieldcpy(path, sysfs_device->path);
531 temp = strrchr(path, '/');
532 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
533 if (strstr(temp, dev->id) != NULL) {
537 temp = strrchr(path, '/');
538 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
539 if (strstr(temp, dev->id) != NULL)
544 strfieldcpy(udev->name, dev->name);
545 strfieldcpy(udev->symlink, dev->symlink);
546 dbg("found matching id '%s', '%s' becomes '%s'",
547 dev->id, class_dev->name, udev->name);
553 static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
555 struct config_device *dev;
556 struct list_head *tmp;
557 char path[SYSFS_PATH_MAX];
561 /* we have to have a sysfs device for TOPOLOGY to work */
565 list_for_each(tmp, &config_device_list) {
566 dev = list_entry(tmp, struct config_device, node);
567 if (dev->type != TOPOLOGY)
570 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
571 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
575 strfieldcpy(path, sysfs_device->path);
576 temp = strrchr(path, '/');
577 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
578 if (strstr(temp, dev->place) != NULL) {
582 temp = strrchr(path, '/');
583 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
584 if (strstr(temp, dev->place) != NULL)
590 strfieldcpy(udev->name, dev->name);
591 strfieldcpy(udev->symlink, dev->symlink);
592 dbg("found matching place '%s', '%s' becomes '%s'",
593 dev->place, class_dev->name, udev->name);
599 static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
601 struct config_device *dev;
602 struct list_head *tmp;
604 list_for_each(tmp, &config_device_list) {
605 dev = list_entry(tmp, struct config_device, node);
606 if (dev->type != REPLACE)
609 dbg("compare name '%s' with '%s'", dev->kernel_name, class_dev->name);
610 if (strcmp_pattern(dev->kernel_name, class_dev->name) != 0)
613 strfieldcpy(udev->name, dev->name);
614 strfieldcpy(udev->symlink, dev->symlink);
615 dbg("found name, '%s' becomes '%s'", dev->kernel_name, udev->name);
622 static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev)
624 /* heh, this is pretty simple... */
625 strfieldcpy(udev->name, class_dev->name);
628 static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev)
630 struct sysfs_device *sysfs_device;
631 struct sysfs_class_device *class_dev_parent;
635 /* This is needed here as we can easily out-race the placement of the
636 * device symlink by the kernel. The call to sleep(1); will be removed
637 * once libsysfs can be queried for sysfs_get_classdev_device()
638 * multiple times and have it return the proper information when the
639 * class device really shows up. For now, we live with the time
645 Something like this could also work, but for some reason doesn't,
646 I also tried just stat() on the device symlink, but that still
647 has nasty races, I'm probably doing something stupid... :( */
652 char filename[SYSFS_PATH_MAX + 6];
654 strcpy(filename, class_dev->path);
655 strcat(filename, "/device");
656 dbg("looking for '%s'", filename);
657 retval = stat(filename, &buf);
661 /* bah, let's go backwards up a level to see if the device is there,
662 * as block partitions don't point to the physical device. Need to fix that
663 * up in the kernel...
665 if (strcmp(class_dev->classname, SYSFS_BLOCK_NAME) == 0) {
666 if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
667 class_dev_parent = sysfs_get_classdev_parent(class_dev);
668 if (class_dev_parent == NULL) {
669 dbg("sysfs_get_classdev_parent for class device '%s' failed", class_dev->name);
671 strcpy(filename, class_dev_parent->path);
672 strcat(filename, "/device");
673 dbg("looking for '%s'", filename);
674 retval = stat(filename, &buf);
680 /* sleep to give the kernel a chance to create the device file */
684 loop = 1; /* FIXME put a real value in here for when everything is fixed... */
686 /* find the sysfs_device for this class device */
687 /* Wouldn't it really be nice if libsysfs could do this for us? */
688 sysfs_device = sysfs_get_classdev_device(class_dev);
689 if (sysfs_device != NULL)
692 /* bah, let's go backwards up a level to see if the device is there,
693 * as block partitions don't point to the physical device. Need to fix that
694 * up in the kernel...
696 if (strcmp(class_dev->classname, SYSFS_BLOCK_NAME) == 0) {
697 dbg("looking at block device");
698 if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
699 dbg("really is a partition");
700 class_dev_parent = sysfs_get_classdev_parent(class_dev);
701 if (class_dev_parent == NULL) {
702 dbg("sysfs_get_classdev_parent for class device '%s' failed", class_dev->name);
704 dbg("class_dev_parent->name='%s'", class_dev_parent->name);
705 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
706 if (sysfs_device != NULL)
711 /* sleep to give the kernel a chance to create the link */
715 dbg("Timed out waiting for device symlink, continuing on anyway...");
720 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
722 struct sysfs_device *sysfs_device = NULL;
723 struct sysfs_class_device *class_dev_parent = NULL;
725 struct perm_device *perm;
730 /* find the sysfs_device associated with this class device */
731 sysfs_device = get_sysfs_device(class_dev);
733 dbg("sysfs_device->path='%s'", sysfs_device->path);
734 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
735 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
736 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
737 wait_for_device_to_initialize(sysfs_device);
739 dbg("class_dev->name = '%s'", class_dev->name);
742 strfieldcpy(udev->kernel_name, class_dev->name);
744 /* get kernel number */
745 pos = class_dev->name + strlen(class_dev->name);
746 while (isdigit(*(pos-1)))
748 strfieldcpy(udev->kernel_number, pos);
749 dbg("kernel_number='%s'", udev->kernel_number);
751 /* rules are looked at in priority order */
752 retval = do_callout(class_dev, udev, sysfs_device);
756 retval = do_label(class_dev, udev, sysfs_device);
760 retval = do_number(class_dev, udev, sysfs_device);
764 retval = do_topology(class_dev, udev, sysfs_device);
768 retval = do_replace(class_dev, udev, sysfs_device);
772 do_kernelname(class_dev, udev);
776 /* substitute placeholder */
777 apply_format(udev, udev->name);
778 apply_format(udev, udev->symlink);
781 perm = find_perm(udev->name);
783 udev->mode = perm->mode;
784 strfieldcpy(udev->owner, perm->owner);
785 strfieldcpy(udev->group, perm->group);
787 /* no matching perms found :( */
788 udev->mode = get_default_mode(class_dev);
789 udev->owner[0] = 0x00;
790 udev->group[0] = 0x00;
792 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
793 udev->name, udev->owner, udev->group, udev->mode);
798 int namedev_init(void)
802 retval = namedev_init_rules();
806 retval = namedev_init_permissions();
810 dump_config_dev_list();
811 dump_perm_dev_list();