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"
47 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr);
49 LIST_HEAD(config_device_list);
50 LIST_HEAD(perm_device_list);
53 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
54 static int strcmp_pattern(const char *p, const char *s)
59 return (p[0] != '\0');
70 while ((p[0] != '\0') && (p[0] != ']')) {
73 if ((s[0] >= p[0]) && (s[0] <= p[2]))
77 match = (p[0] == s[0]);
81 while ((p[0] != '\0') && (p[0] != ']'))
84 return strcmp_pattern(p+1, s+1);
90 if (strcmp_pattern(p, s+1))
91 return strcmp_pattern(p+1, s);
99 if ((p[0] == s[0]) || (p[0] == '?'))
100 return strcmp_pattern(p+1, s+1);
106 static struct perm_device *find_perm(char *name)
108 struct perm_device *perm;
110 list_for_each_entry(perm, &perm_device_list, node) {
111 if (strcmp_pattern(perm->name, name))
118 static mode_t get_default_mode(void)
120 mode_t mode = 0600; /* default to owner rw only */
122 if (strlen(default_mode_str) != 0)
123 mode = strtol(default_mode_str, NULL, 8);
128 static char *get_default_owner(void)
130 if (strlen(default_owner_str) == 0)
131 strfieldcpy(default_owner_str, "root");
133 return default_owner_str;
136 static char *get_default_group(void)
138 if (strlen(default_group_str) == 0)
139 strfieldcpy(default_group_str, "root");
141 return default_group_str;
144 /* extract possible {attr} and move str behind it */
145 static char *get_format_attribute(char **str)
150 if (*str[0] == '{') {
151 pos = strchr(*str, '}');
153 dbg("missing closing brace for format");
159 dbg("attribute='%s', str='%s'", attr, *str);
164 /* extract possible format length and move str behind it*/
165 static int get_format_len(char **str)
170 if (isdigit(*str[0])) {
171 num = (int) strtoul(*str, &tail, 10);
174 dbg("format length=%i", num);
177 dbg("format parsing error '%s'", *str);
183 /** Finds the lowest positive N such that <name>N isn't present in
184 * $(udevroot) either as a file or a symlink.
186 * @param name Name to check for
187 * @return 0 if <name> didn't exist and N otherwise.
189 static unsigned int find_free_number (struct udevice *udev, char *name)
191 char temp[NAME_SIZE];
192 char path[NAME_SIZE];
196 /* have to sweep the database for each lookup */
198 strncpy(temp, name, sizeof (temp));
200 if (udevdb_get_dev_byname(temp, path, &dev) != 0)
202 /* symlink might be stale if $(udevroot) isn't cleaned; check
203 * on major/minor to see if it's the same device
205 if (dev.major == udev->major && dev.minor == udev->minor)
207 snprintf (temp, sizeof(temp), "%s%d", name, ++result);
214 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
215 struct sysfs_class_device *class_dev,
216 struct sysfs_device *sysfs_device)
218 char temp[NAME_SIZE];
219 char temp2[NAME_SIZE];
229 struct sysfs_attribute *tmpattr;
230 unsigned int next_free_number;
234 pos = strchr(pos, '%');
240 len = get_format_len(&tail);
242 strfieldcpy(temp, tail+1);
244 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
245 attr = get_format_attribute(&tail);
250 if (strlen(udev->bus_id) == 0)
252 strfieldcatmax(string, udev->bus_id, maxsize);
253 dbg("substitute bus_id '%s'", udev->bus_id);
256 if (strlen(udev->kernel_name) == 0)
258 strfieldcatmax(string, udev->kernel_name, maxsize);
259 dbg("substitute kernel name '%s'", udev->kernel_name);
262 if (strlen(udev->kernel_number) == 0)
264 strfieldcatmax(string, udev->kernel_number, maxsize);
265 dbg("substitute kernel number '%s'", udev->kernel_number);
268 strintcatmax(string, udev->minor, maxsize);
269 dbg("substitute minor number '%u'", udev->minor);
272 strintcatmax(string, udev->major, maxsize);
273 dbg("substitute major number '%u'", udev->major);
276 if (strlen(udev->program_result) == 0)
278 /* get part part of the result string */
281 i = strtoul(attr, &rest, 10);
283 foreach_strpart(udev->program_result, " \n\r", spos, slen) {
289 dbg("requested part of result string not found");
293 strfieldcpy(temp2, spos);
295 strfieldcpymax(temp2, spos, slen+1);
296 strfieldcatmax(string, temp2, maxsize);
297 dbg("substitute part of result string '%s'", temp2);
299 strfieldcatmax(string, udev->program_result, maxsize);
300 dbg("substitute result string '%s'", udev->program_result);
305 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr);
306 if (tmpattr == NULL) {
307 dbg("sysfa attribute '%s' not found", attr);
310 strfieldcatmax(string, tmpattr->value, maxsize);
311 dbg("substitute sysfs value '%s'", tmpattr->value);
313 dbg("missing attribute");
317 strfieldcatmax(string, "%", maxsize);
321 next_free_number = find_free_number(udev, string);
322 if (next_free_number > 0) {
323 snprintf(temp2, sizeof(temp2), "%d", next_free_number);
324 strfieldcatmax(string, temp2, maxsize);
328 dbg("unknown substitution type '%%%c'", c);
331 /* truncate to specified length */
335 strfieldcatmax(string, tail, maxsize);
340 * Note, we can have multiple files for different busses in here due
341 * to the mess that USB has for its device tree...
343 static struct bus_file {
347 { .bus = "scsi", .file = "vendor" },
348 { .bus = "usb", .file = "idVendor" },
349 { .bus = "usb", .file = "iInterface" },
350 { .bus = "usb-serial", .file = "detach_state" },
351 { .bus = "ide", .file = "detach_state" },
352 { .bus = "pci", .file = "vendor" },
356 #define SECONDS_TO_WAIT_FOR_FILE 10
357 static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device)
359 /* sleep until we see the file for this specific bus type show up this
360 * is needed because we can easily out-run the kernel in looking for
361 * these files before the paticular subsystem has created them in the
362 * sysfs tree properly.
364 * And people thought that the /sbin/hotplug event system was going to
365 * be slow, poo on you for arguing that before even testing it...
367 struct bus_file *b = &bus_files[0];
368 struct sysfs_attribute *tmpattr;
370 int loop = SECONDS_TO_WAIT_FOR_FILE;
373 if (b->bus == NULL) {
376 /* sleep to give the kernel a chance to create the file */
383 if (strcmp(sysfs_device->bus, b->bus) == 0) {
385 dbg("looking for file '%s' on bus '%s'", b->file, b->bus);
386 tmpattr = sysfs_get_device_attr(sysfs_device, b->file);
391 dbg("can't find '%s' file", b->file);
396 dbg("did not find bus type '%s' on list of bus_id_files, "
397 "contact greg@kroah.com", sysfs_device->bus);
399 return; /* here to prevent compiler warning... */
402 static void fix_kernel_name(struct udevice *udev)
404 char *temp = udev->kernel_name;
406 while (*temp != 0x00) {
407 /* Some block devices have a ! in their name,
408 * we need to change that to / */
415 static int execute_program(const char *path, char *value, int len)
423 char arg[PROGRAM_SIZE];
424 char *argv[(PROGRAM_SIZE / 2) + 1];
427 strfieldcpy(arg, path);
429 if (strchr(path, ' ')) {
431 while (pos != NULL) {
432 if (pos[0] == '\'') {
433 /* don't separate if in apostrophes */
435 argv[i] = strsep(&pos, "\'");
436 while (pos && pos[0] == ' ')
439 argv[i] = strsep(&pos, " ");
441 dbg("arg[%i] '%s'", i, argv[i]);
445 dbg("execute '%s' with parsed arguments", arg);
448 argv[1] = main_argv[1];
450 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
463 /* dup2 write side of pipe to STDOUT */
464 dup2(fds[1], STDOUT_FILENO);
465 retval = execv(arg, argv);
467 info(FIELD_PROGRAM " execution of '%s' failed", path);
473 /* parent reads from fds[0] */
478 count = read(fds[0], value + i, len - i-1);
484 dbg("result len %d too short", len);
491 dbg("read failed with '%s'", strerror(errno));
495 if (i > 0 && value[i-1] == '\n')
498 dbg("result is '%s'", value);
503 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
504 dbg("exec program status 0x%x", status);
511 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
513 struct sysfs_attribute *tmpattr = NULL;
516 dbg("look for device attribute '%s'", attr);
517 /* try to find the attribute in the class device directory */
518 tmpattr = sysfs_get_classdev_attr(class_dev, attr);
522 /* look in the class device directory if present */
524 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
532 c = strchr(tmpattr->value, '\n');
536 dbg("found attribute '%s'", tmpattr->path);
540 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
542 struct sysfs_attribute *tmpattr;
546 if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
549 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file);
553 /* strip trailing whitespace of value, if not asked to match for it */
554 if (! isspace(pair->value[strlen(pair->value)-1])) {
555 i = len = strlen(tmpattr->value);
556 while (i > 0 && isspace(tmpattr->value[i-1]))
559 tmpattr->value[i] = '\0';
560 dbg("remove %i trailing whitespace chars from '%s'",
561 len - i, tmpattr->value);
565 dbg("compare attribute '%s' value '%s' with '%s'",
566 pair->file, tmpattr->value, pair->value);
567 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
570 dbg("found matching attribute '%s' with value '%s'",
571 pair->file, pair->value);
575 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
577 struct sysfs_pair *pair;
580 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
581 pair = &dev->sysfs_pair[i];
582 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
584 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
585 dbg("sysfs attribute doesn't match");
593 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
595 char path[SYSFS_PATH_MAX];
598 /* we have to have a sysfs device for ID to work */
602 strfieldcpy(path, sysfs_device->path);
603 temp = strrchr(path, '/');
605 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
606 if (strcmp_pattern(dev->id, temp) != 0)
612 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
614 char path[SYSFS_PATH_MAX];
618 /* we have to have a sysfs device for PLACE to work */
623 strfieldcpy(path, sysfs_device->path);
624 temp = strrchr(path, '/');
625 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
626 if (strstr(temp, dev->place) != NULL) {
630 temp = strrchr(path, '/');
631 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
632 if (strstr(temp, dev->place) != NULL)
636 dbg("place doesn't match");
643 static int whitelist_search(struct sysfs_class_device *class_dev)
645 char *sysblock = "/sys/block";
648 static char *list[] = {
669 if (strncmp(class_dev->path, sysblock, strlen(sysblock)))
672 for (i=0; list[i] != NULL; i++)
673 if (!strncmp(class_dev->name, list[i], strlen(list[i])))
679 static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev)
681 struct sysfs_device *sysfs_device;
682 struct sysfs_class_device *class_dev_parent;
683 struct timespec tspec;
686 /* Figure out where the device symlink is at. For char devices this will
687 * always be in the class_dev->path. But for block devices, it's different.
688 * The main block device will have the device symlink in it's path, but
689 * all partitions have the symlink in its parent directory.
690 * But we need to watch out for block devices that do not have parents, yet
691 * look like a partition (fd0, loop0, etc.) They all do not have a device
692 * symlink yet. We do sit and spin on waiting for them right now unless
693 * they happen to be in the whitelist in which case we exit.
695 class_dev_parent = sysfs_get_classdev_parent(class_dev);
696 if (class_dev_parent != NULL)
697 dbg("given class device has a parent, use this instead");
700 tspec.tv_nsec = 10000000; /* sleep 10 millisec */
704 if (whitelist_search(class_dev)) {
708 nanosleep(&tspec, NULL);
711 if (class_dev_parent)
712 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
714 sysfs_device = sysfs_get_classdev_device(class_dev);
715 if (sysfs_device != NULL)
718 dbg("timed out waiting for device symlink, continuing on anyway...");
721 /* We have another issue with just the wait above - the sysfs part of
722 * the kernel may not be quick enough to have created the link to the
723 * device under the "bus" subsystem. Due to this, the sysfs_device->bus
724 * will not contain the actual bus name :(
727 if (sysfs_device->bus[0] != '\0')
731 tspec.tv_nsec = 10000000;
734 nanosleep(&tspec, NULL);
735 sysfs_get_device_bus(sysfs_device);
737 if (sysfs_device->bus[0] != '\0')
740 dbg("timed out waiting to find the device bus, continuing on anyway");
743 dbg("device %s is registered with bus '%s'",
744 sysfs_device->name, sysfs_device->bus);
750 static int match_rule(struct config_device *dev, struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
753 /* check for matching bus value */
754 if (dev->bus[0] != '\0') {
755 if (sysfs_device == NULL) {
756 dbg("device has no bus");
759 dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
760 if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
761 dbg(FIELD_BUS " is not matching");
764 dbg(FIELD_BUS " matches");
768 /* check for matching kernel name*/
769 if (dev->kernel[0] != '\0') {
770 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'", dev->kernel, class_dev->name);
771 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
772 dbg(FIELD_KERNEL " is not matching");
775 dbg(FIELD_KERNEL " matches");
779 /* check for matching bus id */
780 if (dev->id[0] != '\0') {
781 dbg("check " FIELD_ID);
782 if (match_id(dev, class_dev, sysfs_device) != 0) {
783 dbg(FIELD_ID " is not matching");
786 dbg(FIELD_ID " matches");
790 /* check for matching place of device */
791 if (dev->place[0] != '\0') {
792 dbg("check " FIELD_PLACE);
793 if (match_place(dev, class_dev, sysfs_device) != 0) {
794 dbg(FIELD_PLACE " is not matching");
797 dbg(FIELD_PLACE " matches");
801 /* check for matching sysfs pairs */
802 if (dev->sysfs_pair[0].file[0] != '\0') {
803 dbg("check " FIELD_SYSFS " pairs");
804 if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
805 dbg(FIELD_SYSFS " is not matching");
808 dbg(FIELD_SYSFS " matches");
812 /* execute external program */
813 if (dev->program[0] != '\0') {
814 char program[PROGRAM_SIZE];
816 dbg("check " FIELD_PROGRAM);
817 strfieldcpy(program, dev->program);
818 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
819 if (execute_program(program, udev->program_result, NAME_SIZE) != 0) {
820 dbg(FIELD_PROGRAM " returned nonzero");
823 dbg(FIELD_PROGRAM " returned successful");
827 /* check for matching result of external program */
828 if (dev->result[0] != '\0') {
829 dbg("check for " FIELD_RESULT
830 " dev->result='%s', udev->program_result='%s'",
831 dev->result, udev->program_result);
832 if (strcmp_pattern(dev->result, udev->program_result) != 0) {
833 dbg(FIELD_RESULT " is not matching");
836 dbg(FIELD_RESULT " matches");
840 /* Yeah, we matched! */
844 dbg("try parent sysfs device");
845 sysfs_device = sysfs_get_device_parent(sysfs_device);
846 if (sysfs_device == NULL)
848 dbg("sysfs_device->path='%s'", sysfs_device->path);
849 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
850 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
855 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
857 struct sysfs_device *sysfs_device = NULL;
858 struct config_device *dev;
859 struct perm_device *perm;
865 /* find the sysfs_device associated with this class device */
866 sysfs_device = get_sysfs_device(class_dev);
868 dbg("sysfs_device->path='%s'", sysfs_device->path);
869 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
870 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
871 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
872 wait_for_device_to_initialize(sysfs_device);
874 dbg("class_dev->name = '%s'", class_dev->name);
876 strfieldcpy(udev->kernel_name, class_dev->name);
877 fix_kernel_name(udev);
878 dbg("udev->kernel_name = '%s'", udev->kernel_name);
880 /* get kernel number */
881 pos = class_dev->name + strlen(class_dev->name);
882 while (isdigit(*(pos-1)))
884 strfieldcpy(udev->kernel_number, pos);
885 dbg("kernel_number='%s'", udev->kernel_number);
887 /* look for a matching rule to apply */
888 list_for_each_entry(dev, &config_device_list, node) {
890 if (match_rule(dev, class_dev, udev, sysfs_device) == 0) {
891 if (dev->name[0] == '\0' && dev->symlink[0] == '\0') {
892 info("configured rule in '%s' at line %i applied, '%s' is ignored",
893 dev->config_file, dev->config_line, udev->kernel_name);
897 if (dev->symlink[0] != '\0') {
898 char temp[NAME_SIZE];
900 info("configured rule in '%s' at line %i applied, added symlink '%s'",
901 dev->config_file, dev->config_line, dev->symlink);
902 strfieldcpy(temp, dev->symlink);
903 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
904 if (udev->symlink[0] != '\0')
905 strfieldcat(udev->symlink, " ");
906 strfieldcat(udev->symlink, temp);
909 if (dev->name[0] != '\0') {
910 /* apply all_partitions flag only at a main block device */
911 if (dev->partitions > 0 &&
912 (udev->type != 'b' || udev->kernel_number[0] != '\0'))
915 info("configured rule in '%s' at line %i applied, '%s' becomes '%s'",
916 dev->config_file, dev->config_line, udev->kernel_name, dev->name);
917 strfieldcpy(udev->name, dev->name);
922 /* no rule was found so we use the kernel name */
923 strfieldcpy(udev->name, udev->kernel_name);
924 if (udev->type == 'n')
930 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
931 strfieldcpy(udev->config_file, dev->config_file);
932 udev->config_line = dev->config_line;
934 if (udev->type == 'n')
937 udev->partitions = dev->partitions;
939 /* get permissions given in rule */
940 set_empty_perms(udev, dev->mode,
945 /* get permissions given in config file or set defaults */
946 perm = find_perm(udev->name);
948 set_empty_perms(udev, perm->mode,
952 set_empty_perms(udev, get_default_mode(),
954 get_default_group());
957 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
958 udev->name, udev->owner, udev->group, udev->mode);
961 /* store time of action */
963 udev->config_uptime = info.uptime;
968 int namedev_init(void)
972 retval = namedev_init_rules();
976 retval = namedev_init_permissions();
980 dump_config_dev_list();
981 dump_perm_dev_list();