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 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
47 static int strcmp_pattern(const char *p, const char *s)
52 return (p[0] != '\0');
63 while ((p[0] != '\0') && (p[0] != ']')) {
66 if ((s[0] >= p[0]) && (s[0] <= p[2]))
70 match = (p[0] == s[0]);
74 while ((p[0] != '\0') && (p[0] != ']'))
77 return strcmp_pattern(p+1, s+1);
83 if (strcmp_pattern(p, s+1))
84 return strcmp_pattern(p+1, s);
92 if ((p[0] == s[0]) || (p[0] == '?'))
93 return strcmp_pattern(p+1, s+1);
99 /* extract possible {attr} and move str behind it */
100 static char *get_format_attribute(char **str)
105 if (*str[0] == '{') {
106 pos = strchr(*str, '}');
108 dbg("missing closing brace for format");
114 dbg("attribute='%s', str='%s'", attr, *str);
119 /* extract possible format length and move str behind it*/
120 static int get_format_len(char **str)
125 if (isdigit(*str[0])) {
126 num = (int) strtoul(*str, &tail, 10);
129 dbg("format length=%i", num);
132 dbg("format parsing error '%s'", *str);
138 /** Finds the lowest positive N such that <name>N isn't present in
139 * $(udevroot) either as a file or a symlink.
141 * @param name Name to check for
142 * @return 0 if <name> didn't exist and N otherwise.
144 static int find_free_number(struct udevice *udev, const char *name)
146 char filename[NAME_SIZE];
148 struct udevice db_udev;
150 strfieldcpy(filename, name);
152 dbg("look for existing node '%s'", filename);
153 memset(&db_udev, 0x00, sizeof(struct udevice));
154 if (udev_db_get_device_byname(&db_udev, filename) != 0) {
155 dbg("free num=%d", num);
161 info("find_free_number gone crazy (num=%d), aborted", num);
164 snprintf(filename, NAME_SIZE, "%s%d", name, num);
165 filename[NAME_SIZE-1] = '\0';
169 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
170 struct sysfs_class_device *class_dev,
171 struct sysfs_device *sysfs_device)
173 char temp[NAME_SIZE];
174 char temp2[NAME_SIZE];
184 struct sysfs_attribute *tmpattr;
185 unsigned int next_free_number;
189 pos = strchr(pos, '%');
195 len = get_format_len(&tail);
197 strfieldcpy(temp, tail+1);
199 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
200 attr = get_format_attribute(&tail);
205 if (strlen(udev->bus_id) == 0)
207 strfieldcatmax(string, udev->bus_id, maxsize);
208 dbg("substitute bus_id '%s'", udev->bus_id);
211 if (strlen(udev->kernel_name) == 0)
213 strfieldcatmax(string, udev->kernel_name, maxsize);
214 dbg("substitute kernel name '%s'", udev->kernel_name);
217 if (strlen(udev->kernel_number) == 0)
219 strfieldcatmax(string, udev->kernel_number, maxsize);
220 dbg("substitute kernel number '%s'", udev->kernel_number);
223 strintcatmax(string, udev->minor, maxsize);
224 dbg("substitute minor number '%u'", udev->minor);
227 strintcatmax(string, udev->major, maxsize);
228 dbg("substitute major number '%u'", udev->major);
231 if (strlen(udev->program_result) == 0)
233 /* get part part of the result string */
236 i = strtoul(attr, &rest, 10);
238 foreach_strpart(udev->program_result, " \n\r", spos, slen) {
244 dbg("requested part of result string not found");
248 strfieldcpy(temp2, spos);
250 strfieldcpymax(temp2, spos, slen+1);
251 strfieldcatmax(string, temp2, maxsize);
252 dbg("substitute part of result string '%s'", temp2);
254 strfieldcatmax(string, udev->program_result, maxsize);
255 dbg("substitute result string '%s'", udev->program_result);
260 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr);
261 if (tmpattr == NULL) {
262 dbg("sysfa attribute '%s' not found", attr);
265 /* strip trailing whitespace of matching value */
266 if (isspace(tmpattr->value[strlen(tmpattr->value)-1])) {
267 i = len = strlen(tmpattr->value);
268 while (i > 0 && isspace(tmpattr->value[i-1]))
271 tmpattr->value[i] = '\0';
272 dbg("remove %i trailing whitespace chars from '%s'",
273 len - i, tmpattr->value);
276 strfieldcatmax(string, tmpattr->value, maxsize);
277 dbg("substitute sysfs value '%s'", tmpattr->value);
279 dbg("missing attribute");
283 strfieldcatmax(string, "%", maxsize);
287 next_free_number = find_free_number(udev, string);
288 if (next_free_number > 0) {
289 sprintf(temp2, "%d", next_free_number);
290 strfieldcatmax(string, temp2, maxsize);
294 dbg("unknown substitution type '%%%c'", c);
297 /* truncate to specified length */
301 strfieldcatmax(string, tail, maxsize);
305 static void fix_kernel_name(struct udevice *udev)
307 char *temp = udev->kernel_name;
309 while (*temp != 0x00) {
310 /* Some block devices have a ! in their name,
311 * we need to change that to / */
318 static int execute_program(struct udevice *udev, const char *path, char *value, int len)
326 char arg[PROGRAM_SIZE];
327 char *argv[(PROGRAM_SIZE / 2) + 1];
330 strfieldcpy(arg, path);
332 if (strchr(path, ' ')) {
334 while (pos != NULL) {
335 if (pos[0] == '\'') {
336 /* don't separate if in apostrophes */
338 argv[i] = strsep(&pos, "\'");
339 while (pos && pos[0] == ' ')
342 argv[i] = strsep(&pos, " ");
344 dbg("arg[%i] '%s'", i, argv[i]);
348 dbg("execute '%s' with parsed arguments", arg);
351 argv[1] = udev->subsystem;
353 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
366 /* dup2 write side of pipe to STDOUT */
367 dup2(fds[1], STDOUT_FILENO);
368 retval = execv(arg, argv);
370 info(FIELD_PROGRAM " execution of '%s' failed", path);
376 /* parent reads from fds[0] */
381 count = read(fds[0], value + i, len - i-1);
387 dbg("result len %d too short", len);
394 dbg("read failed with '%s'", strerror(errno));
398 if (i > 0 && value[i-1] == '\n')
401 dbg("result is '%s'", value);
404 waitpid(pid, &status, 0);
406 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
407 dbg("exec program status 0x%x", status);
414 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
416 struct sysfs_attribute *tmpattr = NULL;
419 dbg("look for device attribute '%s'", attr);
420 /* try to find the attribute in the class device directory */
421 tmpattr = sysfs_get_classdev_attr(class_dev, attr);
425 /* look in the class device directory if present */
427 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
435 c = strchr(tmpattr->value, '\n');
439 dbg("found attribute '%s'", tmpattr->path);
443 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
445 struct sysfs_attribute *tmpattr;
449 if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
452 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file);
456 /* strip trailing whitespace of value, if not asked to match for it */
457 if (! isspace(pair->value[strlen(pair->value)-1])) {
458 i = len = strlen(tmpattr->value);
459 while (i > 0 && isspace(tmpattr->value[i-1]))
462 tmpattr->value[i] = '\0';
463 dbg("remove %i trailing whitespace chars from '%s'",
464 len - i, tmpattr->value);
468 dbg("compare attribute '%s' value '%s' with '%s'",
469 pair->file, tmpattr->value, pair->value);
470 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
473 dbg("found matching attribute '%s' with value '%s'",
474 pair->file, pair->value);
478 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
480 struct sysfs_pair *pair;
483 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
484 pair = &dev->sysfs_pair[i];
485 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
487 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
488 dbg("sysfs attribute doesn't match");
496 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
498 char path[SYSFS_PATH_MAX];
501 /* we have to have a sysfs device for ID to work */
505 strfieldcpy(path, sysfs_device->path);
506 temp = strrchr(path, '/');
508 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
509 if (strcmp_pattern(dev->id, temp) != 0)
515 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
517 char path[SYSFS_PATH_MAX];
521 /* we have to have a sysfs device for PLACE to work */
526 strfieldcpy(path, sysfs_device->path);
527 temp = strrchr(path, '/');
528 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
529 if (strstr(temp, dev->place) != NULL) {
533 temp = strrchr(path, '/');
534 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
535 if (strstr(temp, dev->place) != NULL)
539 dbg("place doesn't match");
546 static int match_rule(struct udevice *udev, struct config_device *dev,
547 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
550 /* check for matching bus value */
551 if (dev->bus[0] != '\0') {
552 if (sysfs_device == NULL) {
553 dbg("device has no bus");
556 dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'",
557 dev->bus, sysfs_device->bus);
558 if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
559 dbg(FIELD_BUS " is not matching");
562 dbg(FIELD_BUS " matches");
566 /* check for matching kernel name */
567 if (dev->kernel[0] != '\0') {
568 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'",
569 dev->kernel, class_dev->name);
570 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
571 dbg(FIELD_KERNEL " is not matching");
574 dbg(FIELD_KERNEL " matches");
578 /* check for matching subsystem */
579 if (dev->subsystem[0] != '\0') {
580 dbg("check for " FIELD_SUBSYSTEM " dev->subsystem='%s' class_dev->name='%s'",
581 dev->subsystem, class_dev->name);
582 if (strcmp_pattern(dev->subsystem, udev->subsystem) != 0) {
583 dbg(FIELD_SUBSYSTEM " is not matching");
586 dbg(FIELD_SUBSYSTEM " matches");
590 /* check for matching driver */
591 if (dev->driver[0] != '\0') {
592 dbg("check for " FIELD_DRIVER " dev->driver='%s' sysfs_device->driver_name='%s'",
593 dev->driver, sysfs_device->driver_name);
594 if (strcmp_pattern(dev->driver, sysfs_device->driver_name) != 0) {
595 dbg(FIELD_DRIVER " is not matching");
598 dbg(FIELD_DRIVER " matches");
602 /* check for matching bus id */
603 if (dev->id[0] != '\0') {
604 dbg("check " FIELD_ID);
605 if (match_id(dev, class_dev, sysfs_device) != 0) {
606 dbg(FIELD_ID " is not matching");
609 dbg(FIELD_ID " matches");
613 /* check for matching place of device */
614 if (dev->place[0] != '\0') {
615 dbg("check " FIELD_PLACE);
616 if (match_place(dev, class_dev, sysfs_device) != 0) {
617 dbg(FIELD_PLACE " is not matching");
620 dbg(FIELD_PLACE " matches");
624 /* check for matching sysfs pairs */
625 if (dev->sysfs_pair[0].file[0] != '\0') {
626 dbg("check " FIELD_SYSFS " pairs");
627 if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
628 dbg(FIELD_SYSFS " is not matching");
631 dbg(FIELD_SYSFS " matches");
635 /* execute external program */
636 if (dev->program[0] != '\0') {
637 char program[PROGRAM_SIZE];
639 dbg("check " FIELD_PROGRAM);
640 strfieldcpy(program, dev->program);
641 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
642 if (execute_program(udev, program, udev->program_result, NAME_SIZE) != 0) {
643 dbg(FIELD_PROGRAM " returned nonzero");
646 dbg(FIELD_PROGRAM " returned successful");
650 /* check for matching result of external program */
651 if (dev->result[0] != '\0') {
652 dbg("check for " FIELD_RESULT " dev->result='%s', udev->program_result='%s'",
653 dev->result, udev->program_result);
654 if (strcmp_pattern(dev->result, udev->program_result) != 0) {
655 dbg(FIELD_RESULT " is not matching");
658 dbg(FIELD_RESULT " matches");
662 /* Yeah, we matched! */
666 dbg("try parent sysfs device");
667 sysfs_device = sysfs_get_device_parent(sysfs_device);
668 if (sysfs_device == NULL)
670 dbg("sysfs_device->path='%s'", sysfs_device->path);
671 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
676 int namedev_name_device(struct udevice *udev, struct sysfs_class_device *class_dev)
678 struct sysfs_class_device *class_dev_parent;
679 struct sysfs_device *sysfs_device = NULL;
680 struct config_device *dev;
683 dbg("class_dev->name='%s'", class_dev->name);
685 /* Figure out where the "device"-symlink is at. For char devices this will
686 * always be in the class_dev->path. On block devices, only the main block
687 * device will have the device symlink in it's path. All partition devices
688 * need to look at the symlink in its parent directory.
690 class_dev_parent = sysfs_get_classdev_parent(class_dev);
691 if (class_dev_parent != NULL) {
692 dbg("given class device has a parent, use this instead");
693 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
695 sysfs_device = sysfs_get_classdev_device(class_dev);
699 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
700 sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
701 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
704 strfieldcpy(udev->kernel_name, class_dev->name);
705 fix_kernel_name(udev);
706 dbg("udev->kernel_name = '%s'", udev->kernel_name);
708 /* get kernel number */
709 pos = class_dev->name + strlen(class_dev->name);
710 while (isdigit(*(pos-1)))
712 strfieldcpy(udev->kernel_number, pos);
713 dbg("kernel_number='%s'", udev->kernel_number);
715 /* look for a matching rule to apply */
716 list_for_each_entry(dev, &config_device_list, node) {
718 if (match_rule(udev, dev, class_dev, sysfs_device) == 0) {
719 if (dev->name[0] == '\0' && dev->symlink[0] == '\0') {
720 /* empty name, symlink, perms will not create any node */
721 if (dev->mode == 0000 && dev->owner[0] == '\0' && dev->group[0] == '\0') {
722 info("configured rule in '%s[%i]' applied, '%s' is ignored",
723 dev->config_file, dev->config_line, udev->kernel_name);
727 /* apply permissions only rule values */
728 if (dev->mode != 0000) {
729 udev->mode = dev->mode;
730 dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name);
732 if (dev->owner[0] != '\0') {
733 strfieldcpy(udev->owner, dev->owner);
734 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
735 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
737 if (dev->group[0] != '\0') {
738 strfieldcpy(udev->group, dev->group);
739 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
740 dbg("applied group='%s' to '%s'", udev->group, 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[%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[%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 if (dev->mode != 0000)
778 udev->mode = dev->mode;
779 if (dev->owner[0] != '\0') {
780 strfieldcpy(udev->owner, dev->owner);
781 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
783 if (dev->group[0] != '\0') {
784 strfieldcpy(udev->group, dev->group);
785 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
788 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
789 udev->name, udev->owner, udev->group, udev->mode);
796 /* no rule matched, so we use the kernel name */
797 strfieldcpy(udev->name, udev->kernel_name);
798 dbg("no rule found, use kernel name '%s'", udev->name);