]>
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.
36 #include "libsysfs/sysfs/libsysfs.h"
39 #include "udev_version.h"
42 #include "klibc_fixups.h"
44 static struct sysfs_attribute
*find_sysfs_attribute(struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
, char *attr
);
46 LIST_HEAD(config_device_list
);
47 LIST_HEAD(perm_device_list
);
49 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
50 static int strcmp_pattern(const char *p
, const char *s
)
55 return (p
[0] != '\0');
66 while ((p
[0] != '\0') && (p
[0] != ']')) {
69 if ((s
[0] >= p
[0]) && (s
[0] <= p
[2]))
73 match
= (p
[0] == s
[0]);
77 while ((p
[0] != '\0') && (p
[0] != ']'))
80 return strcmp_pattern(p
+1, s
+1);
86 if (strcmp_pattern(p
, s
+1))
87 return strcmp_pattern(p
+1, s
);
95 if ((p
[0] == s
[0]) || (p
[0] == '?'))
96 return strcmp_pattern(p
+1, s
+1);
102 #define copy_var(a, b, var) \
106 #define copy_string(a, b, var) \
107 if (strlen(b->var)) \
108 strcpy(a->var, b->var);
110 int add_perm_dev(struct perm_device
*new_dev
)
112 struct perm_device
*dev
;
113 struct perm_device
*tmp_dev
;
115 /* update the values if we already have the device */
116 list_for_each_entry(dev
, &perm_device_list
, node
) {
117 if (strcmp(new_dev
->name
, dev
->name
))
119 copy_var(dev
, new_dev
, mode
);
120 copy_string(dev
, new_dev
, owner
);
121 copy_string(dev
, new_dev
, group
);
125 /* not found, add new structure to the perm list */
126 tmp_dev
= malloc(sizeof(*tmp_dev
));
129 memcpy(tmp_dev
, new_dev
, sizeof(*tmp_dev
));
130 list_add_tail(&tmp_dev
->node
, &perm_device_list
);
131 //dump_perm_dev(tmp_dev);
135 static struct perm_device
*find_perm(char *name
)
137 struct perm_device
*perm
;
139 list_for_each_entry(perm
, &perm_device_list
, node
) {
140 if (strcmp_pattern(perm
->name
, name
))
147 static mode_t
get_default_mode(void)
149 mode_t mode
= 0600; /* default to owner rw only */
151 if (strlen(default_mode_str
) != 0)
152 mode
= strtol(default_mode_str
, NULL
, 8);
157 static char *get_default_owner(void)
159 if (strlen(default_owner_str
) == 0)
160 strncpy(default_owner_str
, "root", OWNER_SIZE
);
162 return default_owner_str
;
165 static char *get_default_group(void)
167 if (strlen(default_group_str
) == 0)
168 strncpy(default_group_str
, "root", GROUP_SIZE
);
170 return default_group_str
;
173 /* extract possible {attr} and move str behind it */
174 static char *get_format_attribute(char **str
)
179 if (*str
[0] == '{') {
180 pos
= strchr(*str
, '}');
182 dbg("missing closing brace for format");
188 dbg("attribute='%s', str='%s'", attr
, *str
);
193 /* extract possible format length and move str behind it*/
194 static int get_format_len(char **str
)
199 if (isdigit(*str
[0])) {
200 num
= (int) strtoul(*str
, &tail
, 10);
203 dbg("format length=%i", num
);
206 dbg("format parsing error '%s'", *str
);
212 static void apply_format(struct udevice
*udev
, unsigned char *string
, struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
)
214 char temp
[NAME_SIZE
];
215 char temp1
[NAME_SIZE
];
224 struct sysfs_attribute
*tmpattr
;
229 pos
= strchr(pos
, '%');
233 num
= get_format_len(&tail
);
235 strfieldcpy(temp
, tail
+1);
240 dbg("format=%c, string='%s', tail='%s'",c
, string
, tail
);
242 attr
= get_format_attribute(&tail
);
246 if (strlen(udev
->bus_id
) == 0)
248 strcat(pos
, udev
->bus_id
);
249 dbg("substitute bus_id '%s'", udev
->bus_id
);
252 if (strlen(udev
->kernel_name
) == 0)
254 strcat(pos
, udev
->kernel_name
);
255 dbg("substitute kernel name '%s'", udev
->kernel_name
);
258 if (strlen(udev
->kernel_number
) == 0)
260 strcat(pos
, udev
->kernel_number
);
261 dbg("substitute kernel number '%s'", udev
->kernel_number
);
264 sprintf(pos
, "%u", udev
->minor
);
265 dbg("substitute minor number '%u'", udev
->minor
);
268 sprintf(pos
, "%u", udev
->major
);
269 dbg("substitute major number '%u'", udev
->major
);
272 if (strlen(udev
->program_result
) == 0)
274 /* get part part of the result string */
275 i
= num
; /* num syntax is deprecated and will be removed */
279 strncpy(temp1
, udev
->program_result
, sizeof(temp1
));
283 pos3
= strsep(&pos2
, " ");
285 dbg("requested part of result string not found");
291 dbg("substitute part of result string '%s'", pos3
);
294 strcat(pos
, udev
->program_result
);
295 dbg("substitute result string '%s'", udev
->program_result
);
300 tmpattr
= find_sysfs_attribute(class_dev
, sysfs_device
, attr
);
301 if (tmpattr
== NULL
) {
302 dbg("sysfa attribute '%s' not found", attr
);
305 strcpy(pos
, tmpattr
->value
);
306 dbg("substitute sysfs value '%s'", tmpattr
->value
);
308 dbg("missing attribute");
315 dbg("unknown substitution type '%%%c'", c
);
323 * Note, we can have multiple files for different busses in here due
324 * to the mess that USB has for its device tree...
326 static struct bus_file
{
330 { .bus
= "scsi", .file
= "vendor" },
331 { .bus
= "usb", .file
= "idVendor" },
332 { .bus
= "usb", .file
= "iInterface" },
333 { .bus
= "usb-serial", .file
= "detach_state" },
334 { .bus
= "ide", .file
= "detach_state" },
335 { .bus
= "pci", .file
= "vendor" },
339 #define SECONDS_TO_WAIT_FOR_FILE 10
340 static void wait_for_device_to_initialize(struct sysfs_device
*sysfs_device
)
342 /* sleep until we see the file for this specific bus type show up this
343 * is needed because we can easily out-run the kernel in looking for
344 * these files before the paticular subsystem has created them in the
345 * sysfs tree properly.
347 * And people thought that the /sbin/hotplug event system was going to
348 * be slow, poo on you for arguing that before even testing it...
350 struct bus_file
*b
= &bus_files
[0];
351 struct sysfs_attribute
*tmpattr
;
353 int loop
= SECONDS_TO_WAIT_FOR_FILE
;
356 if (b
->bus
== NULL
) {
359 /* sleep to give the kernel a chance to create the file */
366 if (strcmp(sysfs_device
->bus
, b
->bus
) == 0) {
368 dbg("looking for file '%s' on bus '%s'", b
->file
, b
->bus
);
369 tmpattr
= sysfs_get_device_attr(sysfs_device
, b
->file
);
374 dbg("can't find '%s' file", b
->file
);
379 dbg("did not find bus type '%s' on list of bus_id_files, "
380 "contact greg@kroah.com", sysfs_device
->bus
);
382 return; /* here to prevent compiler warning... */
385 static void fix_kernel_name(struct udevice
*udev
)
387 char *temp
= udev
->kernel_name
;
389 while (*temp
!= 0x00) {
390 /* Some block devices have a ! in their name,
391 * we need to change that to / */
398 static int execute_program(char *path
, char *value
, int len
)
408 char *args
[PROGRAM_MAXARG
];
411 dbg("executing '%s'", path
);
421 close(STDOUT_FILENO
);
422 dup(fds
[1]); /* dup write side of pipe to STDOUT */
423 if (strchr(path
, ' ')) {
424 /* exec with arguments */
426 for (i
=0; i
< PROGRAM_MAXARG
-1; i
++) {
427 args
[i
] = strsep(&pos
, " ");
432 dbg("too many args - %d", i
);
435 retval
= execv(args
[0], args
);
437 retval
= execv(path
, main_argv
);
439 dbg("child execve failed");
445 /* parent reads from fds[0] */
449 res
= read(fds
[0], buffer
, sizeof(buffer
) - 1);
454 dbg("result len %d too short", len
);
458 dbg("result value already set");
462 strncpy(value
, buffer
, len
);
463 pos
= value
+ strlen(value
)-1;
466 dbg("result is '%s'", value
);
472 dbg("wait failed result %d", res
);
476 if (!WIFEXITED(status
) || (WEXITSTATUS(status
) != 0)) {
477 dbg("exec program status 0x%x", status
);
484 static struct sysfs_attribute
*find_sysfs_attribute(struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
, char *attr
)
486 struct sysfs_attribute
*tmpattr
= NULL
;
489 dbg("look for device attribute '%s'", attr
);
490 /* try to find the attribute in the class device directory */
491 tmpattr
= sysfs_get_classdev_attr(class_dev
, attr
);
495 /* look in the class device directory if present */
497 tmpattr
= sysfs_get_device_attr(sysfs_device
, attr
);
505 c
= strchr(tmpattr
->value
, '\n');
509 dbg("found attribute '%s'", tmpattr
->path
);
513 static int compare_sysfs_attribute(struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
, struct sysfs_pair
*pair
)
515 struct sysfs_attribute
*tmpattr
;
517 if ((pair
== NULL
) || (pair
->file
[0] == '\0') || (pair
->value
== '\0'))
520 tmpattr
= find_sysfs_attribute(class_dev
, sysfs_device
, pair
->file
);
524 dbg("compare attribute '%s' value '%s' with '%s'",
525 pair
->file
, tmpattr
->value
, pair
->value
);
526 if (strcmp_pattern(pair
->value
, tmpattr
->value
) != 0)
529 dbg("found matching attribute '%s' with value '%s'",
530 pair
->file
, pair
->value
);
534 static int match_sysfs_pairs(struct config_device
*dev
, struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
)
536 struct sysfs_pair
*pair
;
539 for (i
= 0; i
< MAX_SYSFS_PAIRS
; ++i
) {
540 pair
= &dev
->sysfs_pair
[i
];
541 if ((pair
->file
[0] == '\0') || (pair
->value
[0] == '\0'))
543 if (compare_sysfs_attribute(class_dev
, sysfs_device
, pair
) != 0) {
544 dbg("sysfs attribute doesn't match");
552 static int match_id(struct config_device
*dev
, struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
)
554 char path
[SYSFS_PATH_MAX
];
557 /* we have to have a sysfs device for ID to work */
561 strfieldcpy(path
, sysfs_device
->path
);
562 temp
= strrchr(path
, '/');
564 dbg("search '%s' in '%s', path='%s'", dev
->id
, temp
, path
);
565 if (strcmp_pattern(dev
->id
, temp
) != 0)
571 static int match_place(struct config_device
*dev
, struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
)
573 char path
[SYSFS_PATH_MAX
];
577 /* we have to have a sysfs device for PLACE to work */
582 strfieldcpy(path
, sysfs_device
->path
);
583 temp
= strrchr(path
, '/');
584 dbg("search '%s' in '%s', path='%s'", dev
->place
, temp
, path
);
585 if (strstr(temp
, dev
->place
) != NULL
) {
589 temp
= strrchr(path
, '/');
590 dbg("search '%s' in '%s', path='%s'", dev
->place
, temp
, path
);
591 if (strstr(temp
, dev
->place
) != NULL
)
595 dbg("place doesn't match");
602 static struct sysfs_device
*get_sysfs_device(struct sysfs_class_device
*class_dev
)
604 struct sysfs_device
*sysfs_device
;
605 struct sysfs_class_device
*class_dev_parent
;
606 struct timespec tspec
;
609 /* Figure out where the device symlink is at. For char devices this will
610 * always be in the class_dev->path. But for block devices, it's different.
611 * The main block device will have the device symlink in it's path, but
612 * all partitions have the symlink in its parent directory.
613 * But we need to watch out for block devices that do not have parents, yet
614 * look like a partition (fd0, loop0, etc.) They all do not have a device
615 * symlink yet. We do sit and spin on waiting for them right now, we should
616 * possibly have a whitelist for these devices here...
618 class_dev_parent
= sysfs_get_classdev_parent(class_dev
);
619 if (class_dev_parent
)
620 dbg("Really a partition");
623 tspec
.tv_nsec
= 10000000; /* sleep 10 millisec */
627 nanosleep(&tspec
, NULL
);
628 if (class_dev_parent
)
629 sysfs_device
= sysfs_get_classdev_device(class_dev_parent
);
631 sysfs_device
= sysfs_get_classdev_device(class_dev
);
633 if (sysfs_device
!= NULL
)
636 dbg("timed out waiting for device symlink, continuing on anyway...");
639 /* We have another issue with just the wait above - the sysfs part of
640 * the kernel may not be quick enough to have created the link to the
641 * device under the "bus" subsystem. Due to this, the sysfs_device->bus
642 * will not contain the actual bus name :(
644 * Libsysfs now provides a new API sysfs_get_device_bus(), so use it
648 if (sysfs_device
->bus
[0] != '\0')
652 tspec
.tv_nsec
= 10000000;
655 nanosleep(&tspec
, NULL
);
656 sysfs_get_device_bus(sysfs_device
);
658 if (sysfs_device
->bus
[0] != '\0')
661 dbg("timed out waiting to find the device bus, continuing on anyway");
664 dbg("device %s is registered with bus '%s'",
665 sysfs_device
->name
, sysfs_device
->bus
);
671 static int match_rule(struct config_device
*dev
, struct sysfs_class_device
*class_dev
, struct udevice
*udev
, struct sysfs_device
*sysfs_device
)
674 /* check for matching bus value */
675 if (dev
->bus
[0] != '\0') {
676 if (sysfs_device
== NULL
) {
677 dbg("device has no bus");
680 dbg("check for " FIELD_BUS
" dev->bus='%s' sysfs_device->bus='%s'", dev
->bus
, sysfs_device
->bus
);
681 if (strcmp_pattern(dev
->bus
, sysfs_device
->bus
) != 0) {
682 dbg(FIELD_BUS
" is not matching");
685 dbg(FIELD_BUS
" matches");
689 /* check for matching kernel name*/
690 if (dev
->kernel
[0] != '\0') {
691 dbg("check for " FIELD_KERNEL
" dev->kernel='%s' class_dev->name='%s'", dev
->kernel
, class_dev
->name
);
692 if (strcmp_pattern(dev
->kernel
, class_dev
->name
) != 0) {
693 dbg(FIELD_KERNEL
" is not matching");
696 dbg(FIELD_KERNEL
" matches");
700 /* check for matching bus id */
701 if (dev
->id
[0] != '\0') {
702 dbg("check " FIELD_ID
);
703 if (match_id(dev
, class_dev
, sysfs_device
) != 0) {
704 dbg(FIELD_ID
" is not matching");
707 dbg(FIELD_ID
" matches");
711 /* check for matching place of device */
712 if (dev
->place
[0] != '\0') {
713 dbg("check " FIELD_PLACE
);
714 if (match_place(dev
, class_dev
, sysfs_device
) != 0) {
715 dbg(FIELD_PLACE
" is not matching");
718 dbg(FIELD_PLACE
" matches");
722 /* check for matching sysfs pairs */
723 if (dev
->sysfs_pair
[0].file
[0] != '\0') {
724 dbg("check " FIELD_SYSFS
" pairs");
725 if (match_sysfs_pairs(dev
, class_dev
, sysfs_device
) != 0) {
726 dbg(FIELD_SYSFS
" is not matching");
729 dbg(FIELD_SYSFS
" matches");
733 /* execute external program */
734 if (dev
->program
[0] != '\0') {
735 dbg("check " FIELD_PROGRAM
);
736 apply_format(udev
, dev
->program
, class_dev
, sysfs_device
);
737 if (execute_program(dev
->program
, udev
->program_result
, NAME_SIZE
) != 0) {
738 dbg(FIELD_PROGRAM
" returned nozero");
741 dbg(FIELD_PROGRAM
" returned successful");
745 /* check for matching result of external program */
746 if (dev
->result
[0] != '\0') {
747 dbg("check for " FIELD_RESULT
748 " dev->result='%s', udev->program_result='%s'",
749 dev
->result
, udev
->program_result
);
750 if (strcmp_pattern(dev
->result
, udev
->program_result
) != 0) {
751 dbg(FIELD_RESULT
" is not matching");
754 dbg(FIELD_RESULT
" matches");
758 /* Yeah, we matched! */
762 dbg("try parent sysfs device");
763 sysfs_device
= sysfs_get_device_parent(sysfs_device
);
764 if (sysfs_device
== NULL
)
766 dbg("sysfs_device->path='%s'", sysfs_device
->path
);
767 dbg("sysfs_device->bus_id='%s'", sysfs_device
->bus_id
);
768 dbg("sysfs_device->bus='%s'", sysfs_device
->bus
);
773 int namedev_name_device(struct sysfs_class_device
*class_dev
, struct udevice
*udev
)
775 struct sysfs_device
*sysfs_device
= NULL
;
776 struct config_device
*dev
;
777 struct perm_device
*perm
;
782 /* find the sysfs_device associated with this class device */
783 sysfs_device
= get_sysfs_device(class_dev
);
785 dbg("sysfs_device->path='%s'", sysfs_device
->path
);
786 dbg("sysfs_device->bus_id='%s'", sysfs_device
->bus_id
);
787 dbg("sysfs_device->bus='%s'", sysfs_device
->bus
);
788 strfieldcpy(udev
->bus_id
, sysfs_device
->bus_id
);
789 wait_for_device_to_initialize(sysfs_device
);
791 dbg("class_dev->name = '%s'", class_dev
->name
);
793 strfieldcpy(udev
->kernel_name
, class_dev
->name
);
794 fix_kernel_name(udev
);
795 dbg("udev->kernel_name = '%s'", udev
->kernel_name
);
797 /* get kernel number */
798 pos
= class_dev
->name
+ strlen(class_dev
->name
);
799 while (isdigit(*(pos
-1)))
801 strfieldcpy(udev
->kernel_number
, pos
);
802 dbg("kernel_number='%s'", udev
->kernel_number
);
804 /* look for a matching rule to apply */
805 list_for_each_entry(dev
, &config_device_list
, node
) {
807 if (match_rule(dev
, class_dev
, udev
, sysfs_device
) == 0) {
808 if (dev
->name
[0] == '\0') {
809 info("configured rule in '%s' at line %i applied, '%s' is ignored",
810 udev_rules_filename
, dev
->config_line
, udev
->kernel_name
);
814 info("configured rule in '%s' at line %i applied, '%s' becomes '%s'",
815 udev_rules_filename
, dev
->config_line
, udev
->kernel_name
, dev
->name
);
816 strfieldcpy(udev
->name
, dev
->name
);
817 strfieldcpy(udev
->symlink
, dev
->symlink
);
822 /* no rule was found so we use the kernel name */
823 strfieldcpy(udev
->name
, udev
->kernel_name
);
827 /* substitute placeholder */
828 apply_format(udev
, udev
->name
, class_dev
, sysfs_device
);
829 apply_format(udev
, udev
->symlink
, class_dev
, sysfs_device
);
830 udev
->partitions
= dev
->partitions
;
832 perm
= find_perm(udev
->name
);
834 udev
->mode
= perm
->mode
;
835 strfieldcpy(udev
->owner
, perm
->owner
);
836 strfieldcpy(udev
->group
, perm
->group
);
838 /* no matching perms found :( */
839 udev
->mode
= get_default_mode();
840 strncpy(udev
->owner
, get_default_owner(), OWNER_SIZE
);
841 strncpy(udev
->group
, get_default_group(), GROUP_SIZE
);
843 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
844 udev
->name
, udev
->owner
, udev
->group
, udev
->mode
);
849 int namedev_init(void)
853 retval
= namedev_init_rules();
857 retval
= namedev_init_permissions();
861 dump_config_dev_list();
862 dump_perm_dev_list();