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_value(const char *left, char **orig_string, char **ret_string)
151 char *string = *orig_string;
153 /* eat any whitespace */
154 while (isspace(*string))
157 /* split based on '=' */
158 temp = strsep(&string, "=");
159 if (strcasecmp(temp, left) == 0) {
160 /* got it, now strip off the '"' */
161 while (isspace(*string))
165 temp = strsep(&string, "\"");
167 *orig_string = string;
173 static int get_pair(char **orig_string, char **left, char **right)
176 char *string = *orig_string;
178 /* eat any whitespace */
179 while (isspace(*string))
182 /* split based on '=' */
183 temp = strsep(&string, "=");
186 /* take the right side and strip off the '"' */
187 while (isspace(*string))
191 temp = strsep(&string, "\"");
193 *orig_string = string;
198 static int namedev_init_config(void)
206 struct config_device dev;
208 dbg("opening %s to read as config", udev_config_filename);
209 fd = fopen(udev_config_filename, "r");
211 dbg("Can't open %s", udev_config_filename);
215 /* loop through the whole file */
218 temp = fgets(line, sizeof(line), fd);
222 dbg_parse("read %s", temp);
224 /* eat the whitespace at the beginning of the line */
225 while (isspace(*temp))
232 /* see if this is a comment */
233 if (*temp == COMMENT_CHARACTER)
236 memset(&dev, 0x00, sizeof(struct config_device));
239 temp2 = strsep(&temp, ",");
240 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
245 retval = get_value("BUS", &temp, &temp3);
248 strcpy(dev.bus, temp3);
251 temp2 = strsep(&temp, ",");
252 retval = get_pair(&temp, &temp2, &temp3);
255 strcpy(dev.sysfs_file, temp2);
256 strcpy(dev.sysfs_value, temp3);
258 /* NAME="new_name" */
259 temp2 = strsep(&temp, ",");
260 retval = get_value("NAME", &temp, &temp3);
263 strcpy(dev.name, temp3);
265 dbg_parse("LABEL name = '%s', bus = '%s', "
266 "sysfs_file = '%s', sysfs_value = '%s'",
267 dev.name, dev.bus, dev.sysfs_file,
271 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
276 retval = get_value("BUS", &temp, &temp3);
279 strcpy(dev.bus, temp3);
282 temp2 = strsep(&temp, ",");
283 retval = get_value("id", &temp, &temp3);
286 strcpy(dev.id, temp3);
288 /* NAME="new_name" */
289 temp2 = strsep(&temp, ",");
290 retval = get_value("NAME", &temp, &temp3);
293 strcpy(dev.name, temp3);
295 dbg_parse("NUMBER name = '%s', bus = '%s', id = '%s'",
296 dev.name, dev.bus, dev.id);
299 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
304 retval = get_value("BUS", &temp, &temp3);
307 strcpy(dev.bus, temp3);
310 temp2 = strsep(&temp, ",");
311 retval = get_value("place", &temp, &temp3);
314 strcpy(dev.place, temp3);
316 /* NAME="new_name" */
317 temp2 = strsep(&temp, ",");
318 retval = get_value("NAME", &temp, &temp3);
321 strcpy(dev.name, temp3);
323 dbg_parse("TOPOLOGY name = '%s', bus = '%s', place = '%s'",
324 dev.name, dev.bus, dev.place);
327 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
331 /* KERNEL="kernel_name" */
332 retval = get_value("KERNEL", &temp, &temp3);
335 strcpy(dev.kernel_name, temp3);
337 /* NAME="new_name" */
338 temp2 = strsep(&temp, ",");
339 retval = get_value("NAME", &temp, &temp3);
342 strcpy(dev.name, temp3);
343 dbg_parse("REPLACE name = %s, kernel_name = %s",
344 dev.name, dev.kernel_name);
346 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
350 /* PROGRAM="executable" */
351 retval = get_value("PROGRAM", &temp, &temp3);
354 strcpy(dev.exec_program, temp3);
357 temp2 = strsep(&temp, ",");
358 retval = get_value("BUS", &temp, &temp3);
361 strcpy(dev.bus, temp3);
364 temp2 = strsep(&temp, ",");
365 retval = get_value("ID", &temp, &temp3);
368 strcpy(dev.id, temp3);
370 /* NAME="new_name" */
371 temp2 = strsep(&temp, ",");
372 retval = get_value("NAME", &temp, &temp3);
375 strcpy(dev.name, temp3);
376 dbg_parse("CALLOUT name = %s, program = %s",
377 dev.name, dev.exec_program);
380 retval = add_dev(&dev);
382 dbg("add_dev returned with error %d", retval);
393 static int namedev_init_permissions(void)
400 struct config_device dev;
402 dbg("opening %s to read as permissions config", udev_config_permission_filename);
403 fd = fopen(udev_config_permission_filename, "r");
405 dbg("Can't open %s", udev_config_permission_filename);
409 /* loop through the whole file */
412 temp = fgets(line, sizeof(line), fd);
416 dbg_parse("read %s", temp);
418 /* eat the whitespace at the beginning of the line */
419 while (isspace(*temp))
426 /* see if this is a comment */
427 if (*temp == COMMENT_CHARACTER)
430 memset(&dev, 0x00, sizeof(dev));
433 temp2 = strsep(&temp, ":");
434 strncpy(dev.name, temp2, sizeof(dev.name));
436 temp2 = strsep(&temp, ":");
437 strncpy(dev.owner, temp2, sizeof(dev.owner));
439 temp2 = strsep(&temp, ":");
440 strncpy(dev.group, temp2, sizeof(dev.owner));
442 dev.mode = strtol(temp, NULL, 8);
444 dbg_parse("name = %s, owner = %s, group = %s, mode = %#o",
445 dev.name, dev.owner, dev.group,
447 retval = add_dev(&dev);
449 dbg("add_dev returned with error %d", retval);
459 static mode_t get_default_mode(struct sysfs_class_device *class_dev)
461 /* just default everyone to rw for the world! */
466 static int exec_callout(struct config_device *dev, char *value, int len)
476 dbg("callout to %s\n", dev->exec_program);
492 close(STDOUT_FILENO);
493 dup(fds[1]); /* dup write side of pipe to STDOUT */
494 retval = execve(dev->exec_program, main_argv, main_envp);
496 dbg("child execve failed");
499 return -1; /* avoid compiler warning */
502 * Parent reads from fds[0].
507 res = read(fds[0], buffer, sizeof(buffer) - 1);
512 dbg("callout len %d too short\n", len);
516 dbg("callout value already set");
520 strncpy(value, buffer, len);
526 dbg("wait failed result %d", res);
531 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
532 dbg("callout program status 0x%x", status);
540 static int get_attr(struct sysfs_class_device *class_dev, struct udevice *udev)
542 struct list_head *tmp;
547 if (class_dev->sysdevice) {
548 dbg_parse("class_dev->sysdevice->path = '%s'", class_dev->sysdevice->path);
549 dbg_parse("class_dev->sysdevice->bus_id = '%s'", class_dev->sysdevice->bus_id);
551 dbg_parse("class_dev->name = '%s'", class_dev->name);
553 list_for_each(tmp, &config_device_list) {
554 struct config_device *dev = list_entry(tmp, struct config_device, node);
558 struct sysfs_attribute *tmpattr = NULL;
559 struct sysfs_class_device *class_dev_parent = NULL;
562 dbg_parse("LABEL: match file '%s' with value '%s'",
563 dev->sysfs_file, dev->sysfs_value);
564 /* try to find the attribute in the class device directory */
565 tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file);
569 /* look in the class device directory if present */
570 if (class_dev->sysdevice) {
571 tmpattr = sysfs_get_device_attr(class_dev->sysdevice, dev->sysfs_file);
576 /* bah, let's go backwards up a level to see if the device is there,
577 * as block partitions don't point to the physical device. Need to fix that
578 * up in the kernel...
580 if (strstr(class_dev->path, "block")) {
581 dbg_parse("looking at block device...");
582 if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
583 char path[SYSFS_PATH_MAX];
585 dbg_parse("really is a partition...");
586 strcpy(path, class_dev->path);
587 temp = strrchr(path, '/');
589 dbg_parse("looking for a class device at '%s'", path);
590 class_dev_parent = sysfs_open_class_device(path);
591 if (class_dev_parent == NULL) {
592 dbg("sysfs_open_class_device at '%s' failed", path);
595 dbg_parse("class_dev_parent->name = %s", class_dev_parent->name);
597 /* try to find the attribute in the class device directory */
598 tmpattr = sysfs_get_classdev_attr(class_dev_parent, dev->sysfs_file);
602 /* look in the class device directory if present */
603 if (class_dev_parent->sysdevice) {
604 tmpattr = sysfs_get_device_attr(class_dev_parent->sysdevice, dev->sysfs_file);
611 if (class_dev_parent)
612 sysfs_close_class_device(class_dev_parent);
617 tmpattr->value[strlen(tmpattr->value)-1] = 0x00;
618 dbg_parse("file '%s' found with value '%s' compare with '%s'", dev->sysfs_file, tmpattr->value, dev->sysfs_value);
619 if (strcmp(dev->sysfs_value, tmpattr->value) != 0) {
620 if (class_dev_parent)
621 sysfs_close_class_device(class_dev_parent);
625 strcpy(udev->name, dev->name);
626 if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
627 temp = &class_dev->path[strlen(class_dev->path)-1];
628 strcat(udev->name, temp);
630 if (dev->mode != 0) {
631 udev->mode = dev->mode;
632 strcpy(udev->owner, dev->owner);
633 strcpy(udev->group, dev->group);
635 dbg_parse("file '%s' with value '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
636 dev->sysfs_file, dev->sysfs_value, udev->name,
637 dev->owner, dev->group, dev->mode);
638 if (class_dev_parent)
639 sysfs_close_class_device(class_dev_parent);
645 char path[SYSFS_PATH_MAX];
649 if (!class_dev->sysdevice)
651 strcpy(path, class_dev->sysdevice->path);
652 temp = strrchr(path, '/');
653 dbg_parse("NUMBER path = '%s'", path);
654 dbg_parse("NUMBER temp = '%s' id = '%s'", temp, dev->id);
655 if (strstr(temp, dev->id) != NULL) {
659 temp = strrchr(path, '/');
660 dbg_parse("NUMBERY temp = '%s' id = '%s'", temp, dev->id);
661 if (strstr(temp, dev->id) != NULL)
667 strcpy(udev->name, dev->name);
668 if (dev->mode != 0) {
669 udev->mode = dev->mode;
670 strcpy(udev->owner, dev->owner);
671 strcpy(udev->group, dev->group);
673 dbg_parse("device id '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
675 dev->owner, dev->group, dev->mode);
681 char path[SYSFS_PATH_MAX];
684 if (!class_dev->sysdevice)
687 strcpy(path, class_dev->sysdevice->path);
688 temp = strrchr(path, '/');
689 dbg_parse("TOPOLOGY path = '%s'", path);
690 dbg_parse("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
691 if (strstr(temp, dev->place) != NULL) {
695 temp = strrchr(path, '/');
696 dbg_parse("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
697 if (strstr(temp, dev->place) != NULL)
703 strcpy(udev->name, dev->name);
704 if (dev->mode != 0) {
705 udev->mode = dev->mode;
706 strcpy(udev->owner, dev->owner);
707 strcpy(udev->group, dev->group);
709 dbg_parse("device at '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
710 dev->place, udev->name,
711 dev->owner, dev->group, dev->mode);
719 if (exec_callout(dev, value, sizeof(value)))
721 if (strncmp(value, dev->id, sizeof(value)) != 0)
723 strcpy(udev->name, dev->name);
724 if (dev->mode != 0) {
725 udev->mode = dev->mode;
726 strcpy(udev->owner, dev->owner);
727 strcpy(udev->group, dev->group);
729 dbg_parse("device callout '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
731 dev->owner, dev->group, dev->mode);
736 if (strcmp(dev->kernel_name, class_dev->name) != 0)
738 strcpy(udev->name, dev->name);
739 if (dev->mode != 0) {
740 udev->mode = dev->mode;
741 strcpy(udev->owner, dev->owner);
742 strcpy(udev->group, dev->group);
744 dbg_parse("'%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
745 dev->kernel_name, udev->name,
746 dev->owner, dev->group, dev->mode);
752 dbg_parse("Unknown type of device '%d'", dev->type);
756 strcpy(udev->name, class_dev->name);
759 /* mode was never set above */
761 udev->mode = get_default_mode(class_dev);
762 udev->owner[0] = 0x00;
763 udev->group[0] = 0x00;
768 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *dev)
772 retval = get_attr(class_dev, dev);
774 dbg("get_attr failed");
779 int namedev_init(void)
783 retval = namedev_init_config();
787 retval = namedev_init_permissions();