]>
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.
37 #include "udev_version.h"
39 #include "libsysfs/libsysfs.h"
40 #include "klibc_fixups.h"
42 LIST_HEAD(config_device_list
);
43 LIST_HEAD(perm_device_list
);
45 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
46 static int strcmp_pattern(const char *p
, const char *s
)
62 while (*p
&& (*p
!= ']')) {
65 if ((*s
>= *p
) && (*s
<= p
[2]))
73 while (*p
&& (*p
!= ']'))
75 return strcmp_pattern(p
+1, s
+1);
81 if (strcmp_pattern(p
, s
+1))
82 return strcmp_pattern(p
+1, s
);
90 if ((*p
== *s
) || (*p
== '?'))
91 return strcmp_pattern(p
+1, s
+1);
97 #define copy_var(a, b, var) \
101 #define copy_string(a, b, var) \
102 if (strlen(b->var)) \
103 strcpy(a->var, b->var);
105 int add_perm_dev(struct perm_device
*new_dev
)
107 struct perm_device
*dev
;
108 struct perm_device
*tmp_dev
;
110 /* update the values if we already have the device */
111 list_for_each_entry(dev
, &perm_device_list
, node
) {
112 if (strcmp_pattern(new_dev
->name
, dev
->name
))
114 copy_var(dev
, new_dev
, mode
);
115 copy_string(dev
, new_dev
, owner
);
116 copy_string(dev
, new_dev
, group
);
120 /* not found, add new structure to the perm list */
121 tmp_dev
= malloc(sizeof(*tmp_dev
));
124 memcpy(tmp_dev
, new_dev
, sizeof(*tmp_dev
));
125 list_add_tail(&tmp_dev
->node
, &perm_device_list
);
126 //dump_perm_dev(tmp_dev);
130 static struct perm_device
*find_perm(char *name
)
132 struct perm_device
*perm
;
134 list_for_each_entry(perm
, &perm_device_list
, node
) {
135 if (strcmp_pattern(perm
->name
, name
))
142 static mode_t
get_default_mode(struct sysfs_class_device
*class_dev
)
144 mode_t mode
= 0600; /* default to owner rw only */
146 if (strlen(default_mode_str
) != 0) {
147 mode
= strtol(default_mode_str
, NULL
, 8);
152 static void apply_format(struct udevice
*udev
, unsigned char *string
)
154 char name
[NAME_SIZE
];
155 char temp
[NAME_SIZE
];
164 pos
= strchr(string
, '%');
169 if (isdigit(tail
[0])) {
170 num
= (int) strtoul(&pos
[1], &tail
, 10);
172 dbg("format parsing error '%s'", pos
+1);
176 strfieldcpy(name
, tail
+1);
180 if (strlen(udev
->bus_id
) == 0)
182 strcat(pos
, udev
->bus_id
);
183 dbg("substitute bus_id '%s'", udev
->bus_id
);
186 if (strlen(udev
->kernel_name
) == 0)
188 strcat(pos
, udev
->kernel_name
);
189 dbg("substitute kernel name '%s'", udev
->kernel_name
);
192 if (strlen(udev
->kernel_number
) == 0)
194 strcat(pos
, udev
->kernel_number
);
195 dbg("substitute kernel number '%s'", udev
->kernel_number
);
198 if (strlen(udev
->kernel_number
) == 0) {
200 dbg("substitute devfs disc");
204 strcat(pos
, udev
->kernel_number
);
205 dbg("substitute devfs part '%s'", udev
->kernel_number
);
208 sprintf(pos
, "%u", udev
->minor
);
209 dbg("substitute minor number '%u'", udev
->minor
);
212 sprintf(pos
, "%u", udev
->major
);
213 dbg("substitute major number '%u'", udev
->major
);
216 if (strlen(udev
->program_result
) == 0)
219 /* get part of return string */
220 strncpy(temp
, udev
->program_result
, sizeof(temp
));
224 pos3
= strsep(&pos2
, " ");
226 dbg("requested part of result string not found");
232 dbg("substitute part of result string '%s'", pos3
);
235 strcat(pos
, udev
->program_result
);
236 dbg("substitute result string '%s'", udev
->program_result
);
240 dbg("unknown substitution type '%%%c'", pos
[1]);
243 strcat(string
, name
);
249 static struct bus_file
{
253 { .bus
= "scsi", .file
= "vendor" },
254 { .bus
= "usb", .file
= "idVendor" },
255 { .bus
= "usb-serial", .file
= "detach_state" },
256 { .bus
= "ide", .file
= "detach_state" },
257 { .bus
= "pci", .file
= "vendor" },
261 #define SECONDS_TO_WAIT_FOR_FILE 10
262 static void wait_for_device_to_initialize(struct sysfs_device
*sysfs_device
)
264 /* sleep until we see the file for this specific bus type show up this
265 * is needed because we can easily out-run the kernel in looking for
266 * these files before the paticular subsystem has created them in the
267 * sysfs tree properly.
269 * And people thought that the /sbin/hotplug event system was going to
270 * be slow, poo on you for arguing that before even testing it...
272 struct bus_file
*b
= &bus_files
[0];
273 struct sysfs_attribute
*tmpattr
;
279 if (strcmp(sysfs_device
->bus
, b
->bus
) == 0) {
281 loop
= SECONDS_TO_WAIT_FOR_FILE
;
283 dbg("looking for file '%s' on bus '%s'", b
->file
, b
->bus
);
284 tmpattr
= sysfs_get_device_attr(sysfs_device
, b
->file
);
289 /* sleep to give the kernel a chance to create the file */
292 dbg("Timed out waiting for '%s' file, continuing on anyway...", b
->file
);
297 dbg("Did not find bus type '%s' on list of bus_id_files, contact greg@kroah.com", sysfs_device
->bus
);
299 return; /* here to prevent compiler warning... */
302 static int execute_program(char *path
, char *value
, int len
)
312 char *args
[PROGRAM_MAXARG
];
315 dbg("executing '%s'", path
);
329 close(STDOUT_FILENO
);
330 dup(fds
[1]); /* dup write side of pipe to STDOUT */
331 if (strchr(path
, ' ')) {
332 /* exec with arguments */
334 for (i
=0; i
< PROGRAM_MAXARG
-1; i
++) {
335 args
[i
] = strsep(&pos
, " ");
340 dbg("too many args - %d", i
);
343 retval
= execve(args
[0], args
, main_envp
);
345 retval
= execve(path
, main_argv
, main_envp
);
348 dbg("child execve failed");
351 return -1; /* avoid compiler warning */
353 /* parent reads from fds[0] */
357 res
= read(fds
[0], buffer
, sizeof(buffer
) - 1);
362 dbg("result len %d too short", len
);
366 dbg("result value already set");
370 strncpy(value
, buffer
, len
);
371 pos
= value
+ strlen(value
)-1;
374 dbg("result is '%s'", value
);
380 dbg("wait failed result %d", res
);
384 if (!WIFEXITED(status
) || (WEXITSTATUS(status
) != 0)) {
385 dbg("exec program status 0x%x", status
);
392 static int compare_sysfs_attribute(struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
, struct sysfs_pair
*pair
)
394 struct sysfs_attribute
*tmpattr
= NULL
;
397 if ((pair
== NULL
) || (pair
->file
[0] == '\0') || (pair
->value
== '\0'))
400 dbg("look for device attribute '%s'", pair
->file
);
401 /* try to find the attribute in the class device directory */
402 tmpattr
= sysfs_get_classdev_attr(class_dev
, pair
->file
);
406 /* look in the class device directory if present */
408 tmpattr
= sysfs_get_device_attr(sysfs_device
, pair
->file
);
415 c
= tmpattr
->value
+ strlen(tmpattr
->value
)-1;
418 dbg("compare attribute '%s' value '%s' with '%s'",
419 pair
->file
, tmpattr
->value
, pair
->value
);
420 if (strcmp_pattern(pair
->value
, tmpattr
->value
) != 0)
423 dbg("found matching attribute '%s' with value '%s'",
424 pair
->file
, pair
->value
);
428 static int match_sysfs_pairs(struct config_device
*dev
, struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
)
430 struct sysfs_pair
*pair
;
433 for (i
= 0; i
< MAX_SYSFS_PAIRS
; ++i
) {
434 pair
= &dev
->sysfs_pair
[i
];
435 if ((pair
->file
[0] == '\0') || (pair
->value
[0] == '\0'))
437 if (compare_sysfs_attribute(class_dev
, sysfs_device
, pair
) != 0) {
438 dbg("sysfs attribute doesn't match");
446 static int match_id(struct config_device
*dev
, struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
)
448 char path
[SYSFS_PATH_MAX
];
452 /* we have to have a sysfs device for ID to work */
457 strfieldcpy(path
, sysfs_device
->path
);
458 temp
= strrchr(path
, '/');
459 dbg("search '%s' in '%s', path='%s'", dev
->id
, temp
, path
);
460 if (strstr(temp
, dev
->id
) != NULL
) {
464 temp
= strrchr(path
, '/');
465 dbg("search '%s' in '%s', path='%s'", dev
->id
, temp
, path
);
466 if (strstr(temp
, dev
->id
) != NULL
)
470 dbg("id doesn't match");
477 static int match_place(struct config_device
*dev
, struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
)
479 char path
[SYSFS_PATH_MAX
];
483 /* we have to have a sysfs device for PLACE to work */
488 strfieldcpy(path
, sysfs_device
->path
);
489 temp
= strrchr(path
, '/');
490 dbg("search '%s' in '%s', path='%s'", dev
->place
, temp
, path
);
491 if (strstr(temp
, dev
->place
) != NULL
) {
495 temp
= strrchr(path
, '/');
496 dbg("search '%s' in '%s', path='%s'", dev
->place
, temp
, path
);
497 if (strstr(temp
, dev
->place
) != NULL
)
501 dbg("place doesn't match");
508 static struct sysfs_device
*get_sysfs_device(struct sysfs_class_device
*class_dev
)
510 struct sysfs_device
*sysfs_device
;
511 struct sysfs_class_device
*class_dev_parent
;
513 char filename
[SYSFS_PATH_MAX
+ 6];
518 /* Figure out where the device symlink is at. For char devices this will
519 * always be in the class_dev->path. But for block devices, it's different.
520 * The main block device will have the device symlink in it's path, but
521 * all partitions have the symlink in its parent directory.
522 * But we need to watch out for block devices that do not have parents, yet
523 * look like a partition (fd0, loop0, etc.) They all do not have a device
524 * symlink yet. We do sit and spin on waiting for them right now, we should
525 * possibly have a whitelist for these devices here...
527 strcpy(filename
, class_dev
->path
);
528 dbg("filename = %s", filename
);
529 if (strcmp(class_dev
->classname
, SYSFS_BLOCK_NAME
) == 0) {
530 if (isdigit(class_dev
->path
[strlen(class_dev
->path
)-1])) {
531 temp
= strrchr(filename
, '/');
533 char *temp2
= strrchr(filename
, '/');
536 dbg("temp2 = %s", temp2
);
537 if (temp2
&& (strcmp(temp2
, "/block") == 0)) {
538 /* oops, we have no parent block device, so go back to original directory */
539 strcpy(filename
, class_dev
->path
);
545 strcat(filename
, "/device");
550 dbg("looking for '%s'", filename
);
551 retval
= stat(filename
, &buf
);
554 /* sleep to give the kernel a chance to create the device file */
558 loop
= 1; /* FIXME put a real value in here for when everything is fixed... */
560 /* find the sysfs_device for this class device */
561 /* Wouldn't it really be nice if libsysfs could do this for us? */
562 sysfs_device
= sysfs_get_classdev_device(class_dev
);
563 if (sysfs_device
!= NULL
)
566 /* if it's a partition, we need to get the parent device */
568 /* FIXME HACK HACK HACK HACK
569 * for some reason partitions need this extra sleep here, in order
570 * to wait for the device properly. Once the libsysfs code is
571 * fixed properly, this sleep should go away, and we can just loop above.
574 dbg("really is a partition");
575 class_dev_parent
= sysfs_get_classdev_parent(class_dev
);
576 if (class_dev_parent
== NULL
) {
577 dbg("sysfs_get_classdev_parent for class device '%s' failed", class_dev
->name
);
579 dbg("class_dev_parent->name='%s'", class_dev_parent
->name
);
580 sysfs_device
= sysfs_get_classdev_device(class_dev_parent
);
581 if (sysfs_device
!= NULL
)
585 /* sleep to give the kernel a chance to create the link */
586 /* FIXME remove comment...
589 dbg("Timed out waiting for device symlink, continuing on anyway...");
594 int namedev_name_device(struct sysfs_class_device
*class_dev
, struct udevice
*udev
)
596 struct sysfs_device
*sysfs_device
= NULL
;
597 struct config_device
*dev
;
598 struct perm_device
*perm
;
603 /* find the sysfs_device associated with this class device */
604 sysfs_device
= get_sysfs_device(class_dev
);
606 dbg("sysfs_device->path='%s'", sysfs_device
->path
);
607 dbg("sysfs_device->bus_id='%s'", sysfs_device
->bus_id
);
608 dbg("sysfs_device->bus='%s'", sysfs_device
->bus
);
609 strfieldcpy(udev
->bus_id
, sysfs_device
->bus_id
);
610 wait_for_device_to_initialize(sysfs_device
);
612 dbg("class_dev->name = '%s'", class_dev
->name
);
615 strfieldcpy(udev
->kernel_name
, class_dev
->name
);
617 /* get kernel number */
618 pos
= class_dev
->name
+ strlen(class_dev
->name
);
619 while (isdigit(*(pos
-1)))
621 strfieldcpy(udev
->kernel_number
, pos
);
622 dbg("kernel_number='%s'", udev
->kernel_number
);
624 /* look for a matching rule to apply */
625 list_for_each_entry(dev
, &config_device_list
, node
) {
628 /* check for matching bus value */
629 if (dev
->bus
[0] != '\0') {
630 if (sysfs_device
== NULL
) {
631 dbg("device has no bus");
634 dbg("check for " FIELD_BUS
" dev->bus='%s' sysfs_device->bus='%s'", dev
->bus
, sysfs_device
->bus
);
635 if (strcmp_pattern(dev
->bus
, sysfs_device
->bus
) != 0) {
636 dbg(FIELD_BUS
" is not matching");
639 dbg(FIELD_BUS
" matches");
643 /* check for matching kernel name*/
644 if (dev
->kernel
[0] != '\0') {
645 dbg("check for " FIELD_KERNEL
" dev->kernel='%s' class_dev->name='%s'", dev
->kernel
, class_dev
->name
);
646 if (strcmp_pattern(dev
->kernel
, class_dev
->name
) != 0) {
647 dbg(FIELD_KERNEL
" is not matching");
650 dbg(FIELD_KERNEL
" matches");
654 /* check for matching bus id */
655 if (dev
->id
[0] != '\0') {
656 dbg("check " FIELD_ID
);
657 if (match_id(dev
, class_dev
, sysfs_device
) != 0) {
658 dbg(FIELD_ID
" is not matching");
661 dbg(FIELD_ID
" matches");
665 /* check for matching place of device */
666 if (dev
->place
[0] != '\0') {
667 dbg("check " FIELD_PLACE
);
668 if (match_place(dev
, class_dev
, sysfs_device
) != 0) {
669 dbg(FIELD_PLACE
" is not matching");
672 dbg(FIELD_PLACE
" matches");
676 /* check for matching sysfs pairs */
677 if (dev
->sysfs_pair
[0].file
[0] != '\0') {
678 dbg("check " FIELD_SYSFS
" pairs");
679 if (match_sysfs_pairs(dev
, class_dev
, sysfs_device
) != 0) {
680 dbg(FIELD_SYSFS
" is not matching");
683 dbg(FIELD_SYSFS
" matches");
687 /* execute external program */
688 if (dev
->program
[0] != '\0') {
689 dbg("check " FIELD_PROGRAM
);
690 apply_format(udev
, dev
->program
);
691 if (execute_program(dev
->program
, udev
->program_result
, NAME_SIZE
) != 0) {
692 dbg(FIELD_PROGRAM
" returned nozero");
695 dbg(FIELD_PROGRAM
" returned successful");
699 /* check for matching result of external program */
700 if (dev
->result
[0] != '\0') {
701 dbg("check for " FIELD_RESULT
702 " dev->result='%s', udev->program_result='%s'",
703 dev
->result
, udev
->program_result
);
704 if (strcmp_pattern(dev
->result
, udev
->program_result
) != 0) {
705 dbg(FIELD_RESULT
" is not matching");
708 dbg(FIELD_RESULT
" matches");
712 /* check if we are instructed to ignore this device */
713 if (dev
->name
[0] == '\0') {
714 dbg("instructed to ignore this device");
718 /* Yup, this rule belongs to us! */
719 dbg("found matching rule, '%s' becomes '%s'", dev
->kernel
, dev
->name
);
720 strfieldcpy(udev
->name
, dev
->name
);
721 strfieldcpy(udev
->symlink
, dev
->symlink
);
725 /* no rule was found so we use the kernel name */
726 strfieldcpy(udev
->name
, class_dev
->name
);
730 /* substitute placeholder */
731 apply_format(udev
, udev
->name
);
732 apply_format(udev
, udev
->symlink
);
735 perm
= find_perm(udev
->name
);
737 udev
->mode
= perm
->mode
;
738 strfieldcpy(udev
->owner
, perm
->owner
);
739 strfieldcpy(udev
->group
, perm
->group
);
741 /* no matching perms found :( */
742 udev
->mode
= get_default_mode(class_dev
);
743 udev
->owner
[0] = 0x00;
744 udev
->group
[0] = 0x00;
746 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
747 udev
->name
, udev
->owner
, udev
->group
, udev
->mode
);
752 int namedev_init(void)
756 retval
= namedev_init_rules();
760 retval
= namedev_init_permissions();
764 dump_config_dev_list();
765 dump_perm_dev_list();