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 /* loop through the whole list of devices to see if we already have
110 list_for_each(tmp, &config_device_list) {
111 struct config_device *dev = list_entry(tmp, struct config_device, node);
112 if (strcmp(dev->name, new_dev->name) == 0) {
113 /* the same, copy the new info into this structure */
114 copy_var(dev, new_dev, type);
115 copy_var(dev, new_dev, mode);
116 copy_string(dev, new_dev, bus);
117 copy_string(dev, new_dev, sysfs_file);
118 copy_string(dev, new_dev, sysfs_value);
119 copy_string(dev, new_dev, id);
120 copy_string(dev, new_dev, place);
121 copy_string(dev, new_dev, kernel_name);
122 copy_string(dev, new_dev, owner);
123 copy_string(dev, new_dev, group);
128 /* not found, lets create a new structure, and add it to the list */
129 tmp_dev = malloc(sizeof(*tmp_dev));
132 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
133 list_add(&tmp_dev->node, &config_device_list);
138 static void dump_dev_list(void)
140 struct list_head *tmp;
142 list_for_each(tmp, &config_device_list) {
143 struct config_device *dev = list_entry(tmp, struct config_device, node);
148 static int get_pair(char **orig_string, char **left, char **right)
151 char *string = *orig_string;
156 /* eat any whitespace */
157 while (isspace(*string))
160 /* split based on '=' */
161 temp = strsep(&string, "=");
166 /* take the right side and strip off the '"' */
167 while (isspace(*string))
174 temp = strsep(&string, "\"");
175 if (!string || *temp == '\0')
178 *orig_string = string;
183 static int get_value(const char *left, char **orig_string, char **ret_string)
188 retval = get_pair(orig_string, &left_string, ret_string);
191 if (strcasecmp(left_string, left) != 0)
196 static int namedev_init_config(void)
205 struct config_device dev;
207 dbg("opening %s to read as config", udev_config_filename);
208 fd = fopen(udev_config_filename, "r");
210 dbg("Can't open %s", udev_config_filename);
214 /* loop through the whole file */
218 temp = fgets(line, sizeof(line), fd);
223 dbg_parse("read %s", temp);
225 /* eat the whitespace at the beginning of the line */
226 while (isspace(*temp))
233 /* see if this is a comment */
234 if (*temp == COMMENT_CHARACTER)
237 memset(&dev, 0x00, sizeof(struct config_device));
240 temp2 = strsep(&temp, ",");
241 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
246 retval = get_value("BUS", &temp, &temp3);
249 strfieldcpy(dev.bus, temp3);
252 temp2 = strsep(&temp, ",");
253 retval = get_pair(&temp, &temp2, &temp3);
256 strfieldcpy(dev.sysfs_file, temp2);
257 strfieldcpy(dev.sysfs_value, temp3);
259 /* NAME="new_name" */
260 temp2 = strsep(&temp, ",");
261 retval = get_value("NAME", &temp, &temp3);
264 strfieldcpy(dev.name, temp3);
266 dbg_parse("LABEL name = '%s', bus = '%s', "
267 "sysfs_file = '%s', sysfs_value = '%s'",
268 dev.name, dev.bus, dev.sysfs_file,
272 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
277 retval = get_value("BUS", &temp, &temp3);
280 strfieldcpy(dev.bus, temp3);
283 temp2 = strsep(&temp, ",");
284 retval = get_value("id", &temp, &temp3);
287 strfieldcpy(dev.id, temp3);
289 /* NAME="new_name" */
290 temp2 = strsep(&temp, ",");
291 retval = get_value("NAME", &temp, &temp3);
294 strfieldcpy(dev.name, temp3);
296 dbg_parse("NUMBER name = '%s', bus = '%s', id = '%s'",
297 dev.name, dev.bus, dev.id);
300 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
305 retval = get_value("BUS", &temp, &temp3);
308 strfieldcpy(dev.bus, temp3);
311 temp2 = strsep(&temp, ",");
312 retval = get_value("place", &temp, &temp3);
315 strfieldcpy(dev.place, temp3);
317 /* NAME="new_name" */
318 temp2 = strsep(&temp, ",");
319 retval = get_value("NAME", &temp, &temp3);
322 strfieldcpy(dev.name, temp3);
324 dbg_parse("TOPOLOGY name = '%s', bus = '%s', place = '%s'",
325 dev.name, dev.bus, dev.place);
328 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
332 /* KERNEL="kernel_name" */
333 retval = get_value("KERNEL", &temp, &temp3);
336 strfieldcpy(dev.kernel_name, temp3);
338 /* NAME="new_name" */
339 temp2 = strsep(&temp, ",");
340 retval = get_value("NAME", &temp, &temp3);
343 strfieldcpy(dev.name, temp3);
344 dbg_parse("REPLACE name = %s, kernel_name = %s",
345 dev.name, dev.kernel_name);
347 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
351 /* PROGRAM="executable" */
352 retval = get_value("PROGRAM", &temp, &temp3);
355 strfieldcpy(dev.exec_program, temp3);
358 temp2 = strsep(&temp, ",");
359 retval = get_value("BUS", &temp, &temp3);
362 strfieldcpy(dev.bus, temp3);
365 temp2 = strsep(&temp, ",");
366 retval = get_value("ID", &temp, &temp3);
369 strfieldcpy(dev.id, temp3);
371 /* NAME="new_name" */
372 temp2 = strsep(&temp, ",");
373 retval = get_value("NAME", &temp, &temp3);
376 strfieldcpy(dev.name, temp3);
377 dbg_parse("CALLOUT name = %s, program = %s",
378 dev.name, dev.exec_program);
381 retval = add_dev(&dev);
383 dbg("add_dev returned with error %d", retval);
387 dbg_parse("%s:%d:%Zd: error parsing ``%s''", udev_config_filename,
388 lineno, temp - line, temp);
395 static int namedev_init_permissions(void)
402 struct config_device dev;
404 dbg("opening %s to read as permissions config", udev_config_permission_filename);
405 fd = fopen(udev_config_permission_filename, "r");
407 dbg("Can't open %s", udev_config_permission_filename);
411 /* loop through the whole file */
414 temp = fgets(line, sizeof(line), fd);
418 dbg_parse("read %s", temp);
420 /* eat the whitespace at the beginning of the line */
421 while (isspace(*temp))
428 /* see if this is a comment */
429 if (*temp == COMMENT_CHARACTER)
432 memset(&dev, 0x00, sizeof(dev));
435 temp2 = strsep(&temp, ":");
436 strncpy(dev.name, temp2, sizeof(dev.name));
438 temp2 = strsep(&temp, ":");
439 strncpy(dev.owner, temp2, sizeof(dev.owner));
441 temp2 = strsep(&temp, ":");
442 strncpy(dev.group, temp2, sizeof(dev.owner));
444 dev.mode = strtol(temp, NULL, 8);
446 dbg_parse("name = %s, owner = %s, group = %s, mode = %#o",
447 dev.name, dev.owner, dev.group,
449 retval = add_dev(&dev);
451 dbg("add_dev returned with error %d", retval);
461 static mode_t get_default_mode(struct sysfs_class_device *class_dev)
463 /* just default everyone to rw for the world! */
468 static int exec_callout(struct config_device *dev, char *value, int len)
478 dbg("callout to %s\n", dev->exec_program);
494 close(STDOUT_FILENO);
495 dup(fds[1]); /* dup write side of pipe to STDOUT */
496 retval = execve(dev->exec_program, main_argv, main_envp);
498 dbg("child execve failed");
501 return -1; /* avoid compiler warning */
504 * Parent reads from fds[0].
509 res = read(fds[0], buffer, sizeof(buffer) - 1);
514 dbg("callout len %d too short\n", len);
518 dbg("callout value already set");
522 strncpy(value, buffer, len);
528 dbg("wait failed result %d", res);
533 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
534 dbg("callout program status 0x%x", status);
542 static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev)
544 struct config_device *dev;
545 struct list_head *tmp;
548 list_for_each(tmp, &config_device_list) {
549 dev = list_entry(tmp, struct config_device, node);
550 if (dev->type != CALLOUT)
553 if (exec_callout(dev, value, sizeof(value)))
555 if (strncmp(value, dev->id, sizeof(value)) != 0)
557 strfieldcpy(udev->name, dev->name);
558 if (dev->mode != 0) {
559 udev->mode = dev->mode;
560 strfieldcpy(udev->owner, dev->owner);
561 strfieldcpy(udev->group, dev->group);
563 dbg_parse("device callout '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
565 dev->owner, dev->group, dev->mode);
571 static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
573 struct sysfs_attribute *tmpattr = NULL;
574 struct config_device *dev;
575 struct list_head *tmp;
577 list_for_each(tmp, &config_device_list) {
578 dev = list_entry(tmp, struct config_device, node);
579 if (dev->type != LABEL)
582 dbg_parse("LABEL: match file '%s' with value '%s'",
583 dev->sysfs_file, dev->sysfs_value);
584 /* try to find the attribute in the class device directory */
585 tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file);
589 /* look in the class device directory if present */
591 tmpattr = sysfs_get_device_attr(sysfs_device, dev->sysfs_file);
599 tmpattr->value[strlen(tmpattr->value)-1] = 0x00;
600 dbg_parse("file '%s' found with value '%s' compare with '%s'", dev->sysfs_file, tmpattr->value, dev->sysfs_value);
601 if (strcmp(dev->sysfs_value, tmpattr->value) != 0)
604 strfieldcpy(udev->name, dev->name);
605 if (dev->mode != 0) {
606 udev->mode = dev->mode;
607 strfieldcpy(udev->owner, dev->owner);
608 strfieldcpy(udev->group, dev->group);
610 dbg_parse("file '%s' with value '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
611 dev->sysfs_file, dev->sysfs_value, udev->name,
612 dev->owner, dev->group, dev->mode);
619 static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
621 struct config_device *dev;
622 struct list_head *tmp;
623 char path[SYSFS_PATH_MAX];
627 /* we have to have a sysfs device for NUMBER to work */
631 list_for_each(tmp, &config_device_list) {
632 dev = list_entry(tmp, struct config_device, node);
633 if (dev->type != NUMBER)
637 strfieldcpy(path, sysfs_device->path);
638 temp = strrchr(path, '/');
639 dbg_parse("NUMBER path = '%s'", path);
640 dbg_parse("NUMBER temp = '%s' id = '%s'", temp, dev->id);
641 if (strstr(temp, dev->id) != NULL) {
645 temp = strrchr(path, '/');
646 dbg_parse("NUMBER temp = '%s' id = '%s'", temp, dev->id);
647 if (strstr(temp, dev->id) != NULL)
652 strfieldcpy(udev->name, dev->name);
653 if (dev->mode != 0) {
654 udev->mode = dev->mode;
655 strfieldcpy(udev->owner, dev->owner);
656 strfieldcpy(udev->group, dev->group);
658 dbg_parse("device id '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
660 dev->owner, dev->group, dev->mode);
667 static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
669 struct config_device *dev;
670 struct list_head *tmp;
671 char path[SYSFS_PATH_MAX];
675 /* we have to have a sysfs device for TOPOLOGY to work */
679 list_for_each(tmp, &config_device_list) {
680 dev = list_entry(tmp, struct config_device, node);
681 if (dev->type != TOPOLOGY)
685 strfieldcpy(path, sysfs_device->path);
686 temp = strrchr(path, '/');
687 dbg_parse("TOPOLOGY path = '%s'", path);
688 dbg_parse("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
689 if (strstr(temp, dev->place) != NULL) {
693 temp = strrchr(path, '/');
694 dbg_parse("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
695 if (strstr(temp, dev->place) != NULL)
701 strfieldcpy(udev->name, dev->name);
702 if (dev->mode != 0) {
703 udev->mode = dev->mode;
704 strfieldcpy(udev->owner, dev->owner);
705 strfieldcpy(udev->group, dev->group);
707 dbg_parse("device at '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
708 dev->place, udev->name,
709 dev->owner, dev->group, dev->mode);
716 static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev)
718 struct config_device *dev;
719 struct list_head *tmp;
721 list_for_each(tmp, &config_device_list) {
722 dev = list_entry(tmp, struct config_device, node);
723 if (dev->type != REPLACE)
726 dbg_parse("REPLACE: replace name '%s' with '%s'",
727 dev->kernel_name, dev->name);
728 if (strcmp(dev->kernel_name, class_dev->name) != 0)
731 strfieldcpy(udev->name, dev->name);
732 if (dev->mode != 0) {
733 udev->mode = dev->mode;
734 strfieldcpy(udev->owner, dev->owner);
735 strfieldcpy(udev->group, dev->group);
737 dbg_parse("'%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
738 dev->kernel_name, udev->name,
739 dev->owner, dev->group, dev->mode);
746 static int get_attr(struct sysfs_class_device *class_dev, struct udevice *udev)
748 struct sysfs_device *sysfs_device = NULL;
749 struct sysfs_class_device *class_dev_parent = NULL;
755 /* find the sysfs_device for this class device */
756 /* Wouldn't it really be nice if libsysfs could do this for us? */
757 if (class_dev->sysdevice) {
758 sysfs_device = class_dev->sysdevice;
760 /* bah, let's go backwards up a level to see if the device is there,
761 * as block partitions don't point to the physical device. Need to fix that
762 * up in the kernel...
764 if (strstr(class_dev->path, "block")) {
765 dbg_parse("looking at block device...");
766 if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
767 char path[SYSFS_PATH_MAX];
769 dbg_parse("really is a partition...");
770 strfieldcpy(path, class_dev->path);
771 temp = strrchr(path, '/');
773 dbg_parse("looking for a class device at '%s'", path);
774 class_dev_parent = sysfs_open_class_device(path);
775 if (class_dev_parent == NULL) {
776 dbg("sysfs_open_class_device at '%s' failed", path);
778 dbg_parse("class_dev_parent->name = %s", class_dev_parent->name);
779 if (class_dev_parent->sysdevice)
780 sysfs_device = class_dev_parent->sysdevice;
787 dbg_parse("sysfs_device->path = '%s'", sysfs_device->path);
788 dbg_parse("sysfs_device->bus_id = '%s'", sysfs_device->bus_id);
790 dbg_parse("class_dev->name = '%s'", class_dev->name);
793 /* rules are looked at in priority order */
794 retval = do_callout(class_dev, udev);
798 retval = do_label(class_dev, udev, sysfs_device);
802 retval = do_number(class_dev, udev, sysfs_device);
806 retval = do_topology(class_dev, udev, sysfs_device);
810 retval = do_replace(class_dev, udev);
814 strfieldcpy(udev->name, class_dev->name);
817 /* substitute placeholder in NAME */
819 char *pos = strchr(udev->name, '%');
821 char name[NAME_SIZE];
823 strfieldcpy(name, pos+2);
829 strcat(udev->name, sysfs_device->bus_id);
830 dbg("bus_id appended: %s",
831 sysfs_device->bus_id);
834 dig = class_dev->name + strlen(class_dev->name);
835 while (isdigit(*(dig-1)))
837 strcat(udev->name, dig);
838 dbg("kernel number appended: %s", dig);
841 sprintf(pos, "%u", udev->minor);
842 dbg("minor number appended: %u", udev->minor);
845 sprintf(pos, "%u", udev->major);
846 dbg("major number appended: %u", udev->major);
849 dbg("unknown substitution type: %%%c", pos[1]);
852 strcat(udev->name, name);
857 /* mode was never set above */
859 udev->mode = get_default_mode(class_dev);
860 udev->owner[0] = 0x00;
861 udev->group[0] = 0x00;
864 if (class_dev_parent)
865 sysfs_close_class_device(class_dev_parent);
870 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *dev)
874 retval = get_attr(class_dev, dev);
876 dbg("get_attr failed");
881 int namedev_init(void)
885 retval = namedev_init_config();
889 retval = namedev_init_permissions();