]>
git.ipfire.org Git - thirdparty/systemd.git/blob - namedev.c
6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
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.
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.
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.
38 #include "udev_version.h"
41 #include "libsysfs/libsysfs.h"
42 #include "klibc_fixups.h"
44 LIST_HEAD(config_device_list
);
45 LIST_HEAD(perm_device_list
);
47 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
48 static int strcmp_pattern(const char *p
, const char *s
)
64 while (*p
&& (*p
!= ']')) {
67 if ((*s
>= *p
) && (*s
<= p
[2]))
75 while (*p
&& (*p
!= ']'))
77 return strcmp_pattern(p
+1, s
+1);
83 if (strcmp_pattern(p
, s
+1))
84 return strcmp_pattern(p
+1, s
);
92 if ((*p
== *s
) || (*p
== '?'))
93 return strcmp_pattern(p
+1, s
+1);
99 #define copy_var(a, b, var) \
103 #define copy_string(a, b, var) \
104 if (strlen(b->var)) \
105 strcpy(a->var, b->var);
107 int add_perm_dev(struct perm_device
*new_dev
)
109 struct perm_device
*dev
;
110 struct perm_device
*tmp_dev
;
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
))
116 copy_var(dev
, new_dev
, mode
);
117 copy_string(dev
, new_dev
, owner
);
118 copy_string(dev
, new_dev
, group
);
122 /* not found, add new structure to the perm list */
123 tmp_dev
= malloc(sizeof(*tmp_dev
));
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);
132 static struct perm_device
*find_perm(char *name
)
134 struct perm_device
*perm
;
136 list_for_each_entry(perm
, &perm_device_list
, node
) {
137 if (strcmp_pattern(perm
->name
, name
))
144 static mode_t
get_default_mode(void)
146 mode_t mode
= 0600; /* default to owner rw only */
148 if (strlen(default_mode_str
) != 0)
149 mode
= strtol(default_mode_str
, NULL
, 8);
154 static char *get_default_owner(void)
156 if (strlen(default_owner_str
) == 0)
157 strncpy(default_owner_str
, "root", OWNER_SIZE
);
159 return default_owner_str
;
162 static char *get_default_group(void)
164 if (strlen(default_group_str
) == 0)
165 strncpy(default_group_str
, "root", GROUP_SIZE
);
167 return default_group_str
;
170 static void apply_format(struct udevice
*udev
, unsigned char *string
)
172 char temp
[NAME_SIZE
];
173 char temp1
[NAME_SIZE
];
183 pos
= strchr(pos
, '%');
188 if (isdigit(tail
[0])) {
189 num
= (int) strtoul(&pos
[1], &tail
, 10);
191 dbg("format parsing error '%s'", pos
+1);
195 strfieldcpy(temp
, tail
+1);
199 if (strlen(udev
->bus_id
) == 0)
201 strcat(pos
, udev
->bus_id
);
202 dbg("substitute bus_id '%s'", udev
->bus_id
);
205 if (strlen(udev
->kernel_name
) == 0)
207 strcat(pos
, udev
->kernel_name
);
208 dbg("substitute kernel name '%s'", udev
->kernel_name
);
211 if (strlen(udev
->kernel_number
) == 0)
213 strcat(pos
, udev
->kernel_number
);
214 dbg("substitute kernel number '%s'", udev
->kernel_number
);
217 sprintf(pos
, "%u", udev
->minor
);
218 dbg("substitute minor number '%u'", udev
->minor
);
221 sprintf(pos
, "%u", udev
->major
);
222 dbg("substitute major number '%u'", udev
->major
);
225 if (strlen(udev
->program_result
) == 0)
228 /* get part of return string */
229 strncpy(temp1
, udev
->program_result
, sizeof(temp1
));
233 pos3
= strsep(&pos2
, " ");
235 dbg("requested part of result string not found");
241 dbg("substitute part of result string '%s'", pos3
);
244 strcat(pos
, udev
->program_result
);
245 dbg("substitute result string '%s'", udev
->program_result
);
253 dbg("unknown substitution type '%%%c'", pos
[1]);
256 strcat(string
, temp
);
262 static struct bus_file
{
266 { .bus
= "scsi", .file
= "vendor" },
267 { .bus
= "usb", .file
= "idVendor" },
268 { .bus
= "usb-serial", .file
= "detach_state" },
269 { .bus
= "ide", .file
= "detach_state" },
270 { .bus
= "pci", .file
= "vendor" },
274 #define SECONDS_TO_WAIT_FOR_FILE 10
275 static void wait_for_device_to_initialize(struct sysfs_device
*sysfs_device
)
277 /* sleep until we see the file for this specific bus type show up this
278 * is needed because we can easily out-run the kernel in looking for
279 * these files before the paticular subsystem has created them in the
280 * sysfs tree properly.
282 * And people thought that the /sbin/hotplug event system was going to
283 * be slow, poo on you for arguing that before even testing it...
285 struct bus_file
*b
= &bus_files
[0];
286 struct sysfs_attribute
*tmpattr
;
292 if (strcmp(sysfs_device
->bus
, b
->bus
) == 0) {
294 loop
= SECONDS_TO_WAIT_FOR_FILE
;
296 dbg("looking for file '%s' on bus '%s'", b
->file
, b
->bus
);
297 tmpattr
= sysfs_get_device_attr(sysfs_device
, b
->file
);
302 /* sleep to give the kernel a chance to create the file */
305 dbg("timed out waiting for '%s' file, continuing on anyway...", b
->file
);
310 dbg("did not find bus type '%s' on list of bus_id_files, contact greg@kroah.com", sysfs_device
->bus
);
312 return; /* here to prevent compiler warning... */
315 static int execute_program(char *path
, char *value
, int len
)
325 char *args
[PROGRAM_MAXARG
];
328 dbg("executing '%s'", path
);
342 close(STDOUT_FILENO
);
343 dup(fds
[1]); /* dup write side of pipe to STDOUT */
344 if (strchr(path
, ' ')) {
345 /* exec with arguments */
347 for (i
=0; i
< PROGRAM_MAXARG
-1; i
++) {
348 args
[i
] = strsep(&pos
, " ");
353 dbg("too many args - %d", i
);
356 retval
= execve(args
[0], args
, main_envp
);
358 retval
= execve(path
, main_argv
, main_envp
);
361 dbg("child execve failed");
364 return -1; /* avoid compiler warning */
366 /* parent reads from fds[0] */
370 res
= read(fds
[0], buffer
, sizeof(buffer
) - 1);
375 dbg("result len %d too short", len
);
379 dbg("result value already set");
383 strncpy(value
, buffer
, len
);
384 pos
= value
+ strlen(value
)-1;
387 dbg("result is '%s'", value
);
393 dbg("wait failed result %d", res
);
397 if (!WIFEXITED(status
) || (WEXITSTATUS(status
) != 0)) {
398 dbg("exec program status 0x%x", status
);
405 static int compare_sysfs_attribute(struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
, struct sysfs_pair
*pair
)
407 struct sysfs_attribute
*tmpattr
= NULL
;
410 if ((pair
== NULL
) || (pair
->file
[0] == '\0') || (pair
->value
== '\0'))
413 dbg("look for device attribute '%s'", pair
->file
);
414 /* try to find the attribute in the class device directory */
415 tmpattr
= sysfs_get_classdev_attr(class_dev
, pair
->file
);
419 /* look in the class device directory if present */
421 tmpattr
= sysfs_get_device_attr(sysfs_device
, pair
->file
);
428 c
= tmpattr
->value
+ strlen(tmpattr
->value
)-1;
431 dbg("compare attribute '%s' value '%s' with '%s'",
432 pair
->file
, tmpattr
->value
, pair
->value
);
433 if (strcmp_pattern(pair
->value
, tmpattr
->value
) != 0)
436 dbg("found matching attribute '%s' with value '%s'",
437 pair
->file
, pair
->value
);
441 static int match_sysfs_pairs(struct config_device
*dev
, struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
)
443 struct sysfs_pair
*pair
;
446 for (i
= 0; i
< MAX_SYSFS_PAIRS
; ++i
) {
447 pair
= &dev
->sysfs_pair
[i
];
448 if ((pair
->file
[0] == '\0') || (pair
->value
[0] == '\0'))
450 if (compare_sysfs_attribute(class_dev
, sysfs_device
, pair
) != 0) {
451 dbg("sysfs attribute doesn't match");
459 static int match_id(struct config_device
*dev
, struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
)
461 char path
[SYSFS_PATH_MAX
];
465 /* we have to have a sysfs device for ID to work */
470 strfieldcpy(path
, sysfs_device
->path
);
471 temp
= strrchr(path
, '/');
472 dbg("search '%s' in '%s', path='%s'", dev
->id
, temp
, path
);
473 if (strstr(temp
, dev
->id
) != NULL
) {
477 temp
= strrchr(path
, '/');
478 dbg("search '%s' in '%s', path='%s'", dev
->id
, temp
, path
);
479 if (strstr(temp
, dev
->id
) != NULL
)
483 dbg("id doesn't match");
490 static int match_place(struct config_device
*dev
, struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
)
492 char path
[SYSFS_PATH_MAX
];
496 /* we have to have a sysfs device for PLACE to work */
501 strfieldcpy(path
, sysfs_device
->path
);
502 temp
= strrchr(path
, '/');
503 dbg("search '%s' in '%s', path='%s'", dev
->place
, temp
, path
);
504 if (strstr(temp
, dev
->place
) != NULL
) {
508 temp
= strrchr(path
, '/');
509 dbg("search '%s' in '%s', path='%s'", dev
->place
, temp
, path
);
510 if (strstr(temp
, dev
->place
) != NULL
)
514 dbg("place doesn't match");
521 static struct sysfs_device
*get_sysfs_device(struct sysfs_class_device
*class_dev
)
523 struct sysfs_device
*sysfs_device
;
524 struct sysfs_class_device
*class_dev_parent
;
525 struct timespec tspec
;
528 /* Figure out where the device symlink is at. For char devices this will
529 * always be in the class_dev->path. But for block devices, it's different.
530 * The main block device will have the device symlink in it's path, but
531 * all partitions have the symlink in its parent directory.
532 * But we need to watch out for block devices that do not have parents, yet
533 * look like a partition (fd0, loop0, etc.) They all do not have a device
534 * symlink yet. We do sit and spin on waiting for them right now, we should
535 * possibly have a whitelist for these devices here...
537 class_dev_parent
= sysfs_get_classdev_parent(class_dev
);
538 if (class_dev_parent
)
539 dbg("Really a partition");
542 tspec
.tv_nsec
= 10000000; /* sleep 10 millisec */
545 nanosleep(&tspec
, NULL
);
546 if (class_dev_parent
)
547 sysfs_device
= sysfs_get_classdev_device(class_dev_parent
);
549 sysfs_device
= sysfs_get_classdev_device(class_dev
);
551 if (sysfs_device
!= NULL
)
554 dbg("timed out waiting for device symlink, continuing on anyway...");
557 /* We have another issue with just the wait above - the sysfs part of
558 * the kernel may not be quick enough to have created the link to the
559 * device under the "bus" subsystem. Due to this, the sysfs_device->bus
560 * will not contain the actual bus name :(
562 * Libsysfs now provides a new API sysfs_get_device_bus(), so use it
566 if (sysfs_device
->bus
[0] != '\0')
570 tspec
.tv_nsec
= 10000000;
572 nanosleep(&tspec
, NULL
);
573 sysfs_get_device_bus(sysfs_device
);
575 if (sysfs_device
->bus
[0] != '\0')
578 dbg("timed out waiting to find the device bus, continuing on anyway");
581 dbg("device %s is registered with bus '%s'",
582 sysfs_device
->name
, sysfs_device
->bus
);
588 static int match_rule(struct config_device
*dev
, struct sysfs_class_device
*class_dev
, struct udevice
*udev
, struct sysfs_device
*sysfs_device
)
591 /* check for matching bus value */
592 if (dev
->bus
[0] != '\0') {
593 if (sysfs_device
== NULL
) {
594 dbg("device has no bus");
597 dbg("check for " FIELD_BUS
" dev->bus='%s' sysfs_device->bus='%s'", dev
->bus
, sysfs_device
->bus
);
598 if (strcmp_pattern(dev
->bus
, sysfs_device
->bus
) != 0) {
599 dbg(FIELD_BUS
" is not matching");
602 dbg(FIELD_BUS
" matches");
606 /* check for matching kernel name*/
607 if (dev
->kernel
[0] != '\0') {
608 dbg("check for " FIELD_KERNEL
" dev->kernel='%s' class_dev->name='%s'", dev
->kernel
, class_dev
->name
);
609 if (strcmp_pattern(dev
->kernel
, class_dev
->name
) != 0) {
610 dbg(FIELD_KERNEL
" is not matching");
613 dbg(FIELD_KERNEL
" matches");
617 /* check for matching bus id */
618 if (dev
->id
[0] != '\0') {
619 dbg("check " FIELD_ID
);
620 if (match_id(dev
, class_dev
, sysfs_device
) != 0) {
621 dbg(FIELD_ID
" is not matching");
624 dbg(FIELD_ID
" matches");
628 /* check for matching place of device */
629 if (dev
->place
[0] != '\0') {
630 dbg("check " FIELD_PLACE
);
631 if (match_place(dev
, class_dev
, sysfs_device
) != 0) {
632 dbg(FIELD_PLACE
" is not matching");
635 dbg(FIELD_PLACE
" matches");
639 /* check for matching sysfs pairs */
640 if (dev
->sysfs_pair
[0].file
[0] != '\0') {
641 dbg("check " FIELD_SYSFS
" pairs");
642 if (match_sysfs_pairs(dev
, class_dev
, sysfs_device
) != 0) {
643 dbg(FIELD_SYSFS
" is not matching");
646 dbg(FIELD_SYSFS
" matches");
650 /* execute external program */
651 if (dev
->program
[0] != '\0') {
652 dbg("check " FIELD_PROGRAM
);
653 apply_format(udev
, dev
->program
);
654 if (execute_program(dev
->program
, udev
->program_result
, NAME_SIZE
) != 0) {
655 dbg(FIELD_PROGRAM
" returned nozero");
658 dbg(FIELD_PROGRAM
" returned successful");
662 /* check for matching result of external program */
663 if (dev
->result
[0] != '\0') {
664 dbg("check for " FIELD_RESULT
665 " dev->result='%s', udev->program_result='%s'",
666 dev
->result
, udev
->program_result
);
667 if (strcmp_pattern(dev
->result
, udev
->program_result
) != 0) {
668 dbg(FIELD_RESULT
" is not matching");
671 dbg(FIELD_RESULT
" matches");
675 /* check if we are instructed to ignore this device */
676 if (dev
->name
[0] == '\0') {
677 dbg("instructed to ignore this device");
681 /* Yeah, we matched! */
685 sysfs_device
= sysfs_get_device_parent(sysfs_device
);
686 if (sysfs_device
== NULL
)
688 dbg("sysfs_device->path='%s'", sysfs_device
->path
);
689 dbg("sysfs_device->bus_id='%s'", sysfs_device
->bus_id
);
690 dbg("sysfs_device->bus='%s'", sysfs_device
->bus
);
695 int namedev_name_device(struct sysfs_class_device
*class_dev
, struct udevice
*udev
)
697 struct sysfs_device
*sysfs_device
= NULL
;
698 struct config_device
*dev
;
699 struct perm_device
*perm
;
704 /* find the sysfs_device associated with this class device */
705 sysfs_device
= get_sysfs_device(class_dev
);
707 dbg("sysfs_device->path='%s'", sysfs_device
->path
);
708 dbg("sysfs_device->bus_id='%s'", sysfs_device
->bus_id
);
709 dbg("sysfs_device->bus='%s'", sysfs_device
->bus
);
710 strfieldcpy(udev
->bus_id
, sysfs_device
->bus_id
);
711 wait_for_device_to_initialize(sysfs_device
);
713 dbg("class_dev->name = '%s'", class_dev
->name
);
716 strfieldcpy(udev
->kernel_name
, class_dev
->name
);
718 /* get kernel number */
719 pos
= class_dev
->name
+ strlen(class_dev
->name
);
720 while (isdigit(*(pos
-1)))
722 strfieldcpy(udev
->kernel_number
, pos
);
723 dbg("kernel_number='%s'", udev
->kernel_number
);
725 /* look for a matching rule to apply */
726 list_for_each_entry(dev
, &config_device_list
, node
) {
729 if (match_rule(dev
, class_dev
, udev
, sysfs_device
) == 0) {
730 /* Yup, this rule belongs to us! */
731 info("configured rule in '%s' at line %i applied, '%s' becomes '%s'",
732 udev_rules_filename
, dev
->config_line
, udev
->kernel_name
, dev
->name
);
733 strfieldcpy(udev
->name
, dev
->name
);
734 strfieldcpy(udev
->symlink
, dev
->symlink
);
739 /* no rule was found so we use the kernel name */
740 strfieldcpy(udev
->name
, class_dev
->name
);
744 /* substitute placeholder */
745 apply_format(udev
, udev
->name
);
746 apply_format(udev
, udev
->symlink
);
749 perm
= find_perm(udev
->name
);
751 udev
->mode
= perm
->mode
;
752 strfieldcpy(udev
->owner
, perm
->owner
);
753 strfieldcpy(udev
->group
, perm
->group
);
755 /* no matching perms found :( */
756 udev
->mode
= get_default_mode();
757 strncpy(udev
->owner
, get_default_owner(), OWNER_SIZE
);
758 strncpy(udev
->group
, get_default_group(), GROUP_SIZE
);
760 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
761 udev
->name
, udev
->owner
, udev
->group
, udev
->mode
);
766 int namedev_init(void)
770 retval
= namedev_init_rules();
774 retval
= namedev_init_permissions();
778 dump_config_dev_list();
779 dump_perm_dev_list();