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.
36 #include "libsysfs/sysfs/libsysfs.h"
39 #include "udev_version.h"
42 #include "klibc_fixups.h"
44 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr);
46 LIST_HEAD(config_device_list);
47 LIST_HEAD(perm_device_list);
49 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
50 static int strcmp_pattern(const char *p, const char *s)
55 return (p[0] != '\0');
66 while ((p[0] != '\0') && (p[0] != ']')) {
69 if ((s[0] >= p[0]) && (s[0] <= p[2]))
73 match = (p[0] == s[0]);
77 while ((p[0] != '\0') && (p[0] != ']'))
80 return strcmp_pattern(p+1, s+1);
86 if (strcmp_pattern(p, s+1))
87 return strcmp_pattern(p+1, s);
95 if ((p[0] == s[0]) || (p[0] == '?'))
96 return strcmp_pattern(p+1, s+1);
102 #define copy_var(a, b, var) \
106 #define copy_string(a, b, var) \
107 if (strlen(b->var)) \
108 strcpy(a->var, b->var);
110 int add_perm_dev(struct perm_device *new_dev)
112 struct perm_device *dev;
113 struct perm_device *tmp_dev;
115 /* update the values if we already have the device */
116 list_for_each_entry(dev, &perm_device_list, node) {
117 if (strcmp(new_dev->name, dev->name))
119 copy_var(dev, new_dev, mode);
120 copy_string(dev, new_dev, owner);
121 copy_string(dev, new_dev, group);
125 /* not found, add new structure to the perm list */
126 tmp_dev = malloc(sizeof(*tmp_dev));
129 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
130 list_add_tail(&tmp_dev->node, &perm_device_list);
131 //dump_perm_dev(tmp_dev);
135 static struct perm_device *find_perm(char *name)
137 struct perm_device *perm;
139 list_for_each_entry(perm, &perm_device_list, node) {
140 if (strcmp_pattern(perm->name, name))
147 static mode_t get_default_mode(void)
149 mode_t mode = 0600; /* default to owner rw only */
151 if (strlen(default_mode_str) != 0)
152 mode = strtol(default_mode_str, NULL, 8);
157 static char *get_default_owner(void)
159 if (strlen(default_owner_str) == 0)
160 strfieldcpy(default_owner_str, "root");
162 return default_owner_str;
165 static char *get_default_group(void)
167 if (strlen(default_group_str) == 0)
168 strfieldcpy(default_group_str, "root");
170 return default_group_str;
173 /* extract possible {attr} and move str behind it */
174 static char *get_format_attribute(char **str)
179 if (*str[0] == '{') {
180 pos = strchr(*str, '}');
182 dbg("missing closing brace for format");
188 dbg("attribute='%s', str='%s'", attr, *str);
193 /* extract possible format length and move str behind it*/
194 static int get_format_len(char **str)
199 if (isdigit(*str[0])) {
200 num = (int) strtoul(*str, &tail, 10);
203 dbg("format length=%i", num);
206 dbg("format parsing error '%s'", *str);
212 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
213 struct sysfs_class_device *class_dev,
214 struct sysfs_device *sysfs_device)
216 char temp[NAME_SIZE];
217 char temp1[NAME_SIZE];
226 struct sysfs_attribute *tmpattr;
231 pos = strchr(string, '%');
235 num = get_format_len(&tail);
237 strfieldcpy(temp, tail+1);
242 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
244 attr = get_format_attribute(&tail);
248 if (strlen(udev->bus_id) == 0)
250 strnfieldcat(string, udev->bus_id, maxsize);
251 dbg("substitute bus_id '%s'", udev->bus_id);
254 if (strlen(udev->kernel_name) == 0)
256 strnfieldcat(string, udev->kernel_name, maxsize);
257 dbg("substitute kernel name '%s'", udev->kernel_name);
260 if (strlen(udev->kernel_number) == 0)
262 strnfieldcat(string, udev->kernel_number, maxsize);
263 dbg("substitute kernel number '%s'", udev->kernel_number);
266 sprintf(pos, "%u", udev->minor);
267 dbg("substitute minor number '%u'", udev->minor);
270 sprintf(pos, "%u", udev->major);
271 dbg("substitute major number '%u'", udev->major);
274 if (strlen(udev->program_result) == 0)
276 /* get part part of the result string */
277 i = num; /* num syntax is deprecated and will be removed */
281 strfieldcpy(temp1, udev->program_result);
285 pos3 = strsep(&pos2, " ");
287 dbg("requested part of result string not found");
292 strnfieldcat(string, pos3, maxsize);
293 dbg("substitute part of result string '%s'", pos3);
296 strnfieldcat(string, udev->program_result, maxsize);
297 dbg("substitute result string '%s'", udev->program_result);
302 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr);
303 if (tmpattr == NULL) {
304 dbg("sysfa attribute '%s' not found", attr);
307 strnfieldcat(string, tmpattr->value, maxsize);
308 dbg("substitute sysfs value '%s'", tmpattr->value);
310 dbg("missing attribute");
314 strnfieldcat(string, "%", maxsize);
317 dbg("unknown substitution type '%%%c'", c);
320 strnfieldcat(string, tail, maxsize);
325 * Note, we can have multiple files for different busses in here due
326 * to the mess that USB has for its device tree...
328 static struct bus_file {
332 { .bus = "scsi", .file = "vendor" },
333 { .bus = "usb", .file = "idVendor" },
334 { .bus = "usb", .file = "iInterface" },
335 { .bus = "usb-serial", .file = "detach_state" },
336 { .bus = "ide", .file = "detach_state" },
337 { .bus = "pci", .file = "vendor" },
341 #define SECONDS_TO_WAIT_FOR_FILE 10
342 static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device)
344 /* sleep until we see the file for this specific bus type show up this
345 * is needed because we can easily out-run the kernel in looking for
346 * these files before the paticular subsystem has created them in the
347 * sysfs tree properly.
349 * And people thought that the /sbin/hotplug event system was going to
350 * be slow, poo on you for arguing that before even testing it...
352 struct bus_file *b = &bus_files[0];
353 struct sysfs_attribute *tmpattr;
355 int loop = SECONDS_TO_WAIT_FOR_FILE;
358 if (b->bus == NULL) {
361 /* sleep to give the kernel a chance to create the file */
368 if (strcmp(sysfs_device->bus, b->bus) == 0) {
370 dbg("looking for file '%s' on bus '%s'", b->file, b->bus);
371 tmpattr = sysfs_get_device_attr(sysfs_device, b->file);
376 dbg("can't find '%s' file", b->file);
381 dbg("did not find bus type '%s' on list of bus_id_files, "
382 "contact greg@kroah.com", sysfs_device->bus);
384 return; /* here to prevent compiler warning... */
387 static void fix_kernel_name(struct udevice *udev)
389 char *temp = udev->kernel_name;
391 while (*temp != 0x00) {
392 /* Some block devices have a ! in their name,
393 * we need to change that to / */
400 static int execute_program(char *path, char *value, int len)
410 char *args[PROGRAM_MAXARG];
413 dbg("executing '%s'", path);
423 close(STDOUT_FILENO);
424 dup(fds[1]); /* dup write side of pipe to STDOUT */
425 if (strchr(path, ' ')) {
426 /* exec with arguments */
428 for (i=0; i < PROGRAM_MAXARG-1; i++) {
429 args[i] = strsep(&pos, " ");
434 dbg("too many args - %d", i);
437 retval = execv(args[0], args);
439 retval = execv(path, main_argv);
441 dbg("child execve failed");
447 /* parent reads from fds[0] */
451 res = read(fds[0], buffer, sizeof(buffer) - 1);
456 dbg("result len %d too short", len);
460 dbg("result value already set");
464 strncpy(value, buffer, len);
465 pos = value + strlen(value)-1;
468 dbg("result is '%s'", value);
474 dbg("wait failed result %d", res);
478 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
479 dbg("exec program status 0x%x", status);
486 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
488 struct sysfs_attribute *tmpattr = NULL;
491 dbg("look for device attribute '%s'", attr);
492 /* try to find the attribute in the class device directory */
493 tmpattr = sysfs_get_classdev_attr(class_dev, attr);
497 /* look in the class device directory if present */
499 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
507 c = strchr(tmpattr->value, '\n');
511 dbg("found attribute '%s'", tmpattr->path);
515 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
517 struct sysfs_attribute *tmpattr;
519 if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
522 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file);
526 dbg("compare attribute '%s' value '%s' with '%s'",
527 pair->file, tmpattr->value, pair->value);
528 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
531 dbg("found matching attribute '%s' with value '%s'",
532 pair->file, pair->value);
536 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
538 struct sysfs_pair *pair;
541 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
542 pair = &dev->sysfs_pair[i];
543 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
545 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
546 dbg("sysfs attribute doesn't match");
554 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
556 char path[SYSFS_PATH_MAX];
559 /* we have to have a sysfs device for ID to work */
563 strfieldcpy(path, sysfs_device->path);
564 temp = strrchr(path, '/');
566 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
567 if (strcmp_pattern(dev->id, temp) != 0)
573 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
575 char path[SYSFS_PATH_MAX];
579 /* we have to have a sysfs device for PLACE to work */
584 strfieldcpy(path, sysfs_device->path);
585 temp = strrchr(path, '/');
586 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
587 if (strstr(temp, dev->place) != NULL) {
591 temp = strrchr(path, '/');
592 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
593 if (strstr(temp, dev->place) != NULL)
597 dbg("place doesn't match");
604 static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev)
606 struct sysfs_device *sysfs_device;
607 struct sysfs_class_device *class_dev_parent;
608 struct timespec tspec;
611 /* Figure out where the device symlink is at. For char devices this will
612 * always be in the class_dev->path. But for block devices, it's different.
613 * The main block device will have the device symlink in it's path, but
614 * all partitions have the symlink in its parent directory.
615 * But we need to watch out for block devices that do not have parents, yet
616 * look like a partition (fd0, loop0, etc.) They all do not have a device
617 * symlink yet. We do sit and spin on waiting for them right now, we should
618 * possibly have a whitelist for these devices here...
620 class_dev_parent = sysfs_get_classdev_parent(class_dev);
621 if (class_dev_parent)
622 dbg("Really a partition");
625 tspec.tv_nsec = 10000000; /* sleep 10 millisec */
629 nanosleep(&tspec, NULL);
630 if (class_dev_parent)
631 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
633 sysfs_device = sysfs_get_classdev_device(class_dev);
635 if (sysfs_device != NULL)
638 dbg("timed out waiting for device symlink, continuing on anyway...");
641 /* We have another issue with just the wait above - the sysfs part of
642 * the kernel may not be quick enough to have created the link to the
643 * device under the "bus" subsystem. Due to this, the sysfs_device->bus
644 * will not contain the actual bus name :(
646 * Libsysfs now provides a new API sysfs_get_device_bus(), so use it
650 if (sysfs_device->bus[0] != '\0')
654 tspec.tv_nsec = 10000000;
657 nanosleep(&tspec, NULL);
658 sysfs_get_device_bus(sysfs_device);
660 if (sysfs_device->bus[0] != '\0')
663 dbg("timed out waiting to find the device bus, continuing on anyway");
666 dbg("device %s is registered with bus '%s'",
667 sysfs_device->name, sysfs_device->bus);
673 static int match_rule(struct config_device *dev, struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
676 /* check for matching bus value */
677 if (dev->bus[0] != '\0') {
678 if (sysfs_device == NULL) {
679 dbg("device has no bus");
682 dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
683 if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
684 dbg(FIELD_BUS " is not matching");
687 dbg(FIELD_BUS " matches");
691 /* check for matching kernel name*/
692 if (dev->kernel[0] != '\0') {
693 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'", dev->kernel, class_dev->name);
694 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
695 dbg(FIELD_KERNEL " is not matching");
698 dbg(FIELD_KERNEL " matches");
702 /* check for matching bus id */
703 if (dev->id[0] != '\0') {
704 dbg("check " FIELD_ID);
705 if (match_id(dev, class_dev, sysfs_device) != 0) {
706 dbg(FIELD_ID " is not matching");
709 dbg(FIELD_ID " matches");
713 /* check for matching place of device */
714 if (dev->place[0] != '\0') {
715 dbg("check " FIELD_PLACE);
716 if (match_place(dev, class_dev, sysfs_device) != 0) {
717 dbg(FIELD_PLACE " is not matching");
720 dbg(FIELD_PLACE " matches");
724 /* check for matching sysfs pairs */
725 if (dev->sysfs_pair[0].file[0] != '\0') {
726 dbg("check " FIELD_SYSFS " pairs");
727 if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
728 dbg(FIELD_SYSFS " is not matching");
731 dbg(FIELD_SYSFS " matches");
735 /* execute external program */
736 if (dev->program[0] != '\0') {
737 dbg("check " FIELD_PROGRAM);
738 apply_format(udev, dev->program, sizeof(dev->program),
739 class_dev, sysfs_device);
740 if (execute_program(dev->program, udev->program_result, NAME_SIZE) != 0) {
741 dbg(FIELD_PROGRAM " returned nozero");
744 dbg(FIELD_PROGRAM " returned successful");
748 /* check for matching result of external program */
749 if (dev->result[0] != '\0') {
750 dbg("check for " FIELD_RESULT
751 " dev->result='%s', udev->program_result='%s'",
752 dev->result, udev->program_result);
753 if (strcmp_pattern(dev->result, udev->program_result) != 0) {
754 dbg(FIELD_RESULT " is not matching");
757 dbg(FIELD_RESULT " matches");
761 /* Yeah, we matched! */
765 dbg("try parent sysfs device");
766 sysfs_device = sysfs_get_device_parent(sysfs_device);
767 if (sysfs_device == NULL)
769 dbg("sysfs_device->path='%s'", sysfs_device->path);
770 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
771 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
776 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
778 struct sysfs_device *sysfs_device = NULL;
779 struct config_device *dev;
780 struct perm_device *perm;
785 /* find the sysfs_device associated with this class device */
786 sysfs_device = get_sysfs_device(class_dev);
788 dbg("sysfs_device->path='%s'", sysfs_device->path);
789 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
790 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
791 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
792 wait_for_device_to_initialize(sysfs_device);
794 dbg("class_dev->name = '%s'", class_dev->name);
796 strfieldcpy(udev->kernel_name, class_dev->name);
797 fix_kernel_name(udev);
798 dbg("udev->kernel_name = '%s'", udev->kernel_name);
800 /* get kernel number */
801 pos = class_dev->name + strlen(class_dev->name);
802 while (isdigit(*(pos-1)))
804 strfieldcpy(udev->kernel_number, pos);
805 dbg("kernel_number='%s'", udev->kernel_number);
807 /* look for a matching rule to apply */
808 list_for_each_entry(dev, &config_device_list, node) {
810 if (match_rule(dev, class_dev, udev, sysfs_device) == 0) {
811 if (dev->name[0] == '\0') {
812 info("configured rule in '%s' at line %i applied, '%s' is ignored",
813 udev_rules_filename, dev->config_line, udev->kernel_name);
817 info("configured rule in '%s' at line %i applied, '%s' becomes '%s'",
818 udev_rules_filename, dev->config_line, udev->kernel_name, dev->name);
819 strfieldcpy(udev->name, dev->name);
820 strfieldcpy(udev->symlink, dev->symlink);
825 /* no rule was found so we use the kernel name */
826 strfieldcpy(udev->name, udev->kernel_name);
830 /* substitute placeholder */
831 apply_format(udev, udev->name, sizeof(udev->name),
832 class_dev, sysfs_device);
833 apply_format(udev, udev->symlink, sizeof(udev->symlink),
834 class_dev, sysfs_device);
835 udev->partitions = dev->partitions;
837 perm = find_perm(udev->name);
839 udev->mode = perm->mode;
840 strfieldcpy(udev->owner, perm->owner);
841 strfieldcpy(udev->group, perm->group);
843 /* no matching perms found :( */
844 udev->mode = get_default_mode();
845 strfieldcpy(udev->owner, get_default_owner());
846 strfieldcpy(udev->group, get_default_group());
848 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
849 udev->name, udev->owner, udev->group, udev->mode);
854 int namedev_init(void)
858 retval = namedev_init_rules();
862 retval = namedev_init_permissions();
866 dump_config_dev_list();
867 dump_perm_dev_list();