chiark / gitweb /
0a37ef2fbca2aee86d0c628cf1100d71cd67da1c
[elogind.git] / namedev.c
1 /*
2  * namedev.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7  * Copyright (C) 2003-2005 Kay Sievers <kay.sievers@vrfy.org>
8  *
9  *
10  *      This program is free software; you can redistribute it and/or modify it
11  *      under the terms of the GNU General Public License as published by the
12  *      Free Software Foundation version 2 of the License.
13  * 
14  *      This program is distributed in the hope that it will be useful, but
15  *      WITHOUT ANY WARRANTY; without even the implied warranty of
16  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *      General Public License for more details.
18  * 
19  *      You should have received a copy of the GNU General Public License along
20  *      with this program; if not, write to the Free Software Foundation, Inc.,
21  *      675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
24
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <ctype.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <sys/wait.h>
34 #include <sys/stat.h>
35
36 #include "libsysfs/sysfs/libsysfs.h"
37 #include "list.h"
38 #include "udev.h"
39 #include "udev_utils.h"
40 #include "udev_version.h"
41 #include "logging.h"
42 #include "namedev.h"
43 #include "udev_db.h"
44
45 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr);
46
47 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
48 static int strcmp_pattern(const char *p, const char *s)
49 {
50         if (s[0] == '\0') {
51                 while (p[0] == '*')
52                         p++;
53                 return (p[0] != '\0');
54         }
55         switch (p[0]) {
56         case '[':
57                 {
58                         int not = 0;
59                         p++;
60                         if (p[0] == '!') {
61                                 not = 1;
62                                 p++;
63                         }
64                         while ((p[0] != '\0') && (p[0] != ']')) {
65                                 int match = 0;
66                                 if (p[1] == '-') {
67                                         if ((s[0] >= p[0]) && (s[0] <= p[2]))
68                                                 match = 1;
69                                         p += 3;
70                                 } else {
71                                         match = (p[0] == s[0]);
72                                         p++;
73                                 }
74                                 if (match ^ not) {
75                                         while ((p[0] != '\0') && (p[0] != ']'))
76                                                 p++;
77                                         if (p[0] == ']')
78                                                 return strcmp_pattern(p+1, s+1);
79                                 }
80                         }
81                 }
82                 break;
83         case '*':
84                 if (strcmp_pattern(p, s+1))
85                         return strcmp_pattern(p+1, s);
86                 return 0;
87         case '\0':
88                 if (s[0] == '\0') {
89                         return 0;
90                 }
91                 break;
92         default:
93                 if ((p[0] == s[0]) || (p[0] == '?'))
94                         return strcmp_pattern(p+1, s+1);
95                 break;
96         }
97         return 1;
98 }
99
100 /* extract possible {attr} and move str behind it */
101 static char *get_format_attribute(char **str)
102 {
103         char *pos;
104         char *attr = NULL;
105
106         if (*str[0] == '{') {
107                 pos = strchr(*str, '}');
108                 if (pos == NULL) {
109                         dbg("missing closing brace for format");
110                         return NULL;
111                 }
112                 pos[0] = '\0';
113                 attr = *str+1;
114                 *str = pos+1;
115                 dbg("attribute='%s', str='%s'", attr, *str);
116         }
117         return attr;
118 }
119
120 /* extract possible format length and move str behind it*/
121 static int get_format_len(char **str)
122 {
123         int num;
124         char *tail;
125
126         if (isdigit(*str[0])) {
127                 num = (int) strtoul(*str, &tail, 10);
128                 if (num > 0) {
129                         *str = tail;
130                         dbg("format length=%i", num);
131                         return num;
132                 } else {
133                         dbg("format parsing error '%s'", *str);
134                 }
135         }
136         return -1;
137 }
138
139 /** Finds the lowest positive N such that <name>N isn't present in 
140  *  $(udevroot) either as a file or a symlink.
141  *
142  *  @param  name                Name to check for
143  *  @return                     0 if <name> didn't exist and N otherwise.
144  */
145 static int find_free_number(struct udevice *udev, const char *name)
146 {
147         char filename[NAME_SIZE];
148         int num = 0;
149         struct udevice db_udev;
150
151         strfieldcpy(filename, name);
152         while (1) {
153                 dbg("look for existing node '%s'", filename);
154                 memset(&db_udev, 0x00, sizeof(struct udevice));
155                 if (udev_db_get_device_byname(&db_udev, filename) != 0) {
156                         dbg("free num=%d", num);
157                         return num;
158                 }
159
160                 num++;
161                 if (num > 1000) {
162                         info("find_free_number gone crazy (num=%d), aborted", num);
163                         return -1;
164                 }
165                 snprintf(filename, NAME_SIZE, "%s%d", name, num);
166                 filename[NAME_SIZE-1] = '\0';
167         }
168 }
169
170 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
171                          struct sysfs_class_device *class_dev,
172                          struct sysfs_device *sysfs_device)
173 {
174         char temp[NAME_SIZE];
175         char temp2[NAME_SIZE];
176         char *tail;
177         char *pos;
178         char *attr;
179         int len;
180         int i;
181         char c;
182         char *spos;
183         char *rest;
184         int slen;
185         struct sysfs_attribute *tmpattr;
186         unsigned int next_free_number;
187         struct sysfs_class_device *class_dev_parent;
188
189         pos = string;
190         while (1) {
191                 pos = strchr(pos, '%');
192                 if (pos == NULL)
193                         break;
194
195                 pos[0] = '\0';
196                 tail = pos+1;
197                 len = get_format_len(&tail);
198                 c = tail[0];
199                 strfieldcpy(temp, tail+1);
200                 tail = temp;
201                 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
202                 attr = get_format_attribute(&tail);
203
204
205                 switch (c) {
206                 case 'p':
207                         if (strlen(udev->devpath) == 0)
208                                 break;
209                         strfieldcatmax(string, udev->devpath, maxsize);
210                         dbg("substitute kernel name '%s'", udev->kernel_name);
211                         break;
212                 case 'b':
213                         if (strlen(udev->bus_id) == 0)
214                                 break;
215                         strfieldcatmax(string, udev->bus_id, maxsize);
216                         dbg("substitute bus_id '%s'", udev->bus_id);
217                         break;
218                 case 'k':
219                         if (strlen(udev->kernel_name) == 0)
220                                 break;
221                         strfieldcatmax(string, udev->kernel_name, maxsize);
222                         dbg("substitute kernel name '%s'", udev->kernel_name);
223                         break;
224                 case 'n':
225                         if (strlen(udev->kernel_number) == 0)
226                                 break;
227                         strfieldcatmax(string, udev->kernel_number, maxsize);
228                         dbg("substitute kernel number '%s'", udev->kernel_number);
229                                 break;
230                 case 'm':
231                         strintcatmax(string, minor(udev->devt), maxsize);
232                         dbg("substitute minor number '%u'", minor(udev->devt));
233                         break;
234                 case 'M':
235                         strintcatmax(string, major(udev->devt), maxsize);
236                         dbg("substitute major number '%u'", major(udev->devt));
237                         break;
238                 case 'c':
239                         if (strlen(udev->program_result) == 0)
240                                 break;
241                         /* get part part of the result string */
242                         i = 0;
243                         if (attr != NULL)
244                                 i = strtoul(attr, &rest, 10);
245                         if (i > 0) {
246                                 foreach_strpart(udev->program_result, " \n\r", spos, slen) {
247                                         i--;
248                                         if (i == 0)
249                                                 break;
250                                 }
251                                 if (i > 0) {
252                                         dbg("requested part of result string not found");
253                                         break;
254                                 }
255                                 if (rest[0] == '+')
256                                         strfieldcpy(temp2, spos);
257                                 else
258                                         strfieldcpymax(temp2, spos, slen+1);
259                                 strfieldcatmax(string, temp2, maxsize);
260                                 dbg("substitute part of result string '%s'", temp2);
261                         } else {
262                                 strfieldcatmax(string, udev->program_result, maxsize);
263                                 dbg("substitute result string '%s'", udev->program_result);
264                         }
265                         break;
266                 case 's':
267                         if (!class_dev)
268                                 break;
269                         if (attr == NULL) {
270                                 dbg("missing attribute");
271                                 break;
272                         }
273                         tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr);
274                         if (tmpattr == NULL) {
275                                 dbg("sysfa attribute '%s' not found", attr);
276                                 break;
277                         }
278                         /* strip trailing whitespace of matching value */
279                         if (isspace(tmpattr->value[strlen(tmpattr->value)-1])) {
280                                 i = len = strlen(tmpattr->value);
281                                 while (i > 0 &&  isspace(tmpattr->value[i-1]))
282                                         i--;
283                                 if (i < len) {
284                                         tmpattr->value[i] = '\0';
285                                         dbg("remove %i trailing whitespace chars from '%s'",
286                                                  len - i, tmpattr->value);
287                                 }
288                         }
289                         strfieldcatmax(string, tmpattr->value, maxsize);
290                         dbg("substitute sysfs value '%s'", tmpattr->value);
291                         break;
292                 case '%':
293                         strfieldcatmax(string, "%", maxsize);
294                         pos++;
295                         break;
296                 case 'e':
297                         next_free_number = find_free_number(udev, string);
298                         if (next_free_number > 0) {
299                                 sprintf(temp2, "%d", next_free_number);
300                                 strfieldcatmax(string, temp2, maxsize);
301                         }
302                         break;
303                 case 'P':
304                         if (!class_dev)
305                                 break;
306                         class_dev_parent = sysfs_get_classdev_parent(class_dev);
307                         if (class_dev_parent != NULL) {
308                                 struct udevice udev_parent;
309
310                                 dbg("found parent '%s', get the node name", class_dev_parent->path);
311                                 memset(&udev_parent, 0x00, sizeof(struct udevice));
312                                 /* lookup the name in the udev_db with the DEVPATH of the parent */
313                                 if (udev_db_get_device_by_devpath(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) {
314                                         strfieldcatmax(string, udev_parent.name, maxsize);
315                                         dbg("substitute parent node name'%s'", udev_parent.name);
316                                 } else
317                                         dbg("parent not found in database");
318                         }
319                         break;
320                 case 'N':
321                         if (udev->tmp_node[0] == '\0') {
322                                 dbg("create temporary device node for callout");
323                                 snprintf(udev->tmp_node, NAME_SIZE, "%s/.tmp-%u-%u", udev_root, major(udev->devt), minor(udev->devt));
324                                 udev->tmp_node[NAME_SIZE] = '\0';
325                                 udev_make_node(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
326                         }
327                         strfieldcatmax(string, udev->tmp_node, maxsize);
328                         dbg("substitute temporary device node name '%s'", udev->tmp_node);
329                         break;
330                 case 'r':
331                         strfieldcatmax(string, udev_root, maxsize);
332                         dbg("substitute udev_root '%s'", udev_root);
333                         break;
334                 default:
335                         dbg("unknown substitution type '%%%c'", c);
336                         break;
337                 }
338                 /* truncate to specified length */
339                 if (len > 0)
340                         pos[len] = '\0';
341
342                 strfieldcatmax(string, tail, maxsize);
343         }
344 }
345
346 static void fix_kernel_name(struct udevice *udev)
347 {
348         char *temp = udev->kernel_name;
349
350         while (*temp != 0x00) {
351                 /* Some block devices have a ! in their name, 
352                  * we need to change that to / */
353                 if (*temp == '!')
354                         *temp = '/';
355                 ++temp;
356         }
357 }
358
359 static int execute_program(struct udevice *udev, const char *path, char *value, int len)
360 {
361         int retval;
362         int count;
363         int status;
364         int fds[2];
365         pid_t pid;
366         char *pos;
367         char arg[PROGRAM_SIZE];
368         char *argv[(PROGRAM_SIZE / 2) + 1];
369         int i;
370
371         strfieldcpy(arg, path);
372         i = 0;
373         if (strchr(path, ' ')) {
374                 pos = arg;
375                 while (pos != NULL) {
376                         if (pos[0] == '\'') {
377                                 /* don't separate if in apostrophes */
378                                 pos++;
379                                 argv[i] = strsep(&pos, "\'");
380                                 while (pos && pos[0] == ' ')
381                                         pos++;
382                         } else {
383                                 argv[i] = strsep(&pos, " ");
384                         }
385                         dbg("arg[%i] '%s'", i, argv[i]);
386                         i++;
387                 }
388                 argv[i] =  NULL;
389                 dbg("execute '%s' with parsed arguments", arg);
390         } else {
391                 argv[0] = arg;
392                 argv[1] = udev->subsystem;
393                 argv[2] = NULL;
394                 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
395         }
396
397         retval = pipe(fds);
398         if (retval != 0) {
399                 dbg("pipe failed");
400                 return -1;
401         }
402
403         pid = fork();
404         switch(pid) {
405         case 0:
406                 /* child */
407                 /* dup2 write side of pipe to STDOUT */
408                 dup2(fds[1], STDOUT_FILENO);
409                 retval = execv(arg, argv);
410
411                 info(FIELD_PROGRAM " execution of '%s' failed", path);
412                 exit(1);
413         case -1:
414                 dbg("fork failed");
415                 return -1;
416         default:
417                 /* parent reads from fds[0] */
418                 close(fds[1]);
419                 retval = 0;
420                 i = 0;
421                 while (1) {
422                         count = read(fds[0], value + i, len - i-1);
423                         if (count <= 0)
424                                 break;
425
426                         i += count;
427                         if (i >= len-1) {
428                                 dbg("result len %d too short", len);
429                                 retval = -1;
430                                 break;
431                         }
432                 }
433
434                 if (count < 0) {
435                         dbg("read failed with '%s'", strerror(errno));
436                         retval = -1;
437                 }
438
439                 if (i > 0 && value[i-1] == '\n')
440                         i--;
441                 value[i] = '\0';
442                 dbg("result is '%s'", value);
443
444                 close(fds[0]);
445                 waitpid(pid, &status, 0);
446
447                 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
448                         dbg("exec program status 0x%x", status);
449                         retval = -1;
450                 }
451         }
452         return retval;
453 }
454
455 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
456 {
457         struct sysfs_attribute *tmpattr = NULL;
458         char *c;
459
460         dbg("look for device attribute '%s'", attr);
461         /* try to find the attribute in the class device directory */
462         tmpattr = sysfs_get_classdev_attr(class_dev, attr);
463         if (tmpattr)
464                 goto attr_found;
465
466         /* look in the class device directory if present */
467         if (sysfs_device) {
468                 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
469                 if (tmpattr)
470                         goto attr_found;
471         }
472
473         return NULL;
474
475 attr_found:
476         c = strchr(tmpattr->value, '\n');
477         if (c != NULL)
478                 c[0] = '\0';
479
480         dbg("found attribute '%s'", tmpattr->path);
481         return tmpattr;
482 }
483
484 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
485 {
486         struct sysfs_attribute *tmpattr;
487         int i;
488         int len;
489
490         if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
491                 return -ENODEV;
492
493         tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file);
494         if (tmpattr == NULL)
495                 return -ENODEV;
496
497         /* strip trailing whitespace of value, if not asked to match for it */
498         if (! isspace(pair->value[strlen(pair->value)-1])) {
499                 i = len = strlen(tmpattr->value);
500                 while (i > 0 &&  isspace(tmpattr->value[i-1]))
501                         i--;
502                 if (i < len) {
503                         tmpattr->value[i] = '\0';
504                         dbg("remove %i trailing whitespace chars from '%s'",
505                             len - i, tmpattr->value);
506                 }
507         }
508
509         dbg("compare attribute '%s' value '%s' with '%s'",
510                   pair->file, tmpattr->value, pair->value);
511         if (strcmp_pattern(pair->value, tmpattr->value) != 0)
512                 return -ENODEV;
513
514         dbg("found matching attribute '%s' with value '%s'",
515             pair->file, pair->value);
516         return 0;
517 }
518
519 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
520 {
521         struct sysfs_pair *pair;
522         int i;
523
524         for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
525                 pair = &dev->sysfs_pair[i];
526                 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
527                         break;
528                 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
529                         dbg("sysfs attribute doesn't match");
530                         return -ENODEV;
531                 }
532         }
533
534         return 0;
535 }
536
537 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
538 {
539         char path[SYSFS_PATH_MAX];
540         char *temp;
541
542         /* we have to have a sysfs device for ID to work */
543         if (!sysfs_device)
544                 return -ENODEV;
545
546         strfieldcpy(path, sysfs_device->path);
547         temp = strrchr(path, '/');
548         temp++;
549         dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
550         if (strcmp_pattern(dev->id, temp) != 0)
551                 return -ENODEV;
552         else
553                 return 0;
554 }
555
556 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
557 {
558         char path[SYSFS_PATH_MAX];
559         char *temp;
560
561         /* we have to have a sysfs device for PLACE to work */
562         if (!sysfs_device)
563                 return -ENODEV;
564
565         strfieldcpy(path, sysfs_device->path);
566         temp = strrchr(path, '/');
567         dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
568         if (strstr(temp, dev->place) != NULL)
569                 return 0;
570
571         /* try the parent */
572         temp[0] = '\0';
573         temp = strrchr(path, '/');
574         dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
575         if (strstr(temp, dev->place) == NULL)
576                 return 0;
577
578         dbg("place doesn't match");
579         return -ENODEV;
580 }
581
582 static int match_rule(struct udevice *udev, struct config_device *dev,
583                       struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
584 {
585         /* check for matching kernel name */
586         if (dev->kernel[0] != '\0') {
587                 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'",
588                     dev->kernel, class_dev->name);
589                 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
590                         dbg(FIELD_KERNEL " is not matching");
591                         goto exit;
592                 }
593                 dbg(FIELD_KERNEL " matches");
594         }
595
596         /* check for matching subsystem */
597         if (dev->subsystem[0] != '\0') {
598                 dbg("check for " FIELD_SUBSYSTEM " dev->subsystem='%s' class_dev->name='%s'",
599                     dev->subsystem, class_dev->name);
600                 if (strcmp_pattern(dev->subsystem, udev->subsystem) != 0) {
601                         dbg(FIELD_SUBSYSTEM " is not matching");
602                         goto exit;
603                 }
604                 dbg(FIELD_SUBSYSTEM " matches");
605         }
606
607         /* walk up the chain of physical devices and find a match */
608         while (1) {
609                 /* check for matching driver */
610                 if (dev->driver[0] != '\0') {
611                         dbg("check for " FIELD_DRIVER " dev->driver='%s' sysfs_device->driver_name='%s'",
612                             dev->driver, sysfs_device->driver_name);
613                         if (strcmp_pattern(dev->driver, sysfs_device->driver_name) != 0) {
614                                 dbg(FIELD_DRIVER " is not matching");
615                                 goto try_parent;
616                         }
617                         dbg(FIELD_DRIVER " matches");
618                 }
619
620                 /* check for matching bus value */
621                 if (dev->bus[0] != '\0') {
622                         if (sysfs_device == NULL) {
623                                 dbg("device has no bus");
624                                 goto try_parent;
625                         }
626                         dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'",
627                             dev->bus, sysfs_device->bus);
628                         if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
629                                 dbg(FIELD_BUS " is not matching");
630                                 goto try_parent;
631                         }
632                         dbg(FIELD_BUS " matches");
633                 }
634
635                 /* check for matching bus id */
636                 if (dev->id[0] != '\0') {
637                         dbg("check " FIELD_ID);
638                         if (match_id(dev, class_dev, sysfs_device) != 0) {
639                                 dbg(FIELD_ID " is not matching");
640                                 goto try_parent;
641                         }
642                         dbg(FIELD_ID " matches");
643                 }
644
645                 /* check for matching place of device */
646                 if (dev->place[0] != '\0') {
647                         dbg("check " FIELD_PLACE);
648                         if (match_place(dev, class_dev, sysfs_device) != 0) {
649                                 dbg(FIELD_PLACE " is not matching");
650                                 goto try_parent;
651                         }
652                         dbg(FIELD_PLACE " matches");
653                 }
654
655                 /* check for matching sysfs pairs */
656                 if (dev->sysfs_pair[0].file[0] != '\0') {
657                         dbg("check " FIELD_SYSFS " pairs");
658                         if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
659                                 dbg(FIELD_SYSFS " is not matching");
660                                 goto try_parent;
661                         }
662                         dbg(FIELD_SYSFS " matches");
663                 }
664
665                 /* found matching physical device  */
666                 break;
667 try_parent:
668                 dbg("try parent sysfs device");
669                 sysfs_device = sysfs_get_device_parent(sysfs_device);
670                 if (sysfs_device == NULL)
671                         goto exit;
672                 dbg("sysfs_device->path='%s'", sysfs_device->path);
673                 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
674         }
675
676         /* execute external program */
677         if (dev->program[0] != '\0') {
678                 char program[PROGRAM_SIZE];
679
680                 dbg("check " FIELD_PROGRAM);
681                 strfieldcpy(program, dev->program);
682                 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
683                 if (execute_program(udev, program, udev->program_result, NAME_SIZE) != 0) {
684                         dbg(FIELD_PROGRAM " returned nonzero");
685                         goto try_parent;
686                 }
687                 dbg(FIELD_PROGRAM " returned successful");
688         }
689
690         /* check for matching result of external program */
691         if (dev->result[0] != '\0') {
692                 dbg("check for " FIELD_RESULT " dev->result='%s', udev->program_result='%s'",
693                     dev->result, udev->program_result);
694                 if (strcmp_pattern(dev->result, udev->program_result) != 0) {
695                         dbg(FIELD_RESULT " is not matching");
696                         goto try_parent;
697                 }
698                 dbg(FIELD_RESULT " matches");
699         }
700
701         /* rule matches */
702         return 0;
703
704 exit:
705         return -1;
706 }
707
708 int namedev_name_device(struct udevice *udev, struct sysfs_class_device *class_dev)
709 {
710         struct sysfs_class_device *class_dev_parent;
711         struct sysfs_device *sysfs_device = NULL;
712         struct config_device *dev;
713         char *pos;
714
715         dbg("class_dev->name='%s'", class_dev->name);
716
717         /* Figure out where the "device"-symlink is at.  For char devices this will
718          * always be in the class_dev->path.  On block devices, only the main block
719          * device will have the device symlink in it's path. All partition devices
720          * need to look at the symlink in its parent directory.
721          */
722         class_dev_parent = sysfs_get_classdev_parent(class_dev);
723         if (class_dev_parent != NULL) {
724                 dbg("given class device has a parent, use this instead");
725                 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
726         } else {
727                 sysfs_device = sysfs_get_classdev_device(class_dev);
728         }
729
730         if (sysfs_device) {
731                 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
732                     sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
733                 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
734         }
735
736         strfieldcpy(udev->kernel_name, class_dev->name);
737         fix_kernel_name(udev);
738         dbg("udev->kernel_name = '%s'", udev->kernel_name);
739
740         /* get kernel number */
741         pos = class_dev->name + strlen(class_dev->name);
742         while (isdigit(*(pos-1)))
743                 pos--;
744         strfieldcpy(udev->kernel_number, pos);
745         dbg("kernel_number='%s'", udev->kernel_number);
746
747         /* look for a matching rule to apply */
748         list_for_each_entry(dev, &config_device_list, node) {
749                 dbg("process rule");
750                 if (match_rule(udev, dev, class_dev, sysfs_device) == 0) {
751
752                         /* FIXME: remove old style ignore rule and make OPTION="ignore" mandatory */
753                         if (dev->name[0] == '\0' && dev->symlink[0] == '\0' &&
754                             dev->mode == 0000 && dev->owner[0] == '\0' && dev->group[0] == '\0' &&
755                             !dev->ignore_device && !dev->partitions && !dev->ignore_remove) {
756                                 info("configured rule in '%s[%i]' applied, '%s' is ignored",
757                                      dev->config_file, dev->config_line, udev->kernel_name);
758                                 return -1;
759                         }
760
761                         /* apply options */
762                         if (dev->ignore_device) {
763                                 info("configured rule in '%s[%i]' applied, '%s' is ignored",
764                                      dev->config_file, dev->config_line, udev->kernel_name);
765                                 return -1;
766                         }
767                         if (dev->ignore_remove) {
768                                 udev->ignore_remove = dev->ignore_remove;
769                                 dbg_parse("remove event should be ignored");
770                         }
771                         /* apply all_partitions option only at a main block device */
772                         if (dev->partitions && udev->type == BLOCK && udev->kernel_number[0] == '\0') {
773                                 udev->partitions = dev->partitions;
774                                 dbg("creation of partition nodes requested");
775                         }
776
777                         /* apply permissions */
778                         if (dev->mode != 0000) {
779                                 udev->mode = dev->mode;
780                                 dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name);
781                         }
782                         if (dev->owner[0] != '\0') {
783                                 strfieldcpy(udev->owner, dev->owner);
784                                 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
785                                 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
786                         }
787                         if (dev->group[0] != '\0') {
788                                 strfieldcpy(udev->group, dev->group);
789                                 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
790                                 dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
791                         }
792
793                         /* collect symlinks for this or the final matching rule */
794                         if (dev->symlink[0] != '\0') {
795                                 char temp[NAME_SIZE];
796
797                                 info("configured rule in '%s[%i]' applied, added symlink '%s'",
798                                      dev->config_file, dev->config_line, dev->symlink);
799                                 strfieldcpy(temp, dev->symlink);
800                                 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
801                                 if (udev->symlink[0] != '\0')
802                                         strfieldcat(udev->symlink, " ");
803                                 strfieldcat(udev->symlink, temp);
804                         }
805
806                         /* rule matches */
807                         if (dev->name[0] != '\0') {
808                                 info("configured rule in '%s[%i]' applied, '%s' becomes '%s'",
809                                      dev->config_file, dev->config_line, udev->kernel_name, dev->name);
810
811                                 strfieldcpy(udev->name, dev->name);
812                                 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
813                                 strfieldcpy(udev->config_file, dev->config_file);
814                                 udev->config_line = dev->config_line;
815
816                                 if (udev->type != NET)
817                                         dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
818                                             udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
819
820                                 goto exit;
821                         }
822                 }
823         }
824
825         /* no rule matched, so we use the kernel name */
826         strfieldcpy(udev->name, udev->kernel_name);
827         dbg("no rule found, use kernel name '%s'", udev->name);
828
829 exit:
830         if (udev->tmp_node[0] != '\0') {
831                 dbg("removing temporary device node");
832                 unlink_secure(udev->tmp_node);
833                 udev->tmp_node[0] = '\0';
834         }
835
836         return 0;
837 }