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"
38 #define TYPE_LABEL "LABEL"
39 #define TYPE_NUMBER "NUMBER"
40 #define TYPE_TOPOLOGY "TOPOLOGY"
41 #define TYPE_REPLACE "REPLACE"
44 KERNEL_NAME = 0, /* must be 0 to let memset() default to this value */
53 #define VALUE_SIZE 100
61 struct config_device {
62 struct list_head node;
64 enum config_type type;
67 char sysfs_file[FILE_SIZE];
68 char sysfs_value[VALUE_SIZE];
70 char place[PLACE_SIZE];
71 char kernel_name[NAME_SIZE];
73 /* what to set the device to */
76 char owner[OWNER_SIZE];
77 char group[GROUP_SIZE];
81 static LIST_HEAD(config_device_list);
83 static void dump_dev(struct config_device *dev)
87 dbg("KERNEL name ='%s'"
88 " owner = '%s', group = '%s', mode = '%d'",
90 dev->owner, dev->group, dev->mode);
93 dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'"
94 " owner = '%s', group = '%s', mode = '%d'",
95 dev->name, dev->bus, dev->sysfs_file, dev->sysfs_value,
96 dev->owner, dev->group, dev->mode);
99 dbg("NUMBER name = '%s', bus = '%s', id = '%s'"
100 " owner = '%s', group = '%s', mode = '%d'",
101 dev->name, dev->bus, dev->id,
102 dev->owner, dev->group, dev->mode);
105 dbg("TOPOLOGY name = '%s', bus = '%s', place = '%s'"
106 " owner = '%s', group = '%s', mode = '%d'",
107 dev->name, dev->bus, dev->place,
108 dev->owner, dev->group, dev->mode);
111 dbg("REPLACE name = %s, kernel_name = %s"
112 " owner = '%s', group = '%s', mode = '%d'",
113 dev->name, dev->kernel_name,
114 dev->owner, dev->group, dev->mode);
117 dbg("Unknown type of device!");
121 #define copy_var(a, b, var) \
125 #define copy_string(a, b, var) \
126 if (strlen(b->var)) \
127 strcpy(a->var, b->var);
129 static int add_dev(struct config_device *new_dev)
131 struct list_head *tmp;
132 struct config_device *tmp_dev;
134 /* loop through the whole list of devices to see if we already have
136 list_for_each(tmp, &config_device_list) {
137 struct config_device *dev = list_entry(tmp, struct config_device, node);
138 if (strcmp(dev->name, new_dev->name) == 0) {
139 /* the same, copy the new info into this structure */
140 copy_var(dev, new_dev, type);
141 copy_var(dev, new_dev, mode);
142 copy_string(dev, new_dev, bus);
143 copy_string(dev, new_dev, sysfs_file);
144 copy_string(dev, new_dev, sysfs_value);
145 copy_string(dev, new_dev, id);
146 copy_string(dev, new_dev, place);
147 copy_string(dev, new_dev, kernel_name);
148 copy_string(dev, new_dev, owner);
149 copy_string(dev, new_dev, group);
154 /* not found, lets create a new structure, and add it to the list */
155 tmp_dev = malloc(sizeof(*tmp_dev));
158 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
159 list_add(&tmp_dev->node, &config_device_list);
164 static void dump_dev_list(void)
166 struct list_head *tmp;
168 list_for_each(tmp, &config_device_list) {
169 struct config_device *dev = list_entry(tmp, struct config_device, node);
174 static int get_value(const char *left, char **orig_string, char **ret_string)
177 char *string = *orig_string;
179 /* eat any whitespace */
180 while (isspace(*string))
183 /* split based on '=' */
184 temp = strsep(&string, "=");
185 if (strcasecmp(temp, left) == 0) {
186 /* got it, now strip off the '"' */
187 while (isspace(*string))
191 temp = strsep(&string, "\"");
193 *orig_string = string;
199 static int get_pair(char **orig_string, char **left, char **right)
202 char *string = *orig_string;
204 /* eat any whitespace */
205 while (isspace(*string))
208 /* split based on '=' */
209 temp = strsep(&string, "=");
212 /* take the right side and strip off the '"' */
213 while (isspace(*string))
217 temp = strsep(&string, "\"");
219 *orig_string = string;
224 static int namedev_init_config(void)
233 struct config_device dev;
235 strcpy(filename, NAMEDEV_CONFIG_ROOT NAMEDEV_CONFIG_FILE);
236 dbg("opening %s to read as permissions config", filename);
237 fd = fopen(filename, "r");
239 dbg("Can't open %s", filename);
243 /* loop through the whole file */
246 temp = fgets(line, sizeof(line), fd);
250 dbg("read %s", temp);
252 /* eat the whitespace at the beginning of the line */
253 while (isspace(*temp))
260 /* see if this is a comment */
261 if (*temp == COMMENT_CHARACTER)
264 memset(&dev, 0x00, sizeof(struct config_device));
267 temp2 = strsep(&temp, ",");
268 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
273 retval = get_value("BUS", &temp, &temp3);
276 strcpy(dev.bus, temp3);
279 temp2 = strsep(&temp, ",");
280 retval = get_pair(&temp, &temp2, &temp3);
283 strcpy(dev.sysfs_file, temp2);
284 strcpy(dev.sysfs_value, temp3);
286 /* NAME="new_name" */
287 temp2 = strsep(&temp, ",");
288 retval = get_value("NAME", &temp, &temp3);
291 strcpy(dev.name, temp3);
293 dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'", dev.name, dev.bus, dev.sysfs_file, dev.sysfs_value);
296 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
301 retval = get_value("BUS", &temp, &temp3);
304 strcpy(dev.bus, temp3);
307 temp2 = strsep(&temp, ",");
308 retval = get_value("id", &temp, &temp3);
311 strcpy(dev.id, temp3);
313 /* NAME="new_name" */
314 temp2 = strsep(&temp, ",");
315 retval = get_value("NAME", &temp, &temp3);
318 strcpy(dev.name, temp3);
320 dbg("NUMBER name = '%s', bus = '%s', id = '%s'", dev.name, dev.bus, dev.id);
323 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
328 retval = get_value("BUS", &temp, &temp3);
331 strcpy(dev.bus, temp3);
334 temp2 = strsep(&temp, ",");
335 retval = get_value("place", &temp, &temp3);
338 strcpy(dev.place, temp3);
340 /* NAME="new_name" */
341 temp2 = strsep(&temp, ",");
342 retval = get_value("NAME", &temp, &temp3);
345 strcpy(dev.name, temp3);
347 dbg("TOPOLOGY name = '%s', bus = '%s', place = '%s'", dev.name, dev.bus, dev.place);
350 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
354 /* KERNEL="kernel_name" */
355 retval = get_value("KERNEL", &temp, &temp3);
358 strcpy(dev.kernel_name, temp3);
360 /* NAME="new_name" */
361 temp2 = strsep(&temp, ",");
362 retval = get_value("NAME", &temp, &temp3);
365 strcpy(dev.name, temp3);
366 dbg("REPLACE name = %s, kernel_name = %s", dev.name, dev.kernel_name);
369 retval = add_dev(&dev);
371 dbg("add_dev returned with error %d", retval);
382 static int namedev_init_permissions(void)
390 struct config_device dev;
392 strcpy(filename, NAMEDEV_CONFIG_ROOT NAMEDEV_CONFIG_PERMISSION_FILE);
393 dbg("opening %s to read as permissions config", filename);
394 fd = fopen(filename, "r");
396 dbg("Can't open %s", filename);
400 /* loop through the whole file */
403 temp = fgets(line, sizeof(line), fd);
407 dbg("read %s", temp);
409 /* eat the whitespace at the beginning of the line */
410 while (isspace(*temp))
417 /* see if this is a comment */
418 if (*temp == COMMENT_CHARACTER)
421 memset(&dev, 0x00, sizeof(dev));
424 temp2 = strsep(&temp, ":");
425 strncpy(dev.name, temp2, sizeof(dev.name));
427 temp2 = strsep(&temp, ":");
428 strncpy(dev.owner, temp2, sizeof(dev.owner));
430 temp2 = strsep(&temp, ":");
431 strncpy(dev.group, temp2, sizeof(dev.owner));
433 dev.mode = strtol(temp, NULL, 8);
435 dbg("name = %s, owner = %s, group = %s, mode = %x", dev.name, dev.owner, dev.group, dev.mode);
436 retval = add_dev(&dev);
438 dbg("add_dev returned with error %d", retval);
450 int namedev_init(void)
454 retval = namedev_init_config();
458 retval = namedev_init_permissions();