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