chiark / gitweb /
bed328f6464351fd7cced6226c64d6d927f41e9d
[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 #define CALLOUT_MAXARG  8
49
50 static LIST_HEAD(config_device_list);
51
52 static void dump_dev(struct config_device *dev)
53 {
54         switch (dev->type) {
55         case KERNEL_NAME:
56                 dbg_parse("KERNEL name='%s' ,"
57                           "owner='%s', group='%s', mode=%#o",
58                           dev->name, 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 method");
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, add new structure to the device 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                 /* empty 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                         /* BUS="bus" */
358                         retval = get_value("BUS", &temp, &temp3);
359                         if (retval)
360                                 break;
361                         strfieldcpy(dev.bus, temp3);
362
363                         /* PROGRAM="executable" */
364                         temp2 = strsep(&temp, ",");
365                         retval = get_value("PROGRAM", &temp, &temp3);
366                         if (retval)
367                                 break;
368                         strfieldcpy(dev.exec_program, 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                 /* empty 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         char *arg;
484         char *args[CALLOUT_MAXARG];
485         int i;
486
487         dbg("callout to '%s'", dev->exec_program);
488         retval = pipe(fds);
489         if (retval != 0) {
490                 dbg("pipe failed");
491                 return -1;
492         }
493         pid = fork();
494         if (pid == -1) {
495                 dbg("fork failed");
496                 return -1;
497         }
498
499         if (pid == 0) {
500                 /* child */
501                 close(STDOUT_FILENO);
502                 dup(fds[1]);    /* dup write side of pipe to STDOUT */
503                 if (strchr(dev->exec_program, ' ')) {
504                         /* callout with arguments */
505                         arg = dev->exec_program;
506                         for (i=0; i < CALLOUT_MAXARG-1; i++) {
507                                 args[i] = strsep(&arg, " ");
508                                 if (args[i] == NULL)
509                                         break;
510                         }
511                         if (args[i]) {
512                                 dbg("to many args - %d", i);
513                                 args[i] = NULL;
514                         }
515                         retval = execve(args[0], args, main_envp);
516                 } else {
517                         retval = execve(dev->exec_program, main_argv, main_envp);
518                 }
519                 if (retval != 0) {
520                         dbg("child execve failed");
521                         exit(1);
522                 }
523                 return -1; /* avoid compiler warning */
524         } else {
525                 /* parent reads from fds[0] */
526                 close(fds[1]);
527                 retval = 0;
528                 while (1) {
529                         res = read(fds[0], buffer, sizeof(buffer) - 1);
530                         if (res <= 0)
531                                 break;
532                         buffer[res] = '\0';
533                         if (res > len) {
534                                 dbg("callout len %d too short\n", len);
535                                 retval = -1;
536                         }
537                         if (value_set) {
538                                 dbg("callout value already set");
539                                 retval = -1;
540                         } else {
541                                 value_set = 1;
542                                 strncpy(value, buffer, len);
543                         }
544                 }
545                 dbg("callout returned '%s'", value);
546                 close(fds[0]);
547                 res = wait(&status);
548                 if (res < 0) {
549                         dbg("wait failed result %d", res);
550                         retval = -1;
551                 }
552
553 #ifndef __KLIBC__
554                 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
555                         dbg("callout program status 0x%x", status);
556                         retval = -1;
557                 }
558 #endif
559         }
560         return retval;
561 }
562
563 static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev)
564 {
565         struct config_device *dev;
566         struct list_head *tmp;
567         char value[ID_SIZE];
568
569         list_for_each(tmp, &config_device_list) {
570                 dev = list_entry(tmp, struct config_device, node);
571                 if (dev->type != CALLOUT)
572                         continue;
573
574                 if (exec_callout(dev, value, sizeof(value)))
575                         continue;
576                 if (strncmp(value, dev->id, sizeof(value)) != 0)
577                         continue;
578                 strfieldcpy(udev->name, dev->name);
579                 if (dev->mode != 0) {
580                         udev->mode = dev->mode;
581                         strfieldcpy(udev->owner, dev->owner);
582                         strfieldcpy(udev->group, dev->group);
583                 }
584                 dbg_parse("callout returned matching value '%s', '%s' becomes '%s'"
585                           " - owner='%s', group='%s', mode =%#o",
586                           dev->id, class_dev->name, udev->name,
587                           dev->owner, dev->group, dev->mode);
588                 return 0;
589         }
590         return -ENODEV;
591 }
592
593 static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
594 {
595         struct sysfs_attribute *tmpattr = NULL;
596         struct config_device *dev;
597         struct list_head *tmp;
598
599         list_for_each(tmp, &config_device_list) {
600                 dev = list_entry(tmp, struct config_device, node);
601                 if (dev->type != LABEL)
602                         continue;
603
604                 dbg_parse("look for device attribute '%s'", dev->sysfs_file);
605                 /* try to find the attribute in the class device directory */
606                 tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file);
607                 if (tmpattr)
608                         goto label_found;
609
610                 /* look in the class device directory if present */
611                 if (sysfs_device) {
612                         tmpattr = sysfs_get_device_attr(sysfs_device, dev->sysfs_file);
613                         if (tmpattr)
614                                 goto label_found;
615                 }
616
617                 continue;
618
619 label_found:
620                 tmpattr->value[strlen(tmpattr->value)-1] = 0x00;
621                 dbg_parse("compare attribute '%s' value '%s' with '%s'",
622                           dev->sysfs_file, tmpattr->value, dev->sysfs_value);
623                 if (strcmp(dev->sysfs_value, tmpattr->value) != 0)
624                         continue;
625
626                 strfieldcpy(udev->name, dev->name);
627                 if (dev->mode != 0) {
628                         udev->mode = dev->mode;
629                         strfieldcpy(udev->owner, dev->owner);
630                         strfieldcpy(udev->group, dev->group);
631                 }
632                 dbg_parse("found matching attribute '%s', '%s' becomes '%s' "
633                           "- owner='%s', group='%s', mode=%#o",
634                           dev->sysfs_file, class_dev->name, udev->name,
635                           dev->owner, dev->group, dev->mode);
636
637                 return 0;
638         }
639         return -ENODEV;
640 }
641
642 static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
643 {
644         struct config_device *dev;
645         struct list_head *tmp;
646         char path[SYSFS_PATH_MAX];
647         int found;
648         char *temp = NULL;
649
650         /* we have to have a sysfs device for NUMBER to work */
651         if (!sysfs_device)
652                 return -ENODEV;
653
654         list_for_each(tmp, &config_device_list) {
655                 dev = list_entry(tmp, struct config_device, node);
656                 if (dev->type != NUMBER)
657                         continue;
658
659                 found = 0;
660                 strfieldcpy(path, sysfs_device->path);
661                 temp = strrchr(path, '/');
662                 dbg_parse("search '%s' in '%s', path='%s'", dev->id, temp, path);
663                 if (strstr(temp, dev->id) != NULL) {
664                         found = 1;
665                 } else {
666                         *temp = 0x00;
667                         temp = strrchr(path, '/');
668                         dbg_parse("search '%s' in '%s', path='%s'", dev->id, temp, path);
669                         if (strstr(temp, dev->id) != NULL)
670                                 found = 1;
671                 }
672                 if (!found)
673                         continue;
674                 strfieldcpy(udev->name, dev->name);
675                 if (dev->mode != 0) {
676                         udev->mode = dev->mode;
677                         strfieldcpy(udev->owner, dev->owner);
678                         strfieldcpy(udev->group, dev->group);
679                 }
680                 dbg_parse("found matching id '%s', '%s' becomes '%s'"
681                           " - owner='%s', group ='%s', mode=%#o",
682                           dev->id, class_dev->name, udev->name,
683                           dev->owner, dev->group, dev->mode);
684                 return 0;
685         }
686         return -ENODEV;
687 }
688
689
690 static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
691 {
692         struct config_device *dev;
693         struct list_head *tmp;
694         char path[SYSFS_PATH_MAX];
695         int found;
696         char *temp = NULL;
697
698         /* we have to have a sysfs device for TOPOLOGY to work */
699         if (!sysfs_device)
700                 return -ENODEV;
701
702         list_for_each(tmp, &config_device_list) {
703                 dev = list_entry(tmp, struct config_device, node);
704                 if (dev->type != TOPOLOGY)
705                         continue;
706
707                 found = 0;
708                 strfieldcpy(path, sysfs_device->path);
709                 temp = strrchr(path, '/');
710                 dbg_parse("search '%s' in '%s', path='%s'", dev->place, temp, path);
711                 if (strstr(temp, dev->place) != NULL) {
712                         found = 1;
713                 } else {
714                         *temp = 0x00;
715                         temp = strrchr(path, '/');
716                         dbg_parse("search '%s' in '%s', path='%s'", dev->place, temp, path);
717                         if (strstr(temp, dev->place) != NULL)
718                                 found = 1;
719                 }
720                 if (!found)
721                         continue;
722
723                 strfieldcpy(udev->name, dev->name);
724                 if (dev->mode != 0) {
725                         udev->mode = dev->mode;
726                         strfieldcpy(udev->owner, dev->owner);
727                         strfieldcpy(udev->group, dev->group);
728                 }
729                 dbg_parse("found matching place '%s', '%s' becomes '%s'"
730                           " - owner='%s', group ='%s', mode=%#o",
731                           dev->place, class_dev->name, udev->name,
732                           dev->owner, dev->group, dev->mode);
733                 return 0;
734         }
735         return -ENODEV;
736 }
737
738 static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev)
739 {
740         struct config_device *dev;
741         struct list_head *tmp;
742
743         list_for_each(tmp, &config_device_list) {
744                 dev = list_entry(tmp, struct config_device, node);
745                 if (dev->type != REPLACE)
746                         continue;
747
748                 dbg_parse("compare name '%s' with '%s'",
749                           dev->kernel_name, dev->name);
750                 if (strcmp(dev->kernel_name, class_dev->name) != 0)
751                         continue;
752
753                 strfieldcpy(udev->name, dev->name);
754                 if (dev->mode != 0) {
755                         udev->mode = dev->mode;
756                         strfieldcpy(udev->owner, dev->owner);
757                         strfieldcpy(udev->group, dev->group);
758                 }
759                 dbg_parse("found name, '%s' becomes '%s' - owner='%s', group='%s', mode = %#o",
760                           dev->kernel_name, udev->name, 
761                           dev->owner, dev->group, dev->mode);
762                 
763                 return 0;
764         }
765         return -ENODEV;
766 }
767
768 static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev)
769 {
770         struct config_device *dev;
771         struct list_head *tmp;
772         int len;
773
774         strfieldcpy(udev->name, class_dev->name);
775         list_for_each(tmp, &config_device_list) {
776                 dev = list_entry(tmp, struct config_device, node);
777                 len = strlen(dev->name);
778                 if (dev->name[len-1] == '*') {
779                         len--;
780                         if (strncmp(dev->name, class_dev->name, len))
781                                 continue;
782                 } else {
783                         if (strcmp(dev->name, class_dev->name))
784                                 continue;
785                 }
786                 if (dev->mode != 0) {
787                         dbg_parse("found permissions for '%s'", class_dev->name);
788                         udev->mode = dev->mode;
789                         strfieldcpy(udev->owner, dev->owner);
790                         strfieldcpy(udev->group, dev->group);
791                 }
792         }
793 }
794
795 static int get_attr(struct sysfs_class_device *class_dev, struct udevice *udev)
796 {
797         struct sysfs_device *sysfs_device = NULL;
798         struct sysfs_class_device *class_dev_parent = NULL;
799         int retval = 0;
800         char *temp = NULL;
801
802         udev->mode = 0;
803
804         /* find the sysfs_device for this class device */
805         /* Wouldn't it really be nice if libsysfs could do this for us? */
806         if (class_dev->sysdevice) {
807                 sysfs_device = class_dev->sysdevice;
808         } else {
809                 /* bah, let's go backwards up a level to see if the device is there,
810                  * as block partitions don't point to the physical device.  Need to fix that
811                  * up in the kernel...
812                  */
813                 if (strstr(class_dev->path, "block")) {
814                         dbg_parse("looking at block device...");
815                         if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
816                                 char path[SYSFS_PATH_MAX];
817
818                                 dbg_parse("really is a partition...");
819                                 strfieldcpy(path, class_dev->path);
820                                 temp = strrchr(path, '/');
821                                 *temp = 0x00;
822                                 dbg_parse("looking for a class device at '%s'", path);
823                                 class_dev_parent = sysfs_open_class_device(path);
824                                 if (class_dev_parent == NULL) {
825                                         dbg("sysfs_open_class_device at '%s' failed", path);
826                                 } else {
827                                         dbg_parse("class_dev_parent->name=%s", class_dev_parent->name);
828                                         if (class_dev_parent->sysdevice)
829                                                 sysfs_device = class_dev_parent->sysdevice;
830                                 }
831                         }
832                 }
833         }
834                 
835         if (sysfs_device) {
836                 dbg_parse("sysfs_device->path='%s'", sysfs_device->path);
837                 dbg_parse("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
838         } else {
839                 dbg_parse("class_dev->name = '%s'", class_dev->name);
840         }
841
842         /* rules are looked at in priority order */
843         retval = do_callout(class_dev, udev);
844         if (retval == 0)
845                 goto found;
846
847         retval = do_label(class_dev, udev, sysfs_device);
848         if (retval == 0)
849                 goto found;
850
851         retval = do_number(class_dev, udev, sysfs_device);
852         if (retval == 0)
853                 goto found;
854
855         retval = do_topology(class_dev, udev, sysfs_device);
856         if (retval == 0)
857                 goto found;
858
859         retval = do_replace(class_dev, udev);
860         if (retval == 0)
861                 goto found;
862
863         do_kernelname(class_dev, udev);
864         goto done;
865
866 found:
867         /* substitute placeholder in NAME  */
868         while (1) {
869                 char *pos = strchr(udev->name, '%');
870                 char *dig;
871                 char name[NAME_SIZE];
872                 if (pos) {
873                         strfieldcpy(name, pos+2);
874                         *pos = 0x00;
875                         switch (pos[1]) {
876                         case 'b':
877                                 if (!sysfs_device)
878                                         break;
879                                 strcat(udev->name, sysfs_device->bus_id);
880                                 dbg("bus_id inserted: %s", 
881                                                 sysfs_device->bus_id);
882                                 break;
883                         case 'n':
884                                 dig = class_dev->name + strlen(class_dev->name);
885                                 while (isdigit(*(dig-1)))
886                                         dig--;
887                                 strcat(udev->name, dig);
888                                 dbg("substitute kernel number '%s'", dig);
889                                 break;
890                         case 'm':
891                                 sprintf(pos, "%u", udev->minor);
892                                 dbg("substitute minor number '%u'", udev->minor);
893                                 break;
894                         case 'M':
895                                 sprintf(pos, "%u", udev->major);
896                                 dbg("substitute major number '%u'", udev->major);
897                                 break;
898                         default:
899                                 dbg("unknown substitution type '%%%c'", pos[1]);
900                                 break;
901                         }
902                         strcat(udev->name, name);
903                 } else
904                         break;
905         }
906
907 done:
908         /* mode was never set above */
909         if (!udev->mode) {
910                 udev->mode = get_default_mode(class_dev);
911                 udev->owner[0] = 0x00;
912                 udev->group[0] = 0x00;
913         }
914
915         if (class_dev_parent)
916                 sysfs_close_class_device(class_dev_parent);
917
918         return 0;
919 }
920
921 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *dev)
922 {
923         int retval;
924
925         retval = get_attr(class_dev, dev);
926         if (retval)
927                 dbg("get_attr failed");
928
929         return retval;
930 }
931
932 int namedev_init(void)
933 {
934         int retval;
935         
936         retval = namedev_init_config();
937         if (retval)
938                 return retval;
939
940         retval = namedev_init_permissions();
941         if (retval)
942                 return retval;
943
944         dump_dev_list();
945         return retval;
946 }
947
948