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_perm_dev(struct perm_device *new_dev)
107 struct list_head *tmp;
108 struct perm_device *tmp_dev;
110 /* update the values if we already have the device */
111 list_for_each(tmp, &perm_device_list) {
112 struct perm_device *dev = list_entry(tmp, struct perm_device, node);
113 if (strcmp_pattern(new_dev->name, dev->name))
115 copy_var(dev, new_dev, mode);
116 copy_string(dev, new_dev, owner);
117 copy_string(dev, new_dev, group);
121 /* not found, add new structure to the perm list */
122 tmp_dev = malloc(sizeof(*tmp_dev));
125 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
126 list_add_tail(&tmp_dev->node, &perm_device_list);
127 //dump_perm_dev(tmp_dev);
131 static struct perm_device *find_perm(char *name)
133 struct list_head *tmp;
134 struct perm_device *perm = NULL;
136 list_for_each(tmp, &perm_device_list) {
137 perm = list_entry(tmp, struct perm_device, node);
138 if (strcmp_pattern(perm->name, name))
145 static mode_t get_default_mode(struct sysfs_class_device *class_dev)
147 mode_t mode = 0600; /* default to owner rw only */
149 if (strlen(default_mode_str) != 0) {
150 mode = strtol(default_mode_str, NULL, 8);
155 static void apply_format(struct udevice *udev, unsigned char *string)
157 char name[NAME_SIZE];
158 char temp[NAME_SIZE];
167 pos = strchr(string, '%');
172 if (isdigit(tail[0])) {
173 num = (int) strtoul(&pos[1], &tail, 10);
175 dbg("format parsing error '%s'", pos+1);
179 strfieldcpy(name, tail+1);
183 if (strlen(udev->bus_id) == 0)
185 strcat(pos, udev->bus_id);
186 dbg("substitute bus_id '%s'", udev->bus_id);
189 if (strlen(udev->kernel_name) == 0)
191 strcat(pos, udev->kernel_name);
192 dbg("substitute kernel name '%s'", udev->kernel_name);
195 if (strlen(udev->kernel_number) == 0)
197 strcat(pos, udev->kernel_number);
198 dbg("substitute kernel number '%s'", udev->kernel_number);
201 if (strlen(udev->kernel_number) == 0) {
206 strcat(pos, udev->kernel_number);
207 dbg("substitute kernel number '%s'", udev->kernel_number);
210 sprintf(pos, "%u", udev->minor);
211 dbg("substitute minor number '%u'", udev->minor);
214 sprintf(pos, "%u", udev->major);
215 dbg("substitute major number '%u'", udev->major);
218 if (strlen(udev->callout_value) == 0)
221 /* get part of return string */
222 strncpy(temp, udev->callout_value, sizeof(temp));
226 pos3 = strsep(&pos2, " ");
228 dbg("requested part of callout string not found");
233 dbg("substitute partial callout output '%s'", pos3);
235 strcat(pos, udev->callout_value);
236 dbg("substitute callout output '%s'", udev->callout_value);
240 dbg("unknown substitution type '%%%c'", pos[1]);
243 strcat(string, name);
249 static struct bus_file {
253 { .bus = "scsi", .file = "vendor" },
254 { .bus = "usb", .file = "idVendor" },
255 { .bus = "pci", .file = "vendor" },
259 #define SECONDS_TO_WAIT_FOR_FILE 10
260 static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device)
262 /* sleep until we see the file for this specific bus type show up this
263 * is needed because we can easily out-run the kernel in looking for
264 * these files before the paticular subsystem has created them in the
265 * sysfs tree properly.
267 * And people thought that the /sbin/hotplug event system was going to
268 * be slow, poo on you for arguing that before even testing it...
270 struct bus_file *b = &bus_files[0];
271 struct sysfs_attribute *tmpattr;
277 if (strcmp(sysfs_device->bus, b->bus) == 0) {
279 loop = SECONDS_TO_WAIT_FOR_FILE;
281 dbg("looking for file '%s' on bus '%s'", b->file, b->bus);
282 tmpattr = sysfs_get_device_attr(sysfs_device, b->file);
287 /* sleep to give the kernel a chance to create the file */
290 dbg("Timed out waiting for '%s' file, continuing on anyway...", b->file);
295 dbg("Did not find bus type '%s' on list of bus_id_files, contact greg@kroah.com", sysfs_device->bus);
297 return; /* here to prevent compiler warning... */
300 static int exec_callout(struct config_device *dev, char *value, int len)
310 char *args[CALLOUT_MAXARG];
313 dbg("callout to '%s'", dev->exec_program);
327 close(STDOUT_FILENO);
328 dup(fds[1]); /* dup write side of pipe to STDOUT */
329 if (strchr(dev->exec_program, ' ')) {
330 /* callout with arguments */
331 pos = dev->exec_program;
332 for (i=0; i < CALLOUT_MAXARG-1; i++) {
333 args[i] = strsep(&pos, " ");
338 dbg("too many args - %d", i);
341 retval = execve(args[0], args, main_envp);
343 retval = execve(dev->exec_program, main_argv, main_envp);
346 dbg("child execve failed");
349 return -1; /* avoid compiler warning */
351 /* parent reads from fds[0] */
355 res = read(fds[0], buffer, sizeof(buffer) - 1);
360 dbg("callout len %d too short", len);
364 dbg("callout value already set");
368 strncpy(value, buffer, len);
369 pos = value + strlen(value)-1;
372 dbg("callout returned '%s'", value);
378 dbg("wait failed result %d", res);
383 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
384 dbg("callout program status 0x%x", status);
392 static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
394 struct config_device *dev;
395 struct list_head *tmp;
397 list_for_each(tmp, &config_device_list) {
398 dev = list_entry(tmp, struct config_device, node);
399 if (dev->type != CALLOUT)
402 if (dev->bus[0] != '\0') {
403 /* as the user specified a bus, we must match it up */
406 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
407 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
411 /* substitute anything that needs to be in the program name */
412 apply_format(udev, dev->exec_program);
413 if (exec_callout(dev, udev->callout_value, NAME_SIZE))
415 if (strcmp_pattern(dev->id, udev->callout_value) != 0)
417 strfieldcpy(udev->name, dev->name);
418 strfieldcpy(udev->symlink, dev->symlink);
419 dbg("callout returned matching value '%s', '%s' becomes '%s'",
420 dev->id, class_dev->name, udev->name);
426 static int match_pair(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
428 struct sysfs_attribute *tmpattr = NULL;
431 if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
434 dbg("look for device attribute '%s'", pair->file);
435 /* try to find the attribute in the class device directory */
436 tmpattr = sysfs_get_classdev_attr(class_dev, pair->file);
440 /* look in the class device directory if present */
442 tmpattr = sysfs_get_device_attr(sysfs_device, pair->file);
450 c = tmpattr->value + strlen(tmpattr->value)-1;
453 dbg("compare attribute '%s' value '%s' with '%s'",
454 pair->file, tmpattr->value, pair->value);
455 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
458 dbg("found matching attribute '%s' with value '%s'",
459 pair->file, pair->value);
463 static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
465 struct sysfs_pair *pair;
466 struct config_device *dev;
467 struct list_head *tmp;
471 list_for_each(tmp, &config_device_list) {
472 dev = list_entry(tmp, struct config_device, node);
473 if (dev->type != LABEL)
476 if (dev->bus[0] != '\0') {
477 /* as the user specified a bus, we must match it up */
480 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
481 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
486 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
487 pair = &dev->sysfs_pair[i];
488 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
490 if (match_pair(class_dev, sysfs_device, pair) != 0) {
499 strfieldcpy(udev->name, dev->name);
500 strfieldcpy(udev->symlink, dev->symlink);
501 dbg("found matching attribute, '%s' becomes '%s' ",
502 class_dev->name, udev->name);
509 static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
511 struct config_device *dev;
512 struct list_head *tmp;
513 char path[SYSFS_PATH_MAX];
517 /* we have to have a sysfs device for NUMBER to work */
521 list_for_each(tmp, &config_device_list) {
522 dev = list_entry(tmp, struct config_device, node);
523 if (dev->type != NUMBER)
526 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
527 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
531 strfieldcpy(path, sysfs_device->path);
532 temp = strrchr(path, '/');
533 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
534 if (strstr(temp, dev->id) != NULL) {
538 temp = strrchr(path, '/');
539 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
540 if (strstr(temp, dev->id) != NULL)
545 strfieldcpy(udev->name, dev->name);
546 strfieldcpy(udev->symlink, dev->symlink);
547 dbg("found matching id '%s', '%s' becomes '%s'",
548 dev->id, class_dev->name, udev->name);
554 static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
556 struct config_device *dev;
557 struct list_head *tmp;
558 char path[SYSFS_PATH_MAX];
562 /* we have to have a sysfs device for TOPOLOGY to work */
566 list_for_each(tmp, &config_device_list) {
567 dev = list_entry(tmp, struct config_device, node);
568 if (dev->type != TOPOLOGY)
571 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
572 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
576 strfieldcpy(path, sysfs_device->path);
577 temp = strrchr(path, '/');
578 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
579 if (strstr(temp, dev->place) != NULL) {
583 temp = strrchr(path, '/');
584 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
585 if (strstr(temp, dev->place) != NULL)
591 strfieldcpy(udev->name, dev->name);
592 strfieldcpy(udev->symlink, dev->symlink);
593 dbg("found matching place '%s', '%s' becomes '%s'",
594 dev->place, class_dev->name, udev->name);
600 static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
602 struct config_device *dev;
603 struct list_head *tmp;
605 list_for_each(tmp, &config_device_list) {
606 dev = list_entry(tmp, struct config_device, node);
607 if (dev->type != REPLACE)
610 dbg("compare name '%s' with '%s'", dev->kernel_name, class_dev->name);
611 if (strcmp_pattern(dev->kernel_name, class_dev->name) != 0)
614 strfieldcpy(udev->name, dev->name);
615 strfieldcpy(udev->symlink, dev->symlink);
616 dbg("found name, '%s' becomes '%s'", dev->kernel_name, udev->name);
623 static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev)
625 /* heh, this is pretty simple... */
626 strfieldcpy(udev->name, class_dev->name);
629 static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev)
631 struct sysfs_device *sysfs_device;
632 struct sysfs_class_device *class_dev_parent;
634 char filename[SYSFS_PATH_MAX + 6];
639 /* Figure out where the device symlink is at. For char devices this will
640 * always be in the class_dev->path. But for block devices, it's different.
641 * The main block device will have the device symlink in it's path, but
642 * all partitions have the symlink in its parent directory.
643 * But we need to watch out for block devices that do not have parents, yet
644 * look like a partition (fd0, loop0, etc.) They all do not have a device
645 * symlink yet. We do sit and spin on waiting for them right now, we should
646 * possibly have a whitelist for these devices here...
648 strcpy(filename, class_dev->path);
649 dbg("filename = %s", filename);
650 if (strcmp(class_dev->classname, SYSFS_BLOCK_NAME) == 0) {
651 if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
652 temp = strrchr(filename, '/');
656 char *temp2 = strrchr(filename, '/');
657 dbg("temp2 = %s", temp2);
658 if (temp2 && (strcmp(temp2, "/block") == 0)) {
659 /* oops, we have no parent block device, so go back to original directory */
660 strcpy(filename, class_dev->path);
666 strcat(filename, "/device");
671 dbg("looking for '%s'", filename);
672 retval = stat(filename, &buf);
675 /* sleep to give the kernel a chance to create the device file */
679 loop = 1; /* FIXME put a real value in here for when everything is fixed... */
681 /* find the sysfs_device for this class device */
682 /* Wouldn't it really be nice if libsysfs could do this for us? */
683 sysfs_device = sysfs_get_classdev_device(class_dev);
684 if (sysfs_device != NULL)
687 /* if it's a partition, we need to get the parent device */
689 /* FIXME HACK HACK HACK HACK
690 * for some reason partitions need this extra sleep here, in order
691 * to wait for the device properly. Once the libsysfs code is
692 * fixed properly, this sleep should go away, and we can just loop above.
695 dbg("really is a partition");
696 class_dev_parent = sysfs_get_classdev_parent(class_dev);
697 if (class_dev_parent == NULL) {
698 dbg("sysfs_get_classdev_parent for class device '%s' failed", class_dev->name);
700 dbg("class_dev_parent->name='%s'", class_dev_parent->name);
701 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
702 if (sysfs_device != NULL)
706 /* sleep to give the kernel a chance to create the link */
707 /* FIXME remove comment...
710 dbg("Timed out waiting for device symlink, continuing on anyway...");
715 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
717 struct sysfs_device *sysfs_device = NULL;
719 struct perm_device *perm;
724 /* find the sysfs_device associated with this class device */
725 sysfs_device = get_sysfs_device(class_dev);
727 dbg("sysfs_device->path='%s'", sysfs_device->path);
728 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
729 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
730 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
731 wait_for_device_to_initialize(sysfs_device);
733 dbg("class_dev->name = '%s'", class_dev->name);
736 strfieldcpy(udev->kernel_name, class_dev->name);
738 /* get kernel number */
739 pos = class_dev->name + strlen(class_dev->name);
740 while (isdigit(*(pos-1)))
742 strfieldcpy(udev->kernel_number, pos);
743 dbg("kernel_number='%s'", udev->kernel_number);
745 /* rules are looked at in priority order */
746 retval = do_callout(class_dev, udev, sysfs_device);
750 retval = do_label(class_dev, udev, sysfs_device);
754 retval = do_number(class_dev, udev, sysfs_device);
758 retval = do_topology(class_dev, udev, sysfs_device);
762 retval = do_replace(class_dev, udev, sysfs_device);
766 do_kernelname(class_dev, udev);
770 /* substitute placeholder */
771 apply_format(udev, udev->name);
772 apply_format(udev, udev->symlink);
775 perm = find_perm(udev->name);
777 udev->mode = perm->mode;
778 strfieldcpy(udev->owner, perm->owner);
779 strfieldcpy(udev->group, perm->group);
781 /* no matching perms found :( */
782 udev->mode = get_default_mode(class_dev);
783 udev->owner[0] = 0x00;
784 udev->group[0] = 0x00;
786 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
787 udev->name, udev->owner, udev->group, udev->mode);
792 int namedev_init(void)
796 retval = namedev_init_rules();
800 retval = namedev_init_permissions();
804 dump_config_dev_list();
805 dump_perm_dev_list();