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