]> git.ipfire.org Git - thirdparty/systemd.git/blob - namedev.c
[PATCH] add udev logging to info log
[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 <time.h>
33 #include <sys/wait.h>
34 #include <sys/stat.h>
35
36 #include "list.h"
37 #include "udev.h"
38 #include "udev_version.h"
39 #include "logging.h"
40 #include "namedev.h"
41 #include "libsysfs/libsysfs.h"
42 #include "klibc_fixups.h"
43
44 LIST_HEAD(config_device_list);
45 LIST_HEAD(perm_device_list);
46
47 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
48 static int strcmp_pattern(const char *p, const char *s)
49 {
50 if (*s == '\0') {
51 while (*p == '*')
52 p++;
53 return (*p != '\0');
54 }
55 switch (*p) {
56 case '[':
57 {
58 int not = 0;
59 p++;
60 if (*p == '!') {
61 not = 1;
62 p++;
63 }
64 while (*p && (*p != ']')) {
65 int match = 0;
66 if (p[1] == '-') {
67 if ((*s >= *p) && (*s <= p[2]))
68 match = 1;
69 p += 3;
70 } else {
71 match = (*p == *s);
72 p++;
73 }
74 if (match ^ not) {
75 while (*p && (*p != ']'))
76 p++;
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') {
88 return 0;
89 }
90 break;
91 default:
92 if ((*p == *s) || (*p == '?'))
93 return strcmp_pattern(p+1, s+1);
94 break;
95 }
96 return 1;
97 }
98
99 #define copy_var(a, b, var) \
100 if (b->var) \
101 a->var = b->var;
102
103 #define copy_string(a, b, var) \
104 if (strlen(b->var)) \
105 strcpy(a->var, b->var);
106
107 int add_perm_dev(struct perm_device *new_dev)
108 {
109 struct perm_device *dev;
110 struct perm_device *tmp_dev;
111
112 /* update the values if we already have the device */
113 list_for_each_entry(dev, &perm_device_list, node) {
114 if (strcmp_pattern(new_dev->name, dev->name))
115 continue;
116 copy_var(dev, new_dev, mode);
117 copy_string(dev, new_dev, owner);
118 copy_string(dev, new_dev, group);
119 return 0;
120 }
121
122 /* not found, add new structure to the perm list */
123 tmp_dev = malloc(sizeof(*tmp_dev));
124 if (!tmp_dev)
125 return -ENOMEM;
126 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
127 list_add_tail(&tmp_dev->node, &perm_device_list);
128 //dump_perm_dev(tmp_dev);
129 return 0;
130 }
131
132 static struct perm_device *find_perm(char *name)
133 {
134 struct perm_device *perm;
135
136 list_for_each_entry(perm, &perm_device_list, node) {
137 if (strcmp_pattern(perm->name, name))
138 continue;
139 return perm;
140 }
141 return NULL;
142 }
143
144 static mode_t get_default_mode(struct sysfs_class_device *class_dev)
145 {
146 mode_t mode = 0600; /* default to owner rw only */
147
148 if (strlen(default_mode_str) != 0) {
149 mode = strtol(default_mode_str, NULL, 8);
150 }
151 return mode;
152 }
153
154 static void apply_format(struct udevice *udev, unsigned char *string)
155 {
156 char temp[NAME_SIZE];
157 char temp1[NAME_SIZE];
158 char *tail;
159 char *pos;
160 char *pos2;
161 char *pos3;
162 int num;
163
164 pos = string;
165 while (1) {
166 num = 0;
167 pos = strchr(pos, '%');
168
169 if (pos) {
170 pos[0] = '\0';
171 tail = pos+1;
172 if (isdigit(tail[0])) {
173 num = (int) strtoul(&pos[1], &tail, 10);
174 if (tail == NULL) {
175 dbg("format parsing error '%s'", pos+1);
176 break;
177 }
178 }
179 strfieldcpy(temp, tail+1);
180
181 switch (tail[0]) {
182 case 'b':
183 if (strlen(udev->bus_id) == 0)
184 break;
185 strcat(pos, udev->bus_id);
186 dbg("substitute bus_id '%s'", udev->bus_id);
187 break;
188 case 'k':
189 if (strlen(udev->kernel_name) == 0)
190 break;
191 strcat(pos, udev->kernel_name);
192 dbg("substitute kernel name '%s'", udev->kernel_name);
193 break;
194 case 'n':
195 if (strlen(udev->kernel_number) == 0)
196 break;
197 strcat(pos, udev->kernel_number);
198 dbg("substitute kernel number '%s'", udev->kernel_number);
199 break;
200 case 'm':
201 sprintf(pos, "%u", udev->minor);
202 dbg("substitute minor number '%u'", udev->minor);
203 break;
204 case 'M':
205 sprintf(pos, "%u", udev->major);
206 dbg("substitute major number '%u'", udev->major);
207 break;
208 case 'c':
209 if (strlen(udev->program_result) == 0)
210 break;
211 if (num) {
212 /* get part of return string */
213 strncpy(temp1, udev->program_result, sizeof(temp1));
214 pos2 = temp1;
215 while (num) {
216 num--;
217 pos3 = strsep(&pos2, " ");
218 if (pos3 == NULL) {
219 dbg("requested part of result string not found");
220 break;
221 }
222 }
223 if (pos3) {
224 strcat(pos, pos3);
225 dbg("substitute part of result string '%s'", pos3);
226 }
227 } else {
228 strcat(pos, udev->program_result);
229 dbg("substitute result string '%s'", udev->program_result);
230 }
231 break;
232 case '%':
233 strcat(pos, "%");
234 pos++;
235 break;
236 default:
237 dbg("unknown substitution type '%%%c'", pos[1]);
238 break;
239 }
240 strcat(string, temp);
241 } else
242 break;
243 }
244 }
245
246 static struct bus_file {
247 char *bus;
248 char *file;
249 } bus_files[] = {
250 { .bus = "scsi", .file = "vendor" },
251 { .bus = "usb", .file = "idVendor" },
252 { .bus = "usb-serial", .file = "detach_state" },
253 { .bus = "ide", .file = "detach_state" },
254 { .bus = "pci", .file = "vendor" },
255 {}
256 };
257
258 #define SECONDS_TO_WAIT_FOR_FILE 10
259 static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device)
260 {
261 /* sleep until we see the file for this specific bus type show up this
262 * is needed because we can easily out-run the kernel in looking for
263 * these files before the paticular subsystem has created them in the
264 * sysfs tree properly.
265 *
266 * And people thought that the /sbin/hotplug event system was going to
267 * be slow, poo on you for arguing that before even testing it...
268 */
269 struct bus_file *b = &bus_files[0];
270 struct sysfs_attribute *tmpattr;
271 int loop;
272
273 while (1) {
274 if (b->bus == NULL)
275 break;
276 if (strcmp(sysfs_device->bus, b->bus) == 0) {
277 tmpattr = NULL;
278 loop = SECONDS_TO_WAIT_FOR_FILE;
279 while (loop--) {
280 dbg("looking for file '%s' on bus '%s'", b->file, b->bus);
281 tmpattr = sysfs_get_device_attr(sysfs_device, b->file);
282 if (tmpattr) {
283 /* found it! */
284 goto exit;
285 }
286 /* sleep to give the kernel a chance to create the file */
287 sleep(1);
288 }
289 dbg("timed out waiting for '%s' file, continuing on anyway...", b->file);
290 goto exit;
291 }
292 b++;
293 }
294 dbg("did not find bus type '%s' on list of bus_id_files, contact greg@kroah.com", sysfs_device->bus);
295 exit:
296 return; /* here to prevent compiler warning... */
297 }
298
299 static int execute_program(char *path, char *value, int len)
300 {
301 int retval;
302 int res;
303 int status;
304 int fds[2];
305 pid_t pid;
306 int value_set = 0;
307 char buffer[256];
308 char *pos;
309 char *args[PROGRAM_MAXARG];
310 int i;
311
312 dbg("executing '%s'", path);
313 retval = pipe(fds);
314 if (retval != 0) {
315 dbg("pipe failed");
316 return -1;
317 }
318 pid = fork();
319 if (pid == -1) {
320 dbg("fork failed");
321 return -1;
322 }
323
324 if (pid == 0) {
325 /* child */
326 close(STDOUT_FILENO);
327 dup(fds[1]); /* dup write side of pipe to STDOUT */
328 if (strchr(path, ' ')) {
329 /* exec with arguments */
330 pos = path;
331 for (i=0; i < PROGRAM_MAXARG-1; i++) {
332 args[i] = strsep(&pos, " ");
333 if (args[i] == NULL)
334 break;
335 }
336 if (args[i]) {
337 dbg("too many args - %d", i);
338 args[i] = NULL;
339 }
340 retval = execve(args[0], args, main_envp);
341 } else {
342 retval = execve(path, main_argv, main_envp);
343 }
344 if (retval != 0) {
345 dbg("child execve failed");
346 exit(1);
347 }
348 return -1; /* avoid compiler warning */
349 } else {
350 /* parent reads from fds[0] */
351 close(fds[1]);
352 retval = 0;
353 while (1) {
354 res = read(fds[0], buffer, sizeof(buffer) - 1);
355 if (res <= 0)
356 break;
357 buffer[res] = '\0';
358 if (res > len) {
359 dbg("result len %d too short", len);
360 retval = -1;
361 }
362 if (value_set) {
363 dbg("result value already set");
364 retval = -1;
365 } else {
366 value_set = 1;
367 strncpy(value, buffer, len);
368 pos = value + strlen(value)-1;
369 if (pos[0] == '\n')
370 pos[0] = '\0';
371 dbg("result is '%s'", value);
372 }
373 }
374 close(fds[0]);
375 res = wait(&status);
376 if (res < 0) {
377 dbg("wait failed result %d", res);
378 retval = -1;
379 }
380
381 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
382 dbg("exec program status 0x%x", status);
383 retval = -1;
384 }
385 }
386 return retval;
387 }
388
389 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
390 {
391 struct sysfs_attribute *tmpattr = NULL;
392 char *c;
393
394 if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
395 return -ENODEV;
396
397 dbg("look for device attribute '%s'", pair->file);
398 /* try to find the attribute in the class device directory */
399 tmpattr = sysfs_get_classdev_attr(class_dev, pair->file);
400 if (tmpattr)
401 goto label_found;
402
403 /* look in the class device directory if present */
404 if (sysfs_device) {
405 tmpattr = sysfs_get_device_attr(sysfs_device, pair->file);
406 if (tmpattr)
407 goto label_found;
408 }
409 return -ENODEV;
410
411 label_found:
412 c = tmpattr->value + strlen(tmpattr->value)-1;
413 if (*c == '\n')
414 *c = 0x00;
415 dbg("compare attribute '%s' value '%s' with '%s'",
416 pair->file, tmpattr->value, pair->value);
417 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
418 return -ENODEV;
419
420 dbg("found matching attribute '%s' with value '%s'",
421 pair->file, pair->value);
422 return 0;
423 }
424
425 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
426 {
427 struct sysfs_pair *pair;
428 int i;
429
430 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
431 pair = &dev->sysfs_pair[i];
432 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
433 break;
434 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
435 dbg("sysfs attribute doesn't match");
436 return -ENODEV;
437 }
438 }
439
440 return 0;
441 }
442
443 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
444 {
445 char path[SYSFS_PATH_MAX];
446 int found;
447 char *temp = NULL;
448
449 /* we have to have a sysfs device for ID to work */
450 if (!sysfs_device)
451 return -ENODEV;
452
453 found = 0;
454 strfieldcpy(path, sysfs_device->path);
455 temp = strrchr(path, '/');
456 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
457 if (strstr(temp, dev->id) != NULL) {
458 found = 1;
459 } else {
460 *temp = 0x00;
461 temp = strrchr(path, '/');
462 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
463 if (strstr(temp, dev->id) != NULL)
464 found = 1;
465 }
466 if (!found) {
467 dbg("id doesn't match");
468 return -ENODEV;
469 }
470
471 return 0;
472 }
473
474 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
475 {
476 char path[SYSFS_PATH_MAX];
477 int found;
478 char *temp = NULL;
479
480 /* we have to have a sysfs device for PLACE to work */
481 if (!sysfs_device)
482 return -ENODEV;
483
484 found = 0;
485 strfieldcpy(path, sysfs_device->path);
486 temp = strrchr(path, '/');
487 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
488 if (strstr(temp, dev->place) != NULL) {
489 found = 1;
490 } else {
491 *temp = 0x00;
492 temp = strrchr(path, '/');
493 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
494 if (strstr(temp, dev->place) != NULL)
495 found = 1;
496 }
497 if (!found) {
498 dbg("place doesn't match");
499 return -ENODEV;
500 }
501
502 return 0;
503 }
504
505 static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev)
506 {
507 struct sysfs_device *sysfs_device;
508 struct sysfs_class_device *class_dev_parent;
509 struct timespec tspec;
510 int loop;
511
512 /* Figure out where the device symlink is at. For char devices this will
513 * always be in the class_dev->path. But for block devices, it's different.
514 * The main block device will have the device symlink in it's path, but
515 * all partitions have the symlink in its parent directory.
516 * But we need to watch out for block devices that do not have parents, yet
517 * look like a partition (fd0, loop0, etc.) They all do not have a device
518 * symlink yet. We do sit and spin on waiting for them right now, we should
519 * possibly have a whitelist for these devices here...
520 */
521 class_dev_parent = sysfs_get_classdev_parent(class_dev);
522 if (class_dev_parent)
523 dbg("Really a partition");
524
525 tspec.tv_sec = 0;
526 tspec.tv_nsec = 10000000; /* sleep 10 millisec */
527 loop = 10;
528 while (loop--) {
529 nanosleep(&tspec, NULL);
530 if (class_dev_parent)
531 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
532 else
533 sysfs_device = sysfs_get_classdev_device(class_dev);
534
535 if (sysfs_device != NULL)
536 goto device_found;
537 }
538 dbg("timed out waiting for device symlink, continuing on anyway...");
539
540 device_found:
541 /* We have another issue with just the wait above - the sysfs part of
542 * the kernel may not be quick enough to have created the link to the
543 * device under the "bus" subsystem. Due to this, the sysfs_device->bus
544 * will not contain the actual bus name :(
545 *
546 * Libsysfs now provides a new API sysfs_get_device_bus(), so use it
547 * if needed
548 */
549 if (sysfs_device) {
550
551 if (sysfs_device->bus[0] != '\0')
552 goto bus_found;
553
554 loop = 10;
555 tspec.tv_nsec = 10000000;
556 while (loop--) {
557 nanosleep(&tspec, NULL);
558 sysfs_get_device_bus(sysfs_device);
559
560 if (sysfs_device->bus[0] != '\0')
561 goto bus_found;
562 }
563 dbg("timed out waiting to find the device bus, continuing on anyway");
564 goto exit;
565 bus_found:
566 dbg("device %s is registered with bus '%s'",
567 sysfs_device->name, sysfs_device->bus);
568 }
569 exit:
570 return sysfs_device;
571 }
572
573 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
574 {
575 struct sysfs_device *sysfs_device = NULL;
576 struct config_device *dev;
577 struct perm_device *perm;
578 char *pos;
579
580 udev->mode = 0;
581
582 /* find the sysfs_device associated with this class device */
583 sysfs_device = get_sysfs_device(class_dev);
584 if (sysfs_device) {
585 dbg("sysfs_device->path='%s'", sysfs_device->path);
586 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
587 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
588 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
589 wait_for_device_to_initialize(sysfs_device);
590 } else {
591 dbg("class_dev->name = '%s'", class_dev->name);
592 }
593
594 strfieldcpy(udev->kernel_name, class_dev->name);
595
596 /* get kernel number */
597 pos = class_dev->name + strlen(class_dev->name);
598 while (isdigit(*(pos-1)))
599 pos--;
600 strfieldcpy(udev->kernel_number, pos);
601 dbg("kernel_number='%s'", udev->kernel_number);
602
603 /* look for a matching rule to apply */
604 list_for_each_entry(dev, &config_device_list, node) {
605 dbg("process rule");
606
607 /* check for matching bus value */
608 if (dev->bus[0] != '\0') {
609 if (sysfs_device == NULL) {
610 dbg("device has no bus");
611 continue;
612 }
613 dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
614 if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
615 dbg(FIELD_BUS " is not matching");
616 continue;
617 } else {
618 dbg(FIELD_BUS " matches");
619 }
620 }
621
622 /* check for matching kernel name*/
623 if (dev->kernel[0] != '\0') {
624 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'", dev->kernel, class_dev->name);
625 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
626 dbg(FIELD_KERNEL " is not matching");
627 continue;
628 } else {
629 dbg(FIELD_KERNEL " matches");
630 }
631 }
632
633 /* check for matching bus id */
634 if (dev->id[0] != '\0') {
635 dbg("check " FIELD_ID);
636 if (match_id(dev, class_dev, sysfs_device) != 0) {
637 dbg(FIELD_ID " is not matching");
638 continue;
639 } else {
640 dbg(FIELD_ID " matches");
641 }
642 }
643
644 /* check for matching place of device */
645 if (dev->place[0] != '\0') {
646 dbg("check " FIELD_PLACE);
647 if (match_place(dev, class_dev, sysfs_device) != 0) {
648 dbg(FIELD_PLACE " is not matching");
649 continue;
650 } else {
651 dbg(FIELD_PLACE " matches");
652 }
653 }
654
655 /* check for matching sysfs pairs */
656 if (dev->sysfs_pair[0].file[0] != '\0') {
657 dbg("check " FIELD_SYSFS " pairs");
658 if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
659 dbg(FIELD_SYSFS " is not matching");
660 continue;
661 } else {
662 dbg(FIELD_SYSFS " matches");
663 }
664 }
665
666 /* execute external program */
667 if (dev->program[0] != '\0') {
668 dbg("check " FIELD_PROGRAM);
669 apply_format(udev, dev->program);
670 if (execute_program(dev->program, udev->program_result, NAME_SIZE) != 0) {
671 dbg(FIELD_PROGRAM " returned nozero");
672 continue;
673 } else {
674 dbg(FIELD_PROGRAM " returned successful");
675 }
676 }
677
678 /* check for matching result of external program */
679 if (dev->result[0] != '\0') {
680 dbg("check for " FIELD_RESULT
681 " dev->result='%s', udev->program_result='%s'",
682 dev->result, udev->program_result);
683 if (strcmp_pattern(dev->result, udev->program_result) != 0) {
684 dbg(FIELD_RESULT " is not matching");
685 continue;
686 } else {
687 dbg(FIELD_RESULT " matches");
688 }
689 }
690
691 /* check if we are instructed to ignore this device */
692 if (dev->name[0] == '\0') {
693 dbg("instructed to ignore this device");
694 return -1;
695 }
696
697 /* Yup, this rule belongs to us! */
698 info("configured rule in '%s' at line %i applied, '%s' becomes '%s'",
699 udev_rules_filename, dev->config_line, udev->kernel_name, dev->name);
700 strfieldcpy(udev->name, dev->name);
701 strfieldcpy(udev->symlink, dev->symlink);
702 goto found;
703 }
704
705 /* no rule was found so we use the kernel name */
706 strfieldcpy(udev->name, class_dev->name);
707 goto done;
708
709 found:
710 /* substitute placeholder */
711 apply_format(udev, udev->name);
712 apply_format(udev, udev->symlink);
713
714 done:
715 perm = find_perm(udev->name);
716 if (perm) {
717 udev->mode = perm->mode;
718 strfieldcpy(udev->owner, perm->owner);
719 strfieldcpy(udev->group, perm->group);
720 } else {
721 /* no matching perms found :( */
722 udev->mode = get_default_mode(class_dev);
723 udev->owner[0] = 0x00;
724 udev->group[0] = 0x00;
725 }
726 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
727 udev->name, udev->owner, udev->group, udev->mode);
728
729 return 0;
730 }
731
732 int namedev_init(void)
733 {
734 int retval;
735
736 retval = namedev_init_rules();
737 if (retval)
738 return retval;
739
740 retval = namedev_init_permissions();
741 if (retval)
742 return retval;
743
744 dump_config_dev_list();
745 dump_perm_dev_list();
746 return retval;
747 }