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.
37 #include "udev_version.h"
39 #include "libsysfs/libsysfs.h"
41 #define TYPE_LABEL "LABEL"
42 #define TYPE_NUMBER "NUMBER"
43 #define TYPE_TOPOLOGY "TOPOLOGY"
44 #define TYPE_REPLACE "REPLACE"
46 static LIST_HEAD(config_device_list);
48 static void dump_dev(struct config_device *dev)
52 dbg("KERNEL name ='%s'"
53 " owner = '%s', group = '%s', mode = '%#o'",
55 dev->attr.owner, dev->attr.group, dev->attr.mode);
58 dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'"
59 " owner = '%s', group = '%s', mode = '%#o'",
60 dev->attr.name, dev->bus, dev->sysfs_file, dev->sysfs_value,
61 dev->attr.owner, dev->attr.group, dev->attr.mode);
64 dbg("NUMBER name = '%s', bus = '%s', id = '%s'"
65 " owner = '%s', group = '%s', mode = '%#o'",
66 dev->attr.name, dev->bus, dev->id,
67 dev->attr.owner, dev->attr.group, dev->attr.mode);
70 dbg("TOPOLOGY name = '%s', bus = '%s', place = '%s'"
71 " owner = '%s', group = '%s', mode = '%#o'",
72 dev->attr.name, dev->bus, dev->place,
73 dev->attr.owner, dev->attr.group, dev->attr.mode);
76 dbg("REPLACE name = %s, kernel_name = %s"
77 " owner = '%s', group = '%s', mode = '%#o'",
78 dev->attr.name, dev->kernel_name,
79 dev->attr.owner, dev->attr.group, dev->attr.mode);
82 dbg("Unknown type of device!");
86 #define copy_var(a, b, var) \
90 #define copy_string(a, b, var) \
92 strcpy(a->var, b->var);
94 static int add_dev(struct config_device *new_dev)
96 struct list_head *tmp;
97 struct config_device *tmp_dev;
99 /* loop through the whole list of devices to see if we already have
101 list_for_each(tmp, &config_device_list) {
102 struct config_device *dev = list_entry(tmp, struct config_device, node);
103 if (strcmp(dev->attr.name, new_dev->attr.name) == 0) {
104 /* the same, copy the new info into this structure */
105 copy_var(dev, new_dev, type);
106 copy_var(dev, new_dev, attr.mode);
107 copy_string(dev, new_dev, bus);
108 copy_string(dev, new_dev, sysfs_file);
109 copy_string(dev, new_dev, sysfs_value);
110 copy_string(dev, new_dev, id);
111 copy_string(dev, new_dev, place);
112 copy_string(dev, new_dev, kernel_name);
113 copy_string(dev, new_dev, attr.owner);
114 copy_string(dev, new_dev, attr.group);
119 /* not found, lets create a new structure, and add it to the list */
120 tmp_dev = malloc(sizeof(*tmp_dev));
123 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
124 list_add(&tmp_dev->node, &config_device_list);
129 static void dump_dev_list(void)
131 struct list_head *tmp;
133 list_for_each(tmp, &config_device_list) {
134 struct config_device *dev = list_entry(tmp, struct config_device, node);
139 static int get_value(const char *left, char **orig_string, char **ret_string)
142 char *string = *orig_string;
144 /* eat any whitespace */
145 while (isspace(*string))
148 /* split based on '=' */
149 temp = strsep(&string, "=");
150 if (strcasecmp(temp, left) == 0) {
151 /* got it, now strip off the '"' */
152 while (isspace(*string))
156 temp = strsep(&string, "\"");
158 *orig_string = string;
164 static int get_pair(char **orig_string, char **left, char **right)
167 char *string = *orig_string;
169 /* eat any whitespace */
170 while (isspace(*string))
173 /* split based on '=' */
174 temp = strsep(&string, "=");
177 /* take the right side and strip off the '"' */
178 while (isspace(*string))
182 temp = strsep(&string, "\"");
184 *orig_string = string;
189 static int namedev_init_config(void)
198 struct config_device dev;
200 strcpy(filename, NAMEDEV_CONFIG_ROOT NAMEDEV_CONFIG_FILE);
201 dbg("opening %s to read as permissions config", filename);
202 fd = fopen(filename, "r");
204 dbg("Can't open %s", filename);
208 /* loop through the whole file */
211 temp = fgets(line, sizeof(line), fd);
215 dbg("read %s", temp);
217 /* eat the whitespace at the beginning of the line */
218 while (isspace(*temp))
225 /* see if this is a comment */
226 if (*temp == COMMENT_CHARACTER)
229 memset(&dev, 0x00, sizeof(struct config_device));
232 temp2 = strsep(&temp, ",");
233 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
238 retval = get_value("BUS", &temp, &temp3);
241 strcpy(dev.bus, temp3);
244 temp2 = strsep(&temp, ",");
245 retval = get_pair(&temp, &temp2, &temp3);
248 strcpy(dev.sysfs_file, temp2);
249 strcpy(dev.sysfs_value, temp3);
251 /* NAME="new_name" */
252 temp2 = strsep(&temp, ",");
253 retval = get_value("NAME", &temp, &temp3);
256 strcpy(dev.attr.name, temp3);
258 dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'", dev.attr.name, dev.bus, dev.sysfs_file, dev.sysfs_value);
261 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
266 retval = get_value("BUS", &temp, &temp3);
269 strcpy(dev.bus, temp3);
272 temp2 = strsep(&temp, ",");
273 retval = get_value("id", &temp, &temp3);
276 strcpy(dev.id, temp3);
278 /* NAME="new_name" */
279 temp2 = strsep(&temp, ",");
280 retval = get_value("NAME", &temp, &temp3);
283 strcpy(dev.attr.name, temp3);
285 dbg("NUMBER name = '%s', bus = '%s', id = '%s'", dev.attr.name, dev.bus, dev.id);
288 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
293 retval = get_value("BUS", &temp, &temp3);
296 strcpy(dev.bus, temp3);
299 temp2 = strsep(&temp, ",");
300 retval = get_value("place", &temp, &temp3);
303 strcpy(dev.place, temp3);
305 /* NAME="new_name" */
306 temp2 = strsep(&temp, ",");
307 retval = get_value("NAME", &temp, &temp3);
310 strcpy(dev.attr.name, temp3);
312 dbg("TOPOLOGY name = '%s', bus = '%s', place = '%s'", dev.attr.name, dev.bus, dev.place);
315 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
319 /* KERNEL="kernel_name" */
320 retval = get_value("KERNEL", &temp, &temp3);
323 strcpy(dev.kernel_name, temp3);
325 /* NAME="new_name" */
326 temp2 = strsep(&temp, ",");
327 retval = get_value("NAME", &temp, &temp3);
330 strcpy(dev.attr.name, temp3);
331 dbg("REPLACE name = %s, kernel_name = %s", dev.attr.name, dev.kernel_name);
334 retval = add_dev(&dev);
336 dbg("add_dev returned with error %d", retval);
347 static int namedev_init_permissions(void)
355 struct config_device dev;
357 strcpy(filename, NAMEDEV_CONFIG_ROOT NAMEDEV_CONFIG_PERMISSION_FILE);
358 dbg("opening %s to read as permissions config", filename);
359 fd = fopen(filename, "r");
361 dbg("Can't open %s", filename);
365 /* loop through the whole file */
368 temp = fgets(line, sizeof(line), fd);
372 dbg("read %s", temp);
374 /* eat the whitespace at the beginning of the line */
375 while (isspace(*temp))
382 /* see if this is a comment */
383 if (*temp == COMMENT_CHARACTER)
386 memset(&dev, 0x00, sizeof(dev));
389 temp2 = strsep(&temp, ":");
390 strncpy(dev.attr.name, temp2, sizeof(dev.attr.name));
392 temp2 = strsep(&temp, ":");
393 strncpy(dev.attr.owner, temp2, sizeof(dev.attr.owner));
395 temp2 = strsep(&temp, ":");
396 strncpy(dev.attr.group, temp2, sizeof(dev.attr.owner));
398 dev.attr.mode = strtol(temp, NULL, 8);
400 dbg("name = %s, owner = %s, group = %s, mode = %#o", dev.attr.name, dev.attr.owner, dev.attr.group, dev.attr.mode);
401 retval = add_dev(&dev);
403 dbg("add_dev returned with error %d", retval);
413 static int get_default_mode(struct sysfs_class_device *class_dev)
415 /* just default everyone to rw for the world! */
420 static int get_attr(struct sysfs_class_device *class_dev, struct device_attr *attr)
422 struct list_head *tmp;
427 if (class_dev->sysdevice) {
428 dbg("class_dev->sysdevice->directory->path = '%s'", class_dev->sysdevice->directory->path);
429 dbg("class_dev->sysdevice->bus_id = '%s'", class_dev->sysdevice->bus_id);
431 dbg("class_dev->name = '%s'", class_dev->name);
433 list_for_each(tmp, &config_device_list) {
434 struct config_device *dev = list_entry(tmp, struct config_device, node);
440 dbg("LABEL: match file '%s' with value '%s'", dev->sysfs_file, dev->sysfs_value);
441 /* try to find the attribute in the class device directory */
442 temp = sysfs_get_value_from_attributes(class_dev->directory->attributes, dev->sysfs_file);
446 /* look in the class device device directory if present */
447 if (class_dev->sysdevice) {
448 temp = sysfs_get_value_from_attributes(class_dev->sysdevice->directory->attributes, dev->sysfs_file);
453 /* bah, let's go backwards up a level to see if the device is there,
454 * as block partitions don't point to the physical device. Need to fix that
455 * up in the kernel...
457 if (strstr(class_dev->directory->path, "block")) {
458 dbg("looking at block device...");
459 if (isdigit(class_dev->directory->path[strlen(class_dev->directory->path)-1])) {
460 char path[SYSFS_PATH_MAX];
461 struct sysfs_class_device *class_dev_parent;
463 dbg("really is a partition...");
464 strcpy(path, class_dev->directory->path);
465 temp = strrchr(path, '/');
467 dbg("looking for a class device at '%s'", path);
468 class_dev_parent = sysfs_open_class_device(path);
469 if (class_dev_parent == NULL) {
470 dbg ("sysfs_open_class_device failed");
473 dbg("class_dev_parent->name = %s", class_dev_parent->name);
475 /* try to find the attribute in the class device directory */
476 temp = sysfs_get_value_from_attributes(class_dev_parent->directory->attributes, dev->sysfs_file);
478 //sysfs_close_class_device(class_dev_parent);
482 /* look in the class device device directory if present */
483 if (class_dev_parent->sysdevice) {
484 temp = sysfs_get_value_from_attributes(class_dev_parent->sysdevice->directory->attributes, dev->sysfs_file);
486 // sysfs_close_class_device(class_dev_parent);
496 temp[strlen(temp)-1] = 0x00;
497 dbg("file '%s' found with value '%s' compare with '%s'", dev->sysfs_file, temp, dev->sysfs_value);
498 if (strcmp(dev->sysfs_value, temp) != 0)
501 strcpy(attr->name, dev->attr.name);
502 if (isdigit(class_dev->directory->path[strlen(class_dev->directory->path)-1])) {
503 temp[0] = class_dev->directory->path[strlen(class_dev->directory->path)-1];
505 strcat(attr->name, temp);
507 if (dev->attr.mode != 0) {
508 attr->mode = dev->attr.mode;
509 strcpy(attr->owner, dev->attr.owner);
510 strcpy(attr->group, dev->attr.group);
512 dbg("file '%s' with value '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
513 dev->sysfs_file, dev->sysfs_value, attr->name,
514 dev->attr.owner, dev->attr.group, dev->attr.mode);
520 char path[SYSFS_PATH_MAX];
524 if (!class_dev->sysdevice)
526 strcpy(path, class_dev->sysdevice->directory->path);
527 temp = strrchr(path, '/');
528 dbg("NUMBER path = '%s'", path);
529 dbg("NUMBER temp = '%s' id = '%s'", temp, dev->id);
530 if (strstr(temp, dev->id) != NULL) {
534 temp = strrchr(path, '/');
535 dbg("NUMBERY temp = '%s' id = '%s'", temp, dev->id);
536 if (strstr(temp, dev->id) != NULL)
542 strcpy(attr->name, dev->attr.name);
543 if (dev->attr.mode != 0) {
544 attr->mode = dev->attr.mode;
545 strcpy(attr->owner, dev->attr.owner);
546 strcpy(attr->group, dev->attr.group);
548 dbg("device id '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
550 dev->attr.owner, dev->attr.group, dev->attr.mode);
556 char path[SYSFS_PATH_MAX];
559 if (!class_dev->sysdevice)
562 strcpy(path, class_dev->sysdevice->directory->path);
563 temp = strrchr(path, '/');
564 dbg("TOPOLOGY path = '%s'", path);
565 dbg("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
566 if (strstr(temp, dev->place) != NULL) {
570 temp = strrchr(path, '/');
571 dbg("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
572 if (strstr(temp, dev->place) != NULL)
578 strcpy(attr->name, dev->attr.name);
579 if (dev->attr.mode != 0) {
580 attr->mode = dev->attr.mode;
581 strcpy(attr->owner, dev->attr.owner);
582 strcpy(attr->group, dev->attr.group);
584 dbg("device at '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
585 dev->place, attr->name,
586 dev->attr.owner, dev->attr.group, dev->attr.mode);
591 if (strcmp(dev->kernel_name, class_dev->name) != 0)
593 strcpy(attr->name, dev->attr.name);
594 if (dev->attr.mode != 0) {
595 attr->mode = dev->attr.mode;
596 strcpy(attr->owner, dev->attr.owner);
597 strcpy(attr->group, dev->attr.group);
599 dbg("'%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
600 dev->kernel_name, attr->name,
601 dev->attr.owner, dev->attr.group, dev->attr.mode);
607 dbg("Unknown type of device '%d'", dev->type);
611 strcpy(attr->name, class_dev->name);
614 if (attr->mode == -1) {
615 attr->mode = get_default_mode(class_dev);
616 attr->owner[0] = 0x00;
617 attr->group[0] = 0x00;
622 int namedev_name_device(struct sysfs_class_device *class_dev, struct device_attr *attr)
626 retval = get_attr(class_dev, attr);
628 dbg("get_attr failed");
633 int namedev_init(void)
637 retval = namedev_init_config();
641 retval = namedev_init_permissions();