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 config_device *tmp_dev;
109 tmp_dev = malloc(sizeof(*tmp_dev));
112 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
113 list_add_tail(&tmp_dev->node, &config_device_list);
114 //dump_config_dev(tmp_dev);
118 int add_perm_dev(struct perm_device *new_dev)
120 struct list_head *tmp;
121 struct perm_device *tmp_dev;
123 /* update the values if we already have the device */
124 list_for_each(tmp, &perm_device_list) {
125 struct perm_device *dev = list_entry(tmp, struct perm_device, node);
126 if (strcmp_pattern(new_dev->name, dev->name))
128 copy_var(dev, new_dev, mode);
129 copy_string(dev, new_dev, owner);
130 copy_string(dev, new_dev, group);
134 /* not found, add new structure to the perm list */
135 tmp_dev = malloc(sizeof(*tmp_dev));
138 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
139 list_add_tail(&tmp_dev->node, &perm_device_list);
140 //dump_perm_dev(tmp_dev);
144 static struct perm_device *find_perm(char *name)
146 struct list_head *tmp;
147 struct perm_device *perm = NULL;
149 list_for_each(tmp, &perm_device_list) {
150 perm = list_entry(tmp, struct perm_device, node);
151 if (strcmp_pattern(perm->name, name))
158 static mode_t get_default_mode(struct sysfs_class_device *class_dev)
160 mode_t mode = 0600; /* default to owner rw only */
162 if (strlen(default_mode_str) != 0) {
163 mode = strtol(default_mode_str, NULL, 8);
168 static void apply_format(struct udevice *udev, unsigned char *string)
170 char name[NAME_SIZE];
171 char temp[NAME_SIZE];
180 pos = strchr(string, '%');
185 if (isdigit(tail[0])) {
186 num = (int) strtoul(&pos[1], &tail, 10);
188 dbg("format parsing error '%s'", pos+1);
192 strfieldcpy(name, tail+1);
196 if (strlen(udev->bus_id) == 0)
198 strcat(pos, udev->bus_id);
199 dbg("substitute bus_id '%s'", udev->bus_id);
202 if (strlen(udev->kernel_name) == 0)
204 strcat(pos, udev->kernel_name);
205 dbg("substitute kernel name '%s'", udev->kernel_name);
208 if (strlen(udev->kernel_number) == 0)
210 strcat(pos, udev->kernel_number);
211 dbg("substitute kernel number '%s'", udev->kernel_number);
214 if (strlen(udev->kernel_number) == 0) {
219 strcat(pos, udev->kernel_number);
220 dbg("substitute kernel number '%s'", udev->kernel_number);
223 sprintf(pos, "%u", udev->minor);
224 dbg("substitute minor number '%u'", udev->minor);
227 sprintf(pos, "%u", udev->major);
228 dbg("substitute major number '%u'", udev->major);
231 if (strlen(udev->callout_value) == 0)
234 /* get part of return string */
235 strncpy(temp, udev->callout_value, sizeof(temp));
239 pos3 = strsep(&pos2, " ");
241 dbg("requested part of callout string not found");
246 dbg("substitute partial callout output '%s'", pos3);
248 strcat(pos, udev->callout_value);
249 dbg("substitute callout output '%s'", udev->callout_value);
253 dbg("unknown substitution type '%%%c'", pos[1]);
256 strcat(string, name);
262 static struct bus_file {
266 { .bus = "scsi", .file = "vendor" },
267 { .bus = "usb", .file = "idVendor" },
271 #define SECONDS_TO_WAIT_FOR_FILE 10
272 static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device)
274 /* sleep until we see the file for this specific bus type show up this
275 * is needed because we can easily out-run the kernel in looking for
276 * these files before the paticular subsystem has created them in the
277 * sysfs tree properly.
279 * And people thought that the /sbin/hotplug event system was going to
280 * be slow, poo on you for arguing that before even testing it...
282 struct bus_file *b = &bus_files[0];
283 struct sysfs_attribute *tmpattr;
289 if (strcmp(sysfs_device->bus, b->bus) == 0) {
291 loop = SECONDS_TO_WAIT_FOR_FILE;
293 dbg("looking for file '%s' on bus '%s'", b->file, b->bus);
294 tmpattr = sysfs_get_device_attr(sysfs_device, b->file);
299 /* sleep to give the kernel a chance to create the file */
302 dbg("Timed out waiting for '%s' file, continuing on anyway...", b->file);
307 dbg("Did not find bus type '%s' on list of bus_id_files, contact greg@kroah.com", sysfs_device->bus);
309 return; /* here to prevent compiler warning... */
312 static int exec_callout(struct config_device *dev, char *value, int len)
322 char *args[CALLOUT_MAXARG];
325 dbg("callout to '%s'", dev->exec_program);
339 close(STDOUT_FILENO);
340 dup(fds[1]); /* dup write side of pipe to STDOUT */
341 if (strchr(dev->exec_program, ' ')) {
342 /* callout with arguments */
343 pos = dev->exec_program;
344 for (i=0; i < CALLOUT_MAXARG-1; i++) {
345 args[i] = strsep(&pos, " ");
350 dbg("too many args - %d", i);
353 retval = execve(args[0], args, main_envp);
355 retval = execve(dev->exec_program, main_argv, main_envp);
358 dbg("child execve failed");
361 return -1; /* avoid compiler warning */
363 /* parent reads from fds[0] */
367 res = read(fds[0], buffer, sizeof(buffer) - 1);
372 dbg("callout len %d too short", len);
376 dbg("callout value already set");
380 strncpy(value, buffer, len);
381 pos = value + strlen(value)-1;
384 dbg("callout returned '%s'", value);
390 dbg("wait failed result %d", res);
395 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
396 dbg("callout program status 0x%x", status);
404 static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
406 struct config_device *dev;
407 struct list_head *tmp;
409 list_for_each(tmp, &config_device_list) {
410 dev = list_entry(tmp, struct config_device, node);
411 if (dev->type != CALLOUT)
415 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
416 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
420 /* substitute anything that needs to be in the program name */
421 apply_format(udev, dev->exec_program);
422 if (exec_callout(dev, udev->callout_value, NAME_SIZE))
424 if (strcmp_pattern(dev->id, udev->callout_value) != 0)
426 strfieldcpy(udev->name, dev->name);
427 strfieldcpy(udev->symlink, dev->symlink);
428 dbg("callout returned matching value '%s', '%s' becomes '%s'",
429 dev->id, class_dev->name, udev->name);
435 static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
437 struct sysfs_attribute *tmpattr = NULL;
438 struct config_device *dev;
439 struct list_head *tmp;
442 list_for_each(tmp, &config_device_list) {
443 dev = list_entry(tmp, struct config_device, node);
444 if (dev->type != LABEL)
448 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
449 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
453 dbg("look for device attribute '%s'", dev->sysfs_file);
454 /* try to find the attribute in the class device directory */
455 tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file);
459 /* look in the class device directory if present */
461 tmpattr = sysfs_get_device_attr(sysfs_device, dev->sysfs_file);
469 c = tmpattr->value + strlen(tmpattr->value)-1;
472 dbg("compare attribute '%s' value '%s' with '%s'",
473 dev->sysfs_file, tmpattr->value, dev->sysfs_value);
474 if (strcmp_pattern(dev->sysfs_value, tmpattr->value) != 0)
477 strfieldcpy(udev->name, dev->name);
478 strfieldcpy(udev->symlink, dev->symlink);
479 dbg("found matching attribute '%s', '%s' becomes '%s' ",
480 dev->sysfs_file, class_dev->name, udev->name);
487 static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
489 struct config_device *dev;
490 struct list_head *tmp;
491 char path[SYSFS_PATH_MAX];
495 /* we have to have a sysfs device for NUMBER to work */
499 list_for_each(tmp, &config_device_list) {
500 dev = list_entry(tmp, struct config_device, node);
501 if (dev->type != NUMBER)
504 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
505 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
509 strfieldcpy(path, sysfs_device->path);
510 temp = strrchr(path, '/');
511 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
512 if (strstr(temp, dev->id) != NULL) {
516 temp = strrchr(path, '/');
517 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
518 if (strstr(temp, dev->id) != NULL)
523 strfieldcpy(udev->name, dev->name);
524 strfieldcpy(udev->symlink, dev->symlink);
525 dbg("found matching id '%s', '%s' becomes '%s'",
526 dev->id, class_dev->name, udev->name);
532 static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
534 struct config_device *dev;
535 struct list_head *tmp;
536 char path[SYSFS_PATH_MAX];
540 /* we have to have a sysfs device for TOPOLOGY to work */
544 list_for_each(tmp, &config_device_list) {
545 dev = list_entry(tmp, struct config_device, node);
546 if (dev->type != TOPOLOGY)
549 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
550 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
554 strfieldcpy(path, sysfs_device->path);
555 temp = strrchr(path, '/');
556 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
557 if (strstr(temp, dev->place) != NULL) {
561 temp = strrchr(path, '/');
562 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
563 if (strstr(temp, dev->place) != NULL)
569 strfieldcpy(udev->name, dev->name);
570 strfieldcpy(udev->symlink, dev->symlink);
571 dbg("found matching place '%s', '%s' becomes '%s'",
572 dev->place, class_dev->name, udev->name);
578 static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
580 struct config_device *dev;
581 struct list_head *tmp;
583 list_for_each(tmp, &config_device_list) {
584 dev = list_entry(tmp, struct config_device, node);
585 if (dev->type != REPLACE)
588 dbg("compare name '%s' with '%s'", dev->kernel_name, class_dev->name);
589 if (strcmp_pattern(dev->kernel_name, class_dev->name) != 0)
592 strfieldcpy(udev->name, dev->name);
593 strfieldcpy(udev->symlink, dev->symlink);
594 dbg("found name, '%s' becomes '%s'", dev->kernel_name, udev->name);
601 static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev)
603 /* heh, this is pretty simple... */
604 strfieldcpy(udev->name, class_dev->name);
607 static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev)
609 struct sysfs_device *sysfs_device;
610 struct sysfs_class_device *class_dev_parent;
614 /* This is needed here as we can easily out-race the placement of the
615 * device symlink by the kernel. The call to sleep(1); will be removed
616 * once libsysfs can be queried for sysfs_get_classdev_device()
617 * multiple times and have it return the proper information when the
618 * class device really shows up. For now, we live with the time
624 Something like this could also work, but for some reason doesn't,
625 I also tried just stat() on the device symlink, but that still
626 has nasty races, I'm probably doing something stupid... :( */
631 char filename[SYSFS_PATH_MAX + 6];
633 strcpy(filename, class_dev->path);
634 strcat(filename, "/device");
635 dbg("looking for '%s'", filename);
636 retval = stat(filename, &buf);
640 /* bah, let's go backwards up a level to see if the device is there,
641 * as block partitions don't point to the physical device. Need to fix that
642 * up in the kernel...
644 if (strcmp(class_dev->classname, SYSFS_BLOCK_NAME) == 0) {
645 if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
646 class_dev_parent = sysfs_get_classdev_parent(class_dev);
647 if (class_dev_parent == NULL) {
648 dbg("sysfs_get_classdev_parent for class device '%s' failed", class_dev->name);
650 strcpy(filename, class_dev_parent->path);
651 strcat(filename, "/device");
652 dbg("looking for '%s'", filename);
653 retval = stat(filename, &buf);
659 /* sleep to give the kernel a chance to create the device file */
663 loop = 1; /* FIXME put a real value in here for when everything is fixed... */
665 /* find the sysfs_device for this class device */
666 /* Wouldn't it really be nice if libsysfs could do this for us? */
667 sysfs_device = sysfs_get_classdev_device(class_dev);
668 if (sysfs_device != NULL)
671 /* bah, let's go backwards up a level to see if the device is there,
672 * as block partitions don't point to the physical device. Need to fix that
673 * up in the kernel...
675 if (strcmp(class_dev->classname, SYSFS_BLOCK_NAME) == 0) {
676 dbg("looking at block device");
677 if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
678 dbg("really is a partition");
679 class_dev_parent = sysfs_get_classdev_parent(class_dev);
680 if (class_dev_parent == NULL) {
681 dbg("sysfs_get_classdev_parent for class device '%s' failed", class_dev->name);
683 dbg("class_dev_parent->name='%s'", class_dev_parent->name);
684 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
685 if (sysfs_device != NULL)
690 /* sleep to give the kernel a chance to create the link */
694 dbg("Timed out waiting for device symlink, continuing on anyway...");
699 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
701 struct sysfs_device *sysfs_device = NULL;
702 struct sysfs_class_device *class_dev_parent = NULL;
704 struct perm_device *perm;
709 /* find the sysfs_device associated with this class device */
710 sysfs_device = get_sysfs_device(class_dev);
712 dbg("sysfs_device->path='%s'", sysfs_device->path);
713 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
714 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
715 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
716 wait_for_device_to_initialize(sysfs_device);
718 dbg("class_dev->name = '%s'", class_dev->name);
721 strfieldcpy(udev->kernel_name, class_dev->name);
723 /* get kernel number */
724 pos = class_dev->name + strlen(class_dev->name);
725 while (isdigit(*(pos-1)))
727 strfieldcpy(udev->kernel_number, pos);
728 dbg("kernel_number='%s'", udev->kernel_number);
730 /* rules are looked at in priority order */
731 retval = do_callout(class_dev, udev, sysfs_device);
735 retval = do_label(class_dev, udev, sysfs_device);
739 retval = do_number(class_dev, udev, sysfs_device);
743 retval = do_topology(class_dev, udev, sysfs_device);
747 retval = do_replace(class_dev, udev, sysfs_device);
751 do_kernelname(class_dev, udev);
755 /* substitute placeholder */
756 apply_format(udev, udev->name);
757 apply_format(udev, udev->symlink);
760 perm = find_perm(udev->name);
762 udev->mode = perm->mode;
763 strfieldcpy(udev->owner, perm->owner);
764 strfieldcpy(udev->group, perm->group);
766 /* no matching perms found :( */
767 udev->mode = get_default_mode(class_dev);
768 udev->owner[0] = 0x00;
769 udev->group[0] = 0x00;
771 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
772 udev->name, udev->owner, udev->group, udev->mode);
777 int namedev_init(void)
781 retval = namedev_init_rules();
785 retval = namedev_init_permissions();
789 dump_config_dev_list();
790 dump_perm_dev_list();