]>
git.ipfire.org Git - thirdparty/systemd.git/blob - namedev.c
6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (C) 2003-2005 Kay Sievers <kay.sievers@vrfy.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation version 2 of the License.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 675 Mass Ave, Cambridge, MA 02139, USA.
36 #include "libsysfs/sysfs/libsysfs.h"
39 #include "udev_utils.h"
40 #include "udev_version.h"
45 static struct sysfs_attribute
*find_sysfs_attribute(struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
, char *attr
);
47 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
48 static int strcmp_pattern(const char *p
, const char *s
)
53 return (p
[0] != '\0');
64 while ((p
[0] != '\0') && (p
[0] != ']')) {
67 if ((s
[0] >= p
[0]) && (s
[0] <= p
[2]))
71 match
= (p
[0] == s
[0]);
75 while ((p
[0] != '\0') && (p
[0] != ']'))
78 return strcmp_pattern(p
+1, s
+1);
84 if (strcmp_pattern(p
, s
+1))
85 return strcmp_pattern(p
+1, s
);
93 if ((p
[0] == s
[0]) || (p
[0] == '?'))
94 return strcmp_pattern(p
+1, s
+1);
100 /* extract possible {attr} and move str behind it */
101 static char *get_format_attribute(char **str
)
106 if (*str
[0] == '{') {
107 pos
= strchr(*str
, '}');
109 dbg("missing closing brace for format");
115 dbg("attribute='%s', str='%s'", attr
, *str
);
120 /* extract possible format length and move str behind it*/
121 static int get_format_len(char **str
)
126 if (isdigit(*str
[0])) {
127 num
= (int) strtoul(*str
, &tail
, 10);
130 dbg("format length=%i", num
);
133 dbg("format parsing error '%s'", *str
);
139 /** Finds the lowest positive N such that <name>N isn't present in
140 * $(udevroot) either as a file or a symlink.
142 * @param name Name to check for
143 * @return 0 if <name> didn't exist and N otherwise.
145 static int find_free_number(struct udevice
*udev
, const char *name
)
147 char filename
[NAME_SIZE
];
149 struct udevice db_udev
;
151 strfieldcpy(filename
, name
);
153 dbg("look for existing node '%s'", filename
);
154 memset(&db_udev
, 0x00, sizeof(struct udevice
));
155 if (udev_db_get_device_byname(&db_udev
, filename
) != 0) {
156 dbg("free num=%d", num
);
162 info("find_free_number gone crazy (num=%d), aborted", num
);
165 snprintf(filename
, NAME_SIZE
, "%s%d", name
, num
);
166 filename
[NAME_SIZE
-1] = '\0';
170 static void apply_format(struct udevice
*udev
, char *string
, size_t maxsize
,
171 struct sysfs_class_device
*class_dev
,
172 struct sysfs_device
*sysfs_device
)
174 char temp
[NAME_SIZE
];
175 char temp2
[NAME_SIZE
];
185 struct sysfs_attribute
*tmpattr
;
186 unsigned int next_free_number
;
187 struct sysfs_class_device
*class_dev_parent
;
191 pos
= strchr(pos
, '%');
197 len
= get_format_len(&tail
);
199 strfieldcpy(temp
, tail
+1);
201 dbg("format=%c, string='%s', tail='%s'",c
, string
, tail
);
202 attr
= get_format_attribute(&tail
);
207 if (strlen(udev
->devpath
) == 0)
209 strfieldcatmax(string
, udev
->devpath
, maxsize
);
210 dbg("substitute kernel name '%s'", udev
->kernel_name
);
213 if (strlen(udev
->bus_id
) == 0)
215 strfieldcatmax(string
, udev
->bus_id
, maxsize
);
216 dbg("substitute bus_id '%s'", udev
->bus_id
);
219 if (strlen(udev
->kernel_name
) == 0)
221 strfieldcatmax(string
, udev
->kernel_name
, maxsize
);
222 dbg("substitute kernel name '%s'", udev
->kernel_name
);
225 if (strlen(udev
->kernel_number
) == 0)
227 strfieldcatmax(string
, udev
->kernel_number
, maxsize
);
228 dbg("substitute kernel number '%s'", udev
->kernel_number
);
231 strintcatmax(string
, minor(udev
->devt
), maxsize
);
232 dbg("substitute minor number '%u'", minor(udev
->devt
));
235 strintcatmax(string
, major(udev
->devt
), maxsize
);
236 dbg("substitute major number '%u'", major(udev
->devt
));
239 if (strlen(udev
->program_result
) == 0)
241 /* get part part of the result string */
244 i
= strtoul(attr
, &rest
, 10);
246 foreach_strpart(udev
->program_result
, " \n\r", spos
, slen
) {
252 dbg("requested part of result string not found");
256 strfieldcpy(temp2
, spos
);
258 strfieldcpymax(temp2
, spos
, slen
+1);
259 strfieldcatmax(string
, temp2
, maxsize
);
260 dbg("substitute part of result string '%s'", temp2
);
262 strfieldcatmax(string
, udev
->program_result
, maxsize
);
263 dbg("substitute result string '%s'", udev
->program_result
);
268 tmpattr
= find_sysfs_attribute(class_dev
, sysfs_device
, attr
);
269 if (tmpattr
== NULL
) {
270 dbg("sysfa attribute '%s' not found", attr
);
273 /* strip trailing whitespace of matching value */
274 if (isspace(tmpattr
->value
[strlen(tmpattr
->value
)-1])) {
275 i
= len
= strlen(tmpattr
->value
);
276 while (i
> 0 && isspace(tmpattr
->value
[i
-1]))
279 tmpattr
->value
[i
] = '\0';
280 dbg("remove %i trailing whitespace chars from '%s'",
281 len
- i
, tmpattr
->value
);
284 strfieldcatmax(string
, tmpattr
->value
, maxsize
);
285 dbg("substitute sysfs value '%s'", tmpattr
->value
);
287 dbg("missing attribute");
291 strfieldcatmax(string
, "%", maxsize
);
295 next_free_number
= find_free_number(udev
, string
);
296 if (next_free_number
> 0) {
297 sprintf(temp2
, "%d", next_free_number
);
298 strfieldcatmax(string
, temp2
, maxsize
);
302 class_dev_parent
= sysfs_get_classdev_parent(class_dev
);
303 if (class_dev_parent
!= NULL
) {
304 struct udevice udev_parent
;
306 dbg("found parent '%s', get the node name", class_dev_parent
->path
);
307 memset(&udev_parent
, 0x00, sizeof(struct udevice
));
308 /* lookup the name in the udev_db with the DEVPATH of the parent */
309 strfieldcpy(udev_parent
.devpath
, &class_dev_parent
->path
[strlen(sysfs_path
)]);
310 if (udev_db_get_device(&udev_parent
) == 0) {
311 strfieldcatmax(string
, udev_parent
.name
, maxsize
);
312 dbg("substitute parent node name'%s'", udev_parent
.name
);
314 dbg("parent not found in database");
318 if (udev
->tmp_node
[0] == '\0') {
319 dbg("create temporary device node for callout");
320 snprintf(udev
->tmp_node
, NAME_SIZE
, "%s/.tmp-%u-%u", udev_root
, major(udev
->devt
), minor(udev
->devt
));
321 udev
->tmp_node
[NAME_SIZE
] = '\0';
322 udev_make_node(udev
, udev
->tmp_node
, udev
->devt
, 0600, 0, 0);
324 strfieldcatmax(string
, udev
->tmp_node
, maxsize
);
325 dbg("substitute temporary device node name '%s'", udev
->tmp_node
);
328 strfieldcatmax(string
, udev_root
, maxsize
);
329 dbg("substitute udev_root '%s'", udev_root
);
332 dbg("unknown substitution type '%%%c'", c
);
335 /* truncate to specified length */
339 strfieldcatmax(string
, tail
, maxsize
);
343 static void fix_kernel_name(struct udevice
*udev
)
345 char *temp
= udev
->kernel_name
;
347 while (*temp
!= 0x00) {
348 /* Some block devices have a ! in their name,
349 * we need to change that to / */
356 static int execute_program(struct udevice
*udev
, const char *path
, char *value
, int len
)
364 char arg
[PROGRAM_SIZE
];
365 char *argv
[(PROGRAM_SIZE
/ 2) + 1];
368 strfieldcpy(arg
, path
);
370 if (strchr(path
, ' ')) {
372 while (pos
!= NULL
) {
373 if (pos
[0] == '\'') {
374 /* don't separate if in apostrophes */
376 argv
[i
] = strsep(&pos
, "\'");
377 while (pos
&& pos
[0] == ' ')
380 argv
[i
] = strsep(&pos
, " ");
382 dbg("arg[%i] '%s'", i
, argv
[i
]);
386 dbg("execute '%s' with parsed arguments", arg
);
389 argv
[1] = udev
->subsystem
;
391 dbg("execute '%s' with subsystem '%s' argument", arg
, argv
[1]);
404 /* dup2 write side of pipe to STDOUT */
405 dup2(fds
[1], STDOUT_FILENO
);
406 retval
= execv(arg
, argv
);
408 info(FIELD_PROGRAM
" execution of '%s' failed", path
);
414 /* parent reads from fds[0] */
419 count
= read(fds
[0], value
+ i
, len
- i
-1);
425 dbg("result len %d too short", len
);
432 dbg("read failed with '%s'", strerror(errno
));
436 if (i
> 0 && value
[i
-1] == '\n')
439 dbg("result is '%s'", value
);
442 waitpid(pid
, &status
, 0);
444 if (!WIFEXITED(status
) || (WEXITSTATUS(status
) != 0)) {
445 dbg("exec program status 0x%x", status
);
452 static struct sysfs_attribute
*find_sysfs_attribute(struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
, char *attr
)
454 struct sysfs_attribute
*tmpattr
= NULL
;
457 dbg("look for device attribute '%s'", attr
);
458 /* try to find the attribute in the class device directory */
459 tmpattr
= sysfs_get_classdev_attr(class_dev
, attr
);
463 /* look in the class device directory if present */
465 tmpattr
= sysfs_get_device_attr(sysfs_device
, attr
);
473 c
= strchr(tmpattr
->value
, '\n');
477 dbg("found attribute '%s'", tmpattr
->path
);
481 static int compare_sysfs_attribute(struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
, struct sysfs_pair
*pair
)
483 struct sysfs_attribute
*tmpattr
;
487 if ((pair
== NULL
) || (pair
->file
[0] == '\0') || (pair
->value
== '\0'))
490 tmpattr
= find_sysfs_attribute(class_dev
, sysfs_device
, pair
->file
);
494 /* strip trailing whitespace of value, if not asked to match for it */
495 if (! isspace(pair
->value
[strlen(pair
->value
)-1])) {
496 i
= len
= strlen(tmpattr
->value
);
497 while (i
> 0 && isspace(tmpattr
->value
[i
-1]))
500 tmpattr
->value
[i
] = '\0';
501 dbg("remove %i trailing whitespace chars from '%s'",
502 len
- i
, tmpattr
->value
);
506 dbg("compare attribute '%s' value '%s' with '%s'",
507 pair
->file
, tmpattr
->value
, pair
->value
);
508 if (strcmp_pattern(pair
->value
, tmpattr
->value
) != 0)
511 dbg("found matching attribute '%s' with value '%s'",
512 pair
->file
, pair
->value
);
516 static int match_sysfs_pairs(struct config_device
*dev
, struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
)
518 struct sysfs_pair
*pair
;
521 for (i
= 0; i
< MAX_SYSFS_PAIRS
; ++i
) {
522 pair
= &dev
->sysfs_pair
[i
];
523 if ((pair
->file
[0] == '\0') || (pair
->value
[0] == '\0'))
525 if (compare_sysfs_attribute(class_dev
, sysfs_device
, pair
) != 0) {
526 dbg("sysfs attribute doesn't match");
534 static int match_id(struct config_device
*dev
, struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
)
536 char path
[SYSFS_PATH_MAX
];
539 /* we have to have a sysfs device for ID to work */
543 strfieldcpy(path
, sysfs_device
->path
);
544 temp
= strrchr(path
, '/');
546 dbg("search '%s' in '%s', path='%s'", dev
->id
, temp
, path
);
547 if (strcmp_pattern(dev
->id
, temp
) != 0)
553 static int match_place(struct config_device
*dev
, struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
)
555 char path
[SYSFS_PATH_MAX
];
558 /* we have to have a sysfs device for PLACE to work */
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
)
570 temp
= strrchr(path
, '/');
571 dbg("search '%s' in '%s', path='%s'", dev
->place
, temp
, path
);
572 if (strstr(temp
, dev
->place
) == NULL
)
575 dbg("place doesn't match");
579 static int match_rule(struct udevice
*udev
, struct config_device
*dev
,
580 struct sysfs_class_device
*class_dev
, struct sysfs_device
*sysfs_device
)
582 /* check for matching kernel name */
583 if (dev
->kernel
[0] != '\0') {
584 dbg("check for " FIELD_KERNEL
" dev->kernel='%s' class_dev->name='%s'",
585 dev
->kernel
, class_dev
->name
);
586 if (strcmp_pattern(dev
->kernel
, class_dev
->name
) != 0) {
587 dbg(FIELD_KERNEL
" is not matching");
590 dbg(FIELD_KERNEL
" matches");
593 /* check for matching subsystem */
594 if (dev
->subsystem
[0] != '\0') {
595 dbg("check for " FIELD_SUBSYSTEM
" dev->subsystem='%s' class_dev->name='%s'",
596 dev
->subsystem
, class_dev
->name
);
597 if (strcmp_pattern(dev
->subsystem
, udev
->subsystem
) != 0) {
598 dbg(FIELD_SUBSYSTEM
" is not matching");
601 dbg(FIELD_SUBSYSTEM
" matches");
604 /* walk up the chain of physical devices and find a match */
606 /* check for matching driver */
607 if (dev
->driver
[0] != '\0') {
608 dbg("check for " FIELD_DRIVER
" dev->driver='%s' sysfs_device->driver_name='%s'",
609 dev
->driver
, sysfs_device
->driver_name
);
610 if (strcmp_pattern(dev
->driver
, sysfs_device
->driver_name
) != 0) {
611 dbg(FIELD_DRIVER
" is not matching");
614 dbg(FIELD_DRIVER
" matches");
617 /* check for matching bus value */
618 if (dev
->bus
[0] != '\0') {
619 if (sysfs_device
== NULL
) {
620 dbg("device has no bus");
623 dbg("check for " FIELD_BUS
" dev->bus='%s' sysfs_device->bus='%s'",
624 dev
->bus
, sysfs_device
->bus
);
625 if (strcmp_pattern(dev
->bus
, sysfs_device
->bus
) != 0) {
626 dbg(FIELD_BUS
" is not matching");
629 dbg(FIELD_BUS
" matches");
632 /* check for matching bus id */
633 if (dev
->id
[0] != '\0') {
634 dbg("check " FIELD_ID
);
635 if (match_id(dev
, class_dev
, sysfs_device
) != 0) {
636 dbg(FIELD_ID
" is not matching");
639 dbg(FIELD_ID
" matches");
642 /* check for matching place of device */
643 if (dev
->place
[0] != '\0') {
644 dbg("check " FIELD_PLACE
);
645 if (match_place(dev
, class_dev
, sysfs_device
) != 0) {
646 dbg(FIELD_PLACE
" is not matching");
649 dbg(FIELD_PLACE
" matches");
652 /* check for matching sysfs pairs */
653 if (dev
->sysfs_pair
[0].file
[0] != '\0') {
654 dbg("check " FIELD_SYSFS
" pairs");
655 if (match_sysfs_pairs(dev
, class_dev
, sysfs_device
) != 0) {
656 dbg(FIELD_SYSFS
" is not matching");
659 dbg(FIELD_SYSFS
" matches");
662 /* found matching physical device */
665 dbg("try parent sysfs device");
666 sysfs_device
= sysfs_get_device_parent(sysfs_device
);
667 if (sysfs_device
== NULL
)
669 dbg("sysfs_device->path='%s'", sysfs_device
->path
);
670 dbg("sysfs_device->bus_id='%s'", sysfs_device
->bus_id
);
673 /* execute external program */
674 if (dev
->program
[0] != '\0') {
675 char program
[PROGRAM_SIZE
];
677 dbg("check " FIELD_PROGRAM
);
678 strfieldcpy(program
, dev
->program
);
679 apply_format(udev
, program
, sizeof(program
), class_dev
, sysfs_device
);
680 if (execute_program(udev
, program
, udev
->program_result
, NAME_SIZE
) != 0) {
681 dbg(FIELD_PROGRAM
" returned nonzero");
684 dbg(FIELD_PROGRAM
" returned successful");
687 /* check for matching result of external program */
688 if (dev
->result
[0] != '\0') {
689 dbg("check for " FIELD_RESULT
" dev->result='%s', udev->program_result='%s'",
690 dev
->result
, udev
->program_result
);
691 if (strcmp_pattern(dev
->result
, udev
->program_result
) != 0) {
692 dbg(FIELD_RESULT
" is not matching");
695 dbg(FIELD_RESULT
" matches");
705 int namedev_name_device(struct udevice
*udev
, struct sysfs_class_device
*class_dev
)
707 struct sysfs_class_device
*class_dev_parent
;
708 struct sysfs_device
*sysfs_device
= NULL
;
709 struct config_device
*dev
;
712 dbg("class_dev->name='%s'", class_dev
->name
);
714 /* Figure out where the "device"-symlink is at. For char devices this will
715 * always be in the class_dev->path. On block devices, only the main block
716 * device will have the device symlink in it's path. All partition devices
717 * need to look at the symlink in its parent directory.
719 class_dev_parent
= sysfs_get_classdev_parent(class_dev
);
720 if (class_dev_parent
!= NULL
) {
721 dbg("given class device has a parent, use this instead");
722 sysfs_device
= sysfs_get_classdev_device(class_dev_parent
);
724 sysfs_device
= sysfs_get_classdev_device(class_dev
);
728 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
729 sysfs_device
->path
, sysfs_device
->bus_id
, sysfs_device
->bus
);
730 strfieldcpy(udev
->bus_id
, sysfs_device
->bus_id
);
733 strfieldcpy(udev
->kernel_name
, class_dev
->name
);
734 fix_kernel_name(udev
);
735 dbg("udev->kernel_name = '%s'", udev
->kernel_name
);
737 /* get kernel number */
738 pos
= class_dev
->name
+ strlen(class_dev
->name
);
739 while (isdigit(*(pos
-1)))
741 strfieldcpy(udev
->kernel_number
, pos
);
742 dbg("kernel_number='%s'", udev
->kernel_number
);
744 /* look for a matching rule to apply */
745 list_for_each_entry(dev
, &config_device_list
, node
) {
747 if (match_rule(udev
, dev
, class_dev
, sysfs_device
) == 0) {
749 /* FIXME: remove old style ignore rule and make OPTION="ignore" mandatory */
750 if (dev
->name
[0] == '\0' && dev
->symlink
[0] == '\0' &&
751 dev
->mode
== 0000 && dev
->owner
[0] == '\0' && dev
->group
[0] == '\0' &&
752 !dev
->ignore_device
&& !dev
->partitions
&& !dev
->ignore_remove
) {
753 info("configured rule in '%s[%i]' applied, '%s' is ignored",
754 dev
->config_file
, dev
->config_line
, udev
->kernel_name
);
759 if (dev
->ignore_device
) {
760 info("configured rule in '%s[%i]' applied, '%s' is ignored",
761 dev
->config_file
, dev
->config_line
, udev
->kernel_name
);
764 if (dev
->ignore_remove
) {
765 udev
->ignore_remove
= dev
->ignore_remove
;
766 dbg_parse("remove event should be ignored");
768 /* apply all_partitions option only at a main block device */
769 if (dev
->partitions
&& udev
->type
== BLOCK
&& udev
->kernel_number
[0] == '\0') {
770 udev
->partitions
= dev
->partitions
;
771 dbg("creation of partition nodes requested");
774 /* apply permissions */
775 if (dev
->mode
!= 0000) {
776 udev
->mode
= dev
->mode
;
777 dbg("applied mode=%#o to '%s'", udev
->mode
, udev
->kernel_name
);
779 if (dev
->owner
[0] != '\0') {
780 strfieldcpy(udev
->owner
, dev
->owner
);
781 apply_format(udev
, udev
->owner
, sizeof(udev
->owner
), class_dev
, sysfs_device
);
782 dbg("applied owner='%s' to '%s'", udev
->owner
, udev
->kernel_name
);
784 if (dev
->group
[0] != '\0') {
785 strfieldcpy(udev
->group
, dev
->group
);
786 apply_format(udev
, udev
->group
, sizeof(udev
->group
), class_dev
, sysfs_device
);
787 dbg("applied group='%s' to '%s'", udev
->group
, udev
->kernel_name
);
790 /* collect symlinks for this or the final matching rule */
791 if (dev
->symlink
[0] != '\0') {
792 char temp
[NAME_SIZE
];
794 info("configured rule in '%s[%i]' applied, added symlink '%s'",
795 dev
->config_file
, dev
->config_line
, dev
->symlink
);
796 strfieldcpy(temp
, dev
->symlink
);
797 apply_format(udev
, temp
, sizeof(temp
), class_dev
, sysfs_device
);
798 if (udev
->symlink
[0] != '\0')
799 strfieldcat(udev
->symlink
, " ");
800 strfieldcat(udev
->symlink
, temp
);
804 if (dev
->name
[0] != '\0') {
805 info("configured rule in '%s[%i]' applied, '%s' becomes '%s'",
806 dev
->config_file
, dev
->config_line
, udev
->kernel_name
, dev
->name
);
808 strfieldcpy(udev
->name
, dev
->name
);
809 apply_format(udev
, udev
->name
, sizeof(udev
->name
), class_dev
, sysfs_device
);
810 strfieldcpy(udev
->config_file
, dev
->config_file
);
811 udev
->config_line
= dev
->config_line
;
813 if (udev
->type
!= NET
)
814 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
815 udev
->name
, udev
->owner
, udev
->group
, udev
->mode
, udev
->partitions
);
822 /* no rule matched, so we use the kernel name */
823 strfieldcpy(udev
->name
, udev
->kernel_name
);
824 dbg("no rule found, use kernel name '%s'", udev
->name
);
827 if (udev
->tmp_node
[0] != '\0') {
828 dbg("removing temporary device node");
829 unlink_secure(udev
->tmp_node
);
830 udev
->tmp_node
[0] = '\0';