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);
50 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
51 static int strcmp_pattern(const char *p, const char *s)
56 return (p[0] != '\0');
67 while ((p[0] != '\0') && (p[0] != ']')) {
70 if ((s[0] >= p[0]) && (s[0] <= p[2]))
74 match = (p[0] == s[0]);
78 while ((p[0] != '\0') && (p[0] != ']'))
81 return strcmp_pattern(p+1, s+1);
87 if (strcmp_pattern(p, s+1))
88 return strcmp_pattern(p+1, s);
96 if ((p[0] == s[0]) || (p[0] == '?'))
97 return strcmp_pattern(p+1, s+1);
103 static struct perm_device *find_perm(char *name)
105 struct perm_device *perm;
107 list_for_each_entry(perm, &perm_device_list, node) {
108 if (strcmp_pattern(perm->name, name))
115 static mode_t get_default_mode(void)
117 mode_t mode = 0600; /* default to owner rw only */
119 if (strlen(default_mode_str) != 0)
120 mode = strtol(default_mode_str, NULL, 8);
125 static char *get_default_owner(void)
127 if (strlen(default_owner_str) == 0)
128 strfieldcpy(default_owner_str, "root");
130 return default_owner_str;
133 static char *get_default_group(void)
135 if (strlen(default_group_str) == 0)
136 strfieldcpy(default_group_str, "root");
138 return default_group_str;
141 /* extract possible {attr} and move str behind it */
142 static char *get_format_attribute(char **str)
147 if (*str[0] == '{') {
148 pos = strchr(*str, '}');
150 dbg("missing closing brace for format");
156 dbg("attribute='%s', str='%s'", attr, *str);
161 /* extract possible format length and move str behind it*/
162 static int get_format_len(char **str)
167 if (isdigit(*str[0])) {
168 num = (int) strtoul(*str, &tail, 10);
171 dbg("format length=%i", num);
174 dbg("format parsing error '%s'", *str);
180 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
181 struct sysfs_class_device *class_dev,
182 struct sysfs_device *sysfs_device)
184 char temp[NAME_SIZE];
185 char temp2[NAME_SIZE];
194 struct sysfs_attribute *tmpattr;
199 pos = strchr(string, '%');
203 len = get_format_len(&tail);
205 strfieldcpy(temp, tail+1);
210 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
212 attr = get_format_attribute(&tail);
216 if (strlen(udev->bus_id) == 0)
218 strfieldcatmax(string, udev->bus_id, maxsize);
219 dbg("substitute bus_id '%s'", udev->bus_id);
222 if (strlen(udev->kernel_name) == 0)
224 strfieldcatmax(string, udev->kernel_name, maxsize);
225 dbg("substitute kernel name '%s'", udev->kernel_name);
228 if (strlen(udev->kernel_number) == 0)
230 strfieldcatmax(string, udev->kernel_number, maxsize);
231 dbg("substitute kernel number '%s'", udev->kernel_number);
234 strintcatmax(string, udev->minor, maxsize);
235 dbg("substitute minor number '%u'", udev->minor);
238 strintcatmax(string, udev->major, maxsize);
239 dbg("substitute major number '%u'", udev->major);
242 if (strlen(udev->program_result) == 0)
244 /* get part part of the result string */
249 foreach_strpart(udev->program_result, " \n\r", spos, slen) {
255 dbg("requested part of result string not found");
258 strfieldcpymax(temp2, spos, slen+1);
259 strfieldcatmax(string, temp2, maxsize);
260 dbg("substitute part of result string '%s'", temp2);
262 strfieldcatmax(string, udev->program_result, maxsize);
263 dbg("substitute result string '%s'", udev->program_result);
268 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr);
269 if (tmpattr == NULL) {
270 dbg("sysfa attribute '%s' not found", attr);
273 strfieldcatmax(string, tmpattr->value, maxsize);
274 dbg("substitute sysfs value '%s'", tmpattr->value);
276 dbg("missing attribute");
280 strfieldcatmax(string, "%", maxsize);
283 dbg("unknown substitution type '%%%c'", c);
286 /* truncate to specified length */
290 strfieldcatmax(string, tail, maxsize);
295 * Note, we can have multiple files for different busses in here due
296 * to the mess that USB has for its device tree...
298 static struct bus_file {
302 { .bus = "scsi", .file = "vendor" },
303 { .bus = "usb", .file = "idVendor" },
304 { .bus = "usb", .file = "iInterface" },
305 { .bus = "usb-serial", .file = "detach_state" },
306 { .bus = "ide", .file = "detach_state" },
307 { .bus = "pci", .file = "vendor" },
311 #define SECONDS_TO_WAIT_FOR_FILE 10
312 static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device)
314 /* sleep until we see the file for this specific bus type show up this
315 * is needed because we can easily out-run the kernel in looking for
316 * these files before the paticular subsystem has created them in the
317 * sysfs tree properly.
319 * And people thought that the /sbin/hotplug event system was going to
320 * be slow, poo on you for arguing that before even testing it...
322 struct bus_file *b = &bus_files[0];
323 struct sysfs_attribute *tmpattr;
325 int loop = SECONDS_TO_WAIT_FOR_FILE;
328 if (b->bus == NULL) {
331 /* sleep to give the kernel a chance to create the file */
338 if (strcmp(sysfs_device->bus, b->bus) == 0) {
340 dbg("looking for file '%s' on bus '%s'", b->file, b->bus);
341 tmpattr = sysfs_get_device_attr(sysfs_device, b->file);
346 dbg("can't find '%s' file", b->file);
351 dbg("did not find bus type '%s' on list of bus_id_files, "
352 "contact greg@kroah.com", sysfs_device->bus);
354 return; /* here to prevent compiler warning... */
357 static void fix_kernel_name(struct udevice *udev)
359 char *temp = udev->kernel_name;
361 while (*temp != 0x00) {
362 /* Some block devices have a ! in their name,
363 * we need to change that to / */
370 static int execute_program(char *path, char *value, int len)
378 char arg[PROGRAM_SIZE];
379 char *argv[sizeof(arg) / 2];
383 if (strchr(path, ' ')) {
384 strfieldcpy(arg, path);
386 while (pos != NULL) {
387 if (pos[0] == '\'') {
388 /* don't separate if in apostrophes */
390 argv[i] = strsep(&pos, "\'");
391 while (pos[0] == ' ')
394 argv[i] = strsep(&pos, " ");
396 dbg("arg[%i] '%s'", i, argv[i]);
412 close(STDOUT_FILENO);
414 /* dup write side of pipe to STDOUT */
416 if (argv[0] != NULL) {
417 dbg("execute '%s' with given arguments", argv[0]);
418 retval = execv(argv[0], argv);
420 dbg("execute '%s' with main argument", path);
421 retval = execv(path, main_argv);
424 info(FIELD_PROGRAM " execution of '%s' failed", path);
430 /* parent reads from fds[0] */
435 count = read(fds[0], value + i, len - i-1);
441 dbg("result len %d too short", len);
448 dbg("read failed with '%s'", strerror(errno));
452 if (i > 0 && value[i] == '\n')
455 dbg("result is '%s'", value);
460 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
461 dbg("exec program status 0x%x", status);
468 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
470 struct sysfs_attribute *tmpattr = NULL;
473 dbg("look for device attribute '%s'", attr);
474 /* try to find the attribute in the class device directory */
475 tmpattr = sysfs_get_classdev_attr(class_dev, attr);
479 /* look in the class device directory if present */
481 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
489 c = strchr(tmpattr->value, '\n');
493 dbg("found attribute '%s'", tmpattr->path);
497 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
499 struct sysfs_attribute *tmpattr;
503 if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
506 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file);
510 /* strip trailing whitespace of value, if not asked to match for it */
511 if (! isspace(pair->value[strlen(pair->value)-1])) {
512 i = len = strlen(tmpattr->value);
513 while (i > 0 && isspace(tmpattr->value[i-1]))
516 tmpattr->value[i] = '\0';
517 dbg("remove %i trailing whitespace chars from '%s'",
518 len - i, tmpattr->value);
522 dbg("compare attribute '%s' value '%s' with '%s'",
523 pair->file, tmpattr->value, pair->value);
524 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
527 dbg("found matching attribute '%s' with value '%s'",
528 pair->file, pair->value);
532 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
534 struct sysfs_pair *pair;
537 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
538 pair = &dev->sysfs_pair[i];
539 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
541 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
542 dbg("sysfs attribute doesn't match");
550 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
552 char path[SYSFS_PATH_MAX];
555 /* we have to have a sysfs device for ID to work */
559 strfieldcpy(path, sysfs_device->path);
560 temp = strrchr(path, '/');
562 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
563 if (strcmp_pattern(dev->id, temp) != 0)
569 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
571 char path[SYSFS_PATH_MAX];
575 /* we have to have a sysfs device for PLACE to work */
580 strfieldcpy(path, sysfs_device->path);
581 temp = strrchr(path, '/');
582 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
583 if (strstr(temp, dev->place) != NULL) {
587 temp = strrchr(path, '/');
588 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
589 if (strstr(temp, dev->place) != NULL)
593 dbg("place doesn't match");
600 static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev)
602 struct sysfs_device *sysfs_device;
603 struct sysfs_class_device *class_dev_parent;
604 struct timespec tspec;
607 /* Figure out where the device symlink is at. For char devices this will
608 * always be in the class_dev->path. But for block devices, it's different.
609 * The main block device will have the device symlink in it's path, but
610 * all partitions have the symlink in its parent directory.
611 * But we need to watch out for block devices that do not have parents, yet
612 * look like a partition (fd0, loop0, etc.) They all do not have a device
613 * symlink yet. We do sit and spin on waiting for them right now, we should
614 * possibly have a whitelist for these devices here...
616 class_dev_parent = sysfs_get_classdev_parent(class_dev);
617 if (class_dev_parent)
618 dbg("Really a partition");
621 tspec.tv_nsec = 10000000; /* sleep 10 millisec */
625 nanosleep(&tspec, NULL);
626 if (class_dev_parent)
627 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
629 sysfs_device = sysfs_get_classdev_device(class_dev);
631 if (sysfs_device != NULL)
634 dbg("timed out waiting for device symlink, continuing on anyway...");
637 /* We have another issue with just the wait above - the sysfs part of
638 * the kernel may not be quick enough to have created the link to the
639 * device under the "bus" subsystem. Due to this, the sysfs_device->bus
640 * will not contain the actual bus name :(
642 * Libsysfs now provides a new API sysfs_get_device_bus(), so use it
646 if (sysfs_device->bus[0] != '\0')
650 tspec.tv_nsec = 10000000;
653 nanosleep(&tspec, NULL);
654 sysfs_get_device_bus(sysfs_device);
656 if (sysfs_device->bus[0] != '\0')
659 dbg("timed out waiting to find the device bus, continuing on anyway");
662 dbg("device %s is registered with bus '%s'",
663 sysfs_device->name, sysfs_device->bus);
669 static int match_rule(struct config_device *dev, struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
672 /* check for matching bus value */
673 if (dev->bus[0] != '\0') {
674 if (sysfs_device == NULL) {
675 dbg("device has no bus");
678 dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
679 if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
680 dbg(FIELD_BUS " is not matching");
683 dbg(FIELD_BUS " matches");
687 /* check for matching kernel name*/
688 if (dev->kernel[0] != '\0') {
689 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'", dev->kernel, class_dev->name);
690 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
691 dbg(FIELD_KERNEL " is not matching");
694 dbg(FIELD_KERNEL " matches");
698 /* check for matching bus id */
699 if (dev->id[0] != '\0') {
700 dbg("check " FIELD_ID);
701 if (match_id(dev, class_dev, sysfs_device) != 0) {
702 dbg(FIELD_ID " is not matching");
705 dbg(FIELD_ID " matches");
709 /* check for matching place of device */
710 if (dev->place[0] != '\0') {
711 dbg("check " FIELD_PLACE);
712 if (match_place(dev, class_dev, sysfs_device) != 0) {
713 dbg(FIELD_PLACE " is not matching");
716 dbg(FIELD_PLACE " matches");
720 /* check for matching sysfs pairs */
721 if (dev->sysfs_pair[0].file[0] != '\0') {
722 dbg("check " FIELD_SYSFS " pairs");
723 if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
724 dbg(FIELD_SYSFS " is not matching");
727 dbg(FIELD_SYSFS " matches");
731 /* execute external program */
732 if (dev->program[0] != '\0') {
733 dbg("check " FIELD_PROGRAM);
734 apply_format(udev, dev->program, sizeof(dev->program),
735 class_dev, sysfs_device);
736 if (execute_program(dev->program, udev->program_result, NAME_SIZE) != 0) {
737 dbg(FIELD_PROGRAM " returned nonzero");
740 dbg(FIELD_PROGRAM " returned successful");
744 /* check for matching result of external program */
745 if (dev->result[0] != '\0') {
746 dbg("check for " FIELD_RESULT
747 " dev->result='%s', udev->program_result='%s'",
748 dev->result, udev->program_result);
749 if (strcmp_pattern(dev->result, udev->program_result) != 0) {
750 dbg(FIELD_RESULT " is not matching");
753 dbg(FIELD_RESULT " matches");
757 /* Yeah, we matched! */
761 dbg("try parent sysfs device");
762 sysfs_device = sysfs_get_device_parent(sysfs_device);
763 if (sysfs_device == NULL)
765 dbg("sysfs_device->path='%s'", sysfs_device->path);
766 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
767 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
772 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
774 struct sysfs_device *sysfs_device = NULL;
775 struct config_device *dev;
776 struct perm_device *perm;
781 /* find the sysfs_device associated with this class device */
782 sysfs_device = get_sysfs_device(class_dev);
784 dbg("sysfs_device->path='%s'", sysfs_device->path);
785 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
786 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
787 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
788 wait_for_device_to_initialize(sysfs_device);
790 dbg("class_dev->name = '%s'", class_dev->name);
792 strfieldcpy(udev->kernel_name, class_dev->name);
793 fix_kernel_name(udev);
794 dbg("udev->kernel_name = '%s'", udev->kernel_name);
796 /* get kernel number */
797 pos = class_dev->name + strlen(class_dev->name);
798 while (isdigit(*(pos-1)))
800 strfieldcpy(udev->kernel_number, pos);
801 dbg("kernel_number='%s'", udev->kernel_number);
803 /* look for a matching rule to apply */
804 list_for_each_entry(dev, &config_device_list, node) {
806 if (match_rule(dev, class_dev, udev, sysfs_device) == 0) {
807 if (dev->name[0] == '\0' && dev->symlink[0] == '\0') {
808 info("configured rule in '%s' at line %i applied, '%s' is ignored",
809 dev->config_file, dev->config_line, udev->kernel_name);
813 if (dev->symlink[0] != '\0') {
816 info("configured rule in '%s' at line %i applied, added symlink '%s'",
817 dev->config_file, dev->config_line, dev->symlink);
818 /* do not clobber dev */
819 strfieldcpy(temp, dev->symlink);
820 apply_format(udev, temp, sizeof(temp),
821 class_dev, sysfs_device);
822 strfieldcat(udev->symlink, temp);
823 strfieldcat(udev->symlink, " ");
826 if (dev->name[0] != '\0') {
827 info("configured rule in '%s' at line %i applied, '%s' becomes '%s'",
828 dev->config_file, dev->config_line, udev->kernel_name, dev->name);
829 strfieldcpy(udev->name, dev->name);
835 /* no rule was found so we use the kernel name */
836 strfieldcpy(udev->name, udev->kernel_name);
840 apply_format(udev, udev->name, sizeof(udev->name),
841 class_dev, sysfs_device);
842 udev->partitions = dev->partitions;
845 /* get permissions given in rule */
846 set_empty_perms(udev, dev->mode,
850 /* get permissions given in config file or set defaults */
851 perm = find_perm(udev->name);
853 set_empty_perms(udev, perm->mode,
857 set_empty_perms(udev, get_default_mode(),
859 get_default_group());
862 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
863 udev->name, udev->owner, udev->group, udev->mode);
868 int namedev_init(void)
872 retval = namedev_init_rules();
876 retval = namedev_init_permissions();
880 dump_config_dev_list();
881 dump_perm_dev_list();