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