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 /* s2 may end with '*' to match everything */
54 static int strncmp_wildcard(char *s1, char *s2, int max)
63 return strncmp(s1, s2, len);
66 static void dump_dev(struct config_device *dev)
70 dbg_parse("KERNEL name='%s' ,"
71 "owner='%s', group='%s', mode=%#o",
72 dev->name, dev->owner, dev->group, dev->mode);
75 dbg_parse("LABEL name='%s', bus='%s', sysfs_file='%s', sysfs_value='%s', "
76 "owner='%s', group='%s', mode=%#o",
77 dev->name, dev->bus, dev->sysfs_file, dev->sysfs_value,
78 dev->owner, dev->group, dev->mode);
81 dbg_parse("NUMBER name='%s', bus='%s', id='%s', "
82 "owner='%s', group='%s', mode=%#o",
83 dev->name, dev->bus, dev->id,
84 dev->owner, dev->group, dev->mode);
87 dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s', "
88 "owner='%s', group='%s', mode=%#o",
89 dev->name, dev->bus, dev->place,
90 dev->owner, dev->group, dev->mode);
93 dbg_parse("REPLACE name=%s, kernel_name=%s, "
94 "owner='%s', group='%s', mode=%#o",
95 dev->name, dev->kernel_name,
96 dev->owner, dev->group, dev->mode);
99 dbg_parse("CALLOUT name='%s', bus='%s', program='%s', id='%s', "
100 "owner='%s', group='%s', mode=%#o",
101 dev->name, dev->bus, dev->exec_program, dev->id,
102 dev->owner, dev->group, dev->mode);
105 dbg_parse("unknown type of method");
109 #define copy_var(a, b, var) \
113 #define copy_string(a, b, var) \
114 if (strlen(b->var)) \
115 strcpy(a->var, b->var);
117 static int add_dev(struct config_device *new_dev)
119 struct list_head *tmp;
120 struct config_device *tmp_dev;
122 /* update the values if we already have the device */
123 list_for_each(tmp, &config_device_list) {
124 struct config_device *dev = list_entry(tmp, struct config_device, node);
125 if (strncmp_wildcard(dev->name, new_dev->name, sizeof(dev->name)))
127 if (strncmp(dev->bus, new_dev->bus, sizeof(dev->name)))
129 copy_var(dev, new_dev, type);
130 copy_var(dev, new_dev, mode);
131 copy_string(dev, new_dev, bus);
132 copy_string(dev, new_dev, sysfs_file);
133 copy_string(dev, new_dev, sysfs_value);
134 copy_string(dev, new_dev, id);
135 copy_string(dev, new_dev, place);
136 copy_string(dev, new_dev, kernel_name);
137 copy_string(dev, new_dev, exec_program);
138 copy_string(dev, new_dev, owner);
139 copy_string(dev, new_dev, group);
143 /* not found, add new structure to the device list */
144 tmp_dev = malloc(sizeof(*tmp_dev));
147 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
148 list_add(&tmp_dev->node, &config_device_list);
153 static void dump_dev_list(void)
155 struct list_head *tmp;
157 list_for_each(tmp, &config_device_list) {
158 struct config_device *dev = list_entry(tmp, struct config_device, node);
163 static int get_pair(char **orig_string, char **left, char **right)
166 char *string = *orig_string;
171 /* eat any whitespace */
172 while (isspace(*string))
175 /* split based on '=' */
176 temp = strsep(&string, "=");
181 /* take the right side and strip off the '"' */
182 while (isspace(*string))
189 temp = strsep(&string, "\"");
190 if (!string || *temp == '\0')
193 *orig_string = string;
198 static int get_value(const char *left, char **orig_string, char **ret_string)
203 retval = get_pair(orig_string, &left_string, ret_string);
206 if (strcasecmp(left_string, left) != 0)
211 static int namedev_init_config(void)
220 struct config_device dev;
222 dbg("opening '%s' to read as config", udev_config_filename);
223 fd = fopen(udev_config_filename, "r");
225 dbg("can't open '%s'", udev_config_filename);
229 /* loop through the whole file */
233 temp = fgets(line, sizeof(line), fd);
238 dbg_parse("read '%s'", temp);
240 /* eat the whitespace at the beginning of the line */
241 while (isspace(*temp))
248 /* see if this is a comment */
249 if (*temp == COMMENT_CHARACTER)
252 memset(&dev, 0x00, sizeof(struct config_device));
255 temp2 = strsep(&temp, ",");
256 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
261 retval = get_value("BUS", &temp, &temp3);
264 strfieldcpy(dev.bus, temp3);
267 temp2 = strsep(&temp, ",");
268 retval = get_pair(&temp, &temp2, &temp3);
271 strfieldcpy(dev.sysfs_file, temp2);
272 strfieldcpy(dev.sysfs_value, temp3);
274 /* NAME="new_name" */
275 temp2 = strsep(&temp, ",");
276 retval = get_value("NAME", &temp, &temp3);
279 strfieldcpy(dev.name, temp3);
281 dbg_parse("LABEL name='%s', bus='%s', "
282 "sysfs_file='%s', sysfs_value='%s'",
283 dev.name, dev.bus, dev.sysfs_file,
287 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
292 retval = get_value("BUS", &temp, &temp3);
295 strfieldcpy(dev.bus, temp3);
298 temp2 = strsep(&temp, ",");
299 retval = get_value("ID", &temp, &temp3);
302 strfieldcpy(dev.id, temp3);
304 /* NAME="new_name" */
305 temp2 = strsep(&temp, ",");
306 retval = get_value("NAME", &temp, &temp3);
309 strfieldcpy(dev.name, temp3);
311 dbg_parse("NUMBER name='%s', bus='%s', id='%s'",
312 dev.name, dev.bus, dev.id);
315 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
320 retval = get_value("BUS", &temp, &temp3);
323 strfieldcpy(dev.bus, temp3);
326 temp2 = strsep(&temp, ",");
327 retval = get_value("PLACE", &temp, &temp3);
330 strfieldcpy(dev.place, temp3);
332 /* NAME="new_name" */
333 temp2 = strsep(&temp, ",");
334 retval = get_value("NAME", &temp, &temp3);
337 strfieldcpy(dev.name, temp3);
339 dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s'",
340 dev.name, dev.bus, dev.place);
343 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
347 /* KERNEL="kernel_name" */
348 retval = get_value("KERNEL", &temp, &temp3);
351 strfieldcpy(dev.kernel_name, temp3);
353 /* NAME="new_name" */
354 temp2 = strsep(&temp, ",");
355 retval = get_value("NAME", &temp, &temp3);
358 strfieldcpy(dev.name, temp3);
359 dbg_parse("REPLACE name='%s', kernel_name='%s'",
360 dev.name, dev.kernel_name);
362 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
367 retval = get_value("BUS", &temp, &temp3);
370 strfieldcpy(dev.bus, temp3);
372 /* PROGRAM="executable" */
373 temp2 = strsep(&temp, ",");
374 retval = get_value("PROGRAM", &temp, &temp3);
377 strfieldcpy(dev.exec_program, temp3);
380 temp2 = strsep(&temp, ",");
381 retval = get_value("ID", &temp, &temp3);
384 strfieldcpy(dev.id, temp3);
386 /* NAME="new_name" */
387 temp2 = strsep(&temp, ",");
388 retval = get_value("NAME", &temp, &temp3);
391 strfieldcpy(dev.name, temp3);
392 dbg_parse("CALLOUT name='%s', program='%s'",
393 dev.name, dev.exec_program);
396 retval = add_dev(&dev);
398 dbg("add_dev returned with error %d", retval);
402 dbg_parse("%s:%d:%Zd: error parsing '%s'", udev_config_filename,
403 lineno, temp - line, temp);
410 static int namedev_init_permissions(void)
417 struct config_device dev;
419 dbg("opening '%s' to read as permissions config", udev_config_permission_filename);
420 fd = fopen(udev_config_permission_filename, "r");
422 dbg("can't open '%s'", udev_config_permission_filename);
426 /* loop through the whole file */
428 temp = fgets(line, sizeof(line), fd);
432 dbg_parse("read '%s'", temp);
434 /* eat the whitespace at the beginning of the line */
435 while (isspace(*temp))
442 /* see if this is a comment */
443 if (*temp == COMMENT_CHARACTER)
446 memset(&dev, 0x00, sizeof(dev));
449 temp2 = strsep(&temp, ":");
451 dbg("cannot parse line '%s'", line);
454 strncpy(dev.name, temp2, sizeof(dev.name));
456 temp2 = strsep(&temp, ":");
458 dbg("cannot parse line '%s'", line);
461 strncpy(dev.owner, temp2, sizeof(dev.owner));
463 temp2 = strsep(&temp, ":");
465 dbg("cannot parse line '%s'", line);
468 strncpy(dev.group, temp2, sizeof(dev.owner));
471 dbg("cannot parse line: %s", line);
474 dev.mode = strtol(temp, NULL, 8);
476 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
477 dev.name, dev.owner, dev.group,
479 retval = add_dev(&dev);
481 dbg("add_dev returned with error %d", retval);
491 static mode_t get_default_mode(struct sysfs_class_device *class_dev)
493 /* just default everyone to rw for the world! */
497 static void build_kernel_number(struct sysfs_class_device *class_dev, struct udevice *udev)
501 /* FIXME, figure out how to handle stuff like sdaj which will not work right now. */
502 dig = class_dev->name + strlen(class_dev->name);
503 while (isdigit(*(dig-1)))
505 strfieldcpy(udev->kernel_number, dig);
506 dbg("kernel_number='%s'", udev->kernel_number);
509 static void apply_format(struct udevice *udev, unsigned char *string)
511 char name[NAME_SIZE];
515 pos = strchr(string, '%');
518 strfieldcpy(name, pos+2);
522 if (strlen(udev->bus_id) == 0)
524 strcat(pos, udev->bus_id);
525 dbg("substitute bus_id '%s'", udev->bus_id);
528 if (strlen(udev->kernel_number) == 0)
530 strcat(pos, udev->kernel_number);
531 dbg("substitute kernel number '%s'", udev->kernel_number);
534 if (strlen(udev->kernel_number) == 0) {
539 strcat(pos, udev->kernel_number);
540 dbg("substitute kernel number '%s'", udev->kernel_number);
543 sprintf(pos, "%u", udev->minor);
544 dbg("substitute minor number '%u'", udev->minor);
547 sprintf(pos, "%u", udev->major);
548 dbg("substitute major number '%u'", udev->major);
551 if (strlen(udev->callout_value) == 0)
553 strcat(pos, udev->callout_value);
554 dbg("substitute callout output '%s'", udev->callout_value);
557 dbg("unknown substitution type '%%%c'", pos[1]);
560 strcat(string, name);
567 static int exec_callout(struct config_device *dev, char *value, int len)
577 char *args[CALLOUT_MAXARG];
580 dbg("callout to '%s'", dev->exec_program);
594 close(STDOUT_FILENO);
595 dup(fds[1]); /* dup write side of pipe to STDOUT */
596 if (strchr(dev->exec_program, ' ')) {
597 /* callout with arguments */
598 arg = dev->exec_program;
599 for (i=0; i < CALLOUT_MAXARG-1; i++) {
600 args[i] = strsep(&arg, " ");
605 dbg("too many args - %d", i);
608 retval = execve(args[0], args, main_envp);
610 retval = execve(dev->exec_program, main_argv, main_envp);
613 dbg("child execve failed");
616 return -1; /* avoid compiler warning */
618 /* parent reads from fds[0] */
622 res = read(fds[0], buffer, sizeof(buffer) - 1);
627 dbg("callout len %d too short", len);
631 dbg("callout value already set");
635 strncpy(value, buffer, len);
638 dbg("callout returned '%s'", value);
642 dbg("wait failed result %d", res);
647 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
648 dbg("callout program status 0x%x", status);
656 static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
658 struct config_device *dev;
659 struct list_head *tmp;
661 list_for_each(tmp, &config_device_list) {
662 dev = list_entry(tmp, struct config_device, node);
663 if (dev->type != CALLOUT)
667 dbg_parse("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
668 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
672 /* substitute anything that needs to be in the program name */
673 apply_format(udev, dev->exec_program);
674 if (exec_callout(dev, udev->callout_value, NAME_SIZE))
676 if (strncmp_wildcard(udev->callout_value, dev->id, NAME_SIZE) != 0)
678 strfieldcpy(udev->name, dev->name);
679 if (dev->mode != 0) {
680 udev->mode = dev->mode;
681 strfieldcpy(udev->owner, dev->owner);
682 strfieldcpy(udev->group, dev->group);
684 dbg_parse("callout returned matching value '%s', '%s' becomes '%s'"
685 " - owner='%s', group='%s', mode=%#o",
686 dev->id, class_dev->name, udev->name,
687 dev->owner, dev->group, dev->mode);
693 static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
695 struct sysfs_attribute *tmpattr = NULL;
696 struct config_device *dev;
697 struct list_head *tmp;
699 list_for_each(tmp, &config_device_list) {
700 dev = list_entry(tmp, struct config_device, node);
701 if (dev->type != LABEL)
705 dbg_parse("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
706 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
710 dbg_parse("look for device attribute '%s'", dev->sysfs_file);
711 /* try to find the attribute in the class device directory */
712 tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file);
716 /* look in the class device directory if present */
718 tmpattr = sysfs_get_device_attr(sysfs_device, dev->sysfs_file);
726 tmpattr->value[strlen(tmpattr->value)-1] = 0x00;
727 dbg_parse("compare attribute '%s' value '%s' with '%s'",
728 dev->sysfs_file, tmpattr->value, dev->sysfs_value);
729 if (strcmp(dev->sysfs_value, tmpattr->value) != 0)
732 strfieldcpy(udev->name, dev->name);
733 if (dev->mode != 0) {
734 udev->mode = dev->mode;
735 strfieldcpy(udev->owner, dev->owner);
736 strfieldcpy(udev->group, dev->group);
738 dbg_parse("found matching attribute '%s', '%s' becomes '%s' "
739 "- owner='%s', group='%s', mode=%#o",
740 dev->sysfs_file, class_dev->name, udev->name,
741 dev->owner, dev->group, dev->mode);
748 static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
750 struct config_device *dev;
751 struct list_head *tmp;
752 char path[SYSFS_PATH_MAX];
756 /* we have to have a sysfs device for NUMBER to work */
760 list_for_each(tmp, &config_device_list) {
761 dev = list_entry(tmp, struct config_device, node);
762 if (dev->type != NUMBER)
765 dbg_parse("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
766 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
770 strfieldcpy(path, sysfs_device->path);
771 temp = strrchr(path, '/');
772 dbg_parse("search '%s' in '%s', path='%s'", dev->id, temp, path);
773 if (strstr(temp, dev->id) != NULL) {
777 temp = strrchr(path, '/');
778 dbg_parse("search '%s' in '%s', path='%s'", dev->id, temp, path);
779 if (strstr(temp, dev->id) != NULL)
784 strfieldcpy(udev->name, dev->name);
785 if (dev->mode != 0) {
786 udev->mode = dev->mode;
787 strfieldcpy(udev->owner, dev->owner);
788 strfieldcpy(udev->group, dev->group);
790 dbg_parse("found matching id '%s', '%s' becomes '%s'"
791 " - owner='%s', group ='%s', mode=%#o",
792 dev->id, class_dev->name, udev->name,
793 dev->owner, dev->group, dev->mode);
800 static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
802 struct config_device *dev;
803 struct list_head *tmp;
804 char path[SYSFS_PATH_MAX];
808 /* we have to have a sysfs device for TOPOLOGY to work */
812 list_for_each(tmp, &config_device_list) {
813 dev = list_entry(tmp, struct config_device, node);
814 if (dev->type != TOPOLOGY)
817 dbg_parse("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
818 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
822 strfieldcpy(path, sysfs_device->path);
823 temp = strrchr(path, '/');
824 dbg_parse("search '%s' in '%s', path='%s'", dev->place, temp, path);
825 if (strstr(temp, dev->place) != NULL) {
829 temp = strrchr(path, '/');
830 dbg_parse("search '%s' in '%s', path='%s'", dev->place, temp, path);
831 if (strstr(temp, dev->place) != NULL)
837 strfieldcpy(udev->name, dev->name);
838 if (dev->mode != 0) {
839 udev->mode = dev->mode;
840 strfieldcpy(udev->owner, dev->owner);
841 strfieldcpy(udev->group, dev->group);
843 dbg_parse("found matching place '%s', '%s' becomes '%s'"
844 " - owner='%s', group ='%s', mode=%#o",
845 dev->place, class_dev->name, udev->name,
846 dev->owner, dev->group, dev->mode);
852 static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
854 struct config_device *dev;
855 struct list_head *tmp;
857 list_for_each(tmp, &config_device_list) {
858 dev = list_entry(tmp, struct config_device, node);
859 if (dev->type != REPLACE)
862 dbg_parse("compare name '%s' with '%s'",
863 dev->kernel_name, dev->name);
864 if (strcmp(dev->kernel_name, class_dev->name) != 0)
867 strfieldcpy(udev->name, dev->name);
868 if (dev->mode != 0) {
869 udev->mode = dev->mode;
870 strfieldcpy(udev->owner, dev->owner);
871 strfieldcpy(udev->group, dev->group);
873 dbg_parse("found name, '%s' becomes '%s' - owner='%s', group='%s', mode = %#o",
874 dev->kernel_name, udev->name,
875 dev->owner, dev->group, dev->mode);
882 static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev)
884 struct config_device *dev;
885 struct list_head *tmp;
888 strfieldcpy(udev->name, class_dev->name);
889 /* look for permissions */
890 list_for_each(tmp, &config_device_list) {
891 dev = list_entry(tmp, struct config_device, node);
892 len = strlen(dev->name);
893 if (strncmp_wildcard(class_dev->name, dev->name, sizeof(dev->name)))
895 if (dev->mode != 0) {
896 dbg_parse("found permissions for '%s'", class_dev->name);
897 udev->mode = dev->mode;
898 strfieldcpy(udev->owner, dev->owner);
899 strfieldcpy(udev->group, dev->group);
904 static int get_attr(struct sysfs_class_device *class_dev, struct udevice *udev)
906 struct sysfs_device *sysfs_device = NULL;
907 struct sysfs_class_device *class_dev_parent = NULL;
913 /* find the sysfs_device for this class device */
914 /* Wouldn't it really be nice if libsysfs could do this for us? */
915 if (class_dev->sysdevice) {
916 sysfs_device = class_dev->sysdevice;
918 /* bah, let's go backwards up a level to see if the device is there,
919 * as block partitions don't point to the physical device. Need to fix that
920 * up in the kernel...
922 if (strstr(class_dev->path, "block")) {
923 dbg_parse("looking at block device");
924 if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
925 char path[SYSFS_PATH_MAX];
927 dbg_parse("really is a partition");
928 strfieldcpy(path, class_dev->path);
929 temp = strrchr(path, '/');
931 dbg_parse("looking for a class device at '%s'", path);
932 class_dev_parent = sysfs_open_class_device(path);
933 if (class_dev_parent == NULL) {
934 dbg("sysfs_open_class_device at '%s' failed", path);
936 dbg_parse("class_dev_parent->name='%s'", class_dev_parent->name);
937 if (class_dev_parent->sysdevice)
938 sysfs_device = class_dev_parent->sysdevice;
945 dbg_parse("sysfs_device->path='%s'", sysfs_device->path);
946 dbg_parse("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
947 dbg_parse("sysfs_device->bus='%s'", sysfs_device->bus);
948 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
950 dbg_parse("class_dev->name = '%s'", class_dev->name);
953 build_kernel_number(class_dev, udev);
955 /* rules are looked at in priority order */
956 retval = do_callout(class_dev, udev, sysfs_device);
960 retval = do_label(class_dev, udev, sysfs_device);
964 retval = do_number(class_dev, udev, sysfs_device);
968 retval = do_topology(class_dev, udev, sysfs_device);
972 retval = do_replace(class_dev, udev, sysfs_device);
976 do_kernelname(class_dev, udev);
980 /* substitute placeholder in NAME */
981 apply_format(udev, udev->name);
984 /* mode was never set above */
986 udev->mode = get_default_mode(class_dev);
987 udev->owner[0] = 0x00;
988 udev->group[0] = 0x00;
991 if (class_dev_parent)
992 sysfs_close_class_device(class_dev_parent);
997 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *dev)
1001 retval = get_attr(class_dev, dev);
1003 dbg("get_attr failed");
1008 int namedev_init(void)
1012 retval = namedev_init_config();
1016 retval = namedev_init_permissions();