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, UDEV_PREFIX "/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();
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_file(struct udevice *udev, const char *file, int timeout)
533 char filepath[PATH_SIZE];
534 char devicepath[PATH_SIZE] = "";
536 int loop = timeout * WAIT_LOOP_PER_SECOND;
538 /* a relative path is a device attribute */
539 if (file[0] != '/') {
540 strlcpy(devicepath, sysfs_path, sizeof(devicepath));
541 strlcat(devicepath, udev->dev->devpath, sizeof(devicepath));
543 strlcpy(filepath, devicepath, sizeof(filepath));
544 strlcat(filepath, "/", sizeof(filepath));
545 strlcat(filepath, file, sizeof(filepath));
549 dbg("will wait %i sec for '%s'\n", timeout, file);
552 if (stat(file, &stats) == 0) {
553 info("file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
556 /* make sure, the device did not disappear in the meantime */
557 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
558 info("device disappeared while waiting for '%s'\n", file);
561 info("wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
562 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
564 info("waiting for '%s' failed\n", file);
568 /* handle "[$SUBSYSTEM/$KERNEL]<attribute>" lookup */
569 static int attr_get_by_subsys_id(const char *attrstr, char *devpath, size_t len, char **attr)
571 char subsys[NAME_SIZE];
577 if (attrstr[0] != '[')
580 attrib = strchr(&attrstr[1], ']');
585 strlcpy(subsys, &attrstr[1], sizeof(subsys));
586 pos = strchr(subsys, ']');
590 id = strchr(subsys, '/');
595 if (sysfs_lookup_devpath_by_subsys_id(devpath, len, subsys, id)) {
597 if (attrib[0] != '\0')
608 static int attr_subst_subdir(char *attr, size_t len)
613 pos = strstr(attr, "/*/");
619 strlcpy(str, &pos[2], sizeof(str));
624 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
627 if (dent->d_name[0] == '.')
629 strlcat(attr, dent->d_name, len);
630 strlcat(attr, str, len);
631 if (stat(attr, &stats) == 0) {
640 strlcat(attr, str, len);
646 void udev_rules_apply_format(struct udevice *udev, char *string, size_t maxsize)
648 char temp[PATH_SIZE];
649 char temp2[PATH_SIZE];
650 char *head, *tail, *pos, *cpos, *attr, *rest;
673 static const struct subst_map {
676 enum subst_type type;
678 { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
679 { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
680 { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL },
681 { .name = "id", .fmt = 'b', .type = SUBST_ID },
682 { .name = "driver", .fmt = 'd', .type = SUBST_DRIVER },
683 { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
684 { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
685 { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
686 { .name = "attr", .fmt = 's', .type = SUBST_ATTR },
687 { .name = "sysfs", .fmt = 's', .type = SUBST_ATTR },
688 { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
689 { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
690 { .name = "name", .fmt = 'D', .type = SUBST_NAME },
691 { .name = "links", .fmt = 'L', .type = SUBST_LINKS },
692 { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
693 { .name = "sys", .fmt = 'S', .type = SUBST_SYS },
694 { .name = "env", .fmt = 'E', .type = SUBST_ENV },
697 enum subst_type type;
698 const struct subst_map *subst;
703 while (head[0] != '\0') {
704 if (head[0] == '$') {
705 /* substitute named variable */
708 if (head[1] == '$') {
709 strlcpy(temp, head+2, sizeof(temp));
710 strlcpy(head+1, temp, maxsize);
715 for (subst = map; subst->name; subst++) {
716 if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
718 tail = head + strlen(subst->name)+1;
719 dbg("will substitute format name '%s'\n", subst->name);
724 err("unknown format variable '%s'\n", head);
725 } else if (head[0] == '%') {
726 /* substitute format char */
729 if (head[1] == '%') {
730 strlcpy(temp, head+2, sizeof(temp));
731 strlcpy(head+1, temp, maxsize);
737 len = get_format_len(&tail);
738 for (subst = map; subst->name; subst++) {
739 if (tail[0] == subst->fmt) {
742 dbg("will substitute format char '%c'\n", subst->fmt);
747 err("unknown format char '%c'\n", tail[0]);
753 attr = get_format_attribute(&tail);
754 strlcpy(temp, tail, sizeof(temp));
755 dbg("format=%i, string='%s', tail='%s'\n", type ,string, tail);
759 strlcat(string, udev->dev->devpath, maxsize);
760 dbg("substitute devpath '%s'\n", udev->dev->devpath);
763 strlcat(string, udev->dev->kernel, maxsize);
764 dbg("substitute kernel name '%s'\n", udev->dev->kernel);
766 case SUBST_KERNEL_NUMBER:
767 strlcat(string, udev->dev->kernel_number, maxsize);
768 dbg("substitute kernel number '%s'\n", udev->dev->kernel_number);
771 if (udev->dev_parent != NULL) {
772 strlcat(string, udev->dev_parent->kernel, maxsize);
773 dbg("substitute id '%s'\n", udev->dev_parent->kernel);
777 if (udev->dev_parent != NULL) {
778 strlcat(string, udev->dev_parent->driver, maxsize);
779 dbg("substitute driver '%s'\n", udev->dev_parent->driver);
783 sprintf(temp2, "%d", major(udev->devt));
784 strlcat(string, temp2, maxsize);
785 dbg("substitute major number '%s'\n", temp2);
788 sprintf(temp2, "%d", minor(udev->devt));
789 strlcat(string, temp2, maxsize);
790 dbg("substitute minor number '%s'\n", temp2);
793 if (udev->program_result[0] == '\0')
795 /* get part part of the result string */
798 i = strtoul(attr, &rest, 10);
800 dbg("request part #%d of result string\n", i);
801 cpos = udev->program_result;
803 while (cpos[0] != '\0' && !isspace(cpos[0]))
805 while (isspace(cpos[0]))
809 err("requested part of result string not found\n");
812 strlcpy(temp2, cpos, sizeof(temp2));
813 /* %{2+}c copies the whole string from the second part on */
814 if (rest[0] != '+') {
815 cpos = strchr(temp2, ' ');
819 strlcat(string, temp2, maxsize);
820 dbg("substitute part of result string '%s'\n", temp2);
822 strlcat(string, udev->program_result, maxsize);
823 dbg("substitute result string '%s'\n", udev->program_result);
828 err("missing file parameter for attr\n");
830 char devpath[PATH_SIZE];
832 const char *value = NULL;
835 if (attr_get_by_subsys_id(attr, devpath, sizeof(devpath), &attrib)) {
837 value = sysfs_attr_get_value(devpath, attrib);
842 /* try the current device, other matches may have selected */
843 if (value == NULL && udev->dev_parent != NULL && udev->dev_parent != udev->dev)
844 value = sysfs_attr_get_value(udev->dev_parent->devpath, attr);
846 /* look at all devices along the chain of parents */
848 struct sysfs_device *dev_parent = udev->dev;
851 dbg("looking at '%s'\n", dev_parent->devpath);
852 value = sysfs_attr_get_value(dev_parent->devpath, attr);
854 strlcpy(temp2, value, sizeof(temp2));
857 dev_parent = sysfs_device_get_parent(dev_parent);
858 } while (dev_parent != NULL);
864 /* strip trailing whitespace, and replace unwanted characters */
865 size = strlcpy(temp2, value, sizeof(temp2));
866 if (size >= sizeof(temp2))
867 size = sizeof(temp2)-1;
868 while (size > 0 && isspace(temp2[size-1]))
869 temp2[--size] = '\0';
870 count = replace_chars(temp2, ALLOWED_CHARS_INPUT);
872 info("%i character(s) replaced\n" , count);
873 strlcat(string, temp2, maxsize);
874 dbg("substitute sysfs value '%s'\n", temp2);
879 struct sysfs_device *dev_parent;
881 dev_parent = sysfs_device_get_parent(udev->dev);
882 if (dev_parent != NULL) {
883 struct udevice *udev_parent;
885 dbg("found parent '%s', get the node name\n", dev_parent->devpath);
886 udev_parent = udev_device_init();
887 if (udev_parent != NULL) {
888 /* lookup the name in the udev_db with the DEVPATH of the parent */
889 if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
890 strlcat(string, udev_parent->name, maxsize);
891 dbg("substitute parent node name'%s'\n", udev_parent->name);
893 dbg("parent not found in database\n");
894 udev_device_cleanup(udev_parent);
899 case SUBST_TEMP_NODE:
900 if (udev->tmp_node[0] == '\0' && major(udev->devt) > 0) {
901 dbg("create temporary device node for callout\n");
902 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
903 udev_root, major(udev->devt), minor(udev->devt));
904 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
905 udev_node_mknod(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
907 strlcat(string, udev->tmp_node, maxsize);
908 dbg("substitute temporary device node name '%s'\n", udev->tmp_node);
911 if (udev->name[0] == '\0') {
912 strlcat(string, udev->dev->kernel, maxsize);
913 dbg("substitute udev->kernel '%s'\n", udev->name);
915 strlcat(string, udev->name, maxsize);
916 dbg("substitute udev->name '%s'\n", udev->name);
920 if (!list_empty(&udev->symlink_list)) {
921 struct name_entry *name_loop;
922 char symlinks[PATH_SIZE] = "";
924 list_for_each_entry(name_loop, &udev->symlink_list, node) {
925 strlcat(symlinks, name_loop->name, sizeof(symlinks));
926 strlcat(symlinks, " ", sizeof(symlinks));
928 remove_trailing_chars(symlinks, ' ');
929 strlcat(string, symlinks, maxsize);
933 strlcat(string, udev_root, maxsize);
934 dbg("substitute udev_root '%s'\n", udev_root);
937 strlcat(string, sysfs_path, maxsize);
938 dbg("substitute sysfs_path '%s'\n", sysfs_path);
942 dbg("missing attribute\n");
947 dbg("env '%s' not available\n", attr);
950 dbg("substitute env '%s=%s'\n", attr, pos);
951 strlcat(string, pos, maxsize);
954 err("unknown substitution type=%i\n", type);
957 /* possibly truncate to format-char specified length */
958 if (len >= 0 && len < (int)strlen(head)) {
960 dbg("truncate to %i chars, subtitution string becomes '%s'\n", len, head);
962 strlcat(string, temp, maxsize);
966 static char *key_val(struct udev_rule *rule, struct key *key)
968 return rule->buf + key->val_off;
971 static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
973 return rule->buf + pair->key_name_off;
976 static int match_key(const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
978 char value[PATH_SIZE];
983 if (key->operation != KEY_OP_MATCH &&
984 key->operation != KEY_OP_NOMATCH)
987 /* look for a matching string, parts are separated by '|' */
988 strlcpy(value, rule->buf + key->val_off, sizeof(value));
990 dbg("key %s value='%s'\n", key_name, key_value);
992 pos = strchr(key_value, '|');
998 dbg("match %s '%s' <-> '%s'\n", key_name, key_value, val);
999 match = (fnmatch(key_value, val, 0) == 0);
1006 if (match && (key->operation == KEY_OP_MATCH)) {
1007 dbg("%s is true (matching value)\n", key_name);
1010 if (!match && (key->operation == KEY_OP_NOMATCH)) {
1011 dbg("%s is true (non-matching value)\n", key_name);
1017 /* match a single rule against a given device and possibly its parent devices */
1018 static int match_rule(struct udevice *udev, struct udev_rule *rule)
1022 if (match_key("ACTION", rule, &rule->action, udev->action))
1025 if (match_key("KERNEL", rule, &rule->kernel, udev->dev->kernel))
1028 if (match_key("SUBSYSTEM", rule, &rule->subsystem, udev->dev->subsystem))
1031 if (match_key("DEVPATH", rule, &rule->devpath, udev->dev->devpath))
1034 if (match_key("DRIVER", rule, &rule->driver, udev->dev->driver))
1037 /* match NAME against a value assigned by an earlier rule */
1038 if (match_key("NAME", rule, &rule->name, udev->name))
1041 /* match against current list of symlinks */
1042 if (rule->symlink_match.operation == KEY_OP_MATCH ||
1043 rule->symlink_match.operation == KEY_OP_NOMATCH) {
1044 struct name_entry *name_loop;
1047 list_for_each_entry(name_loop, &udev->symlink_list, node) {
1048 if (match_key("SYMLINK", rule, &rule->symlink_match, name_loop->name) == 0) {
1057 for (i = 0; i < rule->env.count; i++) {
1058 struct key_pair *pair = &rule->env.keys[i];
1060 /* we only check for matches, assignments will be handled later */
1061 if (pair->key.operation == KEY_OP_MATCH ||
1062 pair->key.operation == KEY_OP_NOMATCH) {
1063 const char *key_name = key_pair_name(rule, pair);
1064 const char *value = getenv(key_name);
1067 dbg("ENV{'%s'} is not set, treat as empty\n", key_name);
1070 if (match_key("ENV", rule, &pair->key, value))
1075 if (rule->test.operation == KEY_OP_MATCH ||
1076 rule->test.operation == KEY_OP_NOMATCH) {
1077 char filename[PATH_SIZE];
1078 char devpath[PATH_SIZE];
1080 struct stat statbuf;
1083 strlcpy(filename, key_val(rule, &rule->test), sizeof(filename));
1084 udev_rules_apply_format(udev, filename, sizeof(filename));
1086 if (attr_get_by_subsys_id(filename, devpath, sizeof(devpath), &attr)) {
1087 strlcpy(filename, sysfs_path, sizeof(filename));
1088 strlcat(filename, devpath, sizeof(filename));
1090 strlcat(filename, "/", sizeof(filename));
1091 strlcat(filename, attr, sizeof(filename));
1093 } else if (filename[0] != '/') {
1094 char tmp[PATH_SIZE];
1096 strlcpy(tmp, sysfs_path, sizeof(tmp));
1097 strlcat(tmp, udev->dev->devpath, sizeof(tmp));
1098 strlcat(tmp, "/", sizeof(tmp));
1099 strlcat(tmp, filename, sizeof(tmp));
1100 strlcpy(filename, tmp, sizeof(filename));
1103 attr_subst_subdir(filename, sizeof(filename));
1105 match = (stat(filename, &statbuf) == 0);
1106 info("'%s' %s", filename, match ? "exists\n" : "does not exist\n");
1107 if (match && rule->test_mode_mask > 0) {
1108 match = ((statbuf.st_mode & rule->test_mode_mask) > 0);
1109 info("'%s' has mode=%#o and %s %#o\n", filename, statbuf.st_mode,
1110 match ? "matches" : "does not match",
1111 rule->test_mode_mask);
1113 if (match && rule->test.operation == KEY_OP_NOMATCH)
1115 if (!match && rule->test.operation == KEY_OP_MATCH)
1117 dbg("TEST key is true\n");
1120 if (rule->wait_for.operation != KEY_OP_UNSET) {
1121 char filename[PATH_SIZE];
1124 strlcpy(filename, key_val(rule, &rule->wait_for), sizeof(filename));
1125 udev_rules_apply_format(udev, filename, sizeof(filename));
1126 found = (wait_for_file(udev, filename, 10) == 0);
1127 if (!found && (rule->wait_for.operation != KEY_OP_NOMATCH))
1131 /* check for matching sysfs attribute pairs */
1132 for (i = 0; i < rule->attr.count; i++) {
1133 struct key_pair *pair = &rule->attr.keys[i];
1135 if (pair->key.operation == KEY_OP_MATCH ||
1136 pair->key.operation == KEY_OP_NOMATCH) {
1137 const char *key_name = key_pair_name(rule, pair);
1138 const char *key_value = key_val(rule, &pair->key);
1139 char devpath[PATH_SIZE];
1141 const char *value = NULL;
1142 char val[VALUE_SIZE];
1145 if (attr_get_by_subsys_id(key_name, devpath, sizeof(devpath), &attrib)) {
1147 value = sysfs_attr_get_value(devpath, attrib);
1152 value = sysfs_attr_get_value(udev->dev->devpath, key_name);
1155 strlcpy(val, value, sizeof(val));
1157 /* strip trailing whitespace of value, if not asked to match for it */
1158 len = strlen(key_value);
1159 if (len > 0 && !isspace(key_value[len-1])) {
1161 while (len > 0 && isspace(val[len-1]))
1163 dbg("removed %zi trailing whitespace chars from '%s'\n", strlen(val)-len, val);
1166 if (match_key("ATTR", rule, &pair->key, val))
1171 /* walk up the chain of parent devices and find a match */
1172 udev->dev_parent = udev->dev;
1174 /* check for matching kernel device name */
1175 if (match_key("KERNELS", rule, &rule->kernels, udev->dev_parent->kernel))
1178 /* check for matching subsystem value */
1179 if (match_key("SUBSYSTEMS", rule, &rule->subsystems, udev->dev_parent->subsystem))
1182 /* check for matching driver */
1183 if (match_key("DRIVERS", rule, &rule->drivers, udev->dev_parent->driver))
1186 /* check for matching sysfs attribute pairs */
1187 for (i = 0; i < rule->attrs.count; i++) {
1188 struct key_pair *pair = &rule->attrs.keys[i];
1190 if (pair->key.operation == KEY_OP_MATCH ||
1191 pair->key.operation == KEY_OP_NOMATCH) {
1192 const char *key_name = key_pair_name(rule, pair);
1193 const char *key_value = key_val(rule, &pair->key);
1195 char val[VALUE_SIZE];
1198 value = sysfs_attr_get_value(udev->dev_parent->devpath, key_name);
1200 value = sysfs_attr_get_value(udev->dev->devpath, key_name);
1203 strlcpy(val, value, sizeof(val));
1205 /* strip trailing whitespace of value, if not asked to match for it */
1206 len = strlen(key_value);
1207 if (len > 0 && !isspace(key_value[len-1])) {
1209 while (len > 0 && isspace(val[len-1]))
1211 dbg("removed %zi trailing whitespace chars from '%s'\n", strlen(val)-len, val);
1214 if (match_key("ATTRS", rule, &pair->key, val))
1219 /* found matching device */
1222 /* move to parent device */
1223 dbg("try parent sysfs device\n");
1224 udev->dev_parent = sysfs_device_get_parent(udev->dev_parent);
1225 if (udev->dev_parent == NULL)
1227 dbg("looking at dev_parent->devpath='%s'\n", udev->dev_parent->devpath);
1228 dbg("looking at dev_parent->kernel='%s'\n", udev->dev_parent->kernel);
1231 /* execute external program */
1232 if (rule->program.operation != KEY_OP_UNSET) {
1233 char program[PATH_SIZE];
1234 char result[PATH_SIZE];
1236 strlcpy(program, key_val(rule, &rule->program), sizeof(program));
1237 udev_rules_apply_format(udev, program, sizeof(program));
1238 if (run_program(program, udev->dev->subsystem, result, sizeof(result), NULL) != 0) {
1239 dbg("PROGRAM is false\n");
1240 udev->program_result[0] = '\0';
1241 if (rule->program.operation != KEY_OP_NOMATCH)
1246 dbg("PROGRAM matches\n");
1247 remove_trailing_chars(result, '\n');
1248 if (rule->string_escape == ESCAPE_UNSET ||
1249 rule->string_escape == ESCAPE_REPLACE) {
1250 count = replace_chars(result, ALLOWED_CHARS_INPUT);
1252 info("%i character(s) replaced\n" , count);
1254 dbg("result is '%s'\n", result);
1255 strlcpy(udev->program_result, result, sizeof(udev->program_result));
1256 dbg("PROGRAM returned successful\n");
1257 if (rule->program.operation == KEY_OP_NOMATCH)
1260 dbg("PROGRAM key is true\n");
1263 /* check for matching result of external program */
1264 if (match_key("RESULT", rule, &rule->result, udev->program_result))
1267 /* import variables returned from program or or file into environment */
1268 if (rule->import.operation != KEY_OP_UNSET) {
1269 char import[PATH_SIZE];
1272 strlcpy(import, key_val(rule, &rule->import), sizeof(import));
1273 udev_rules_apply_format(udev, import, sizeof(import));
1274 dbg("check for IMPORT import='%s'\n", import);
1275 if (rule->import_type == IMPORT_PROGRAM) {
1276 rc = import_program_into_env(udev, import);
1277 } else if (rule->import_type == IMPORT_FILE) {
1278 dbg("import file import='%s'\n", import);
1279 rc = import_file_into_env(udev, import);
1280 } else if (rule->import_type == IMPORT_PARENT) {
1281 dbg("import parent import='%s'\n", import);
1282 rc = import_parent_into_env(udev, import);
1285 dbg("IMPORT failed\n");
1286 if (rule->import.operation != KEY_OP_NOMATCH)
1289 dbg("IMPORT '%s' imported\n", key_val(rule, &rule->import));
1290 dbg("IMPORT key is true\n");
1293 /* rule matches, if we have ENV assignments export it */
1294 for (i = 0; i < rule->env.count; i++) {
1295 struct key_pair *pair = &rule->env.keys[i];
1297 if (pair->key.operation == KEY_OP_ASSIGN) {
1298 char temp_value[NAME_SIZE];
1299 const char *key_name = key_pair_name(rule, pair);
1300 const char *value = key_val(rule, &pair->key);
1302 /* make sure we don't write to the same string we possibly read from */
1303 strlcpy(temp_value, value, sizeof(temp_value));
1304 udev_rules_apply_format(udev, temp_value, NAME_SIZE);
1306 if (temp_value[0] == '\0') {
1307 name_list_key_remove(&udev->env_list, key_name);
1309 info("unset ENV '%s'\n", key_name);
1311 struct name_entry *entry;
1313 entry = name_list_key_add(&udev->env_list, key_name, temp_value);
1316 putenv(entry->name);
1317 info("set ENV '%s'\n", entry->name);
1322 /* if we have ATTR assignments, write value to sysfs file */
1323 for (i = 0; i < rule->attr.count; i++) {
1324 struct key_pair *pair = &rule->attr.keys[i];
1326 if (pair->key.operation == KEY_OP_ASSIGN) {
1327 const char *key_name = key_pair_name(rule, pair);
1328 char devpath[PATH_SIZE];
1330 char attr[PATH_SIZE] = "";
1331 char value[NAME_SIZE];
1334 if (attr_get_by_subsys_id(key_name, devpath, sizeof(devpath), &attrib)) {
1335 if (attrib != NULL) {
1336 strlcpy(attr, sysfs_path, sizeof(attr));
1337 strlcat(attr, devpath, sizeof(attr));
1338 strlcat(attr, "/", sizeof(attr));
1339 strlcat(attr, attrib, sizeof(attr));
1343 if (attr[0] == '\0') {
1344 strlcpy(attr, sysfs_path, sizeof(attr));
1345 strlcat(attr, udev->dev->devpath, sizeof(attr));
1346 strlcat(attr, "/", sizeof(attr));
1347 strlcat(attr, key_name, sizeof(attr));
1350 attr_subst_subdir(attr, sizeof(attr));
1352 strlcpy(value, key_val(rule, &pair->key), sizeof(value));
1353 udev_rules_apply_format(udev, value, sizeof(value));
1354 info("writing '%s' to sysfs file '%s'\n", value, attr);
1355 f = fopen(attr, "w");
1357 if (!udev->test_run)
1358 if (fprintf(f, "%s", value) <= 0)
1359 err("error writing ATTR{%s}: %s\n", attr, strerror(errno));
1362 err("error opening ATTR{%s} for writing: %s\n", attr, strerror(errno));
1371 int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev)
1373 struct udev_rule *rule;
1376 dbg("udev->dev->devpath='%s'\n", udev->dev->devpath);
1377 dbg("udev->dev->kernel='%s'\n", udev->dev->kernel);
1379 /* look for a matching rule to apply */
1380 udev_rules_iter_init(rules);
1382 rule = udev_rules_iter_next(rules);
1387 (rule->name.operation == KEY_OP_ASSIGN ||
1388 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
1389 rule->name.operation == KEY_OP_ADD)) {
1390 dbg("node name already set, rule ignored\n");
1394 dbg("process rule\n");
1395 if (match_rule(udev, rule) == 0) {
1397 if (rule->ignore_device) {
1398 info("rule applied, '%s' is ignored\n", udev->dev->kernel);
1399 udev->ignore_device = 1;
1402 if (rule->ignore_remove) {
1403 udev->ignore_remove = 1;
1404 dbg("remove event should be ignored\n");
1406 if (rule->link_priority != 0) {
1407 udev->link_priority = rule->link_priority;
1408 info("link_priority=%i\n", udev->link_priority);
1410 if (rule->event_timeout >= 0) {
1411 udev->event_timeout = rule->event_timeout;
1412 info("event_timeout=%i\n", udev->event_timeout);
1414 /* apply all_partitions option only at a main block device */
1415 if (rule->partitions &&
1416 strcmp(udev->dev->subsystem, "block") == 0 && udev->dev->kernel_number[0] == '\0') {
1417 udev->partitions = rule->partitions;
1418 dbg("creation of partition nodes requested\n");
1421 /* apply permissions */
1422 if (!udev->mode_final && rule->mode.operation != KEY_OP_UNSET) {
1423 if (rule->mode.operation == KEY_OP_ASSIGN_FINAL)
1424 udev->mode_final = 1;
1426 strlcpy(buf, key_val(rule, &rule->mode), sizeof(buf));
1427 udev_rules_apply_format(udev, buf, sizeof(buf));
1428 udev->mode = strtol(buf, NULL, 8);
1429 dbg("applied mode=%#o to '%s'\n", udev->mode, udev->dev->kernel);
1431 if (!udev->owner_final && rule->owner.operation != KEY_OP_UNSET) {
1432 if (rule->owner.operation == KEY_OP_ASSIGN_FINAL)
1433 udev->owner_final = 1;
1434 strlcpy(udev->owner, key_val(rule, &rule->owner), sizeof(udev->owner));
1435 udev_rules_apply_format(udev, udev->owner, sizeof(udev->owner));
1436 dbg("applied owner='%s' to '%s'\n", udev->owner, udev->dev->kernel);
1438 if (!udev->group_final && rule->group.operation != KEY_OP_UNSET) {
1439 if (rule->group.operation == KEY_OP_ASSIGN_FINAL)
1440 udev->group_final = 1;
1441 strlcpy(udev->group, key_val(rule, &rule->group), sizeof(udev->group));
1442 udev_rules_apply_format(udev, udev->group, sizeof(udev->group));
1443 dbg("applied group='%s' to '%s'\n", udev->group, udev->dev->kernel);
1446 /* collect symlinks */
1447 if (!udev->symlink_final &&
1448 (rule->symlink.operation == KEY_OP_ASSIGN ||
1449 rule->symlink.operation == KEY_OP_ASSIGN_FINAL ||
1450 rule->symlink.operation == KEY_OP_ADD)) {
1451 char temp[PATH_SIZE];
1455 if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL)
1456 udev->symlink_final = 1;
1457 if (rule->symlink.operation == KEY_OP_ASSIGN ||
1458 rule->symlink.operation == KEY_OP_ASSIGN_FINAL) {
1459 info("reset symlink list\n");
1460 name_list_cleanup(&udev->symlink_list);
1462 /* allow multiple symlinks separated by spaces */
1463 strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
1464 udev_rules_apply_format(udev, temp, sizeof(temp));
1465 if (rule->string_escape == ESCAPE_UNSET ||
1466 rule->string_escape == ESCAPE_REPLACE) {
1467 count = replace_chars(temp, ALLOWED_CHARS_FILE " ");
1469 info("%i character(s) replaced\n" , count);
1471 dbg("rule applied, added symlink(s) '%s'\n", temp);
1473 while (isspace(pos[0]))
1475 next = strchr(pos, ' ');
1478 info("add symlink '%s'\n", pos);
1479 name_list_add(&udev->symlink_list, pos, 0);
1480 while (isspace(next[1]))
1483 next = strchr(pos, ' ');
1485 if (pos[0] != '\0') {
1486 info("add symlink '%s'\n", pos);
1487 name_list_add(&udev->symlink_list, pos, 0);
1491 /* set name, later rules with name set will be ignored */
1492 if (rule->name.operation == KEY_OP_ASSIGN ||
1493 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
1494 rule->name.operation == KEY_OP_ADD) {
1498 strlcpy(udev->name, key_val(rule, &rule->name), sizeof(udev->name));
1499 udev_rules_apply_format(udev, udev->name, sizeof(udev->name));
1500 if (rule->string_escape == ESCAPE_UNSET ||
1501 rule->string_escape == ESCAPE_REPLACE) {
1502 count = replace_chars(udev->name, ALLOWED_CHARS_FILE);
1504 info("%i character(s) replaced\n", count);
1507 info("rule applied, '%s' becomes '%s'\n", udev->dev->kernel, udev->name);
1508 if (strcmp(udev->dev->subsystem, "net") != 0)
1509 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i\n",
1510 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
1513 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1514 struct name_entry *entry;
1516 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1517 udev->run_final = 1;
1518 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1519 info("reset run list\n");
1520 name_list_cleanup(&udev->run_list);
1522 dbg("add run '%s'\n", key_val(rule, &rule->run));
1523 entry = name_list_add(&udev->run_list, key_val(rule, &rule->run), 0);
1524 if (rule->run_ignore_error)
1525 entry->ignore_error = 1;
1528 if (rule->last_rule) {
1529 dbg("last rule to be applied\n");
1533 if (rule->goto_label.operation != KEY_OP_UNSET) {
1534 dbg("moving forward to label '%s'\n", key_val(rule, &rule->goto_label));
1535 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));
1541 info("no node name set, will use kernel name '%s'\n", udev->dev->kernel);
1542 strlcpy(udev->name, udev->dev->kernel, sizeof(udev->name));
1545 if (udev->tmp_node[0] != '\0') {
1546 dbg("removing temporary device node\n");
1547 unlink_secure(udev->tmp_node);
1548 udev->tmp_node[0] = '\0';
1554 int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev)
1556 struct udev_rule *rule;
1558 dbg("udev->kernel='%s'\n", udev->dev->kernel);
1560 /* look for a matching rule to apply */
1561 udev_rules_iter_init(rules);
1563 rule = udev_rules_iter_next(rules);
1567 dbg("process rule\n");
1568 if (rule->name.operation == KEY_OP_ASSIGN ||
1569 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
1570 rule->name.operation == KEY_OP_ADD ||
1571 rule->symlink.operation == KEY_OP_ASSIGN ||
1572 rule->symlink.operation == KEY_OP_ASSIGN_FINAL ||
1573 rule->symlink.operation == KEY_OP_ADD ||
1574 rule->mode.operation != KEY_OP_UNSET ||
1575 rule->owner.operation != KEY_OP_UNSET || rule->group.operation != KEY_OP_UNSET) {
1576 dbg("skip rule that names a device\n");
1580 if (match_rule(udev, rule) == 0) {
1581 if (rule->ignore_device) {
1582 info("rule applied, '%s' is ignored\n", udev->dev->kernel);
1583 udev->ignore_device = 1;
1586 if (rule->ignore_remove) {
1587 udev->ignore_remove = 1;
1588 dbg("remove event should be ignored\n");
1591 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1592 struct name_entry *entry;
1594 if (rule->run.operation == KEY_OP_ASSIGN ||
1595 rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1596 info("reset run list\n");
1597 name_list_cleanup(&udev->run_list);
1599 dbg("add run '%s'\n", key_val(rule, &rule->run));
1600 entry = name_list_add(&udev->run_list, key_val(rule, &rule->run), 0);
1601 if (rule->run_ignore_error)
1602 entry->ignore_error = 1;
1603 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1607 if (rule->last_rule) {
1608 dbg("last rule to be applied\n");
1612 if (rule->goto_label.operation != KEY_OP_UNSET) {
1613 dbg("moving forward to label '%s'\n", key_val(rule, &rule->goto_label));
1614 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));