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.
35 #include "udev_version.h"
37 #include "libsysfs/libsysfs.h"
39 #define TYPE_LABEL "LABEL"
40 #define TYPE_NUMBER "NUMBER"
41 #define TYPE_TOPOLOGY "TOPOLOGY"
42 #define TYPE_REPLACE "REPLACE"
45 KERNEL_NAME = 0, /* must be 0 to let memset() default to this value */
54 #define VALUE_SIZE 100
59 struct config_device {
60 struct list_head node;
62 enum config_type type;
65 char sysfs_file[FILE_SIZE];
66 char sysfs_value[VALUE_SIZE];
68 char place[PLACE_SIZE];
69 char kernel_name[NAME_SIZE];
71 /* what to set the device to */
74 char owner[OWNER_SIZE];
75 char group[GROUP_SIZE];
79 static LIST_HEAD(config_device_list);
81 static void dump_dev(struct config_device *dev)
85 dbg("KERNEL name ='%s'"
86 " owner = '%s', group = '%s', mode = '%#o'",
88 dev->owner, dev->group, dev->mode);
91 dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'"
92 " owner = '%s', group = '%s', mode = '%#o'",
93 dev->name, dev->bus, dev->sysfs_file, dev->sysfs_value,
94 dev->owner, dev->group, dev->mode);
97 dbg("NUMBER name = '%s', bus = '%s', id = '%s'"
98 " owner = '%s', group = '%s', mode = '%#o'",
99 dev->name, dev->bus, dev->id,
100 dev->owner, dev->group, dev->mode);
103 dbg("TOPOLOGY name = '%s', bus = '%s', place = '%s'"
104 " owner = '%s', group = '%s', mode = '%#o'",
105 dev->name, dev->bus, dev->place,
106 dev->owner, dev->group, dev->mode);
109 dbg("REPLACE name = %s, kernel_name = %s"
110 " owner = '%s', group = '%s', mode = '%#o'",
111 dev->name, dev->kernel_name,
112 dev->owner, dev->group, dev->mode);
115 dbg("Unknown type of device!");
119 #define copy_var(a, b, var) \
123 #define copy_string(a, b, var) \
124 if (strlen(b->var)) \
125 strcpy(a->var, b->var);
127 static int add_dev(struct config_device *new_dev)
129 struct list_head *tmp;
130 struct config_device *tmp_dev;
132 /* loop through the whole list of devices to see if we already have
134 list_for_each(tmp, &config_device_list) {
135 struct config_device *dev = list_entry(tmp, struct config_device, node);
136 if (strcmp(dev->name, new_dev->name) == 0) {
137 /* the same, copy the new info into this structure */
138 copy_var(dev, new_dev, type);
139 copy_var(dev, new_dev, mode);
140 copy_string(dev, new_dev, bus);
141 copy_string(dev, new_dev, sysfs_file);
142 copy_string(dev, new_dev, sysfs_value);
143 copy_string(dev, new_dev, id);
144 copy_string(dev, new_dev, place);
145 copy_string(dev, new_dev, kernel_name);
146 copy_string(dev, new_dev, owner);
147 copy_string(dev, new_dev, group);
152 /* not found, lets create a new structure, and add it to the list */
153 tmp_dev = malloc(sizeof(*tmp_dev));
156 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
157 list_add(&tmp_dev->node, &config_device_list);
162 static void dump_dev_list(void)
164 struct list_head *tmp;
166 list_for_each(tmp, &config_device_list) {
167 struct config_device *dev = list_entry(tmp, struct config_device, node);
172 static int get_value(const char *left, char **orig_string, char **ret_string)
175 char *string = *orig_string;
177 /* eat any whitespace */
178 while (isspace(*string))
181 /* split based on '=' */
182 temp = strsep(&string, "=");
183 if (strcasecmp(temp, left) == 0) {
184 /* got it, now strip off the '"' */
185 while (isspace(*string))
189 temp = strsep(&string, "\"");
191 *orig_string = string;
197 static int get_pair(char **orig_string, char **left, char **right)
200 char *string = *orig_string;
202 /* eat any whitespace */
203 while (isspace(*string))
206 /* split based on '=' */
207 temp = strsep(&string, "=");
210 /* take the right side and strip off the '"' */
211 while (isspace(*string))
215 temp = strsep(&string, "\"");
217 *orig_string = string;
222 static int namedev_init_config(void)
231 struct config_device dev;
233 strcpy(filename, NAMEDEV_CONFIG_ROOT NAMEDEV_CONFIG_FILE);
234 dbg("opening %s to read as permissions config", filename);
235 fd = fopen(filename, "r");
237 dbg("Can't open %s", filename);
241 /* loop through the whole file */
244 temp = fgets(line, sizeof(line), fd);
248 dbg("read %s", temp);
250 /* eat the whitespace at the beginning of the line */
251 while (isspace(*temp))
258 /* see if this is a comment */
259 if (*temp == COMMENT_CHARACTER)
262 memset(&dev, 0x00, sizeof(struct config_device));
265 temp2 = strsep(&temp, ",");
266 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
271 retval = get_value("BUS", &temp, &temp3);
274 strcpy(dev.bus, temp3);
277 temp2 = strsep(&temp, ",");
278 retval = get_pair(&temp, &temp2, &temp3);
281 strcpy(dev.sysfs_file, temp2);
282 strcpy(dev.sysfs_value, temp3);
284 /* NAME="new_name" */
285 temp2 = strsep(&temp, ",");
286 retval = get_value("NAME", &temp, &temp3);
289 strcpy(dev.name, temp3);
291 dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'", dev.name, dev.bus, dev.sysfs_file, dev.sysfs_value);
294 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
299 retval = get_value("BUS", &temp, &temp3);
302 strcpy(dev.bus, temp3);
305 temp2 = strsep(&temp, ",");
306 retval = get_value("id", &temp, &temp3);
309 strcpy(dev.id, temp3);
311 /* NAME="new_name" */
312 temp2 = strsep(&temp, ",");
313 retval = get_value("NAME", &temp, &temp3);
316 strcpy(dev.name, temp3);
318 dbg("NUMBER name = '%s', bus = '%s', id = '%s'", dev.name, dev.bus, dev.id);
321 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
326 retval = get_value("BUS", &temp, &temp3);
329 strcpy(dev.bus, temp3);
332 temp2 = strsep(&temp, ",");
333 retval = get_value("place", &temp, &temp3);
336 strcpy(dev.place, temp3);
338 /* NAME="new_name" */
339 temp2 = strsep(&temp, ",");
340 retval = get_value("NAME", &temp, &temp3);
343 strcpy(dev.name, temp3);
345 dbg("TOPOLOGY name = '%s', bus = '%s', place = '%s'", dev.name, dev.bus, dev.place);
348 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
352 /* KERNEL="kernel_name" */
353 retval = get_value("KERNEL", &temp, &temp3);
356 strcpy(dev.kernel_name, temp3);
358 /* NAME="new_name" */
359 temp2 = strsep(&temp, ",");
360 retval = get_value("NAME", &temp, &temp3);
363 strcpy(dev.name, temp3);
364 dbg("REPLACE name = %s, kernel_name = %s", dev.name, dev.kernel_name);
367 retval = add_dev(&dev);
369 dbg("add_dev returned with error %d", retval);
380 static int namedev_init_permissions(void)
388 struct config_device dev;
390 strcpy(filename, NAMEDEV_CONFIG_ROOT NAMEDEV_CONFIG_PERMISSION_FILE);
391 dbg("opening %s to read as permissions config", filename);
392 fd = fopen(filename, "r");
394 dbg("Can't open %s", filename);
398 /* loop through the whole file */
401 temp = fgets(line, sizeof(line), fd);
405 dbg("read %s", temp);
407 /* eat the whitespace at the beginning of the line */
408 while (isspace(*temp))
415 /* see if this is a comment */
416 if (*temp == COMMENT_CHARACTER)
419 memset(&dev, 0x00, sizeof(dev));
422 temp2 = strsep(&temp, ":");
423 strncpy(dev.name, temp2, sizeof(dev.name));
425 temp2 = strsep(&temp, ":");
426 strncpy(dev.owner, temp2, sizeof(dev.owner));
428 temp2 = strsep(&temp, ":");
429 strncpy(dev.group, temp2, sizeof(dev.owner));
431 dev.mode = strtol(temp, NULL, 8);
433 dbg("name = %s, owner = %s, group = %s, mode = %#o", dev.name, dev.owner, dev.group, dev.mode);
434 retval = add_dev(&dev);
436 dbg("add_dev returned with error %d", retval);
446 static int get_default_mode(struct sysfs_class_device *class_dev)
448 /* just default everyone to rw for the world! */
453 static int get_attr(struct sysfs_class_device *class_dev, struct device_attr *attr)
455 struct list_head *tmp;
458 list_for_each(tmp, &config_device_list) {
459 struct config_device *dev = list_entry(tmp, struct config_device, node);
460 if (strcmp(dev->name, class_dev->name) == 0) {
461 attr->mode = dev->mode;
462 strcpy(attr->owner, dev->owner);
463 strcpy(attr->group, dev->group);
464 /* FIXME put the proper name here!!! */
465 strcpy(attr->name, dev->name);
466 dbg("%s - owner = %s, group = %s, mode = %#o", dev->name, dev->owner, dev->group, dev->mode);
470 attr->mode = get_default_mode(class_dev);
471 attr->owner[0] = 0x00;
472 attr->group[0] = 0x00;
473 strcpy(attr->name, class_dev->name);
478 int namedev_name_device(struct sysfs_class_device *class_dev, struct device_attr *attr)
482 retval = get_attr(class_dev, attr);
484 dbg("get_attr failed");
489 int namedev_init(void)
493 retval = namedev_init_config();
497 retval = namedev_init_permissions();