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