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