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"
39 #include "udev_utils.h"
40 #include "udev_version.h"
45 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr);
47 LIST_HEAD(config_device_list);
48 LIST_HEAD(perm_device_list);
51 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
52 static int strcmp_pattern(const char *p, const char *s)
57 return (p[0] != '\0');
68 while ((p[0] != '\0') && (p[0] != ']')) {
71 if ((s[0] >= p[0]) && (s[0] <= p[2]))
75 match = (p[0] == s[0]);
79 while ((p[0] != '\0') && (p[0] != ']'))
82 return strcmp_pattern(p+1, s+1);
88 if (strcmp_pattern(p, s+1))
89 return strcmp_pattern(p+1, s);
97 if ((p[0] == s[0]) || (p[0] == '?'))
98 return strcmp_pattern(p+1, s+1);
104 static struct perm_device *find_perm(char *name)
106 struct perm_device *perm;
108 list_for_each_entry(perm, &perm_device_list, node) {
109 if (strcmp_pattern(perm->name, name))
116 static mode_t get_default_mode(void)
118 mode_t mode = 0600; /* default to owner rw only */
120 if (strlen(default_mode_str) != 0)
121 mode = strtol(default_mode_str, NULL, 8);
126 static char *get_default_owner(void)
128 if (strlen(default_owner_str) == 0)
129 strfieldcpy(default_owner_str, "root");
131 return default_owner_str;
134 static char *get_default_group(void)
136 if (strlen(default_group_str) == 0)
137 strfieldcpy(default_group_str, "root");
139 return default_group_str;
142 /* extract possible {attr} and move str behind it */
143 static char *get_format_attribute(char **str)
148 if (*str[0] == '{') {
149 pos = strchr(*str, '}');
151 dbg("missing closing brace for format");
157 dbg("attribute='%s', str='%s'", attr, *str);
162 /* extract possible format length and move str behind it*/
163 static int get_format_len(char **str)
168 if (isdigit(*str[0])) {
169 num = (int) strtoul(*str, &tail, 10);
172 dbg("format length=%i", num);
175 dbg("format parsing error '%s'", *str);
181 /** Finds the lowest positive N such that <name>N isn't present in
182 * $(udevroot) either as a file or a symlink.
184 * @param name Name to check for
185 * @return 0 if <name> didn't exist and N otherwise.
187 static int find_free_number(struct udevice *udev, const char *name)
189 char filename[NAME_SIZE];
191 struct udevice db_udev;
193 strfieldcpy(filename, name);
195 dbg("look for existing node '%s'", filename);
196 memset(&db_udev, 0x00, sizeof(struct udevice));
197 if (udev_db_get_device_byname(&db_udev, filename) != 0) {
198 dbg("free num=%d", num);
204 info("find_free_number gone crazy (num=%d), aborted", num);
207 snprintf(filename, NAME_SIZE, "%s%d", name, num);
208 filename[NAME_SIZE-1] = '\0';
212 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
213 struct sysfs_class_device *class_dev,
214 struct sysfs_device *sysfs_device)
216 char temp[NAME_SIZE];
217 char temp2[NAME_SIZE];
227 struct sysfs_attribute *tmpattr;
228 unsigned int next_free_number;
232 pos = strchr(pos, '%');
238 len = get_format_len(&tail);
240 strfieldcpy(temp, tail+1);
242 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
243 attr = get_format_attribute(&tail);
248 if (strlen(udev->bus_id) == 0)
250 strfieldcatmax(string, udev->bus_id, maxsize);
251 dbg("substitute bus_id '%s'", udev->bus_id);
254 if (strlen(udev->kernel_name) == 0)
256 strfieldcatmax(string, udev->kernel_name, maxsize);
257 dbg("substitute kernel name '%s'", udev->kernel_name);
260 if (strlen(udev->kernel_number) == 0)
262 strfieldcatmax(string, udev->kernel_number, maxsize);
263 dbg("substitute kernel number '%s'", udev->kernel_number);
266 strintcatmax(string, udev->minor, maxsize);
267 dbg("substitute minor number '%u'", udev->minor);
270 strintcatmax(string, udev->major, maxsize);
271 dbg("substitute major number '%u'", udev->major);
274 if (strlen(udev->program_result) == 0)
276 /* get part part of the result string */
279 i = strtoul(attr, &rest, 10);
281 foreach_strpart(udev->program_result, " \n\r", spos, slen) {
287 dbg("requested part of result string not found");
291 strfieldcpy(temp2, spos);
293 strfieldcpymax(temp2, spos, slen+1);
294 strfieldcatmax(string, temp2, maxsize);
295 dbg("substitute part of result string '%s'", temp2);
297 strfieldcatmax(string, udev->program_result, maxsize);
298 dbg("substitute result string '%s'", udev->program_result);
303 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr);
304 if (tmpattr == NULL) {
305 dbg("sysfa attribute '%s' not found", attr);
308 /* strip trailing whitespace of matching value */
309 if (isspace(tmpattr->value[strlen(tmpattr->value)-1])) {
310 i = len = strlen(tmpattr->value);
311 while (i > 0 && isspace(tmpattr->value[i-1]))
314 tmpattr->value[i] = '\0';
315 dbg("remove %i trailing whitespace chars from '%s'",
316 len - i, tmpattr->value);
319 strfieldcatmax(string, tmpattr->value, maxsize);
320 dbg("substitute sysfs value '%s'", tmpattr->value);
322 dbg("missing attribute");
326 strfieldcatmax(string, "%", maxsize);
330 next_free_number = find_free_number(udev, string);
331 if (next_free_number > 0) {
332 sprintf(temp2, "%d", next_free_number);
333 strfieldcatmax(string, temp2, maxsize);
337 dbg("unknown substitution type '%%%c'", c);
340 /* truncate to specified length */
344 strfieldcatmax(string, tail, maxsize);
348 static void fix_kernel_name(struct udevice *udev)
350 char *temp = udev->kernel_name;
352 while (*temp != 0x00) {
353 /* Some block devices have a ! in their name,
354 * we need to change that to / */
361 static int execute_program(struct udevice *udev, const char *path, char *value, int len)
369 char arg[PROGRAM_SIZE];
370 char *argv[(PROGRAM_SIZE / 2) + 1];
373 strfieldcpy(arg, path);
375 if (strchr(path, ' ')) {
377 while (pos != NULL) {
378 if (pos[0] == '\'') {
379 /* don't separate if in apostrophes */
381 argv[i] = strsep(&pos, "\'");
382 while (pos && pos[0] == ' ')
385 argv[i] = strsep(&pos, " ");
387 dbg("arg[%i] '%s'", i, argv[i]);
391 dbg("execute '%s' with parsed arguments", arg);
394 argv[1] = udev->subsystem;
396 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
409 /* dup2 write side of pipe to STDOUT */
410 dup2(fds[1], STDOUT_FILENO);
411 retval = execv(arg, argv);
413 info(FIELD_PROGRAM " execution of '%s' failed", path);
419 /* parent reads from fds[0] */
424 count = read(fds[0], value + i, len - i-1);
430 dbg("result len %d too short", len);
437 dbg("read failed with '%s'", strerror(errno));
441 if (i > 0 && value[i-1] == '\n')
444 dbg("result is '%s'", value);
447 waitpid(pid, &status, 0);
449 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
450 dbg("exec program status 0x%x", status);
457 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
459 struct sysfs_attribute *tmpattr = NULL;
462 dbg("look for device attribute '%s'", attr);
463 /* try to find the attribute in the class device directory */
464 tmpattr = sysfs_get_classdev_attr(class_dev, attr);
468 /* look in the class device directory if present */
470 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
478 c = strchr(tmpattr->value, '\n');
482 dbg("found attribute '%s'", tmpattr->path);
486 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
488 struct sysfs_attribute *tmpattr;
492 if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
495 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file);
499 /* strip trailing whitespace of value, if not asked to match for it */
500 if (! isspace(pair->value[strlen(pair->value)-1])) {
501 i = len = strlen(tmpattr->value);
502 while (i > 0 && isspace(tmpattr->value[i-1]))
505 tmpattr->value[i] = '\0';
506 dbg("remove %i trailing whitespace chars from '%s'",
507 len - i, tmpattr->value);
511 dbg("compare attribute '%s' value '%s' with '%s'",
512 pair->file, tmpattr->value, pair->value);
513 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
516 dbg("found matching attribute '%s' with value '%s'",
517 pair->file, pair->value);
521 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
523 struct sysfs_pair *pair;
526 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
527 pair = &dev->sysfs_pair[i];
528 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
530 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
531 dbg("sysfs attribute doesn't match");
539 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
541 char path[SYSFS_PATH_MAX];
544 /* we have to have a sysfs device for ID to work */
548 strfieldcpy(path, sysfs_device->path);
549 temp = strrchr(path, '/');
551 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
552 if (strcmp_pattern(dev->id, temp) != 0)
558 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
560 char path[SYSFS_PATH_MAX];
564 /* we have to have a sysfs device for PLACE to work */
569 strfieldcpy(path, sysfs_device->path);
570 temp = strrchr(path, '/');
571 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
572 if (strstr(temp, dev->place) != NULL) {
576 temp = strrchr(path, '/');
577 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
578 if (strstr(temp, dev->place) != NULL)
582 dbg("place doesn't match");
589 static int match_rule(struct config_device *dev, struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
592 /* check for matching bus value */
593 if (dev->bus[0] != '\0') {
594 if (sysfs_device == NULL) {
595 dbg("device has no bus");
598 dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
599 if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
600 dbg(FIELD_BUS " is not matching");
603 dbg(FIELD_BUS " matches");
607 /* check for matching kernel name */
608 if (dev->kernel[0] != '\0') {
609 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'", dev->kernel, class_dev->name);
610 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
611 dbg(FIELD_KERNEL " is not matching");
614 dbg(FIELD_KERNEL " matches");
618 /* check for matching subsystem */
619 if (dev->subsystem[0] != '\0') {
620 dbg("check for " FIELD_SUBSYSTEM " dev->subsystem='%s' class_dev->name='%s'", dev->subsystem, class_dev->name);
621 if (strcmp_pattern(dev->subsystem, udev->subsystem) != 0) {
622 dbg(FIELD_SUBSYSTEM " is not matching");
625 dbg(FIELD_SUBSYSTEM " matches");
629 /* check for matching driver */
630 if (dev->driver[0] != '\0') {
631 dbg("check for " FIELD_DRIVER " dev->driver='%s' sysfs_device->driver_name='%s'", dev->driver, sysfs_device->driver_name);
632 if (strcmp_pattern(dev->driver, sysfs_device->driver_name) != 0) {
633 dbg(FIELD_DRIVER " is not matching");
636 dbg(FIELD_DRIVER " matches");
640 /* check for matching bus id */
641 if (dev->id[0] != '\0') {
642 dbg("check " FIELD_ID);
643 if (match_id(dev, class_dev, sysfs_device) != 0) {
644 dbg(FIELD_ID " is not matching");
647 dbg(FIELD_ID " matches");
651 /* check for matching place of device */
652 if (dev->place[0] != '\0') {
653 dbg("check " FIELD_PLACE);
654 if (match_place(dev, class_dev, sysfs_device) != 0) {
655 dbg(FIELD_PLACE " is not matching");
658 dbg(FIELD_PLACE " matches");
662 /* check for matching sysfs pairs */
663 if (dev->sysfs_pair[0].file[0] != '\0') {
664 dbg("check " FIELD_SYSFS " pairs");
665 if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
666 dbg(FIELD_SYSFS " is not matching");
669 dbg(FIELD_SYSFS " matches");
673 /* execute external program */
674 if (dev->program[0] != '\0') {
675 char program[PROGRAM_SIZE];
677 dbg("check " FIELD_PROGRAM);
678 strfieldcpy(program, dev->program);
679 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
680 if (execute_program(udev, program, udev->program_result, NAME_SIZE) != 0) {
681 dbg(FIELD_PROGRAM " returned nonzero");
684 dbg(FIELD_PROGRAM " returned successful");
688 /* check for matching result of external program */
689 if (dev->result[0] != '\0') {
690 dbg("check for " FIELD_RESULT
691 " dev->result='%s', udev->program_result='%s'",
692 dev->result, udev->program_result);
693 if (strcmp_pattern(dev->result, udev->program_result) != 0) {
694 dbg(FIELD_RESULT " is not matching");
697 dbg(FIELD_RESULT " matches");
701 /* Yeah, we matched! */
705 dbg("try parent sysfs device");
706 sysfs_device = sysfs_get_device_parent(sysfs_device);
707 if (sysfs_device == NULL)
709 dbg("sysfs_device->path='%s'", sysfs_device->path);
710 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
711 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
716 int namedev_name_device(struct udevice *udev, struct sysfs_class_device *class_dev)
718 struct sysfs_class_device *class_dev_parent;
719 struct sysfs_device *sysfs_device = NULL;
720 struct config_device *dev;
721 struct perm_device *perm;
726 dbg("class_dev->name='%s'", class_dev->name);
728 /* Figure out where the "device"-symlink is at. For char devices this will
729 * always be in the class_dev->path. On block devices, only the main block
730 * device will have the device symlink in it's path. All partition devices
731 * need to look at the symlink in its parent directory.
733 class_dev_parent = sysfs_get_classdev_parent(class_dev);
734 if (class_dev_parent != NULL) {
735 dbg("given class device has a parent, use this instead");
736 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
738 sysfs_device = sysfs_get_classdev_device(class_dev);
742 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
743 sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
744 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
745 strfieldcpy(udev->bus, sysfs_device->bus);
748 strfieldcpy(udev->kernel_name, class_dev->name);
749 fix_kernel_name(udev);
750 dbg("udev->kernel_name = '%s'", udev->kernel_name);
752 /* get kernel number */
753 pos = class_dev->name + strlen(class_dev->name);
754 while (isdigit(*(pos-1)))
756 strfieldcpy(udev->kernel_number, pos);
757 dbg("kernel_number='%s'", udev->kernel_number);
759 /* look for a matching rule to apply */
760 list_for_each_entry(dev, &config_device_list, node) {
762 if (match_rule(dev, class_dev, udev, sysfs_device) == 0) {
763 if (dev->name[0] == '\0' && dev->symlink[0] == '\0') {
764 info("configured rule in '%s' at line %i applied, '%s' is ignored",
765 dev->config_file, dev->config_line, udev->kernel_name);
769 if (dev->symlink[0] != '\0') {
770 char temp[NAME_SIZE];
772 info("configured rule in '%s' at line %i applied, added symlink '%s'",
773 dev->config_file, dev->config_line, dev->symlink);
774 strfieldcpy(temp, dev->symlink);
775 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
776 if (udev->symlink[0] != '\0')
777 strfieldcat(udev->symlink, " ");
778 strfieldcat(udev->symlink, temp);
781 if (dev->name[0] != '\0') {
782 /* apply all_partitions flag only at a main block device */
783 if (dev->partitions > 0 &&
784 (udev->type != 'b' || udev->kernel_number[0] != '\0'))
787 info("configured rule in '%s' at line %i applied, '%s' becomes '%s'",
788 dev->config_file, dev->config_line, udev->kernel_name, dev->name);
789 strfieldcpy(udev->name, dev->name);
794 /* no rule was found so we use the kernel name */
795 strfieldcpy(udev->name, udev->kernel_name);
796 if (udev->type == 'n')
802 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
803 strfieldcpy(udev->config_file, dev->config_file);
804 udev->config_line = dev->config_line;
806 if (udev->type == 'n')
809 udev->partitions = dev->partitions;
810 udev->ignore_remove = dev->ignore_remove;
812 /* get permissions given in rule */
813 set_empty_perms(udev, dev->mode,
818 /* get permissions given in config file or set defaults */
819 perm = find_perm(udev->name);
821 set_empty_perms(udev, perm->mode,
825 set_empty_perms(udev, get_default_mode(),
827 get_default_group());
829 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
830 udev->name, udev->owner, udev->group, udev->mode);
833 /* store time of action */
835 udev->config_uptime = info.uptime;
840 int namedev_init(void)
844 retval = namedev_init_rules();
848 retval = namedev_init_permissions();
852 dump_config_dev_list();
853 dump_perm_dev_list();