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.
38 #include "udev_version.h"
40 #include "libsysfs/libsysfs.h"
42 #define TYPE_LABEL "LABEL"
43 #define TYPE_NUMBER "NUMBER"
44 #define TYPE_TOPOLOGY "TOPOLOGY"
45 #define TYPE_REPLACE "REPLACE"
46 #define TYPE_CALLOUT "CALLOUT"
48 static LIST_HEAD(config_device_list);
50 static void dump_dev(struct config_device *dev)
54 dbg("KERNEL name ='%s'"
55 " owner = '%s', group = '%s', mode = '%#o'",
57 dev->attr.owner, dev->attr.group, dev->attr.mode);
60 dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'"
61 " owner = '%s', group = '%s', mode = '%#o'",
62 dev->attr.name, dev->bus, dev->sysfs_file, dev->sysfs_value,
63 dev->attr.owner, dev->attr.group, dev->attr.mode);
66 dbg("NUMBER name = '%s', bus = '%s', id = '%s'"
67 " owner = '%s', group = '%s', mode = '%#o'",
68 dev->attr.name, dev->bus, dev->id,
69 dev->attr.owner, dev->attr.group, dev->attr.mode);
72 dbg("TOPOLOGY name = '%s', bus = '%s', place = '%s'"
73 " owner = '%s', group = '%s', mode = '%#o'",
74 dev->attr.name, dev->bus, dev->place,
75 dev->attr.owner, dev->attr.group, dev->attr.mode);
78 dbg("REPLACE name = %s, kernel_name = %s"
79 " owner = '%s', group = '%s', mode = '%#o'",
80 dev->attr.name, dev->kernel_name,
81 dev->attr.owner, dev->attr.group, dev->attr.mode);
84 dbg("CALLOUT name = '%s', program ='%s', bus = '%s', id = '%s'"
85 " owner = '%s', group = '%s', mode = '%#o'",
86 dev->attr.name, dev->exec_program, dev->bus, dev->id,
87 dev->attr.owner, dev->attr.group, dev->attr.mode);
90 dbg("Unknown type of device!");
94 #define copy_var(a, b, var) \
98 #define copy_string(a, b, var) \
100 strcpy(a->var, b->var);
102 static int add_dev(struct config_device *new_dev)
104 struct list_head *tmp;
105 struct config_device *tmp_dev;
107 /* loop through the whole list of devices to see if we already have
109 list_for_each(tmp, &config_device_list) {
110 struct config_device *dev = list_entry(tmp, struct config_device, node);
111 if (strcmp(dev->attr.name, new_dev->attr.name) == 0) {
112 /* the same, copy the new info into this structure */
113 copy_var(dev, new_dev, type);
114 copy_var(dev, new_dev, attr.mode);
115 copy_string(dev, new_dev, bus);
116 copy_string(dev, new_dev, sysfs_file);
117 copy_string(dev, new_dev, sysfs_value);
118 copy_string(dev, new_dev, id);
119 copy_string(dev, new_dev, place);
120 copy_string(dev, new_dev, kernel_name);
121 copy_string(dev, new_dev, attr.owner);
122 copy_string(dev, new_dev, attr.group);
127 /* not found, lets create a new structure, and add it to the list */
128 tmp_dev = malloc(sizeof(*tmp_dev));
131 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
132 list_add(&tmp_dev->node, &config_device_list);
137 static void dump_dev_list(void)
139 struct list_head *tmp;
141 list_for_each(tmp, &config_device_list) {
142 struct config_device *dev = list_entry(tmp, struct config_device, node);
147 static int get_value(const char *left, char **orig_string, char **ret_string)
150 char *string = *orig_string;
152 /* eat any whitespace */
153 while (isspace(*string))
156 /* split based on '=' */
157 temp = strsep(&string, "=");
158 if (strcasecmp(temp, left) == 0) {
159 /* got it, now strip off the '"' */
160 while (isspace(*string))
164 temp = strsep(&string, "\"");
166 *orig_string = string;
172 static int get_pair(char **orig_string, char **left, char **right)
175 char *string = *orig_string;
177 /* eat any whitespace */
178 while (isspace(*string))
181 /* split based on '=' */
182 temp = strsep(&string, "=");
185 /* take the right side and strip off the '"' */
186 while (isspace(*string))
190 temp = strsep(&string, "\"");
192 *orig_string = string;
197 static int namedev_init_config(void)
206 struct config_device dev;
208 strcpy(filename, NAMEDEV_CONFIG_ROOT NAMEDEV_CONFIG_FILE);
209 dbg("opening %s to read as permissions config", filename);
210 fd = fopen(filename, "r");
212 dbg("Can't open %s", filename);
216 /* loop through the whole file */
219 temp = fgets(line, sizeof(line), fd);
223 dbg("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 strcpy(dev.bus, temp3);
252 temp2 = strsep(&temp, ",");
253 retval = get_pair(&temp, &temp2, &temp3);
256 strcpy(dev.sysfs_file, temp2);
257 strcpy(dev.sysfs_value, temp3);
259 /* NAME="new_name" */
260 temp2 = strsep(&temp, ",");
261 retval = get_value("NAME", &temp, &temp3);
264 strcpy(dev.attr.name, temp3);
266 dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'", dev.attr.name, dev.bus, dev.sysfs_file, dev.sysfs_value);
269 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
274 retval = get_value("BUS", &temp, &temp3);
277 strcpy(dev.bus, temp3);
280 temp2 = strsep(&temp, ",");
281 retval = get_value("id", &temp, &temp3);
284 strcpy(dev.id, temp3);
286 /* NAME="new_name" */
287 temp2 = strsep(&temp, ",");
288 retval = get_value("NAME", &temp, &temp3);
291 strcpy(dev.attr.name, temp3);
293 dbg("NUMBER name = '%s', bus = '%s', id = '%s'", dev.attr.name, dev.bus, dev.id);
296 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
301 retval = get_value("BUS", &temp, &temp3);
304 strcpy(dev.bus, temp3);
307 temp2 = strsep(&temp, ",");
308 retval = get_value("place", &temp, &temp3);
311 strcpy(dev.place, temp3);
313 /* NAME="new_name" */
314 temp2 = strsep(&temp, ",");
315 retval = get_value("NAME", &temp, &temp3);
318 strcpy(dev.attr.name, temp3);
320 dbg("TOPOLOGY name = '%s', bus = '%s', place = '%s'", dev.attr.name, dev.bus, dev.place);
323 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
327 /* KERNEL="kernel_name" */
328 retval = get_value("KERNEL", &temp, &temp3);
331 strcpy(dev.kernel_name, temp3);
333 /* NAME="new_name" */
334 temp2 = strsep(&temp, ",");
335 retval = get_value("NAME", &temp, &temp3);
338 strcpy(dev.attr.name, temp3);
339 dbg("REPLACE name = %s, kernel_name = %s", dev.attr.name, dev.kernel_name);
341 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
345 /* PROGRAM="executable" */
346 retval = get_value("PROGRAM", &temp, &temp3);
349 strcpy(dev.exec_program, temp3);
352 temp2 = strsep(&temp, ",");
353 retval = get_value("BUS", &temp, &temp3);
356 strcpy(dev.bus, temp3);
359 temp2 = strsep(&temp, ",");
360 retval = get_value("ID", &temp, &temp3);
363 strcpy(dev.id, temp3);
365 /* NAME="new_name" */
366 temp2 = strsep(&temp, ",");
367 retval = get_value("NAME", &temp, &temp3);
370 strcpy(dev.attr.name, temp3);
371 dbg("CALLOUT name = %s, program = %s", dev.attr.name, dev.exec_program);
374 retval = add_dev(&dev);
376 dbg("add_dev returned with error %d", retval);
387 static int namedev_init_permissions(void)
395 struct config_device dev;
397 strcpy(filename, NAMEDEV_CONFIG_ROOT NAMEDEV_CONFIG_PERMISSION_FILE);
398 dbg("opening %s to read as permissions config", filename);
399 fd = fopen(filename, "r");
401 dbg("Can't open %s", filename);
405 /* loop through the whole file */
408 temp = fgets(line, sizeof(line), fd);
412 dbg("read %s", temp);
414 /* eat the whitespace at the beginning of the line */
415 while (isspace(*temp))
422 /* see if this is a comment */
423 if (*temp == COMMENT_CHARACTER)
426 memset(&dev, 0x00, sizeof(dev));
429 temp2 = strsep(&temp, ":");
430 strncpy(dev.attr.name, temp2, sizeof(dev.attr.name));
432 temp2 = strsep(&temp, ":");
433 strncpy(dev.attr.owner, temp2, sizeof(dev.attr.owner));
435 temp2 = strsep(&temp, ":");
436 strncpy(dev.attr.group, temp2, sizeof(dev.attr.owner));
438 dev.attr.mode = strtol(temp, NULL, 8);
440 dbg("name = %s, owner = %s, group = %s, mode = %#o", dev.attr.name, dev.attr.owner, dev.attr.group, dev.attr.mode);
441 retval = add_dev(&dev);
443 dbg("add_dev returned with error %d", retval);
453 static int get_default_mode(struct sysfs_class_device *class_dev)
455 /* just default everyone to rw for the world! */
460 static int exec_callout(struct config_device *dev, char *value, int len)
470 dbg("callout to %s\n", dev->exec_program);
486 close(STDOUT_FILENO);
487 dup(fds[1]); /* dup write side of pipe to STDOUT */
488 retval = execve(dev->exec_program, main_argv, main_envp);
490 dbg("child execve failed");
493 return -1; /* avoid compiler warning */
496 * Parent reads from fds[0].
501 res = read(fds[0], buffer, sizeof(buffer) - 1);
506 dbg("callout len %d too short\n", len);
510 dbg("callout value already set");
514 strncpy(value, buffer, len);
520 dbg("wait failed result %d", res);
524 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
525 dbg("callout program status 0x%x", status);
532 static int get_attr(struct sysfs_class_device *class_dev, struct device_attr *attr)
534 struct list_head *tmp;
539 if (class_dev->sysdevice) {
540 dbg("class_dev->sysdevice->directory->path = '%s'", class_dev->sysdevice->directory->path);
541 dbg("class_dev->sysdevice->bus_id = '%s'", class_dev->sysdevice->bus_id);
543 dbg("class_dev->name = '%s'", class_dev->name);
545 list_for_each(tmp, &config_device_list) {
546 struct config_device *dev = list_entry(tmp, struct config_device, node);
552 dbg("LABEL: match file '%s' with value '%s'", dev->sysfs_file, dev->sysfs_value);
553 /* try to find the attribute in the class device directory */
554 temp = sysfs_get_value_from_attributes(class_dev->directory->attributes, dev->sysfs_file);
558 /* look in the class device device directory if present */
559 if (class_dev->sysdevice) {
560 temp = sysfs_get_value_from_attributes(class_dev->sysdevice->directory->attributes, dev->sysfs_file);
565 /* bah, let's go backwards up a level to see if the device is there,
566 * as block partitions don't point to the physical device. Need to fix that
567 * up in the kernel...
569 if (strstr(class_dev->directory->path, "block")) {
570 dbg("looking at block device...");
571 if (isdigit(class_dev->directory->path[strlen(class_dev->directory->path)-1])) {
572 char path[SYSFS_PATH_MAX];
573 struct sysfs_class_device *class_dev_parent;
575 dbg("really is a partition...");
576 strcpy(path, class_dev->directory->path);
577 temp = strrchr(path, '/');
579 dbg("looking for a class device at '%s'", path);
580 class_dev_parent = sysfs_open_class_device(path);
581 if (class_dev_parent == NULL) {
582 dbg ("sysfs_open_class_device failed");
585 dbg("class_dev_parent->name = %s", class_dev_parent->name);
587 /* try to find the attribute in the class device directory */
588 temp = sysfs_get_value_from_attributes(class_dev_parent->directory->attributes, dev->sysfs_file);
590 //sysfs_close_class_device(class_dev_parent);
594 /* look in the class device device directory if present */
595 if (class_dev_parent->sysdevice) {
596 temp = sysfs_get_value_from_attributes(class_dev_parent->sysdevice->directory->attributes, dev->sysfs_file);
598 // sysfs_close_class_device(class_dev_parent);
608 temp[strlen(temp)-1] = 0x00;
609 dbg("file '%s' found with value '%s' compare with '%s'", dev->sysfs_file, temp, dev->sysfs_value);
610 if (strcmp(dev->sysfs_value, temp) != 0)
613 strcpy(attr->name, dev->attr.name);
614 if (isdigit(class_dev->directory->path[strlen(class_dev->directory->path)-1])) {
615 temp[0] = class_dev->directory->path[strlen(class_dev->directory->path)-1];
617 strcat(attr->name, temp);
619 if (dev->attr.mode != 0) {
620 attr->mode = dev->attr.mode;
621 strcpy(attr->owner, dev->attr.owner);
622 strcpy(attr->group, dev->attr.group);
624 dbg("file '%s' with value '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
625 dev->sysfs_file, dev->sysfs_value, attr->name,
626 dev->attr.owner, dev->attr.group, dev->attr.mode);
632 char path[SYSFS_PATH_MAX];
636 if (!class_dev->sysdevice)
638 strcpy(path, class_dev->sysdevice->directory->path);
639 temp = strrchr(path, '/');
640 dbg("NUMBER path = '%s'", path);
641 dbg("NUMBER temp = '%s' id = '%s'", temp, dev->id);
642 if (strstr(temp, dev->id) != NULL) {
646 temp = strrchr(path, '/');
647 dbg("NUMBERY temp = '%s' id = '%s'", temp, dev->id);
648 if (strstr(temp, dev->id) != NULL)
654 strcpy(attr->name, dev->attr.name);
655 if (dev->attr.mode != 0) {
656 attr->mode = dev->attr.mode;
657 strcpy(attr->owner, dev->attr.owner);
658 strcpy(attr->group, dev->attr.group);
660 dbg("device id '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
662 dev->attr.owner, dev->attr.group, dev->attr.mode);
668 char path[SYSFS_PATH_MAX];
671 if (!class_dev->sysdevice)
674 strcpy(path, class_dev->sysdevice->directory->path);
675 temp = strrchr(path, '/');
676 dbg("TOPOLOGY path = '%s'", path);
677 dbg("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
678 if (strstr(temp, dev->place) != NULL) {
682 temp = strrchr(path, '/');
683 dbg("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
684 if (strstr(temp, dev->place) != NULL)
690 strcpy(attr->name, dev->attr.name);
691 if (dev->attr.mode != 0) {
692 attr->mode = dev->attr.mode;
693 strcpy(attr->owner, dev->attr.owner);
694 strcpy(attr->group, dev->attr.group);
696 dbg("device at '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
697 dev->place, attr->name,
698 dev->attr.owner, dev->attr.group, dev->attr.mode);
706 if (exec_callout(dev, value, sizeof(value)))
708 if (strncmp(value, dev->id, sizeof(value)) != 0)
710 strcpy(attr->name, dev->attr.name);
711 if (dev->attr.mode != 0) {
712 attr->mode = dev->attr.mode;
713 strcpy(attr->owner, dev->attr.owner);
714 strcpy(attr->group, dev->attr.group);
716 dbg("device callout '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
718 dev->attr.owner, dev->attr.group, dev->attr.mode);
723 if (strcmp(dev->kernel_name, class_dev->name) != 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("'%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
732 dev->kernel_name, attr->name,
733 dev->attr.owner, dev->attr.group, dev->attr.mode);
739 dbg("Unknown type of device '%d'", dev->type);
743 strcpy(attr->name, class_dev->name);
746 if (attr->mode == -1) {
747 attr->mode = get_default_mode(class_dev);
748 attr->owner[0] = 0x00;
749 attr->group[0] = 0x00;
754 int namedev_name_device(struct sysfs_class_device *class_dev, struct device_attr *attr)
758 retval = get_attr(class_dev, attr);
760 dbg("get_attr failed");
765 int namedev_init(void)
769 retval = namedev_init_config();
773 retval = namedev_init_permissions();