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");
57 dbg("attribute='%s', str='%s'", 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", num);
75 err("format parsing error '%s'", *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'", i, argv[i]);
159 info("'%s'", 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", strerror(errno));
168 if (udev_log_priority >= LOG_INFO) {
169 if (pipe(errpipe) != 0) {
170 err("pipe failed: %s", 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", 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", argv[0]);
216 err("exec of program '%s' failed", argv[0]);
220 err("fork of '%s' failed: %s", 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", 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", (long)ressize);
281 while ((line = strsep(&pos, "\n")))
282 if (pos || line[0] != '\0')
283 info("'%s' (stdout) '%s'", 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", strerror(errno));
300 errbuf[count] = '\0';
302 while ((line = strsep(&pos, "\n")))
303 if (pos || line[0] != '\0')
304 info("'%s' (stderr) '%s'", 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'", result);
320 waitpid(pid, &status, 0);
321 if (WIFEXITED(status)) {
322 info("'%s' returned with status %i", argv[0], WEXITSTATUS(status));
323 if (WEXITSTATUS(status) != 0)
326 err("'%s' abnormal exit", 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", udev_config_filename, lineno);
371 memcpy(line, bufline, count);
375 if (get_key(&linepos, &variable, &value) == 0) {
376 dbg("import '%s=%s'", 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'", 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", 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", 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'", 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'", name_loop->name);
444 name_list_add(&udev->env_list, name_loop->name, 0);
445 setenv(name, pos, 1);
447 dbg("skip key '%s'", name_loop->name);
452 dbg("parent not found in database");
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'", 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', ", count, sockpath);
507 int udev_rules_run(struct udevice *udev)
509 struct name_entry *name_loop;
512 dbg("executing run list");
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'", timeout, filepath);
547 if (stat(filepath, &stats) == 0) {
548 info("file '%s' appeared after %i loops", 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'", filepath);
556 info("wait for '%s' for %i mseconds", filepath, 1000 / WAIT_LOOP_PER_SECOND);
557 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
559 info("waiting for '%s' failed", 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;
665 static const struct subst_map {
668 enum subst_type type;
670 { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
671 { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
672 { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL },
673 { .name = "id", .fmt = 'b', .type = SUBST_ID },
674 { .name = "driver", .fmt = 'd', .type = SUBST_DRIVER },
675 { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
676 { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
677 { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
678 { .name = "attr", .fmt = 's', .type = SUBST_ATTR },
679 { .name = "sysfs", .fmt = 's', .type = SUBST_ATTR },
680 { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
681 { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
682 { .name = "name", .fmt = 'D', .type = SUBST_NAME },
683 { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
684 { .name = "sys", .fmt = 'S', .type = SUBST_SYS },
685 { .name = "env", .fmt = 'E', .type = SUBST_ENV },
688 enum subst_type type;
689 const struct subst_map *subst;
694 while (head[0] != '\0') {
695 if (head[0] == '$') {
696 /* substitute named variable */
699 if (head[1] == '$') {
700 strlcpy(temp, head+2, sizeof(temp));
701 strlcpy(head+1, temp, maxsize);
706 for (subst = map; subst->name; subst++) {
707 if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
709 tail = head + strlen(subst->name)+1;
710 dbg("will substitute format name '%s'", subst->name);
715 err("unknown format variable '%s'", head);
716 } else if (head[0] == '%') {
717 /* substitute format char */
720 if (head[1] == '%') {
721 strlcpy(temp, head+2, sizeof(temp));
722 strlcpy(head+1, temp, maxsize);
728 len = get_format_len(&tail);
729 for (subst = map; subst->name; subst++) {
730 if (tail[0] == subst->fmt) {
733 dbg("will substitute format char '%c'", subst->fmt);
738 err("unknown format char '%c'", tail[0]);
744 attr = get_format_attribute(&tail);
745 strlcpy(temp, tail, sizeof(temp));
746 dbg("format=%i, string='%s', tail='%s'", type ,string, tail);
750 strlcat(string, udev->dev->devpath, maxsize);
751 dbg("substitute devpath '%s'", udev->dev->devpath);
754 strlcat(string, udev->dev->kernel, maxsize);
755 dbg("substitute kernel name '%s'", udev->dev->kernel);
757 case SUBST_KERNEL_NUMBER:
758 strlcat(string, udev->dev->kernel_number, maxsize);
759 dbg("substitute kernel number '%s'", udev->dev->kernel_number);
762 if (udev->dev_parent != NULL) {
763 strlcat(string, udev->dev_parent->kernel, maxsize);
764 dbg("substitute id '%s'", udev->dev_parent->kernel);
768 if (udev->dev_parent != NULL) {
769 strlcat(string, udev->dev_parent->driver, maxsize);
770 dbg("substitute driver '%s'", udev->dev_parent->driver);
774 sprintf(temp2, "%d", major(udev->devt));
775 strlcat(string, temp2, maxsize);
776 dbg("substitute major number '%s'", temp2);
779 sprintf(temp2, "%d", minor(udev->devt));
780 strlcat(string, temp2, maxsize);
781 dbg("substitute minor number '%s'", temp2);
784 if (udev->program_result[0] == '\0')
786 /* get part part of the result string */
789 i = strtoul(attr, &rest, 10);
791 dbg("request part #%d of result string", i);
792 cpos = udev->program_result;
794 while (cpos[0] != '\0' && !isspace(cpos[0]))
796 while (isspace(cpos[0]))
800 err("requested part of result string not found");
803 strlcpy(temp2, cpos, sizeof(temp2));
804 /* %{2+}c copies the whole string from the second part on */
805 if (rest[0] != '+') {
806 cpos = strchr(temp2, ' ');
810 strlcat(string, temp2, maxsize);
811 dbg("substitute part of result string '%s'", temp2);
813 strlcat(string, udev->program_result, maxsize);
814 dbg("substitute result string '%s'", udev->program_result);
819 err("missing file parameter for attr");
821 char devpath[PATH_SIZE];
823 const char *value = NULL;
826 if (attr_get_by_subsys_id(attr, devpath, sizeof(devpath), &attrib)) {
828 value = sysfs_attr_get_value(devpath, attrib);
833 /* try the current device, other matches may have selected */
834 if (value == NULL && udev->dev_parent != NULL && udev->dev_parent != udev->dev)
835 value = sysfs_attr_get_value(udev->dev_parent->devpath, attr);
837 /* look at all devices along the chain of parents */
839 struct sysfs_device *dev_parent = udev->dev;
842 dbg("looking at '%s'", dev_parent->devpath);
843 value = sysfs_attr_get_value(dev_parent->devpath, attr);
845 strlcpy(temp2, value, sizeof(temp2));
848 dev_parent = sysfs_device_get_parent(dev_parent);
849 } while (dev_parent != NULL);
855 /* strip trailing whitespace, and replace unwanted characters */
856 size = strlcpy(temp2, value, sizeof(temp2));
857 if (size >= sizeof(temp2))
858 size = sizeof(temp2)-1;
859 while (size > 0 && isspace(temp2[size-1]))
860 temp2[--size] = '\0';
861 count = replace_chars(temp2, ALLOWED_CHARS_INPUT);
863 info("%i character(s) replaced" , count);
864 strlcat(string, temp2, maxsize);
865 dbg("substitute sysfs value '%s'", temp2);
870 struct sysfs_device *dev_parent;
872 dev_parent = sysfs_device_get_parent(udev->dev);
873 if (dev_parent != NULL) {
874 struct udevice *udev_parent;
876 dbg("found parent '%s', get the node name", dev_parent->devpath);
877 udev_parent = udev_device_init(NULL);
878 if (udev_parent != NULL) {
879 /* lookup the name in the udev_db with the DEVPATH of the parent */
880 if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
881 strlcat(string, udev_parent->name, maxsize);
882 dbg("substitute parent node name'%s'", udev_parent->name);
884 dbg("parent not found in database");
885 udev_device_cleanup(udev_parent);
890 case SUBST_TEMP_NODE:
891 if (udev->tmp_node[0] == '\0' && major(udev->devt) > 0) {
892 dbg("create temporary device node for callout");
893 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
894 udev_root, major(udev->devt), minor(udev->devt));
895 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
896 udev_node_mknod(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
898 strlcat(string, udev->tmp_node, maxsize);
899 dbg("substitute temporary device node name '%s'", udev->tmp_node);
902 strlcat(string, udev->name, maxsize);
903 dbg("substitute udev->name '%s'", udev->name);
906 strlcat(string, udev_root, maxsize);
907 dbg("substitute udev_root '%s'", udev_root);
910 strlcat(string, sysfs_path, maxsize);
911 dbg("substitute sysfs_path '%s'", sysfs_path);
915 dbg("missing attribute");
920 dbg("env '%s' not available", attr);
923 dbg("substitute env '%s=%s'", attr, pos);
924 strlcat(string, pos, maxsize);
927 err("unknown substitution type=%i", type);
930 /* possibly truncate to format-char specified length */
931 if (len >= 0 && len < (int)strlen(head)) {
933 dbg("truncate to %i chars, subtitution string becomes '%s'", len, head);
935 strlcat(string, temp, maxsize);
939 static char *key_val(struct udev_rule *rule, struct key *key)
941 return rule->buf + key->val_off;
944 static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
946 return rule->buf + pair->key_name_off;
949 static int match_key(const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
951 char value[PATH_SIZE];
956 if (key->operation != KEY_OP_MATCH &&
957 key->operation != KEY_OP_NOMATCH)
960 /* look for a matching string, parts are separated by '|' */
961 strlcpy(value, rule->buf + key->val_off, sizeof(value));
963 dbg("key %s value='%s'", key_name, key_value);
965 pos = strchr(key_value, '|');
971 dbg("match %s '%s' <-> '%s'", key_name, key_value, val);
972 match = (fnmatch(key_value, val, 0) == 0);
979 if (match && (key->operation == KEY_OP_MATCH)) {
980 dbg("%s is true (matching value)", key_name);
983 if (!match && (key->operation == KEY_OP_NOMATCH)) {
984 dbg("%s is true (non-matching value)", key_name);
990 /* match a single rule against a given device and possibly its parent devices */
991 static int match_rule(struct udevice *udev, struct udev_rule *rule)
995 if (match_key("ACTION", rule, &rule->action, udev->action))
998 if (match_key("KERNEL", rule, &rule->kernel, udev->dev->kernel))
1001 if (match_key("SUBSYSTEM", rule, &rule->subsystem, udev->dev->subsystem))
1004 if (match_key("DEVPATH", rule, &rule->devpath, udev->dev->devpath))
1007 if (match_key("DRIVER", rule, &rule->driver, udev->dev->driver))
1010 /* match NAME against a value assigned by an earlier rule */
1011 if (match_key("NAME", rule, &rule->name, udev->name))
1014 /* match against current list of symlinks */
1015 if (rule->symlink_match.operation == KEY_OP_MATCH ||
1016 rule->symlink_match.operation == KEY_OP_NOMATCH) {
1017 struct name_entry *name_loop;
1020 list_for_each_entry(name_loop, &udev->symlink_list, node) {
1021 if (match_key("SYMLINK", rule, &rule->symlink_match, name_loop->name) == 0) {
1030 for (i = 0; i < rule->env.count; i++) {
1031 struct key_pair *pair = &rule->env.keys[i];
1033 /* we only check for matches, assignments will be handled later */
1034 if (pair->key.operation == KEY_OP_MATCH ||
1035 pair->key.operation == KEY_OP_NOMATCH) {
1036 const char *key_name = key_pair_name(rule, pair);
1037 const char *value = getenv(key_name);
1040 dbg("ENV{'%s'} is not set, treat as empty", key_name);
1043 if (match_key("ENV", rule, &pair->key, value))
1048 if (rule->test.operation == KEY_OP_MATCH ||
1049 rule->test.operation == KEY_OP_NOMATCH) {
1050 char filename[PATH_SIZE];
1051 char devpath[PATH_SIZE];
1053 struct stat statbuf;
1056 strlcpy(filename, key_val(rule, &rule->test), sizeof(filename));
1057 udev_rules_apply_format(udev, filename, sizeof(filename));
1059 if (attr_get_by_subsys_id(filename, devpath, sizeof(devpath), &attr)) {
1060 strlcpy(filename, sysfs_path, sizeof(filename));
1061 strlcat(filename, devpath, sizeof(filename));
1063 strlcat(filename, "/", sizeof(filename));
1064 strlcat(filename, attr, sizeof(filename));
1066 } else if (filename[0] != '/') {
1067 char tmp[PATH_SIZE];
1069 strlcpy(tmp, sysfs_path, sizeof(tmp));
1070 strlcat(tmp, udev->dev->devpath, sizeof(tmp));
1071 strlcat(tmp, "/", sizeof(tmp));
1072 strlcat(tmp, filename, sizeof(tmp));
1073 strlcpy(filename, tmp, sizeof(filename));
1076 attr_subst_subdir(filename, sizeof(filename));
1078 match = (stat(filename, &statbuf) == 0);
1079 info("'%s' %s", filename, match ? "exists" : "does not exist");
1080 if (match && rule->test_mode_mask > 0) {
1081 match = ((statbuf.st_mode & rule->test_mode_mask) > 0);
1082 info("'%s' has mode=%#o and %s %#o", filename, statbuf.st_mode,
1083 match ? "matches" : "does not match",
1084 rule->test_mode_mask);
1086 if (match && rule->test.operation == KEY_OP_NOMATCH)
1088 if (!match && rule->test.operation == KEY_OP_MATCH)
1090 dbg("TEST key is true");
1093 if (rule->wait_for_sysfs.operation != KEY_OP_UNSET) {
1096 found = (wait_for_sysfs(udev, key_val(rule, &rule->wait_for_sysfs), 10) == 0);
1097 if (!found && (rule->wait_for_sysfs.operation != KEY_OP_NOMATCH))
1101 /* check for matching sysfs attribute pairs */
1102 for (i = 0; i < rule->attr.count; i++) {
1103 struct key_pair *pair = &rule->attr.keys[i];
1105 if (pair->key.operation == KEY_OP_MATCH ||
1106 pair->key.operation == KEY_OP_NOMATCH) {
1107 const char *key_name = key_pair_name(rule, pair);
1108 const char *key_value = key_val(rule, &pair->key);
1109 char devpath[PATH_SIZE];
1111 const char *value = NULL;
1112 char val[VALUE_SIZE];
1115 if (attr_get_by_subsys_id(key_name, devpath, sizeof(devpath), &attrib)) {
1117 value = sysfs_attr_get_value(devpath, attrib);
1122 value = sysfs_attr_get_value(udev->dev->devpath, key_name);
1125 strlcpy(val, value, sizeof(val));
1127 /* strip trailing whitespace of value, if not asked to match for it */
1128 len = strlen(key_value);
1129 if (len > 0 && !isspace(key_value[len-1])) {
1131 while (len > 0 && isspace(val[len-1]))
1133 dbg("removed %zi trailing whitespace chars from '%s'", strlen(val)-len, val);
1136 if (match_key("ATTR", rule, &pair->key, val))
1141 /* walk up the chain of parent devices and find a match */
1142 udev->dev_parent = udev->dev;
1144 /* check for matching kernel device name */
1145 if (match_key("KERNELS", rule, &rule->kernels, udev->dev_parent->kernel))
1148 /* check for matching subsystem value */
1149 if (match_key("SUBSYSTEMS", rule, &rule->subsystems, udev->dev_parent->subsystem))
1152 /* check for matching driver */
1153 if (match_key("DRIVERS", rule, &rule->drivers, udev->dev_parent->driver))
1156 /* check for matching sysfs attribute pairs */
1157 for (i = 0; i < rule->attrs.count; i++) {
1158 struct key_pair *pair = &rule->attrs.keys[i];
1160 if (pair->key.operation == KEY_OP_MATCH ||
1161 pair->key.operation == KEY_OP_NOMATCH) {
1162 const char *key_name = key_pair_name(rule, pair);
1163 const char *key_value = key_val(rule, &pair->key);
1165 char val[VALUE_SIZE];
1168 value = sysfs_attr_get_value(udev->dev_parent->devpath, key_name);
1170 value = sysfs_attr_get_value(udev->dev->devpath, key_name);
1173 strlcpy(val, value, sizeof(val));
1175 /* strip trailing whitespace of value, if not asked to match for it */
1176 len = strlen(key_value);
1177 if (len > 0 && !isspace(key_value[len-1])) {
1179 while (len > 0 && isspace(val[len-1]))
1181 dbg("removed %zi trailing whitespace chars from '%s'", strlen(val)-len, val);
1184 if (match_key("ATTRS", rule, &pair->key, val))
1189 /* found matching device */
1192 /* move to parent device */
1193 dbg("try parent sysfs device");
1194 udev->dev_parent = sysfs_device_get_parent(udev->dev_parent);
1195 if (udev->dev_parent == NULL)
1197 dbg("looking at dev_parent->devpath='%s'", udev->dev_parent->devpath);
1198 dbg("looking at dev_parent->kernel='%s'", udev->dev_parent->kernel);
1201 /* execute external program */
1202 if (rule->program.operation != KEY_OP_UNSET) {
1203 char program[PATH_SIZE];
1204 char result[PATH_SIZE];
1206 strlcpy(program, key_val(rule, &rule->program), sizeof(program));
1207 udev_rules_apply_format(udev, program, sizeof(program));
1208 if (run_program(program, udev->dev->subsystem, result, sizeof(result), NULL) != 0) {
1209 dbg("PROGRAM is false");
1210 udev->program_result[0] = '\0';
1211 if (rule->program.operation != KEY_OP_NOMATCH)
1216 dbg("PROGRAM matches");
1217 remove_trailing_chars(result, '\n');
1218 if (rule->string_escape == ESCAPE_UNSET ||
1219 rule->string_escape == ESCAPE_REPLACE) {
1220 count = replace_chars(result, ALLOWED_CHARS_INPUT);
1222 info("%i character(s) replaced" , count);
1224 dbg("result is '%s'", result);
1225 strlcpy(udev->program_result, result, sizeof(udev->program_result));
1226 dbg("PROGRAM returned successful");
1227 if (rule->program.operation == KEY_OP_NOMATCH)
1230 dbg("PROGRAM key is true");
1233 /* check for matching result of external program */
1234 if (match_key("RESULT", rule, &rule->result, udev->program_result))
1237 /* import variables returned from program or or file into environment */
1238 if (rule->import.operation != KEY_OP_UNSET) {
1239 char import[PATH_SIZE];
1242 strlcpy(import, key_val(rule, &rule->import), sizeof(import));
1243 udev_rules_apply_format(udev, import, sizeof(import));
1244 dbg("check for IMPORT import='%s'", import);
1245 if (rule->import_type == IMPORT_PROGRAM) {
1246 rc = import_program_into_env(udev, import);
1247 } else if (rule->import_type == IMPORT_FILE) {
1248 dbg("import file import='%s'", import);
1249 rc = import_file_into_env(udev, import);
1250 } else if (rule->import_type == IMPORT_PARENT) {
1251 dbg("import parent import='%s'", import);
1252 rc = import_parent_into_env(udev, import);
1255 dbg("IMPORT failed");
1256 if (rule->import.operation != KEY_OP_NOMATCH)
1259 dbg("IMPORT '%s' imported", key_val(rule, &rule->import));
1260 dbg("IMPORT key is true");
1263 /* rule matches, if we have ENV assignments export it */
1264 for (i = 0; i < rule->env.count; i++) {
1265 struct key_pair *pair = &rule->env.keys[i];
1267 if (pair->key.operation == KEY_OP_ASSIGN) {
1268 char temp_value[NAME_SIZE];
1269 const char *key_name = key_pair_name(rule, pair);
1270 const char *value = key_val(rule, &pair->key);
1272 /* make sure we don't write to the same string we possibly read from */
1273 strlcpy(temp_value, value, sizeof(temp_value));
1274 udev_rules_apply_format(udev, temp_value, NAME_SIZE);
1276 if (temp_value[0] == '\0') {
1277 name_list_key_remove(&udev->env_list, key_name);
1279 info("unset ENV '%s'", key_name);
1281 struct name_entry *entry;
1283 entry = name_list_key_add(&udev->env_list, key_name, temp_value);
1286 putenv(entry->name);
1287 info("set ENV '%s'", entry->name);
1292 /* if we have ATTR assignments, write value to sysfs file */
1293 for (i = 0; i < rule->attr.count; i++) {
1294 struct key_pair *pair = &rule->attr.keys[i];
1296 if (pair->key.operation == KEY_OP_ASSIGN) {
1297 const char *key_name = key_pair_name(rule, pair);
1298 char devpath[PATH_SIZE];
1300 char attr[PATH_SIZE] = "";
1301 char value[NAME_SIZE];
1304 if (attr_get_by_subsys_id(key_name, devpath, sizeof(devpath), &attrib)) {
1305 if (attrib != NULL) {
1306 strlcpy(attr, sysfs_path, sizeof(attr));
1307 strlcat(attr, devpath, sizeof(attr));
1308 strlcat(attr, "/", sizeof(attr));
1309 strlcat(attr, attrib, sizeof(attr));
1313 if (attr[0] == '\0') {
1314 strlcpy(attr, sysfs_path, sizeof(attr));
1315 strlcat(attr, udev->dev->devpath, sizeof(attr));
1316 strlcat(attr, "/", sizeof(attr));
1317 strlcat(attr, key_name, sizeof(attr));
1320 attr_subst_subdir(attr, sizeof(attr));
1322 strlcpy(value, key_val(rule, &pair->key), sizeof(value));
1323 udev_rules_apply_format(udev, value, sizeof(value));
1324 info("writing '%s' to sysfs file '%s'", value, attr);
1325 f = fopen(attr, "w");
1327 if (!udev->test_run)
1328 if (fprintf(f, "%s", value) <= 0)
1329 err("error writing ATTR{%s}: %s", attr, strerror(errno));
1332 err("error opening ATTR{%s} for writing: %s", attr, strerror(errno));
1341 int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev)
1343 struct udev_rule *rule;
1346 dbg("udev->dev->devpath='%s'", udev->dev->devpath);
1347 dbg("udev->dev->kernel='%s'", udev->dev->kernel);
1349 /* look for a matching rule to apply */
1350 udev_rules_iter_init(rules);
1352 rule = udev_rules_iter_next(rules);
1357 (rule->name.operation == KEY_OP_ASSIGN ||
1358 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
1359 rule->name.operation == KEY_OP_ADD)) {
1360 dbg("node name already set, rule ignored");
1364 dbg("process rule");
1365 if (match_rule(udev, rule) == 0) {
1367 if (rule->ignore_device) {
1368 info("rule applied, '%s' is ignored", udev->dev->kernel);
1369 udev->ignore_device = 1;
1372 if (rule->ignore_remove) {
1373 udev->ignore_remove = 1;
1374 dbg("remove event should be ignored");
1376 if (rule->link_priority != 0) {
1377 udev->link_priority = rule->link_priority;
1378 info("link_priority=%i", udev->link_priority);
1380 /* apply all_partitions option only at a main block device */
1381 if (rule->partitions &&
1382 strcmp(udev->dev->subsystem, "block") == 0 && udev->dev->kernel_number[0] == '\0') {
1383 udev->partitions = rule->partitions;
1384 dbg("creation of partition nodes requested");
1387 /* apply permissions */
1388 if (!udev->mode_final && rule->mode.operation != KEY_OP_UNSET) {
1389 if (rule->mode.operation == KEY_OP_ASSIGN_FINAL)
1390 udev->mode_final = 1;
1391 udev->mode = strtol(key_val(rule, &rule->mode), NULL, 8);
1392 dbg("applied mode=%#o to '%s'", udev->mode, udev->dev->kernel);
1394 if (!udev->owner_final && rule->owner.operation != KEY_OP_UNSET) {
1395 if (rule->owner.operation == KEY_OP_ASSIGN_FINAL)
1396 udev->owner_final = 1;
1397 strlcpy(udev->owner, key_val(rule, &rule->owner), sizeof(udev->owner));
1398 udev_rules_apply_format(udev, udev->owner, sizeof(udev->owner));
1399 dbg("applied owner='%s' to '%s'", udev->owner, udev->dev->kernel);
1401 if (!udev->group_final && rule->group.operation != KEY_OP_UNSET) {
1402 if (rule->group.operation == KEY_OP_ASSIGN_FINAL)
1403 udev->group_final = 1;
1404 strlcpy(udev->group, key_val(rule, &rule->group), sizeof(udev->group));
1405 udev_rules_apply_format(udev, udev->group, sizeof(udev->group));
1406 dbg("applied group='%s' to '%s'", udev->group, udev->dev->kernel);
1409 /* collect symlinks */
1410 if (!udev->symlink_final &&
1411 (rule->symlink.operation == KEY_OP_ASSIGN ||
1412 rule->symlink.operation == KEY_OP_ASSIGN_FINAL ||
1413 rule->symlink.operation == KEY_OP_ADD)) {
1414 char temp[PATH_SIZE];
1418 if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL)
1419 udev->symlink_final = 1;
1420 if (rule->symlink.operation == KEY_OP_ASSIGN ||
1421 rule->symlink.operation == KEY_OP_ASSIGN_FINAL) {
1422 info("reset symlink list");
1423 name_list_cleanup(&udev->symlink_list);
1425 /* allow multiple symlinks separated by spaces */
1426 strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
1427 udev_rules_apply_format(udev, temp, sizeof(temp));
1428 if (rule->string_escape == ESCAPE_UNSET ||
1429 rule->string_escape == ESCAPE_REPLACE) {
1430 count = replace_chars(temp, ALLOWED_CHARS_FILE " ");
1432 info("%i character(s) replaced" , count);
1434 dbg("rule applied, added symlink(s) '%s'", temp);
1436 while (isspace(pos[0]))
1438 next = strchr(pos, ' ');
1441 info("add symlink '%s'", pos);
1442 name_list_add(&udev->symlink_list, pos, 0);
1443 while (isspace(next[1]))
1446 next = strchr(pos, ' ');
1448 if (pos[0] != '\0') {
1449 info("add symlink '%s'", pos);
1450 name_list_add(&udev->symlink_list, pos, 0);
1454 /* set name, later rules with name set will be ignored */
1455 if (rule->name.operation == KEY_OP_ASSIGN ||
1456 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
1457 rule->name.operation == KEY_OP_ADD) {
1461 strlcpy(udev->name, key_val(rule, &rule->name), sizeof(udev->name));
1462 udev_rules_apply_format(udev, udev->name, sizeof(udev->name));
1463 if (rule->string_escape == ESCAPE_UNSET ||
1464 rule->string_escape == ESCAPE_REPLACE) {
1465 count = replace_chars(udev->name, ALLOWED_CHARS_FILE);
1467 info("%i character(s) replaced", count);
1470 info("rule applied, '%s' becomes '%s'", udev->dev->kernel, udev->name);
1471 if (strcmp(udev->dev->subsystem, "net") != 0)
1472 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
1473 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
1476 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1477 struct name_entry *entry;
1479 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1480 udev->run_final = 1;
1481 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1482 info("reset run list");
1483 name_list_cleanup(&udev->run_list);
1485 dbg("add run '%s'", key_val(rule, &rule->run));
1486 entry = name_list_add(&udev->run_list, key_val(rule, &rule->run), 0);
1487 if (rule->run_ignore_error)
1488 entry->ignore_error = 1;
1491 if (rule->last_rule) {
1492 dbg("last rule to be applied");
1496 if (rule->goto_label.operation != KEY_OP_UNSET) {
1497 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1498 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));
1504 info("no node name set, will use kernel name '%s'", udev->dev->kernel);
1505 strlcpy(udev->name, udev->dev->kernel, sizeof(udev->name));
1508 if (udev->tmp_node[0] != '\0') {
1509 dbg("removing temporary device node");
1510 unlink_secure(udev->tmp_node);
1511 udev->tmp_node[0] = '\0';
1517 int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev)
1519 struct udev_rule *rule;
1521 dbg("udev->kernel='%s'", udev->dev->kernel);
1523 /* look for a matching rule to apply */
1524 udev_rules_iter_init(rules);
1526 rule = udev_rules_iter_next(rules);
1530 dbg("process rule");
1531 if (rule->name.operation == KEY_OP_ASSIGN ||
1532 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
1533 rule->name.operation == KEY_OP_ADD ||
1534 rule->symlink.operation == KEY_OP_ASSIGN ||
1535 rule->symlink.operation == KEY_OP_ASSIGN_FINAL ||
1536 rule->symlink.operation == KEY_OP_ADD ||
1537 rule->mode.operation != KEY_OP_UNSET ||
1538 rule->owner.operation != KEY_OP_UNSET || rule->group.operation != KEY_OP_UNSET) {
1539 dbg("skip rule that names a device");
1543 if (match_rule(udev, rule) == 0) {
1544 if (rule->ignore_device) {
1545 info("rule applied, '%s' is ignored", udev->dev->kernel);
1546 udev->ignore_device = 1;
1549 if (rule->ignore_remove) {
1550 udev->ignore_remove = 1;
1551 dbg("remove event should be ignored");
1554 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1555 struct name_entry *entry;
1557 if (rule->run.operation == KEY_OP_ASSIGN ||
1558 rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1559 info("reset run list");
1560 name_list_cleanup(&udev->run_list);
1562 dbg("add run '%s'", key_val(rule, &rule->run));
1563 entry = name_list_add(&udev->run_list, key_val(rule, &rule->run), 0);
1564 if (rule->run_ignore_error)
1565 entry->ignore_error = 1;
1566 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1570 if (rule->last_rule) {
1571 dbg("last rule to be applied");
1575 if (rule->goto_label.operation != KEY_OP_UNSET) {
1576 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1577 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));