"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 3 | * mdev - Mini udev for busybox |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 4 | * |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 5 | * Copyright 2005 Rob Landley <rob@landley.net> |
| 6 | * Copyright 2005 Frank Sorenson <frank@tuxrocks.com> |
| 7 | * |
Rob Landley | e9a7a62 | 2006-09-22 02:52:41 +0000 | [diff] [blame] | 8 | * Licensed under GPL version 2, see file LICENSE in this tarball for details. |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 9 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 11 | #include "xregex.h" |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 12 | |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 13 | /* "mdev -s" scans /sys/class/xxx, looking for directories which have dev |
| 14 | * file (it is of the form "M:m\n"). Example: /sys/class/tty/tty0/dev |
| 15 | * contains "4:0\n". Directory name is taken as device name, path component |
| 16 | * directly after /sys/class/ as subsystem. In this example, "tty0" and "tty". |
| 17 | * Then mdev creates the /dev/device_name node. |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 18 | * If /sys/class/.../dev file does not exist, mdev still may act |
| 19 | * on this device: see "@|$|*command args..." parameter in config file. |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 20 | * |
| 21 | * mdev w/o parameters is called as hotplug helper. It takes device |
| 22 | * and subsystem names from $DEVPATH and $SUBSYSTEM, extracts |
| 23 | * maj,min from "/sys/$DEVPATH/dev" and also examines |
| 24 | * $ACTION ("add"/"delete") and $FIRMWARE. |
| 25 | * |
| 26 | * If action is "add", mdev creates /dev/device_name similarly to mdev -s. |
| 27 | * (todo: explain "delete" and $FIRMWARE) |
| 28 | * |
| 29 | * If /etc/mdev.conf exists, it may modify /dev/device_name's properties. |
| 30 | * /etc/mdev.conf file format: |
| 31 | * |
| 32 | * [-][subsystem/]device user:grp mode [>|=path] [@|$|*command args...] |
| 33 | * [-]@maj,min[-min2] user:grp mode [>|=path] [@|$|*command args...] |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 34 | * [-]$envvar=val user:grp mode [>|=path] [@|$|*command args...] |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 35 | * |
| 36 | * Leading minus in 1st field means "don't stop on this line", otherwise |
| 37 | * search is stopped after the matching line is encountered. |
| 38 | * |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 39 | * The device name or "subsystem/device" combo is matched against 1st field |
| 40 | * (which is a regex), or maj,min is matched against 1st field, |
| 41 | * or specified environment variable (as regex) is matched against 1st field. |
| 42 | * |
| 43 | * $envvar=val format is useful for loading modules for hot-plugged devices |
| 44 | * which do not have driver loaded yet. In this case /sys/class/.../dev |
| 45 | * does not exist, but $MODALIAS is set to needed module's name |
| 46 | * (actually, an alias to it) by kernel. This rule instructs mdev |
| 47 | * to load the module and exit: |
| 48 | * $MODALIAS=.* 0:0 660 @modprobe "$MODALIAS" |
| 49 | * The kernel will generate another hotplug event when /sys/class/.../dev |
| 50 | * file appears. |
| 51 | * |
| 52 | * When line matches, the device node is created, chmod'ed and chown'ed, |
| 53 | * moved to path, and if >path, a symlink to moved node is created, |
| 54 | * all this if /sys/class/.../dev exists. |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 55 | * Examples: |
| 56 | * =loop/ - moves to /dev/loop |
| 57 | * >disk/sda%1 - moves to /dev/disk/sdaN, makes /dev/sdaN a symlink |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 58 | * |
| 59 | * Then "command args..." is executed (via sh -c 'command args...'). |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 60 | * @:execute on creation, $:on deletion, *:on both. |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 61 | * This happens regardless of /sys/class/.../dev existence. |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 62 | */ |
| 63 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 64 | struct globals { |
Rob Landley | 5cd1ccd | 2006-05-21 18:33:27 +0000 | [diff] [blame] | 65 | int root_major, root_minor; |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 66 | char *subsystem; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 67 | } FIX_ALIASING; |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 68 | #define G (*(struct globals*)&bb_common_bufsiz1) |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 70 | /* Prevent infinite loops in /sys symlinks */ |
| 71 | #define MAX_SYSFS_DEPTH 3 |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 72 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 73 | /* We use additional 64+ bytes in make_device() */ |
| 74 | #define SCRATCH_SIZE 80 |
| 75 | |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 76 | /* Builds an alias path. |
| 77 | * This function potentionally reallocates the alias parameter. |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 78 | * Only used for ENABLE_FEATURE_MDEV_RENAME |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 79 | */ |
| 80 | static char *build_alias(char *alias, const char *device_name) |
| 81 | { |
| 82 | char *dest; |
| 83 | |
| 84 | /* ">bar/": rename to bar/device_name */ |
| 85 | /* ">bar[/]baz": rename to bar[/]baz */ |
| 86 | dest = strrchr(alias, '/'); |
| 87 | if (dest) { /* ">bar/[baz]" ? */ |
| 88 | *dest = '\0'; /* mkdir bar */ |
| 89 | bb_make_directory(alias, 0755, FILEUTILS_RECUR); |
| 90 | *dest = '/'; |
| 91 | if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */ |
| 92 | dest = alias; |
| 93 | alias = concat_path_file(alias, device_name); |
| 94 | free(dest); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return alias; |
| 99 | } |
| 100 | |
Denys Vlasenko | 76f5e38 | 2009-05-04 21:59:05 +0200 | [diff] [blame] | 101 | /* mknod in /dev based on a path like "/sys/block/hda/hda1" |
| 102 | * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes |
| 103 | * after NUL, but we promise to not mangle (IOW: to restore if needed) |
| 104 | * path string. |
| 105 | * NB2: "mdev -s" may call us many times, do not leak memory/fds! |
| 106 | */ |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 107 | static void make_device(char *path, int delete) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 108 | { |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame^] | 109 | char *device_name, *subsystem_slash_devname; |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 110 | int major, minor, type, len; |
Bernhard Reutner-Fischer | 933993a | 2009-10-26 23:27:05 +0100 | [diff] [blame] | 111 | mode_t mode; |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 112 | parser_t *parser; |
Mike Frysinger | f0044c4 | 2008-02-01 06:53:50 +0000 | [diff] [blame] | 113 | |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 114 | /* Try to read major/minor string. Note that the kernel puts \n after |
| 115 | * the data, so we don't need to worry about null terminating the string |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 116 | * because sscanf() will stop at the first nondigit, which \n is. |
| 117 | * We also depend on path having writeable space after it. |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 118 | */ |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 119 | major = -1; |
Rob Landley | 10b36f9 | 2006-08-10 01:09:37 +0000 | [diff] [blame] | 120 | if (!delete) { |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 121 | char *dev_maj_min = path + strlen(path); |
| 122 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 123 | strcpy(dev_maj_min, "/dev"); |
| 124 | len = open_read_close(path, dev_maj_min + 1, 64); |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 125 | *dev_maj_min = '\0'; |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 126 | if (len < 1) { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 127 | if (!ENABLE_FEATURE_MDEV_EXEC) |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 128 | return; |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 129 | /* no "dev" file, but we can still run scripts |
| 130 | * based on device name */ |
| 131 | } else if (sscanf(++dev_maj_min, "%u:%u", &major, &minor) != 2) { |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 132 | major = -1; |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 133 | } |
Rob Landley | 10b36f9 | 2006-08-10 01:09:37 +0000 | [diff] [blame] | 134 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 135 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 136 | /* Determine device name, type, major and minor */ |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 137 | device_name = (char*) bb_basename(path); |
Denis Vlasenko | aa8a601 | 2008-03-29 16:07:46 +0000 | [diff] [blame] | 138 | /* http://kernel.org/doc/pending/hotplug.txt says that only |
Denis Vlasenko | a87045c | 2008-07-12 10:28:41 +0000 | [diff] [blame] | 139 | * "/sys/block/..." is for block devices. "/sys/bus" etc is not. |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame^] | 140 | * But since 2.6.25 block devices are also in /sys/class/block. |
| 141 | * We use strstr("/block/") to forestall future surprises. */ |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 142 | type = S_IFCHR; |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame^] | 143 | if (strstr(path, "/block/") || strncmp(G.subsystem, "block", 5) == 0) |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 144 | type = S_IFBLK; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 145 | |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 146 | /* Make path point to "subsystem/device_name" */ |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame^] | 147 | subsystem_slash_devname = NULL; |
| 148 | /* Check for coldplug invocations first */ |
| 149 | if (strncmp(path, "/sys/block/", 11) == 0) /* legacy case */ |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 150 | path += sizeof("/sys/") - 1; |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame^] | 151 | else if (strncmp(path, "/sys/class/", 11) == 0) |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 152 | path += sizeof("/sys/class/") - 1; |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame^] | 153 | else { |
| 154 | /* Example of a hotplug invocation: |
| 155 | * SUBSYSTEM="block" |
| 156 | * DEVPATH="/sys" + "/devices/virtual/mtd/mtd3/mtdblock3" |
| 157 | * ("/sys" is added by mdev_main) |
| 158 | * - path does not contain subsystem |
| 159 | */ |
| 160 | subsystem_slash_devname = concat_path_file(G.subsystem, device_name); |
| 161 | path = subsystem_slash_devname; |
| 162 | } |
Denis Vlasenko | 6dda5de | 2009-04-16 21:42:12 +0000 | [diff] [blame] | 163 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 164 | /* If we have config file, look up user settings */ |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 165 | if (ENABLE_FEATURE_MDEV_CONF) |
| 166 | parser = config_open2("/etc/mdev.conf", fopen_for_read); |
| 167 | |
| 168 | do { |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 169 | int keep_matching; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 170 | struct bb_uidgid_t ugid; |
| 171 | char *tokens[4]; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 172 | char *command = NULL; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 173 | char *alias = NULL; |
| 174 | char aliaslink = aliaslink; /* for compiler */ |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 175 | |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 176 | /* Defaults in case we won't match any line */ |
| 177 | ugid.uid = ugid.gid = 0; |
| 178 | keep_matching = 0; |
| 179 | mode = 0660; |
| 180 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 181 | if (ENABLE_FEATURE_MDEV_CONF |
| 182 | && config_read(parser, tokens, 4, 3, "# \t", PARSE_NORMAL) |
| 183 | ) { |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 184 | char *val; |
| 185 | char *str_to_match; |
| 186 | regmatch_t off[1 + 9 * ENABLE_FEATURE_MDEV_RENAME_REGEXP]; |
| 187 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 188 | val = tokens[0]; |
| 189 | keep_matching = ('-' == val[0]); |
| 190 | val += keep_matching; /* swallow leading dash */ |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 191 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 192 | /* Match against either "subsystem/device_name" |
| 193 | * or "device_name" alone */ |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 194 | str_to_match = strchr(val, '/') ? path : device_name; |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 195 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 196 | /* Fields: regex uid:gid mode [alias] [cmd] */ |
Denis Vlasenko | 6dda5de | 2009-04-16 21:42:12 +0000 | [diff] [blame] | 197 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 198 | if (val[0] == '@') { |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 199 | /* @major,minor[-minor2] */ |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 200 | /* (useful when name is ambiguous: |
| 201 | * "/sys/class/usb/lp0" and |
| 202 | * "/sys/class/printer/lp0") */ |
| 203 | int cmaj, cmin0, cmin1, sc; |
| 204 | if (major < 0) |
| 205 | continue; /* no dev, no match */ |
| 206 | sc = sscanf(val, "@%u,%u-%u", &cmaj, &cmin0, &cmin1); |
| 207 | if (sc < 1 || major != cmaj |
| 208 | || (sc == 2 && minor != cmin0) |
| 209 | || (sc == 3 && (minor < cmin0 || minor > cmin1)) |
| 210 | ) { |
| 211 | continue; /* this line doesn't match */ |
| 212 | } |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 213 | goto line_matches; |
| 214 | } |
| 215 | if (val[0] == '$') { |
| 216 | /* regex to match an environment variable */ |
| 217 | char *eq = strchr(++val, '='); |
| 218 | if (!eq) |
| 219 | continue; |
| 220 | *eq = '\0'; |
| 221 | str_to_match = getenv(val); |
| 222 | if (!str_to_match) |
| 223 | continue; |
| 224 | str_to_match -= strlen(val) + 1; |
| 225 | *eq = '='; |
| 226 | } |
| 227 | /* else: regex to match [subsystem/]device_name */ |
| 228 | |
| 229 | { |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 230 | regex_t match; |
| 231 | int result; |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 232 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 233 | xregcomp(&match, val, REG_EXTENDED); |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 234 | result = regexec(&match, str_to_match, ARRAY_SIZE(off), off, 0); |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 235 | regfree(&match); |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 236 | //bb_error_msg("matches:"); |
| 237 | //for (int i = 0; i < ARRAY_SIZE(off); i++) { |
| 238 | // if (off[i].rm_so < 0) continue; |
| 239 | // bb_error_msg("match %d: '%.*s'\n", i, |
| 240 | // (int)(off[i].rm_eo - off[i].rm_so), |
| 241 | // device_name + off[i].rm_so); |
| 242 | //} |
| 243 | |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 244 | /* If no match, skip rest of line */ |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 245 | /* (regexec returns whole pattern as "range" 0) */ |
| 246 | if (result || off[0].rm_so |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 247 | || ((int)off[0].rm_eo != (int)strlen(str_to_match)) |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 248 | ) { |
| 249 | continue; /* this line doesn't match */ |
| 250 | } |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 251 | } |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 252 | line_matches: |
| 253 | /* This line matches. Stop parsing after parsing |
| 254 | * the rest the line unless keep_matching == 1 */ |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 255 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 256 | /* 2nd field: uid:gid - device ownership */ |
Denys Vlasenko | aa0a12d | 2009-05-02 16:33:10 +0200 | [diff] [blame] | 257 | if (get_uidgid(&ugid, tokens[1], 1) == 0) |
Denys Vlasenko | 1612c75 | 2009-11-07 03:33:05 +0100 | [diff] [blame] | 258 | bb_error_msg("unknown user/group %s on line %d", tokens[1], parser->lineno); |
Denis Vlasenko | f2f3868 | 2008-03-29 17:26:10 +0000 | [diff] [blame] | 259 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 260 | /* 3rd field: mode - device permissions */ |
Bernhard Reutner-Fischer | 933993a | 2009-10-26 23:27:05 +0100 | [diff] [blame] | 261 | /* mode = strtoul(tokens[2], NULL, 8); */ |
| 262 | bb_parse_mode(tokens[2], &mode); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 263 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 264 | val = tokens[3]; |
| 265 | /* 4th field (opt): >|=alias */ |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 266 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 267 | if (ENABLE_FEATURE_MDEV_RENAME && val) { |
| 268 | aliaslink = val[0]; |
| 269 | if (aliaslink == '>' || aliaslink == '=') { |
| 270 | char *a, *s, *st; |
| 271 | char *p; |
| 272 | unsigned i, n; |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 273 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 274 | a = val; |
| 275 | s = strchrnul(val, ' '); |
| 276 | st = strchrnul(val, '\t'); |
| 277 | if (st < s) |
| 278 | s = st; |
| 279 | val = (s[0] && s[1]) ? s+1 : NULL; |
| 280 | s[0] = '\0'; |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 281 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 282 | if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) { |
| 283 | /* substitute %1..9 with off[1..9], if any */ |
| 284 | n = 0; |
| 285 | s = a; |
| 286 | while (*s) |
| 287 | if (*s++ == '%') |
| 288 | n++; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 289 | |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 290 | p = alias = xzalloc(strlen(a) + n * strlen(str_to_match)); |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 291 | s = a + 1; |
| 292 | while (*s) { |
| 293 | *p = *s; |
| 294 | if ('%' == *s) { |
| 295 | i = (s[1] - '0'); |
| 296 | if (i <= 9 && off[i].rm_so >= 0) { |
| 297 | n = off[i].rm_eo - off[i].rm_so; |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 298 | strncpy(p, str_to_match + off[i].rm_so, n); |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 299 | p += n - 1; |
| 300 | s++; |
| 301 | } |
| 302 | } |
| 303 | p++; |
| 304 | s++; |
| 305 | } |
| 306 | } else { |
| 307 | alias = xstrdup(a + 1); |
Denis Vlasenko | f2f3868 | 2008-03-29 17:26:10 +0000 | [diff] [blame] | 308 | } |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 312 | if (ENABLE_FEATURE_MDEV_EXEC && val) { |
| 313 | const char *s = "$@*"; |
| 314 | const char *s2 = strchr(s, val[0]); |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 315 | |
Denys Vlasenko | 5478aaf | 2009-07-02 13:17:10 +0200 | [diff] [blame] | 316 | if (!s2) { |
| 317 | bb_error_msg("bad line %u", parser->lineno); |
| 318 | if (ENABLE_FEATURE_MDEV_RENAME) |
| 319 | free(alias); |
| 320 | continue; |
| 321 | } |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 322 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 323 | /* Are we running this command now? |
| 324 | * Run $cmd on delete, @cmd on create, *cmd on both |
| 325 | */ |
| 326 | if (s2-s != delete) |
| 327 | command = xstrdup(val + 1); |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 328 | } |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 329 | } |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 330 | |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 331 | /* End of field parsing */ |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 332 | |
| 333 | /* "Execute" the line we found */ |
Denys Vlasenko | 90db359 | 2009-07-02 13:07:11 +0200 | [diff] [blame] | 334 | { |
| 335 | const char *node_name; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 336 | |
Denys Vlasenko | 90db359 | 2009-07-02 13:07:11 +0200 | [diff] [blame] | 337 | node_name = device_name; |
Denys Vlasenko | 2f86ca1 | 2009-06-22 01:31:12 +0200 | [diff] [blame] | 338 | if (ENABLE_FEATURE_MDEV_RENAME && alias) |
Denys Vlasenko | 90db359 | 2009-07-02 13:07:11 +0200 | [diff] [blame] | 339 | node_name = alias = build_alias(alias, device_name); |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 340 | |
Denys Vlasenko | 90db359 | 2009-07-02 13:07:11 +0200 | [diff] [blame] | 341 | if (!delete && major >= 0) { |
| 342 | if (mknod(node_name, mode | type, makedev(major, minor)) && errno != EEXIST) |
Denys Vlasenko | e62a246 | 2009-11-01 21:51:31 +0100 | [diff] [blame] | 343 | bb_perror_msg("can't create %s", node_name); |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame^] | 344 | if (major == G.root_major && minor == G.root_minor) |
Denys Vlasenko | 90db359 | 2009-07-02 13:07:11 +0200 | [diff] [blame] | 345 | symlink(node_name, "root"); |
| 346 | if (ENABLE_FEATURE_MDEV_CONF) { |
| 347 | chmod(node_name, mode); |
| 348 | chown(node_name, ugid.uid, ugid.gid); |
| 349 | } |
| 350 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { |
| 351 | if (aliaslink == '>') |
| 352 | symlink(node_name, device_name); |
| 353 | } |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 354 | } |
Denys Vlasenko | 90db359 | 2009-07-02 13:07:11 +0200 | [diff] [blame] | 355 | |
| 356 | if (ENABLE_FEATURE_MDEV_EXEC && command) { |
| 357 | /* setenv will leak memory, use putenv/unsetenv/free */ |
| 358 | char *s = xasprintf("%s=%s", "MDEV", node_name); |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame^] | 359 | char *s1 = xasprintf("%s=%s", "SUBSYSTEM", G.subsystem); |
Denys Vlasenko | 90db359 | 2009-07-02 13:07:11 +0200 | [diff] [blame] | 360 | putenv(s); |
| 361 | putenv(s1); |
| 362 | if (system(command) == -1) |
Denys Vlasenko | 5478aaf | 2009-07-02 13:17:10 +0200 | [diff] [blame] | 363 | bb_perror_msg("can't run '%s'", command); |
Denys Vlasenko | 90db359 | 2009-07-02 13:07:11 +0200 | [diff] [blame] | 364 | unsetenv("SUBSYSTEM"); |
| 365 | free(s1); |
| 366 | unsetenv("MDEV"); |
| 367 | free(s); |
| 368 | free(command); |
| 369 | } |
| 370 | |
| 371 | if (delete) { |
| 372 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { |
| 373 | if (aliaslink == '>') |
| 374 | unlink(device_name); |
| 375 | } |
| 376 | unlink(node_name); |
| 377 | } |
| 378 | |
Denys Vlasenko | 2f86ca1 | 2009-06-22 01:31:12 +0200 | [diff] [blame] | 379 | if (ENABLE_FEATURE_MDEV_RENAME) |
| 380 | free(alias); |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 383 | /* We found matching line. |
| 384 | * Stop unless it was prefixed with '-' */ |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 385 | if (ENABLE_FEATURE_MDEV_CONF && !keep_matching) |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 386 | break; |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 387 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 388 | /* end of "while line is read from /etc/mdev.conf" */ |
| 389 | } while (ENABLE_FEATURE_MDEV_CONF); |
| 390 | |
| 391 | if (ENABLE_FEATURE_MDEV_CONF) |
| 392 | config_close(parser); |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame^] | 393 | free(subsystem_slash_devname); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 396 | /* File callback for /sys/ traversal */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 397 | static int FAST_FUNC fileAction(const char *fileName, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 398 | struct stat *statbuf UNUSED_PARAM, |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 399 | void *userData, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 400 | int depth UNUSED_PARAM) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 401 | { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 402 | size_t len = strlen(fileName) - 4; /* can't underflow */ |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 403 | char *scratch = userData; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 404 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 405 | /* len check is for paranoid reasons */ |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 406 | if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX) |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 407 | return FALSE; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 408 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 409 | strcpy(scratch, fileName); |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 410 | scratch[len] = '\0'; |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame^] | 411 | make_device(scratch, /*delete:*/ 0); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 412 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 413 | return TRUE; |
| 414 | } |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 415 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 416 | /* Directory callback for /sys/ traversal */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 417 | static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM, |
| 418 | struct stat *statbuf UNUSED_PARAM, |
| 419 | void *userData UNUSED_PARAM, |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 420 | int depth) |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 421 | { |
Denis Vlasenko | 32a3d08 | 2009-04-14 21:23:33 +0000 | [diff] [blame] | 422 | /* Extract device subsystem -- the name of the directory |
| 423 | * under /sys/class/ */ |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 424 | if (1 == depth) { |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame^] | 425 | free(G.subsystem); |
| 426 | G.subsystem = strrchr(fileName, '/'); |
| 427 | if (G.subsystem) |
| 428 | G.subsystem = xstrdup(G.subsystem + 1); |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 431 | return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 434 | /* For the full gory details, see linux/Documentation/firmware_class/README |
| 435 | * |
| 436 | * Firmware loading works like this: |
| 437 | * - kernel sets FIRMWARE env var |
| 438 | * - userspace checks /lib/firmware/$FIRMWARE |
| 439 | * - userspace waits for /sys/$DEVPATH/loading to appear |
| 440 | * - userspace writes "1" to /sys/$DEVPATH/loading |
| 441 | * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data |
| 442 | * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading |
| 443 | * - kernel loads firmware into device |
| 444 | */ |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 445 | static void load_firmware(const char *firmware, const char *sysfs_path) |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 446 | { |
| 447 | int cnt; |
| 448 | int firmware_fd, loading_fd, data_fd; |
| 449 | |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 450 | /* check for /lib/firmware/$FIRMWARE */ |
| 451 | xchdir("/lib/firmware"); |
Mike Frysinger | 0e0639b | 2007-06-14 09:29:48 +0000 | [diff] [blame] | 452 | firmware_fd = xopen(firmware, O_RDONLY); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 453 | |
| 454 | /* in case we goto out ... */ |
| 455 | data_fd = -1; |
| 456 | |
| 457 | /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */ |
| 458 | xchdir(sysfs_path); |
| 459 | for (cnt = 0; cnt < 30; ++cnt) { |
| 460 | loading_fd = open("loading", O_WRONLY); |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 461 | if (loading_fd != -1) |
| 462 | goto loading; |
| 463 | sleep(1); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 464 | } |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 465 | goto out; |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 466 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 467 | loading: |
Denis Vlasenko | 32a3d08 | 2009-04-14 21:23:33 +0000 | [diff] [blame] | 468 | /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */ |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 469 | if (full_write(loading_fd, "1", 1) != 1) |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 470 | goto out; |
| 471 | |
Denis Vlasenko | 32a3d08 | 2009-04-14 21:23:33 +0000 | [diff] [blame] | 472 | /* load firmware into /sys/$DEVPATH/data */ |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 473 | data_fd = open("data", O_WRONLY); |
| 474 | if (data_fd == -1) |
| 475 | goto out; |
| 476 | cnt = bb_copyfd_eof(firmware_fd, data_fd); |
| 477 | |
Denis Vlasenko | 32a3d08 | 2009-04-14 21:23:33 +0000 | [diff] [blame] | 478 | /* tell kernel result by "echo [0|-1] > /sys/$DEVPATH/loading" */ |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 479 | if (cnt > 0) |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 480 | full_write(loading_fd, "0", 1); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 481 | else |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 482 | full_write(loading_fd, "-1", 2); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 483 | |
| 484 | out: |
| 485 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 486 | close(firmware_fd); |
| 487 | close(loading_fd); |
| 488 | close(data_fd); |
| 489 | } |
| 490 | } |
| 491 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 492 | int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 493 | int mdev_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 494 | { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 495 | RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 496 | |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 497 | /* We can be called as hotplug helper */ |
Denis Vlasenko | f4e6bd0 | 2008-05-31 18:27:58 +0000 | [diff] [blame] | 498 | /* Kernel cannot provide suitable stdio fds for us, do it ourself */ |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 499 | bb_sanitize_stdio(); |
Denis Vlasenko | f4e6bd0 | 2008-05-31 18:27:58 +0000 | [diff] [blame] | 500 | |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 501 | /* Force the configuration file settings exactly */ |
| 502 | umask(0); |
| 503 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 504 | xchdir("/dev"); |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 505 | |
Denis Vlasenko | aafbae6 | 2009-04-13 13:33:02 +0000 | [diff] [blame] | 506 | if (argv[1] && strcmp(argv[1], "-s") == 0) { |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 507 | /* Scan: |
| 508 | * mdev -s |
| 509 | */ |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 510 | struct stat st; |
| 511 | |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 512 | xstat("/", &st); |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame^] | 513 | G.root_major = major(st.st_dev); |
| 514 | G.root_minor = minor(st.st_dev); |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 515 | |
Denis Vlasenko | 67075bb | 2008-07-06 17:00:49 +0000 | [diff] [blame] | 516 | /* ACTION_FOLLOWLINKS is needed since in newer kernels |
| 517 | * /sys/block/loop* (for example) are symlinks to dirs, |
| 518 | * not real directories. |
| 519 | * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs, |
Denis Vlasenko | d63cd1b | 2009-02-13 00:02:54 +0000 | [diff] [blame] | 520 | * but we can't enforce that on users) |
| 521 | */ |
| 522 | if (access("/sys/class/block", F_OK) != 0) { |
| 523 | /* Scan obsolete /sys/block only if /sys/class/block |
| 524 | * doesn't exist. Otherwise we'll have dupes. |
Denis Vlasenko | f1df35c | 2009-04-12 14:00:12 +0000 | [diff] [blame] | 525 | * Also, do not complain if it doesn't exist. |
| 526 | * Some people configure kernel to have no blockdevs. |
Denis Vlasenko | d63cd1b | 2009-02-13 00:02:54 +0000 | [diff] [blame] | 527 | */ |
| 528 | recursive_action("/sys/block", |
Denis Vlasenko | f1df35c | 2009-04-12 14:00:12 +0000 | [diff] [blame] | 529 | ACTION_RECURSE | ACTION_FOLLOWLINKS | ACTION_QUIET, |
Denis Vlasenko | d63cd1b | 2009-02-13 00:02:54 +0000 | [diff] [blame] | 530 | fileAction, dirAction, temp, 0); |
| 531 | } |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 532 | recursive_action("/sys/class", |
Denis Vlasenko | 67075bb | 2008-07-06 17:00:49 +0000 | [diff] [blame] | 533 | ACTION_RECURSE | ACTION_FOLLOWLINKS, |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 534 | fileAction, dirAction, temp, 0); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 535 | } else { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 536 | char *fw; |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 537 | char *seq; |
| 538 | char *action; |
| 539 | char *env_path; |
Bernhard Reutner-Fischer | 6531f09 | 2009-10-26 23:27:04 +0100 | [diff] [blame] | 540 | static const char keywords[] ALIGN1 = "remove\0add\0"; |
| 541 | enum { OP_remove = 0, OP_add }; |
| 542 | smalluint op; |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 543 | |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 544 | /* Hotplug: |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 545 | * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 546 | * ACTION can be "add" or "remove" |
| 547 | * DEVPATH is like "/block/sda" or "/class/input/mice" |
| 548 | */ |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 549 | action = getenv("ACTION"); |
| 550 | env_path = getenv("DEVPATH"); |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame^] | 551 | G.subsystem = getenv("SUBSYSTEM"); |
| 552 | if (!action || !env_path /*|| !G.subsystem*/) |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 553 | bb_show_usage(); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 554 | fw = getenv("FIRMWARE"); |
Bernhard Reutner-Fischer | 6531f09 | 2009-10-26 23:27:04 +0100 | [diff] [blame] | 555 | op = index_in_strings(keywords, action); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 556 | /* If it exists, does /dev/mdev.seq match $SEQNUM? |
| 557 | * If it does not match, earlier mdev is running |
| 558 | * in parallel, and we need to wait */ |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 559 | seq = getenv("SEQNUM"); |
| 560 | if (seq) { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 561 | int timeout = 2000 / 32; /* 2000 msec */ |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 562 | do { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 563 | int seqlen; |
| 564 | char seqbuf[sizeof(int)*3 + 2]; |
| 565 | |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 566 | seqlen = open_read_close("mdev.seq", seqbuf, sizeof(seqbuf-1)); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 567 | if (seqlen < 0) { |
| 568 | seq = NULL; |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 569 | break; |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 570 | } |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 571 | seqbuf[seqlen] = '\0'; |
| 572 | if (seqbuf[0] == '\n' /* seed file? */ |
| 573 | || strcmp(seq, seqbuf) == 0 /* correct idx? */ |
| 574 | ) { |
| 575 | break; |
| 576 | } |
| 577 | usleep(32*1000); |
| 578 | } while (--timeout); |
| 579 | } |
| 580 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 581 | snprintf(temp, PATH_MAX, "/sys%s", env_path); |
Bernhard Reutner-Fischer | 6531f09 | 2009-10-26 23:27:04 +0100 | [diff] [blame] | 582 | if (op == OP_remove) { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 583 | /* Ignoring "remove firmware". It was reported |
| 584 | * to happen and to cause erroneous deletion |
| 585 | * of device nodes. */ |
| 586 | if (!fw) |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame^] | 587 | make_device(temp, /*delete:*/ 1); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 588 | } |
Bernhard Reutner-Fischer | 6531f09 | 2009-10-26 23:27:04 +0100 | [diff] [blame] | 589 | else if (op == OP_add) { |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame^] | 590 | make_device(temp, /*delete:*/ 0); |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 591 | if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 592 | if (fw) |
| 593 | load_firmware(fw, temp); |
| 594 | } |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 595 | } |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 596 | |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 597 | if (seq) { |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 598 | xopen_xwrite_close("mdev.seq", utoa(xatou(seq) + 1)); |
| 599 | } |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 602 | if (ENABLE_FEATURE_CLEAN_UP) |
| 603 | RELEASE_CONFIG_BUFFER(temp); |
| 604 | |
Denis Vlasenko | aafbae6 | 2009-04-13 13:33:02 +0000 | [diff] [blame] | 605 | return EXIT_SUCCESS; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 606 | } |