2 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2003-2006 Kay Sievers <kay.sievers@vrfy.org>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 #include <sys/socket.h>
37 #include "udev_rules.h"
39 extern char **environ;
41 /* extract possible {attr} and move str behind it */
42 static char *get_format_attribute(char **str)
48 pos = strchr(*str, '}');
50 err("missing closing brace for format");
56 dbg("attribute='%s', str='%s'", attr, *str);
61 /* extract possible format length and move str behind it*/
62 static int get_format_len(char **str)
67 if (isdigit(*str[0])) {
68 num = (int) strtoul(*str, &tail, 10);
71 dbg("format length=%i", num);
74 err("format parsing error '%s'", *str);
80 static int get_key(char **line, char **key, char **value)
90 while (isspace(linepos[0]))
94 temp = strchr(linepos, '=');
95 if (temp == NULL || temp == linepos)
101 /* get a quoted value */
102 if (linepos[0] == '"' || linepos[0] == '\'') {
103 temp = strchr(&linepos[1], linepos[0]);
106 *value = &linepos[1];
112 temp = strchr(linepos, '\n');
120 static int run_program(const char *command, const char *subsystem,
121 char *result, size_t ressize, size_t *reslen)
124 int outpipe[2] = {-1, -1};
125 int errpipe[2] = {-1, -1};
128 char program[PATH_SIZE];
129 char *argv[(sizeof(arg) / 2) + 1];
134 /* build argv from comand */
135 strlcpy(arg, command, sizeof(arg));
137 if (strchr(arg, ' ') != NULL) {
140 while (pos != NULL) {
141 if (pos[0] == '\'') {
142 /* don't separate if in apostrophes */
144 argv[i] = strsep(&pos, "\'");
145 while (pos != NULL && pos[0] == ' ')
148 argv[i] = strsep(&pos, " ");
150 dbg("arg[%i] '%s'", i, argv[i]);
158 info("'%s'", command);
160 /* prepare pipes from child to parent */
161 if (result != NULL || udev_log_priority >= LOG_INFO) {
162 if (pipe(outpipe) != 0) {
163 err("pipe failed: %s", strerror(errno));
167 if (udev_log_priority >= LOG_INFO) {
168 if (pipe(errpipe) != 0) {
169 err("pipe failed: %s", strerror(errno));
174 /* allow programs in /lib/udev called without the path */
175 if (strchr(argv[0], '/') == NULL) {
176 strlcpy(program, "/lib/udev/", sizeof(program));
177 strlcat(program, argv[0], sizeof(program));
184 /* child closes parent ends of pipes */
185 if (outpipe[READ_END] > 0)
186 close(outpipe[READ_END]);
187 if (errpipe[READ_END] > 0)
188 close(errpipe[READ_END]);
190 /* discard child output or connect to pipe */
191 devnull = open("/dev/null", O_RDWR);
193 dup2(devnull, STDIN_FILENO);
194 if (outpipe[WRITE_END] < 0)
195 dup2(devnull, STDOUT_FILENO);
196 if (errpipe[WRITE_END] < 0)
197 dup2(devnull, STDERR_FILENO);
200 err("open /dev/null failed: %s", strerror(errno));
201 if (outpipe[WRITE_END] > 0) {
202 dup2(outpipe[WRITE_END], STDOUT_FILENO);
203 close(outpipe[WRITE_END]);
205 if (errpipe[WRITE_END] > 0) {
206 dup2(errpipe[WRITE_END], STDERR_FILENO);
207 close(errpipe[WRITE_END]);
209 execv(argv[0], argv);
210 if (errno == ENOENT || errno == ENOTDIR) {
211 /* may be on a filesytem which is not mounted right now */
212 info("program '%s' not found", argv[0]);
215 err("exec of program '%s' failed", argv[0]);
219 err("fork of '%s' failed: %s", argv[0], strerror(errno));
222 /* read from child if requested */
223 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
227 /* parent closes child ends of pipes */
228 if (outpipe[WRITE_END] > 0)
229 close(outpipe[WRITE_END]);
230 if (errpipe[WRITE_END] > 0)
231 close(errpipe[WRITE_END]);
233 /* read child output */
234 while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
239 if (outpipe[READ_END] > 0)
240 FD_SET(outpipe[READ_END], &readfds);
241 if (errpipe[READ_END] > 0)
242 FD_SET(errpipe[READ_END], &readfds);
243 fdcount = select(UDEV_MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
252 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
257 count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
259 close(outpipe[READ_END]);
260 outpipe[READ_END] = -1;
262 err("stdin read failed: %s", strerror(errno));
269 /* store result for rule processing */
271 if (respos + count < ressize) {
272 memcpy(&result[respos], inbuf, count);
275 err("ressize %ld too short", (long)ressize);
280 while ((line = strsep(&pos, "\n")))
281 if (pos || line[0] != '\0')
282 info("'%s' (stdout) '%s'", argv[0], line);
286 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
291 count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
293 close(errpipe[READ_END]);
294 errpipe[READ_END] = -1;
296 err("stderr read failed: %s", strerror(errno));
299 errbuf[count] = '\0';
301 while ((line = strsep(&pos, "\n")))
302 if (pos || line[0] != '\0')
303 info("'%s' (stderr) '%s'", argv[0], line);
306 if (outpipe[READ_END] > 0)
307 close(outpipe[READ_END]);
308 if (errpipe[READ_END] > 0)
309 close(errpipe[READ_END]);
311 /* return the childs stdout string */
313 result[respos] = '\0';
314 dbg("result='%s'", result);
319 waitpid(pid, &status, 0);
320 if (WIFEXITED(status)) {
321 info("'%s' returned with status %i", argv[0], WEXITSTATUS(status));
322 if (WEXITSTATUS(status) != 0)
325 err("'%s' abnormal exit", argv[0]);
333 static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bufsize)
335 char line[LINE_SIZE];
344 /* loop through the whole buffer */
347 while (cur < bufsize) {
348 count = buf_get_line(buf, bufsize, cur);
353 /* eat the whitespace */
354 while ((count > 0) && isspace(bufline[0])) {
361 /* see if this is a comment */
362 if (bufline[0] == COMMENT_CHARACTER)
365 if (count >= sizeof(line)) {
366 err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
370 memcpy(line, bufline, count);
374 if (get_key(&linepos, &variable, &value) == 0) {
375 dbg("import '%s=%s'", variable, value);
377 /* handle device, renamed by external tool, returning new path */
378 if (strcmp(variable, "DEVPATH") == 0) {
379 info("updating devpath from '%s' to '%s'", udev->dev->devpath, value);
380 sysfs_device_set_values(udev->dev, value, NULL, NULL);
382 name_list_key_add(&udev->env_list, variable, value);
383 setenv(variable, value, 1);
390 static int import_file_into_env(struct udevice *udev, const char *filename)
395 if (file_map(filename, &buf, &bufsize) != 0) {
396 err("can't open '%s': %s", filename, strerror(errno));
399 import_keys_into_env(udev, buf, bufsize);
400 file_unmap(buf, bufsize);
405 static int import_program_into_env(struct udevice *udev, const char *program)
410 if (run_program(program, udev->dev->subsystem, result, sizeof(result), &reslen) != 0)
412 return import_keys_into_env(udev, result, reslen);
415 static int import_parent_into_env(struct udevice *udev, const char *filter)
417 struct sysfs_device *dev_parent;
420 dev_parent = sysfs_device_get_parent(udev->dev);
421 if (dev_parent != NULL) {
422 struct udevice *udev_parent;
423 struct name_entry *name_loop;
425 dbg("found parent '%s', get the node name", dev_parent->devpath);
426 udev_parent = udev_device_init(NULL);
427 if (udev_parent == NULL)
429 /* import the udev_db of the parent */
430 if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
431 dbg("import stored parent env '%s'", udev_parent->name);
432 list_for_each_entry(name_loop, &udev_parent->env_list, node) {
433 char name[NAME_SIZE];
436 strlcpy(name, name_loop->name, sizeof(name));
437 pos = strchr(name, '=');
441 if (fnmatch(filter, name, 0) == 0) {
442 dbg("import key '%s'", name_loop->name);
443 name_list_add(&udev->env_list, name_loop->name, 0);
444 setenv(name, pos, 1);
446 dbg("skip key '%s'", name_loop->name);
451 dbg("parent not found in database");
452 udev_device_cleanup(udev_parent);
458 static int pass_env_to_socket(const char *sockname, const char *devpath, const char *action)
461 struct sockaddr_un saddr;
469 dbg("pass environment to socket '%s'", sockname);
470 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
471 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
472 saddr.sun_family = AF_LOCAL;
473 /* abstract namespace only */
474 strcpy(&saddr.sun_path[1], sockname);
475 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
477 bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
479 for (i = 0; environ[i] != NULL && bufpos < (sizeof(buf)-1); i++) {
480 bufpos += strlcpy(&buf[bufpos], environ[i], sizeof(buf) - bufpos-1);
483 if (bufpos > sizeof(buf))
484 bufpos = sizeof(buf);
486 count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, addrlen);
489 info("passed %zi bytes to socket '%s', ", count, sockname);
495 int udev_rules_run(struct udevice *udev)
497 struct name_entry *name_loop;
500 dbg("executing run list");
501 list_for_each_entry(name_loop, &udev->run_list, node) {
502 if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0) {
503 pass_env_to_socket(&name_loop->name[strlen("socket:")], udev->dev->devpath, udev->action);
505 char program[PATH_SIZE];
507 strlcpy(program, name_loop->name, sizeof(program));
508 udev_rules_apply_format(udev, program, sizeof(program));
509 if (run_program(program, udev->dev->subsystem, NULL, 0, NULL) != 0)
510 if (!name_loop->ignore_error)
518 #define WAIT_LOOP_PER_SECOND 50
519 static int wait_for_sysfs(struct udevice *udev, const char *file, int timeout)
521 char devicepath[PATH_SIZE];
522 char filepath[PATH_SIZE];
524 int loop = timeout * WAIT_LOOP_PER_SECOND;
526 strlcpy(devicepath, sysfs_path, sizeof(devicepath));
527 strlcat(devicepath, udev->dev->devpath, sizeof(devicepath));
528 strlcpy(filepath, devicepath, sizeof(filepath));
529 strlcat(filepath, "/", sizeof(filepath));
530 strlcat(filepath, file, sizeof(filepath));
532 dbg("will wait %i sec for '%s'", timeout, filepath);
535 if (stat(filepath, &stats) == 0) {
536 info("file '%s' appeared after %i loops", filepath, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
539 /* make sure, the device did not disappear in the meantime */
540 if (stat(devicepath, &stats) != 0) {
541 info("device disappeared while waiting for '%s'", filepath);
544 info("wait for '%s' for %i mseconds", filepath, 1000 / WAIT_LOOP_PER_SECOND);
545 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
547 info("waiting for '%s' failed", filepath);
551 /* handle "[$SUBSYSTEM/$KERNEL]<attribute>" lookup */
552 static int attr_get_by_subsys_id(const char *attrstr, char *devpath, size_t len, char **attr)
554 char subsys[NAME_SIZE];
559 if (attrstr[0] != '[')
562 strlcpy(subsys, &attrstr[1], sizeof(subsys));
564 attrib = strchr(subsys, ']');
570 id = strchr(subsys, '/');
576 if (sysfs_lookup_devpath_by_subsys_id(devpath, len, subsys, id)) {
578 if (attrib[0] != '\0')
589 void udev_rules_apply_format(struct udevice *udev, char *string, size_t maxsize)
591 char temp[PATH_SIZE];
592 char temp2[PATH_SIZE];
593 char *head, *tail, *pos, *cpos, *attr, *rest;
615 static const struct subst_map {
618 enum subst_type type;
620 { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
621 { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
622 { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL },
623 { .name = "id", .fmt = 'b', .type = SUBST_ID },
624 { .name = "driver", .fmt = 'd', .type = SUBST_DRIVER },
625 { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
626 { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
627 { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
628 { .name = "attr", .fmt = 's', .type = SUBST_ATTR },
629 { .name = "sysfs", .fmt = 's', .type = SUBST_ATTR },
630 { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
631 { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
632 { .name = "name", .fmt = 'D', .type = SUBST_NAME },
633 { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
634 { .name = "sys", .fmt = 'S', .type = SUBST_SYS },
635 { .name = "env", .fmt = 'E', .type = SUBST_ENV },
638 enum subst_type type;
639 const struct subst_map *subst;
644 while (head[0] != '\0') {
645 if (head[0] == '$') {
646 /* substitute named variable */
649 if (head[1] == '$') {
650 strlcpy(temp, head+2, sizeof(temp));
651 strlcpy(head+1, temp, maxsize);
656 for (subst = map; subst->name; subst++) {
657 if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
659 tail = head + strlen(subst->name)+1;
660 dbg("will substitute format name '%s'", subst->name);
665 err("unknown format variable '%s'", head);
666 } else if (head[0] == '%') {
667 /* substitute format char */
670 if (head[1] == '%') {
671 strlcpy(temp, head+2, sizeof(temp));
672 strlcpy(head+1, temp, maxsize);
678 len = get_format_len(&tail);
679 for (subst = map; subst->name; subst++) {
680 if (tail[0] == subst->fmt) {
683 dbg("will substitute format char '%c'", subst->fmt);
688 err("unknown format char '%c'", tail[0]);
694 attr = get_format_attribute(&tail);
695 strlcpy(temp, tail, sizeof(temp));
696 dbg("format=%i, string='%s', tail='%s'", type ,string, tail);
700 strlcat(string, udev->dev->devpath, maxsize);
701 dbg("substitute devpath '%s'", udev->dev->devpath);
704 strlcat(string, udev->dev->kernel, maxsize);
705 dbg("substitute kernel name '%s'", udev->dev->kernel);
707 case SUBST_KERNEL_NUMBER:
708 strlcat(string, udev->dev->kernel_number, maxsize);
709 dbg("substitute kernel number '%s'", udev->dev->kernel_number);
712 if (udev->dev_parent != NULL) {
713 strlcat(string, udev->dev_parent->kernel, maxsize);
714 dbg("substitute id '%s'", udev->dev_parent->kernel);
718 if (udev->dev_parent != NULL) {
719 strlcat(string, udev->dev_parent->driver, maxsize);
720 dbg("substitute driver '%s'", udev->dev_parent->driver);
724 sprintf(temp2, "%d", major(udev->devt));
725 strlcat(string, temp2, maxsize);
726 dbg("substitute major number '%s'", temp2);
729 sprintf(temp2, "%d", minor(udev->devt));
730 strlcat(string, temp2, maxsize);
731 dbg("substitute minor number '%s'", temp2);
734 if (udev->program_result[0] == '\0')
736 /* get part part of the result string */
739 i = strtoul(attr, &rest, 10);
741 dbg("request part #%d of result string", i);
742 cpos = udev->program_result;
744 while (cpos[0] != '\0' && !isspace(cpos[0]))
746 while (isspace(cpos[0]))
750 err("requested part of result string not found");
753 strlcpy(temp2, cpos, sizeof(temp2));
754 /* %{2+}c copies the whole string from the second part on */
755 if (rest[0] != '+') {
756 cpos = strchr(temp2, ' ');
760 strlcat(string, temp2, maxsize);
761 dbg("substitute part of result string '%s'", temp2);
763 strlcat(string, udev->program_result, maxsize);
764 dbg("substitute result string '%s'", udev->program_result);
769 err("missing file parameter for attr");
771 char devpath[PATH_SIZE];
773 const char *value = NULL;
776 if (attr_get_by_subsys_id(attr, devpath, sizeof(devpath), &attrib)) {
778 value = sysfs_attr_get_value(devpath, attrib);
783 /* try the current device, other matches may have selected */
784 if (value == NULL && udev->dev_parent != NULL && udev->dev_parent != udev->dev)
785 value = sysfs_attr_get_value(udev->dev_parent->devpath, attr);
787 /* look at all devices along the chain of parents */
789 struct sysfs_device *dev_parent = udev->dev;
792 dbg("looking at '%s'", dev_parent->devpath);
793 value = sysfs_attr_get_value(dev_parent->devpath, attr);
795 strlcpy(temp2, value, sizeof(temp2));
798 dev_parent = sysfs_device_get_parent(dev_parent);
799 } while (dev_parent != NULL);
805 /* strip trailing whitespace, and replace unwanted characters */
806 size = strlcpy(temp2, value, sizeof(temp2));
807 if (size >= sizeof(temp2))
808 size = sizeof(temp2)-1;
809 while (size > 0 && isspace(temp2[size-1]))
810 temp2[--size] = '\0';
811 count = replace_chars(temp2, ALLOWED_CHARS_INPUT);
813 info("%i character(s) replaced" , count);
814 strlcat(string, temp2, maxsize);
815 dbg("substitute sysfs value '%s'", temp2);
820 struct sysfs_device *dev_parent;
822 dev_parent = sysfs_device_get_parent(udev->dev);
823 if (dev_parent != NULL) {
824 struct udevice *udev_parent;
826 dbg("found parent '%s', get the node name", dev_parent->devpath);
827 udev_parent = udev_device_init(NULL);
828 if (udev_parent != NULL) {
829 /* lookup the name in the udev_db with the DEVPATH of the parent */
830 if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
831 strlcat(string, udev_parent->name, maxsize);
832 dbg("substitute parent node name'%s'", udev_parent->name);
834 dbg("parent not found in database");
835 udev_device_cleanup(udev_parent);
840 case SUBST_TEMP_NODE:
841 if (udev->tmp_node[0] == '\0' && major(udev->devt) > 0) {
842 dbg("create temporary device node for callout");
843 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
844 udev_root, major(udev->devt), minor(udev->devt));
845 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
846 udev_node_mknod(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
848 strlcat(string, udev->tmp_node, maxsize);
849 dbg("substitute temporary device node name '%s'", udev->tmp_node);
852 strlcat(string, udev->name, maxsize);
853 dbg("substitute udev->name '%s'", udev->name);
856 strlcat(string, udev_root, maxsize);
857 dbg("substitute udev_root '%s'", udev_root);
860 strlcat(string, sysfs_path, maxsize);
861 dbg("substitute sysfs_path '%s'", sysfs_path);
865 dbg("missing attribute");
870 dbg("env '%s' not available", attr);
873 dbg("substitute env '%s=%s'", attr, pos);
874 strlcat(string, pos, maxsize);
877 err("unknown substitution type=%i", type);
880 /* possibly truncate to format-char specified length */
881 if (len >= 0 && len < (int)strlen(head)) {
883 dbg("truncate to %i chars, subtitution string becomes '%s'", len, head);
885 strlcat(string, temp, maxsize);
889 static char *key_val(struct udev_rule *rule, struct key *key)
891 return rule->buf + key->val_off;
894 static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
896 return rule->buf + pair->key_name_off;
899 static int match_key(const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
901 char value[PATH_SIZE];
906 if (key->operation != KEY_OP_MATCH &&
907 key->operation != KEY_OP_NOMATCH)
910 /* look for a matching string, parts are separated by '|' */
911 strlcpy(value, rule->buf + key->val_off, sizeof(value));
913 dbg("key %s value='%s'", key_name, key_value);
915 pos = strchr(key_value, '|');
921 dbg("match %s '%s' <-> '%s'", key_name, key_value, val);
922 match = (fnmatch(key_value, val, 0) == 0);
929 if (match && (key->operation == KEY_OP_MATCH)) {
930 dbg("%s is true (matching value)", key_name);
933 if (!match && (key->operation == KEY_OP_NOMATCH)) {
934 dbg("%s is true (non-matching value)", key_name);
940 /* match a single rule against a given device and possibly its parent devices */
941 static int match_rule(struct udevice *udev, struct udev_rule *rule)
945 if (match_key("ACTION", rule, &rule->action, udev->action))
948 if (match_key("KERNEL", rule, &rule->kernel, udev->dev->kernel))
951 if (match_key("SUBSYSTEM", rule, &rule->subsystem, udev->dev->subsystem))
954 if (match_key("DEVPATH", rule, &rule->devpath, udev->dev->devpath))
957 if (match_key("DRIVER", rule, &rule->driver, udev->dev->driver))
960 /* match NAME against a value assigned by an earlier rule */
961 if (match_key("NAME", rule, &rule->name, udev->name))
964 /* match against current list of symlinks */
965 if (rule->symlink_match.operation == KEY_OP_MATCH ||
966 rule->symlink_match.operation == KEY_OP_NOMATCH) {
967 struct name_entry *name_loop;
970 list_for_each_entry(name_loop, &udev->symlink_list, node) {
971 if (match_key("SYMLINK", rule, &rule->symlink_match, name_loop->name) == 0) {
980 for (i = 0; i < rule->env.count; i++) {
981 struct key_pair *pair = &rule->env.keys[i];
983 /* we only check for matches, assignments will be handled later */
984 if (pair->key.operation == KEY_OP_MATCH ||
985 pair->key.operation == KEY_OP_NOMATCH) {
986 const char *key_name = key_pair_name(rule, pair);
987 const char *value = getenv(key_name);
990 dbg("ENV{'%s'} is not set, treat as empty", key_name);
993 if (match_key("ENV", rule, &pair->key, value))
998 if (rule->test.operation == KEY_OP_MATCH ||
999 rule->test.operation == KEY_OP_NOMATCH) {
1000 char filename[PATH_SIZE];
1001 char devpath[PATH_SIZE];
1003 struct stat statbuf;
1006 strlcpy(filename, key_val(rule, &rule->test), sizeof(filename));
1007 udev_rules_apply_format(udev, filename, sizeof(filename));
1009 if (attr_get_by_subsys_id(filename, devpath, sizeof(devpath), &attr)) {
1010 strlcpy(filename, sysfs_path, sizeof(filename));
1011 strlcat(filename, devpath, sizeof(filename));
1013 strlcat(filename, "/", sizeof(filename));
1014 strlcat(filename, attr, sizeof(filename));
1016 } else if (filename[0] != '/') {
1017 char tmp[PATH_SIZE];
1019 strlcpy(tmp, sysfs_path, sizeof(tmp));
1020 strlcat(tmp, udev->dev->devpath, sizeof(tmp));
1021 strlcat(tmp, "/", sizeof(tmp));
1022 strlcat(tmp, filename, sizeof(tmp));
1023 strlcpy(filename, tmp, sizeof(filename));
1026 match = (stat(filename, &statbuf) == 0);
1027 info("'%s' %s", filename, match ? "exists" : "does not exist");
1028 if (match && rule->test_mode_mask > 0) {
1029 match = ((statbuf.st_mode & rule->test_mode_mask) > 0);
1030 info("'%s' has mode=%#o and %s %#o", filename, statbuf.st_mode,
1031 match ? "matches" : "does not match",
1032 rule->test_mode_mask);
1034 if (match && rule->test.operation == KEY_OP_NOMATCH)
1036 if (!match && rule->test.operation == KEY_OP_MATCH)
1038 dbg("TEST key is true");
1041 if (rule->wait_for_sysfs.operation != KEY_OP_UNSET) {
1044 found = (wait_for_sysfs(udev, key_val(rule, &rule->wait_for_sysfs), 10) == 0);
1045 if (!found && (rule->wait_for_sysfs.operation != KEY_OP_NOMATCH))
1049 /* check for matching sysfs attribute pairs */
1050 for (i = 0; i < rule->attr.count; i++) {
1051 struct key_pair *pair = &rule->attr.keys[i];
1053 if (pair->key.operation == KEY_OP_MATCH ||
1054 pair->key.operation == KEY_OP_NOMATCH) {
1055 const char *key_name = key_pair_name(rule, pair);
1056 const char *key_value = key_val(rule, &pair->key);
1057 char devpath[PATH_SIZE];
1059 const char *value = NULL;
1060 char val[VALUE_SIZE];
1063 if (attr_get_by_subsys_id(key_name, devpath, sizeof(devpath), &attrib)) {
1065 value = sysfs_attr_get_value(devpath, attrib);
1070 value = sysfs_attr_get_value(udev->dev->devpath, key_name);
1073 strlcpy(val, value, sizeof(val));
1075 /* strip trailing whitespace of value, if not asked to match for it */
1076 len = strlen(key_value);
1077 if (len > 0 && !isspace(key_value[len-1])) {
1079 while (len > 0 && isspace(val[len-1]))
1081 dbg("removed %zi trailing whitespace chars from '%s'", strlen(val)-len, val);
1084 if (match_key("ATTR", rule, &pair->key, val))
1089 /* walk up the chain of parent devices and find a match */
1090 udev->dev_parent = udev->dev;
1092 /* check for matching kernel device name */
1093 if (match_key("KERNELS", rule, &rule->kernels, udev->dev_parent->kernel))
1096 /* check for matching subsystem value */
1097 if (match_key("SUBSYSTEMS", rule, &rule->subsystems, udev->dev_parent->subsystem))
1100 /* check for matching driver */
1101 if (match_key("DRIVERS", rule, &rule->drivers, udev->dev_parent->driver))
1104 /* check for matching sysfs attribute pairs */
1105 for (i = 0; i < rule->attrs.count; i++) {
1106 struct key_pair *pair = &rule->attrs.keys[i];
1108 if (pair->key.operation == KEY_OP_MATCH ||
1109 pair->key.operation == KEY_OP_NOMATCH) {
1110 const char *key_name = key_pair_name(rule, pair);
1111 const char *key_value = key_val(rule, &pair->key);
1113 char val[VALUE_SIZE];
1116 value = sysfs_attr_get_value(udev->dev_parent->devpath, key_name);
1118 value = sysfs_attr_get_value(udev->dev->devpath, key_name);
1121 strlcpy(val, value, sizeof(val));
1123 /* strip trailing whitespace of value, if not asked to match for it */
1124 len = strlen(key_value);
1125 if (len > 0 && !isspace(key_value[len-1])) {
1127 while (len > 0 && isspace(val[len-1]))
1129 dbg("removed %zi trailing whitespace chars from '%s'", strlen(val)-len, val);
1132 if (match_key("ATTRS", rule, &pair->key, val))
1137 /* found matching device */
1140 /* move to parent device */
1141 dbg("try parent sysfs device");
1142 udev->dev_parent = sysfs_device_get_parent(udev->dev_parent);
1143 if (udev->dev_parent == NULL)
1145 dbg("looking at dev_parent->devpath='%s'", udev->dev_parent->devpath);
1146 dbg("looking at dev_parent->kernel='%s'", udev->dev_parent->kernel);
1149 /* execute external program */
1150 if (rule->program.operation != KEY_OP_UNSET) {
1151 char program[PATH_SIZE];
1152 char result[PATH_SIZE];
1154 strlcpy(program, key_val(rule, &rule->program), sizeof(program));
1155 udev_rules_apply_format(udev, program, sizeof(program));
1156 if (run_program(program, udev->dev->subsystem, result, sizeof(result), NULL) != 0) {
1157 dbg("PROGRAM is false");
1158 udev->program_result[0] = '\0';
1159 if (rule->program.operation != KEY_OP_NOMATCH)
1164 dbg("PROGRAM matches");
1165 remove_trailing_chars(result, '\n');
1166 if (rule->string_escape == ESCAPE_UNSET ||
1167 rule->string_escape == ESCAPE_REPLACE) {
1168 count = replace_chars(result, ALLOWED_CHARS_INPUT);
1170 info("%i character(s) replaced" , count);
1172 dbg("result is '%s'", result);
1173 strlcpy(udev->program_result, result, sizeof(udev->program_result));
1174 dbg("PROGRAM returned successful");
1175 if (rule->program.operation == KEY_OP_NOMATCH)
1178 dbg("PROGRAM key is true");
1181 /* check for matching result of external program */
1182 if (match_key("RESULT", rule, &rule->result, udev->program_result))
1185 /* import variables returned from program or or file into environment */
1186 if (rule->import.operation != KEY_OP_UNSET) {
1187 char import[PATH_SIZE];
1190 strlcpy(import, key_val(rule, &rule->import), sizeof(import));
1191 udev_rules_apply_format(udev, import, sizeof(import));
1192 dbg("check for IMPORT import='%s'", import);
1193 if (rule->import_type == IMPORT_PROGRAM) {
1194 rc = import_program_into_env(udev, import);
1195 } else if (rule->import_type == IMPORT_FILE) {
1196 dbg("import file import='%s'", import);
1197 rc = import_file_into_env(udev, import);
1198 } else if (rule->import_type == IMPORT_PARENT) {
1199 dbg("import parent import='%s'", import);
1200 rc = import_parent_into_env(udev, import);
1203 dbg("IMPORT failed");
1204 if (rule->import.operation != KEY_OP_NOMATCH)
1207 dbg("IMPORT '%s' imported", key_val(rule, &rule->import));
1208 dbg("IMPORT key is true");
1211 /* rule matches, if we have ENV assignments export it */
1212 for (i = 0; i < rule->env.count; i++) {
1213 struct key_pair *pair = &rule->env.keys[i];
1215 if (pair->key.operation == KEY_OP_ASSIGN) {
1216 char temp_value[NAME_SIZE];
1217 const char *key_name = key_pair_name(rule, pair);
1218 const char *value = key_val(rule, &pair->key);
1220 /* make sure we don't write to the same string we possibly read from */
1221 strlcpy(temp_value, value, sizeof(temp_value));
1222 udev_rules_apply_format(udev, temp_value, NAME_SIZE);
1224 if (temp_value[0] == '\0') {
1225 name_list_key_remove(&udev->env_list, key_name);
1227 info("unset ENV '%s'", key_name);
1229 struct name_entry *entry;
1231 entry = name_list_key_add(&udev->env_list, key_name, temp_value);
1234 putenv(entry->name);
1235 info("set ENV '%s'", entry->name);
1240 /* if we have ATTR assignments, write value to sysfs file */
1241 for (i = 0; i < rule->attr.count; i++) {
1242 struct key_pair *pair = &rule->attr.keys[i];
1244 if (pair->key.operation == KEY_OP_ASSIGN) {
1245 const char *key_name = key_pair_name(rule, pair);
1246 char devpath[PATH_SIZE];
1249 char attr[PATH_SIZE] = "";
1250 char value[NAME_SIZE];
1253 if (attr_get_by_subsys_id(key_name, devpath, sizeof(devpath), &attrib)) {
1254 if (attrib != NULL) {
1255 strlcpy(attr, sysfs_path, sizeof(attr));
1256 strlcat(attr, devpath, sizeof(attr));
1257 strlcat(attr, "/", sizeof(attr));
1258 strlcat(attr, attrib, sizeof(attr));
1262 if (attr[0] == '\0') {
1263 strlcpy(attr, sysfs_path, sizeof(attr));
1264 strlcat(attr, udev->dev->devpath, sizeof(attr));
1265 strlcat(attr, "/", sizeof(attr));
1266 strlcat(attr, key_name, sizeof(attr));
1269 pos = strstr(attr, "/*/");
1271 char str[PATH_SIZE];
1275 strlcpy(str, &pos[2], sizeof(str));
1276 dir = opendir(attr);
1278 struct dirent *dent;
1280 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
1281 if (dent->d_name[0] == '.')
1283 strlcat(attr, dent->d_name, sizeof(attr));
1288 strlcat(attr, str, sizeof(attr));
1291 strlcpy(value, key_val(rule, &pair->key), sizeof(value));
1292 udev_rules_apply_format(udev, value, sizeof(value));
1293 info("writing '%s' to sysfs file '%s'", value, attr);
1294 f = fopen(attr, "w");
1296 if (!udev->test_run)
1297 if (fprintf(f, "%s", value) <= 0)
1298 err("error writing ATTR{%s}: %s", attr, strerror(errno));
1301 err("error opening ATTR{%s} for writing: %s", attr, strerror(errno));
1310 int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev)
1312 struct udev_rule *rule;
1315 dbg("udev->dev->devpath='%s'", udev->dev->devpath);
1316 dbg("udev->dev->kernel='%s'", udev->dev->kernel);
1318 /* look for a matching rule to apply */
1319 udev_rules_iter_init(rules);
1321 rule = udev_rules_iter_next(rules);
1326 (rule->name.operation == KEY_OP_ASSIGN ||
1327 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
1328 rule->name.operation == KEY_OP_ADD)) {
1329 dbg("node name already set, rule ignored");
1333 dbg("process rule");
1334 if (match_rule(udev, rule) == 0) {
1336 if (rule->ignore_device) {
1337 info("rule applied, '%s' is ignored", udev->dev->kernel);
1338 udev->ignore_device = 1;
1341 if (rule->ignore_remove) {
1342 udev->ignore_remove = 1;
1343 dbg("remove event should be ignored");
1345 if (rule->link_priority != 0) {
1346 udev->link_priority = rule->link_priority;
1347 info("link_priority=%i", udev->link_priority);
1349 /* apply all_partitions option only at a main block device */
1350 if (rule->partitions &&
1351 strcmp(udev->dev->subsystem, "block") == 0 && udev->dev->kernel_number[0] == '\0') {
1352 udev->partitions = rule->partitions;
1353 dbg("creation of partition nodes requested");
1356 /* apply permissions */
1357 if (!udev->mode_final && rule->mode != 0000) {
1358 if (rule->mode_operation == KEY_OP_ASSIGN_FINAL)
1359 udev->mode_final = 1;
1360 udev->mode = rule->mode;
1361 dbg("applied mode=%#o to '%s'", rule->mode, udev->dev->kernel);
1363 if (!udev->owner_final && rule->owner.operation != KEY_OP_UNSET) {
1364 if (rule->owner.operation == KEY_OP_ASSIGN_FINAL)
1365 udev->owner_final = 1;
1366 strlcpy(udev->owner, key_val(rule, &rule->owner), sizeof(udev->owner));
1367 udev_rules_apply_format(udev, udev->owner, sizeof(udev->owner));
1368 dbg("applied owner='%s' to '%s'", udev->owner, udev->dev->kernel);
1370 if (!udev->group_final && rule->group.operation != KEY_OP_UNSET) {
1371 if (rule->group.operation == KEY_OP_ASSIGN_FINAL)
1372 udev->group_final = 1;
1373 strlcpy(udev->group, key_val(rule, &rule->group), sizeof(udev->group));
1374 udev_rules_apply_format(udev, udev->group, sizeof(udev->group));
1375 dbg("applied group='%s' to '%s'", udev->group, udev->dev->kernel);
1378 /* collect symlinks */
1379 if (!udev->symlink_final &&
1380 (rule->symlink.operation == KEY_OP_ASSIGN ||
1381 rule->symlink.operation == KEY_OP_ASSIGN_FINAL ||
1382 rule->symlink.operation == KEY_OP_ADD)) {
1383 char temp[PATH_SIZE];
1387 if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL)
1388 udev->symlink_final = 1;
1389 if (rule->symlink.operation == KEY_OP_ASSIGN ||
1390 rule->symlink.operation == KEY_OP_ASSIGN_FINAL) {
1391 info("reset symlink list");
1392 name_list_cleanup(&udev->symlink_list);
1394 /* allow multiple symlinks separated by spaces */
1395 strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
1396 udev_rules_apply_format(udev, temp, sizeof(temp));
1397 if (rule->string_escape == ESCAPE_UNSET ||
1398 rule->string_escape == ESCAPE_REPLACE) {
1399 count = replace_chars(temp, ALLOWED_CHARS_FILE " ");
1401 info("%i character(s) replaced" , count);
1403 dbg("rule applied, added symlink(s) '%s'", temp);
1405 while (isspace(pos[0]))
1407 next = strchr(pos, ' ');
1410 info("add symlink '%s'", pos);
1411 name_list_add(&udev->symlink_list, pos, 0);
1412 while (isspace(next[1]))
1415 next = strchr(pos, ' ');
1417 if (pos[0] != '\0') {
1418 info("add symlink '%s'", pos);
1419 name_list_add(&udev->symlink_list, pos, 0);
1423 /* set name, later rules with name set will be ignored */
1424 if (rule->name.operation == KEY_OP_ASSIGN ||
1425 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
1426 rule->name.operation == KEY_OP_ADD) {
1430 strlcpy(udev->name, key_val(rule, &rule->name), sizeof(udev->name));
1431 udev_rules_apply_format(udev, udev->name, sizeof(udev->name));
1432 if (rule->string_escape == ESCAPE_UNSET ||
1433 rule->string_escape == ESCAPE_REPLACE) {
1434 count = replace_chars(udev->name, ALLOWED_CHARS_FILE);
1436 info("%i character(s) replaced", count);
1439 info("rule applied, '%s' becomes '%s'", udev->dev->kernel, udev->name);
1440 if (strcmp(udev->dev->subsystem, "net") != 0)
1441 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
1442 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
1445 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1446 struct name_entry *entry;
1448 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1449 udev->run_final = 1;
1450 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1451 info("reset run list");
1452 name_list_cleanup(&udev->run_list);
1454 dbg("add run '%s'", key_val(rule, &rule->run));
1455 entry = name_list_add(&udev->run_list, key_val(rule, &rule->run), 0);
1456 if (rule->run_ignore_error)
1457 entry->ignore_error = 1;
1460 if (rule->last_rule) {
1461 dbg("last rule to be applied");
1465 if (rule->goto_label.operation != KEY_OP_UNSET) {
1466 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1467 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));
1473 info("no node name set, will use kernel name '%s'", udev->dev->kernel);
1474 strlcpy(udev->name, udev->dev->kernel, sizeof(udev->name));
1477 if (udev->tmp_node[0] != '\0') {
1478 dbg("removing temporary device node");
1479 unlink_secure(udev->tmp_node);
1480 udev->tmp_node[0] = '\0';
1486 int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev)
1488 struct udev_rule *rule;
1490 dbg("udev->kernel='%s'", udev->dev->kernel);
1492 /* look for a matching rule to apply */
1493 udev_rules_iter_init(rules);
1495 rule = udev_rules_iter_next(rules);
1499 dbg("process rule");
1500 if (rule->name.operation == KEY_OP_ASSIGN ||
1501 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
1502 rule->name.operation == KEY_OP_ADD ||
1503 rule->symlink.operation == KEY_OP_ASSIGN ||
1504 rule->symlink.operation == KEY_OP_ASSIGN_FINAL ||
1505 rule->symlink.operation == KEY_OP_ADD ||
1506 rule->mode_operation != KEY_OP_UNSET ||
1507 rule->owner.operation != KEY_OP_UNSET || rule->group.operation != KEY_OP_UNSET) {
1508 dbg("skip rule that names a device");
1512 if (match_rule(udev, rule) == 0) {
1513 if (rule->ignore_device) {
1514 info("rule applied, '%s' is ignored", udev->dev->kernel);
1515 udev->ignore_device = 1;
1518 if (rule->ignore_remove) {
1519 udev->ignore_remove = 1;
1520 dbg("remove event should be ignored");
1523 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1524 struct name_entry *entry;
1526 if (rule->run.operation == KEY_OP_ASSIGN ||
1527 rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1528 info("reset run list");
1529 name_list_cleanup(&udev->run_list);
1531 dbg("add run '%s'", key_val(rule, &rule->run));
1532 entry = name_list_add(&udev->run_list, key_val(rule, &rule->run), 0);
1533 if (rule->run_ignore_error)
1534 entry->ignore_error = 1;
1535 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1539 if (rule->last_rule) {
1540 dbg("last rule to be applied");
1544 if (rule->goto_label.operation != KEY_OP_UNSET) {
1545 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1546 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));