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.
30 #include <sys/socket.h>
36 #include "udev_rules.h"
38 extern char **environ;
40 /* extract possible {attr} and move str behind it */
41 static char *get_format_attribute(char **str)
47 pos = strchr(*str, '}');
49 err("missing closing brace for format");
55 dbg("attribute='%s', str='%s'", attr, *str);
60 /* extract possible format length and move str behind it*/
61 static int get_format_len(char **str)
66 if (isdigit(*str[0])) {
67 num = (int) strtoul(*str, &tail, 10);
70 dbg("format length=%i", num);
73 err("format parsing error '%s'", *str);
79 static int get_key(char **line, char **key, char **value)
89 while (isspace(linepos[0]))
93 temp = strchr(linepos, '=');
94 if (temp == NULL || temp == linepos)
100 /* get a quoted value */
101 if (linepos[0] == '"' || linepos[0] == '\'') {
102 temp = strchr(&linepos[1], linepos[0]);
105 *value = &linepos[1];
111 temp = strchr(linepos, '\n');
119 static int run_program(const char *command, const char *subsystem,
120 char *result, size_t ressize, size_t *reslen)
123 int outpipe[2] = {-1, -1};
124 int errpipe[2] = {-1, -1};
127 char program[PATH_SIZE];
128 char *argv[(sizeof(arg) / 2) + 1];
133 /* build argv from comand */
134 strlcpy(arg, command, sizeof(arg));
136 if (strchr(arg, ' ') != NULL) {
139 while (pos != NULL) {
140 if (pos[0] == '\'') {
141 /* don't separate if in apostrophes */
143 argv[i] = strsep(&pos, "\'");
144 while (pos != NULL && pos[0] == ' ')
147 argv[i] = strsep(&pos, " ");
149 dbg("arg[%i] '%s'", i, argv[i]);
157 info("'%s'", command);
159 /* prepare pipes from child to parent */
160 if (result != NULL || udev_log_priority >= LOG_INFO) {
161 if (pipe(outpipe) != 0) {
162 err("pipe failed: %s", strerror(errno));
166 if (udev_log_priority >= LOG_INFO) {
167 if (pipe(errpipe) != 0) {
168 err("pipe failed: %s", strerror(errno));
173 /* allow programs in /lib/udev called without the path */
174 if (strchr(argv[0], '/') == NULL) {
175 strlcpy(program, "/lib/udev/", sizeof(program));
176 strlcat(program, argv[0], sizeof(program));
183 /* child closes parent ends of pipes */
184 if (outpipe[READ_END] > 0)
185 close(outpipe[READ_END]);
186 if (errpipe[READ_END] > 0)
187 close(errpipe[READ_END]);
189 /* discard child output or connect to pipe */
190 devnull = open("/dev/null", O_RDWR);
192 dup2(devnull, STDIN_FILENO);
193 if (outpipe[WRITE_END] < 0)
194 dup2(devnull, STDOUT_FILENO);
195 if (errpipe[WRITE_END] < 0)
196 dup2(devnull, STDERR_FILENO);
199 err("open /dev/null failed: %s", strerror(errno));
200 if (outpipe[WRITE_END] > 0) {
201 dup2(outpipe[WRITE_END], STDOUT_FILENO);
202 close(outpipe[WRITE_END]);
204 if (errpipe[WRITE_END] > 0) {
205 dup2(errpipe[WRITE_END], STDERR_FILENO);
206 close(errpipe[WRITE_END]);
208 execv(argv[0], argv);
209 if (errno == ENOENT || errno == ENOTDIR) {
210 /* may be on a filesytem which is not mounted right now */
211 info("program '%s' not found", argv[0]);
214 err("exec of program '%s' failed", argv[0]);
218 err("fork of '%s' failed: %s", argv[0], strerror(errno));
221 /* read from child if requested */
222 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
226 /* parent closes child ends of pipes */
227 if (outpipe[WRITE_END] > 0)
228 close(outpipe[WRITE_END]);
229 if (errpipe[WRITE_END] > 0)
230 close(errpipe[WRITE_END]);
232 /* read child output */
233 while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
238 if (outpipe[READ_END] > 0)
239 FD_SET(outpipe[READ_END], &readfds);
240 if (errpipe[READ_END] > 0)
241 FD_SET(errpipe[READ_END], &readfds);
242 fdcount = select(UDEV_MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
251 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
256 count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
258 close(outpipe[READ_END]);
259 outpipe[READ_END] = -1;
261 err("stdin read failed: %s", strerror(errno));
268 /* store result for rule processing */
270 if (respos + count < ressize) {
271 memcpy(&result[respos], inbuf, count);
274 err("ressize %ld too short", (long)ressize);
279 while ((line = strsep(&pos, "\n")))
280 if (pos || line[0] != '\0')
281 info("'%s' (stdout) '%s'", argv[0], line);
285 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
290 count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
292 close(errpipe[READ_END]);
293 errpipe[READ_END] = -1;
295 err("stderr read failed: %s", strerror(errno));
298 errbuf[count] = '\0';
300 while ((line = strsep(&pos, "\n")))
301 if (pos || line[0] != '\0')
302 info("'%s' (stderr) '%s'", argv[0], line);
305 if (outpipe[READ_END] > 0)
306 close(outpipe[READ_END]);
307 if (errpipe[READ_END] > 0)
308 close(errpipe[READ_END]);
310 /* return the childs stdout string */
312 result[respos] = '\0';
313 dbg("result='%s'", result);
318 waitpid(pid, &status, 0);
319 if (WIFEXITED(status)) {
320 info("'%s' returned with status %i", argv[0], WEXITSTATUS(status));
321 if (WEXITSTATUS(status) != 0)
324 err("'%s' abnormal exit", argv[0]);
332 static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bufsize)
334 char line[LINE_SIZE];
343 /* loop through the whole buffer */
346 while (cur < bufsize) {
347 count = buf_get_line(buf, bufsize, cur);
352 if (count >= sizeof(line)) {
353 err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
357 /* eat the whitespace */
358 while ((count > 0) && isspace(bufline[0])) {
365 /* see if this is a comment */
366 if (bufline[0] == COMMENT_CHARACTER)
369 memcpy(line, bufline, count);
373 if (get_key(&linepos, &variable, &value) == 0) {
374 dbg("import '%s=%s'", variable, value);
376 /* handle device, renamed by external tool, returning new path */
377 if (strcmp(variable, "DEVPATH") == 0) {
378 info("updating devpath from '%s' to '%s'", udev->dev->devpath, value);
379 sysfs_device_set_values(udev->dev, value, NULL, NULL);
381 name_list_key_add(&udev->env_list, variable, value);
382 setenv(variable, value, 1);
389 static int import_file_into_env(struct udevice *udev, const char *filename)
394 if (file_map(filename, &buf, &bufsize) != 0) {
395 err("can't open '%s': %s", filename, strerror(errno));
398 import_keys_into_env(udev, buf, bufsize);
399 file_unmap(buf, bufsize);
404 static int import_program_into_env(struct udevice *udev, const char *program)
409 if (run_program(program, udev->dev->subsystem, result, sizeof(result), &reslen) != 0)
411 return import_keys_into_env(udev, result, reslen);
414 static int import_parent_into_env(struct udevice *udev, const char *filter)
416 struct sysfs_device *dev_parent;
419 dev_parent = sysfs_device_get_parent(udev->dev);
420 if (dev_parent != NULL) {
421 struct udevice *udev_parent;
422 struct name_entry *name_loop;
424 dbg("found parent '%s', get the node name", dev_parent->devpath);
425 udev_parent = udev_device_init(NULL);
426 if (udev_parent == NULL)
428 /* import the udev_db of the parent */
429 if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
430 dbg("import stored parent env '%s'", udev_parent->name);
431 list_for_each_entry(name_loop, &udev_parent->env_list, node) {
432 char name[NAME_SIZE];
435 strlcpy(name, name_loop->name, sizeof(name));
436 pos = strchr(name, '=');
440 if (fnmatch(filter, name, 0) == 0) {
441 dbg("import key '%s'", name_loop->name);
442 name_list_add(&udev->env_list, name_loop->name, 0);
443 setenv(name, pos, 1);
445 dbg("skip key '%s'", name_loop->name);
450 dbg("parent not found in database");
451 udev_device_cleanup(udev_parent);
457 static int pass_env_to_socket(const char *sockname, const char *devpath, const char *action)
460 struct sockaddr_un saddr;
468 dbg("pass environment to socket '%s'", sockname);
469 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
470 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
471 saddr.sun_family = AF_LOCAL;
472 /* abstract namespace only */
473 strcpy(&saddr.sun_path[1], sockname);
474 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
476 bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
478 for (i = 0; environ[i] != NULL && bufpos < (sizeof(buf)-1); i++) {
479 bufpos += strlcpy(&buf[bufpos], environ[i], sizeof(buf) - bufpos-1);
482 if (bufpos > sizeof(buf))
483 bufpos = sizeof(buf);
485 count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, addrlen);
488 info("passed %zi bytes to socket '%s', ", count, sockname);
494 int udev_rules_run(struct udevice *udev)
496 struct name_entry *name_loop;
499 dbg("executing run list");
500 list_for_each_entry(name_loop, &udev->run_list, node) {
501 if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0) {
502 pass_env_to_socket(&name_loop->name[strlen("socket:")], udev->dev->devpath, udev->action);
504 char program[PATH_SIZE];
506 strlcpy(program, name_loop->name, sizeof(program));
507 udev_rules_apply_format(udev, program, sizeof(program));
508 if (run_program(program, udev->dev->subsystem, NULL, 0, NULL) != 0)
509 if (!name_loop->ignore_error)
517 #define WAIT_LOOP_PER_SECOND 50
518 static int wait_for_sysfs(struct udevice *udev, const char *file, int timeout)
520 char devicepath[PATH_SIZE];
521 char filepath[PATH_SIZE];
523 int loop = timeout * WAIT_LOOP_PER_SECOND;
525 strlcpy(devicepath, sysfs_path, sizeof(devicepath));
526 strlcat(devicepath, udev->dev->devpath, sizeof(devicepath));
527 strlcpy(filepath, devicepath, sizeof(filepath));
528 strlcat(filepath, "/", sizeof(filepath));
529 strlcat(filepath, file, sizeof(filepath));
531 dbg("will wait %i sec for '%s'", timeout, filepath);
534 if (stat(filepath, &stats) == 0) {
535 info("file '%s' appeared after %i loops", filepath, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
538 /* make sure, the device did not disappear in the meantime */
539 if (stat(devicepath, &stats) != 0) {
540 info("device disappeared while waiting for '%s'", filepath);
543 info("wait for '%s' for %i mseconds", filepath, 1000 / WAIT_LOOP_PER_SECOND);
544 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
546 info("waiting for '%s' failed", filepath);
550 /* handle "[$SUBSYSTEM/$KERNEL]<attribute>" lookup */
551 static int attr_get_by_subsys_id(const char *attrstr, char *devpath, size_t len, char **attr)
553 char subsys[NAME_SIZE];
558 if (attrstr[0] != '[')
561 strlcpy(subsys, &attrstr[1], sizeof(subsys));
563 attrib = strchr(subsys, ']');
569 id = strchr(subsys, '/');
575 if (sysfs_lookup_devpath_by_subsys_id(devpath, len, subsys, id)) {
577 if (attrib[0] != '\0')
588 void udev_rules_apply_format(struct udevice *udev, char *string, size_t maxsize)
590 char temp[PATH_SIZE];
591 char temp2[PATH_SIZE];
592 char *head, *tail, *pos, *cpos, *attr, *rest;
613 static const struct subst_map {
616 enum subst_type type;
618 { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
619 { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
620 { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL },
621 { .name = "id", .fmt = 'b', .type = SUBST_ID },
622 { .name = "driver", .fmt = 'd', .type = SUBST_DRIVER },
623 { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
624 { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
625 { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
626 { .name = "attr", .fmt = 's', .type = SUBST_ATTR },
627 { .name = "sysfs", .fmt = 's', .type = SUBST_ATTR },
628 { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
629 { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
630 { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
631 { .name = "sys", .fmt = 'S', .type = SUBST_SYS },
632 { .name = "env", .fmt = 'E', .type = SUBST_ENV },
635 enum subst_type type;
636 const struct subst_map *subst;
641 while (head[0] != '\0') {
642 if (head[0] == '$') {
643 /* substitute named variable */
646 if (head[1] == '$') {
647 strlcpy(temp, head+2, sizeof(temp));
648 strlcpy(head+1, temp, maxsize);
653 for (subst = map; subst->name; subst++) {
654 if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
656 tail = head + strlen(subst->name)+1;
657 dbg("will substitute format name '%s'", subst->name);
662 err("unknown format variable '%s'", head);
663 } else if (head[0] == '%') {
664 /* substitute format char */
667 if (head[1] == '%') {
668 strlcpy(temp, head+2, sizeof(temp));
669 strlcpy(head+1, temp, maxsize);
675 len = get_format_len(&tail);
676 for (subst = map; subst->name; subst++) {
677 if (tail[0] == subst->fmt) {
680 dbg("will substitute format char '%c'", subst->fmt);
685 err("unknown format char '%c'", tail[0]);
691 attr = get_format_attribute(&tail);
692 strlcpy(temp, tail, sizeof(temp));
693 dbg("format=%i, string='%s', tail='%s'", type ,string, tail);
697 strlcat(string, udev->dev->devpath, maxsize);
698 dbg("substitute devpath '%s'", udev->dev->devpath);
701 strlcat(string, udev->dev->kernel, maxsize);
702 dbg("substitute kernel name '%s'", udev->dev->kernel);
704 case SUBST_KERNEL_NUMBER:
705 strlcat(string, udev->dev->kernel_number, maxsize);
706 dbg("substitute kernel number '%s'", udev->dev->kernel_number);
709 if (udev->dev_parent != NULL) {
710 strlcat(string, udev->dev_parent->kernel, maxsize);
711 dbg("substitute id '%s'", udev->dev_parent->kernel);
715 if (udev->dev_parent != NULL) {
716 strlcat(string, udev->dev_parent->driver, maxsize);
717 dbg("substitute driver '%s'", udev->dev_parent->driver);
721 sprintf(temp2, "%d", major(udev->devt));
722 strlcat(string, temp2, maxsize);
723 dbg("substitute major number '%s'", temp2);
726 sprintf(temp2, "%d", minor(udev->devt));
727 strlcat(string, temp2, maxsize);
728 dbg("substitute minor number '%s'", temp2);
731 if (udev->program_result[0] == '\0')
733 /* get part part of the result string */
736 i = strtoul(attr, &rest, 10);
738 dbg("request part #%d of result string", i);
739 cpos = udev->program_result;
741 while (cpos[0] != '\0' && !isspace(cpos[0]))
743 while (isspace(cpos[0]))
747 err("requested part of result string not found");
750 strlcpy(temp2, cpos, sizeof(temp2));
751 /* %{2+}c copies the whole string from the second part on */
752 if (rest[0] != '+') {
753 cpos = strchr(temp2, ' ');
757 strlcat(string, temp2, maxsize);
758 dbg("substitute part of result string '%s'", temp2);
760 strlcat(string, udev->program_result, maxsize);
761 dbg("substitute result string '%s'", udev->program_result);
766 err("missing file parameter for attr");
768 char devpath[PATH_SIZE];
770 const char *value = NULL;
773 if (attr_get_by_subsys_id(attr, devpath, sizeof(devpath), &attrib)) {
775 value = sysfs_attr_get_value(devpath, attrib);
780 /* try the current device, other matches may have selected */
781 if (value == NULL && udev->dev_parent != NULL && udev->dev_parent != udev->dev)
782 value = sysfs_attr_get_value(udev->dev_parent->devpath, attr);
784 /* look at all devices along the chain of parents */
786 struct sysfs_device *dev_parent = udev->dev;
789 dbg("looking at '%s'", dev_parent->devpath);
790 value = sysfs_attr_get_value(dev_parent->devpath, attr);
792 strlcpy(temp2, value, sizeof(temp2));
795 dev_parent = sysfs_device_get_parent(dev_parent);
796 } while (dev_parent != NULL);
802 /* strip trailing whitespace, and replace unwanted characters */
803 size = strlcpy(temp2, value, sizeof(temp2));
804 if (size >= sizeof(temp2))
805 size = sizeof(temp2)-1;
806 while (size > 0 && isspace(temp2[size-1]))
807 temp2[--size] = '\0';
808 count = replace_chars(temp2, ALLOWED_CHARS_INPUT);
810 info("%i character(s) replaced" , count);
811 strlcat(string, temp2, maxsize);
812 dbg("substitute sysfs value '%s'", temp2);
817 struct sysfs_device *dev_parent;
819 dev_parent = sysfs_device_get_parent(udev->dev);
820 if (dev_parent != NULL) {
821 struct udevice *udev_parent;
823 dbg("found parent '%s', get the node name", dev_parent->devpath);
824 udev_parent = udev_device_init(NULL);
825 if (udev_parent != NULL) {
826 /* lookup the name in the udev_db with the DEVPATH of the parent */
827 if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
828 strlcat(string, udev_parent->name, maxsize);
829 dbg("substitute parent node name'%s'", udev_parent->name);
831 dbg("parent not found in database");
832 udev_device_cleanup(udev_parent);
837 case SUBST_TEMP_NODE:
838 if (udev->tmp_node[0] == '\0' && major(udev->devt) > 0) {
839 dbg("create temporary device node for callout");
840 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
841 udev_root, major(udev->devt), minor(udev->devt));
842 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
843 udev_node_mknod(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
845 strlcat(string, udev->tmp_node, maxsize);
846 dbg("substitute temporary device node name '%s'", udev->tmp_node);
849 strlcat(string, udev_root, maxsize);
850 dbg("substitute udev_root '%s'", udev_root);
853 strlcat(string, sysfs_path, maxsize);
854 dbg("substitute sysfs_path '%s'", sysfs_path);
858 dbg("missing attribute");
863 dbg("env '%s' not available", attr);
866 dbg("substitute env '%s=%s'", attr, pos);
867 strlcat(string, pos, maxsize);
870 err("unknown substitution type=%i", type);
873 /* possibly truncate to format-char specified length */
874 if (len >= 0 && len < (int)strlen(head)) {
876 dbg("truncate to %i chars, subtitution string becomes '%s'", len, head);
878 strlcat(string, temp, maxsize);
882 static char *key_val(struct udev_rule *rule, struct key *key)
884 return rule->buf + key->val_off;
887 static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
889 return rule->buf + pair->key_name_off;
892 static int match_key(const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
894 char value[PATH_SIZE];
899 if (key->operation != KEY_OP_MATCH &&
900 key->operation != KEY_OP_NOMATCH)
903 /* look for a matching string, parts are separated by '|' */
904 strlcpy(value, rule->buf + key->val_off, sizeof(value));
906 dbg("key %s value='%s'", key_name, key_value);
908 pos = strchr(key_value, '|');
914 dbg("match %s '%s' <-> '%s'", key_name, key_value, val);
915 match = (fnmatch(key_value, val, 0) == 0);
922 if (match && (key->operation == KEY_OP_MATCH)) {
923 dbg("%s is true (matching value)", key_name);
926 if (!match && (key->operation == KEY_OP_NOMATCH)) {
927 dbg("%s is true (non-matching value)", key_name);
933 /* match a single rule against a given device and possibly its parent devices */
934 static int match_rule(struct udevice *udev, struct udev_rule *rule)
938 if (match_key("ACTION", rule, &rule->action, udev->action))
941 if (match_key("KERNEL", rule, &rule->kernel, udev->dev->kernel))
944 if (match_key("SUBSYSTEM", rule, &rule->subsystem, udev->dev->subsystem))
947 if (match_key("DEVPATH", rule, &rule->devpath, udev->dev->devpath))
950 if (match_key("DRIVER", rule, &rule->driver, udev->dev->driver))
953 /* match NAME against a value assigned by an earlier rule */
954 if (match_key("NAME", rule, &rule->name, udev->name))
957 /* match against current list of symlinks */
958 if (rule->symlink_match.operation == KEY_OP_MATCH ||
959 rule->symlink_match.operation == KEY_OP_NOMATCH) {
960 struct name_entry *name_loop;
963 list_for_each_entry(name_loop, &udev->symlink_list, node) {
964 if (match_key("SYMLINK", rule, &rule->symlink_match, name_loop->name) == 0) {
973 for (i = 0; i < rule->env.count; i++) {
974 struct key_pair *pair = &rule->env.keys[i];
976 /* we only check for matches, assignments will be handled later */
977 if (pair->key.operation == KEY_OP_MATCH ||
978 pair->key.operation == KEY_OP_NOMATCH) {
979 const char *key_name = key_pair_name(rule, pair);
980 const char *value = getenv(key_name);
983 dbg("ENV{'%s'} is not set, treat as empty", key_name);
986 if (match_key("ENV", rule, &pair->key, value))
991 if (rule->test.operation != KEY_OP_UNSET) {
992 char filename[PATH_SIZE];
993 char devpath[PATH_SIZE];
998 strlcpy(filename, key_val(rule, &rule->test), sizeof(filename));
999 udev_rules_apply_format(udev, filename, sizeof(filename));
1001 if (attr_get_by_subsys_id(filename, devpath, sizeof(devpath), &attr)) {
1002 strlcpy(filename, sysfs_path, sizeof(filename));
1003 strlcat(filename, devpath, sizeof(filename));
1005 strlcat(filename, "/", sizeof(filename));
1006 strlcat(filename, attr, sizeof(filename));
1008 } else if (filename[0] != '/') {
1009 char tmp[PATH_SIZE];
1011 strlcpy(tmp, sysfs_path, sizeof(tmp));
1012 strlcat(tmp, udev->dev->devpath, sizeof(tmp));
1013 strlcat(tmp, "/", sizeof(tmp));
1014 strlcat(tmp, filename, sizeof(tmp));
1015 strlcpy(filename, tmp, sizeof(filename));
1018 match = (stat(filename, &statbuf) == 0);
1019 info("'%s' %s", filename, match ? "exists" : "does not exist");
1020 if (match && rule->test_mode_mask > 0) {
1021 match = ((statbuf.st_mode & rule->test_mode_mask) > 0);
1022 info("'%s' has mode=%#o and %s %#o", filename, statbuf.st_mode,
1023 match ? "matches" : "does not match",
1024 rule->test_mode_mask);
1026 if (match && rule->test.operation == KEY_OP_NOMATCH)
1028 if (!match && rule->test.operation == KEY_OP_MATCH)
1030 dbg("TEST key is true");
1033 if (rule->wait_for_sysfs.operation != KEY_OP_UNSET) {
1036 found = (wait_for_sysfs(udev, key_val(rule, &rule->wait_for_sysfs), 3) == 0);
1037 if (!found && (rule->wait_for_sysfs.operation != KEY_OP_NOMATCH))
1041 /* check for matching sysfs attribute pairs */
1042 for (i = 0; i < rule->attr.count; i++) {
1043 struct key_pair *pair = &rule->attr.keys[i];
1045 if (pair->key.operation == KEY_OP_MATCH ||
1046 pair->key.operation == KEY_OP_NOMATCH) {
1047 const char *key_name = key_pair_name(rule, pair);
1048 const char *key_value = key_val(rule, &pair->key);
1049 char devpath[PATH_SIZE];
1051 const char *value = NULL;
1052 char val[VALUE_SIZE];
1055 if (attr_get_by_subsys_id(key_name, devpath, sizeof(devpath), &attrib)) {
1057 value = sysfs_attr_get_value(devpath, attrib);
1062 value = sysfs_attr_get_value(udev->dev->devpath, key_name);
1065 strlcpy(val, value, sizeof(val));
1067 /* strip trailing whitespace of value, if not asked to match for it */
1068 len = strlen(key_value);
1069 if (len > 0 && !isspace(key_value[len-1])) {
1071 while (len > 0 && isspace(val[len-1]))
1073 dbg("removed %zi trailing whitespace chars from '%s'", strlen(val)-len, val);
1076 if (match_key("ATTR", rule, &pair->key, val))
1081 /* walk up the chain of parent devices and find a match */
1082 udev->dev_parent = udev->dev;
1084 /* check for matching kernel device name */
1085 if (match_key("KERNELS", rule, &rule->kernels, udev->dev_parent->kernel))
1088 /* check for matching subsystem value */
1089 if (match_key("SUBSYSTEMS", rule, &rule->subsystems, udev->dev_parent->subsystem))
1092 /* check for matching driver */
1093 if (match_key("DRIVERS", rule, &rule->drivers, udev->dev_parent->driver))
1096 /* check for matching sysfs attribute pairs */
1097 for (i = 0; i < rule->attrs.count; i++) {
1098 struct key_pair *pair = &rule->attrs.keys[i];
1100 if (pair->key.operation == KEY_OP_MATCH ||
1101 pair->key.operation == KEY_OP_NOMATCH) {
1102 const char *key_name = key_pair_name(rule, pair);
1103 const char *key_value = key_val(rule, &pair->key);
1105 char val[VALUE_SIZE];
1108 value = sysfs_attr_get_value(udev->dev_parent->devpath, key_name);
1110 value = sysfs_attr_get_value(udev->dev->devpath, key_name);
1113 strlcpy(val, value, sizeof(val));
1115 /* strip trailing whitespace of value, if not asked to match for it */
1116 len = strlen(key_value);
1117 if (len > 0 && !isspace(key_value[len-1])) {
1119 while (len > 0 && isspace(val[len-1]))
1121 dbg("removed %zi trailing whitespace chars from '%s'", strlen(val)-len, val);
1124 if (match_key("ATTRS", rule, &pair->key, val))
1129 /* found matching device */
1132 /* move to parent device */
1133 dbg("try parent sysfs device");
1134 udev->dev_parent = sysfs_device_get_parent(udev->dev_parent);
1135 if (udev->dev_parent == NULL)
1137 dbg("looking at dev_parent->devpath='%s'", udev->dev_parent->devpath);
1138 dbg("looking at dev_parent->kernel='%s'", udev->dev_parent->kernel);
1141 /* execute external program */
1142 if (rule->program.operation != KEY_OP_UNSET) {
1143 char program[PATH_SIZE];
1144 char result[PATH_SIZE];
1146 strlcpy(program, key_val(rule, &rule->program), sizeof(program));
1147 udev_rules_apply_format(udev, program, sizeof(program));
1148 if (run_program(program, udev->dev->subsystem, result, sizeof(result), NULL) != 0) {
1149 dbg("PROGRAM is false");
1150 udev->program_result[0] = '\0';
1151 if (rule->program.operation != KEY_OP_NOMATCH)
1156 dbg("PROGRAM matches");
1157 remove_trailing_chars(result, '\n');
1158 if (rule->string_escape == ESCAPE_UNSET ||
1159 rule->string_escape == ESCAPE_REPLACE) {
1160 count = replace_chars(result, ALLOWED_CHARS_INPUT);
1162 info("%i character(s) replaced" , count);
1164 dbg("result is '%s'", result);
1165 strlcpy(udev->program_result, result, sizeof(udev->program_result));
1166 dbg("PROGRAM returned successful");
1167 if (rule->program.operation == KEY_OP_NOMATCH)
1170 dbg("PROGRAM key is true");
1173 /* check for matching result of external program */
1174 if (match_key("RESULT", rule, &rule->result, udev->program_result))
1177 /* import variables returned from program or or file into environment */
1178 if (rule->import.operation != KEY_OP_UNSET) {
1179 char import[PATH_SIZE];
1182 strlcpy(import, key_val(rule, &rule->import), sizeof(import));
1183 udev_rules_apply_format(udev, import, sizeof(import));
1184 dbg("check for IMPORT import='%s'", import);
1185 if (rule->import_type == IMPORT_PROGRAM) {
1186 rc = import_program_into_env(udev, import);
1187 } else if (rule->import_type == IMPORT_FILE) {
1188 dbg("import file import='%s'", import);
1189 rc = import_file_into_env(udev, import);
1190 } else if (rule->import_type == IMPORT_PARENT) {
1191 dbg("import parent import='%s'", import);
1192 rc = import_parent_into_env(udev, import);
1195 dbg("IMPORT failed");
1196 if (rule->import.operation != KEY_OP_NOMATCH)
1199 dbg("IMPORT '%s' imported", key_val(rule, &rule->import));
1200 dbg("IMPORT key is true");
1203 /* rule matches, if we have ENV assignments export it */
1204 for (i = 0; i < rule->env.count; i++) {
1205 struct key_pair *pair = &rule->env.keys[i];
1207 if (pair->key.operation == KEY_OP_ASSIGN) {
1208 char temp_value[NAME_SIZE];
1209 const char *key_name = key_pair_name(rule, pair);
1210 const char *value = key_val(rule, &pair->key);
1212 /* make sure we don't write to the same string we possibly read from */
1213 strlcpy(temp_value, value, sizeof(temp_value));
1214 udev_rules_apply_format(udev, temp_value, NAME_SIZE);
1216 if (temp_value[0] == '\0') {
1217 name_list_key_remove(&udev->env_list, key_name);
1219 info("unset ENV '%s'", key_name);
1221 struct name_entry *entry;
1223 entry = name_list_key_add(&udev->env_list, key_name, temp_value);
1226 putenv(entry->name);
1227 info("set ENV '%s'", entry->name);
1232 /* if we have ATTR assignments, write value to sysfs file */
1233 for (i = 0; i < rule->attr.count; i++) {
1234 struct key_pair *pair = &rule->attr.keys[i];
1236 if (pair->key.operation == KEY_OP_ASSIGN) {
1237 const char *key_name = key_pair_name(rule, pair);
1238 char devpath[PATH_SIZE];
1240 char attr[PATH_SIZE] = "";
1241 char value[NAME_SIZE];
1244 if (attr_get_by_subsys_id(key_name, devpath, sizeof(devpath), &attrib)) {
1245 if (attrib != NULL) {
1246 strlcpy(attr, sysfs_path, sizeof(attr));
1247 strlcat(attr, devpath, sizeof(attr));
1248 strlcat(attr, "/", sizeof(attr));
1249 strlcat(attr, attrib, sizeof(attr));
1253 if (attr[0] == '\0') {
1254 strlcpy(attr, sysfs_path, sizeof(attr));
1255 strlcat(attr, udev->dev->devpath, sizeof(attr));
1256 strlcat(attr, "/", sizeof(attr));
1257 strlcat(attr, key_name, sizeof(attr));
1260 strlcpy(value, key_val(rule, &pair->key), sizeof(value));
1261 udev_rules_apply_format(udev, value, sizeof(value));
1262 info("writing '%s' to sysfs file '%s'", value, attr);
1263 f = fopen(attr, "w");
1265 if (!udev->test_run)
1266 if (fprintf(f, "%s", value) <= 0)
1267 err("error writing ATTR{%s}: %s", attr, strerror(errno));
1270 err("error opening ATTR{%s} for writing: %s", attr, strerror(errno));
1279 int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev)
1281 struct udev_rule *rule;
1284 dbg("udev->dev->devpath='%s'", udev->dev->devpath);
1285 dbg("udev->dev->kernel='%s'", udev->dev->kernel);
1287 /* look for a matching rule to apply */
1288 udev_rules_iter_init(rules);
1290 rule = udev_rules_iter_next(rules);
1295 (rule->name.operation == KEY_OP_ASSIGN ||
1296 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
1297 rule->name.operation == KEY_OP_ADD)) {
1298 dbg("node name already set, rule ignored");
1302 dbg("process rule");
1303 if (match_rule(udev, rule) == 0) {
1305 if (rule->ignore_device) {
1306 info("rule applied, '%s' is ignored", udev->dev->kernel);
1307 udev->ignore_device = 1;
1310 if (rule->ignore_remove) {
1311 udev->ignore_remove = 1;
1312 dbg("remove event should be ignored");
1314 if (rule->link_priority != 0) {
1315 udev->link_priority = rule->link_priority;
1316 info("link_priority=%i", udev->link_priority);
1318 /* apply all_partitions option only at a main block device */
1319 if (rule->partitions &&
1320 strcmp(udev->dev->subsystem, "block") == 0 && udev->dev->kernel_number[0] == '\0') {
1321 udev->partitions = rule->partitions;
1322 dbg("creation of partition nodes requested");
1325 /* apply permissions */
1326 if (!udev->mode_final && rule->mode != 0000) {
1327 if (rule->mode_operation == KEY_OP_ASSIGN_FINAL)
1328 udev->mode_final = 1;
1329 udev->mode = rule->mode;
1330 dbg("applied mode=%#o to '%s'", rule->mode, udev->dev->kernel);
1332 if (!udev->owner_final && rule->owner.operation != KEY_OP_UNSET) {
1333 if (rule->owner.operation == KEY_OP_ASSIGN_FINAL)
1334 udev->owner_final = 1;
1335 strlcpy(udev->owner, key_val(rule, &rule->owner), sizeof(udev->owner));
1336 udev_rules_apply_format(udev, udev->owner, sizeof(udev->owner));
1337 dbg("applied owner='%s' to '%s'", udev->owner, udev->dev->kernel);
1339 if (!udev->group_final && rule->group.operation != KEY_OP_UNSET) {
1340 if (rule->group.operation == KEY_OP_ASSIGN_FINAL)
1341 udev->group_final = 1;
1342 strlcpy(udev->group, key_val(rule, &rule->group), sizeof(udev->group));
1343 udev_rules_apply_format(udev, udev->group, sizeof(udev->group));
1344 dbg("applied group='%s' to '%s'", udev->group, udev->dev->kernel);
1347 /* collect symlinks */
1348 if (!udev->symlink_final &&
1349 (rule->symlink.operation == KEY_OP_ASSIGN ||
1350 rule->symlink.operation == KEY_OP_ASSIGN_FINAL ||
1351 rule->symlink.operation == KEY_OP_ADD)) {
1352 char temp[PATH_SIZE];
1356 if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL)
1357 udev->symlink_final = 1;
1358 if (rule->symlink.operation == KEY_OP_ASSIGN ||
1359 rule->symlink.operation == KEY_OP_ASSIGN_FINAL) {
1360 info("reset symlink list");
1361 name_list_cleanup(&udev->symlink_list);
1363 /* allow multiple symlinks separated by spaces */
1364 strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
1365 udev_rules_apply_format(udev, temp, sizeof(temp));
1366 if (rule->string_escape == ESCAPE_UNSET ||
1367 rule->string_escape == ESCAPE_REPLACE) {
1368 count = replace_chars(temp, ALLOWED_CHARS_FILE " ");
1370 info("%i character(s) replaced" , count);
1372 dbg("rule applied, added symlink(s) '%s'", temp);
1374 while (isspace(pos[0]))
1376 next = strchr(pos, ' ');
1379 info("add symlink '%s'", pos);
1380 name_list_add(&udev->symlink_list, pos, 0);
1381 while (isspace(next[1]))
1384 next = strchr(pos, ' ');
1386 if (pos[0] != '\0') {
1387 info("add symlink '%s'", pos);
1388 name_list_add(&udev->symlink_list, pos, 0);
1392 /* set name, later rules with name set will be ignored */
1393 if (rule->name.operation == KEY_OP_ASSIGN ||
1394 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
1395 rule->name.operation == KEY_OP_ADD) {
1399 strlcpy(udev->name, key_val(rule, &rule->name), sizeof(udev->name));
1400 udev_rules_apply_format(udev, udev->name, sizeof(udev->name));
1401 if (rule->string_escape == ESCAPE_UNSET ||
1402 rule->string_escape == ESCAPE_REPLACE) {
1403 count = replace_chars(udev->name, ALLOWED_CHARS_FILE);
1405 info("%i character(s) replaced", count);
1408 info("rule applied, '%s' becomes '%s'", udev->dev->kernel, udev->name);
1409 if (strcmp(udev->dev->subsystem, "net") != 0)
1410 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
1411 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
1414 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1415 struct name_entry *entry;
1417 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1418 udev->run_final = 1;
1419 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1420 info("reset run list");
1421 name_list_cleanup(&udev->run_list);
1423 dbg("add run '%s'", key_val(rule, &rule->run));
1424 entry = name_list_add(&udev->run_list, key_val(rule, &rule->run), 0);
1425 if (rule->run_ignore_error)
1426 entry->ignore_error = 1;
1429 if (rule->last_rule) {
1430 dbg("last rule to be applied");
1434 if (rule->goto_label.operation != KEY_OP_UNSET) {
1435 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1436 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));
1442 info("no node name set, will use kernel name '%s'", udev->name);
1443 strlcpy(udev->name, udev->dev->kernel, sizeof(udev->name));
1446 if (udev->tmp_node[0] != '\0') {
1447 dbg("removing temporary device node");
1448 unlink_secure(udev->tmp_node);
1449 udev->tmp_node[0] = '\0';
1455 int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev)
1457 struct udev_rule *rule;
1459 dbg("udev->kernel='%s'", udev->dev->kernel);
1461 /* look for a matching rule to apply */
1462 udev_rules_iter_init(rules);
1464 rule = udev_rules_iter_next(rules);
1468 dbg("process rule");
1469 if (rule->name.operation != KEY_OP_UNSET || rule->symlink.operation != KEY_OP_UNSET ||
1470 rule->mode_operation != KEY_OP_UNSET || rule->owner.operation != KEY_OP_UNSET ||
1471 rule->group.operation != KEY_OP_UNSET) {
1472 dbg("skip rule that names a device");
1476 if (match_rule(udev, rule) == 0) {
1477 if (rule->ignore_device) {
1478 info("rule applied, '%s' is ignored", udev->dev->kernel);
1479 udev->ignore_device = 1;
1483 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1484 struct name_entry *entry;
1486 if (rule->run.operation == KEY_OP_ASSIGN ||
1487 rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1488 info("reset run list");
1489 name_list_cleanup(&udev->run_list);
1491 dbg("add run '%s'", key_val(rule, &rule->run));
1492 entry = name_list_add(&udev->run_list, key_val(rule, &rule->run), 0);
1493 if (rule->run_ignore_error)
1494 entry->ignore_error = 1;
1495 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1499 if (rule->last_rule) {
1500 dbg("last rule to be applied");
1504 if (rule->goto_label.operation != KEY_OP_UNSET) {
1505 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1506 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));