chiark / gitweb /
[PATCH] remove udevsend syslog noise on udevd startup
[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 'b':
205                         if (strlen(udev->bus_id) == 0)
206                                 break;
207                         strfieldcatmax(string, udev->bus_id, maxsize);
208                         dbg("substitute bus_id '%s'", udev->bus_id);
209                         break;
210                 case 'k':
211                         if (strlen(udev->kernel_name) == 0)
212                                 break;
213                         strfieldcatmax(string, udev->kernel_name, maxsize);
214                         dbg("substitute kernel name '%s'", udev->kernel_name);
215                         break;
216                 case 'n':
217                         if (strlen(udev->kernel_number) == 0)
218                                 break;
219                         strfieldcatmax(string, udev->kernel_number, maxsize);
220                         dbg("substitute kernel number '%s'", udev->kernel_number);
221                                 break;
222                 case 'm':
223                         strintcatmax(string, udev->minor, maxsize);
224                         dbg("substitute minor number '%u'", udev->minor);
225                         break;
226                 case 'M':
227                         strintcatmax(string, udev->major, maxsize);
228                         dbg("substitute major number '%u'", udev->major);
229                         break;
230                 case 'c':
231                         if (strlen(udev->program_result) == 0)
232                                 break;
233                         /* get part part of the result string */
234                         i = 0;
235                         if (attr != NULL)
236                                 i = strtoul(attr, &rest, 10);
237                         if (i > 0) {
238                                 foreach_strpart(udev->program_result, " \n\r", spos, slen) {
239                                         i--;
240                                         if (i == 0)
241                                                 break;
242                                 }
243                                 if (i > 0) {
244                                         dbg("requested part of result string not found");
245                                         break;
246                                 }
247                                 if (rest[0] == '+')
248                                         strfieldcpy(temp2, spos);
249                                 else
250                                         strfieldcpymax(temp2, spos, slen+1);
251                                 strfieldcatmax(string, temp2, maxsize);
252                                 dbg("substitute part of result string '%s'", temp2);
253                         } else {
254                                 strfieldcatmax(string, udev->program_result, maxsize);
255                                 dbg("substitute result string '%s'", udev->program_result);
256                         }
257                         break;
258                 case 's':
259                         if (attr != NULL) {
260                                 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr);
261                                 if (tmpattr == NULL) {
262                                         dbg("sysfa attribute '%s' not found", attr);
263                                         break;
264                                 }
265                                 /* strip trailing whitespace of matching value */
266                                 if (isspace(tmpattr->value[strlen(tmpattr->value)-1])) {
267                                         i = len = strlen(tmpattr->value);
268                                         while (i > 0 &&  isspace(tmpattr->value[i-1]))
269                                                 i--;
270                                         if (i < len) {
271                                                 tmpattr->value[i] = '\0';
272                                                 dbg("remove %i trailing whitespace chars from '%s'",
273                                                          len - i, tmpattr->value);
274                                         }
275                                 }
276                                 strfieldcatmax(string, tmpattr->value, maxsize);
277                                 dbg("substitute sysfs value '%s'", tmpattr->value);
278                         } else {
279                                 dbg("missing attribute");
280                         }
281                         break;
282                 case '%':
283                         strfieldcatmax(string, "%", maxsize);
284                         pos++;
285                         break;
286                 case 'e':
287                         next_free_number = find_free_number(udev, string);
288                         if (next_free_number > 0) {
289                                 sprintf(temp2, "%d", next_free_number);
290                                 strfieldcatmax(string, temp2, maxsize);
291                         }
292                         break;
293                 default:
294                         dbg("unknown substitution type '%%%c'", c);
295                         break;
296                 }
297                 /* truncate to specified length */
298                 if (len > 0)
299                         pos[len] = '\0';
300
301                 strfieldcatmax(string, tail, maxsize);
302         }
303 }
304
305 static void fix_kernel_name(struct udevice *udev)
306 {
307         char *temp = udev->kernel_name;
308
309         while (*temp != 0x00) {
310                 /* Some block devices have a ! in their name, 
311                  * we need to change that to / */
312                 if (*temp == '!')
313                         *temp = '/';
314                 ++temp;
315         }
316 }
317
318 static int execute_program(struct udevice *udev, const char *path, char *value, int len)
319 {
320         int retval;
321         int count;
322         int status;
323         int fds[2];
324         pid_t pid;
325         char *pos;
326         char arg[PROGRAM_SIZE];
327         char *argv[(PROGRAM_SIZE / 2) + 1];
328         int i;
329
330         strfieldcpy(arg, path);
331         i = 0;
332         if (strchr(path, ' ')) {
333                 pos = arg;
334                 while (pos != NULL) {
335                         if (pos[0] == '\'') {
336                                 /* don't separate if in apostrophes */
337                                 pos++;
338                                 argv[i] = strsep(&pos, "\'");
339                                 while (pos && pos[0] == ' ')
340                                         pos++;
341                         } else {
342                                 argv[i] = strsep(&pos, " ");
343                         }
344                         dbg("arg[%i] '%s'", i, argv[i]);
345                         i++;
346                 }
347                 argv[i] =  NULL;
348                 dbg("execute '%s' with parsed arguments", arg);
349         } else {
350                 argv[0] = arg;
351                 argv[1] = udev->subsystem;
352                 argv[2] = NULL;
353                 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
354         }
355
356         retval = pipe(fds);
357         if (retval != 0) {
358                 dbg("pipe failed");
359                 return -1;
360         }
361
362         pid = fork();
363         switch(pid) {
364         case 0:
365                 /* child */
366                 /* dup2 write side of pipe to STDOUT */
367                 dup2(fds[1], STDOUT_FILENO);
368                 retval = execv(arg, argv);
369
370                 info(FIELD_PROGRAM " execution of '%s' failed", path);
371                 exit(1);
372         case -1:
373                 dbg("fork failed");
374                 return -1;
375         default:
376                 /* parent reads from fds[0] */
377                 close(fds[1]);
378                 retval = 0;
379                 i = 0;
380                 while (1) {
381                         count = read(fds[0], value + i, len - i-1);
382                         if (count <= 0)
383                                 break;
384
385                         i += count;
386                         if (i >= len-1) {
387                                 dbg("result len %d too short", len);
388                                 retval = -1;
389                                 break;
390                         }
391                 }
392
393                 if (count < 0) {
394                         dbg("read failed with '%s'", strerror(errno));
395                         retval = -1;
396                 }
397
398                 if (i > 0 && value[i-1] == '\n')
399                         i--;
400                 value[i] = '\0';
401                 dbg("result is '%s'", value);
402
403                 close(fds[0]);
404                 waitpid(pid, &status, 0);
405
406                 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
407                         dbg("exec program status 0x%x", status);
408                         retval = -1;
409                 }
410         }
411         return retval;
412 }
413
414 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
415 {
416         struct sysfs_attribute *tmpattr = NULL;
417         char *c;
418
419         dbg("look for device attribute '%s'", attr);
420         /* try to find the attribute in the class device directory */
421         tmpattr = sysfs_get_classdev_attr(class_dev, attr);
422         if (tmpattr)
423                 goto attr_found;
424
425         /* look in the class device directory if present */
426         if (sysfs_device) {
427                 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
428                 if (tmpattr)
429                         goto attr_found;
430         }
431
432         return NULL;
433
434 attr_found:
435         c = strchr(tmpattr->value, '\n');
436         if (c != NULL)
437                 c[0] = '\0';
438
439         dbg("found attribute '%s'", tmpattr->path);
440         return tmpattr;
441 }
442
443 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
444 {
445         struct sysfs_attribute *tmpattr;
446         int i;
447         int len;
448
449         if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
450                 return -ENODEV;
451
452         tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file);
453         if (tmpattr == NULL)
454                 return -ENODEV;
455
456         /* strip trailing whitespace of value, if not asked to match for it */
457         if (! isspace(pair->value[strlen(pair->value)-1])) {
458                 i = len = strlen(tmpattr->value);
459                 while (i > 0 &&  isspace(tmpattr->value[i-1]))
460                         i--;
461                 if (i < len) {
462                         tmpattr->value[i] = '\0';
463                         dbg("remove %i trailing whitespace chars from '%s'",
464                             len - i, tmpattr->value);
465                 }
466         }
467
468         dbg("compare attribute '%s' value '%s' with '%s'",
469                   pair->file, tmpattr->value, pair->value);
470         if (strcmp_pattern(pair->value, tmpattr->value) != 0)
471                 return -ENODEV;
472
473         dbg("found matching attribute '%s' with value '%s'",
474             pair->file, pair->value);
475         return 0;
476 }
477
478 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
479 {
480         struct sysfs_pair *pair;
481         int i;
482
483         for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
484                 pair = &dev->sysfs_pair[i];
485                 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
486                         break;
487                 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
488                         dbg("sysfs attribute doesn't match");
489                         return -ENODEV;
490                 }
491         }
492
493         return 0;
494 }
495
496 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
497 {
498         char path[SYSFS_PATH_MAX];
499         char *temp = NULL;
500
501         /* we have to have a sysfs device for ID to work */
502         if (!sysfs_device)
503                 return -ENODEV;
504
505         strfieldcpy(path, sysfs_device->path);
506         temp = strrchr(path, '/');
507         temp++;
508         dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
509         if (strcmp_pattern(dev->id, temp) != 0)
510                 return -ENODEV;
511         else
512                 return 0;
513 }
514
515 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
516 {
517         char path[SYSFS_PATH_MAX];
518         int found;
519         char *temp = NULL;
520
521         /* we have to have a sysfs device for PLACE to work */
522         if (!sysfs_device)
523                 return -ENODEV;
524
525         found = 0;
526         strfieldcpy(path, sysfs_device->path);
527         temp = strrchr(path, '/');
528         dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
529         if (strstr(temp, dev->place) != NULL) {
530                 found = 1;
531         } else {
532                 *temp = 0x00;
533                 temp = strrchr(path, '/');
534                 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
535                 if (strstr(temp, dev->place) != NULL)
536                         found = 1;
537         }
538         if (!found) {
539                 dbg("place doesn't match");
540                 return -ENODEV;
541         }
542
543         return 0;
544 }
545
546 static int match_rule(struct udevice *udev, struct config_device *dev,
547                       struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
548 {
549         while (1) {
550                 /* check for matching bus value */
551                 if (dev->bus[0] != '\0') {
552                         if (sysfs_device == NULL) {
553                                 dbg("device has no bus");
554                                 goto try_parent;
555                         }
556                         dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'",
557                             dev->bus, sysfs_device->bus);
558                         if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
559                                 dbg(FIELD_BUS " is not matching");
560                                 goto try_parent;
561                         } else {
562                                 dbg(FIELD_BUS " matches");
563                         }
564                 }
565
566                 /* check for matching kernel name */
567                 if (dev->kernel[0] != '\0') {
568                         dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'",
569                             dev->kernel, class_dev->name);
570                         if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
571                                 dbg(FIELD_KERNEL " is not matching");
572                                 goto try_parent;
573                         } else {
574                                 dbg(FIELD_KERNEL " matches");
575                         }
576                 }
577
578                 /* check for matching subsystem */
579                 if (dev->subsystem[0] != '\0') {
580                         dbg("check for " FIELD_SUBSYSTEM " dev->subsystem='%s' class_dev->name='%s'",
581                             dev->subsystem, class_dev->name);
582                         if (strcmp_pattern(dev->subsystem, udev->subsystem) != 0) {
583                                 dbg(FIELD_SUBSYSTEM " is not matching");
584                                 goto try_parent;
585                         } else {
586                                 dbg(FIELD_SUBSYSTEM " matches");
587                         }
588                 }
589
590                 /* check for matching driver */
591                 if (dev->driver[0] != '\0') {
592                         dbg("check for " FIELD_DRIVER " dev->driver='%s' sysfs_device->driver_name='%s'",
593                             dev->driver, sysfs_device->driver_name);
594                         if (strcmp_pattern(dev->driver, sysfs_device->driver_name) != 0) {
595                                 dbg(FIELD_DRIVER " is not matching");
596                                 goto try_parent;
597                         } else {
598                                 dbg(FIELD_DRIVER " matches");
599                         }
600                 }
601
602                 /* check for matching bus id */
603                 if (dev->id[0] != '\0') {
604                         dbg("check " FIELD_ID);
605                         if (match_id(dev, class_dev, sysfs_device) != 0) {
606                                 dbg(FIELD_ID " is not matching");
607                                 goto try_parent;
608                         } else {
609                                 dbg(FIELD_ID " matches");
610                         }
611                 }
612
613                 /* check for matching place of device */
614                 if (dev->place[0] != '\0') {
615                         dbg("check " FIELD_PLACE);
616                         if (match_place(dev, class_dev, sysfs_device) != 0) {
617                                 dbg(FIELD_PLACE " is not matching");
618                                 goto try_parent;
619                         } else {
620                                 dbg(FIELD_PLACE " matches");
621                         }
622                 }
623
624                 /* check for matching sysfs pairs */
625                 if (dev->sysfs_pair[0].file[0] != '\0') {
626                         dbg("check " FIELD_SYSFS " pairs");
627                         if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
628                                 dbg(FIELD_SYSFS " is not matching");
629                                 goto try_parent;
630                         } else {
631                                 dbg(FIELD_SYSFS " matches");
632                         }
633                 }
634
635                 /* execute external program */
636                 if (dev->program[0] != '\0') {
637                         char program[PROGRAM_SIZE];
638
639                         dbg("check " FIELD_PROGRAM);
640                         strfieldcpy(program, dev->program);
641                         apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
642                         if (execute_program(udev, program, udev->program_result, NAME_SIZE) != 0) {
643                                 dbg(FIELD_PROGRAM " returned nonzero");
644                                 goto try_parent;
645                         } else {
646                                 dbg(FIELD_PROGRAM " returned successful");
647                         }
648                 }
649
650                 /* check for matching result of external program */
651                 if (dev->result[0] != '\0') {
652                         dbg("check for " FIELD_RESULT " dev->result='%s', udev->program_result='%s'",
653                             dev->result, udev->program_result);
654                         if (strcmp_pattern(dev->result, udev->program_result) != 0) {
655                                 dbg(FIELD_RESULT " is not matching");
656                                 goto try_parent;
657                         } else {
658                                 dbg(FIELD_RESULT " matches");
659                         }
660                 }
661
662                 /* Yeah, we matched! */
663                 return 0;
664
665 try_parent:
666                 dbg("try parent sysfs device");
667                 sysfs_device = sysfs_get_device_parent(sysfs_device);
668                 if (sysfs_device == NULL)
669                         return -ENODEV;
670                 dbg("sysfs_device->path='%s'", sysfs_device->path);
671                 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
672         }
673
674 }
675
676 int namedev_name_device(struct udevice *udev, struct sysfs_class_device *class_dev)
677 {
678         struct sysfs_class_device *class_dev_parent;
679         struct sysfs_device *sysfs_device = NULL;
680         struct config_device *dev;
681         char *pos;
682
683         dbg("class_dev->name='%s'", class_dev->name);
684
685         /* Figure out where the "device"-symlink is at.  For char devices this will
686          * always be in the class_dev->path.  On block devices, only the main block
687          * device will have the device symlink in it's path. All partition devices
688          * need to look at the symlink in its parent directory.
689          */
690         class_dev_parent = sysfs_get_classdev_parent(class_dev);
691         if (class_dev_parent != NULL) {
692                 dbg("given class device has a parent, use this instead");
693                 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
694         } else {
695                 sysfs_device = sysfs_get_classdev_device(class_dev);
696         }
697
698         if (sysfs_device) {
699                 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
700                     sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
701                 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
702         }
703
704         strfieldcpy(udev->kernel_name, class_dev->name);
705         fix_kernel_name(udev);
706         dbg("udev->kernel_name = '%s'", udev->kernel_name);
707
708         /* get kernel number */
709         pos = class_dev->name + strlen(class_dev->name);
710         while (isdigit(*(pos-1)))
711                 pos--;
712         strfieldcpy(udev->kernel_number, pos);
713         dbg("kernel_number='%s'", udev->kernel_number);
714
715         /* look for a matching rule to apply */
716         list_for_each_entry(dev, &config_device_list, node) {
717                 dbg("process rule");
718                 if (match_rule(udev, dev, class_dev, sysfs_device) == 0) {
719                         if (dev->name[0] == '\0' && dev->symlink[0] == '\0') {
720                                 /* empty name, symlink, perms will not create any node */
721                                 if (dev->mode == 0000 && dev->owner[0] == '\0' && dev->group[0] == '\0') {
722                                         info("configured rule in '%s[%i]' applied, '%s' is ignored",
723                                              dev->config_file, dev->config_line, udev->kernel_name);
724                                         return -1;
725                                 }
726
727                                 /* apply permissions only rule values */
728                                 if (dev->mode != 0000) {
729                                         udev->mode = dev->mode;
730                                         dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name);
731                                 }
732                                 if (dev->owner[0] != '\0') {
733                                         strfieldcpy(udev->owner, dev->owner);
734                                         apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
735                                         dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
736                                 }
737                                 if (dev->group[0] != '\0') {
738                                         strfieldcpy(udev->group, dev->group);
739                                         apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
740                                         dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
741                                 }
742                         }
743
744                         /* collect symlinks for the final matching rule */
745                         if (dev->symlink[0] != '\0') {
746                                 char temp[NAME_SIZE];
747
748                                 info("configured rule in '%s[%i]' applied, added symlink '%s'",
749                                      dev->config_file, dev->config_line, dev->symlink);
750                                 strfieldcpy(temp, dev->symlink);
751                                 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
752                                 if (udev->symlink[0] != '\0')
753                                         strfieldcat(udev->symlink, " ");
754                                 strfieldcat(udev->symlink, temp);
755                         }
756
757                         /* rule matches */
758                         if (dev->name[0] != '\0') {
759                                 /* apply all_partitions flag only at a main block device */
760                                 if (dev->partitions > 0 &&
761                                     (udev->type != 'b' || udev->kernel_number[0] != '\0'))
762                                         continue;
763
764                                 info("configured rule in '%s[%i]' applied, '%s' becomes '%s'",
765                                      dev->config_file, dev->config_line, udev->kernel_name, dev->name);
766
767                                 strfieldcpy(udev->name, dev->name);
768                                 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
769                                 strfieldcpy(udev->config_file, dev->config_file);
770                                 udev->config_line = dev->config_line;
771                                 udev->ignore_remove = dev->ignore_remove;
772
773                                 if (udev->type == 'n')
774                                         goto exit;
775
776                                 udev->partitions = dev->partitions;
777                                 if (dev->mode != 0000)
778                                         udev->mode = dev->mode;
779                                 if (dev->owner[0] != '\0') {
780                                         strfieldcpy(udev->owner, dev->owner);
781                                         apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
782                                 }
783                                 if (dev->group[0] != '\0') {
784                                         strfieldcpy(udev->group, dev->group);
785                                         apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
786                                 }
787
788                                 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
789                                     udev->name, udev->owner, udev->group, udev->mode);
790
791                                 goto exit;
792                         }
793                 }
794         }
795
796         /* no rule matched, so we use the kernel name */
797         strfieldcpy(udev->name, udev->kernel_name);
798         dbg("no rule found, use kernel name '%s'", udev->name);
799
800 exit:
801         return 0;
802 }