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"
43 #define TYPE_LABEL "LABEL"
44 #define TYPE_NUMBER "NUMBER"
45 #define TYPE_TOPOLOGY "TOPOLOGY"
46 #define TYPE_REPLACE "REPLACE"
47 #define TYPE_CALLOUT "CALLOUT"
49 static LIST_HEAD(config_device_list);
51 static void dump_dev(struct config_device *dev)
55 dbg_parse("KERNEL name ='%s'"
56 " owner = '%s', group = '%s', mode = '%#o'",
58 dev->owner, dev->group, dev->mode);
61 dbg_parse("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'"
62 " owner = '%s', group = '%s', mode = '%#o'",
63 dev->name, dev->bus, dev->sysfs_file, dev->sysfs_value,
64 dev->owner, dev->group, dev->mode);
67 dbg_parse("NUMBER name = '%s', bus = '%s', id = '%s'"
68 " owner = '%s', group = '%s', mode = '%#o'",
69 dev->name, dev->bus, dev->id,
70 dev->owner, dev->group, dev->mode);
73 dbg_parse("TOPOLOGY name = '%s', bus = '%s', place = '%s'"
74 " owner = '%s', group = '%s', mode = '%#o'",
75 dev->name, dev->bus, dev->place,
76 dev->owner, dev->group, dev->mode);
79 dbg_parse("REPLACE name = %s, kernel_name = %s"
80 " owner = '%s', group = '%s', mode = '%#o'",
81 dev->name, dev->kernel_name,
82 dev->owner, dev->group, dev->mode);
85 dbg_parse("CALLOUT name = '%s', program ='%s', bus = '%s', id = '%s'"
86 " owner = '%s', group = '%s', mode = '%#o'",
87 dev->name, dev->exec_program, dev->bus, dev->id,
88 dev->owner, dev->group, dev->mode);
91 dbg_parse("Unknown type of device!");
95 #define copy_var(a, b, var) \
99 #define copy_string(a, b, var) \
100 if (strlen(b->var)) \
101 strcpy(a->var, b->var);
103 static int add_dev(struct config_device *new_dev)
105 struct list_head *tmp;
106 struct config_device *tmp_dev;
108 /* update the values if we already have the device */
109 list_for_each(tmp, &config_device_list) {
110 struct config_device *dev = list_entry(tmp, struct config_device, node);
111 int len = strlen(new_dev->name);
112 if (new_dev->name[len-1] == '*') {
114 if (strncmp(dev->name, new_dev->name, len))
117 if (strcmp(dev->name, new_dev->name))
120 /* the same, copy the new info into this structure */
121 copy_var(dev, new_dev, type);
122 copy_var(dev, new_dev, mode);
123 copy_string(dev, new_dev, bus);
124 copy_string(dev, new_dev, sysfs_file);
125 copy_string(dev, new_dev, sysfs_value);
126 copy_string(dev, new_dev, id);
127 copy_string(dev, new_dev, place);
128 copy_string(dev, new_dev, kernel_name);
129 copy_string(dev, new_dev, owner);
130 copy_string(dev, new_dev, group);
134 /* not found, lets create a new structure, and add it to the list */
135 tmp_dev = malloc(sizeof(*tmp_dev));
138 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
139 list_add(&tmp_dev->node, &config_device_list);
144 static void dump_dev_list(void)
146 struct list_head *tmp;
148 list_for_each(tmp, &config_device_list) {
149 struct config_device *dev = list_entry(tmp, struct config_device, node);
154 static int get_pair(char **orig_string, char **left, char **right)
157 char *string = *orig_string;
162 /* eat any whitespace */
163 while (isspace(*string))
166 /* split based on '=' */
167 temp = strsep(&string, "=");
172 /* take the right side and strip off the '"' */
173 while (isspace(*string))
180 temp = strsep(&string, "\"");
181 if (!string || *temp == '\0')
184 *orig_string = string;
189 static int get_value(const char *left, char **orig_string, char **ret_string)
194 retval = get_pair(orig_string, &left_string, ret_string);
197 if (strcasecmp(left_string, left) != 0)
202 static int namedev_init_config(void)
211 struct config_device dev;
213 dbg("opening %s to read as config", udev_config_filename);
214 fd = fopen(udev_config_filename, "r");
216 dbg("Can't open %s", udev_config_filename);
220 /* loop through the whole file */
224 temp = fgets(line, sizeof(line), fd);
229 dbg_parse("read %s", temp);
231 /* eat the whitespace at the beginning of the line */
232 while (isspace(*temp))
239 /* see if this is a comment */
240 if (*temp == COMMENT_CHARACTER)
243 memset(&dev, 0x00, sizeof(struct config_device));
246 temp2 = strsep(&temp, ",");
247 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
252 retval = get_value("BUS", &temp, &temp3);
255 strfieldcpy(dev.bus, temp3);
258 temp2 = strsep(&temp, ",");
259 retval = get_pair(&temp, &temp2, &temp3);
262 strfieldcpy(dev.sysfs_file, temp2);
263 strfieldcpy(dev.sysfs_value, temp3);
265 /* NAME="new_name" */
266 temp2 = strsep(&temp, ",");
267 retval = get_value("NAME", &temp, &temp3);
270 strfieldcpy(dev.name, temp3);
272 dbg_parse("LABEL name = '%s', bus = '%s', "
273 "sysfs_file = '%s', sysfs_value = '%s'",
274 dev.name, dev.bus, dev.sysfs_file,
278 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
283 retval = get_value("BUS", &temp, &temp3);
286 strfieldcpy(dev.bus, temp3);
289 temp2 = strsep(&temp, ",");
290 retval = get_value("id", &temp, &temp3);
293 strfieldcpy(dev.id, temp3);
295 /* NAME="new_name" */
296 temp2 = strsep(&temp, ",");
297 retval = get_value("NAME", &temp, &temp3);
300 strfieldcpy(dev.name, temp3);
302 dbg_parse("NUMBER name = '%s', bus = '%s', id = '%s'",
303 dev.name, dev.bus, dev.id);
306 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
311 retval = get_value("BUS", &temp, &temp3);
314 strfieldcpy(dev.bus, temp3);
317 temp2 = strsep(&temp, ",");
318 retval = get_value("place", &temp, &temp3);
321 strfieldcpy(dev.place, temp3);
323 /* NAME="new_name" */
324 temp2 = strsep(&temp, ",");
325 retval = get_value("NAME", &temp, &temp3);
328 strfieldcpy(dev.name, temp3);
330 dbg_parse("TOPOLOGY name = '%s', bus = '%s', place = '%s'",
331 dev.name, dev.bus, dev.place);
334 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
338 /* KERNEL="kernel_name" */
339 retval = get_value("KERNEL", &temp, &temp3);
342 strfieldcpy(dev.kernel_name, temp3);
344 /* NAME="new_name" */
345 temp2 = strsep(&temp, ",");
346 retval = get_value("NAME", &temp, &temp3);
349 strfieldcpy(dev.name, temp3);
350 dbg_parse("REPLACE name = %s, kernel_name = %s",
351 dev.name, dev.kernel_name);
353 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
357 /* PROGRAM="executable" */
358 retval = get_value("PROGRAM", &temp, &temp3);
361 strfieldcpy(dev.exec_program, temp3);
364 temp2 = strsep(&temp, ",");
365 retval = get_value("BUS", &temp, &temp3);
368 strfieldcpy(dev.bus, temp3);
371 temp2 = strsep(&temp, ",");
372 retval = get_value("ID", &temp, &temp3);
375 strfieldcpy(dev.id, temp3);
377 /* NAME="new_name" */
378 temp2 = strsep(&temp, ",");
379 retval = get_value("NAME", &temp, &temp3);
382 strfieldcpy(dev.name, temp3);
383 dbg_parse("CALLOUT name = %s, program = %s",
384 dev.name, dev.exec_program);
387 retval = add_dev(&dev);
389 dbg("add_dev returned with error %d", retval);
393 dbg_parse("%s:%d:%Zd: error parsing ``%s''", udev_config_filename,
394 lineno, temp - line, temp);
401 static int namedev_init_permissions(void)
408 struct config_device dev;
410 dbg("opening %s to read as permissions config", udev_config_permission_filename);
411 fd = fopen(udev_config_permission_filename, "r");
413 dbg("Can't open %s", udev_config_permission_filename);
417 /* loop through the whole file */
420 temp = fgets(line, sizeof(line), fd);
424 dbg_parse("read %s", temp);
426 /* eat the whitespace at the beginning of the line */
427 while (isspace(*temp))
434 /* see if this is a comment */
435 if (*temp == COMMENT_CHARACTER)
438 memset(&dev, 0x00, sizeof(dev));
441 temp2 = strsep(&temp, ":");
442 strncpy(dev.name, temp2, sizeof(dev.name));
444 temp2 = strsep(&temp, ":");
445 strncpy(dev.owner, temp2, sizeof(dev.owner));
447 temp2 = strsep(&temp, ":");
448 strncpy(dev.group, temp2, sizeof(dev.owner));
450 dev.mode = strtol(temp, NULL, 8);
452 dbg_parse("name = %s, owner = %s, group = %s, mode = %#o",
453 dev.name, dev.owner, dev.group,
455 retval = add_dev(&dev);
457 dbg("add_dev returned with error %d", retval);
467 static mode_t get_default_mode(struct sysfs_class_device *class_dev)
469 /* just default everyone to rw for the world! */
474 static int exec_callout(struct config_device *dev, char *value, int len)
484 dbg("callout to %s\n", dev->exec_program);
500 close(STDOUT_FILENO);
501 dup(fds[1]); /* dup write side of pipe to STDOUT */
502 retval = execve(dev->exec_program, main_argv, main_envp);
504 dbg("child execve failed");
507 return -1; /* avoid compiler warning */
510 * Parent reads from fds[0].
515 res = read(fds[0], buffer, sizeof(buffer) - 1);
520 dbg("callout len %d too short\n", len);
524 dbg("callout value already set");
528 strncpy(value, buffer, len);
534 dbg("wait failed result %d", res);
539 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
540 dbg("callout program status 0x%x", status);
548 static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev)
550 struct config_device *dev;
551 struct list_head *tmp;
554 list_for_each(tmp, &config_device_list) {
555 dev = list_entry(tmp, struct config_device, node);
556 if (dev->type != CALLOUT)
559 if (exec_callout(dev, value, sizeof(value)))
561 if (strncmp(value, dev->id, sizeof(value)) != 0)
563 strfieldcpy(udev->name, dev->name);
564 if (dev->mode != 0) {
565 udev->mode = dev->mode;
566 strfieldcpy(udev->owner, dev->owner);
567 strfieldcpy(udev->group, dev->group);
569 dbg_parse("device callout '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
571 dev->owner, dev->group, dev->mode);
577 static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
579 struct sysfs_attribute *tmpattr = NULL;
580 struct config_device *dev;
581 struct list_head *tmp;
583 list_for_each(tmp, &config_device_list) {
584 dev = list_entry(tmp, struct config_device, node);
585 if (dev->type != LABEL)
588 dbg_parse("LABEL: match file '%s' with value '%s'",
589 dev->sysfs_file, dev->sysfs_value);
590 /* try to find the attribute in the class device directory */
591 tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file);
595 /* look in the class device directory if present */
597 tmpattr = sysfs_get_device_attr(sysfs_device, dev->sysfs_file);
605 tmpattr->value[strlen(tmpattr->value)-1] = 0x00;
606 dbg_parse("file '%s' found with value '%s' compare with '%s'", dev->sysfs_file, tmpattr->value, dev->sysfs_value);
607 if (strcmp(dev->sysfs_value, tmpattr->value) != 0)
610 strfieldcpy(udev->name, dev->name);
611 if (dev->mode != 0) {
612 udev->mode = dev->mode;
613 strfieldcpy(udev->owner, dev->owner);
614 strfieldcpy(udev->group, dev->group);
616 dbg_parse("file '%s' with value '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
617 dev->sysfs_file, dev->sysfs_value, udev->name,
618 dev->owner, dev->group, dev->mode);
625 static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
627 struct config_device *dev;
628 struct list_head *tmp;
629 char path[SYSFS_PATH_MAX];
633 /* we have to have a sysfs device for NUMBER to work */
637 list_for_each(tmp, &config_device_list) {
638 dev = list_entry(tmp, struct config_device, node);
639 if (dev->type != NUMBER)
643 strfieldcpy(path, sysfs_device->path);
644 temp = strrchr(path, '/');
645 dbg_parse("NUMBER path = '%s'", path);
646 dbg_parse("NUMBER temp = '%s' id = '%s'", temp, dev->id);
647 if (strstr(temp, dev->id) != NULL) {
651 temp = strrchr(path, '/');
652 dbg_parse("NUMBER temp = '%s' id = '%s'", temp, dev->id);
653 if (strstr(temp, dev->id) != NULL)
658 strfieldcpy(udev->name, dev->name);
659 if (dev->mode != 0) {
660 udev->mode = dev->mode;
661 strfieldcpy(udev->owner, dev->owner);
662 strfieldcpy(udev->group, dev->group);
664 dbg_parse("device id '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
666 dev->owner, dev->group, dev->mode);
673 static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
675 struct config_device *dev;
676 struct list_head *tmp;
677 char path[SYSFS_PATH_MAX];
681 /* we have to have a sysfs device for TOPOLOGY to work */
685 list_for_each(tmp, &config_device_list) {
686 dev = list_entry(tmp, struct config_device, node);
687 if (dev->type != TOPOLOGY)
691 strfieldcpy(path, sysfs_device->path);
692 temp = strrchr(path, '/');
693 dbg_parse("TOPOLOGY path = '%s'", path);
694 dbg_parse("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
695 if (strstr(temp, dev->place) != NULL) {
699 temp = strrchr(path, '/');
700 dbg_parse("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
701 if (strstr(temp, dev->place) != NULL)
707 strfieldcpy(udev->name, dev->name);
708 if (dev->mode != 0) {
709 udev->mode = dev->mode;
710 strfieldcpy(udev->owner, dev->owner);
711 strfieldcpy(udev->group, dev->group);
713 dbg_parse("device at '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
714 dev->place, udev->name,
715 dev->owner, dev->group, dev->mode);
722 static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev)
724 struct config_device *dev;
725 struct list_head *tmp;
727 list_for_each(tmp, &config_device_list) {
728 dev = list_entry(tmp, struct config_device, node);
729 if (dev->type != REPLACE)
732 dbg_parse("REPLACE: replace name '%s' with '%s'",
733 dev->kernel_name, dev->name);
734 if (strcmp(dev->kernel_name, class_dev->name) != 0)
737 strfieldcpy(udev->name, dev->name);
738 if (dev->mode != 0) {
739 udev->mode = dev->mode;
740 strfieldcpy(udev->owner, dev->owner);
741 strfieldcpy(udev->group, dev->group);
743 dbg_parse("'%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
744 dev->kernel_name, udev->name,
745 dev->owner, dev->group, dev->mode);
752 static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev)
754 struct config_device *dev;
755 struct list_head *tmp;
758 strfieldcpy(udev->name, class_dev->name);
759 list_for_each(tmp, &config_device_list) {
760 dev = list_entry(tmp, struct config_device, node);
761 len = strlen(dev->name);
762 if (dev->name[len-1] == '*') {
764 if (strncmp(dev->name, class_dev->name, len))
767 if (strcmp(dev->name, class_dev->name))
770 if (dev->mode != 0) {
771 dbg_parse("found permissions from config for '%s'", class_dev->name);
772 udev->mode = dev->mode;
773 strfieldcpy(udev->owner, dev->owner);
774 strfieldcpy(udev->group, dev->group);
779 static int get_attr(struct sysfs_class_device *class_dev, struct udevice *udev)
781 struct sysfs_device *sysfs_device = NULL;
782 struct sysfs_class_device *class_dev_parent = NULL;
788 /* find the sysfs_device for this class device */
789 /* Wouldn't it really be nice if libsysfs could do this for us? */
790 if (class_dev->sysdevice) {
791 sysfs_device = class_dev->sysdevice;
793 /* bah, let's go backwards up a level to see if the device is there,
794 * as block partitions don't point to the physical device. Need to fix that
795 * up in the kernel...
797 if (strstr(class_dev->path, "block")) {
798 dbg_parse("looking at block device...");
799 if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
800 char path[SYSFS_PATH_MAX];
802 dbg_parse("really is a partition...");
803 strfieldcpy(path, class_dev->path);
804 temp = strrchr(path, '/');
806 dbg_parse("looking for a class device at '%s'", path);
807 class_dev_parent = sysfs_open_class_device(path);
808 if (class_dev_parent == NULL) {
809 dbg("sysfs_open_class_device at '%s' failed", path);
811 dbg_parse("class_dev_parent->name = %s", class_dev_parent->name);
812 if (class_dev_parent->sysdevice)
813 sysfs_device = class_dev_parent->sysdevice;
820 dbg_parse("sysfs_device->path = '%s'", sysfs_device->path);
821 dbg_parse("sysfs_device->bus_id = '%s'", sysfs_device->bus_id);
823 dbg_parse("class_dev->name = '%s'", class_dev->name);
826 /* rules are looked at in priority order */
827 retval = do_callout(class_dev, udev);
831 retval = do_label(class_dev, udev, sysfs_device);
835 retval = do_number(class_dev, udev, sysfs_device);
839 retval = do_topology(class_dev, udev, sysfs_device);
843 retval = do_replace(class_dev, udev);
847 do_kernelname(class_dev, udev);
851 /* substitute placeholder in NAME */
853 char *pos = strchr(udev->name, '%');
855 char name[NAME_SIZE];
857 strfieldcpy(name, pos+2);
863 strcat(udev->name, sysfs_device->bus_id);
864 dbg("bus_id appended: %s",
865 sysfs_device->bus_id);
868 dig = class_dev->name + strlen(class_dev->name);
869 while (isdigit(*(dig-1)))
871 strcat(udev->name, dig);
872 dbg("kernel number appended: %s", dig);
875 sprintf(pos, "%u", udev->minor);
876 dbg("minor number appended: %u", udev->minor);
879 sprintf(pos, "%u", udev->major);
880 dbg("major number appended: %u", udev->major);
883 dbg("unknown substitution type: %%%c", pos[1]);
886 strcat(udev->name, name);
892 /* mode was never set above */
894 udev->mode = get_default_mode(class_dev);
895 udev->owner[0] = 0x00;
896 udev->group[0] = 0x00;
899 if (class_dev_parent)
900 sysfs_close_class_device(class_dev_parent);
905 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *dev)
909 retval = get_attr(class_dev, dev);
911 dbg("get_attr failed");
916 int namedev_init(void)
920 retval = namedev_init_config();
924 retval = namedev_init_permissions();