chiark / gitweb /
391da7efcacc59f053a1e5f737efefe2d03e74ad
[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 #include "klibc_fixups.h"
43
44 #define TYPE_LABEL      "LABEL"
45 #define TYPE_NUMBER     "NUMBER"
46 #define TYPE_TOPOLOGY   "TOPOLOGY"
47 #define TYPE_REPLACE    "REPLACE"
48 #define TYPE_CALLOUT    "CALLOUT"
49 #define CALLOUT_MAXARG  8
50
51 static LIST_HEAD(config_device_list);
52
53 /* s2 may end with '*' to match everything */
54 static int strncmp_wildcard(char *s1, char *s2, int max)
55 {
56         int len = strlen(s2);
57         if (len > max)
58                 len = max;
59         if (s2[len-1] == '*')
60                 len--;
61         else
62                 len = max;
63         return strncmp(s1, s2, len);
64 }
65
66 static void dump_dev(struct config_device *dev)
67 {
68         switch (dev->type) {
69         case KERNEL_NAME:
70                 dbg_parse("KERNEL name='%s' ,"
71                           "owner='%s', group='%s', mode=%#o",
72                           dev->name, dev->owner, dev->group, dev->mode);
73                 break;
74         case LABEL:
75                 dbg_parse("LABEL name='%s', bus='%s', sysfs_file='%s', sysfs_value='%s', "
76                           "owner='%s', group='%s', mode=%#o",
77                           dev->name, dev->bus, dev->sysfs_file, dev->sysfs_value,
78                           dev->owner, dev->group, dev->mode);
79                 break;
80         case NUMBER:
81                 dbg_parse("NUMBER name='%s', bus='%s', id='%s', "
82                           "owner='%s', group='%s', mode=%#o",
83                           dev->name, dev->bus, dev->id,
84                           dev->owner, dev->group, dev->mode);
85                 break;
86         case TOPOLOGY:
87                 dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s', "
88                           "owner='%s', group='%s', mode=%#o",
89                           dev->name, dev->bus, dev->place,
90                           dev->owner, dev->group, dev->mode);
91                 break;
92         case REPLACE:
93                 dbg_parse("REPLACE name=%s, kernel_name=%s, "
94                           "owner='%s', group='%s', mode=%#o",
95                           dev->name, dev->kernel_name,
96                           dev->owner, dev->group, dev->mode);
97                 break;
98         case CALLOUT:
99                 dbg_parse("CALLOUT name='%s', bus='%s', program='%s', id='%s', "
100                           "owner='%s', group='%s', mode=%#o",
101                           dev->name, dev->bus, dev->exec_program, dev->id,
102                           dev->owner, dev->group, dev->mode);
103                 break;
104         default:
105                 dbg_parse("unknown type of method");
106         }
107 }
108
109 #define copy_var(a, b, var)             \
110         if (b->var)                     \
111                 a->var = b->var;
112
113 #define copy_string(a, b, var)          \
114         if (strlen(b->var))             \
115                 strcpy(a->var, b->var);
116
117 static int add_dev(struct config_device *new_dev)
118 {
119         struct list_head *tmp;
120         struct config_device *tmp_dev;
121
122         /* update the values if we already have the device */
123         list_for_each(tmp, &config_device_list) {
124                 struct config_device *dev = list_entry(tmp, struct config_device, node);
125                 if (strncmp_wildcard(dev->name, new_dev->name, sizeof(dev->name)))
126                         continue;
127                 copy_var(dev, new_dev, type);
128                 copy_var(dev, new_dev, mode);
129                 copy_string(dev, new_dev, bus);
130                 copy_string(dev, new_dev, sysfs_file);
131                 copy_string(dev, new_dev, sysfs_value);
132                 copy_string(dev, new_dev, id);
133                 copy_string(dev, new_dev, place);
134                 copy_string(dev, new_dev, kernel_name);
135                 copy_string(dev, new_dev, owner);
136                 copy_string(dev, new_dev, group);
137                 return 0;
138         }
139
140         /* not found, add new structure to the device list */
141         tmp_dev = malloc(sizeof(*tmp_dev));
142         if (!tmp_dev)
143                 return -ENOMEM;
144         memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
145         list_add(&tmp_dev->node, &config_device_list);
146         //dump_dev(tmp_dev);
147         return 0;
148 }
149
150 static void dump_dev_list(void)
151 {
152         struct list_head *tmp;
153
154         list_for_each(tmp, &config_device_list) {
155                 struct config_device *dev = list_entry(tmp, struct config_device, node);
156                 dump_dev(dev);
157         }
158 }
159         
160 static int get_pair(char **orig_string, char **left, char **right)
161 {
162         char *temp;
163         char *string = *orig_string;
164
165         if (!string)
166                 return -ENODEV;
167
168         /* eat any whitespace */
169         while (isspace(*string))
170                 ++string;
171
172         /* split based on '=' */
173         temp = strsep(&string, "=");
174         *left = temp;
175         if (!string)
176                 return -ENODEV;
177
178         /* take the right side and strip off the '"' */
179         while (isspace(*string))
180                 ++string;
181         if (*string == '"')
182                 ++string;
183         else
184                 return -ENODEV;
185
186         temp = strsep(&string, "\"");
187         if (!string || *temp == '\0')
188                 return -ENODEV;
189         *right = temp;
190         *orig_string = string;
191         
192         return 0;
193 }
194
195 static int get_value(const char *left, char **orig_string, char **ret_string)
196 {
197         int retval;
198         char *left_string;
199
200         retval = get_pair(orig_string, &left_string, ret_string);
201         if (retval)
202                 return retval;
203         if (strcasecmp(left_string, left) != 0)
204                 return -ENODEV;
205         return 0;
206 }
207
208 static int namedev_init_config(void)
209 {
210         char line[255];
211         int lineno;
212         char *temp;
213         char *temp2;
214         char *temp3;
215         FILE *fd;
216         int retval = 0;
217         struct config_device dev;
218
219         dbg("opening '%s' to read as config", udev_config_filename);
220         fd = fopen(udev_config_filename, "r");
221         if (fd == NULL) {
222                 dbg("can't open '%s'", udev_config_filename);
223                 return -ENODEV;
224         }
225
226         /* loop through the whole file */
227         lineno = 0;
228         while (1) {
229                 /* get a line */
230                 temp = fgets(line, sizeof(line), fd);
231                 if (temp == NULL)
232                         goto exit;
233                 lineno++;
234
235                 dbg_parse("read '%s'", temp);
236
237                 /* eat the whitespace at the beginning of the line */
238                 while (isspace(*temp))
239                         ++temp;
240
241                 /* empty line? */
242                 if (*temp == 0x00)
243                         continue;
244
245                 /* see if this is a comment */
246                 if (*temp == COMMENT_CHARACTER)
247                         continue;
248
249                 memset(&dev, 0x00, sizeof(struct config_device));
250
251                 /* parse the line */
252                 temp2 = strsep(&temp, ",");
253                 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
254                         /* label type */
255                         dev.type = LABEL;
256
257                         /* BUS="bus" */
258                         retval = get_value("BUS", &temp, &temp3);
259                         if (retval)
260                                 break;
261                         strfieldcpy(dev.bus, temp3);
262
263                         /* file="value" */
264                         temp2 = strsep(&temp, ",");
265                         retval = get_pair(&temp, &temp2, &temp3);
266                         if (retval)
267                                 break;
268                         strfieldcpy(dev.sysfs_file, temp2);
269                         strfieldcpy(dev.sysfs_value, temp3);
270
271                         /* NAME="new_name" */
272                         temp2 = strsep(&temp, ",");
273                         retval = get_value("NAME", &temp, &temp3);
274                         if (retval)
275                                 break;
276                         strfieldcpy(dev.name, temp3);
277
278                         dbg_parse("LABEL name='%s', bus='%s', "
279                                   "sysfs_file='%s', sysfs_value='%s'",
280                                   dev.name, dev.bus, dev.sysfs_file,
281                                   dev.sysfs_value);
282                 }
283
284                 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
285                         /* number type */
286                         dev.type = NUMBER;
287
288                         /* BUS="bus" */
289                         retval = get_value("BUS", &temp, &temp3);
290                         if (retval)
291                                 break;
292                         strfieldcpy(dev.bus, temp3);
293
294                         /* ID="id" */
295                         temp2 = strsep(&temp, ",");
296                         retval = get_value("ID", &temp, &temp3);
297                         if (retval)
298                                 break;
299                         strfieldcpy(dev.id, temp3);
300
301                         /* NAME="new_name" */
302                         temp2 = strsep(&temp, ",");
303                         retval = get_value("NAME", &temp, &temp3);
304                         if (retval)
305                                 break;
306                         strfieldcpy(dev.name, temp3);
307
308                         dbg_parse("NUMBER name='%s', bus='%s', id='%s'",
309                                   dev.name, dev.bus, dev.id);
310                 }
311
312                 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
313                         /* number type */
314                         dev.type = TOPOLOGY;
315
316                         /* BUS="bus" */
317                         retval = get_value("BUS", &temp, &temp3);
318                         if (retval)
319                                 break;
320                         strfieldcpy(dev.bus, temp3);
321
322                         /* PLACE="place" */
323                         temp2 = strsep(&temp, ",");
324                         retval = get_value("PLACE", &temp, &temp3);
325                         if (retval)
326                                 break;
327                         strfieldcpy(dev.place, temp3);
328
329                         /* NAME="new_name" */
330                         temp2 = strsep(&temp, ",");
331                         retval = get_value("NAME", &temp, &temp3);
332                         if (retval)
333                                 break;
334                         strfieldcpy(dev.name, temp3);
335
336                         dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s'",
337                                   dev.name, dev.bus, dev.place);
338                 }
339
340                 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
341                         /* number type */
342                         dev.type = REPLACE;
343
344                         /* KERNEL="kernel_name" */
345                         retval = get_value("KERNEL", &temp, &temp3);
346                         if (retval)
347                                 break;
348                         strfieldcpy(dev.kernel_name, temp3);
349
350                         /* NAME="new_name" */
351                         temp2 = strsep(&temp, ",");
352                         retval = get_value("NAME", &temp, &temp3);
353                         if (retval)
354                                 break;
355                         strfieldcpy(dev.name, temp3);
356                         dbg_parse("REPLACE name='%s', kernel_name='%s'",
357                                   dev.name, dev.kernel_name);
358                 }
359                 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
360                         /* number type */
361                         dev.type = CALLOUT;
362
363                         /* BUS="bus" */
364                         retval = get_value("BUS", &temp, &temp3);
365                         if (retval)
366                                 break;
367                         strfieldcpy(dev.bus, temp3);
368
369                         /* PROGRAM="executable" */
370                         temp2 = strsep(&temp, ",");
371                         retval = get_value("PROGRAM", &temp, &temp3);
372                         if (retval)
373                                 break;
374                         strfieldcpy(dev.exec_program, temp3);
375
376                         /* ID="id" */
377                         temp2 = strsep(&temp, ",");
378                         retval = get_value("ID", &temp, &temp3);
379                         if (retval)
380                                 break;
381                         strfieldcpy(dev.id, temp3);
382
383                         /* NAME="new_name" */
384                         temp2 = strsep(&temp, ",");
385                         retval = get_value("NAME", &temp, &temp3);
386                         if (retval)
387                                 break;
388                         strfieldcpy(dev.name, temp3);
389                         dbg_parse("CALLOUT name='%s', program='%s'",
390                                   dev.name, dev.exec_program);
391                 }
392
393                 retval = add_dev(&dev);
394                 if (retval) {
395                         dbg("add_dev returned with error %d", retval);
396                         goto exit;
397                 }
398         }
399         dbg_parse("%s:%d:%Zd: error parsing '%s'", udev_config_filename,
400                   lineno, temp - line, temp);
401 exit:
402         fclose(fd);
403         return retval;
404 }       
405
406
407 static int namedev_init_permissions(void)
408 {
409         char line[255];
410         char *temp;
411         char *temp2;
412         FILE *fd;
413         int retval = 0;
414         struct config_device dev;
415
416         dbg("opening '%s' to read as permissions config", udev_config_permission_filename);
417         fd = fopen(udev_config_permission_filename, "r");
418         if (fd == NULL) {
419                 dbg("can't open '%s'", udev_config_permission_filename);
420                 return -ENODEV;
421         }
422
423         /* loop through the whole file */
424         while (1) {
425                 temp = fgets(line, sizeof(line), fd);
426                 if (temp == NULL)
427                         break;
428
429                 dbg_parse("read '%s'", temp);
430
431                 /* eat the whitespace at the beginning of the line */
432                 while (isspace(*temp))
433                         ++temp;
434
435                 /* empty line? */
436                 if (*temp == 0x00)
437                         continue;
438
439                 /* see if this is a comment */
440                 if (*temp == COMMENT_CHARACTER)
441                         continue;
442
443                 memset(&dev, 0x00, sizeof(dev));
444
445                 /* parse the line */
446                 temp2 = strsep(&temp, ":");
447                 if (!temp2) {
448                         dbg("cannot parse line '%s'", line);
449                         continue;
450                 }
451                 strncpy(dev.name, temp2, sizeof(dev.name));
452
453                 temp2 = strsep(&temp, ":");
454                 if (!temp2) {
455                         dbg("cannot parse line '%s'", line);
456                         continue;
457                 }
458                 strncpy(dev.owner, temp2, sizeof(dev.owner));
459
460                 temp2 = strsep(&temp, ":");
461                 if (!temp2) {
462                         dbg("cannot parse line '%s'", line);
463                         continue;
464                 }
465                 strncpy(dev.group, temp2, sizeof(dev.owner));
466
467                 if (!temp) {
468                         dbg("cannot parse line: %s", line);
469                         continue;
470                 }
471                 dev.mode = strtol(temp, NULL, 8);
472
473                 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
474                           dev.name, dev.owner, dev.group,
475                           dev.mode);
476                 retval = add_dev(&dev);
477                 if (retval) {
478                         dbg("add_dev returned with error %d", retval);
479                         goto exit;
480                 }
481         }
482
483 exit:
484         fclose(fd);
485         return retval;
486 }       
487
488 static mode_t get_default_mode(struct sysfs_class_device *class_dev)
489 {
490         /* just default everyone to rw for the world! */
491         return 0666;
492 }
493
494 static void build_kernel_number(struct sysfs_class_device *class_dev, struct udevice *udev)
495 {
496         char *dig;
497
498         /* FIXME, figure out how to handle stuff like sdaj which will not work right now. */
499         dig = class_dev->name + strlen(class_dev->name);
500         while (isdigit(*(dig-1)))
501                 dig--;
502         strfieldcpy(udev->kernel_number, dig);
503         dbg("kernel_number='%s'", udev->kernel_number);
504 }
505
506 static void apply_format(struct udevice *udev, unsigned char *string)
507 {
508         char name[NAME_SIZE];
509         char *pos;
510
511         while (1) {
512                 pos = strchr(string, '%');
513
514                 if (pos) {
515                         strfieldcpy(name, pos+2);
516                         *pos = 0x00;
517                         switch (pos[1]) {
518                         case 'b':
519                                 if (strlen(udev->bus_id) == 0)
520                                         break;
521                                 strcat(pos, udev->bus_id);
522                                 dbg("substitute bus_id '%s'", udev->bus_id);
523                                 break;
524                         case 'n':
525                                 if (strlen(udev->kernel_number) == 0)
526                                         break;
527                                 strcat(pos, udev->kernel_number);
528                                 dbg("substitute kernel number '%s'", udev->kernel_number);
529                                 break;
530                         case 'm':
531                                 sprintf(pos, "%u", udev->minor);
532                                 dbg("substitute minor number '%u'", udev->minor);
533                                 break;
534                         case 'M':
535                                 sprintf(pos, "%u", udev->major);
536                                 dbg("substitute major number '%u'", udev->major);
537                                 break;
538                         case 'c':
539                                 if (strlen(udev->callout_value) == 0)
540                                         break;
541                                 strcat(pos, udev->callout_value);
542                                 dbg("substitute callout output '%s'", udev->callout_value);
543                                 break;
544                         default:
545                                 dbg("unknown substitution type '%%%c'", pos[1]);
546                                 break;
547                         }
548                         strcat(string, name);
549                 } else
550                         break;
551         }
552 }
553
554
555 static int exec_callout(struct config_device *dev, char *value, int len)
556 {
557         int retval;
558         int res;
559         int status;
560         int fds[2];
561         pid_t pid;
562         int value_set = 0;
563         char buffer[256];
564         char *arg;
565         char *args[CALLOUT_MAXARG];
566         int i;
567
568         dbg("callout to '%s'", dev->exec_program);
569         retval = pipe(fds);
570         if (retval != 0) {
571                 dbg("pipe failed");
572                 return -1;
573         }
574         pid = fork();
575         if (pid == -1) {
576                 dbg("fork failed");
577                 return -1;
578         }
579
580         if (pid == 0) {
581                 /* child */
582                 close(STDOUT_FILENO);
583                 dup(fds[1]);    /* dup write side of pipe to STDOUT */
584                 if (strchr(dev->exec_program, ' ')) {
585                         /* callout with arguments */
586                         arg = dev->exec_program;
587                         for (i=0; i < CALLOUT_MAXARG-1; i++) {
588                                 args[i] = strsep(&arg, " ");
589                                 if (args[i] == NULL)
590                                         break;
591                         }
592                         if (args[i]) {
593                                 dbg("too many args - %d", i);
594                                 args[i] = NULL;
595                         }
596                         retval = execve(args[0], args, main_envp);
597                 } else {
598                         retval = execve(dev->exec_program, main_argv, main_envp);
599                 }
600                 if (retval != 0) {
601                         dbg("child execve failed");
602                         exit(1);
603                 }
604                 return -1; /* avoid compiler warning */
605         } else {
606                 /* parent reads from fds[0] */
607                 close(fds[1]);
608                 retval = 0;
609                 while (1) {
610                         res = read(fds[0], buffer, sizeof(buffer) - 1);
611                         if (res <= 0)
612                                 break;
613                         buffer[res] = '\0';
614                         if (res > len) {
615                                 dbg("callout len %d too short", len);
616                                 retval = -1;
617                         }
618                         if (value_set) {
619                                 dbg("callout value already set");
620                                 retval = -1;
621                         } else {
622                                 value_set = 1;
623                                 strncpy(value, buffer, len);
624                         }
625                 }
626                 dbg("callout returned '%s'", value);
627                 close(fds[0]);
628                 res = wait(&status);
629                 if (res < 0) {
630                         dbg("wait failed result %d", res);
631                         retval = -1;
632                 }
633
634 #ifndef __KLIBC__
635                 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
636                         dbg("callout program status 0x%x", status);
637                         retval = -1;
638                 }
639 #endif
640         }
641         return retval;
642 }
643
644 static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev)
645 {
646         struct config_device *dev;
647         struct list_head *tmp;
648
649         list_for_each(tmp, &config_device_list) {
650                 dev = list_entry(tmp, struct config_device, node);
651                 if (dev->type != CALLOUT)
652                         continue;
653
654                 /* substitute anything that needs to be in the program name */
655                 apply_format(udev, dev->exec_program);
656                 if (exec_callout(dev, udev->callout_value, NAME_SIZE))
657                         continue;
658                 if (strncmp_wildcard(udev->callout_value, dev->id, NAME_SIZE) != 0)
659                         continue;
660                 strfieldcpy(udev->name, dev->name);
661                 if (dev->mode != 0) {
662                         udev->mode = dev->mode;
663                         strfieldcpy(udev->owner, dev->owner);
664                         strfieldcpy(udev->group, dev->group);
665                 }
666                 dbg_parse("callout returned matching value '%s', '%s' becomes '%s'"
667                           " - owner='%s', group='%s', mode=%#o",
668                           dev->id, class_dev->name, udev->name,
669                           dev->owner, dev->group, dev->mode);
670                 return 0;
671         }
672         return -ENODEV;
673 }
674
675 static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
676 {
677         struct sysfs_attribute *tmpattr = NULL;
678         struct config_device *dev;
679         struct list_head *tmp;
680
681         list_for_each(tmp, &config_device_list) {
682                 dev = list_entry(tmp, struct config_device, node);
683                 if (dev->type != LABEL)
684                         continue;
685
686                 dbg_parse("look for device attribute '%s'", dev->sysfs_file);
687                 /* try to find the attribute in the class device directory */
688                 tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file);
689                 if (tmpattr)
690                         goto label_found;
691
692                 /* look in the class device directory if present */
693                 if (sysfs_device) {
694                         tmpattr = sysfs_get_device_attr(sysfs_device, dev->sysfs_file);
695                         if (tmpattr)
696                                 goto label_found;
697                 }
698
699                 continue;
700
701 label_found:
702                 tmpattr->value[strlen(tmpattr->value)-1] = 0x00;
703                 dbg_parse("compare attribute '%s' value '%s' with '%s'",
704                           dev->sysfs_file, tmpattr->value, dev->sysfs_value);
705                 if (strcmp(dev->sysfs_value, tmpattr->value) != 0)
706                         continue;
707
708                 strfieldcpy(udev->name, dev->name);
709                 if (dev->mode != 0) {
710                         udev->mode = dev->mode;
711                         strfieldcpy(udev->owner, dev->owner);
712                         strfieldcpy(udev->group, dev->group);
713                 }
714                 dbg_parse("found matching attribute '%s', '%s' becomes '%s' "
715                           "- owner='%s', group='%s', mode=%#o",
716                           dev->sysfs_file, class_dev->name, udev->name,
717                           dev->owner, dev->group, dev->mode);
718
719                 return 0;
720         }
721         return -ENODEV;
722 }
723
724 static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
725 {
726         struct config_device *dev;
727         struct list_head *tmp;
728         char path[SYSFS_PATH_MAX];
729         int found;
730         char *temp = NULL;
731
732         /* we have to have a sysfs device for NUMBER to work */
733         if (!sysfs_device)
734                 return -ENODEV;
735
736         list_for_each(tmp, &config_device_list) {
737                 dev = list_entry(tmp, struct config_device, node);
738                 if (dev->type != NUMBER)
739                         continue;
740
741                 found = 0;
742                 strfieldcpy(path, sysfs_device->path);
743                 temp = strrchr(path, '/');
744                 dbg_parse("search '%s' in '%s', path='%s'", dev->id, temp, path);
745                 if (strstr(temp, dev->id) != NULL) {
746                         found = 1;
747                 } else {
748                         *temp = 0x00;
749                         temp = strrchr(path, '/');
750                         dbg_parse("search '%s' in '%s', path='%s'", dev->id, temp, path);
751                         if (strstr(temp, dev->id) != NULL)
752                                 found = 1;
753                 }
754                 if (!found)
755                         continue;
756                 strfieldcpy(udev->name, dev->name);
757                 if (dev->mode != 0) {
758                         udev->mode = dev->mode;
759                         strfieldcpy(udev->owner, dev->owner);
760                         strfieldcpy(udev->group, dev->group);
761                 }
762                 dbg_parse("found matching id '%s', '%s' becomes '%s'"
763                           " - owner='%s', group ='%s', mode=%#o",
764                           dev->id, class_dev->name, udev->name,
765                           dev->owner, dev->group, dev->mode);
766                 return 0;
767         }
768         return -ENODEV;
769 }
770
771
772 static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
773 {
774         struct config_device *dev;
775         struct list_head *tmp;
776         char path[SYSFS_PATH_MAX];
777         int found;
778         char *temp = NULL;
779
780         /* we have to have a sysfs device for TOPOLOGY to work */
781         if (!sysfs_device)
782                 return -ENODEV;
783
784         list_for_each(tmp, &config_device_list) {
785                 dev = list_entry(tmp, struct config_device, node);
786                 if (dev->type != TOPOLOGY)
787                         continue;
788
789                 found = 0;
790                 strfieldcpy(path, sysfs_device->path);
791                 temp = strrchr(path, '/');
792                 dbg_parse("search '%s' in '%s', path='%s'", dev->place, temp, path);
793                 if (strstr(temp, dev->place) != NULL) {
794                         found = 1;
795                 } else {
796                         *temp = 0x00;
797                         temp = strrchr(path, '/');
798                         dbg_parse("search '%s' in '%s', path='%s'", dev->place, temp, path);
799                         if (strstr(temp, dev->place) != NULL)
800                                 found = 1;
801                 }
802                 if (!found)
803                         continue;
804
805                 strfieldcpy(udev->name, dev->name);
806                 if (dev->mode != 0) {
807                         udev->mode = dev->mode;
808                         strfieldcpy(udev->owner, dev->owner);
809                         strfieldcpy(udev->group, dev->group);
810                 }
811                 dbg_parse("found matching place '%s', '%s' becomes '%s'"
812                           " - owner='%s', group ='%s', mode=%#o",
813                           dev->place, class_dev->name, udev->name,
814                           dev->owner, dev->group, dev->mode);
815                 return 0;
816         }
817         return -ENODEV;
818 }
819
820 static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev)
821 {
822         struct config_device *dev;
823         struct list_head *tmp;
824
825         list_for_each(tmp, &config_device_list) {
826                 dev = list_entry(tmp, struct config_device, node);
827                 if (dev->type != REPLACE)
828                         continue;
829
830                 dbg_parse("compare name '%s' with '%s'",
831                           dev->kernel_name, dev->name);
832                 if (strcmp(dev->kernel_name, class_dev->name) != 0)
833                         continue;
834
835                 strfieldcpy(udev->name, dev->name);
836                 if (dev->mode != 0) {
837                         udev->mode = dev->mode;
838                         strfieldcpy(udev->owner, dev->owner);
839                         strfieldcpy(udev->group, dev->group);
840                 }
841                 dbg_parse("found name, '%s' becomes '%s' - owner='%s', group='%s', mode = %#o",
842                           dev->kernel_name, udev->name, 
843                           dev->owner, dev->group, dev->mode);
844                 
845                 return 0;
846         }
847         return -ENODEV;
848 }
849
850 static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev)
851 {
852         struct config_device *dev;
853         struct list_head *tmp;
854         int len;
855
856         strfieldcpy(udev->name, class_dev->name);
857         /* look for permissions */
858         list_for_each(tmp, &config_device_list) {
859                 dev = list_entry(tmp, struct config_device, node);
860                 len = strlen(dev->name);
861                 if (strncmp_wildcard(class_dev->name, dev->name, sizeof(dev->name)))
862                         continue;
863                 if (dev->mode != 0) {
864                         dbg_parse("found permissions for '%s'", class_dev->name);
865                         udev->mode = dev->mode;
866                         strfieldcpy(udev->owner, dev->owner);
867                         strfieldcpy(udev->group, dev->group);
868                 }
869         }
870 }
871
872 static int get_attr(struct sysfs_class_device *class_dev, struct udevice *udev)
873 {
874         struct sysfs_device *sysfs_device = NULL;
875         struct sysfs_class_device *class_dev_parent = NULL;
876         int retval = 0;
877         char *temp = NULL;
878
879         udev->mode = 0;
880
881         /* find the sysfs_device for this class device */
882         /* Wouldn't it really be nice if libsysfs could do this for us? */
883         if (class_dev->sysdevice) {
884                 sysfs_device = class_dev->sysdevice;
885         } else {
886                 /* bah, let's go backwards up a level to see if the device is there,
887                  * as block partitions don't point to the physical device.  Need to fix that
888                  * up in the kernel...
889                  */
890                 if (strstr(class_dev->path, "block")) {
891                         dbg_parse("looking at block device");
892                         if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
893                                 char path[SYSFS_PATH_MAX];
894
895                                 dbg_parse("really is a partition");
896                                 strfieldcpy(path, class_dev->path);
897                                 temp = strrchr(path, '/');
898                                 *temp = 0x00;
899                                 dbg_parse("looking for a class device at '%s'", path);
900                                 class_dev_parent = sysfs_open_class_device(path);
901                                 if (class_dev_parent == NULL) {
902                                         dbg("sysfs_open_class_device at '%s' failed", path);
903                                 } else {
904                                         dbg_parse("class_dev_parent->name='%s'", class_dev_parent->name);
905                                         if (class_dev_parent->sysdevice)
906                                                 sysfs_device = class_dev_parent->sysdevice;
907                                 }
908                         }
909                 }
910         }
911                 
912         if (sysfs_device) {
913                 dbg_parse("sysfs_device->path='%s'", sysfs_device->path);
914                 dbg_parse("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
915                 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
916         } else {
917                 dbg_parse("class_dev->name = '%s'", class_dev->name);
918         }
919
920         build_kernel_number(class_dev, udev);
921
922         /* rules are looked at in priority order */
923         retval = do_callout(class_dev, udev);
924         if (retval == 0)
925                 goto found;
926
927         retval = do_label(class_dev, udev, sysfs_device);
928         if (retval == 0)
929                 goto found;
930
931         retval = do_number(class_dev, udev, sysfs_device);
932         if (retval == 0)
933                 goto found;
934
935         retval = do_topology(class_dev, udev, sysfs_device);
936         if (retval == 0)
937                 goto found;
938
939         retval = do_replace(class_dev, udev);
940         if (retval == 0)
941                 goto found;
942
943         do_kernelname(class_dev, udev);
944         goto done;
945
946 found:
947         /* substitute placeholder in NAME  */
948         apply_format(udev, udev->name);
949
950 done:
951         /* mode was never set above */
952         if (!udev->mode) {
953                 udev->mode = get_default_mode(class_dev);
954                 udev->owner[0] = 0x00;
955                 udev->group[0] = 0x00;
956         }
957
958         if (class_dev_parent)
959                 sysfs_close_class_device(class_dev_parent);
960
961         return 0;
962 }
963
964 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *dev)
965 {
966         int retval;
967
968         retval = get_attr(class_dev, dev);
969         if (retval)
970                 dbg("get_attr failed");
971
972         return retval;
973 }
974
975 int namedev_init(void)
976 {
977         int retval;
978         
979         retval = namedev_init_config();
980         if (retval)
981                 return retval;
982
983         retval = namedev_init_permissions();
984         if (retval)
985                 return retval;
986
987         dump_dev_list();
988         return retval;
989 }