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 <sys/sysinfo.h>
39 #include "libsysfs/sysfs/libsysfs.h"
43 #include "udev_version.h"
46 #include "klibc_fixups.h"
48 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr);
50 LIST_HEAD(config_device_list);
51 LIST_HEAD(perm_device_list);
54 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
55 static int strcmp_pattern(const char *p, const char *s)
60 return (p[0] != '\0');
71 while ((p[0] != '\0') && (p[0] != ']')) {
74 if ((s[0] >= p[0]) && (s[0] <= p[2]))
78 match = (p[0] == s[0]);
82 while ((p[0] != '\0') && (p[0] != ']'))
85 return strcmp_pattern(p+1, s+1);
91 if (strcmp_pattern(p, s+1))
92 return strcmp_pattern(p+1, s);
100 if ((p[0] == s[0]) || (p[0] == '?'))
101 return strcmp_pattern(p+1, s+1);
107 static struct perm_device *find_perm(char *name)
109 struct perm_device *perm;
111 list_for_each_entry(perm, &perm_device_list, node) {
112 if (strcmp_pattern(perm->name, name))
119 static mode_t get_default_mode(void)
121 mode_t mode = 0600; /* default to owner rw only */
123 if (strlen(default_mode_str) != 0)
124 mode = strtol(default_mode_str, NULL, 8);
129 static char *get_default_owner(void)
131 if (strlen(default_owner_str) == 0)
132 strfieldcpy(default_owner_str, "root");
134 return default_owner_str;
137 static char *get_default_group(void)
139 if (strlen(default_group_str) == 0)
140 strfieldcpy(default_group_str, "root");
142 return default_group_str;
145 /* extract possible {attr} and move str behind it */
146 static char *get_format_attribute(char **str)
151 if (*str[0] == '{') {
152 pos = strchr(*str, '}');
154 dbg("missing closing brace for format");
160 dbg("attribute='%s', str='%s'", attr, *str);
165 /* extract possible format length and move str behind it*/
166 static int get_format_len(char **str)
171 if (isdigit(*str[0])) {
172 num = (int) strtoul(*str, &tail, 10);
175 dbg("format length=%i", num);
178 dbg("format parsing error '%s'", *str);
184 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
185 struct sysfs_class_device *class_dev,
186 struct sysfs_device *sysfs_device)
188 char temp[NAME_SIZE];
189 char temp2[NAME_SIZE];
199 struct sysfs_attribute *tmpattr;
204 pos = strchr(string, '%');
208 len = get_format_len(&tail);
210 strfieldcpy(temp, tail+1);
215 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
217 attr = get_format_attribute(&tail);
221 if (strlen(udev->bus_id) == 0)
223 strfieldcatmax(string, udev->bus_id, maxsize);
224 dbg("substitute bus_id '%s'", udev->bus_id);
227 if (strlen(udev->kernel_name) == 0)
229 strfieldcatmax(string, udev->kernel_name, maxsize);
230 dbg("substitute kernel name '%s'", udev->kernel_name);
233 if (strlen(udev->kernel_number) == 0)
235 strfieldcatmax(string, udev->kernel_number, maxsize);
236 dbg("substitute kernel number '%s'", udev->kernel_number);
239 strintcatmax(string, udev->minor, maxsize);
240 dbg("substitute minor number '%u'", udev->minor);
243 strintcatmax(string, udev->major, maxsize);
244 dbg("substitute major number '%u'", udev->major);
247 if (strlen(udev->program_result) == 0)
249 /* get part part of the result string */
252 i = strtoul(attr, &rest, 10);
254 foreach_strpart(udev->program_result, " \n\r", spos, slen) {
260 dbg("requested part of result string not found");
264 strfieldcpy(temp2, spos);
266 strfieldcpymax(temp2, spos, slen+1);
267 strfieldcatmax(string, temp2, maxsize);
268 dbg("substitute part of result string '%s'", temp2);
270 strfieldcatmax(string, udev->program_result, maxsize);
271 dbg("substitute result string '%s'", udev->program_result);
276 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr);
277 if (tmpattr == NULL) {
278 dbg("sysfa attribute '%s' not found", attr);
281 strfieldcatmax(string, tmpattr->value, maxsize);
282 dbg("substitute sysfs value '%s'", tmpattr->value);
284 dbg("missing attribute");
288 strfieldcatmax(string, "%", maxsize);
291 dbg("unknown substitution type '%%%c'", c);
294 /* truncate to specified length */
298 strfieldcatmax(string, tail, maxsize);
303 * Note, we can have multiple files for different busses in here due
304 * to the mess that USB has for its device tree...
306 static struct bus_file {
310 { .bus = "scsi", .file = "vendor" },
311 { .bus = "usb", .file = "idVendor" },
312 { .bus = "usb", .file = "iInterface" },
313 { .bus = "usb-serial", .file = "detach_state" },
314 { .bus = "ide", .file = "detach_state" },
315 { .bus = "pci", .file = "vendor" },
319 #define SECONDS_TO_WAIT_FOR_FILE 10
320 static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device)
322 /* sleep until we see the file for this specific bus type show up this
323 * is needed because we can easily out-run the kernel in looking for
324 * these files before the paticular subsystem has created them in the
325 * sysfs tree properly.
327 * And people thought that the /sbin/hotplug event system was going to
328 * be slow, poo on you for arguing that before even testing it...
330 struct bus_file *b = &bus_files[0];
331 struct sysfs_attribute *tmpattr;
333 int loop = SECONDS_TO_WAIT_FOR_FILE;
336 if (b->bus == NULL) {
339 /* sleep to give the kernel a chance to create the file */
346 if (strcmp(sysfs_device->bus, b->bus) == 0) {
348 dbg("looking for file '%s' on bus '%s'", b->file, b->bus);
349 tmpattr = sysfs_get_device_attr(sysfs_device, b->file);
354 dbg("can't find '%s' file", b->file);
359 dbg("did not find bus type '%s' on list of bus_id_files, "
360 "contact greg@kroah.com", sysfs_device->bus);
362 return; /* here to prevent compiler warning... */
365 static void fix_kernel_name(struct udevice *udev)
367 char *temp = udev->kernel_name;
369 while (*temp != 0x00) {
370 /* Some block devices have a ! in their name,
371 * we need to change that to / */
378 static int execute_program(char *path, char *value, int len)
386 char arg[PROGRAM_SIZE];
387 char *argv[sizeof(arg) / 2];
391 if (strchr(path, ' ')) {
392 strfieldcpy(arg, path);
394 while (pos != NULL) {
395 if (pos[0] == '\'') {
396 /* don't separate if in apostrophes */
398 argv[i] = strsep(&pos, "\'");
399 while (pos[0] == ' ')
402 argv[i] = strsep(&pos, " ");
404 dbg("arg[%i] '%s'", i, argv[i]);
420 close(STDOUT_FILENO);
422 /* dup write side of pipe to STDOUT */
424 if (argv[0] != NULL) {
425 dbg("execute '%s' with given arguments", argv[0]);
426 retval = execv(argv[0], argv);
428 dbg("execute '%s' with main argument", path);
429 retval = execv(path, main_argv);
432 info(FIELD_PROGRAM " execution of '%s' failed", path);
438 /* parent reads from fds[0] */
443 count = read(fds[0], value + i, len - i-1);
449 dbg("result len %d too short", len);
456 dbg("read failed with '%s'", strerror(errno));
460 if (i > 0 && value[i-1] == '\n')
463 dbg("result is '%s'", value);
468 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
469 dbg("exec program status 0x%x", status);
476 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
478 struct sysfs_attribute *tmpattr = NULL;
481 dbg("look for device attribute '%s'", attr);
482 /* try to find the attribute in the class device directory */
483 tmpattr = sysfs_get_classdev_attr(class_dev, attr);
487 /* look in the class device directory if present */
489 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
497 c = strchr(tmpattr->value, '\n');
501 dbg("found attribute '%s'", tmpattr->path);
505 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
507 struct sysfs_attribute *tmpattr;
511 if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
514 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file);
518 /* strip trailing whitespace of value, if not asked to match for it */
519 if (! isspace(pair->value[strlen(pair->value)-1])) {
520 i = len = strlen(tmpattr->value);
521 while (i > 0 && isspace(tmpattr->value[i-1]))
524 tmpattr->value[i] = '\0';
525 dbg("remove %i trailing whitespace chars from '%s'",
526 len - i, tmpattr->value);
530 dbg("compare attribute '%s' value '%s' with '%s'",
531 pair->file, tmpattr->value, pair->value);
532 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
535 dbg("found matching attribute '%s' with value '%s'",
536 pair->file, pair->value);
540 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
542 struct sysfs_pair *pair;
545 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
546 pair = &dev->sysfs_pair[i];
547 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
549 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
550 dbg("sysfs attribute doesn't match");
558 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
560 char path[SYSFS_PATH_MAX];
563 /* we have to have a sysfs device for ID to work */
567 strfieldcpy(path, sysfs_device->path);
568 temp = strrchr(path, '/');
570 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
571 if (strcmp_pattern(dev->id, temp) != 0)
577 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
579 char path[SYSFS_PATH_MAX];
583 /* we have to have a sysfs device for PLACE to work */
588 strfieldcpy(path, sysfs_device->path);
589 temp = strrchr(path, '/');
590 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
591 if (strstr(temp, dev->place) != NULL) {
595 temp = strrchr(path, '/');
596 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
597 if (strstr(temp, dev->place) != NULL)
601 dbg("place doesn't match");
608 static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev)
610 struct sysfs_device *sysfs_device;
611 struct sysfs_class_device *class_dev_parent;
612 struct timespec tspec;
615 /* Figure out where the device symlink is at. For char devices this will
616 * always be in the class_dev->path. But for block devices, it's different.
617 * The main block device will have the device symlink in it's path, but
618 * all partitions have the symlink in its parent directory.
619 * But we need to watch out for block devices that do not have parents, yet
620 * look like a partition (fd0, loop0, etc.) They all do not have a device
621 * symlink yet. We do sit and spin on waiting for them right now, we should
622 * possibly have a whitelist for these devices here...
624 class_dev_parent = sysfs_get_classdev_parent(class_dev);
625 if (class_dev_parent != NULL)
626 dbg("given class device has a parent, use this instead");
629 tspec.tv_nsec = 10000000; /* sleep 10 millisec */
633 nanosleep(&tspec, NULL);
635 if (class_dev_parent)
636 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
638 sysfs_device = sysfs_get_classdev_device(class_dev);
639 if (sysfs_device != NULL)
642 dbg("timed out waiting for device symlink, continuing on anyway...");
645 /* We have another issue with just the wait above - the sysfs part of
646 * the kernel may not be quick enough to have created the link to the
647 * device under the "bus" subsystem. Due to this, the sysfs_device->bus
648 * will not contain the actual bus name :(
651 if (sysfs_device->bus[0] != '\0')
655 tspec.tv_nsec = 10000000;
658 nanosleep(&tspec, NULL);
659 sysfs_get_device_bus(sysfs_device);
661 if (sysfs_device->bus[0] != '\0')
664 dbg("timed out waiting to find the device bus, continuing on anyway");
667 dbg("device %s is registered with bus '%s'",
668 sysfs_device->name, sysfs_device->bus);
674 static int match_rule(struct config_device *dev, struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
677 /* check for matching bus value */
678 if (dev->bus[0] != '\0') {
679 if (sysfs_device == NULL) {
680 dbg("device has no bus");
683 dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
684 if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
685 dbg(FIELD_BUS " is not matching");
688 dbg(FIELD_BUS " matches");
692 /* check for matching kernel name*/
693 if (dev->kernel[0] != '\0') {
694 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'", dev->kernel, class_dev->name);
695 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
696 dbg(FIELD_KERNEL " is not matching");
699 dbg(FIELD_KERNEL " matches");
703 /* check for matching bus id */
704 if (dev->id[0] != '\0') {
705 dbg("check " FIELD_ID);
706 if (match_id(dev, class_dev, sysfs_device) != 0) {
707 dbg(FIELD_ID " is not matching");
710 dbg(FIELD_ID " matches");
714 /* check for matching place of device */
715 if (dev->place[0] != '\0') {
716 dbg("check " FIELD_PLACE);
717 if (match_place(dev, class_dev, sysfs_device) != 0) {
718 dbg(FIELD_PLACE " is not matching");
721 dbg(FIELD_PLACE " matches");
725 /* check for matching sysfs pairs */
726 if (dev->sysfs_pair[0].file[0] != '\0') {
727 dbg("check " FIELD_SYSFS " pairs");
728 if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
729 dbg(FIELD_SYSFS " is not matching");
732 dbg(FIELD_SYSFS " matches");
736 /* execute external program */
737 if (dev->program[0] != '\0') {
738 dbg("check " FIELD_PROGRAM);
739 apply_format(udev, dev->program, sizeof(dev->program),
740 class_dev, sysfs_device);
741 if (execute_program(dev->program, udev->program_result, NAME_SIZE) != 0) {
742 dbg(FIELD_PROGRAM " returned nonzero");
745 dbg(FIELD_PROGRAM " returned successful");
749 /* check for matching result of external program */
750 if (dev->result[0] != '\0') {
751 dbg("check for " FIELD_RESULT
752 " dev->result='%s', udev->program_result='%s'",
753 dev->result, udev->program_result);
754 if (strcmp_pattern(dev->result, udev->program_result) != 0) {
755 dbg(FIELD_RESULT " is not matching");
758 dbg(FIELD_RESULT " matches");
762 /* Yeah, we matched! */
766 dbg("try parent sysfs device");
767 sysfs_device = sysfs_get_device_parent(sysfs_device);
768 if (sysfs_device == NULL)
770 dbg("sysfs_device->path='%s'", sysfs_device->path);
771 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
772 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
777 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
779 struct sysfs_device *sysfs_device = NULL;
780 struct config_device *dev;
781 struct perm_device *perm;
787 /* find the sysfs_device associated with this class device */
788 sysfs_device = get_sysfs_device(class_dev);
790 dbg("sysfs_device->path='%s'", sysfs_device->path);
791 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
792 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
793 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
794 wait_for_device_to_initialize(sysfs_device);
796 dbg("class_dev->name = '%s'", class_dev->name);
798 strfieldcpy(udev->kernel_name, class_dev->name);
799 fix_kernel_name(udev);
800 dbg("udev->kernel_name = '%s'", udev->kernel_name);
802 /* get kernel number */
803 pos = class_dev->name + strlen(class_dev->name);
804 while (isdigit(*(pos-1)))
806 strfieldcpy(udev->kernel_number, pos);
807 dbg("kernel_number='%s'", udev->kernel_number);
809 /* look for a matching rule to apply */
810 list_for_each_entry(dev, &config_device_list, node) {
812 if (match_rule(dev, class_dev, udev, sysfs_device) == 0) {
813 if (dev->name[0] == '\0' && dev->symlink[0] == '\0') {
814 info("configured rule in '%s' at line %i applied, '%s' is ignored",
815 dev->config_file, dev->config_line, udev->kernel_name);
819 if (dev->symlink[0] != '\0') {
820 char temp[NAME_SIZE];
822 info("configured rule in '%s' at line %i applied, added symlink '%s'",
823 dev->config_file, dev->config_line, dev->symlink);
824 strfieldcpy(temp, dev->symlink);
825 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
826 if (udev->symlink[0] != '\0')
827 strfieldcat(udev->symlink, " ");
828 strfieldcat(udev->symlink, temp);
831 if (dev->name[0] != '\0') {
832 /* apply all_partitions flag only at a main block device */
833 if (dev->partitions > 0 &&
834 (udev->type != 'b' || udev->kernel_number[0] != '\0'))
837 info("configured rule in '%s' at line %i applied, '%s' becomes '%s'",
838 dev->config_file, dev->config_line, udev->kernel_name, dev->name);
839 strfieldcpy(udev->name, dev->name);
844 /* no rule was found so we use the kernel name */
845 strfieldcpy(udev->name, udev->kernel_name);
846 if (udev->type == 'n')
852 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
854 if (udev->type == 'n')
857 udev->partitions = dev->partitions;
858 strfieldcpy(udev->config_file, dev->config_file);
859 udev->config_line = dev->config_line;
861 /* get permissions given in rule */
862 set_empty_perms(udev, dev->mode,
867 /* get permissions given in config file or set defaults */
868 perm = find_perm(udev->name);
870 set_empty_perms(udev, perm->mode,
874 set_empty_perms(udev, get_default_mode(),
876 get_default_group());
879 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
880 udev->name, udev->owner, udev->group, udev->mode);
883 /* store time of action */
885 udev->config_uptime = info.uptime;
890 int namedev_init(void)
894 retval = namedev_init_rules();
898 retval = namedev_init_permissions();
902 dump_config_dev_list();
903 dump_perm_dev_list();