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 perm_device *dev;
108 struct perm_device *tmp_dev;
110 /* update the values if we already have the device */
111 list_for_each_entry(dev, &perm_device_list, node) {
112 if (strcmp_pattern(new_dev->name, dev->name))
114 copy_var(dev, new_dev, mode);
115 copy_string(dev, new_dev, owner);
116 copy_string(dev, new_dev, group);
120 /* not found, add new structure to the perm list */
121 tmp_dev = malloc(sizeof(*tmp_dev));
124 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
125 list_add_tail(&tmp_dev->node, &perm_device_list);
126 //dump_perm_dev(tmp_dev);
130 static struct perm_device *find_perm(char *name)
132 struct perm_device *perm;
134 list_for_each_entry(perm, &perm_device_list, node) {
135 if (strcmp_pattern(perm->name, name))
142 static mode_t get_default_mode(struct sysfs_class_device *class_dev)
144 mode_t mode = 0600; /* default to owner rw only */
146 if (strlen(default_mode_str) != 0) {
147 mode = strtol(default_mode_str, NULL, 8);
152 static void apply_format(struct udevice *udev, unsigned char *string)
154 char temp[NAME_SIZE];
155 char temp1[NAME_SIZE];
165 pos = strchr(pos, '%');
170 if (isdigit(tail[0])) {
171 num = (int) strtoul(&pos[1], &tail, 10);
173 dbg("format parsing error '%s'", pos+1);
177 strfieldcpy(temp, tail+1);
181 if (strlen(udev->bus_id) == 0)
183 strcat(pos, udev->bus_id);
184 dbg("substitute bus_id '%s'", udev->bus_id);
187 if (strlen(udev->kernel_name) == 0)
189 strcat(pos, udev->kernel_name);
190 dbg("substitute kernel name '%s'", udev->kernel_name);
193 if (strlen(udev->kernel_number) == 0)
195 strcat(pos, udev->kernel_number);
196 dbg("substitute kernel number '%s'", udev->kernel_number);
199 if (strlen(udev->kernel_number) == 0) {
201 dbg("substitute devfs disc");
205 strcat(pos, udev->kernel_number);
206 dbg("substitute devfs part '%s'", udev->kernel_number);
209 sprintf(pos, "%u", udev->minor);
210 dbg("substitute minor number '%u'", udev->minor);
213 sprintf(pos, "%u", udev->major);
214 dbg("substitute major number '%u'", udev->major);
217 if (strlen(udev->program_result) == 0)
220 /* get part of return string */
221 strncpy(temp1, udev->program_result, sizeof(temp1));
225 pos3 = strsep(&pos2, " ");
227 dbg("requested part of result string not found");
233 dbg("substitute part of result string '%s'", pos3);
236 strcat(pos, udev->program_result);
237 dbg("substitute result string '%s'", udev->program_result);
245 dbg("unknown substitution type '%%%c'", pos[1]);
248 strcat(string, temp);
254 static struct bus_file {
258 { .bus = "scsi", .file = "vendor" },
259 { .bus = "usb", .file = "idVendor" },
260 { .bus = "usb-serial", .file = "detach_state" },
261 { .bus = "ide", .file = "detach_state" },
262 { .bus = "pci", .file = "vendor" },
266 #define SECONDS_TO_WAIT_FOR_FILE 10
267 static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device)
269 /* sleep until we see the file for this specific bus type show up this
270 * is needed because we can easily out-run the kernel in looking for
271 * these files before the paticular subsystem has created them in the
272 * sysfs tree properly.
274 * And people thought that the /sbin/hotplug event system was going to
275 * be slow, poo on you for arguing that before even testing it...
277 struct bus_file *b = &bus_files[0];
278 struct sysfs_attribute *tmpattr;
284 if (strcmp(sysfs_device->bus, b->bus) == 0) {
286 loop = SECONDS_TO_WAIT_FOR_FILE;
288 dbg("looking for file '%s' on bus '%s'", b->file, b->bus);
289 tmpattr = sysfs_get_device_attr(sysfs_device, b->file);
294 /* sleep to give the kernel a chance to create the file */
297 dbg("Timed out waiting for '%s' file, continuing on anyway...", b->file);
302 dbg("Did not find bus type '%s' on list of bus_id_files, contact greg@kroah.com", sysfs_device->bus);
304 return; /* here to prevent compiler warning... */
307 static int execute_program(char *path, char *value, int len)
317 char *args[PROGRAM_MAXARG];
320 dbg("executing '%s'", path);
334 close(STDOUT_FILENO);
335 dup(fds[1]); /* dup write side of pipe to STDOUT */
336 if (strchr(path, ' ')) {
337 /* exec with arguments */
339 for (i=0; i < PROGRAM_MAXARG-1; i++) {
340 args[i] = strsep(&pos, " ");
345 dbg("too many args - %d", i);
348 retval = execve(args[0], args, main_envp);
350 retval = execve(path, main_argv, main_envp);
353 dbg("child execve failed");
356 return -1; /* avoid compiler warning */
358 /* parent reads from fds[0] */
362 res = read(fds[0], buffer, sizeof(buffer) - 1);
367 dbg("result len %d too short", len);
371 dbg("result value already set");
375 strncpy(value, buffer, len);
376 pos = value + strlen(value)-1;
379 dbg("result is '%s'", value);
385 dbg("wait failed result %d", res);
389 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
390 dbg("exec program status 0x%x", status);
397 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
399 struct sysfs_attribute *tmpattr = NULL;
402 if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
405 dbg("look for device attribute '%s'", pair->file);
406 /* try to find the attribute in the class device directory */
407 tmpattr = sysfs_get_classdev_attr(class_dev, pair->file);
411 /* look in the class device directory if present */
413 tmpattr = sysfs_get_device_attr(sysfs_device, pair->file);
420 c = tmpattr->value + strlen(tmpattr->value)-1;
423 dbg("compare attribute '%s' value '%s' with '%s'",
424 pair->file, tmpattr->value, pair->value);
425 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
428 dbg("found matching attribute '%s' with value '%s'",
429 pair->file, pair->value);
433 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
435 struct sysfs_pair *pair;
438 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
439 pair = &dev->sysfs_pair[i];
440 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
442 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
443 dbg("sysfs attribute doesn't match");
451 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
453 char path[SYSFS_PATH_MAX];
457 /* we have to have a sysfs device for ID to work */
462 strfieldcpy(path, sysfs_device->path);
463 temp = strrchr(path, '/');
464 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
465 if (strstr(temp, dev->id) != NULL) {
469 temp = strrchr(path, '/');
470 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
471 if (strstr(temp, dev->id) != NULL)
475 dbg("id doesn't match");
482 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
484 char path[SYSFS_PATH_MAX];
488 /* we have to have a sysfs device for PLACE to work */
493 strfieldcpy(path, sysfs_device->path);
494 temp = strrchr(path, '/');
495 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
496 if (strstr(temp, dev->place) != NULL) {
500 temp = strrchr(path, '/');
501 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
502 if (strstr(temp, dev->place) != NULL)
506 dbg("place doesn't match");
513 static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev)
515 struct sysfs_device *sysfs_device;
516 struct sysfs_class_device *class_dev_parent;
518 char filename[SYSFS_PATH_MAX + 6];
523 /* Figure out where the device symlink is at. For char devices this will
524 * always be in the class_dev->path. But for block devices, it's different.
525 * The main block device will have the device symlink in it's path, but
526 * all partitions have the symlink in its parent directory.
527 * But we need to watch out for block devices that do not have parents, yet
528 * look like a partition (fd0, loop0, etc.) They all do not have a device
529 * symlink yet. We do sit and spin on waiting for them right now, we should
530 * possibly have a whitelist for these devices here...
532 strcpy(filename, class_dev->path);
533 dbg("filename = %s", filename);
534 if (strcmp(class_dev->classname, SYSFS_BLOCK_NAME) == 0) {
535 if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
536 temp = strrchr(filename, '/');
538 char *temp2 = strrchr(filename, '/');
541 dbg("temp2 = %s", temp2);
542 if (temp2 && (strcmp(temp2, "/block") == 0)) {
543 /* oops, we have no parent block device, so go back to original directory */
544 strcpy(filename, class_dev->path);
550 strcat(filename, "/device");
555 dbg("looking for '%s'", filename);
556 retval = stat(filename, &buf);
559 /* sleep to give the kernel a chance to create the device file */
563 loop = 1; /* FIXME put a real value in here for when everything is fixed... */
565 /* find the sysfs_device for this class device */
566 /* Wouldn't it really be nice if libsysfs could do this for us? */
567 sysfs_device = sysfs_get_classdev_device(class_dev);
568 if (sysfs_device != NULL)
571 /* if it's a partition, we need to get the parent device */
573 /* FIXME HACK HACK HACK HACK
574 * for some reason partitions need this extra sleep here, in order
575 * to wait for the device properly. Once the libsysfs code is
576 * fixed properly, this sleep should go away, and we can just loop above.
579 dbg("really is a partition");
580 class_dev_parent = sysfs_get_classdev_parent(class_dev);
581 if (class_dev_parent == NULL) {
582 dbg("sysfs_get_classdev_parent for class device '%s' failed", class_dev->name);
584 dbg("class_dev_parent->name='%s'", class_dev_parent->name);
585 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
586 if (sysfs_device != NULL)
590 /* sleep to give the kernel a chance to create the link */
591 /* FIXME remove comment...
594 dbg("Timed out waiting for device symlink, continuing on anyway...");
599 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
601 struct sysfs_device *sysfs_device = NULL;
602 struct config_device *dev;
603 struct perm_device *perm;
608 /* find the sysfs_device associated with this class device */
609 sysfs_device = get_sysfs_device(class_dev);
611 dbg("sysfs_device->path='%s'", sysfs_device->path);
612 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
613 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
614 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
615 wait_for_device_to_initialize(sysfs_device);
617 dbg("class_dev->name = '%s'", class_dev->name);
620 strfieldcpy(udev->kernel_name, class_dev->name);
622 /* get kernel number */
623 pos = class_dev->name + strlen(class_dev->name);
624 while (isdigit(*(pos-1)))
626 strfieldcpy(udev->kernel_number, pos);
627 dbg("kernel_number='%s'", udev->kernel_number);
629 /* look for a matching rule to apply */
630 list_for_each_entry(dev, &config_device_list, node) {
633 /* check for matching bus value */
634 if (dev->bus[0] != '\0') {
635 if (sysfs_device == NULL) {
636 dbg("device has no bus");
639 dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
640 if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
641 dbg(FIELD_BUS " is not matching");
644 dbg(FIELD_BUS " matches");
648 /* check for matching kernel name*/
649 if (dev->kernel[0] != '\0') {
650 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'", dev->kernel, class_dev->name);
651 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
652 dbg(FIELD_KERNEL " is not matching");
655 dbg(FIELD_KERNEL " matches");
659 /* check for matching bus id */
660 if (dev->id[0] != '\0') {
661 dbg("check " FIELD_ID);
662 if (match_id(dev, class_dev, sysfs_device) != 0) {
663 dbg(FIELD_ID " is not matching");
666 dbg(FIELD_ID " matches");
670 /* check for matching place of device */
671 if (dev->place[0] != '\0') {
672 dbg("check " FIELD_PLACE);
673 if (match_place(dev, class_dev, sysfs_device) != 0) {
674 dbg(FIELD_PLACE " is not matching");
677 dbg(FIELD_PLACE " matches");
681 /* check for matching sysfs pairs */
682 if (dev->sysfs_pair[0].file[0] != '\0') {
683 dbg("check " FIELD_SYSFS " pairs");
684 if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
685 dbg(FIELD_SYSFS " is not matching");
688 dbg(FIELD_SYSFS " matches");
692 /* execute external program */
693 if (dev->program[0] != '\0') {
694 dbg("check " FIELD_PROGRAM);
695 apply_format(udev, dev->program);
696 if (execute_program(dev->program, udev->program_result, NAME_SIZE) != 0) {
697 dbg(FIELD_PROGRAM " returned nozero");
700 dbg(FIELD_PROGRAM " returned successful");
704 /* check for matching result of external program */
705 if (dev->result[0] != '\0') {
706 dbg("check for " FIELD_RESULT
707 " dev->result='%s', udev->program_result='%s'",
708 dev->result, udev->program_result);
709 if (strcmp_pattern(dev->result, udev->program_result) != 0) {
710 dbg(FIELD_RESULT " is not matching");
713 dbg(FIELD_RESULT " matches");
717 /* check if we are instructed to ignore this device */
718 if (dev->name[0] == '\0') {
719 dbg("instructed to ignore this device");
723 /* Yup, this rule belongs to us! */
724 dbg("found matching rule, '%s' becomes '%s'", dev->kernel, dev->name);
725 strfieldcpy(udev->name, dev->name);
726 strfieldcpy(udev->symlink, dev->symlink);
730 /* no rule was found so we use the kernel name */
731 strfieldcpy(udev->name, class_dev->name);
735 /* substitute placeholder */
736 apply_format(udev, udev->name);
737 apply_format(udev, udev->symlink);
740 perm = find_perm(udev->name);
742 udev->mode = perm->mode;
743 strfieldcpy(udev->owner, perm->owner);
744 strfieldcpy(udev->group, perm->group);
746 /* no matching perms found :( */
747 udev->mode = get_default_mode(class_dev);
748 udev->owner[0] = 0x00;
749 udev->group[0] = 0x00;
751 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
752 udev->name, udev->owner, udev->group, udev->mode);
757 int namedev_init(void)
761 retval = namedev_init_rules();
765 retval = namedev_init_permissions();
769 dump_config_dev_list();
770 dump_perm_dev_list();