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.
24 /* define this to enable parsing debugging */
25 /* #define DEBUG_PARSER */
39 #include "udev_version.h"
41 #include "libsysfs/libsysfs.h"
42 #include "klibc_fixups.h"
44 #define TYPE_LABEL "LABEL"
45 #define TYPE_NUMBER "NUMBER"
46 #define TYPE_TOPOLOGY "TOPOLOGY"
47 #define TYPE_REPLACE "REPLACE"
48 #define TYPE_CALLOUT "CALLOUT"
49 #define CALLOUT_MAXARG 8
51 static LIST_HEAD(config_device_list);
53 static void dump_dev(struct config_device *dev)
57 dbg_parse("KERNEL name='%s' ,"
58 "owner='%s', group='%s', mode=%#o",
59 dev->name, dev->owner, dev->group, dev->mode);
62 dbg_parse("LABEL name='%s', bus='%s', sysfs_file='%s', sysfs_value='%s', "
63 "owner='%s', group='%s', mode=%#o",
64 dev->name, dev->bus, dev->sysfs_file, dev->sysfs_value,
65 dev->owner, dev->group, dev->mode);
68 dbg_parse("NUMBER name='%s', bus='%s', id='%s', "
69 "owner='%s', group='%s', mode=%#o",
70 dev->name, dev->bus, dev->id,
71 dev->owner, dev->group, dev->mode);
74 dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s', "
75 "owner='%s', group='%s', mode=%#o",
76 dev->name, dev->bus, dev->place,
77 dev->owner, dev->group, dev->mode);
80 dbg_parse("REPLACE name=%s, kernel_name=%s, "
81 "owner='%s', group='%s', mode=%#o",
82 dev->name, dev->kernel_name,
83 dev->owner, dev->group, dev->mode);
86 dbg_parse("CALLOUT name='%s', program='%s', bus='%s', id='%s', "
87 "owner='%s', group='%s', mode=%#o",
88 dev->name, dev->exec_program, dev->bus, dev->id,
89 dev->owner, dev->group, dev->mode);
92 dbg_parse("unknown type of method");
96 #define copy_var(a, b, var) \
100 #define copy_string(a, b, var) \
101 if (strlen(b->var)) \
102 strcpy(a->var, b->var);
104 static int add_dev(struct config_device *new_dev)
106 struct list_head *tmp;
107 struct config_device *tmp_dev;
109 /* update the values if we already have the device */
110 list_for_each(tmp, &config_device_list) {
111 struct config_device *dev = list_entry(tmp, struct config_device, node);
112 int len = strlen(new_dev->name);
113 if (new_dev->name[len-1] == '*') {
115 if (strncmp(dev->name, new_dev->name, len))
118 if (strcmp(dev->name, new_dev->name))
121 /* the same, copy the new info into this structure */
122 copy_var(dev, new_dev, type);
123 copy_var(dev, new_dev, mode);
124 copy_string(dev, new_dev, bus);
125 copy_string(dev, new_dev, sysfs_file);
126 copy_string(dev, new_dev, sysfs_value);
127 copy_string(dev, new_dev, id);
128 copy_string(dev, new_dev, place);
129 copy_string(dev, new_dev, kernel_name);
130 copy_string(dev, new_dev, owner);
131 copy_string(dev, new_dev, group);
135 /* not found, add new structure to the device list */
136 tmp_dev = malloc(sizeof(*tmp_dev));
139 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
140 list_add(&tmp_dev->node, &config_device_list);
145 static void dump_dev_list(void)
147 struct list_head *tmp;
149 list_for_each(tmp, &config_device_list) {
150 struct config_device *dev = list_entry(tmp, struct config_device, node);
155 static int get_pair(char **orig_string, char **left, char **right)
158 char *string = *orig_string;
163 /* eat any whitespace */
164 while (isspace(*string))
167 /* split based on '=' */
168 temp = strsep(&string, "=");
173 /* take the right side and strip off the '"' */
174 while (isspace(*string))
181 temp = strsep(&string, "\"");
182 if (!string || *temp == '\0')
185 *orig_string = string;
190 static int get_value(const char *left, char **orig_string, char **ret_string)
195 retval = get_pair(orig_string, &left_string, ret_string);
198 if (strcasecmp(left_string, left) != 0)
203 static int namedev_init_config(void)
212 struct config_device dev;
214 dbg("opening %s to read as config", udev_config_filename);
215 fd = fopen(udev_config_filename, "r");
217 dbg("can't open %s", udev_config_filename);
221 /* loop through the whole file */
225 temp = fgets(line, sizeof(line), fd);
230 dbg_parse("read %s", temp);
232 /* eat the whitespace at the beginning of the line */
233 while (isspace(*temp))
240 /* see if this is a comment */
241 if (*temp == COMMENT_CHARACTER)
244 memset(&dev, 0x00, sizeof(struct config_device));
247 temp2 = strsep(&temp, ",");
248 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
253 retval = get_value("BUS", &temp, &temp3);
256 strfieldcpy(dev.bus, temp3);
259 temp2 = strsep(&temp, ",");
260 retval = get_pair(&temp, &temp2, &temp3);
263 strfieldcpy(dev.sysfs_file, temp2);
264 strfieldcpy(dev.sysfs_value, temp3);
266 /* NAME="new_name" */
267 temp2 = strsep(&temp, ",");
268 retval = get_value("NAME", &temp, &temp3);
271 strfieldcpy(dev.name, temp3);
273 dbg_parse("LABEL name='%s', bus='%s', "
274 "sysfs_file='%s', sysfs_value='%s'",
275 dev.name, dev.bus, dev.sysfs_file,
279 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
284 retval = get_value("BUS", &temp, &temp3);
287 strfieldcpy(dev.bus, temp3);
290 temp2 = strsep(&temp, ",");
291 retval = get_value("ID", &temp, &temp3);
294 strfieldcpy(dev.id, temp3);
296 /* NAME="new_name" */
297 temp2 = strsep(&temp, ",");
298 retval = get_value("NAME", &temp, &temp3);
301 strfieldcpy(dev.name, temp3);
303 dbg_parse("NUMBER name='%s', bus='%s', id='%s'",
304 dev.name, dev.bus, dev.id);
307 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
312 retval = get_value("BUS", &temp, &temp3);
315 strfieldcpy(dev.bus, temp3);
318 temp2 = strsep(&temp, ",");
319 retval = get_value("PLACE", &temp, &temp3);
322 strfieldcpy(dev.place, temp3);
324 /* NAME="new_name" */
325 temp2 = strsep(&temp, ",");
326 retval = get_value("NAME", &temp, &temp3);
329 strfieldcpy(dev.name, temp3);
331 dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s'",
332 dev.name, dev.bus, dev.place);
335 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
339 /* KERNEL="kernel_name" */
340 retval = get_value("KERNEL", &temp, &temp3);
343 strfieldcpy(dev.kernel_name, temp3);
345 /* NAME="new_name" */
346 temp2 = strsep(&temp, ",");
347 retval = get_value("NAME", &temp, &temp3);
350 strfieldcpy(dev.name, temp3);
351 dbg_parse("REPLACE name='%s', kernel_name='%s'",
352 dev.name, dev.kernel_name);
354 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
359 retval = get_value("BUS", &temp, &temp3);
362 strfieldcpy(dev.bus, temp3);
364 /* PROGRAM="executable" */
365 temp2 = strsep(&temp, ",");
366 retval = get_value("PROGRAM", &temp, &temp3);
369 strfieldcpy(dev.exec_program, temp3);
372 temp2 = strsep(&temp, ",");
373 retval = get_value("ID", &temp, &temp3);
376 strfieldcpy(dev.id, temp3);
378 /* NAME="new_name" */
379 temp2 = strsep(&temp, ",");
380 retval = get_value("NAME", &temp, &temp3);
383 strfieldcpy(dev.name, temp3);
384 dbg_parse("CALLOUT name='%s', program='%s'",
385 dev.name, dev.exec_program);
388 retval = add_dev(&dev);
390 dbg("add_dev returned with error %d", retval);
394 dbg_parse("%s:%d:%Zd: error parsing '%s'", udev_config_filename,
395 lineno, temp - line, temp);
402 static int namedev_init_permissions(void)
409 struct config_device dev;
411 dbg("opening %s to read as permissions config", udev_config_permission_filename);
412 fd = fopen(udev_config_permission_filename, "r");
414 dbg("can't open %s", udev_config_permission_filename);
418 /* loop through the whole file */
421 temp = fgets(line, sizeof(line), fd);
425 dbg_parse("read %s", temp);
427 /* eat the whitespace at the beginning of the line */
428 while (isspace(*temp))
435 /* see if this is a comment */
436 if (*temp == COMMENT_CHARACTER)
439 memset(&dev, 0x00, sizeof(dev));
442 temp2 = strsep(&temp, ":");
444 dbg("cannot parse line: %s", line);
447 strncpy(dev.name, temp2, sizeof(dev.name));
449 temp2 = strsep(&temp, ":");
451 dbg("cannot parse line: %s", line);
454 strncpy(dev.owner, temp2, sizeof(dev.owner));
456 temp2 = strsep(&temp, ":");
458 dbg("cannot parse line: %s", line);
461 strncpy(dev.group, temp2, sizeof(dev.owner));
463 dev.mode = strtol(temp, NULL, 8);
465 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
466 dev.name, dev.owner, dev.group,
468 retval = add_dev(&dev);
470 dbg("add_dev returned with error %d", retval);
480 static mode_t get_default_mode(struct sysfs_class_device *class_dev)
482 /* just default everyone to rw for the world! */
487 static int exec_callout(struct config_device *dev, char *value, int len)
497 char *args[CALLOUT_MAXARG];
500 dbg("callout to '%s'", dev->exec_program);
514 close(STDOUT_FILENO);
515 dup(fds[1]); /* dup write side of pipe to STDOUT */
516 if (strchr(dev->exec_program, ' ')) {
517 /* callout with arguments */
518 arg = dev->exec_program;
519 for (i=0; i < CALLOUT_MAXARG-1; i++) {
520 args[i] = strsep(&arg, " ");
525 dbg("to many args - %d", i);
528 retval = execve(args[0], args, main_envp);
530 retval = execve(dev->exec_program, main_argv, main_envp);
533 dbg("child execve failed");
536 return -1; /* avoid compiler warning */
538 /* parent reads from fds[0] */
542 res = read(fds[0], buffer, sizeof(buffer) - 1);
547 dbg("callout len %d too short\n", len);
551 dbg("callout value already set");
555 strncpy(value, buffer, len);
558 dbg("callout returned '%s'", value);
562 dbg("wait failed result %d", res);
567 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
568 dbg("callout program status 0x%x", status);
576 static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev)
578 struct config_device *dev;
579 struct list_head *tmp;
582 list_for_each(tmp, &config_device_list) {
583 dev = list_entry(tmp, struct config_device, node);
584 if (dev->type != CALLOUT)
587 if (exec_callout(dev, value, sizeof(value)))
589 if (strncmp(value, dev->id, sizeof(value)) != 0)
591 strfieldcpy(udev->name, dev->name);
592 if (dev->mode != 0) {
593 udev->mode = dev->mode;
594 strfieldcpy(udev->owner, dev->owner);
595 strfieldcpy(udev->group, dev->group);
597 dbg_parse("callout returned matching value '%s', '%s' becomes '%s'"
598 " - owner='%s', group='%s', mode =%#o",
599 dev->id, class_dev->name, udev->name,
600 dev->owner, dev->group, dev->mode);
606 static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
608 struct sysfs_attribute *tmpattr = NULL;
609 struct config_device *dev;
610 struct list_head *tmp;
612 list_for_each(tmp, &config_device_list) {
613 dev = list_entry(tmp, struct config_device, node);
614 if (dev->type != LABEL)
617 dbg_parse("look for device attribute '%s'", dev->sysfs_file);
618 /* try to find the attribute in the class device directory */
619 tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file);
623 /* look in the class device directory if present */
625 tmpattr = sysfs_get_device_attr(sysfs_device, dev->sysfs_file);
633 tmpattr->value[strlen(tmpattr->value)-1] = 0x00;
634 dbg_parse("compare attribute '%s' value '%s' with '%s'",
635 dev->sysfs_file, tmpattr->value, dev->sysfs_value);
636 if (strcmp(dev->sysfs_value, tmpattr->value) != 0)
639 strfieldcpy(udev->name, dev->name);
640 if (dev->mode != 0) {
641 udev->mode = dev->mode;
642 strfieldcpy(udev->owner, dev->owner);
643 strfieldcpy(udev->group, dev->group);
645 dbg_parse("found matching attribute '%s', '%s' becomes '%s' "
646 "- owner='%s', group='%s', mode=%#o",
647 dev->sysfs_file, class_dev->name, udev->name,
648 dev->owner, dev->group, dev->mode);
655 static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
657 struct config_device *dev;
658 struct list_head *tmp;
659 char path[SYSFS_PATH_MAX];
663 /* we have to have a sysfs device for NUMBER to work */
667 list_for_each(tmp, &config_device_list) {
668 dev = list_entry(tmp, struct config_device, node);
669 if (dev->type != NUMBER)
673 strfieldcpy(path, sysfs_device->path);
674 temp = strrchr(path, '/');
675 dbg_parse("search '%s' in '%s', path='%s'", dev->id, temp, path);
676 if (strstr(temp, dev->id) != NULL) {
680 temp = strrchr(path, '/');
681 dbg_parse("search '%s' in '%s', path='%s'", dev->id, temp, path);
682 if (strstr(temp, dev->id) != NULL)
687 strfieldcpy(udev->name, dev->name);
688 if (dev->mode != 0) {
689 udev->mode = dev->mode;
690 strfieldcpy(udev->owner, dev->owner);
691 strfieldcpy(udev->group, dev->group);
693 dbg_parse("found matching id '%s', '%s' becomes '%s'"
694 " - owner='%s', group ='%s', mode=%#o",
695 dev->id, class_dev->name, udev->name,
696 dev->owner, dev->group, dev->mode);
703 static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
705 struct config_device *dev;
706 struct list_head *tmp;
707 char path[SYSFS_PATH_MAX];
711 /* we have to have a sysfs device for TOPOLOGY to work */
715 list_for_each(tmp, &config_device_list) {
716 dev = list_entry(tmp, struct config_device, node);
717 if (dev->type != TOPOLOGY)
721 strfieldcpy(path, sysfs_device->path);
722 temp = strrchr(path, '/');
723 dbg_parse("search '%s' in '%s', path='%s'", dev->place, temp, path);
724 if (strstr(temp, dev->place) != NULL) {
728 temp = strrchr(path, '/');
729 dbg_parse("search '%s' in '%s', path='%s'", dev->place, temp, path);
730 if (strstr(temp, dev->place) != NULL)
736 strfieldcpy(udev->name, dev->name);
737 if (dev->mode != 0) {
738 udev->mode = dev->mode;
739 strfieldcpy(udev->owner, dev->owner);
740 strfieldcpy(udev->group, dev->group);
742 dbg_parse("found matching place '%s', '%s' becomes '%s'"
743 " - owner='%s', group ='%s', mode=%#o",
744 dev->place, class_dev->name, udev->name,
745 dev->owner, dev->group, dev->mode);
751 static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev)
753 struct config_device *dev;
754 struct list_head *tmp;
756 list_for_each(tmp, &config_device_list) {
757 dev = list_entry(tmp, struct config_device, node);
758 if (dev->type != REPLACE)
761 dbg_parse("compare name '%s' with '%s'",
762 dev->kernel_name, dev->name);
763 if (strcmp(dev->kernel_name, class_dev->name) != 0)
766 strfieldcpy(udev->name, dev->name);
767 if (dev->mode != 0) {
768 udev->mode = dev->mode;
769 strfieldcpy(udev->owner, dev->owner);
770 strfieldcpy(udev->group, dev->group);
772 dbg_parse("found name, '%s' becomes '%s' - owner='%s', group='%s', mode = %#o",
773 dev->kernel_name, udev->name,
774 dev->owner, dev->group, dev->mode);
781 static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev)
783 struct config_device *dev;
784 struct list_head *tmp;
787 strfieldcpy(udev->name, class_dev->name);
788 list_for_each(tmp, &config_device_list) {
789 dev = list_entry(tmp, struct config_device, node);
790 len = strlen(dev->name);
791 if (dev->name[len-1] == '*') {
793 if (strncmp(dev->name, class_dev->name, len))
796 if (strcmp(dev->name, class_dev->name))
799 if (dev->mode != 0) {
800 dbg_parse("found permissions for '%s'", class_dev->name);
801 udev->mode = dev->mode;
802 strfieldcpy(udev->owner, dev->owner);
803 strfieldcpy(udev->group, dev->group);
808 static int get_attr(struct sysfs_class_device *class_dev, struct udevice *udev)
810 struct sysfs_device *sysfs_device = NULL;
811 struct sysfs_class_device *class_dev_parent = NULL;
817 /* find the sysfs_device for this class device */
818 /* Wouldn't it really be nice if libsysfs could do this for us? */
819 if (class_dev->sysdevice) {
820 sysfs_device = class_dev->sysdevice;
822 /* bah, let's go backwards up a level to see if the device is there,
823 * as block partitions don't point to the physical device. Need to fix that
824 * up in the kernel...
826 if (strstr(class_dev->path, "block")) {
827 dbg_parse("looking at block device...");
828 if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
829 char path[SYSFS_PATH_MAX];
831 dbg_parse("really is a partition...");
832 strfieldcpy(path, class_dev->path);
833 temp = strrchr(path, '/');
835 dbg_parse("looking for a class device at '%s'", path);
836 class_dev_parent = sysfs_open_class_device(path);
837 if (class_dev_parent == NULL) {
838 dbg("sysfs_open_class_device at '%s' failed", path);
840 dbg_parse("class_dev_parent->name=%s", class_dev_parent->name);
841 if (class_dev_parent->sysdevice)
842 sysfs_device = class_dev_parent->sysdevice;
849 dbg_parse("sysfs_device->path='%s'", sysfs_device->path);
850 dbg_parse("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
852 dbg_parse("class_dev->name = '%s'", class_dev->name);
855 /* rules are looked at in priority order */
856 retval = do_callout(class_dev, udev);
860 retval = do_label(class_dev, udev, sysfs_device);
864 retval = do_number(class_dev, udev, sysfs_device);
868 retval = do_topology(class_dev, udev, sysfs_device);
872 retval = do_replace(class_dev, udev);
876 do_kernelname(class_dev, udev);
880 /* substitute placeholder in NAME */
882 char *pos = strchr(udev->name, '%');
884 char name[NAME_SIZE];
886 strfieldcpy(name, pos+2);
892 strcat(udev->name, sysfs_device->bus_id);
893 dbg("bus_id inserted: %s",
894 sysfs_device->bus_id);
897 dig = class_dev->name + strlen(class_dev->name);
898 while (isdigit(*(dig-1)))
900 strcat(udev->name, dig);
901 dbg("substitute kernel number '%s'", dig);
904 sprintf(pos, "%u", udev->minor);
905 dbg("substitute minor number '%u'", udev->minor);
908 sprintf(pos, "%u", udev->major);
909 dbg("substitute major number '%u'", udev->major);
912 dbg("unknown substitution type '%%%c'", pos[1]);
915 strcat(udev->name, name);
921 /* mode was never set above */
923 udev->mode = get_default_mode(class_dev);
924 udev->owner[0] = 0x00;
925 udev->group[0] = 0x00;
928 if (class_dev_parent)
929 sysfs_close_class_device(class_dev_parent);
934 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *dev)
938 retval = get_attr(class_dev, dev);
940 dbg("get_attr failed");
945 int namedev_init(void)
949 retval = namedev_init_config();
953 retval = namedev_init_permissions();