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.
35 #include <sys/sysinfo.h>
37 #include "libsysfs/sysfs/libsysfs.h"
41 #include "udev_version.h"
44 #include "klibc_fixups.h"
46 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr);
48 LIST_HEAD(config_device_list);
49 LIST_HEAD(perm_device_list);
52 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
53 static int strcmp_pattern(const char *p, const char *s)
58 return (p[0] != '\0');
69 while ((p[0] != '\0') && (p[0] != ']')) {
72 if ((s[0] >= p[0]) && (s[0] <= p[2]))
76 match = (p[0] == s[0]);
80 while ((p[0] != '\0') && (p[0] != ']'))
83 return strcmp_pattern(p+1, s+1);
89 if (strcmp_pattern(p, s+1))
90 return strcmp_pattern(p+1, s);
98 if ((p[0] == s[0]) || (p[0] == '?'))
99 return strcmp_pattern(p+1, s+1);
105 static struct perm_device *find_perm(char *name)
107 struct perm_device *perm;
109 list_for_each_entry(perm, &perm_device_list, node) {
110 if (strcmp_pattern(perm->name, name))
117 static mode_t get_default_mode(void)
119 mode_t mode = 0600; /* default to owner rw only */
121 if (strlen(default_mode_str) != 0)
122 mode = strtol(default_mode_str, NULL, 8);
127 static char *get_default_owner(void)
129 if (strlen(default_owner_str) == 0)
130 strfieldcpy(default_owner_str, "root");
132 return default_owner_str;
135 static char *get_default_group(void)
137 if (strlen(default_group_str) == 0)
138 strfieldcpy(default_group_str, "root");
140 return default_group_str;
143 /* extract possible {attr} and move str behind it */
144 static char *get_format_attribute(char **str)
149 if (*str[0] == '{') {
150 pos = strchr(*str, '}');
152 dbg("missing closing brace for format");
158 dbg("attribute='%s', str='%s'", attr, *str);
163 /* extract possible format length and move str behind it*/
164 static int get_format_len(char **str)
169 if (isdigit(*str[0])) {
170 num = (int) strtoul(*str, &tail, 10);
173 dbg("format length=%i", num);
176 dbg("format parsing error '%s'", *str);
182 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
183 struct sysfs_class_device *class_dev,
184 struct sysfs_device *sysfs_device)
186 char temp[NAME_SIZE];
187 char temp2[NAME_SIZE];
197 struct sysfs_attribute *tmpattr;
201 pos = strchr(pos, '%');
207 len = get_format_len(&tail);
209 strfieldcpy(temp, tail+1);
211 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
212 attr = get_format_attribute(&tail);
217 if (strlen(udev->bus_id) == 0)
219 strfieldcatmax(string, udev->bus_id, maxsize);
220 dbg("substitute bus_id '%s'", udev->bus_id);
223 if (strlen(udev->kernel_name) == 0)
225 strfieldcatmax(string, udev->kernel_name, maxsize);
226 dbg("substitute kernel name '%s'", udev->kernel_name);
229 if (strlen(udev->kernel_number) == 0)
231 strfieldcatmax(string, udev->kernel_number, maxsize);
232 dbg("substitute kernel number '%s'", udev->kernel_number);
235 strintcatmax(string, udev->minor, maxsize);
236 dbg("substitute minor number '%u'", udev->minor);
239 strintcatmax(string, udev->major, maxsize);
240 dbg("substitute major number '%u'", udev->major);
243 if (strlen(udev->program_result) == 0)
245 /* get part part of the result string */
248 i = strtoul(attr, &rest, 10);
250 foreach_strpart(udev->program_result, " \n\r", spos, slen) {
256 dbg("requested part of result string not found");
260 strfieldcpy(temp2, spos);
262 strfieldcpymax(temp2, spos, slen+1);
263 strfieldcatmax(string, temp2, maxsize);
264 dbg("substitute part of result string '%s'", temp2);
266 strfieldcatmax(string, udev->program_result, maxsize);
267 dbg("substitute result string '%s'", udev->program_result);
272 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr);
273 if (tmpattr == NULL) {
274 dbg("sysfa attribute '%s' not found", attr);
277 strfieldcatmax(string, tmpattr->value, maxsize);
278 dbg("substitute sysfs value '%s'", tmpattr->value);
280 dbg("missing attribute");
284 strfieldcatmax(string, "%", maxsize);
288 dbg("unknown substitution type '%%%c'", c);
291 /* truncate to specified length */
295 strfieldcatmax(string, tail, maxsize);
300 * Note, we can have multiple files for different busses in here due
301 * to the mess that USB has for its device tree...
303 static struct bus_file {
307 { .bus = "scsi", .file = "vendor" },
308 { .bus = "usb", .file = "idVendor" },
309 { .bus = "usb", .file = "iInterface" },
310 { .bus = "usb-serial", .file = "detach_state" },
311 { .bus = "ide", .file = "detach_state" },
312 { .bus = "pci", .file = "vendor" },
316 #define SECONDS_TO_WAIT_FOR_FILE 10
317 static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device)
319 /* sleep until we see the file for this specific bus type show up this
320 * is needed because we can easily out-run the kernel in looking for
321 * these files before the paticular subsystem has created them in the
322 * sysfs tree properly.
324 * And people thought that the /sbin/hotplug event system was going to
325 * be slow, poo on you for arguing that before even testing it...
327 struct bus_file *b = &bus_files[0];
328 struct sysfs_attribute *tmpattr;
330 int loop = SECONDS_TO_WAIT_FOR_FILE;
333 if (b->bus == NULL) {
336 /* sleep to give the kernel a chance to create the file */
343 if (strcmp(sysfs_device->bus, b->bus) == 0) {
345 dbg("looking for file '%s' on bus '%s'", b->file, b->bus);
346 tmpattr = sysfs_get_device_attr(sysfs_device, b->file);
351 dbg("can't find '%s' file", b->file);
356 dbg("did not find bus type '%s' on list of bus_id_files, "
357 "contact greg@kroah.com", sysfs_device->bus);
359 return; /* here to prevent compiler warning... */
362 static void fix_kernel_name(struct udevice *udev)
364 char *temp = udev->kernel_name;
366 while (*temp != 0x00) {
367 /* Some block devices have a ! in their name,
368 * we need to change that to / */
375 static int execute_program(char *path, char *value, int len)
383 char arg[PROGRAM_SIZE];
384 char *argv[sizeof(arg) / 2];
388 if (strchr(path, ' ')) {
389 strfieldcpy(arg, path);
391 while (pos != NULL) {
392 if (pos[0] == '\'') {
393 /* don't separate if in apostrophes */
395 argv[i] = strsep(&pos, "\'");
396 while (pos[0] == ' ')
399 argv[i] = strsep(&pos, " ");
401 dbg("arg[%i] '%s'", i, argv[i]);
417 close(STDOUT_FILENO);
419 /* dup write side of pipe to STDOUT */
421 if (argv[0] != NULL) {
422 dbg("execute '%s' with given arguments", argv[0]);
423 retval = execv(argv[0], argv);
425 dbg("execute '%s' with main argument", path);
426 retval = execv(path, main_argv);
429 info(FIELD_PROGRAM " execution of '%s' failed", path);
435 /* parent reads from fds[0] */
440 count = read(fds[0], value + i, len - i-1);
446 dbg("result len %d too short", len);
453 dbg("read failed with '%s'", strerror(errno));
457 if (i > 0 && value[i-1] == '\n')
460 dbg("result is '%s'", value);
465 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
466 dbg("exec program status 0x%x", status);
473 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
475 struct sysfs_attribute *tmpattr = NULL;
478 dbg("look for device attribute '%s'", attr);
479 /* try to find the attribute in the class device directory */
480 tmpattr = sysfs_get_classdev_attr(class_dev, attr);
484 /* look in the class device directory if present */
486 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
494 c = strchr(tmpattr->value, '\n');
498 dbg("found attribute '%s'", tmpattr->path);
502 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
504 struct sysfs_attribute *tmpattr;
508 if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
511 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file);
515 /* strip trailing whitespace of value, if not asked to match for it */
516 if (! isspace(pair->value[strlen(pair->value)-1])) {
517 i = len = strlen(tmpattr->value);
518 while (i > 0 && isspace(tmpattr->value[i-1]))
521 tmpattr->value[i] = '\0';
522 dbg("remove %i trailing whitespace chars from '%s'",
523 len - i, tmpattr->value);
527 dbg("compare attribute '%s' value '%s' with '%s'",
528 pair->file, tmpattr->value, pair->value);
529 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
532 dbg("found matching attribute '%s' with value '%s'",
533 pair->file, pair->value);
537 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
539 struct sysfs_pair *pair;
542 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
543 pair = &dev->sysfs_pair[i];
544 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
546 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
547 dbg("sysfs attribute doesn't match");
555 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
557 char path[SYSFS_PATH_MAX];
560 /* we have to have a sysfs device for ID to work */
564 strfieldcpy(path, sysfs_device->path);
565 temp = strrchr(path, '/');
567 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
568 if (strcmp_pattern(dev->id, temp) != 0)
574 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
576 char path[SYSFS_PATH_MAX];
580 /* we have to have a sysfs device for PLACE to work */
585 strfieldcpy(path, sysfs_device->path);
586 temp = strrchr(path, '/');
587 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
588 if (strstr(temp, dev->place) != NULL) {
592 temp = strrchr(path, '/');
593 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
594 if (strstr(temp, dev->place) != NULL)
598 dbg("place doesn't match");
605 static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev)
607 struct sysfs_device *sysfs_device;
608 struct sysfs_class_device *class_dev_parent;
609 struct timespec tspec;
612 /* Figure out where the device symlink is at. For char devices this will
613 * always be in the class_dev->path. But for block devices, it's different.
614 * The main block device will have the device symlink in it's path, but
615 * all partitions have the symlink in its parent directory.
616 * But we need to watch out for block devices that do not have parents, yet
617 * look like a partition (fd0, loop0, etc.) They all do not have a device
618 * symlink yet. We do sit and spin on waiting for them right now, we should
619 * possibly have a whitelist for these devices here...
621 class_dev_parent = sysfs_get_classdev_parent(class_dev);
622 if (class_dev_parent != NULL)
623 dbg("given class device has a parent, use this instead");
626 tspec.tv_nsec = 10000000; /* sleep 10 millisec */
630 nanosleep(&tspec, NULL);
632 if (class_dev_parent)
633 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
635 sysfs_device = sysfs_get_classdev_device(class_dev);
636 if (sysfs_device != NULL)
639 dbg("timed out waiting for device symlink, continuing on anyway...");
642 /* We have another issue with just the wait above - the sysfs part of
643 * the kernel may not be quick enough to have created the link to the
644 * device under the "bus" subsystem. Due to this, the sysfs_device->bus
645 * will not contain the actual bus name :(
648 if (sysfs_device->bus[0] != '\0')
652 tspec.tv_nsec = 10000000;
655 nanosleep(&tspec, NULL);
656 sysfs_get_device_bus(sysfs_device);
658 if (sysfs_device->bus[0] != '\0')
661 dbg("timed out waiting to find the device bus, continuing on anyway");
664 dbg("device %s is registered with bus '%s'",
665 sysfs_device->name, sysfs_device->bus);
671 static int match_rule(struct config_device *dev, struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
674 /* check for matching bus value */
675 if (dev->bus[0] != '\0') {
676 if (sysfs_device == NULL) {
677 dbg("device has no bus");
680 dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
681 if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
682 dbg(FIELD_BUS " is not matching");
685 dbg(FIELD_BUS " matches");
689 /* check for matching kernel name*/
690 if (dev->kernel[0] != '\0') {
691 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'", dev->kernel, class_dev->name);
692 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
693 dbg(FIELD_KERNEL " is not matching");
696 dbg(FIELD_KERNEL " matches");
700 /* check for matching bus id */
701 if (dev->id[0] != '\0') {
702 dbg("check " FIELD_ID);
703 if (match_id(dev, class_dev, sysfs_device) != 0) {
704 dbg(FIELD_ID " is not matching");
707 dbg(FIELD_ID " matches");
711 /* check for matching place of device */
712 if (dev->place[0] != '\0') {
713 dbg("check " FIELD_PLACE);
714 if (match_place(dev, class_dev, sysfs_device) != 0) {
715 dbg(FIELD_PLACE " is not matching");
718 dbg(FIELD_PLACE " matches");
722 /* check for matching sysfs pairs */
723 if (dev->sysfs_pair[0].file[0] != '\0') {
724 dbg("check " FIELD_SYSFS " pairs");
725 if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
726 dbg(FIELD_SYSFS " is not matching");
729 dbg(FIELD_SYSFS " matches");
733 /* execute external program */
734 if (dev->program[0] != '\0') {
735 dbg("check " FIELD_PROGRAM);
736 apply_format(udev, dev->program, sizeof(dev->program),
737 class_dev, sysfs_device);
738 if (execute_program(dev->program, udev->program_result, NAME_SIZE) != 0) {
739 dbg(FIELD_PROGRAM " returned nonzero");
742 dbg(FIELD_PROGRAM " returned successful");
746 /* check for matching result of external program */
747 if (dev->result[0] != '\0') {
748 dbg("check for " FIELD_RESULT
749 " dev->result='%s', udev->program_result='%s'",
750 dev->result, udev->program_result);
751 if (strcmp_pattern(dev->result, udev->program_result) != 0) {
752 dbg(FIELD_RESULT " is not matching");
755 dbg(FIELD_RESULT " matches");
759 /* Yeah, we matched! */
763 dbg("try parent sysfs device");
764 sysfs_device = sysfs_get_device_parent(sysfs_device);
765 if (sysfs_device == NULL)
767 dbg("sysfs_device->path='%s'", sysfs_device->path);
768 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
769 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
774 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
776 struct sysfs_device *sysfs_device = NULL;
777 struct config_device *dev;
778 struct perm_device *perm;
784 /* find the sysfs_device associated with this class device */
785 sysfs_device = get_sysfs_device(class_dev);
787 dbg("sysfs_device->path='%s'", sysfs_device->path);
788 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
789 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
790 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
791 wait_for_device_to_initialize(sysfs_device);
793 dbg("class_dev->name = '%s'", class_dev->name);
795 strfieldcpy(udev->kernel_name, class_dev->name);
796 fix_kernel_name(udev);
797 dbg("udev->kernel_name = '%s'", udev->kernel_name);
799 /* get kernel number */
800 pos = class_dev->name + strlen(class_dev->name);
801 while (isdigit(*(pos-1)))
803 strfieldcpy(udev->kernel_number, pos);
804 dbg("kernel_number='%s'", udev->kernel_number);
806 /* look for a matching rule to apply */
807 list_for_each_entry(dev, &config_device_list, node) {
809 if (match_rule(dev, class_dev, udev, sysfs_device) == 0) {
810 if (dev->name[0] == '\0' && dev->symlink[0] == '\0') {
811 info("configured rule in '%s' at line %i applied, '%s' is ignored",
812 dev->config_file, dev->config_line, udev->kernel_name);
816 if (dev->symlink[0] != '\0') {
817 char temp[NAME_SIZE];
819 info("configured rule in '%s' at line %i applied, added symlink '%s'",
820 dev->config_file, dev->config_line, dev->symlink);
821 strfieldcpy(temp, dev->symlink);
822 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
823 if (udev->symlink[0] != '\0')
824 strfieldcat(udev->symlink, " ");
825 strfieldcat(udev->symlink, temp);
828 if (dev->name[0] != '\0') {
829 /* apply all_partitions flag only at a main block device */
830 if (dev->partitions > 0 &&
831 (udev->type != 'b' || udev->kernel_number[0] != '\0'))
834 info("configured rule in '%s' at line %i applied, '%s' becomes '%s'",
835 dev->config_file, dev->config_line, udev->kernel_name, dev->name);
836 strfieldcpy(udev->name, dev->name);
841 /* no rule was found so we use the kernel name */
842 strfieldcpy(udev->name, udev->kernel_name);
843 if (udev->type == 'n')
849 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
850 strfieldcpy(udev->config_file, dev->config_file);
851 udev->config_line = dev->config_line;
853 if (udev->type == 'n')
856 udev->partitions = dev->partitions;
858 /* get permissions given in rule */
859 set_empty_perms(udev, dev->mode,
864 /* get permissions given in config file or set defaults */
865 perm = find_perm(udev->name);
867 set_empty_perms(udev, perm->mode,
871 set_empty_perms(udev, get_default_mode(),
873 get_default_group());
876 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
877 udev->name, udev->owner, udev->group, udev->mode);
880 /* store time of action */
882 udev->config_uptime = info.uptime;
887 int namedev_init(void)
891 retval = namedev_init_rules();
895 retval = namedev_init_permissions();
899 dump_config_dev_list();
900 dump_perm_dev_list();