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.
34 #include <sys/sysinfo.h>
36 #include "libsysfs/sysfs/libsysfs.h"
40 #include "udev_version.h"
43 #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 /** Finds the lowest positive N such that <name>N isn't present in
183 * $(udevroot) either as a file or a symlink.
185 * @param name Name to check for
186 * @return 0 if <name> didn't exist and N otherwise.
188 static unsigned int find_free_number (struct udevice *udev, char *name)
190 char temp[NAME_SIZE];
191 char path[NAME_SIZE];
195 /* have to sweep the database for each lookup */
197 strncpy(temp, name, sizeof (temp));
199 if (udevdb_get_dev_byname(temp, path, &dev) != 0)
201 /* symlink might be stale if $(udevroot) isn't cleaned; check
202 * on major/minor to see if it's the same device
204 if (dev.major == udev->major && dev.minor == udev->minor)
206 snprintf (temp, sizeof(temp), "%s%d", name, ++result);
213 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
214 struct sysfs_class_device *class_dev,
215 struct sysfs_device *sysfs_device)
217 char temp[NAME_SIZE];
218 char temp2[NAME_SIZE];
228 struct sysfs_attribute *tmpattr;
229 unsigned int next_free_number;
233 pos = strchr(pos, '%');
239 len = get_format_len(&tail);
241 strfieldcpy(temp, tail+1);
243 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
244 attr = get_format_attribute(&tail);
249 if (strlen(udev->bus_id) == 0)
251 strfieldcatmax(string, udev->bus_id, maxsize);
252 dbg("substitute bus_id '%s'", udev->bus_id);
255 if (strlen(udev->kernel_name) == 0)
257 strfieldcatmax(string, udev->kernel_name, maxsize);
258 dbg("substitute kernel name '%s'", udev->kernel_name);
261 if (strlen(udev->kernel_number) == 0)
263 strfieldcatmax(string, udev->kernel_number, maxsize);
264 dbg("substitute kernel number '%s'", udev->kernel_number);
267 strintcatmax(string, udev->minor, maxsize);
268 dbg("substitute minor number '%u'", udev->minor);
271 strintcatmax(string, udev->major, maxsize);
272 dbg("substitute major number '%u'", udev->major);
275 if (strlen(udev->program_result) == 0)
277 /* get part part of the result string */
280 i = strtoul(attr, &rest, 10);
282 foreach_strpart(udev->program_result, " \n\r", spos, slen) {
288 dbg("requested part of result string not found");
292 strfieldcpy(temp2, spos);
294 strfieldcpymax(temp2, spos, slen+1);
295 strfieldcatmax(string, temp2, maxsize);
296 dbg("substitute part of result string '%s'", temp2);
298 strfieldcatmax(string, udev->program_result, maxsize);
299 dbg("substitute result string '%s'", udev->program_result);
304 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr);
305 if (tmpattr == NULL) {
306 dbg("sysfa attribute '%s' not found", attr);
309 /* strip trailing whitespace of matching value */
310 if (isspace(tmpattr->value[strlen(tmpattr->value)-1])) {
311 i = len = strlen(tmpattr->value);
312 while (i > 0 && isspace(tmpattr->value[i-1]))
315 tmpattr->value[i] = '\0';
316 dbg("remove %i trailing whitespace chars from '%s'",
317 len - i, tmpattr->value);
320 strfieldcatmax(string, tmpattr->value, maxsize);
321 dbg("substitute sysfs value '%s'", tmpattr->value);
323 dbg("missing attribute");
327 strfieldcatmax(string, "%", maxsize);
331 next_free_number = find_free_number(udev, string);
332 if (next_free_number > 0) {
333 snprintf(temp2, sizeof(temp2), "%d", next_free_number);
334 strfieldcatmax(string, temp2, maxsize);
338 dbg("unknown substitution type '%%%c'", c);
341 /* truncate to specified length */
345 strfieldcatmax(string, tail, maxsize);
350 * Note, we can have multiple files for different busses in here due
351 * to the mess that USB has for its device tree...
353 static struct bus_file {
357 { .bus = "scsi", .file = "vendor" },
358 { .bus = "usb", .file = "idVendor" },
359 { .bus = "usb", .file = "iInterface" },
360 { .bus = "usb-serial", .file = "detach_state" },
361 { .bus = "ide", .file = "detach_state" },
362 { .bus = "pci", .file = "vendor" },
366 static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device)
368 /* sleep until we see the file for this specific bus type show up this
369 * is needed because we can easily out-run the kernel in looking for
370 * these files before the paticular subsystem has created them in the
371 * sysfs tree properly.
373 * And people thought that the /sbin/hotplug event system was going to
374 * be slow, poo on you for arguing that before even testing it...
376 struct bus_file *b = &bus_files[0];
377 struct sysfs_attribute *tmpattr;
379 int loop = WAIT_FOR_FILE_SECONDS * WAIT_FOR_FILE_RETRY_FREQ;
382 if (b->bus == NULL) {
385 /* give the kernel a chance to create the file */
386 usleep(1000 * 1000 / WAIT_FOR_FILE_RETRY_FREQ);
392 if (strcmp(sysfs_device->bus, b->bus) == 0) {
394 dbg("looking for file '%s' on bus '%s'", b->file, b->bus);
395 tmpattr = sysfs_get_device_attr(sysfs_device, b->file);
400 dbg("can't find '%s' file", b->file);
405 dbg("did not find bus type '%s' on list of bus_id_files, "
406 "please report to <linux-hotplug-devel@lists.sourceforge.net>",
409 return; /* here to prevent compiler warning... */
412 static void fix_kernel_name(struct udevice *udev)
414 char *temp = udev->kernel_name;
416 while (*temp != 0x00) {
417 /* Some block devices have a ! in their name,
418 * we need to change that to / */
425 static int execute_program(const char *path, char *value, int len)
433 char arg[PROGRAM_SIZE];
434 char *argv[(PROGRAM_SIZE / 2) + 1];
437 strfieldcpy(arg, path);
439 if (strchr(path, ' ')) {
441 while (pos != NULL) {
442 if (pos[0] == '\'') {
443 /* don't separate if in apostrophes */
445 argv[i] = strsep(&pos, "\'");
446 while (pos && pos[0] == ' ')
449 argv[i] = strsep(&pos, " ");
451 dbg("arg[%i] '%s'", i, argv[i]);
455 dbg("execute '%s' with parsed arguments", arg);
458 argv[1] = main_argv[1];
460 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
473 /* dup2 write side of pipe to STDOUT */
474 dup2(fds[1], STDOUT_FILENO);
475 retval = execv(arg, argv);
477 info(FIELD_PROGRAM " execution of '%s' failed", path);
483 /* parent reads from fds[0] */
488 count = read(fds[0], value + i, len - i-1);
494 dbg("result len %d too short", len);
501 dbg("read failed with '%s'", strerror(errno));
505 if (i > 0 && value[i-1] == '\n')
508 dbg("result is '%s'", value);
513 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
514 dbg("exec program status 0x%x", status);
521 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
523 struct sysfs_attribute *tmpattr = NULL;
526 dbg("look for device attribute '%s'", attr);
527 /* try to find the attribute in the class device directory */
528 tmpattr = sysfs_get_classdev_attr(class_dev, attr);
532 /* look in the class device directory if present */
534 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
542 c = strchr(tmpattr->value, '\n');
546 dbg("found attribute '%s'", tmpattr->path);
550 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
552 struct sysfs_attribute *tmpattr;
556 if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
559 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file);
563 /* strip trailing whitespace of value, if not asked to match for it */
564 if (! isspace(pair->value[strlen(pair->value)-1])) {
565 i = len = strlen(tmpattr->value);
566 while (i > 0 && isspace(tmpattr->value[i-1]))
569 tmpattr->value[i] = '\0';
570 dbg("remove %i trailing whitespace chars from '%s'",
571 len - i, tmpattr->value);
575 dbg("compare attribute '%s' value '%s' with '%s'",
576 pair->file, tmpattr->value, pair->value);
577 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
580 dbg("found matching attribute '%s' with value '%s'",
581 pair->file, pair->value);
585 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
587 struct sysfs_pair *pair;
590 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
591 pair = &dev->sysfs_pair[i];
592 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
594 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
595 dbg("sysfs attribute doesn't match");
603 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
605 char path[SYSFS_PATH_MAX];
608 /* we have to have a sysfs device for ID to work */
612 strfieldcpy(path, sysfs_device->path);
613 temp = strrchr(path, '/');
615 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
616 if (strcmp_pattern(dev->id, temp) != 0)
622 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
624 char path[SYSFS_PATH_MAX];
628 /* we have to have a sysfs device for PLACE to work */
633 strfieldcpy(path, sysfs_device->path);
634 temp = strrchr(path, '/');
635 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
636 if (strstr(temp, dev->place) != NULL) {
640 temp = strrchr(path, '/');
641 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
642 if (strstr(temp, dev->place) != NULL)
646 dbg("place doesn't match");
653 static int whitelist_search(struct sysfs_class_device *class_dev)
655 char *sysblock = "/sys/block";
658 static char *list[] = {
679 if (strncmp(class_dev->path, sysblock, strlen(sysblock)))
682 for (i=0; list[i] != NULL; i++)
683 if (!strncmp(class_dev->name, list[i], strlen(list[i])))
689 static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev)
691 struct sysfs_device *sysfs_device;
692 struct sysfs_class_device *class_dev_parent;
695 /* Figure out where the device symlink is at. For char devices this will
696 * always be in the class_dev->path. But for block devices, it's different.
697 * The main block device will have the device symlink in it's path, but
698 * all partitions have the symlink in its parent directory.
699 * But we need to watch out for block devices that do not have parents, yet
700 * look like a partition (fd0, loop0, etc.) They all do not have a device
701 * symlink yet. We do sit and spin on waiting for them right now unless
702 * they happen to be in the whitelist in which case we exit.
704 class_dev_parent = sysfs_get_classdev_parent(class_dev);
705 if (class_dev_parent != NULL)
706 dbg("given class device has a parent, use this instead");
708 loop = WAIT_FOR_FILE_SECONDS * WAIT_FOR_FILE_RETRY_FREQ;
711 if (whitelist_search(class_dev)) {
715 usleep(1000 * 1000 / WAIT_FOR_FILE_RETRY_FREQ);
718 if (class_dev_parent)
719 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
721 sysfs_device = sysfs_get_classdev_device(class_dev);
722 if (sysfs_device != NULL)
725 dbg("timed out waiting for device symlink, continuing on anyway...");
728 /* We have another issue with just the wait above - the sysfs part of
729 * the kernel may not be quick enough to have created the link to the
730 * device under the "bus" subsystem. Due to this, the sysfs_device->bus
731 * will not contain the actual bus name :(
734 if (sysfs_device->bus[0] != '\0')
739 usleep(1000 * 1000 / WAIT_FOR_FILE_RETRY_FREQ);
740 sysfs_get_device_bus(sysfs_device);
742 if (sysfs_device->bus[0] != '\0')
745 dbg("timed out waiting to find the device bus, continuing on anyway");
748 dbg("device %s is registered with bus '%s'",
749 sysfs_device->name, sysfs_device->bus);
755 static int match_rule(struct config_device *dev, struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
758 /* check for matching bus value */
759 if (dev->bus[0] != '\0') {
760 if (sysfs_device == NULL) {
761 dbg("device has no bus");
764 dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
765 if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
766 dbg(FIELD_BUS " is not matching");
769 dbg(FIELD_BUS " matches");
773 /* check for matching kernel name*/
774 if (dev->kernel[0] != '\0') {
775 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'", dev->kernel, class_dev->name);
776 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
777 dbg(FIELD_KERNEL " is not matching");
780 dbg(FIELD_KERNEL " matches");
784 /* check for matching bus id */
785 if (dev->id[0] != '\0') {
786 dbg("check " FIELD_ID);
787 if (match_id(dev, class_dev, sysfs_device) != 0) {
788 dbg(FIELD_ID " is not matching");
791 dbg(FIELD_ID " matches");
795 /* check for matching place of device */
796 if (dev->place[0] != '\0') {
797 dbg("check " FIELD_PLACE);
798 if (match_place(dev, class_dev, sysfs_device) != 0) {
799 dbg(FIELD_PLACE " is not matching");
802 dbg(FIELD_PLACE " matches");
806 /* check for matching sysfs pairs */
807 if (dev->sysfs_pair[0].file[0] != '\0') {
808 dbg("check " FIELD_SYSFS " pairs");
809 if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
810 dbg(FIELD_SYSFS " is not matching");
813 dbg(FIELD_SYSFS " matches");
817 /* execute external program */
818 if (dev->program[0] != '\0') {
819 char program[PROGRAM_SIZE];
821 dbg("check " FIELD_PROGRAM);
822 strfieldcpy(program, dev->program);
823 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
824 if (execute_program(program, udev->program_result, NAME_SIZE) != 0) {
825 dbg(FIELD_PROGRAM " returned nonzero");
828 dbg(FIELD_PROGRAM " returned successful");
832 /* check for matching result of external program */
833 if (dev->result[0] != '\0') {
834 dbg("check for " FIELD_RESULT
835 " dev->result='%s', udev->program_result='%s'",
836 dev->result, udev->program_result);
837 if (strcmp_pattern(dev->result, udev->program_result) != 0) {
838 dbg(FIELD_RESULT " is not matching");
841 dbg(FIELD_RESULT " matches");
845 /* Yeah, we matched! */
849 dbg("try parent sysfs device");
850 sysfs_device = sysfs_get_device_parent(sysfs_device);
851 if (sysfs_device == NULL)
853 dbg("sysfs_device->path='%s'", sysfs_device->path);
854 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
855 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
860 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
862 struct sysfs_device *sysfs_device = NULL;
863 struct config_device *dev;
864 struct perm_device *perm;
870 /* find the sysfs_device associated with this class device */
871 sysfs_device = get_sysfs_device(class_dev);
873 dbg("sysfs_device->path='%s'", sysfs_device->path);
874 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
875 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
876 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
877 wait_for_device_to_initialize(sysfs_device);
879 dbg("class_dev->name = '%s'", class_dev->name);
881 strfieldcpy(udev->kernel_name, class_dev->name);
882 fix_kernel_name(udev);
883 dbg("udev->kernel_name = '%s'", udev->kernel_name);
885 /* get kernel number */
886 pos = class_dev->name + strlen(class_dev->name);
887 while (isdigit(*(pos-1)))
889 strfieldcpy(udev->kernel_number, pos);
890 dbg("kernel_number='%s'", udev->kernel_number);
892 /* look for a matching rule to apply */
893 list_for_each_entry(dev, &config_device_list, node) {
895 if (match_rule(dev, class_dev, udev, sysfs_device) == 0) {
896 if (dev->name[0] == '\0' && dev->symlink[0] == '\0') {
897 info("configured rule in '%s' at line %i applied, '%s' is ignored",
898 dev->config_file, dev->config_line, udev->kernel_name);
902 if (dev->symlink[0] != '\0') {
903 char temp[NAME_SIZE];
905 info("configured rule in '%s' at line %i applied, added symlink '%s'",
906 dev->config_file, dev->config_line, dev->symlink);
907 strfieldcpy(temp, dev->symlink);
908 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
909 if (udev->symlink[0] != '\0')
910 strfieldcat(udev->symlink, " ");
911 strfieldcat(udev->symlink, temp);
914 if (dev->name[0] != '\0') {
915 /* apply all_partitions flag only at a main block device */
916 if (dev->partitions > 0 &&
917 (udev->type != 'b' || udev->kernel_number[0] != '\0'))
920 info("configured rule in '%s' at line %i applied, '%s' becomes '%s'",
921 dev->config_file, dev->config_line, udev->kernel_name, dev->name);
922 strfieldcpy(udev->name, dev->name);
927 /* no rule was found so we use the kernel name */
928 strfieldcpy(udev->name, udev->kernel_name);
929 if (udev->type == 'n')
935 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
936 strfieldcpy(udev->config_file, dev->config_file);
937 udev->config_line = dev->config_line;
939 if (udev->type == 'n')
942 udev->partitions = dev->partitions;
944 /* get permissions given in rule */
945 set_empty_perms(udev, dev->mode,
950 /* get permissions given in config file or set defaults */
951 perm = find_perm(udev->name);
953 set_empty_perms(udev, perm->mode,
957 set_empty_perms(udev, get_default_mode(),
959 get_default_group());
962 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
963 udev->name, udev->owner, udev->group, udev->mode);
966 /* store time of action */
968 udev->config_uptime = info.uptime;
973 int namedev_init(void)
977 retval = namedev_init_rules();
981 retval = namedev_init_permissions();
985 dump_config_dev_list();
986 dump_perm_dev_list();