chiark / gitweb /
[PATCH] got REPLACE to work properly.
[elogind.git] / namedev.c
1 /*
2  * namedev.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7  *
8  *
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.
12  * 
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.
17  * 
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.
21  *
22  */
23
24 #include <stddef.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30 #include <unistd.h>
31 #include <errno.h>
32
33 #include "list.h"
34 #include "udev.h"
35 #include "udev_version.h"
36 #include "namedev.h"
37 #include "libsysfs/libsysfs.h"
38
39 #define TYPE_LABEL      "LABEL"
40 #define TYPE_NUMBER     "NUMBER"
41 #define TYPE_TOPOLOGY   "TOPOLOGY"
42 #define TYPE_REPLACE    "REPLACE"
43
44 static LIST_HEAD(config_device_list);
45
46 static void dump_dev(struct config_device *dev)
47 {
48         switch (dev->type) {
49         case KERNEL_NAME:
50                 dbg("KERNEL name ='%s'"
51                         " owner = '%s', group = '%s', mode = '%#o'",
52                         dev->attr.name, 
53                         dev->attr.owner, dev->attr.group, dev->attr.mode);
54                 break;
55         case LABEL:
56                 dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'"
57                         " owner = '%s', group = '%s', mode = '%#o'",
58                         dev->attr.name, dev->bus, dev->sysfs_file, dev->sysfs_value,
59                         dev->attr.owner, dev->attr.group, dev->attr.mode);
60                 break;
61         case NUMBER:
62                 dbg("NUMBER name = '%s', bus = '%s', id = '%s'"
63                         " owner = '%s', group = '%s', mode = '%#o'",
64                         dev->attr.name, dev->bus, dev->id,
65                         dev->attr.owner, dev->attr.group, dev->attr.mode);
66                 break;
67         case TOPOLOGY:
68                 dbg("TOPOLOGY name = '%s', bus = '%s', place = '%s'"
69                         " owner = '%s', group = '%s', mode = '%#o'",
70                         dev->attr.name, dev->bus, dev->place,
71                         dev->attr.owner, dev->attr.group, dev->attr.mode);
72                 break;
73         case REPLACE:
74                 dbg("REPLACE name = %s, kernel_name = %s"
75                         " owner = '%s', group = '%s', mode = '%#o'",
76                         dev->attr.name, dev->kernel_name,
77                         dev->attr.owner, dev->attr.group, dev->attr.mode);
78                 break;
79         default:
80                 dbg("Unknown type of device!");
81         }
82 }
83
84 #define copy_var(a, b, var)             \
85         if (b->var)                     \
86                 a->var = b->var;
87
88 #define copy_string(a, b, var)          \
89         if (strlen(b->var))             \
90                 strcpy(a->var, b->var);
91
92 static int add_dev(struct config_device *new_dev)
93 {
94         struct list_head *tmp;
95         struct config_device *tmp_dev;
96
97         /* loop through the whole list of devices to see if we already have
98          * this one... */
99         list_for_each(tmp, &config_device_list) {
100                 struct config_device *dev = list_entry(tmp, struct config_device, node);
101                 if (strcmp(dev->attr.name, new_dev->attr.name) == 0) {
102                         /* the same, copy the new info into this structure */
103                         copy_var(dev, new_dev, type);
104                         copy_var(dev, new_dev, attr.mode);
105                         copy_string(dev, new_dev, bus);
106                         copy_string(dev, new_dev, sysfs_file);
107                         copy_string(dev, new_dev, sysfs_value);
108                         copy_string(dev, new_dev, id);
109                         copy_string(dev, new_dev, place);
110                         copy_string(dev, new_dev, kernel_name);
111                         copy_string(dev, new_dev, attr.owner);
112                         copy_string(dev, new_dev, attr.group);
113                         return 0;
114                 }
115         }
116
117         /* not found, lets create a new structure, and add it to the list */
118         tmp_dev = malloc(sizeof(*tmp_dev));
119         if (!tmp_dev)
120                 return -ENOMEM;
121         memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
122         list_add(&tmp_dev->node, &config_device_list);
123         //dump_dev(tmp_dev);
124         return 0;
125 }
126
127 static void dump_dev_list(void)
128 {
129         struct list_head *tmp;
130
131         list_for_each(tmp, &config_device_list) {
132                 struct config_device *dev = list_entry(tmp, struct config_device, node);
133                 dump_dev(dev);
134         }
135 }
136
137 static int get_value(const char *left, char **orig_string, char **ret_string)
138 {
139         char *temp;
140         char *string = *orig_string;
141
142         /* eat any whitespace */
143         while (isspace(*string))
144                 ++string;
145
146         /* split based on '=' */
147         temp = strsep(&string, "=");
148         if (strcasecmp(temp, left) == 0) {
149                 /* got it, now strip off the '"' */
150                 while (isspace(*string))
151                         ++string;
152                 if (*string == '"')
153                         ++string;
154                 temp = strsep(&string, "\"");
155                 *ret_string = temp;
156                 *orig_string = string;
157                 return 0;
158         }
159         return -ENODEV;
160 }
161         
162 static int get_pair(char **orig_string, char **left, char **right)
163 {
164         char *temp;
165         char *string = *orig_string;
166
167         /* eat any whitespace */
168         while (isspace(*string))
169                 ++string;
170
171         /* split based on '=' */
172         temp = strsep(&string, "=");
173         *left = temp;
174
175         /* take the right side and strip off the '"' */
176         while (isspace(*string))
177                 ++string;
178         if (*string == '"')
179                 ++string;
180         temp = strsep(&string, "\"");
181         *right = temp;
182         *orig_string = string;
183         
184         return 0;
185 }
186
187 static int namedev_init_config(void)
188 {
189         char filename[255];
190         char line[255];
191         char *temp;
192         char *temp2;
193         char *temp3;
194         FILE *fd;
195         int retval = 0;
196         struct config_device dev;
197
198         strcpy(filename, NAMEDEV_CONFIG_ROOT NAMEDEV_CONFIG_FILE);
199         dbg("opening %s to read as permissions config", filename);
200         fd = fopen(filename, "r");
201         if (fd == NULL) {
202                 dbg("Can't open %s", filename);
203                 return -ENODEV;
204         }
205
206         /* loop through the whole file */
207         while (1) {
208                 /* get a line */
209                 temp = fgets(line, sizeof(line), fd);
210                 if (temp == NULL)
211                         break;
212
213                 dbg("read %s", temp);
214
215                 /* eat the whitespace at the beginning of the line */
216                 while (isspace(*temp))
217                         ++temp;
218
219                 /* no more line? */
220                 if (*temp == 0x00)
221                         continue;
222
223                 /* see if this is a comment */
224                 if (*temp == COMMENT_CHARACTER)
225                         continue;
226
227                 memset(&dev, 0x00, sizeof(struct config_device));
228
229                 /* parse the line */
230                 temp2 = strsep(&temp, ",");
231                 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
232                         /* label type */
233                         dev.type = LABEL;
234
235                         /* BUS="bus" */
236                         retval = get_value("BUS", &temp, &temp3);
237                         if (retval)
238                                 continue;
239                         strcpy(dev.bus, temp3);
240
241                         /* file="value" */
242                         temp2 = strsep(&temp, ",");
243                         retval = get_pair(&temp, &temp2, &temp3);
244                         if (retval)
245                                 continue;
246                         strcpy(dev.sysfs_file, temp2);
247                         strcpy(dev.sysfs_value, temp3);
248
249                         /* NAME="new_name" */
250                         temp2 = strsep(&temp, ",");
251                         retval = get_value("NAME", &temp, &temp3);
252                         if (retval)
253                                 continue;
254                         strcpy(dev.attr.name, temp3);
255
256                         dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'", dev.attr.name, dev.bus, dev.sysfs_file, dev.sysfs_value);
257                 }
258
259                 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
260                         /* number type */
261                         dev.type = NUMBER;
262
263                         /* BUS="bus" */
264                         retval = get_value("BUS", &temp, &temp3);
265                         if (retval)
266                                 continue;
267                         strcpy(dev.bus, temp3);
268
269                         /* ID="id" */
270                         temp2 = strsep(&temp, ",");
271                         retval = get_value("id", &temp, &temp3);
272                         if (retval)
273                                 continue;
274                         strcpy(dev.id, temp3);
275
276                         /* NAME="new_name" */
277                         temp2 = strsep(&temp, ",");
278                         retval = get_value("NAME", &temp, &temp3);
279                         if (retval)
280                                 continue;
281                         strcpy(dev.attr.name, temp3);
282
283                         dbg("NUMBER name = '%s', bus = '%s', id = '%s'", dev.attr.name, dev.bus, dev.id);
284                 }
285
286                 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
287                         /* number type */
288                         dev.type = TOPOLOGY;
289
290                         /* BUS="bus" */
291                         retval = get_value("BUS", &temp, &temp3);
292                         if (retval)
293                                 continue;
294                         strcpy(dev.bus, temp3);
295
296                         /* PLACE="place" */
297                         temp2 = strsep(&temp, ",");
298                         retval = get_value("place", &temp, &temp3);
299                         if (retval)
300                                 continue;
301                         strcpy(dev.place, temp3);
302
303                         /* NAME="new_name" */
304                         temp2 = strsep(&temp, ",");
305                         retval = get_value("NAME", &temp, &temp3);
306                         if (retval)
307                                 continue;
308                         strcpy(dev.attr.name, temp3);
309
310                         dbg("TOPOLOGY name = '%s', bus = '%s', place = '%s'", dev.attr.name, dev.bus, dev.place);
311                 }
312
313                 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
314                         /* number type */
315                         dev.type = REPLACE;
316
317                         /* KERNEL="kernel_name" */
318                         retval = get_value("KERNEL", &temp, &temp3);
319                         if (retval)
320                                 continue;
321                         strcpy(dev.kernel_name, temp3);
322
323                         /* NAME="new_name" */
324                         temp2 = strsep(&temp, ",");
325                         retval = get_value("NAME", &temp, &temp3);
326                         if (retval)
327                                 continue;
328                         strcpy(dev.attr.name, temp3);
329                         dbg("REPLACE name = %s, kernel_name = %s", dev.attr.name, dev.kernel_name);
330                 }
331
332                 retval = add_dev(&dev);
333                 if (retval) {
334                         dbg("add_dev returned with error %d", retval);
335                         goto exit;
336                 }
337         }
338
339 exit:
340         fclose(fd);
341         return retval;
342 }       
343
344
345 static int namedev_init_permissions(void)
346 {
347         char filename[255];
348         char line[255];
349         char *temp;
350         char *temp2;
351         FILE *fd;
352         int retval = 0;
353         struct config_device dev;
354
355         strcpy(filename, NAMEDEV_CONFIG_ROOT NAMEDEV_CONFIG_PERMISSION_FILE);
356         dbg("opening %s to read as permissions config", filename);
357         fd = fopen(filename, "r");
358         if (fd == NULL) {
359                 dbg("Can't open %s", filename);
360                 return -ENODEV;
361         }
362
363         /* loop through the whole file */
364         while (1) {
365                 /* get a line */
366                 temp = fgets(line, sizeof(line), fd);
367                 if (temp == NULL)
368                         break;
369
370                 dbg("read %s", temp);
371
372                 /* eat the whitespace at the beginning of the line */
373                 while (isspace(*temp))
374                         ++temp;
375
376                 /* no more line? */
377                 if (*temp == 0x00)
378                         continue;
379
380                 /* see if this is a comment */
381                 if (*temp == COMMENT_CHARACTER)
382                         continue;
383
384                 memset(&dev, 0x00, sizeof(dev));
385
386                 /* parse the line */
387                 temp2 = strsep(&temp, ":");
388                 strncpy(dev.attr.name, temp2, sizeof(dev.attr.name));
389
390                 temp2 = strsep(&temp, ":");
391                 strncpy(dev.attr.owner, temp2, sizeof(dev.attr.owner));
392
393                 temp2 = strsep(&temp, ":");
394                 strncpy(dev.attr.group, temp2, sizeof(dev.attr.owner));
395
396                 dev.attr.mode = strtol(temp, NULL, 8);
397
398                 dbg("name = %s, owner = %s, group = %s, mode = %#o", dev.attr.name, dev.attr.owner, dev.attr.group, dev.attr.mode);
399                 retval = add_dev(&dev);
400                 if (retval) {
401                         dbg("add_dev returned with error %d", retval);
402                         goto exit;
403                 }
404         }
405
406 exit:
407         fclose(fd);
408         return retval;
409 }       
410
411 static int get_default_mode(struct sysfs_class_device *class_dev)
412 {
413         /* just default everyone to rw for the world! */
414         return 0666;
415 }
416
417
418 static int get_attr(struct sysfs_class_device *class_dev, struct device_attr *attr)
419 {
420         struct list_head *tmp;
421         int retval = 0;
422         int found;
423
424         list_for_each(tmp, &config_device_list) {
425                 struct config_device *dev = list_entry(tmp, struct config_device, node);
426                 switch (dev->type) {
427                 case LABEL:
428                         dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'"
429                                 " owner = '%s', group = '%s', mode = '%#o'",
430                                 dev->attr.name, dev->bus, dev->sysfs_file, dev->sysfs_value,
431                                 dev->attr.owner, dev->attr.group, dev->attr.mode);
432                         break;
433                 case NUMBER:
434                         dbg("NUMBER name = '%s', bus = '%s', id = '%s'"
435                                 " owner = '%s', group = '%s', mode = '%#o'",
436                                 dev->attr.name, dev->bus, dev->id,
437                                 dev->attr.owner, dev->attr.group, dev->attr.mode);
438                         break;
439                 case TOPOLOGY:
440                         dbg("TOPOLOGY name = '%s', bus = '%s', place = '%s'"
441                                 " owner = '%s', group = '%s', mode = '%#o'",
442                                 dev->attr.name, dev->bus, dev->place,
443                                 dev->attr.owner, dev->attr.group, dev->attr.mode);
444                         break;
445                 case REPLACE:
446                         if (strcmp(dev->kernel_name, class_dev->name) != 0)
447                                 continue;
448                         strcpy(attr->name, dev->attr.name);
449                         attr->mode = dev->attr.mode;
450                         strcpy(attr->owner, dev->attr.owner);
451                         strcpy(attr->group, dev->attr.group);
452                         dbg("'%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
453                                 dev->kernel_name, attr->name, 
454                                 dev->attr.owner, dev->attr.group, dev->attr.mode);
455                         return retval;
456                         break;
457                 default:
458                         dbg("Unknown type of device!");
459                         break;
460                 }       
461         }
462         attr->mode = get_default_mode(class_dev);
463         attr->owner[0] = 0x00;
464         attr->group[0] = 0x00;
465         strcpy(attr->name, class_dev->name);
466 exit:
467         return retval;
468 }
469
470 int namedev_name_device(struct sysfs_class_device *class_dev, struct device_attr *attr)
471 {
472         int retval;
473
474         retval = get_attr(class_dev, attr);
475         if (retval)
476                 dbg("get_attr failed");
477
478         return retval;
479 }
480
481 int namedev_init(void)
482 {
483         int retval;
484         
485         retval = namedev_init_config();
486         if (retval)
487                 return retval;
488
489         retval = namedev_init_permissions();
490         if (retval)
491                 return retval;
492
493         dump_dev_list();
494         return retval;
495 }
496
497