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