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.
38 #include "udev_version.h"
41 #include "libsysfs/libsysfs.h"
42 #include "klibc_fixups.h"
44 LIST_HEAD(config_device_list);
45 LIST_HEAD(perm_device_list);
47 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
48 static int strcmp_pattern(const char *p, const char *s)
53 return (p[0] != '\0');
64 while ((p[0] != '\0') && (p[0] != ']')) {
67 if ((s[0] >= p[0]) && (s[0] <= p[2]))
71 match = (p[0] == s[0]);
75 while ((p[0] != '\0') && (p[0] != ']'))
78 return strcmp_pattern(p+1, s+1);
84 if (strcmp_pattern(p, s+1))
85 return strcmp_pattern(p+1, s);
93 if ((p[0] == s[0]) || (p[0] == '?'))
94 return strcmp_pattern(p+1, s+1);
100 #define copy_var(a, b, var) \
104 #define copy_string(a, b, var) \
105 if (strlen(b->var)) \
106 strcpy(a->var, b->var);
108 int add_perm_dev(struct perm_device *new_dev)
110 struct perm_device *dev;
111 struct perm_device *tmp_dev;
113 /* update the values if we already have the device */
114 list_for_each_entry(dev, &perm_device_list, node) {
115 if (strcmp_pattern(new_dev->name, dev->name))
117 copy_var(dev, new_dev, mode);
118 copy_string(dev, new_dev, owner);
119 copy_string(dev, new_dev, group);
123 /* not found, add new structure to the perm list */
124 tmp_dev = malloc(sizeof(*tmp_dev));
127 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
128 list_add_tail(&tmp_dev->node, &perm_device_list);
129 //dump_perm_dev(tmp_dev);
133 static struct perm_device *find_perm(char *name)
135 struct perm_device *perm;
137 list_for_each_entry(perm, &perm_device_list, node) {
138 if (strcmp_pattern(perm->name, name))
145 static mode_t get_default_mode(void)
147 mode_t mode = 0600; /* default to owner rw only */
149 if (strlen(default_mode_str) != 0)
150 mode = strtol(default_mode_str, NULL, 8);
155 static char *get_default_owner(void)
157 if (strlen(default_owner_str) == 0)
158 strncpy(default_owner_str, "root", OWNER_SIZE);
160 return default_owner_str;
163 static char *get_default_group(void)
165 if (strlen(default_group_str) == 0)
166 strncpy(default_group_str, "root", GROUP_SIZE);
168 return default_group_str;
171 static void apply_format(struct udevice *udev, unsigned char *string)
173 char temp[NAME_SIZE];
174 char temp1[NAME_SIZE];
184 pos = strchr(pos, '%');
189 if (isdigit(tail[0])) {
190 num = (int) strtoul(&pos[1], &tail, 10);
192 dbg("format parsing error '%s'", pos+1);
196 strfieldcpy(temp, tail+1);
200 if (strlen(udev->bus_id) == 0)
202 strcat(pos, udev->bus_id);
203 dbg("substitute bus_id '%s'", udev->bus_id);
206 if (strlen(udev->kernel_name) == 0)
208 strcat(pos, udev->kernel_name);
209 dbg("substitute kernel name '%s'", udev->kernel_name);
212 if (strlen(udev->kernel_number) == 0)
214 strcat(pos, udev->kernel_number);
215 dbg("substitute kernel number '%s'", udev->kernel_number);
218 sprintf(pos, "%u", udev->minor);
219 dbg("substitute minor number '%u'", udev->minor);
222 sprintf(pos, "%u", udev->major);
223 dbg("substitute major number '%u'", udev->major);
226 if (strlen(udev->program_result) == 0)
229 /* get part of return string */
230 strncpy(temp1, udev->program_result, sizeof(temp1));
234 pos3 = strsep(&pos2, " ");
236 dbg("requested part of result string not found");
242 dbg("substitute part of result string '%s'", pos3);
245 strcat(pos, udev->program_result);
246 dbg("substitute result string '%s'", udev->program_result);
254 dbg("unknown substitution type '%%%c'", pos[1]);
257 strcat(string, temp);
263 static struct bus_file {
267 { .bus = "scsi", .file = "vendor" },
268 { .bus = "usb", .file = "idVendor" },
269 { .bus = "usb-serial", .file = "detach_state" },
270 { .bus = "ide", .file = "detach_state" },
271 { .bus = "pci", .file = "vendor" },
275 #define SECONDS_TO_WAIT_FOR_FILE 10
276 static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device)
278 /* sleep until we see the file for this specific bus type show up this
279 * is needed because we can easily out-run the kernel in looking for
280 * these files before the paticular subsystem has created them in the
281 * sysfs tree properly.
283 * And people thought that the /sbin/hotplug event system was going to
284 * be slow, poo on you for arguing that before even testing it...
286 struct bus_file *b = &bus_files[0];
287 struct sysfs_attribute *tmpattr;
293 if (strcmp(sysfs_device->bus, b->bus) == 0) {
295 loop = SECONDS_TO_WAIT_FOR_FILE;
297 dbg("looking for file '%s' on bus '%s'", b->file, b->bus);
298 tmpattr = sysfs_get_device_attr(sysfs_device, b->file);
303 /* sleep to give the kernel a chance to create the file */
306 dbg("timed out waiting for '%s' file, continuing on anyway...", b->file);
311 dbg("did not find bus type '%s' on list of bus_id_files, contact greg@kroah.com", sysfs_device->bus);
313 return; /* here to prevent compiler warning... */
316 static int execute_program(char *path, char *value, int len)
326 char *args[PROGRAM_MAXARG];
329 dbg("executing '%s'", path);
339 close(STDOUT_FILENO);
340 dup(fds[1]); /* dup write side of pipe to STDOUT */
341 if (strchr(path, ' ')) {
342 /* exec with arguments */
344 for (i=0; i < PROGRAM_MAXARG-1; i++) {
345 args[i] = strsep(&pos, " ");
350 dbg("too many args - %d", i);
353 retval = execv(args[0], args);
355 retval = execv(path, main_argv);
357 dbg("child execve failed");
363 /* parent reads from fds[0] */
367 res = read(fds[0], buffer, sizeof(buffer) - 1);
372 dbg("result len %d too short", len);
376 dbg("result value already set");
380 strncpy(value, buffer, len);
381 pos = value + strlen(value)-1;
384 dbg("result is '%s'", value);
390 dbg("wait failed result %d", res);
394 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
395 dbg("exec program status 0x%x", status);
402 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
404 struct sysfs_attribute *tmpattr = NULL;
407 if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
410 dbg("look for device attribute '%s'", pair->file);
411 /* try to find the attribute in the class device directory */
412 tmpattr = sysfs_get_classdev_attr(class_dev, pair->file);
416 /* look in the class device directory if present */
418 tmpattr = sysfs_get_device_attr(sysfs_device, pair->file);
425 c = tmpattr->value + strlen(tmpattr->value)-1;
428 dbg("compare attribute '%s' value '%s' with '%s'",
429 pair->file, tmpattr->value, pair->value);
430 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
433 dbg("found matching attribute '%s' with value '%s'",
434 pair->file, pair->value);
438 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
440 struct sysfs_pair *pair;
443 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
444 pair = &dev->sysfs_pair[i];
445 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
447 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
448 dbg("sysfs attribute doesn't match");
456 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
458 char path[SYSFS_PATH_MAX];
462 /* we have to have a sysfs device for ID to work */
467 strfieldcpy(path, sysfs_device->path);
468 temp = strrchr(path, '/');
469 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
470 if (strstr(temp, dev->id) != NULL) {
474 temp = strrchr(path, '/');
475 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
476 if (strstr(temp, dev->id) != NULL)
480 dbg("id doesn't match");
487 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
489 char path[SYSFS_PATH_MAX];
493 /* we have to have a sysfs device for PLACE to work */
498 strfieldcpy(path, sysfs_device->path);
499 temp = strrchr(path, '/');
500 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
501 if (strstr(temp, dev->place) != NULL) {
505 temp = strrchr(path, '/');
506 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
507 if (strstr(temp, dev->place) != NULL)
511 dbg("place doesn't match");
518 static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev)
520 struct sysfs_device *sysfs_device;
521 struct sysfs_class_device *class_dev_parent;
522 struct timespec tspec;
525 /* Figure out where the device symlink is at. For char devices this will
526 * always be in the class_dev->path. But for block devices, it's different.
527 * The main block device will have the device symlink in it's path, but
528 * all partitions have the symlink in its parent directory.
529 * But we need to watch out for block devices that do not have parents, yet
530 * look like a partition (fd0, loop0, etc.) They all do not have a device
531 * symlink yet. We do sit and spin on waiting for them right now, we should
532 * possibly have a whitelist for these devices here...
534 class_dev_parent = sysfs_get_classdev_parent(class_dev);
535 if (class_dev_parent)
536 dbg("Really a partition");
539 tspec.tv_nsec = 10000000; /* sleep 10 millisec */
542 nanosleep(&tspec, NULL);
543 if (class_dev_parent)
544 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
546 sysfs_device = sysfs_get_classdev_device(class_dev);
548 if (sysfs_device != NULL)
551 dbg("timed out waiting for device symlink, continuing on anyway...");
554 /* We have another issue with just the wait above - the sysfs part of
555 * the kernel may not be quick enough to have created the link to the
556 * device under the "bus" subsystem. Due to this, the sysfs_device->bus
557 * will not contain the actual bus name :(
559 * Libsysfs now provides a new API sysfs_get_device_bus(), so use it
563 if (sysfs_device->bus[0] != '\0')
567 tspec.tv_nsec = 10000000;
569 nanosleep(&tspec, NULL);
570 sysfs_get_device_bus(sysfs_device);
572 if (sysfs_device->bus[0] != '\0')
575 dbg("timed out waiting to find the device bus, continuing on anyway");
578 dbg("device %s is registered with bus '%s'",
579 sysfs_device->name, sysfs_device->bus);
585 static int match_rule(struct config_device *dev, struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
588 /* check for matching bus value */
589 if (dev->bus[0] != '\0') {
590 if (sysfs_device == NULL) {
591 dbg("device has no bus");
594 dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
595 if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
596 dbg(FIELD_BUS " is not matching");
599 dbg(FIELD_BUS " matches");
603 /* check for matching kernel name*/
604 if (dev->kernel[0] != '\0') {
605 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'", dev->kernel, class_dev->name);
606 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
607 dbg(FIELD_KERNEL " is not matching");
610 dbg(FIELD_KERNEL " matches");
614 /* check for matching bus id */
615 if (dev->id[0] != '\0') {
616 dbg("check " FIELD_ID);
617 if (match_id(dev, class_dev, sysfs_device) != 0) {
618 dbg(FIELD_ID " is not matching");
621 dbg(FIELD_ID " matches");
625 /* check for matching place of device */
626 if (dev->place[0] != '\0') {
627 dbg("check " FIELD_PLACE);
628 if (match_place(dev, class_dev, sysfs_device) != 0) {
629 dbg(FIELD_PLACE " is not matching");
632 dbg(FIELD_PLACE " matches");
636 /* check for matching sysfs pairs */
637 if (dev->sysfs_pair[0].file[0] != '\0') {
638 dbg("check " FIELD_SYSFS " pairs");
639 if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
640 dbg(FIELD_SYSFS " is not matching");
643 dbg(FIELD_SYSFS " matches");
647 /* execute external program */
648 if (dev->program[0] != '\0') {
649 dbg("check " FIELD_PROGRAM);
650 apply_format(udev, dev->program);
651 if (execute_program(dev->program, udev->program_result, NAME_SIZE) != 0) {
652 dbg(FIELD_PROGRAM " returned nozero");
655 dbg(FIELD_PROGRAM " returned successful");
659 /* check for matching result of external program */
660 if (dev->result[0] != '\0') {
661 dbg("check for " FIELD_RESULT
662 " dev->result='%s', udev->program_result='%s'",
663 dev->result, udev->program_result);
664 if (strcmp_pattern(dev->result, udev->program_result) != 0) {
665 dbg(FIELD_RESULT " is not matching");
668 dbg(FIELD_RESULT " matches");
672 /* check if we are instructed to ignore this device */
673 if (dev->name[0] == '\0') {
674 dbg("instructed to ignore this device");
678 /* Yeah, we matched! */
682 sysfs_device = sysfs_get_device_parent(sysfs_device);
683 if (sysfs_device == NULL)
685 dbg("sysfs_device->path='%s'", sysfs_device->path);
686 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
687 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
692 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
694 struct sysfs_device *sysfs_device = NULL;
695 struct config_device *dev;
696 struct perm_device *perm;
701 /* find the sysfs_device associated with this class device */
702 sysfs_device = get_sysfs_device(class_dev);
704 dbg("sysfs_device->path='%s'", sysfs_device->path);
705 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
706 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
707 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
708 wait_for_device_to_initialize(sysfs_device);
710 dbg("class_dev->name = '%s'", class_dev->name);
713 strfieldcpy(udev->kernel_name, class_dev->name);
715 /* get kernel number */
716 pos = class_dev->name + strlen(class_dev->name);
717 while (isdigit(*(pos-1)))
719 strfieldcpy(udev->kernel_number, pos);
720 dbg("kernel_number='%s'", udev->kernel_number);
722 /* look for a matching rule to apply */
723 list_for_each_entry(dev, &config_device_list, node) {
726 if (match_rule(dev, class_dev, udev, sysfs_device) == 0) {
727 /* Yup, this rule belongs to us! */
728 info("configured rule in '%s' at line %i applied, '%s' becomes '%s'",
729 udev_rules_filename, dev->config_line, udev->kernel_name, dev->name);
730 strfieldcpy(udev->name, dev->name);
731 strfieldcpy(udev->symlink, dev->symlink);
736 /* no rule was found so we use the kernel name */
737 strfieldcpy(udev->name, class_dev->name);
741 /* substitute placeholder */
742 apply_format(udev, udev->name);
743 apply_format(udev, udev->symlink);
746 perm = find_perm(udev->name);
748 udev->mode = perm->mode;
749 strfieldcpy(udev->owner, perm->owner);
750 strfieldcpy(udev->group, perm->group);
752 /* no matching perms found :( */
753 udev->mode = get_default_mode();
754 strncpy(udev->owner, get_default_owner(), OWNER_SIZE);
755 strncpy(udev->group, get_default_group(), GROUP_SIZE);
757 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
758 udev->name, udev->owner, udev->group, udev->mode);
763 int namedev_init(void)
767 retval = namedev_init_rules();
771 retval = namedev_init_permissions();
775 dump_config_dev_list();
776 dump_perm_dev_list();