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"
38 #include "udev_selinux.h"
40 extern char **environ;
42 /* extract possible {attr} and move str behind it */
43 static char *get_format_attribute(char **str)
49 pos = strchr(*str, '}');
51 err("missing closing brace for format\n");
57 dbg("attribute='%s', str='%s'\n", attr, *str);
62 /* extract possible format length and move str behind it*/
63 static int get_format_len(char **str)
68 if (isdigit(*str[0])) {
69 num = (int) strtoul(*str, &tail, 10);
72 dbg("format length=%i\n", num);
75 err("format parsing error '%s'\n", *str);
81 static int get_key(char **line, char **key, char **value)
91 while (isspace(linepos[0]))
95 temp = strchr(linepos, '=');
96 if (temp == NULL || temp == linepos)
102 /* get a quoted value */
103 if (linepos[0] == '"' || linepos[0] == '\'') {
104 temp = strchr(&linepos[1], linepos[0]);
107 *value = &linepos[1];
113 temp = strchr(linepos, '\n');
121 static int run_program(const char *command, const char *subsystem,
122 char *result, size_t ressize, size_t *reslen)
125 int outpipe[2] = {-1, -1};
126 int errpipe[2] = {-1, -1};
129 char program[PATH_SIZE];
130 char *argv[(sizeof(arg) / 2) + 1];
135 /* build argv from comand */
136 strlcpy(arg, command, sizeof(arg));
138 if (strchr(arg, ' ') != NULL) {
141 while (pos != NULL) {
142 if (pos[0] == '\'') {
143 /* don't separate if in apostrophes */
145 argv[i] = strsep(&pos, "\'");
146 while (pos != NULL && pos[0] == ' ')
149 argv[i] = strsep(&pos, " ");
151 dbg("arg[%i] '%s'\n", i, argv[i]);
159 info("'%s'\n", command);
161 /* prepare pipes from child to parent */
162 if (result != NULL || udev_log_priority >= LOG_INFO) {
163 if (pipe(outpipe) != 0) {
164 err("pipe failed: %s\n", strerror(errno));
168 if (udev_log_priority >= LOG_INFO) {
169 if (pipe(errpipe) != 0) {
170 err("pipe failed: %s\n", strerror(errno));
175 /* allow programs in /lib/udev called without the path */
176 if (strchr(argv[0], '/') == NULL) {
177 strlcpy(program, "/lib/udev/", sizeof(program));
178 strlcat(program, argv[0], sizeof(program));
185 /* child closes parent ends of pipes */
186 if (outpipe[READ_END] > 0)
187 close(outpipe[READ_END]);
188 if (errpipe[READ_END] > 0)
189 close(errpipe[READ_END]);
191 /* discard child output or connect to pipe */
192 devnull = open("/dev/null", O_RDWR);
194 dup2(devnull, STDIN_FILENO);
195 if (outpipe[WRITE_END] < 0)
196 dup2(devnull, STDOUT_FILENO);
197 if (errpipe[WRITE_END] < 0)
198 dup2(devnull, STDERR_FILENO);
201 err("open /dev/null failed: %s\n", strerror(errno));
202 if (outpipe[WRITE_END] > 0) {
203 dup2(outpipe[WRITE_END], STDOUT_FILENO);
204 close(outpipe[WRITE_END]);
206 if (errpipe[WRITE_END] > 0) {
207 dup2(errpipe[WRITE_END], STDERR_FILENO);
208 close(errpipe[WRITE_END]);
210 execv(argv[0], argv);
211 if (errno == ENOENT || errno == ENOTDIR) {
212 /* may be on a filesytem which is not mounted right now */
213 info("program '%s' not found\n", argv[0]);
216 err("exec of program '%s' failed\n", argv[0]);
220 err("fork of '%s' failed: %s\n", argv[0], strerror(errno));
223 /* read from child if requested */
224 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
228 /* parent closes child ends of pipes */
229 if (outpipe[WRITE_END] > 0)
230 close(outpipe[WRITE_END]);
231 if (errpipe[WRITE_END] > 0)
232 close(errpipe[WRITE_END]);
234 /* read child output */
235 while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
240 if (outpipe[READ_END] > 0)
241 FD_SET(outpipe[READ_END], &readfds);
242 if (errpipe[READ_END] > 0)
243 FD_SET(errpipe[READ_END], &readfds);
244 fdcount = select(UDEV_MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
253 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
258 count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
260 close(outpipe[READ_END]);
261 outpipe[READ_END] = -1;
263 err("stdin read failed: %s\n", strerror(errno));
270 /* store result for rule processing */
272 if (respos + count < ressize) {
273 memcpy(&result[respos], inbuf, count);
276 err("ressize %ld too short\n", (long)ressize);
281 while ((line = strsep(&pos, "\n")))
282 if (pos || line[0] != '\0')
283 info("'%s' (stdout) '%s'\n", argv[0], line);
287 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
292 count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
294 close(errpipe[READ_END]);
295 errpipe[READ_END] = -1;
297 err("stderr read failed: %s\n", strerror(errno));
300 errbuf[count] = '\0';
302 while ((line = strsep(&pos, "\n")))
303 if (pos || line[0] != '\0')
304 info("'%s' (stderr) '%s'\n", argv[0], line);
307 if (outpipe[READ_END] > 0)
308 close(outpipe[READ_END]);
309 if (errpipe[READ_END] > 0)
310 close(errpipe[READ_END]);
312 /* return the childs stdout string */
314 result[respos] = '\0';
315 dbg("result='%s'\n", result);
320 waitpid(pid, &status, 0);
321 if (WIFEXITED(status)) {
322 info("'%s' returned with status %i\n", argv[0], WEXITSTATUS(status));
323 if (WEXITSTATUS(status) != 0)
326 err("'%s' abnormal exit\n", argv[0]);
334 static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bufsize)
336 char line[LINE_SIZE];
345 /* loop through the whole buffer */
348 while (cur < bufsize) {
349 count = buf_get_line(buf, bufsize, cur);
354 /* eat the whitespace */
355 while ((count > 0) && isspace(bufline[0])) {
362 /* see if this is a comment */
363 if (bufline[0] == COMMENT_CHARACTER)
366 if (count >= sizeof(line)) {
367 err("line too long, conf line skipped %s, line %d\n", udev_config_filename, lineno);
371 memcpy(line, bufline, count);
375 if (get_key(&linepos, &variable, &value) == 0) {
376 dbg("import '%s=%s'\n", variable, value);
378 /* handle device, renamed by external tool, returning new path */
379 if (strcmp(variable, "DEVPATH") == 0) {
380 info("updating devpath from '%s' to '%s'\n", udev->dev->devpath, value);
381 sysfs_device_set_values(udev->dev, value, NULL, NULL);
383 name_list_key_add(&udev->env_list, variable, value);
384 setenv(variable, value, 1);
391 static int import_file_into_env(struct udevice *udev, const char *filename)
396 if (file_map(filename, &buf, &bufsize) != 0) {
397 err("can't open '%s': %s\n", filename, strerror(errno));
400 import_keys_into_env(udev, buf, bufsize);
401 file_unmap(buf, bufsize);
406 static int import_program_into_env(struct udevice *udev, const char *program)
411 if (run_program(program, udev->dev->subsystem, result, sizeof(result), &reslen) != 0)
413 return import_keys_into_env(udev, result, reslen);
416 static int import_parent_into_env(struct udevice *udev, const char *filter)
418 struct sysfs_device *dev_parent;
421 dev_parent = sysfs_device_get_parent(udev->dev);
422 if (dev_parent != NULL) {
423 struct udevice *udev_parent;
424 struct name_entry *name_loop;
426 dbg("found parent '%s', get the node name\n", dev_parent->devpath);
427 udev_parent = udev_device_init(NULL);
428 if (udev_parent == NULL)
430 /* import the udev_db of the parent */
431 if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
432 dbg("import stored parent env '%s'\n", udev_parent->name);
433 list_for_each_entry(name_loop, &udev_parent->env_list, node) {
434 char name[NAME_SIZE];
437 strlcpy(name, name_loop->name, sizeof(name));
438 pos = strchr(name, '=');
442 if (fnmatch(filter, name, 0) == 0) {
443 dbg("import key '%s'\n", name_loop->name);
444 name_list_add(&udev->env_list, name_loop->name, 0);
445 setenv(name, pos, 1);
447 dbg("skip key '%s'\n", name_loop->name);
452 dbg("parent not found in database\n");
453 udev_device_cleanup(udev_parent);
459 static int pass_env_to_socket(const char *sockpath, const char *devpath, const char *action)
462 struct sockaddr_un saddr;
471 dbg("pass environment to socket '%s'\n", sockpath);
472 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
473 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
474 saddr.sun_family = AF_LOCAL;
475 if (sockpath[0] == '@') {
476 /* abstract namespace socket requested */
477 strlcpy(&saddr.sun_path[1], &sockpath[1], sizeof(saddr.sun_path)-1);
478 saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
479 } else if (stat(sockpath, &stats) == 0 && S_ISSOCK(stats.st_mode)) {
480 /* existing socket file */
481 strlcpy(saddr.sun_path, sockpath, sizeof(saddr.sun_path));
482 saddrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path);
484 /* no socket file, assume abstract namespace socket */
485 strlcpy(&saddr.sun_path[1], sockpath, sizeof(saddr.sun_path)-1);
486 saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
489 bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
491 for (i = 0; environ[i] != NULL && bufpos < (sizeof(buf)-1); i++) {
492 bufpos += strlcpy(&buf[bufpos], environ[i], sizeof(buf) - bufpos-1);
495 if (bufpos > sizeof(buf))
496 bufpos = sizeof(buf);
498 count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, saddrlen);
501 info("passed %zi bytes to socket '%s', \n", count, sockpath);
507 int udev_rules_run(struct udevice *udev)
509 struct name_entry *name_loop;
512 dbg("executing run list\n");
513 list_for_each_entry(name_loop, &udev->run_list, node) {
514 if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0) {
515 pass_env_to_socket(&name_loop->name[strlen("socket:")], udev->dev->devpath, udev->action);
517 char program[PATH_SIZE];
519 strlcpy(program, name_loop->name, sizeof(program));
520 udev_rules_apply_format(udev, program, sizeof(program));
521 if (run_program(program, udev->dev->subsystem, NULL, 0, NULL) != 0)
522 if (!name_loop->ignore_error)
530 #define WAIT_LOOP_PER_SECOND 50
531 static int wait_for_sysfs(struct udevice *udev, const char *file, int timeout)
533 char devicepath[PATH_SIZE];
534 char filepath[PATH_SIZE];
536 int loop = timeout * WAIT_LOOP_PER_SECOND;
538 strlcpy(devicepath, sysfs_path, sizeof(devicepath));
539 strlcat(devicepath, udev->dev->devpath, sizeof(devicepath));
540 strlcpy(filepath, devicepath, sizeof(filepath));
541 strlcat(filepath, "/", sizeof(filepath));
542 strlcat(filepath, file, sizeof(filepath));
544 dbg("will wait %i sec for '%s'\n", timeout, filepath);
547 if (stat(filepath, &stats) == 0) {
548 info("file '%s' appeared after %i loops\n", filepath, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
551 /* make sure, the device did not disappear in the meantime */
552 if (stat(devicepath, &stats) != 0) {
553 info("device disappeared while waiting for '%s'\n", filepath);
556 info("wait for '%s' for %i mseconds\n", filepath, 1000 / WAIT_LOOP_PER_SECOND);
557 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
559 info("waiting for '%s' failed\n", filepath);
563 /* handle "[$SUBSYSTEM/$KERNEL]<attribute>" lookup */
564 static int attr_get_by_subsys_id(const char *attrstr, char *devpath, size_t len, char **attr)
566 char subsys[NAME_SIZE];
571 if (attrstr[0] != '[')
574 strlcpy(subsys, &attrstr[1], sizeof(subsys));
576 attrib = strchr(subsys, ']');
582 id = strchr(subsys, '/');
588 if (sysfs_lookup_devpath_by_subsys_id(devpath, len, subsys, id)) {
590 if (attrib[0] != '\0')
601 static int attr_subst_subdir(char *attr, size_t len)
606 pos = strstr(attr, "/*/");
612 strlcpy(str, &pos[2], sizeof(str));
617 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
620 if (dent->d_name[0] == '.')
622 strlcat(attr, dent->d_name, len);
623 strlcat(attr, str, len);
624 if (stat(attr, &stats) == 0) {
633 strlcat(attr, str, len);
639 void udev_rules_apply_format(struct udevice *udev, char *string, size_t maxsize)
641 char temp[PATH_SIZE];
642 char temp2[PATH_SIZE];
643 char *head, *tail, *pos, *cpos, *attr, *rest;
666 static const struct subst_map {
669 enum subst_type type;
671 { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
672 { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
673 { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL },
674 { .name = "id", .fmt = 'b', .type = SUBST_ID },
675 { .name = "driver", .fmt = 'd', .type = SUBST_DRIVER },
676 { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
677 { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
678 { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
679 { .name = "attr", .fmt = 's', .type = SUBST_ATTR },
680 { .name = "sysfs", .fmt = 's', .type = SUBST_ATTR },
681 { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
682 { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
683 { .name = "name", .fmt = 'D', .type = SUBST_NAME },
684 { .name = "links", .fmt = 'L', .type = SUBST_LINKS },
685 { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
686 { .name = "sys", .fmt = 'S', .type = SUBST_SYS },
687 { .name = "env", .fmt = 'E', .type = SUBST_ENV },
690 enum subst_type type;
691 const struct subst_map *subst;
696 while (head[0] != '\0') {
697 if (head[0] == '$') {
698 /* substitute named variable */
701 if (head[1] == '$') {
702 strlcpy(temp, head+2, sizeof(temp));
703 strlcpy(head+1, temp, maxsize);
708 for (subst = map; subst->name; subst++) {
709 if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
711 tail = head + strlen(subst->name)+1;
712 dbg("will substitute format name '%s'\n", subst->name);
717 err("unknown format variable '%s'\n", head);
718 } else if (head[0] == '%') {
719 /* substitute format char */
722 if (head[1] == '%') {
723 strlcpy(temp, head+2, sizeof(temp));
724 strlcpy(head+1, temp, maxsize);
730 len = get_format_len(&tail);
731 for (subst = map; subst->name; subst++) {
732 if (tail[0] == subst->fmt) {
735 dbg("will substitute format char '%c'\n", subst->fmt);
740 err("unknown format char '%c'\n", tail[0]);
746 attr = get_format_attribute(&tail);
747 strlcpy(temp, tail, sizeof(temp));
748 dbg("format=%i, string='%s', tail='%s'\n", type ,string, tail);
752 strlcat(string, udev->dev->devpath, maxsize);
753 dbg("substitute devpath '%s'\n", udev->dev->devpath);
756 strlcat(string, udev->dev->kernel, maxsize);
757 dbg("substitute kernel name '%s'\n", udev->dev->kernel);
759 case SUBST_KERNEL_NUMBER:
760 strlcat(string, udev->dev->kernel_number, maxsize);
761 dbg("substitute kernel number '%s'\n", udev->dev->kernel_number);
764 if (udev->dev_parent != NULL) {
765 strlcat(string, udev->dev_parent->kernel, maxsize);
766 dbg("substitute id '%s'\n", udev->dev_parent->kernel);
770 if (udev->dev_parent != NULL) {
771 strlcat(string, udev->dev_parent->driver, maxsize);
772 dbg("substitute driver '%s'\n", udev->dev_parent->driver);
776 sprintf(temp2, "%d", major(udev->devt));
777 strlcat(string, temp2, maxsize);
778 dbg("substitute major number '%s'\n", temp2);
781 sprintf(temp2, "%d", minor(udev->devt));
782 strlcat(string, temp2, maxsize);
783 dbg("substitute minor number '%s'\n", temp2);
786 if (udev->program_result[0] == '\0')
788 /* get part part of the result string */
791 i = strtoul(attr, &rest, 10);
793 dbg("request part #%d of result string\n", i);
794 cpos = udev->program_result;
796 while (cpos[0] != '\0' && !isspace(cpos[0]))
798 while (isspace(cpos[0]))
802 err("requested part of result string not found\n");
805 strlcpy(temp2, cpos, sizeof(temp2));
806 /* %{2+}c copies the whole string from the second part on */
807 if (rest[0] != '+') {
808 cpos = strchr(temp2, ' ');
812 strlcat(string, temp2, maxsize);
813 dbg("substitute part of result string '%s'\n", temp2);
815 strlcat(string, udev->program_result, maxsize);
816 dbg("substitute result string '%s'\n", udev->program_result);
821 err("missing file parameter for attr\n");
823 char devpath[PATH_SIZE];
825 const char *value = NULL;
828 if (attr_get_by_subsys_id(attr, devpath, sizeof(devpath), &attrib)) {
830 value = sysfs_attr_get_value(devpath, attrib);
835 /* try the current device, other matches may have selected */
836 if (value == NULL && udev->dev_parent != NULL && udev->dev_parent != udev->dev)
837 value = sysfs_attr_get_value(udev->dev_parent->devpath, attr);
839 /* look at all devices along the chain of parents */
841 struct sysfs_device *dev_parent = udev->dev;
844 dbg("looking at '%s'\n", dev_parent->devpath);
845 value = sysfs_attr_get_value(dev_parent->devpath, attr);
847 strlcpy(temp2, value, sizeof(temp2));
850 dev_parent = sysfs_device_get_parent(dev_parent);
851 } while (dev_parent != NULL);
857 /* strip trailing whitespace, and replace unwanted characters */
858 size = strlcpy(temp2, value, sizeof(temp2));
859 if (size >= sizeof(temp2))
860 size = sizeof(temp2)-1;
861 while (size > 0 && isspace(temp2[size-1]))
862 temp2[--size] = '\0';
863 count = replace_chars(temp2, ALLOWED_CHARS_INPUT);
865 info("%i character(s) replaced\n" , count);
866 strlcat(string, temp2, maxsize);
867 dbg("substitute sysfs value '%s'\n", temp2);
872 struct sysfs_device *dev_parent;
874 dev_parent = sysfs_device_get_parent(udev->dev);
875 if (dev_parent != NULL) {
876 struct udevice *udev_parent;
878 dbg("found parent '%s', get the node name\n", dev_parent->devpath);
879 udev_parent = udev_device_init(NULL);
880 if (udev_parent != NULL) {
881 /* lookup the name in the udev_db with the DEVPATH of the parent */
882 if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
883 strlcat(string, udev_parent->name, maxsize);
884 dbg("substitute parent node name'%s'\n", udev_parent->name);
886 dbg("parent not found in database\n");
887 udev_device_cleanup(udev_parent);
892 case SUBST_TEMP_NODE:
893 if (udev->tmp_node[0] == '\0' && major(udev->devt) > 0) {
894 dbg("create temporary device node for callout\n");
895 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
896 udev_root, major(udev->devt), minor(udev->devt));
897 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
898 udev_node_mknod(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
900 strlcat(string, udev->tmp_node, maxsize);
901 dbg("substitute temporary device node name '%s'\n", udev->tmp_node);
904 if (udev->name[0] == '\0') {
905 strlcat(string, udev->dev->kernel, maxsize);
906 dbg("substitute udev->kernel '%s'\n", udev->name);
908 strlcat(string, udev->name, maxsize);
909 dbg("substitute udev->name '%s'\n", udev->name);
913 if (!list_empty(&udev->symlink_list)) {
914 struct name_entry *name_loop;
915 char symlinks[PATH_SIZE] = "";
917 list_for_each_entry(name_loop, &udev->symlink_list, node) {
918 strlcat(symlinks, name_loop->name, sizeof(symlinks));
919 strlcat(symlinks, " ", sizeof(symlinks));
921 remove_trailing_chars(symlinks, ' ');
922 strlcat(string, symlinks, maxsize);
926 strlcat(string, udev_root, maxsize);
927 dbg("substitute udev_root '%s'\n", udev_root);
930 strlcat(string, sysfs_path, maxsize);
931 dbg("substitute sysfs_path '%s'\n", sysfs_path);
935 dbg("missing attribute\n");
940 dbg("env '%s' not available\n", attr);
943 dbg("substitute env '%s=%s'\n", attr, pos);
944 strlcat(string, pos, maxsize);
947 err("unknown substitution type=%i\n", type);
950 /* possibly truncate to format-char specified length */
951 if (len >= 0 && len < (int)strlen(head)) {
953 dbg("truncate to %i chars, subtitution string becomes '%s'\n", len, head);
955 strlcat(string, temp, maxsize);
959 static char *key_val(struct udev_rule *rule, struct key *key)
961 return rule->buf + key->val_off;
964 static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
966 return rule->buf + pair->key_name_off;
969 static int match_key(const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
971 char value[PATH_SIZE];
976 if (key->operation != KEY_OP_MATCH &&
977 key->operation != KEY_OP_NOMATCH)
980 /* look for a matching string, parts are separated by '|' */
981 strlcpy(value, rule->buf + key->val_off, sizeof(value));
983 dbg("key %s value='%s'\n", key_name, key_value);
985 pos = strchr(key_value, '|');
991 dbg("match %s '%s' <-> '%s'\n", key_name, key_value, val);
992 match = (fnmatch(key_value, val, 0) == 0);
999 if (match && (key->operation == KEY_OP_MATCH)) {
1000 dbg("%s is true (matching value)\n", key_name);
1003 if (!match && (key->operation == KEY_OP_NOMATCH)) {
1004 dbg("%s is true (non-matching value)\n", key_name);
1010 /* match a single rule against a given device and possibly its parent devices */
1011 static int match_rule(struct udevice *udev, struct udev_rule *rule)
1015 if (match_key("ACTION", rule, &rule->action, udev->action))
1018 if (match_key("KERNEL", rule, &rule->kernel, udev->dev->kernel))
1021 if (match_key("SUBSYSTEM", rule, &rule->subsystem, udev->dev->subsystem))
1024 if (match_key("DEVPATH", rule, &rule->devpath, udev->dev->devpath))
1027 if (match_key("DRIVER", rule, &rule->driver, udev->dev->driver))
1030 /* match NAME against a value assigned by an earlier rule */
1031 if (match_key("NAME", rule, &rule->name, udev->name))
1034 /* match against current list of symlinks */
1035 if (rule->symlink_match.operation == KEY_OP_MATCH ||
1036 rule->symlink_match.operation == KEY_OP_NOMATCH) {
1037 struct name_entry *name_loop;
1040 list_for_each_entry(name_loop, &udev->symlink_list, node) {
1041 if (match_key("SYMLINK", rule, &rule->symlink_match, name_loop->name) == 0) {
1050 for (i = 0; i < rule->env.count; i++) {
1051 struct key_pair *pair = &rule->env.keys[i];
1053 /* we only check for matches, assignments will be handled later */
1054 if (pair->key.operation == KEY_OP_MATCH ||
1055 pair->key.operation == KEY_OP_NOMATCH) {
1056 const char *key_name = key_pair_name(rule, pair);
1057 const char *value = getenv(key_name);
1060 dbg("ENV{'%s'} is not set, treat as empty\n", key_name);
1063 if (match_key("ENV", rule, &pair->key, value))
1068 if (rule->test.operation == KEY_OP_MATCH ||
1069 rule->test.operation == KEY_OP_NOMATCH) {
1070 char filename[PATH_SIZE];
1071 char devpath[PATH_SIZE];
1073 struct stat statbuf;
1076 strlcpy(filename, key_val(rule, &rule->test), sizeof(filename));
1077 udev_rules_apply_format(udev, filename, sizeof(filename));
1079 if (attr_get_by_subsys_id(filename, devpath, sizeof(devpath), &attr)) {
1080 strlcpy(filename, sysfs_path, sizeof(filename));
1081 strlcat(filename, devpath, sizeof(filename));
1083 strlcat(filename, "/", sizeof(filename));
1084 strlcat(filename, attr, sizeof(filename));
1086 } else if (filename[0] != '/') {
1087 char tmp[PATH_SIZE];
1089 strlcpy(tmp, sysfs_path, sizeof(tmp));
1090 strlcat(tmp, udev->dev->devpath, sizeof(tmp));
1091 strlcat(tmp, "/", sizeof(tmp));
1092 strlcat(tmp, filename, sizeof(tmp));
1093 strlcpy(filename, tmp, sizeof(filename));
1096 attr_subst_subdir(filename, sizeof(filename));
1098 match = (stat(filename, &statbuf) == 0);
1099 info("'%s' %s", filename, match ? "exists" : "does not exist\n");
1100 if (match && rule->test_mode_mask > 0) {
1101 match = ((statbuf.st_mode & rule->test_mode_mask) > 0);
1102 info("'%s' has mode=%#o and %s %#o\n", filename, statbuf.st_mode,
1103 match ? "matches" : "does not match",
1104 rule->test_mode_mask);
1106 if (match && rule->test.operation == KEY_OP_NOMATCH)
1108 if (!match && rule->test.operation == KEY_OP_MATCH)
1110 dbg("TEST key is true\n");
1113 if (rule->wait_for_sysfs.operation != KEY_OP_UNSET) {
1116 found = (wait_for_sysfs(udev, key_val(rule, &rule->wait_for_sysfs), 10) == 0);
1117 if (!found && (rule->wait_for_sysfs.operation != KEY_OP_NOMATCH))
1121 /* check for matching sysfs attribute pairs */
1122 for (i = 0; i < rule->attr.count; i++) {
1123 struct key_pair *pair = &rule->attr.keys[i];
1125 if (pair->key.operation == KEY_OP_MATCH ||
1126 pair->key.operation == KEY_OP_NOMATCH) {
1127 const char *key_name = key_pair_name(rule, pair);
1128 const char *key_value = key_val(rule, &pair->key);
1129 char devpath[PATH_SIZE];
1131 const char *value = NULL;
1132 char val[VALUE_SIZE];
1135 if (attr_get_by_subsys_id(key_name, devpath, sizeof(devpath), &attrib)) {
1137 value = sysfs_attr_get_value(devpath, attrib);
1142 value = sysfs_attr_get_value(udev->dev->devpath, key_name);
1145 strlcpy(val, value, sizeof(val));
1147 /* strip trailing whitespace of value, if not asked to match for it */
1148 len = strlen(key_value);
1149 if (len > 0 && !isspace(key_value[len-1])) {
1151 while (len > 0 && isspace(val[len-1]))
1153 dbg("removed %zi trailing whitespace chars from '%s'\n", strlen(val)-len, val);
1156 if (match_key("ATTR", rule, &pair->key, val))
1161 /* walk up the chain of parent devices and find a match */
1162 udev->dev_parent = udev->dev;
1164 /* check for matching kernel device name */
1165 if (match_key("KERNELS", rule, &rule->kernels, udev->dev_parent->kernel))
1168 /* check for matching subsystem value */
1169 if (match_key("SUBSYSTEMS", rule, &rule->subsystems, udev->dev_parent->subsystem))
1172 /* check for matching driver */
1173 if (match_key("DRIVERS", rule, &rule->drivers, udev->dev_parent->driver))
1176 /* check for matching sysfs attribute pairs */
1177 for (i = 0; i < rule->attrs.count; i++) {
1178 struct key_pair *pair = &rule->attrs.keys[i];
1180 if (pair->key.operation == KEY_OP_MATCH ||
1181 pair->key.operation == KEY_OP_NOMATCH) {
1182 const char *key_name = key_pair_name(rule, pair);
1183 const char *key_value = key_val(rule, &pair->key);
1185 char val[VALUE_SIZE];
1188 value = sysfs_attr_get_value(udev->dev_parent->devpath, key_name);
1190 value = sysfs_attr_get_value(udev->dev->devpath, key_name);
1193 strlcpy(val, value, sizeof(val));
1195 /* strip trailing whitespace of value, if not asked to match for it */
1196 len = strlen(key_value);
1197 if (len > 0 && !isspace(key_value[len-1])) {
1199 while (len > 0 && isspace(val[len-1]))
1201 dbg("removed %zi trailing whitespace chars from '%s'\n", strlen(val)-len, val);
1204 if (match_key("ATTRS", rule, &pair->key, val))
1209 /* found matching device */
1212 /* move to parent device */
1213 dbg("try parent sysfs device\n");
1214 udev->dev_parent = sysfs_device_get_parent(udev->dev_parent);
1215 if (udev->dev_parent == NULL)
1217 dbg("looking at dev_parent->devpath='%s'\n", udev->dev_parent->devpath);
1218 dbg("looking at dev_parent->kernel='%s'\n", udev->dev_parent->kernel);
1221 /* execute external program */
1222 if (rule->program.operation != KEY_OP_UNSET) {
1223 char program[PATH_SIZE];
1224 char result[PATH_SIZE];
1226 strlcpy(program, key_val(rule, &rule->program), sizeof(program));
1227 udev_rules_apply_format(udev, program, sizeof(program));
1228 if (run_program(program, udev->dev->subsystem, result, sizeof(result), NULL) != 0) {
1229 dbg("PROGRAM is false\n");
1230 udev->program_result[0] = '\0';
1231 if (rule->program.operation != KEY_OP_NOMATCH)
1236 dbg("PROGRAM matches\n");
1237 remove_trailing_chars(result, '\n');
1238 if (rule->string_escape == ESCAPE_UNSET ||
1239 rule->string_escape == ESCAPE_REPLACE) {
1240 count = replace_chars(result, ALLOWED_CHARS_INPUT);
1242 info("%i character(s) replaced\n" , count);
1244 dbg("result is '%s'\n", result);
1245 strlcpy(udev->program_result, result, sizeof(udev->program_result));
1246 dbg("PROGRAM returned successful\n");
1247 if (rule->program.operation == KEY_OP_NOMATCH)
1250 dbg("PROGRAM key is true\n");
1253 /* check for matching result of external program */
1254 if (match_key("RESULT", rule, &rule->result, udev->program_result))
1257 /* import variables returned from program or or file into environment */
1258 if (rule->import.operation != KEY_OP_UNSET) {
1259 char import[PATH_SIZE];
1262 strlcpy(import, key_val(rule, &rule->import), sizeof(import));
1263 udev_rules_apply_format(udev, import, sizeof(import));
1264 dbg("check for IMPORT import='%s'\n", import);
1265 if (rule->import_type == IMPORT_PROGRAM) {
1266 rc = import_program_into_env(udev, import);
1267 } else if (rule->import_type == IMPORT_FILE) {
1268 dbg("import file import='%s'\n", import);
1269 rc = import_file_into_env(udev, import);
1270 } else if (rule->import_type == IMPORT_PARENT) {
1271 dbg("import parent import='%s'\n", import);
1272 rc = import_parent_into_env(udev, import);
1275 dbg("IMPORT failed\n");
1276 if (rule->import.operation != KEY_OP_NOMATCH)
1279 dbg("IMPORT '%s' imported\n", key_val(rule, &rule->import));
1280 dbg("IMPORT key is true\n");
1283 /* rule matches, if we have ENV assignments export it */
1284 for (i = 0; i < rule->env.count; i++) {
1285 struct key_pair *pair = &rule->env.keys[i];
1287 if (pair->key.operation == KEY_OP_ASSIGN) {
1288 char temp_value[NAME_SIZE];
1289 const char *key_name = key_pair_name(rule, pair);
1290 const char *value = key_val(rule, &pair->key);
1292 /* make sure we don't write to the same string we possibly read from */
1293 strlcpy(temp_value, value, sizeof(temp_value));
1294 udev_rules_apply_format(udev, temp_value, NAME_SIZE);
1296 if (temp_value[0] == '\0') {
1297 name_list_key_remove(&udev->env_list, key_name);
1299 info("unset ENV '%s'\n", key_name);
1301 struct name_entry *entry;
1303 entry = name_list_key_add(&udev->env_list, key_name, temp_value);
1306 putenv(entry->name);
1307 info("set ENV '%s'\n", entry->name);
1312 /* if we have ATTR assignments, write value to sysfs file */
1313 for (i = 0; i < rule->attr.count; i++) {
1314 struct key_pair *pair = &rule->attr.keys[i];
1316 if (pair->key.operation == KEY_OP_ASSIGN) {
1317 const char *key_name = key_pair_name(rule, pair);
1318 char devpath[PATH_SIZE];
1320 char attr[PATH_SIZE] = "";
1321 char value[NAME_SIZE];
1324 if (attr_get_by_subsys_id(key_name, devpath, sizeof(devpath), &attrib)) {
1325 if (attrib != NULL) {
1326 strlcpy(attr, sysfs_path, sizeof(attr));
1327 strlcat(attr, devpath, sizeof(attr));
1328 strlcat(attr, "/", sizeof(attr));
1329 strlcat(attr, attrib, sizeof(attr));
1333 if (attr[0] == '\0') {
1334 strlcpy(attr, sysfs_path, sizeof(attr));
1335 strlcat(attr, udev->dev->devpath, sizeof(attr));
1336 strlcat(attr, "/", sizeof(attr));
1337 strlcat(attr, key_name, sizeof(attr));
1340 attr_subst_subdir(attr, sizeof(attr));
1342 strlcpy(value, key_val(rule, &pair->key), sizeof(value));
1343 udev_rules_apply_format(udev, value, sizeof(value));
1344 info("writing '%s' to sysfs file '%s'\n", value, attr);
1345 f = fopen(attr, "w");
1347 if (!udev->test_run)
1348 if (fprintf(f, "%s", value) <= 0)
1349 err("error writing ATTR{%s}: %s\n", attr, strerror(errno));
1352 err("error opening ATTR{%s} for writing: %s\n", attr, strerror(errno));
1361 int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev)
1363 struct udev_rule *rule;
1366 dbg("udev->dev->devpath='%s'\n", udev->dev->devpath);
1367 dbg("udev->dev->kernel='%s'\n", udev->dev->kernel);
1369 /* look for a matching rule to apply */
1370 udev_rules_iter_init(rules);
1372 rule = udev_rules_iter_next(rules);
1377 (rule->name.operation == KEY_OP_ASSIGN ||
1378 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
1379 rule->name.operation == KEY_OP_ADD)) {
1380 dbg("node name already set, rule ignored\n");
1384 dbg("process rule\n");
1385 if (match_rule(udev, rule) == 0) {
1387 if (rule->ignore_device) {
1388 info("rule applied, '%s' is ignored\n", udev->dev->kernel);
1389 udev->ignore_device = 1;
1392 if (rule->ignore_remove) {
1393 udev->ignore_remove = 1;
1394 dbg("remove event should be ignored\n");
1396 if (rule->link_priority != 0) {
1397 udev->link_priority = rule->link_priority;
1398 info("link_priority=%i\n", udev->link_priority);
1400 /* apply all_partitions option only at a main block device */
1401 if (rule->partitions &&
1402 strcmp(udev->dev->subsystem, "block") == 0 && udev->dev->kernel_number[0] == '\0') {
1403 udev->partitions = rule->partitions;
1404 dbg("creation of partition nodes requested\n");
1407 /* apply permissions */
1408 if (!udev->mode_final && rule->mode.operation != KEY_OP_UNSET) {
1409 if (rule->mode.operation == KEY_OP_ASSIGN_FINAL)
1410 udev->mode_final = 1;
1411 udev->mode = strtol(key_val(rule, &rule->mode), NULL, 8);
1412 dbg("applied mode=%#o to '%s'\n", udev->mode, udev->dev->kernel);
1414 if (!udev->owner_final && rule->owner.operation != KEY_OP_UNSET) {
1415 if (rule->owner.operation == KEY_OP_ASSIGN_FINAL)
1416 udev->owner_final = 1;
1417 strlcpy(udev->owner, key_val(rule, &rule->owner), sizeof(udev->owner));
1418 udev_rules_apply_format(udev, udev->owner, sizeof(udev->owner));
1419 dbg("applied owner='%s' to '%s'\n", udev->owner, udev->dev->kernel);
1421 if (!udev->group_final && rule->group.operation != KEY_OP_UNSET) {
1422 if (rule->group.operation == KEY_OP_ASSIGN_FINAL)
1423 udev->group_final = 1;
1424 strlcpy(udev->group, key_val(rule, &rule->group), sizeof(udev->group));
1425 udev_rules_apply_format(udev, udev->group, sizeof(udev->group));
1426 dbg("applied group='%s' to '%s'\n", udev->group, udev->dev->kernel);
1429 /* collect symlinks */
1430 if (!udev->symlink_final &&
1431 (rule->symlink.operation == KEY_OP_ASSIGN ||
1432 rule->symlink.operation == KEY_OP_ASSIGN_FINAL ||
1433 rule->symlink.operation == KEY_OP_ADD)) {
1434 char temp[PATH_SIZE];
1438 if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL)
1439 udev->symlink_final = 1;
1440 if (rule->symlink.operation == KEY_OP_ASSIGN ||
1441 rule->symlink.operation == KEY_OP_ASSIGN_FINAL) {
1442 info("reset symlink list\n");
1443 name_list_cleanup(&udev->symlink_list);
1445 /* allow multiple symlinks separated by spaces */
1446 strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
1447 udev_rules_apply_format(udev, temp, sizeof(temp));
1448 if (rule->string_escape == ESCAPE_UNSET ||
1449 rule->string_escape == ESCAPE_REPLACE) {
1450 count = replace_chars(temp, ALLOWED_CHARS_FILE " ");
1452 info("%i character(s) replaced\n" , count);
1454 dbg("rule applied, added symlink(s) '%s'\n", temp);
1456 while (isspace(pos[0]))
1458 next = strchr(pos, ' ');
1461 info("add symlink '%s'\n", pos);
1462 name_list_add(&udev->symlink_list, pos, 0);
1463 while (isspace(next[1]))
1466 next = strchr(pos, ' ');
1468 if (pos[0] != '\0') {
1469 info("add symlink '%s'\n", pos);
1470 name_list_add(&udev->symlink_list, pos, 0);
1474 /* set name, later rules with name set will be ignored */
1475 if (rule->name.operation == KEY_OP_ASSIGN ||
1476 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
1477 rule->name.operation == KEY_OP_ADD) {
1481 strlcpy(udev->name, key_val(rule, &rule->name), sizeof(udev->name));
1482 udev_rules_apply_format(udev, udev->name, sizeof(udev->name));
1483 if (rule->string_escape == ESCAPE_UNSET ||
1484 rule->string_escape == ESCAPE_REPLACE) {
1485 count = replace_chars(udev->name, ALLOWED_CHARS_FILE);
1487 info("%i character(s) replaced\n", count);
1490 info("rule applied, '%s' becomes '%s'\n", udev->dev->kernel, udev->name);
1491 if (strcmp(udev->dev->subsystem, "net") != 0)
1492 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i\n",
1493 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
1496 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1497 struct name_entry *entry;
1499 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1500 udev->run_final = 1;
1501 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1502 info("reset run list\n");
1503 name_list_cleanup(&udev->run_list);
1505 dbg("add run '%s'\n", key_val(rule, &rule->run));
1506 entry = name_list_add(&udev->run_list, key_val(rule, &rule->run), 0);
1507 if (rule->run_ignore_error)
1508 entry->ignore_error = 1;
1511 if (rule->last_rule) {
1512 dbg("last rule to be applied\n");
1516 if (rule->goto_label.operation != KEY_OP_UNSET) {
1517 dbg("moving forward to label '%s'\n", key_val(rule, &rule->goto_label));
1518 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));
1524 info("no node name set, will use kernel name '%s'\n", udev->dev->kernel);
1525 strlcpy(udev->name, udev->dev->kernel, sizeof(udev->name));
1528 if (udev->tmp_node[0] != '\0') {
1529 dbg("removing temporary device node\n");
1530 unlink_secure(udev->tmp_node);
1531 udev->tmp_node[0] = '\0';
1537 int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev)
1539 struct udev_rule *rule;
1541 dbg("udev->kernel='%s'\n", udev->dev->kernel);
1543 /* look for a matching rule to apply */
1544 udev_rules_iter_init(rules);
1546 rule = udev_rules_iter_next(rules);
1550 dbg("process rule\n");
1551 if (rule->name.operation == KEY_OP_ASSIGN ||
1552 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
1553 rule->name.operation == KEY_OP_ADD ||
1554 rule->symlink.operation == KEY_OP_ASSIGN ||
1555 rule->symlink.operation == KEY_OP_ASSIGN_FINAL ||
1556 rule->symlink.operation == KEY_OP_ADD ||
1557 rule->mode.operation != KEY_OP_UNSET ||
1558 rule->owner.operation != KEY_OP_UNSET || rule->group.operation != KEY_OP_UNSET) {
1559 dbg("skip rule that names a device\n");
1563 if (match_rule(udev, rule) == 0) {
1564 if (rule->ignore_device) {
1565 info("rule applied, '%s' is ignored\n", udev->dev->kernel);
1566 udev->ignore_device = 1;
1569 if (rule->ignore_remove) {
1570 udev->ignore_remove = 1;
1571 dbg("remove event should be ignored\n");
1574 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1575 struct name_entry *entry;
1577 if (rule->run.operation == KEY_OP_ASSIGN ||
1578 rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1579 info("reset run list\n");
1580 name_list_cleanup(&udev->run_list);
1582 dbg("add run '%s'\n", key_val(rule, &rule->run));
1583 entry = name_list_add(&udev->run_list, key_val(rule, &rule->run), 0);
1584 if (rule->run_ignore_error)
1585 entry->ignore_error = 1;
1586 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1590 if (rule->last_rule) {
1591 dbg("last rule to be applied\n");
1595 if (rule->goto_label.operation != KEY_OP_UNSET) {
1596 dbg("moving forward to label '%s'\n", key_val(rule, &rule->goto_label));
1597 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));