2 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2003-2008 Kay Sievers <kay.sievers@vrfy.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
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(struct udev *udev, char **str)
47 pos = strchr(*str, '}');
49 err(udev, "missing closing brace for format\n");
55 dbg(udev, "attribute='%s', str='%s'\n", attr, *str);
60 /* extract possible format length and move str behind it*/
61 static int get_format_len(struct udev *udev, char **str)
66 if (isdigit(*str[0])) {
67 num = (int) strtoul(*str, &tail, 10);
70 dbg(udev, "format length=%i\n", num);
73 err(udev, "format parsing error '%s'\n", *str);
79 static int run_program(struct udev_device *dev, const char *command,
80 char *result, size_t ressize, size_t *reslen)
82 struct udev *udev = udev_device_get_udev(dev);
85 int outpipe[2] = {-1, -1};
86 int errpipe[2] = {-1, -1};
88 char arg[UTIL_PATH_SIZE];
89 char program[UTIL_PATH_SIZE];
90 char *argv[(sizeof(arg) / 2) + 1];
95 /* build argv from command */
96 util_strlcpy(arg, command, sizeof(arg));
98 if (strchr(arg, ' ') != NULL) {
101 while (pos != NULL && pos[0] != '\0') {
102 if (pos[0] == '\'') {
103 /* do not separate quotes */
105 argv[i] = strsep(&pos, "\'");
106 while (pos != NULL && pos[0] == ' ')
109 argv[i] = strsep(&pos, " ");
111 dbg(udev, "arg[%i] '%s'\n", i, argv[i]);
119 info(udev, "'%s'\n", command);
121 /* prepare pipes from child to parent */
122 if (result != NULL || udev_get_log_priority(udev) >= LOG_INFO) {
123 if (pipe(outpipe) != 0) {
124 err(udev, "pipe failed: %m\n");
128 if (udev_get_log_priority(udev) >= LOG_INFO) {
129 if (pipe(errpipe) != 0) {
130 err(udev, "pipe failed: %m\n");
135 /* allow programs in /lib/udev/ to be called without the path */
136 if (strchr(argv[0], '/') == NULL) {
137 util_strlcpy(program, UDEV_PREFIX "/lib/udev/", sizeof(program));
138 util_strlcat(program, argv[0], sizeof(program));
142 envp = udev_device_get_properties_envp(dev);
147 /* child closes parent ends of pipes */
148 if (outpipe[READ_END] > 0)
149 close(outpipe[READ_END]);
150 if (errpipe[READ_END] > 0)
151 close(errpipe[READ_END]);
153 /* discard child output or connect to pipe */
154 devnull = open("/dev/null", O_RDWR);
156 dup2(devnull, STDIN_FILENO);
157 if (outpipe[WRITE_END] < 0)
158 dup2(devnull, STDOUT_FILENO);
159 if (errpipe[WRITE_END] < 0)
160 dup2(devnull, STDERR_FILENO);
163 err(udev, "open /dev/null failed: %m\n");
164 if (outpipe[WRITE_END] > 0) {
165 dup2(outpipe[WRITE_END], STDOUT_FILENO);
166 close(outpipe[WRITE_END]);
168 if (errpipe[WRITE_END] > 0) {
169 dup2(errpipe[WRITE_END], STDERR_FILENO);
170 close(errpipe[WRITE_END]);
172 execve(argv[0], argv, envp);
173 if (errno == ENOENT || errno == ENOTDIR) {
174 /* may be on a filesytem which is not mounted right now */
175 info(udev, "program '%s' not found\n", argv[0]);
178 err(udev, "exec of program '%s' failed\n", argv[0]);
182 err(udev, "fork of '%s' failed: %m\n", argv[0]);
185 /* read from child if requested */
186 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
190 /* parent closes child ends of pipes */
191 if (outpipe[WRITE_END] > 0)
192 close(outpipe[WRITE_END]);
193 if (errpipe[WRITE_END] > 0)
194 close(errpipe[WRITE_END]);
196 /* read child output */
197 while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
202 if (outpipe[READ_END] > 0)
203 FD_SET(outpipe[READ_END], &readfds);
204 if (errpipe[READ_END] > 0)
205 FD_SET(errpipe[READ_END], &readfds);
206 fdcount = select(UDEV_MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
215 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
220 count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
222 close(outpipe[READ_END]);
223 outpipe[READ_END] = -1;
225 err(udev, "stdin read failed: %m\n");
232 /* store result for rule processing */
234 if (respos + count < ressize) {
235 memcpy(&result[respos], inbuf, count);
238 err(udev, "ressize %ld too short\n", (long)ressize);
243 while ((line = strsep(&pos, "\n")))
244 if (pos || line[0] != '\0')
245 info(udev, "'%s' (stdout) '%s'\n", argv[0], line);
249 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
254 count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
256 close(errpipe[READ_END]);
257 errpipe[READ_END] = -1;
259 err(udev, "stderr read failed: %m\n");
262 errbuf[count] = '\0';
264 while ((line = strsep(&pos, "\n")))
265 if (pos || line[0] != '\0')
266 info(udev, "'%s' (stderr) '%s'\n", argv[0], line);
269 if (outpipe[READ_END] > 0)
270 close(outpipe[READ_END]);
271 if (errpipe[READ_END] > 0)
272 close(errpipe[READ_END]);
274 /* return the childs stdout string */
276 result[respos] = '\0';
277 dbg(udev, "result='%s'\n", result);
282 waitpid(pid, &status, 0);
283 if (WIFEXITED(status)) {
284 info(udev, "'%s' returned with status %i\n", argv[0], WEXITSTATUS(status));
285 if (WEXITSTATUS(status) != 0)
288 err(udev, "'%s' abnormal exit\n", argv[0]);
296 static int import_property_from_string(struct udev_device *dev, char *line)
298 struct udev *udev = udev_device_get_udev(dev);
305 while (isspace(key[0]))
308 /* comment or empty line */
309 if (key[0] == '#' || key[0] == '\0')
312 /* split key/value */
313 val = strchr(key, '=');
320 while (isspace(val[0]))
327 while (isspace(key[len-1]))
331 /* terminate value */
335 while (isspace(val[len-1]))
343 if (val[0] == '"' || val[0] == '\'') {
344 if (val[len-1] != val[0]) {
345 info(udev, "inconsistent quoting: '%s', skip\n", line);
352 info(udev, "adding '%s'='%s'\n", key, val);
354 /* handle device, renamed by external tool, returning new path */
355 if (strcmp(key, "DEVPATH") == 0) {
356 char syspath[UTIL_PATH_SIZE];
358 info(udev, "updating devpath from '%s' to '%s'\n",
359 udev_device_get_devpath(dev), val);
360 util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath));
361 util_strlcat(syspath, val, sizeof(syspath));
362 udev_device_set_syspath(dev, syspath);
364 struct udev_list_entry *entry;
366 entry = udev_device_add_property(dev, key, val);
368 udev_list_entry_set_flag(entry, 1);
373 static int import_file_into_env(struct udev_device *dev, const char *filename)
376 char line[UTIL_LINE_SIZE];
378 f = fopen(filename, "r");
381 while (fgets(line, sizeof(line), f) != NULL)
382 import_property_from_string(dev, line);
387 static int import_program_into_env(struct udev_device *dev, const char *program)
393 if (run_program(dev, program, result, sizeof(result), &reslen) != 0)
397 while (line != NULL) {
400 pos = strchr(line, '\n');
405 import_property_from_string(dev, line);
411 static int import_parent_into_env(struct udev_device *dev, const char *filter)
413 struct udev *udev = udev_device_get_udev(dev);
414 struct udev_device *dev_parent;
415 struct udev_list_entry *list_entry;
417 dev_parent = udev_device_get_parent(dev);
418 if (dev_parent == NULL)
421 dbg(udev, "found parent '%s', get the node name\n", udev_device_get_syspath(dev_parent));
422 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
423 const char *key = udev_list_entry_get_name(list_entry);
424 const char *val = udev_list_entry_get_value(list_entry);
426 if (fnmatch(filter, key, 0) == 0) {
427 struct udev_list_entry *entry;
429 dbg(udev, "import key '%s=%s'\n", key, val);
430 entry = udev_device_add_property(dev, key, val);
432 udev_list_entry_set_flag(entry, 1);
438 int udev_rules_run(struct udev_event *event)
440 struct udev_list_entry *list_entry;
443 dbg(event->udev, "executing run list\n");
444 udev_list_entry_foreach(list_entry, udev_list_get_entry(&event->run_list)) {
445 const char *cmd = udev_list_entry_get_name(list_entry);
447 if (strncmp(cmd, "socket:", strlen("socket:")) == 0) {
448 struct udev_monitor *monitor;
450 monitor = udev_monitor_new_from_socket(event->udev, &cmd[strlen("socket:")]);
453 udev_monitor_send_device(monitor, event->dev);
454 udev_monitor_unref(monitor);
456 char program[UTIL_PATH_SIZE];
458 util_strlcpy(program, cmd, sizeof(program));
459 udev_rules_apply_format(event, program, sizeof(program));
460 if (run_program(event->dev, program, NULL, 0, NULL) != 0) {
461 if (!udev_list_entry_get_flag(list_entry))
469 #define WAIT_LOOP_PER_SECOND 50
470 static int wait_for_file(struct udev_event *event, const char *file, int timeout)
472 char filepath[UTIL_PATH_SIZE];
473 char devicepath[UTIL_PATH_SIZE] = "";
475 int loop = timeout * WAIT_LOOP_PER_SECOND;
477 /* a relative path is a device attribute */
478 if (file[0] != '/') {
479 util_strlcpy(devicepath, udev_get_sys_path(event->udev), sizeof(devicepath));
480 util_strlcat(devicepath, udev_device_get_devpath(event->dev), sizeof(devicepath));
482 util_strlcpy(filepath, devicepath, sizeof(filepath));
483 util_strlcat(filepath, "/", sizeof(filepath));
484 util_strlcat(filepath, file, sizeof(filepath));
488 dbg(event->udev, "will wait %i sec for '%s'\n", timeout, file);
491 if (stat(file, &stats) == 0) {
492 info(event->udev, "file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
495 /* make sure, the device did not disappear in the meantime */
496 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
497 info(event->udev, "device disappeared while waiting for '%s'\n", file);
500 info(event->udev, "wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
501 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
503 info(event->udev, "waiting for '%s' failed\n", file);
507 /* handle "[$SUBSYSTEM/$KERNEL]<attribute>" lookup */
508 static int split_subsys_sysname(struct udev *udev, char *attrstr, char **subsys, char **sysname, char **attr)
512 if (attrstr[0] != '[')
515 *subsys = &attrstr[1];
516 pos = strchr(*subsys, ']');
529 pos = strchr(*subsys, '/');
537 static int attr_subst_subdir(char *attr, size_t len)
542 pos = strstr(attr, "/*/");
544 char str[UTIL_PATH_SIZE];
548 util_strlcpy(str, &pos[2], sizeof(str));
553 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
556 if (dent->d_name[0] == '.')
558 util_strlcat(attr, dent->d_name, len);
559 util_strlcat(attr, str, len);
560 if (stat(attr, &stats) == 0) {
569 util_strlcat(attr, str, len);
575 void udev_rules_apply_format(struct udev_event *event, char *string, size_t maxsize)
577 struct udev_device *dev = event->dev;
578 char temp[UTIL_PATH_SIZE];
579 char temp2[UTIL_PATH_SIZE];
580 char *head, *tail, *cpos, *attr, *rest;
603 static const struct subst_map {
606 enum subst_type type;
608 { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
609 { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
610 { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL },
611 { .name = "id", .fmt = 'b', .type = SUBST_ID },
612 { .name = "driver", .fmt = 'd', .type = SUBST_DRIVER },
613 { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
614 { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
615 { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
616 { .name = "attr", .fmt = 's', .type = SUBST_ATTR },
617 { .name = "sysfs", .fmt = 's', .type = SUBST_ATTR },
618 { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
619 { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
620 { .name = "name", .fmt = 'D', .type = SUBST_NAME },
621 { .name = "links", .fmt = 'L', .type = SUBST_LINKS },
622 { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
623 { .name = "sys", .fmt = 'S', .type = SUBST_SYS },
624 { .name = "env", .fmt = 'E', .type = SUBST_ENV },
627 enum subst_type type;
628 const struct subst_map *subst;
633 while (head[0] != '\0') {
634 if (head[0] == '$') {
635 /* substitute named variable */
638 if (head[1] == '$') {
639 util_strlcpy(temp, head+2, sizeof(temp));
640 util_strlcpy(head+1, temp, maxsize);
645 for (subst = map; subst->name; subst++) {
646 if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
648 tail = head + strlen(subst->name)+1;
649 dbg(event->udev, "will substitute format name '%s'\n", subst->name);
654 err(event->udev, "unknown format variable '%s'\n", head);
655 } else if (head[0] == '%') {
656 /* substitute format char */
659 if (head[1] == '%') {
660 util_strlcpy(temp, head+2, sizeof(temp));
661 util_strlcpy(head+1, temp, maxsize);
667 len = get_format_len(event->udev, &tail);
668 for (subst = map; subst->name; subst++) {
669 if (tail[0] == subst->fmt) {
672 dbg(event->udev, "will substitute format char '%c'\n", subst->fmt);
677 err(event->udev, "unknown format char '%c'\n", tail[0]);
683 attr = get_format_attribute(event->udev, &tail);
684 util_strlcpy(temp, tail, sizeof(temp));
685 dbg(event->udev, "format=%i, string='%s', tail='%s'\n", type ,string, tail);
689 util_strlcat(string, udev_device_get_devpath(dev), maxsize);
690 dbg(event->udev, "substitute devpath '%s'\n", udev_device_get_devpath(dev));
693 util_strlcat(string, udev_device_get_sysname(dev), maxsize);
694 dbg(event->udev, "substitute kernel name '%s'\n", udev_device_get_sysname(dev));
696 case SUBST_KERNEL_NUMBER:
697 if (udev_device_get_sysnum(dev) == NULL)
699 util_strlcat(string, udev_device_get_sysnum(dev), maxsize);
700 dbg(event->udev, "substitute kernel number '%s'\n", udev_device_get_sysnum(dev));
703 if (event->dev_parent != NULL) {
704 util_strlcat(string, udev_device_get_sysname(event->dev_parent), maxsize);
705 dbg(event->udev, "substitute id '%s'\n", udev_device_get_sysname(event->dev_parent));
709 if (event->dev_parent != NULL) {
710 const char *driver = udev_device_get_driver(event->dev_parent);
714 util_strlcat(string, driver, maxsize);
715 dbg(event->udev, "substitute driver '%s'\n", driver);
719 sprintf(temp2, "%d", major(udev_device_get_devnum(dev)));
720 util_strlcat(string, temp2, maxsize);
721 dbg(event->udev, "substitute major number '%s'\n", temp2);
724 sprintf(temp2, "%d", minor(udev_device_get_devnum(dev)));
725 util_strlcat(string, temp2, maxsize);
726 dbg(event->udev, "substitute minor number '%s'\n", temp2);
729 if (event->program_result[0] == '\0')
731 /* get part part of the result string */
734 i = strtoul(attr, &rest, 10);
736 dbg(event->udev, "request part #%d of result string\n", i);
737 cpos = event->program_result;
739 while (cpos[0] != '\0' && !isspace(cpos[0]))
741 while (isspace(cpos[0]))
745 err(event->udev, "requested part of result string not found\n");
748 util_strlcpy(temp2, cpos, sizeof(temp2));
749 /* %{2+}c copies the whole string from the second part on */
750 if (rest[0] != '+') {
751 cpos = strchr(temp2, ' ');
755 util_strlcat(string, temp2, maxsize);
756 dbg(event->udev, "substitute part of result string '%s'\n", temp2);
758 util_strlcat(string, event->program_result, maxsize);
759 dbg(event->udev, "substitute result string '%s'\n", event->program_result);
764 err(event->udev, "missing file parameter for attr\n");
769 char value[UTIL_NAME_SIZE] = "";
772 if (split_subsys_sysname(event->udev, attr, &subsys, &sysname, &attrib) == 0) {
773 struct udev_device *d;
778 d = udev_device_new_from_subsystem_sysname(event->udev, subsys, sysname);
781 val = udev_device_get_attr_value(d, attrib);
783 util_strlcpy(value, val, sizeof(value));
784 udev_device_unref(d);
787 /* try the current device, other matches may have selected */
788 if (value[0]=='\0' && event->dev_parent != NULL && event->dev_parent != event->dev) {
791 val = udev_device_get_attr_value(event->dev_parent, attr);
793 util_strlcpy(value, val, sizeof(value));
796 /* look at all devices along the chain of parents */
797 if (value[0]=='\0') {
798 struct udev_device *dev_parent = dev;
802 dbg(event->udev, "looking at '%s'\n", udev_device_get_syspath(dev_parent));
803 val = udev_device_get_attr_value(dev_parent, attr);
805 util_strlcpy(value, val, sizeof(value));
808 dev_parent = udev_device_get_parent(dev_parent);
809 } while (dev_parent != NULL);
815 /* strip trailing whitespace, and replace unwanted characters */
816 size = strlen(value);
817 while (size > 0 && isspace(value[--size]))
819 count = util_replace_chars(value, ALLOWED_CHARS_INPUT);
821 info(event->udev, "%i character(s) replaced\n" , count);
822 util_strlcat(string, value, maxsize);
823 dbg(event->udev, "substitute sysfs value '%s'\n", value);
828 struct udev_device *dev_parent;
831 dev_parent = udev_device_get_parent(event->dev);
832 if (dev_parent == NULL)
834 devnode = udev_device_get_devnode(dev_parent);
835 if (devnode != NULL) {
836 size_t devlen = strlen(udev_get_dev_path(event->udev))+1;
838 util_strlcat(string, &devnode[devlen], maxsize);
839 dbg(event->udev, "found parent '%s', got node name '%s'\n",
840 udev_device_get_syspath(dev_parent), &devnode[devlen]);
844 case SUBST_TEMP_NODE:
845 if (event->tmp_node[0] == '\0' && major(udev_device_get_devnum(dev)) > 0) {
846 dbg(event->udev, "create temporary device node for callout\n");
847 snprintf(event->tmp_node, sizeof(event->tmp_node), "%s/.tmp-%u-%u",
848 udev_get_dev_path(event->udev),
849 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
850 udev_node_mknod(dev, event->tmp_node, makedev(0,0), 0600, 0, 0);
852 util_strlcat(string, event->tmp_node, maxsize);
853 dbg(event->udev, "substitute temporary device node name '%s'\n", event->tmp_node);
856 if (event->name != NULL) {
857 util_strlcat(string, event->name, maxsize);
858 dbg(event->udev, "substitute name '%s'\n", event->name);
860 util_strlcat(string, udev_device_get_sysname(dev), maxsize);
861 dbg(event->udev, "substitute sysname '%s'\n", udev_device_get_sysname(dev));
866 struct udev_list_entry *list_entry;
868 list_entry = udev_device_get_properties_list_entry(dev);
869 util_strlcpy(string, udev_list_entry_get_name(list_entry), maxsize);
870 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) {
871 util_strlcat(string, " ", maxsize);
872 util_strlcat(string, udev_list_entry_get_name(list_entry), maxsize);
877 util_strlcat(string, udev_get_dev_path(event->udev), maxsize);
878 dbg(event->udev, "substitute udev_root '%s'\n", udev_get_dev_path(event->udev));
881 util_strlcat(string, udev_get_sys_path(event->udev), maxsize);
882 dbg(event->udev, "substitute sys_path '%s'\n", udev_get_sys_path(event->udev));
886 dbg(event->udev, "missing attribute\n");
889 struct udev_list_entry *list_entry;
892 list_entry = udev_device_get_properties_list_entry(event->dev);
893 list_entry = udev_list_entry_get_by_name(list_entry, attr);
894 if (list_entry == NULL)
896 value = udev_list_entry_get_value(list_entry);
897 dbg(event->udev, "substitute env '%s=%s'\n", attr, value);
898 util_strlcat(string, value, maxsize);
902 err(event->udev, "unknown substitution type=%i\n", type);
905 /* possibly truncate to format-char specified length */
906 if (len >= 0 && len < (int)strlen(head)) {
908 dbg(event->udev, "truncate to %i chars, subtitution string becomes '%s'\n", len, head);
910 util_strlcat(string, temp, maxsize);
914 static char *key_val(struct udev_rule *rule, struct key *key)
916 return rule->buf + key->val_off;
919 static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
921 return rule->buf + pair->key_name_off;
924 static int match_key(struct udev *udev, const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
926 char value[UTIL_PATH_SIZE];
931 if (key->operation != KEY_OP_MATCH &&
932 key->operation != KEY_OP_NOMATCH)
938 /* look for a matching string, parts are separated by '|' */
939 util_strlcpy(value, rule->buf + key->val_off, sizeof(value));
941 dbg(udev, "key %s value='%s'\n", key_name, key_value);
942 while (key_value != NULL) {
943 pos = strchr(key_value, '|');
949 dbg(udev, "match %s '%s' <-> '%s'\n", key_name, key_value, val);
950 match = (fnmatch(key_value, val, 0) == 0);
957 if (match && (key->operation == KEY_OP_MATCH)) {
958 dbg(udev, "%s is true (matching value)\n", key_name);
961 if (!match && (key->operation == KEY_OP_NOMATCH)) {
962 dbg(udev, "%s is true (non-matching value)\n", key_name);
968 /* match a single rule against a given device and possibly its parent devices */
969 static int match_rule(struct udev_event *event, struct udev_rule *rule)
971 struct udev_device *dev = event->dev;
974 if (match_key(event->udev, "ACTION", rule, &rule->action, udev_device_get_action(dev)))
977 if (match_key(event->udev, "KERNEL", rule, &rule->kernel, udev_device_get_sysname(dev)))
980 if (match_key(event->udev, "SUBSYSTEM", rule, &rule->subsystem, udev_device_get_subsystem(dev)))
983 if (match_key(event->udev, "DEVPATH", rule, &rule->devpath, udev_device_get_devpath(dev)))
986 if (match_key(event->udev, "DRIVER", rule, &rule->driver, udev_device_get_driver(dev)))
989 /* match NAME against a value assigned by an earlier rule */
990 if (match_key(event->udev, "NAME", rule, &rule->name, event->name))
993 /* match against current list of symlinks */
994 if (rule->symlink_match.operation == KEY_OP_MATCH ||
995 rule->symlink_match.operation == KEY_OP_NOMATCH) {
996 size_t devlen = strlen(udev_get_dev_path(event->udev))+1;
997 struct udev_list_entry *list_entry;
1000 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
1001 const char *devlink;
1003 devlink = &udev_list_entry_get_name(list_entry)[devlen];
1004 if (match_key(event->udev, "SYMLINK", rule, &rule->symlink_match, devlink) == 0) {
1013 for (i = 0; i < rule->env.count; i++) {
1014 struct key_pair *pair = &rule->env.keys[i];
1016 /* we only check for matches, assignments will be handled later */
1017 if (pair->key.operation == KEY_OP_MATCH ||
1018 pair->key.operation == KEY_OP_NOMATCH) {
1019 struct udev_list_entry *list_entry;
1020 const char *key_name = key_pair_name(rule, pair);
1023 list_entry = udev_device_get_properties_list_entry(event->dev);
1024 list_entry = udev_list_entry_get_by_name(list_entry, key_name);
1025 value = udev_list_entry_get_value(list_entry);
1026 if (value == NULL) {
1027 dbg(event->udev, "ENV{%s} is not set, treat as empty\n", key_name);
1030 if (match_key(event->udev, "ENV", rule, &pair->key, value))
1035 if (rule->test.operation == KEY_OP_MATCH ||
1036 rule->test.operation == KEY_OP_NOMATCH) {
1037 char filename[UTIL_PATH_SIZE];
1041 struct stat statbuf;
1044 util_strlcpy(filename, key_val(rule, &rule->test), sizeof(filename));
1045 udev_rules_apply_format(event, filename, sizeof(filename));
1047 if (split_subsys_sysname(event->udev, filename, &subsys, &sysname, &attrib) == 0) {
1048 struct udev_device *d;
1049 d = udev_device_new_from_subsystem_sysname(event->udev, subsys, sysname);
1051 util_strlcpy(filename, udev_device_get_syspath(d), sizeof(filename));
1052 if (attrib != NULL) {
1053 util_strlcat(filename, "/", sizeof(filename));
1054 util_strlcat(filename, attrib, sizeof(filename));
1056 udev_device_unref(d);
1058 } else if (filename[0] != '/') {
1059 char tmp[UTIL_PATH_SIZE];
1061 util_strlcpy(tmp, udev_device_get_syspath(dev), sizeof(tmp));
1062 util_strlcat(tmp, "/", sizeof(tmp));
1063 util_strlcat(tmp, filename, sizeof(tmp));
1064 util_strlcpy(filename, tmp, sizeof(filename));
1067 attr_subst_subdir(filename, sizeof(filename));
1069 match = (stat(filename, &statbuf) == 0);
1070 info(event->udev, "'%s' %s", filename, match ? "exists\n" : "does not exist\n");
1071 if (match && rule->test_mode_mask > 0) {
1072 match = ((statbuf.st_mode & rule->test_mode_mask) > 0);
1073 info(event->udev, "'%s' has mode=%#o and %s %#o\n", filename, statbuf.st_mode,
1074 match ? "matches" : "does not match",
1075 rule->test_mode_mask);
1077 if (match && rule->test.operation == KEY_OP_NOMATCH)
1079 if (!match && rule->test.operation == KEY_OP_MATCH)
1081 dbg(event->udev, "TEST key is true\n");
1084 if (rule->wait_for.operation != KEY_OP_UNSET) {
1085 char filename[UTIL_PATH_SIZE];
1088 util_strlcpy(filename, key_val(rule, &rule->wait_for), sizeof(filename));
1089 udev_rules_apply_format(event, filename, sizeof(filename));
1090 found = (wait_for_file(event, filename, 10) == 0);
1091 if (!found && (rule->wait_for.operation != KEY_OP_NOMATCH))
1095 /* check for matching sysfs attribute pairs */
1096 for (i = 0; i < rule->attr.count; i++) {
1097 struct key_pair *pair = &rule->attr.keys[i];
1099 if (pair->key.operation == KEY_OP_MATCH ||
1100 pair->key.operation == KEY_OP_NOMATCH) {
1101 char attr[UTIL_PATH_SIZE];
1102 const char *key_name = key_pair_name(rule, pair);
1103 const char *key_value = key_val(rule, &pair->key);
1107 char value[UTIL_NAME_SIZE] = "";
1110 util_strlcpy(attr, key_name, sizeof(attr));
1111 if (split_subsys_sysname(event->udev, attr, &subsys, &sysname, &attrib) == 0) {
1112 struct udev_device *d;
1117 d = udev_device_new_from_subsystem_sysname(event->udev, subsys, sysname);
1120 val = udev_device_get_attr_value(d, attrib);
1122 util_strlcpy(value, val, sizeof(value));
1123 udev_device_unref(d);
1126 if (value[0]=='\0') {
1129 val = udev_device_get_attr_value(dev, key_name);
1131 util_strlcpy(value, val, sizeof(value));
1137 /* strip trailing whitespace of value, if not asked to match for it */
1138 len = strlen(key_value);
1139 if (len > 0 && !isspace(key_value[len-1])) {
1140 len = strlen(value);
1141 while (len > 0 && isspace(value[--len]))
1143 dbg(event->udev, "removed trailing whitespace from '%s'\n", value);
1146 if (match_key(event->udev, "ATTR", rule, &pair->key, value))
1151 /* walk up the chain of parent devices and find a match */
1152 event->dev_parent = dev;
1154 /* check for matching kernel device name */
1155 if (match_key(event->udev, "KERNELS", rule,
1156 &rule->kernels, udev_device_get_sysname(event->dev_parent)))
1159 /* check for matching subsystem value */
1160 if (match_key(event->udev, "SUBSYSTEMS", rule,
1161 &rule->subsystems, udev_device_get_subsystem(event->dev_parent)))
1164 /* check for matching driver */
1165 if (match_key(event->udev, "DRIVERS", rule,
1166 &rule->drivers, udev_device_get_driver(event->dev_parent)))
1169 /* check for matching sysfs attribute pairs */
1170 for (i = 0; i < rule->attrs.count; i++) {
1171 struct key_pair *pair = &rule->attrs.keys[i];
1173 if (pair->key.operation == KEY_OP_MATCH ||
1174 pair->key.operation == KEY_OP_NOMATCH) {
1175 const char *key_name = key_pair_name(rule, pair);
1176 const char *key_value = key_val(rule, &pair->key);
1178 char value[UTIL_NAME_SIZE];
1181 val = udev_device_get_attr_value(event->dev_parent, key_name);
1183 val = udev_device_get_attr_value(dev, key_name);
1186 util_strlcpy(value, val, sizeof(value));
1188 /* strip trailing whitespace of value, if not asked to match for it */
1189 len = strlen(key_value);
1190 if (len > 0 && !isspace(key_value[len-1])) {
1191 len = strlen(value);
1192 while (len > 0 && isspace(value[--len]))
1194 dbg(event->udev, "removed trailing whitespace from '%s'\n", value);
1197 if (match_key(event->udev, "ATTRS", rule, &pair->key, value))
1202 /* found matching device */
1205 /* move to parent device */
1206 dbg(event->udev, "try parent sysfs device\n");
1207 event->dev_parent = udev_device_get_parent(event->dev_parent);
1208 if (event->dev_parent == NULL)
1210 dbg(event->udev, "looking at dev_parent->devpath='%s'\n",
1211 udev_device_get_syspath(event->dev_parent));
1214 /* execute external program */
1215 if (rule->program.operation != KEY_OP_UNSET) {
1216 char program[UTIL_PATH_SIZE];
1217 char result[UTIL_PATH_SIZE];
1219 util_strlcpy(program, key_val(rule, &rule->program), sizeof(program));
1220 udev_rules_apply_format(event, program, sizeof(program));
1221 if (run_program(event->dev, program, result, sizeof(result), NULL) != 0) {
1222 dbg(event->udev, "PROGRAM is false\n");
1223 event->program_result[0] = '\0';
1224 if (rule->program.operation != KEY_OP_NOMATCH)
1229 dbg(event->udev, "PROGRAM matches\n");
1230 util_remove_trailing_chars(result, '\n');
1231 if (rule->string_escape == ESCAPE_UNSET ||
1232 rule->string_escape == ESCAPE_REPLACE) {
1233 count = util_replace_chars(result, ALLOWED_CHARS_INPUT);
1235 info(event->udev, "%i character(s) replaced\n" , count);
1237 dbg(event->udev, "result is '%s'\n", result);
1238 util_strlcpy(event->program_result, result, sizeof(event->program_result));
1239 dbg(event->udev, "PROGRAM returned successful\n");
1240 if (rule->program.operation == KEY_OP_NOMATCH)
1243 dbg(event->udev, "PROGRAM key is true\n");
1246 /* check for matching result of external program */
1247 if (match_key(event->udev, "RESULT", rule, &rule->result, event->program_result))
1250 /* import variables returned from program or or file into environment */
1251 if (rule->import.operation != KEY_OP_UNSET) {
1252 char import[UTIL_PATH_SIZE];
1255 util_strlcpy(import, key_val(rule, &rule->import), sizeof(import));
1256 udev_rules_apply_format(event, import, sizeof(import));
1257 dbg(event->udev, "check for IMPORT import='%s'\n", import);
1258 if (rule->import_type == IMPORT_PROGRAM) {
1259 rc = import_program_into_env(event->dev, import);
1260 } else if (rule->import_type == IMPORT_FILE) {
1261 dbg(event->udev, "import file import='%s'\n", import);
1262 rc = import_file_into_env(event->dev, import);
1263 } else if (rule->import_type == IMPORT_PARENT) {
1264 dbg(event->udev, "import parent import='%s'\n", import);
1265 rc = import_parent_into_env(event->dev, import);
1268 dbg(event->udev, "IMPORT failed\n");
1269 if (rule->import.operation != KEY_OP_NOMATCH)
1272 dbg(event->udev, "IMPORT '%s' imported\n", key_val(rule, &rule->import));
1273 dbg(event->udev, "IMPORT key is true\n");
1276 /* rule matches, if we have ENV assignments export it */
1277 for (i = 0; i < rule->env.count; i++) {
1278 struct key_pair *pair = &rule->env.keys[i];
1280 if (pair->key.operation == KEY_OP_ASSIGN) {
1281 char temp_value[UTIL_NAME_SIZE];
1282 const char *key_name = key_pair_name(rule, pair);
1283 const char *value = key_val(rule, &pair->key);
1285 /* make sure we don't write to the same string we possibly read from */
1286 util_strlcpy(temp_value, value, sizeof(temp_value));
1287 udev_rules_apply_format(event, temp_value, sizeof(temp_value));
1289 if (temp_value[0] != '\0') {
1290 struct udev_list_entry *entry;
1292 info(event->udev, "set ENV '%s=%s'\n", key_name, temp_value);
1293 entry = udev_device_add_property(dev, key_name, temp_value);
1295 udev_list_entry_set_flag(entry, 1);
1300 /* if we have ATTR assignments, write value to sysfs file */
1301 for (i = 0; i < rule->attr.count; i++) {
1302 struct key_pair *pair = &rule->attr.keys[i];
1304 if (pair->key.operation == KEY_OP_ASSIGN) {
1305 const char *key_name = key_pair_name(rule, pair);
1309 char attr[UTIL_PATH_SIZE];
1310 char value[UTIL_NAME_SIZE];
1313 util_strlcpy(attr, key_name, sizeof(attr));
1314 if (split_subsys_sysname(event->udev, attr, &subsys, &sysname, &attrib) == 0) {
1315 struct udev_device *d;
1317 d = udev_device_new_from_subsystem_sysname(event->udev, subsys, sysname);
1319 util_strlcpy(attr, udev_device_get_syspath(d), sizeof(attr));
1320 if (attrib != NULL) {
1321 util_strlcat(attr, "/", sizeof(attr));
1322 util_strlcat(attr, attrib, sizeof(attr));
1324 udev_device_unref(d);
1327 util_strlcpy(attr, udev_device_get_syspath(dev), sizeof(attr));
1328 util_strlcat(attr, "/", sizeof(attr));
1329 util_strlcat(attr, key_name, sizeof(attr));
1332 attr_subst_subdir(attr, sizeof(attr));
1334 util_strlcpy(value, key_val(rule, &pair->key), sizeof(value));
1335 udev_rules_apply_format(event, value, sizeof(value));
1336 info(event->udev, "writing '%s' to sysfs file '%s'\n", value, attr);
1337 f = fopen(attr, "w");
1340 if (fprintf(f, "%s", value) <= 0)
1341 err(event->udev, "error writing ATTR{%s}: %m\n", attr);
1344 err(event->udev, "error opening ATTR{%s} for writing: %m\n", attr);
1353 int udev_rules_get_name(struct udev_rules *rules, struct udev_event *event)
1355 struct udev_device *dev = event->dev;
1356 struct udev_rules_iter iter;
1357 struct udev_rule *rule;
1360 dbg(event->udev, "device: '%s'\n", udev_device_get_syspath(dev));
1362 /* look for a matching rule to apply */
1363 udev_rules_iter_init(&iter, rules);
1365 rule = udev_rules_iter_next(&iter);
1370 (rule->name.operation == KEY_OP_ASSIGN ||
1371 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
1372 rule->name.operation == KEY_OP_ADD)) {
1373 dbg(event->udev, "node name already set, rule ignored\n");
1377 dbg(event->udev, "process rule\n");
1378 if (match_rule(event, rule) == 0) {
1380 if (rule->ignore_device) {
1381 info(event->udev, "rule applied, '%s' is ignored\n", udev_device_get_sysname(dev));
1382 event->ignore_device = 1;
1385 if (rule->ignore_remove) {
1386 udev_device_set_ignore_remove(dev, 1);
1387 dbg(event->udev, "remove event should be ignored\n");
1389 if (rule->link_priority != 0) {
1390 udev_device_set_devlink_priority(dev, rule->link_priority);
1391 info(event->udev, "devlink_priority=%i\n", rule->link_priority);
1393 if (rule->event_timeout >= 0) {
1394 udev_device_set_event_timeout(dev, rule->event_timeout);
1395 info(event->udev, "event_timeout=%i\n", rule->event_timeout);
1397 /* apply all_partitions option only at a disk device */
1398 if (rule->partitions > 0 &&
1399 strcmp(udev_device_get_subsystem(dev), "block") == 0 &&
1400 udev_device_get_sysnum(dev) == NULL) {
1401 udev_device_set_num_fake_partitions(dev, rule->partitions);
1402 dbg(event->udev, "creation of partition nodes requested\n");
1405 /* apply permissions */
1406 if (!event->mode_final && rule->mode.operation != KEY_OP_UNSET) {
1407 if (rule->mode.operation == KEY_OP_ASSIGN_FINAL)
1408 event->mode_final = 1;
1410 util_strlcpy(buf, key_val(rule, &rule->mode), sizeof(buf));
1411 udev_rules_apply_format(event, buf, sizeof(buf));
1412 event->mode = strtol(buf, NULL, 8);
1413 dbg(event->udev, "applied mode=%#o to '%s'\n",
1414 event->mode, udev_device_get_sysname(dev));
1416 if (!event->owner_final && rule->owner.operation != KEY_OP_UNSET) {
1417 if (rule->owner.operation == KEY_OP_ASSIGN_FINAL)
1418 event->owner_final = 1;
1419 util_strlcpy(event->owner, key_val(rule, &rule->owner), sizeof(event->owner));
1420 udev_rules_apply_format(event, event->owner, sizeof(event->owner));
1421 dbg(event->udev, "applied owner='%s' to '%s'\n",
1422 event->owner, udev_device_get_sysname(dev));
1424 if (!event->group_final && rule->group.operation != KEY_OP_UNSET) {
1425 if (rule->group.operation == KEY_OP_ASSIGN_FINAL)
1426 event->group_final = 1;
1427 util_strlcpy(event->group, key_val(rule, &rule->group), sizeof(event->group));
1428 udev_rules_apply_format(event, event->group, sizeof(event->group));
1429 dbg(event->udev, "applied group='%s' to '%s'\n",
1430 event->group, udev_device_get_sysname(dev));
1433 /* collect symlinks */
1434 if (!event->devlink_final &&
1435 (rule->symlink.operation == KEY_OP_ASSIGN ||
1436 rule->symlink.operation == KEY_OP_ASSIGN_FINAL ||
1437 rule->symlink.operation == KEY_OP_ADD)) {
1438 char temp[UTIL_PATH_SIZE];
1439 char filename[UTIL_PATH_SIZE];
1443 if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL)
1444 event->devlink_final = 1;
1445 if (rule->symlink.operation == KEY_OP_ASSIGN ||
1446 rule->symlink.operation == KEY_OP_ASSIGN_FINAL) {
1447 info(event->udev, "reset symlink list\n");
1448 udev_device_cleanup_devlinks_list(dev);
1450 /* allow multiple symlinks separated by spaces */
1451 util_strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
1452 udev_rules_apply_format(event, temp, sizeof(temp));
1453 if (rule->string_escape == ESCAPE_UNSET)
1454 count = util_replace_chars(temp, ALLOWED_CHARS_FILE " ");
1455 else if (rule->string_escape == ESCAPE_REPLACE)
1456 count = util_replace_chars(temp, ALLOWED_CHARS_FILE);
1458 info(event->udev, "%i character(s) replaced\n" , count);
1459 dbg(event->udev, "rule applied, added symlink(s) '%s'\n", temp);
1461 while (isspace(pos[0]))
1463 next = strchr(pos, ' ');
1466 info(event->udev, "add symlink '%s'\n", pos);
1467 util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename));
1468 util_strlcat(filename, "/", sizeof(filename));
1469 util_strlcat(filename, pos, sizeof(filename));
1470 udev_device_add_devlink(dev, filename);
1471 while (isspace(next[1]))
1474 next = strchr(pos, ' ');
1476 if (pos[0] != '\0') {
1477 info(event->udev, "add symlink '%s'\n", pos);
1478 util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename));
1479 util_strlcat(filename, "/", sizeof(filename));
1480 util_strlcat(filename, pos, sizeof(filename));
1481 udev_device_add_devlink(dev, filename);
1485 /* set name, later rules with name set will be ignored */
1486 if (rule->name.operation == KEY_OP_ASSIGN ||
1487 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
1488 rule->name.operation == KEY_OP_ADD) {
1492 util_strlcpy(event->name, key_val(rule, &rule->name), sizeof(event->name));
1493 udev_rules_apply_format(event, event->name, sizeof(event->name));
1494 if (rule->string_escape == ESCAPE_UNSET ||
1495 rule->string_escape == ESCAPE_REPLACE) {
1496 count = util_replace_chars(event->name, ALLOWED_CHARS_FILE);
1498 info(event->udev, "%i character(s) replaced\n", count);
1501 info(event->udev, "rule applied, '%s' becomes '%s'\n",
1502 udev_device_get_sysname(dev), event->name);
1503 if (strcmp(udev_device_get_subsystem(dev), "net") != 0)
1504 dbg(event->udev, "'%s' owner='%s', group='%s', mode=%#o partitions=%i\n",
1505 event->name, event->owner, event->group, event->mode,
1506 udev_device_get_num_fake_partitions(dev));
1509 if (!event->run_final && rule->run.operation != KEY_OP_UNSET) {
1510 struct udev_list_entry *list_entry;
1512 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1513 event->run_final = 1;
1514 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1515 info(event->udev, "reset run list\n");
1516 udev_list_cleanup(event->udev, &event->run_list);
1518 dbg(event->udev, "add run '%s'\n", key_val(rule, &rule->run));
1519 list_entry = udev_list_entry_add(event->udev, &event->run_list,
1520 key_val(rule, &rule->run), NULL, 1, 0);
1521 if (rule->run_ignore_error && list_entry != NULL)
1522 udev_list_entry_set_flag(list_entry, 1);
1525 if (rule->last_rule) {
1526 dbg(event->udev, "last rule to be applied\n");
1530 if (rule->goto_label.operation != KEY_OP_UNSET) {
1531 dbg(event->udev, "moving forward to label '%s'\n", key_val(rule, &rule->goto_label));
1532 udev_rules_iter_goto(&iter, rule->goto_rule_off);
1538 info(event->udev, "no node name set, will use kernel name '%s'\n",
1539 udev_device_get_sysname(dev));
1540 util_strlcpy(event->name, udev_device_get_sysname(dev), sizeof(event->name));
1543 if (event->tmp_node[0] != '\0') {
1544 dbg(event->udev, "removing temporary device node\n");
1545 unlink_secure(event->udev, event->tmp_node);
1546 event->tmp_node[0] = '\0';
1551 int udev_rules_get_run(struct udev_rules *rules, struct udev_event *event)
1553 struct udev_device *dev = event->dev;
1554 struct udev_rules_iter iter;
1555 struct udev_rule *rule;
1557 dbg(event->udev, "sysname: '%s'\n", udev_device_get_sysname(dev));
1559 /* look for a matching rule to apply */
1560 udev_rules_iter_init(&iter, rules);
1562 rule = udev_rules_iter_next(&iter);
1566 dbg(event->udev, "process rule\n");
1567 if (rule->name.operation == KEY_OP_ASSIGN ||
1568 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
1569 rule->name.operation == KEY_OP_ADD ||
1570 rule->symlink.operation == KEY_OP_ASSIGN ||
1571 rule->symlink.operation == KEY_OP_ASSIGN_FINAL ||
1572 rule->symlink.operation == KEY_OP_ADD ||
1573 rule->mode.operation != KEY_OP_UNSET ||
1574 rule->owner.operation != KEY_OP_UNSET || rule->group.operation != KEY_OP_UNSET) {
1575 dbg(event->udev, "skip rule that names a device\n");
1579 if (match_rule(event, rule) == 0) {
1580 if (rule->ignore_device) {
1581 info(event->udev, "rule applied, '%s' is ignored\n", udev_device_get_sysname(dev));
1582 event->ignore_device = 1;
1585 if (rule->ignore_remove) {
1586 udev_device_set_ignore_remove(dev, 1);
1587 dbg(event->udev, "remove event should be ignored\n");
1590 if (!event->run_final && rule->run.operation != KEY_OP_UNSET) {
1591 struct udev_list_entry *list_entry;
1593 if (rule->run.operation == KEY_OP_ASSIGN ||
1594 rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1595 info(event->udev, "reset run list\n");
1596 udev_list_cleanup(event->udev, &event->run_list);
1598 dbg(event->udev, "add run '%s'\n", key_val(rule, &rule->run));
1599 list_entry = udev_list_entry_add(event->udev, &event->run_list,
1600 key_val(rule, &rule->run), NULL, 1, 0);
1601 if (rule->run_ignore_error && list_entry != NULL)
1602 udev_list_entry_set_flag(list_entry, 1);
1603 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1607 if (rule->last_rule) {
1608 dbg(event->udev, "last rule to be applied\n");
1612 if (rule->goto_label.operation != KEY_OP_UNSET) {
1613 dbg(event->udev, "moving forward to label '%s'\n", key_val(rule, &rule->goto_label));
1614 udev_rules_iter_goto(&iter, rule->goto_rule_off);