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 "libsysfs/sysfs/libsysfs.h"
38 #include "udev_utils.h"
39 #include "udev_version.h"
44 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr);
46 LIST_HEAD(config_device_list);
47 LIST_HEAD(perm_device_list);
50 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
51 static int strcmp_pattern(const char *p, const char *s)
56 return (p[0] != '\0');
67 while ((p[0] != '\0') && (p[0] != ']')) {
70 if ((s[0] >= p[0]) && (s[0] <= p[2]))
74 match = (p[0] == s[0]);
78 while ((p[0] != '\0') && (p[0] != ']'))
81 return strcmp_pattern(p+1, s+1);
87 if (strcmp_pattern(p, s+1))
88 return strcmp_pattern(p+1, s);
96 if ((p[0] == s[0]) || (p[0] == '?'))
97 return strcmp_pattern(p+1, s+1);
103 static struct perm_device *find_perm_entry(const char *name)
105 struct perm_device *perm;
107 list_for_each_entry(perm, &perm_device_list, node) {
108 if (strcmp_pattern(perm->name, name))
115 /* extract possible {attr} and move str behind it */
116 static char *get_format_attribute(char **str)
121 if (*str[0] == '{') {
122 pos = strchr(*str, '}');
124 dbg("missing closing brace for format");
130 dbg("attribute='%s', str='%s'", attr, *str);
135 /* extract possible format length and move str behind it*/
136 static int get_format_len(char **str)
141 if (isdigit(*str[0])) {
142 num = (int) strtoul(*str, &tail, 10);
145 dbg("format length=%i", num);
148 dbg("format parsing error '%s'", *str);
154 /** Finds the lowest positive N such that <name>N isn't present in
155 * $(udevroot) either as a file or a symlink.
157 * @param name Name to check for
158 * @return 0 if <name> didn't exist and N otherwise.
160 static int find_free_number(struct udevice *udev, const char *name)
162 char filename[NAME_SIZE];
164 struct udevice db_udev;
166 strfieldcpy(filename, name);
168 dbg("look for existing node '%s'", filename);
169 memset(&db_udev, 0x00, sizeof(struct udevice));
170 if (udev_db_get_device_byname(&db_udev, filename) != 0) {
171 dbg("free num=%d", num);
177 info("find_free_number gone crazy (num=%d), aborted", num);
180 snprintf(filename, NAME_SIZE, "%s%d", name, num);
181 filename[NAME_SIZE-1] = '\0';
185 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
186 struct sysfs_class_device *class_dev,
187 struct sysfs_device *sysfs_device)
189 char temp[NAME_SIZE];
190 char temp2[NAME_SIZE];
200 struct sysfs_attribute *tmpattr;
201 unsigned int next_free_number;
205 pos = strchr(pos, '%');
211 len = get_format_len(&tail);
213 strfieldcpy(temp, tail+1);
215 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
216 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 /* strip trailing whitespace of matching value */
282 if (isspace(tmpattr->value[strlen(tmpattr->value)-1])) {
283 i = len = strlen(tmpattr->value);
284 while (i > 0 && isspace(tmpattr->value[i-1]))
287 tmpattr->value[i] = '\0';
288 dbg("remove %i trailing whitespace chars from '%s'",
289 len - i, tmpattr->value);
292 strfieldcatmax(string, tmpattr->value, maxsize);
293 dbg("substitute sysfs value '%s'", tmpattr->value);
295 dbg("missing attribute");
299 strfieldcatmax(string, "%", maxsize);
303 next_free_number = find_free_number(udev, string);
304 if (next_free_number > 0) {
305 sprintf(temp2, "%d", next_free_number);
306 strfieldcatmax(string, temp2, maxsize);
310 dbg("unknown substitution type '%%%c'", c);
313 /* truncate to specified length */
317 strfieldcatmax(string, tail, maxsize);
321 static void fix_kernel_name(struct udevice *udev)
323 char *temp = udev->kernel_name;
325 while (*temp != 0x00) {
326 /* Some block devices have a ! in their name,
327 * we need to change that to / */
334 static int execute_program(struct udevice *udev, const char *path, char *value, int len)
342 char arg[PROGRAM_SIZE];
343 char *argv[(PROGRAM_SIZE / 2) + 1];
346 strfieldcpy(arg, path);
348 if (strchr(path, ' ')) {
350 while (pos != NULL) {
351 if (pos[0] == '\'') {
352 /* don't separate if in apostrophes */
354 argv[i] = strsep(&pos, "\'");
355 while (pos && pos[0] == ' ')
358 argv[i] = strsep(&pos, " ");
360 dbg("arg[%i] '%s'", i, argv[i]);
364 dbg("execute '%s' with parsed arguments", arg);
367 argv[1] = udev->subsystem;
369 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
382 /* dup2 write side of pipe to STDOUT */
383 dup2(fds[1], STDOUT_FILENO);
384 retval = execv(arg, argv);
386 info(FIELD_PROGRAM " execution of '%s' failed", path);
392 /* parent reads from fds[0] */
397 count = read(fds[0], value + i, len - i-1);
403 dbg("result len %d too short", len);
410 dbg("read failed with '%s'", strerror(errno));
414 if (i > 0 && value[i-1] == '\n')
417 dbg("result is '%s'", value);
420 waitpid(pid, &status, 0);
422 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
423 dbg("exec program status 0x%x", status);
430 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
432 struct sysfs_attribute *tmpattr = NULL;
435 dbg("look for device attribute '%s'", attr);
436 /* try to find the attribute in the class device directory */
437 tmpattr = sysfs_get_classdev_attr(class_dev, attr);
441 /* look in the class device directory if present */
443 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
451 c = strchr(tmpattr->value, '\n');
455 dbg("found attribute '%s'", tmpattr->path);
459 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
461 struct sysfs_attribute *tmpattr;
465 if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
468 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file);
472 /* strip trailing whitespace of value, if not asked to match for it */
473 if (! isspace(pair->value[strlen(pair->value)-1])) {
474 i = len = strlen(tmpattr->value);
475 while (i > 0 && isspace(tmpattr->value[i-1]))
478 tmpattr->value[i] = '\0';
479 dbg("remove %i trailing whitespace chars from '%s'",
480 len - i, tmpattr->value);
484 dbg("compare attribute '%s' value '%s' with '%s'",
485 pair->file, tmpattr->value, pair->value);
486 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
489 dbg("found matching attribute '%s' with value '%s'",
490 pair->file, pair->value);
494 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
496 struct sysfs_pair *pair;
499 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
500 pair = &dev->sysfs_pair[i];
501 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
503 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
504 dbg("sysfs attribute doesn't match");
512 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
514 char path[SYSFS_PATH_MAX];
517 /* we have to have a sysfs device for ID to work */
521 strfieldcpy(path, sysfs_device->path);
522 temp = strrchr(path, '/');
524 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
525 if (strcmp_pattern(dev->id, temp) != 0)
531 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
533 char path[SYSFS_PATH_MAX];
537 /* we have to have a sysfs device for PLACE to work */
542 strfieldcpy(path, sysfs_device->path);
543 temp = strrchr(path, '/');
544 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
545 if (strstr(temp, dev->place) != NULL) {
549 temp = strrchr(path, '/');
550 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
551 if (strstr(temp, dev->place) != NULL)
555 dbg("place doesn't match");
562 static int match_rule(struct udevice *udev, struct config_device *dev,
563 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
566 /* check for matching bus value */
567 if (dev->bus[0] != '\0') {
568 if (sysfs_device == NULL) {
569 dbg("device has no bus");
572 dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'",
573 dev->bus, sysfs_device->bus);
574 if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
575 dbg(FIELD_BUS " is not matching");
578 dbg(FIELD_BUS " matches");
582 /* check for matching kernel name */
583 if (dev->kernel[0] != '\0') {
584 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'",
585 dev->kernel, class_dev->name);
586 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
587 dbg(FIELD_KERNEL " is not matching");
590 dbg(FIELD_KERNEL " matches");
594 /* check for matching subsystem */
595 if (dev->subsystem[0] != '\0') {
596 dbg("check for " FIELD_SUBSYSTEM " dev->subsystem='%s' class_dev->name='%s'",
597 dev->subsystem, class_dev->name);
598 if (strcmp_pattern(dev->subsystem, udev->subsystem) != 0) {
599 dbg(FIELD_SUBSYSTEM " is not matching");
602 dbg(FIELD_SUBSYSTEM " matches");
606 /* check for matching driver */
607 if (dev->driver[0] != '\0') {
608 dbg("check for " FIELD_DRIVER " dev->driver='%s' sysfs_device->driver_name='%s'",
609 dev->driver, sysfs_device->driver_name);
610 if (strcmp_pattern(dev->driver, sysfs_device->driver_name) != 0) {
611 dbg(FIELD_DRIVER " is not matching");
614 dbg(FIELD_DRIVER " matches");
618 /* check for matching bus id */
619 if (dev->id[0] != '\0') {
620 dbg("check " FIELD_ID);
621 if (match_id(dev, class_dev, sysfs_device) != 0) {
622 dbg(FIELD_ID " is not matching");
625 dbg(FIELD_ID " matches");
629 /* check for matching place of device */
630 if (dev->place[0] != '\0') {
631 dbg("check " FIELD_PLACE);
632 if (match_place(dev, class_dev, sysfs_device) != 0) {
633 dbg(FIELD_PLACE " is not matching");
636 dbg(FIELD_PLACE " matches");
640 /* check for matching sysfs pairs */
641 if (dev->sysfs_pair[0].file[0] != '\0') {
642 dbg("check " FIELD_SYSFS " pairs");
643 if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
644 dbg(FIELD_SYSFS " is not matching");
647 dbg(FIELD_SYSFS " matches");
651 /* execute external program */
652 if (dev->program[0] != '\0') {
653 char program[PROGRAM_SIZE];
655 dbg("check " FIELD_PROGRAM);
656 strfieldcpy(program, dev->program);
657 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
658 if (execute_program(udev, program, udev->program_result, NAME_SIZE) != 0) {
659 dbg(FIELD_PROGRAM " returned nonzero");
662 dbg(FIELD_PROGRAM " returned successful");
666 /* check for matching result of external program */
667 if (dev->result[0] != '\0') {
668 dbg("check for " FIELD_RESULT " dev->result='%s', udev->program_result='%s'",
669 dev->result, udev->program_result);
670 if (strcmp_pattern(dev->result, udev->program_result) != 0) {
671 dbg(FIELD_RESULT " is not matching");
674 dbg(FIELD_RESULT " matches");
678 /* Yeah, we matched! */
682 dbg("try parent sysfs device");
683 sysfs_device = sysfs_get_device_parent(sysfs_device);
684 if (sysfs_device == NULL)
686 dbg("sysfs_device->path='%s'", sysfs_device->path);
687 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
692 int namedev_name_device(struct udevice *udev, struct sysfs_class_device *class_dev)
694 struct sysfs_class_device *class_dev_parent;
695 struct sysfs_device *sysfs_device = NULL;
696 struct config_device *dev;
697 struct perm_device *perm;
701 dbg("class_dev->name='%s'", class_dev->name);
703 /* Figure out where the "device"-symlink is at. For char devices this will
704 * always be in the class_dev->path. On block devices, only the main block
705 * device will have the device symlink in it's path. All partition devices
706 * need to look at the symlink in its parent directory.
708 class_dev_parent = sysfs_get_classdev_parent(class_dev);
709 if (class_dev_parent != NULL) {
710 dbg("given class device has a parent, use this instead");
711 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
713 sysfs_device = sysfs_get_classdev_device(class_dev);
717 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
718 sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
719 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
722 strfieldcpy(udev->kernel_name, class_dev->name);
723 fix_kernel_name(udev);
724 dbg("udev->kernel_name = '%s'", udev->kernel_name);
726 /* get kernel number */
727 pos = class_dev->name + strlen(class_dev->name);
728 while (isdigit(*(pos-1)))
730 strfieldcpy(udev->kernel_number, pos);
731 dbg("kernel_number='%s'", udev->kernel_number);
733 /* look for a matching rule to apply */
734 list_for_each_entry(dev, &config_device_list, node) {
736 if (match_rule(udev, dev, class_dev, sysfs_device) == 0) {
737 /* empty name and symlink will not create any node */
738 if (dev->name[0] == '\0' && dev->symlink[0] == '\0') {
739 info("configured rule in '%s' at line %i applied, '%s' is ignored",
740 dev->config_file, dev->config_line, udev->kernel_name);
744 /* collect symlinks for the final matching rule */
745 if (dev->symlink[0] != '\0') {
746 char temp[NAME_SIZE];
748 info("configured rule in '%s' at line %i applied, added symlink '%s'",
749 dev->config_file, dev->config_line, dev->symlink);
750 strfieldcpy(temp, dev->symlink);
751 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
752 if (udev->symlink[0] != '\0')
753 strfieldcat(udev->symlink, " ");
754 strfieldcat(udev->symlink, temp);
758 if (dev->name[0] != '\0') {
759 /* apply all_partitions flag only at a main block device */
760 if (dev->partitions > 0 &&
761 (udev->type != 'b' || udev->kernel_number[0] != '\0'))
764 info("configured rule in '%s' at line %i applied, '%s' becomes '%s'",
765 dev->config_file, dev->config_line, udev->kernel_name, dev->name);
767 strfieldcpy(udev->name, dev->name);
768 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
769 strfieldcpy(udev->config_file, dev->config_file);
770 udev->config_line = dev->config_line;
771 udev->ignore_remove = dev->ignore_remove;
773 if (udev->type == 'n')
776 udev->partitions = dev->partitions;
777 udev->mode = dev->mode;
778 strfieldcpy(udev->owner, dev->owner);
779 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
780 strfieldcpy(udev->group, dev->group);
781 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
788 /* no rule matched, so we use the kernel name */
789 strfieldcpy(udev->name, udev->kernel_name);
791 if (udev->type == 'n')
795 /* apply permissions from permissions file to empty fields */
796 perm = find_perm_entry(udev->name);
798 if (udev->mode == 0000)
799 udev->mode = perm->mode;
800 if (udev->owner[0] == '\0')
801 strfieldcpy(udev->owner, perm->owner);
802 if (udev->group[0] == '\0')
803 strfieldcpy(udev->group, perm->group);
806 /* apply permissions from config to empty fields */
807 if (udev->mode == 0000)
808 udev->mode = default_mode;
809 if (udev->owner[0] == '\0')
810 strfieldcpy(udev->owner, default_owner);
811 if (udev->group[0] == '\0')
812 strfieldcpy(udev->group, default_group);
814 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
815 udev->name, udev->owner, udev->group, udev->mode);
821 int namedev_init(void)
825 retval = namedev_init_rules();
829 retval = namedev_init_permissions();
833 dump_config_dev_list();
834 dump_perm_dev_list();