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->attr.owner, dev->attr.group, dev->attr.mode);
61 dbg_parse("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'"
62 " owner = '%s', group = '%s', mode = '%#o'",
63 dev->attr.name, dev->bus, dev->sysfs_file, dev->sysfs_value,
64 dev->attr.owner, dev->attr.group, dev->attr.mode);
67 dbg_parse("NUMBER name = '%s', bus = '%s', id = '%s'"
68 " owner = '%s', group = '%s', mode = '%#o'",
69 dev->attr.name, dev->bus, dev->id,
70 dev->attr.owner, dev->attr.group, dev->attr.mode);
73 dbg_parse("TOPOLOGY name = '%s', bus = '%s', place = '%s'"
74 " owner = '%s', group = '%s', mode = '%#o'",
75 dev->attr.name, dev->bus, dev->place,
76 dev->attr.owner, dev->attr.group, dev->attr.mode);
79 dbg_parse("REPLACE name = %s, kernel_name = %s"
80 " owner = '%s', group = '%s', mode = '%#o'",
81 dev->attr.name, dev->kernel_name,
82 dev->attr.owner, dev->attr.group, dev->attr.mode);
85 dbg_parse("CALLOUT name = '%s', program ='%s', bus = '%s', id = '%s'"
86 " owner = '%s', group = '%s', mode = '%#o'",
87 dev->attr.name, dev->exec_program, dev->bus, dev->id,
88 dev->attr.owner, dev->attr.group, dev->attr.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->attr.name, new_dev->attr.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, attr.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, attr.owner);
123 copy_string(dev, new_dev, attr.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)
207 struct config_device dev;
209 strcpy(filename, UDEV_CONFIG_DIR NAMEDEV_CONFIG_FILE);
210 dbg("opening %s to read as permissions config", filename);
211 fd = fopen(filename, "r");
213 dbg("Can't open %s", filename);
217 /* loop through the whole file */
220 temp = fgets(line, sizeof(line), fd);
224 dbg_parse("read %s", temp);
226 /* eat the whitespace at the beginning of the line */
227 while (isspace(*temp))
234 /* see if this is a comment */
235 if (*temp == COMMENT_CHARACTER)
238 memset(&dev, 0x00, sizeof(struct config_device));
241 temp2 = strsep(&temp, ",");
242 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
247 retval = get_value("BUS", &temp, &temp3);
250 strcpy(dev.bus, temp3);
253 temp2 = strsep(&temp, ",");
254 retval = get_pair(&temp, &temp2, &temp3);
257 strcpy(dev.sysfs_file, temp2);
258 strcpy(dev.sysfs_value, temp3);
260 /* NAME="new_name" */
261 temp2 = strsep(&temp, ",");
262 retval = get_value("NAME", &temp, &temp3);
265 strcpy(dev.attr.name, temp3);
267 dbg_parse("LABEL name = '%s', bus = '%s', "
268 "sysfs_file = '%s', sysfs_value = '%s'",
269 dev.attr.name, dev.bus, dev.sysfs_file,
273 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
278 retval = get_value("BUS", &temp, &temp3);
281 strcpy(dev.bus, temp3);
284 temp2 = strsep(&temp, ",");
285 retval = get_value("id", &temp, &temp3);
288 strcpy(dev.id, temp3);
290 /* NAME="new_name" */
291 temp2 = strsep(&temp, ",");
292 retval = get_value("NAME", &temp, &temp3);
295 strcpy(dev.attr.name, temp3);
297 dbg_parse("NUMBER name = '%s', bus = '%s', id = '%s'",
298 dev.attr.name, dev.bus, dev.id);
301 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
306 retval = get_value("BUS", &temp, &temp3);
309 strcpy(dev.bus, temp3);
312 temp2 = strsep(&temp, ",");
313 retval = get_value("place", &temp, &temp3);
316 strcpy(dev.place, temp3);
318 /* NAME="new_name" */
319 temp2 = strsep(&temp, ",");
320 retval = get_value("NAME", &temp, &temp3);
323 strcpy(dev.attr.name, temp3);
325 dbg_parse("TOPOLOGY name = '%s', bus = '%s', place = '%s'",
326 dev.attr.name, dev.bus, dev.place);
329 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
333 /* KERNEL="kernel_name" */
334 retval = get_value("KERNEL", &temp, &temp3);
337 strcpy(dev.kernel_name, temp3);
339 /* NAME="new_name" */
340 temp2 = strsep(&temp, ",");
341 retval = get_value("NAME", &temp, &temp3);
344 strcpy(dev.attr.name, temp3);
345 dbg_parse("REPLACE name = %s, kernel_name = %s",
346 dev.attr.name, dev.kernel_name);
348 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
352 /* PROGRAM="executable" */
353 retval = get_value("PROGRAM", &temp, &temp3);
356 strcpy(dev.exec_program, temp3);
359 temp2 = strsep(&temp, ",");
360 retval = get_value("BUS", &temp, &temp3);
363 strcpy(dev.bus, temp3);
366 temp2 = strsep(&temp, ",");
367 retval = get_value("ID", &temp, &temp3);
370 strcpy(dev.id, temp3);
372 /* NAME="new_name" */
373 temp2 = strsep(&temp, ",");
374 retval = get_value("NAME", &temp, &temp3);
377 strcpy(dev.attr.name, temp3);
378 dbg_parse("CALLOUT name = %s, program = %s",
379 dev.attr.name, dev.exec_program);
382 retval = add_dev(&dev);
384 dbg("add_dev returned with error %d", retval);
395 static int namedev_init_permissions(void)
403 struct config_device dev;
405 strcpy(filename, UDEV_CONFIG_DIR NAMEDEV_CONFIG_PERMISSION_FILE);
406 dbg("opening %s to read as permissions config", filename);
407 fd = fopen(filename, "r");
409 dbg("Can't open %s", filename);
413 /* loop through the whole file */
416 temp = fgets(line, sizeof(line), fd);
420 dbg_parse("read %s", temp);
422 /* eat the whitespace at the beginning of the line */
423 while (isspace(*temp))
430 /* see if this is a comment */
431 if (*temp == COMMENT_CHARACTER)
434 memset(&dev, 0x00, sizeof(dev));
437 temp2 = strsep(&temp, ":");
438 strncpy(dev.attr.name, temp2, sizeof(dev.attr.name));
440 temp2 = strsep(&temp, ":");
441 strncpy(dev.attr.owner, temp2, sizeof(dev.attr.owner));
443 temp2 = strsep(&temp, ":");
444 strncpy(dev.attr.group, temp2, sizeof(dev.attr.owner));
446 dev.attr.mode = strtol(temp, NULL, 8);
448 dbg_parse("name = %s, owner = %s, group = %s, mode = %#o",
449 dev.attr.name, dev.attr.owner, dev.attr.group,
451 retval = add_dev(&dev);
453 dbg("add_dev returned with error %d", retval);
463 static mode_t get_default_mode(struct sysfs_class_device *class_dev)
465 /* just default everyone to rw for the world! */
470 static int exec_callout(struct config_device *dev, char *value, int len)
480 dbg("callout to %s\n", dev->exec_program);
496 close(STDOUT_FILENO);
497 dup(fds[1]); /* dup write side of pipe to STDOUT */
498 retval = execve(dev->exec_program, main_argv, main_envp);
500 dbg("child execve failed");
503 return -1; /* avoid compiler warning */
506 * Parent reads from fds[0].
511 res = read(fds[0], buffer, sizeof(buffer) - 1);
516 dbg("callout len %d too short\n", len);
520 dbg("callout value already set");
524 strncpy(value, buffer, len);
530 dbg("wait failed result %d", res);
534 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
535 dbg("callout program status 0x%x", status);
542 static int get_attr(struct sysfs_class_device *class_dev, struct device_attr *attr)
544 struct list_head *tmp;
549 if (class_dev->sysdevice) {
550 dbg_parse("class_dev->sysdevice->path = '%s'", class_dev->sysdevice->path);
551 dbg_parse("class_dev->sysdevice->bus_id = '%s'", class_dev->sysdevice->bus_id);
553 dbg_parse("class_dev->name = '%s'", class_dev->name);
555 list_for_each(tmp, &config_device_list) {
556 struct config_device *dev = list_entry(tmp, struct config_device, node);
560 struct sysfs_attribute *tmpattr = NULL;
561 struct sysfs_class_device *class_dev_parent = NULL;
564 dbg_parse("LABEL: match file '%s' with value '%s'",
565 dev->sysfs_file, dev->sysfs_value);
566 /* try to find the attribute in the class device directory */
567 tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file);
571 /* look in the class device device directory if present */
572 if (class_dev->sysdevice) {
573 tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file);
578 /* bah, let's go backwards up a level to see if the device is there,
579 * as block partitions don't point to the physical device. Need to fix that
580 * up in the kernel...
582 if (strstr(class_dev->path, "block")) {
583 dbg_parse("looking at block device...");
584 if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
585 char path[SYSFS_PATH_MAX];
587 dbg_parse("really is a partition...");
588 strcpy(path, class_dev->path);
589 temp = strrchr(path, '/');
591 dbg_parse("looking for a class device at '%s'", path);
592 class_dev_parent = sysfs_open_class_device(path);
593 if (class_dev_parent == NULL) {
594 dbg("sysfs_open_class_device at '%s' failed", path);
597 dbg_parse("class_dev_parent->name = %s", class_dev_parent->name);
599 /* try to find the attribute in the class device directory */
600 tmpattr = sysfs_get_classdev_attr(class_dev_parent, dev->sysfs_file);
604 /* look in the class device device directory if present */
605 if (class_dev_parent->sysdevice) {
606 tmpattr = sysfs_get_classdev_attr(class_dev_parent, dev->sysfs_file);
613 if (class_dev_parent)
614 sysfs_close_class_device(class_dev_parent);
619 tmpattr->value[strlen(tmpattr->value)-1] = 0x00;
620 dbg_parse("file '%s' found with value '%s' compare with '%s'", dev->sysfs_file, tmpattr->value, dev->sysfs_value);
621 if (strcmp(dev->sysfs_value, tmpattr->value) != 0) {
622 if (class_dev_parent)
623 sysfs_close_class_device(class_dev_parent);
627 strcpy(attr->name, dev->attr.name);
628 if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
629 temp = &class_dev->path[strlen(class_dev->path)-1];
630 strcat(attr->name, temp);
632 if (dev->attr.mode != 0) {
633 attr->mode = dev->attr.mode;
634 strcpy(attr->owner, dev->attr.owner);
635 strcpy(attr->group, dev->attr.group);
637 dbg_parse("file '%s' with value '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
638 dev->sysfs_file, dev->sysfs_value, attr->name,
639 dev->attr.owner, dev->attr.group, dev->attr.mode);
640 if (class_dev_parent)
641 sysfs_close_class_device(class_dev_parent);
647 char path[SYSFS_PATH_MAX];
651 if (!class_dev->sysdevice)
653 strcpy(path, class_dev->sysdevice->path);
654 temp = strrchr(path, '/');
655 dbg_parse("NUMBER path = '%s'", path);
656 dbg_parse("NUMBER temp = '%s' id = '%s'", temp, dev->id);
657 if (strstr(temp, dev->id) != NULL) {
661 temp = strrchr(path, '/');
662 dbg_parse("NUMBERY temp = '%s' id = '%s'", temp, dev->id);
663 if (strstr(temp, dev->id) != NULL)
669 strcpy(attr->name, dev->attr.name);
670 if (dev->attr.mode != 0) {
671 attr->mode = dev->attr.mode;
672 strcpy(attr->owner, dev->attr.owner);
673 strcpy(attr->group, dev->attr.group);
675 dbg_parse("device id '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
677 dev->attr.owner, dev->attr.group, dev->attr.mode);
683 char path[SYSFS_PATH_MAX];
686 if (!class_dev->sysdevice)
689 strcpy(path, class_dev->sysdevice->path);
690 temp = strrchr(path, '/');
691 dbg_parse("TOPOLOGY path = '%s'", path);
692 dbg_parse("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
693 if (strstr(temp, dev->place) != NULL) {
697 temp = strrchr(path, '/');
698 dbg_parse("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
699 if (strstr(temp, dev->place) != NULL)
705 strcpy(attr->name, dev->attr.name);
706 if (dev->attr.mode != 0) {
707 attr->mode = dev->attr.mode;
708 strcpy(attr->owner, dev->attr.owner);
709 strcpy(attr->group, dev->attr.group);
711 dbg_parse("device at '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
712 dev->place, attr->name,
713 dev->attr.owner, dev->attr.group, dev->attr.mode);
721 if (exec_callout(dev, value, sizeof(value)))
723 if (strncmp(value, dev->id, sizeof(value)) != 0)
725 strcpy(attr->name, dev->attr.name);
726 if (dev->attr.mode != 0) {
727 attr->mode = dev->attr.mode;
728 strcpy(attr->owner, dev->attr.owner);
729 strcpy(attr->group, dev->attr.group);
731 dbg_parse("device callout '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
733 dev->attr.owner, dev->attr.group, dev->attr.mode);
738 if (strcmp(dev->kernel_name, class_dev->name) != 0)
740 strcpy(attr->name, dev->attr.name);
741 if (dev->attr.mode != 0) {
742 attr->mode = dev->attr.mode;
743 strcpy(attr->owner, dev->attr.owner);
744 strcpy(attr->group, dev->attr.group);
746 dbg_parse("'%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
747 dev->kernel_name, attr->name,
748 dev->attr.owner, dev->attr.group, dev->attr.mode);
754 dbg_parse("Unknown type of device '%d'", dev->type);
758 strcpy(attr->name, class_dev->name);
761 /* mode was never set above */
763 attr->mode = get_default_mode(class_dev);
764 attr->owner[0] = 0x00;
765 attr->group[0] = 0x00;
770 int namedev_name_device(struct sysfs_class_device *class_dev, struct device_attr *attr)
774 retval = get_attr(class_dev, attr);
776 dbg("get_attr failed");
781 int namedev_init(void)
785 retval = namedev_init_config();
789 retval = namedev_init_permissions();