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