chiark / gitweb /
ac05fd88e4d4e4cc31ddd2dbc44a40164cffab7c
[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 /* define this to enable parsing debugging */
25 /* #define DEBUG_PARSER */
26
27 #include <stddef.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <ctype.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <sys/wait.h>
36
37 #include "list.h"
38 #include "udev.h"
39 #include "udev_version.h"
40 #include "namedev.h"
41 #include "libsysfs/libsysfs.h"
42
43 #define TYPE_LABEL      "LABEL"
44 #define TYPE_NUMBER     "NUMBER"
45 #define TYPE_TOPOLOGY   "TOPOLOGY"
46 #define TYPE_REPLACE    "REPLACE"
47 #define TYPE_CALLOUT    "CALLOUT"
48
49 static LIST_HEAD(config_device_list);
50
51 static void dump_dev(struct config_device *dev)
52 {
53         switch (dev->type) {
54         case KERNEL_NAME:
55                 dbg_parse("KERNEL name ='%s'"
56                         " owner = '%s', group = '%s', mode = '%#o'",
57                         dev->attr.name, 
58                         dev->attr.owner, dev->attr.group, dev->attr.mode);
59                 break;
60         case LABEL:
61                 dbg_parse("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'"
62                         " owner = '%s', group = '%s', mode = '%#o'",
63                         dev->attr.name, dev->bus, dev->sysfs_file, dev->sysfs_value,
64                         dev->attr.owner, dev->attr.group, dev->attr.mode);
65                 break;
66         case NUMBER:
67                 dbg_parse("NUMBER name = '%s', bus = '%s', id = '%s'"
68                         " owner = '%s', group = '%s', mode = '%#o'",
69                         dev->attr.name, dev->bus, dev->id,
70                         dev->attr.owner, dev->attr.group, dev->attr.mode);
71                 break;
72         case TOPOLOGY:
73                 dbg_parse("TOPOLOGY name = '%s', bus = '%s', place = '%s'"
74                         " owner = '%s', group = '%s', mode = '%#o'",
75                         dev->attr.name, dev->bus, dev->place,
76                         dev->attr.owner, dev->attr.group, dev->attr.mode);
77                 break;
78         case REPLACE:
79                 dbg_parse("REPLACE name = %s, kernel_name = %s"
80                         " owner = '%s', group = '%s', mode = '%#o'",
81                         dev->attr.name, dev->kernel_name,
82                         dev->attr.owner, dev->attr.group, dev->attr.mode);
83                 break;
84         case CALLOUT:
85                 dbg_parse("CALLOUT name = '%s', program ='%s', bus = '%s', id = '%s'"
86                         " owner = '%s', group = '%s', mode = '%#o'",
87                         dev->attr.name, dev->exec_program, dev->bus, dev->id,
88                         dev->attr.owner, dev->attr.group, dev->attr.mode);
89                 break;
90         default:
91                 dbg_parse("Unknown type of device!");
92         }
93 }
94
95 #define copy_var(a, b, var)             \
96         if (b->var)                     \
97                 a->var = b->var;
98
99 #define copy_string(a, b, var)          \
100         if (strlen(b->var))             \
101                 strcpy(a->var, b->var);
102
103 static int add_dev(struct config_device *new_dev)
104 {
105         struct list_head *tmp;
106         struct config_device *tmp_dev;
107
108         /* loop through the whole list of devices to see if we already have
109          * this one... */
110         list_for_each(tmp, &config_device_list) {
111                 struct config_device *dev = list_entry(tmp, struct config_device, node);
112                 if (strcmp(dev->attr.name, new_dev->attr.name) == 0) {
113                         /* the same, copy the new info into this structure */
114                         copy_var(dev, new_dev, type);
115                         copy_var(dev, new_dev, attr.mode);
116                         copy_string(dev, new_dev, bus);
117                         copy_string(dev, new_dev, sysfs_file);
118                         copy_string(dev, new_dev, sysfs_value);
119                         copy_string(dev, new_dev, id);
120                         copy_string(dev, new_dev, place);
121                         copy_string(dev, new_dev, kernel_name);
122                         copy_string(dev, new_dev, attr.owner);
123                         copy_string(dev, new_dev, attr.group);
124                         return 0;
125                 }
126         }
127
128         /* not found, lets create a new structure, and add it to the list */
129         tmp_dev = malloc(sizeof(*tmp_dev));
130         if (!tmp_dev)
131                 return -ENOMEM;
132         memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
133         list_add(&tmp_dev->node, &config_device_list);
134         //dump_dev(tmp_dev);
135         return 0;
136 }
137
138 static void dump_dev_list(void)
139 {
140         struct list_head *tmp;
141
142         list_for_each(tmp, &config_device_list) {
143                 struct config_device *dev = list_entry(tmp, struct config_device, node);
144                 dump_dev(dev);
145         }
146 }
147
148 static int get_value(const char *left, char **orig_string, char **ret_string)
149 {
150         char *temp;
151         char *string = *orig_string;
152
153         /* eat any whitespace */
154         while (isspace(*string))
155                 ++string;
156
157         /* split based on '=' */
158         temp = strsep(&string, "=");
159         if (strcasecmp(temp, left) == 0) {
160                 /* got it, now strip off the '"' */
161                 while (isspace(*string))
162                         ++string;
163                 if (*string == '"')
164                         ++string;
165                 temp = strsep(&string, "\"");
166                 *ret_string = temp;
167                 *orig_string = string;
168                 return 0;
169         }
170         return -ENODEV;
171 }
172         
173 static int get_pair(char **orig_string, char **left, char **right)
174 {
175         char *temp;
176         char *string = *orig_string;
177
178         /* eat any whitespace */
179         while (isspace(*string))
180                 ++string;
181
182         /* split based on '=' */
183         temp = strsep(&string, "=");
184         *left = temp;
185
186         /* take the right side and strip off the '"' */
187         while (isspace(*string))
188                 ++string;
189         if (*string == '"')
190                 ++string;
191         temp = strsep(&string, "\"");
192         *right = temp;
193         *orig_string = string;
194         
195         return 0;
196 }
197
198 static int namedev_init_config(void)
199 {
200         char filename[255];
201         char line[255];
202         char *temp;
203         char *temp2;
204         char *temp3;
205         FILE *fd;
206         int retval = 0;
207         struct config_device dev;
208
209         strcpy(filename, UDEV_CONFIG_DIR NAMEDEV_CONFIG_FILE);
210         dbg("opening %s to read as permissions config", filename);
211         fd = fopen(filename, "r");
212         if (fd == NULL) {
213                 dbg("Can't open %s", filename);
214                 return -ENODEV;
215         }
216
217         /* loop through the whole file */
218         while (1) {
219                 /* get a line */
220                 temp = fgets(line, sizeof(line), fd);
221                 if (temp == NULL)
222                         break;
223
224                 dbg_parse("read %s", temp);
225
226                 /* eat the whitespace at the beginning of the line */
227                 while (isspace(*temp))
228                         ++temp;
229
230                 /* no more line? */
231                 if (*temp == 0x00)
232                         continue;
233
234                 /* see if this is a comment */
235                 if (*temp == COMMENT_CHARACTER)
236                         continue;
237
238                 memset(&dev, 0x00, sizeof(struct config_device));
239
240                 /* parse the line */
241                 temp2 = strsep(&temp, ",");
242                 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
243                         /* label type */
244                         dev.type = LABEL;
245
246                         /* BUS="bus" */
247                         retval = get_value("BUS", &temp, &temp3);
248                         if (retval)
249                                 continue;
250                         strcpy(dev.bus, temp3);
251
252                         /* file="value" */
253                         temp2 = strsep(&temp, ",");
254                         retval = get_pair(&temp, &temp2, &temp3);
255                         if (retval)
256                                 continue;
257                         strcpy(dev.sysfs_file, temp2);
258                         strcpy(dev.sysfs_value, temp3);
259
260                         /* NAME="new_name" */
261                         temp2 = strsep(&temp, ",");
262                         retval = get_value("NAME", &temp, &temp3);
263                         if (retval)
264                                 continue;
265                         strcpy(dev.attr.name, temp3);
266
267                         dbg_parse("LABEL name = '%s', bus = '%s', "
268                                 "sysfs_file = '%s', sysfs_value = '%s'", 
269                                 dev.attr.name, dev.bus, dev.sysfs_file, 
270                                 dev.sysfs_value);
271                 }
272
273                 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
274                         /* number type */
275                         dev.type = NUMBER;
276
277                         /* BUS="bus" */
278                         retval = get_value("BUS", &temp, &temp3);
279                         if (retval)
280                                 continue;
281                         strcpy(dev.bus, temp3);
282
283                         /* ID="id" */
284                         temp2 = strsep(&temp, ",");
285                         retval = get_value("id", &temp, &temp3);
286                         if (retval)
287                                 continue;
288                         strcpy(dev.id, temp3);
289
290                         /* NAME="new_name" */
291                         temp2 = strsep(&temp, ",");
292                         retval = get_value("NAME", &temp, &temp3);
293                         if (retval)
294                                 continue;
295                         strcpy(dev.attr.name, temp3);
296
297                         dbg_parse("NUMBER name = '%s', bus = '%s', id = '%s'",
298                                         dev.attr.name, dev.bus, dev.id);
299                 }
300
301                 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
302                         /* number type */
303                         dev.type = TOPOLOGY;
304
305                         /* BUS="bus" */
306                         retval = get_value("BUS", &temp, &temp3);
307                         if (retval)
308                                 continue;
309                         strcpy(dev.bus, temp3);
310
311                         /* PLACE="place" */
312                         temp2 = strsep(&temp, ",");
313                         retval = get_value("place", &temp, &temp3);
314                         if (retval)
315                                 continue;
316                         strcpy(dev.place, temp3);
317
318                         /* NAME="new_name" */
319                         temp2 = strsep(&temp, ",");
320                         retval = get_value("NAME", &temp, &temp3);
321                         if (retval)
322                                 continue;
323                         strcpy(dev.attr.name, temp3);
324
325                         dbg_parse("TOPOLOGY name = '%s', bus = '%s', place = '%s'",
326                                         dev.attr.name, dev.bus, dev.place);
327                 }
328
329                 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
330                         /* number type */
331                         dev.type = REPLACE;
332
333                         /* KERNEL="kernel_name" */
334                         retval = get_value("KERNEL", &temp, &temp3);
335                         if (retval)
336                                 continue;
337                         strcpy(dev.kernel_name, temp3);
338
339                         /* NAME="new_name" */
340                         temp2 = strsep(&temp, ",");
341                         retval = get_value("NAME", &temp, &temp3);
342                         if (retval)
343                                 continue;
344                         strcpy(dev.attr.name, temp3);
345                         dbg_parse("REPLACE name = %s, kernel_name = %s",
346                                         dev.attr.name, dev.kernel_name);
347                 }
348                 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
349                         /* number type */
350                         dev.type = CALLOUT;
351
352                         /* PROGRAM="executable" */
353                         retval = get_value("PROGRAM", &temp, &temp3);
354                         if (retval)
355                                 continue;
356                         strcpy(dev.exec_program, temp3);
357
358                         /* BUS="bus" */
359                         temp2 = strsep(&temp, ",");
360                         retval = get_value("BUS", &temp, &temp3);
361                         if (retval)
362                                 continue;
363                         strcpy(dev.bus, temp3);
364
365                         /* ID="id" */
366                         temp2 = strsep(&temp, ",");
367                         retval = get_value("ID", &temp, &temp3);
368                         if (retval)
369                                 continue;
370                         strcpy(dev.id, temp3);
371
372                         /* NAME="new_name" */
373                         temp2 = strsep(&temp, ",");
374                         retval = get_value("NAME", &temp, &temp3);
375                         if (retval)
376                                 continue;
377                         strcpy(dev.attr.name, temp3);
378                         dbg_parse("CALLOUT name = %s, program = %s",
379                                         dev.attr.name, dev.exec_program);
380                 }
381
382                 retval = add_dev(&dev);
383                 if (retval) {
384                         dbg("add_dev returned with error %d", retval);
385                         goto exit;
386                 }
387         }
388
389 exit:
390         fclose(fd);
391         return retval;
392 }       
393
394
395 static int namedev_init_permissions(void)
396 {
397         char filename[255];
398         char line[255];
399         char *temp;
400         char *temp2;
401         FILE *fd;
402         int retval = 0;
403         struct config_device dev;
404
405         strcpy(filename, UDEV_CONFIG_DIR NAMEDEV_CONFIG_PERMISSION_FILE);
406         dbg("opening %s to read as permissions config", filename);
407         fd = fopen(filename, "r");
408         if (fd == NULL) {
409                 dbg("Can't open %s", filename);
410                 return -ENODEV;
411         }
412
413         /* loop through the whole file */
414         while (1) {
415                 /* get a line */
416                 temp = fgets(line, sizeof(line), fd);
417                 if (temp == NULL)
418                         break;
419
420                 dbg_parse("read %s", temp);
421
422                 /* eat the whitespace at the beginning of the line */
423                 while (isspace(*temp))
424                         ++temp;
425
426                 /* no more line? */
427                 if (*temp == 0x00)
428                         continue;
429
430                 /* see if this is a comment */
431                 if (*temp == COMMENT_CHARACTER)
432                         continue;
433
434                 memset(&dev, 0x00, sizeof(dev));
435
436                 /* parse the line */
437                 temp2 = strsep(&temp, ":");
438                 strncpy(dev.attr.name, temp2, sizeof(dev.attr.name));
439
440                 temp2 = strsep(&temp, ":");
441                 strncpy(dev.attr.owner, temp2, sizeof(dev.attr.owner));
442
443                 temp2 = strsep(&temp, ":");
444                 strncpy(dev.attr.group, temp2, sizeof(dev.attr.owner));
445
446                 dev.attr.mode = strtol(temp, NULL, 8);
447
448                 dbg_parse("name = %s, owner = %s, group = %s, mode = %#o",
449                                 dev.attr.name, dev.attr.owner, dev.attr.group,
450                                 dev.attr.mode);
451                 retval = add_dev(&dev);
452                 if (retval) {
453                         dbg("add_dev returned with error %d", retval);
454                         goto exit;
455                 }
456         }
457
458 exit:
459         fclose(fd);
460         return retval;
461 }       
462
463 static mode_t get_default_mode(struct sysfs_class_device *class_dev)
464 {
465         /* just default everyone to rw for the world! */
466         return 0666;
467 }
468
469
470 static int exec_callout(struct config_device *dev, char *value, int len)
471 {
472         int retval;
473         int res;
474         int status;
475         int fds[2];
476         pid_t pid;
477         int value_set = 0;
478         char buffer[256];
479
480         dbg("callout to %s\n", dev->exec_program);
481         retval = pipe(fds);
482         if (retval != 0) {
483                 dbg("pipe failed");
484                 return -1;
485         }
486         pid = fork();
487         if (pid == -1) {
488                 dbg("fork failed");
489                 return -1;
490         }
491
492         if (pid == 0) {
493                 /*
494                  * child 
495                  */
496                 close(STDOUT_FILENO);
497                 dup(fds[1]);    /* dup write side of pipe to STDOUT */
498                 retval = execve(dev->exec_program, main_argv, main_envp);
499                 if (retval != 0) {
500                         dbg("child execve failed");
501                         exit(1);
502                 }
503                 return -1; /* avoid compiler warning */
504         } else {
505                 /*
506                  * Parent reads from fds[0].
507                  */
508                 close(fds[1]);
509                 retval = 0;
510                 while (1) {
511                         res = read(fds[0], buffer, sizeof(buffer) - 1);
512                         if (res <= 0)
513                                 break;
514                         buffer[res] = '\0';
515                         if (res > len) {
516                                 dbg("callout len %d too short\n", len);
517                                 retval = -1;
518                         }
519                         if (value_set) {
520                                 dbg("callout value already set");
521                                 retval = -1;
522                         } else {
523                                 value_set = 1;
524                                 strncpy(value, buffer, len);
525                         }
526                 }
527                 close(fds[0]);
528                 res = wait(&status);
529                 if (res < 0) {
530                         dbg("wait failed result %d", res);
531                         retval = -1;
532                 }
533
534                 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
535                         dbg("callout program status 0x%x", status);
536                         retval = -1;
537                 }
538         }
539         return retval;
540 }
541
542 static int get_attr(struct sysfs_class_device *class_dev, struct device_attr *attr)
543 {
544         struct list_head *tmp;
545         int retval = 0;
546         int found;
547
548         attr->mode = 0;
549         if (class_dev->sysdevice) {
550                 dbg_parse("class_dev->sysdevice->directory->path = '%s'", class_dev->sysdevice->directory->path);
551                 dbg_parse("class_dev->sysdevice->bus_id = '%s'", class_dev->sysdevice->bus_id);
552         } else {
553                 dbg_parse("class_dev->name = '%s'", class_dev->name);
554         }
555         list_for_each(tmp, &config_device_list) {
556                 struct config_device *dev = list_entry(tmp, struct config_device, node);
557                 switch (dev->type) {
558                 case LABEL:
559                         {
560                         char *temp;
561
562                         dbg_parse("LABEL: match file '%s' with value '%s'",
563                                         dev->sysfs_file, dev->sysfs_value);
564                         /* try to find the attribute in the class device directory */
565                         temp = sysfs_get_value_from_attributes(class_dev->directory->attributes, dev->sysfs_file);
566                         if (temp)
567                                 goto label_found;
568
569                         /* look in the class device device directory if present */
570                         if (class_dev->sysdevice) {
571                                 temp = sysfs_get_value_from_attributes(class_dev->sysdevice->directory->attributes, dev->sysfs_file);
572                                 if (temp)
573                                         goto label_found;
574                         }
575
576                         /* bah, let's go backwards up a level to see if the device is there,
577                          * as block partitions don't point to the physical device.  Need to fix that
578                          * up in the kernel...
579                          */
580                         if (strstr(class_dev->directory->path, "block")) {
581                                 dbg_parse("looking at block device...");
582                                 if (isdigit(class_dev->directory->path[strlen(class_dev->directory->path)-1])) {
583                                         char path[SYSFS_PATH_MAX];
584                                         struct sysfs_class_device *class_dev_parent;
585
586                                         dbg_parse("really is a partition...");
587                                         strcpy(path, class_dev->directory->path);
588                                         temp = strrchr(path, '/');
589                                         *temp = 0x00;
590                                         dbg_parse("looking for a class device at '%s'", path);
591                                         class_dev_parent = sysfs_open_class_device(path);
592                                         if (class_dev_parent == NULL) {
593                                                 dbg("sysfs_open_class_device at '%s' failed", path);
594                                                 continue;
595                                         }
596                                         dbg_parse("class_dev_parent->name = %s", class_dev_parent->name);
597
598                                         /* try to find the attribute in the class device directory */
599                                         temp = sysfs_get_value_from_attributes(class_dev_parent->directory->attributes, dev->sysfs_file);
600                                         if (temp) {
601                                                 //sysfs_close_class_device(class_dev_parent);
602                                                 goto label_found;
603                                         }
604
605                                         /* look in the class device device directory if present */
606                                         if (class_dev_parent->sysdevice) {
607                                                 temp = sysfs_get_value_from_attributes(class_dev_parent->sysdevice->directory->attributes, dev->sysfs_file);
608                                                 if (temp) {
609                                                         // sysfs_close_class_device(class_dev_parent);
610                                                         goto label_found;
611                                                 }
612                                         }
613                                         
614                                 }
615                         }
616                         continue;
617
618 label_found:
619                         temp[strlen(temp)-1] = 0x00;
620                         dbg_parse("file '%s' found with value '%s' compare with '%s'", dev->sysfs_file, temp, dev->sysfs_value);
621                         if (strcmp(dev->sysfs_value, temp) != 0)
622                                 continue;
623
624                         strcpy(attr->name, dev->attr.name);
625                         if (isdigit(class_dev->directory->path[strlen(class_dev->directory->path)-1])) {
626                                 temp[0] = class_dev->directory->path[strlen(class_dev->directory->path)-1];
627                                 temp[1] = 0x00;
628                                 strcat(attr->name, temp);
629                         }
630                         if (dev->attr.mode != 0) {
631                                 attr->mode = dev->attr.mode;
632                                 strcpy(attr->owner, dev->attr.owner);
633                                 strcpy(attr->group, dev->attr.group);
634                         }
635                         dbg_parse("file '%s' with value '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
636                                 dev->sysfs_file, dev->sysfs_value, attr->name, 
637                                 dev->attr.owner, dev->attr.group, dev->attr.mode);
638                         goto done;
639                         break;
640                         }
641                 case NUMBER:
642                         {
643                         char path[SYSFS_PATH_MAX];
644                         char *temp;
645
646                         found = 0;
647                         if (!class_dev->sysdevice)
648                                 continue;
649                         strcpy(path, class_dev->sysdevice->directory->path);
650                         temp = strrchr(path, '/');
651                         dbg_parse("NUMBER path = '%s'", path);
652                         dbg_parse("NUMBER temp = '%s' id = '%s'", temp, dev->id);
653                         if (strstr(temp, dev->id) != NULL) {
654                                 found = 1;
655                         } else {
656                                 *temp = 0x00;
657                                 temp = strrchr(path, '/');
658                                 dbg_parse("NUMBERY temp = '%s' id = '%s'", temp, dev->id);
659                                 if (strstr(temp, dev->id) != NULL)
660                                         found = 1;
661                         }
662                         if (!found)
663                                 continue;
664
665                         strcpy(attr->name, dev->attr.name);
666                         if (dev->attr.mode != 0) {
667                                 attr->mode = dev->attr.mode;
668                                 strcpy(attr->owner, dev->attr.owner);
669                                 strcpy(attr->group, dev->attr.group);
670                         }
671                         dbg_parse("device id '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
672                                 dev->id, attr->name, 
673                                 dev->attr.owner, dev->attr.group, dev->attr.mode);
674                         goto done;
675                         break;
676                         }
677                 case TOPOLOGY:
678                         {
679                         char path[SYSFS_PATH_MAX];
680                         char *temp;
681
682                         if (!class_dev->sysdevice)
683                                 continue;
684                         found = 0;      
685                         strcpy(path, class_dev->sysdevice->directory->path);
686                         temp = strrchr(path, '/');
687                         dbg_parse("TOPOLOGY path = '%s'", path);
688                         dbg_parse("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
689                         if (strstr(temp, dev->place) != NULL) {
690                                 found = 1;
691                         } else {
692                                 *temp = 0x00;
693                                 temp = strrchr(path, '/');
694                                 dbg_parse("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
695                                 if (strstr(temp, dev->place) != NULL)
696                                         found = 1;
697                         }
698                         if (!found)
699                                 continue;
700
701                         strcpy(attr->name, dev->attr.name);
702                         if (dev->attr.mode != 0) {
703                                 attr->mode = dev->attr.mode;
704                                 strcpy(attr->owner, dev->attr.owner);
705                                 strcpy(attr->group, dev->attr.group);
706                         }
707                         dbg_parse("device at '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
708                                 dev->place, attr->name, 
709                                 dev->attr.owner, dev->attr.group, dev->attr.mode);
710                         goto done;
711                         break;
712                         }
713                 case CALLOUT:
714                         {
715                         char value[ID_SIZE];
716
717                         if (exec_callout(dev, value, sizeof(value)))
718                                 continue;
719                         if (strncmp(value, dev->id, sizeof(value)) != 0)
720                                 continue;
721                         strcpy(attr->name, dev->attr.name);
722                         if (dev->attr.mode != 0) {
723                                 attr->mode = dev->attr.mode;
724                                 strcpy(attr->owner, dev->attr.owner);
725                                 strcpy(attr->group, dev->attr.group);
726                         }
727                         dbg_parse("device callout '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
728                                 dev->id, attr->name, 
729                                 dev->attr.owner, dev->attr.group, dev->attr.mode);
730                         goto done;
731                         break;
732                         }
733                 case REPLACE:
734                         if (strcmp(dev->kernel_name, class_dev->name) != 0)
735                                 continue;
736                         strcpy(attr->name, dev->attr.name);
737                         if (dev->attr.mode != 0) {
738                                 attr->mode = dev->attr.mode;
739                                 strcpy(attr->owner, dev->attr.owner);
740                                 strcpy(attr->group, dev->attr.group);
741                         }
742                         dbg_parse("'%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
743                                 dev->kernel_name, attr->name, 
744                                 dev->attr.owner, dev->attr.group, dev->attr.mode);
745                         goto done;
746                         break;
747                 case KERNEL_NAME:
748                         break;
749                 default:
750                         dbg_parse("Unknown type of device '%d'", dev->type);
751                         break;
752                 }       
753         }
754         strcpy(attr->name, class_dev->name);
755
756 done:
757         /* mode was never set above */
758         if (!attr->mode) {
759                 attr->mode = get_default_mode(class_dev);
760                 attr->owner[0] = 0x00;
761                 attr->group[0] = 0x00;
762         }
763         return retval;
764 }
765
766 int namedev_name_device(struct sysfs_class_device *class_dev, struct device_attr *attr)
767 {
768         int retval;
769
770         retval = get_attr(class_dev, attr);
771         if (retval)
772                 dbg("get_attr failed");
773
774         return retval;
775 }
776
777 int namedev_init(void)
778 {
779         int retval;
780         
781         retval = namedev_init_config();
782         if (retval)
783                 return retval;
784
785         retval = namedev_init_permissions();
786         if (retval)
787                 return retval;
788
789         dump_dev_list();
790         return retval;
791 }
792
793