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