"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 3 | * |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 4 | * mdev - Mini udev for busybox |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 5 | * |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 6 | * Copyright 2005 Rob Landley <rob@landley.net> |
| 7 | * Copyright 2005 Frank Sorenson <frank@tuxrocks.com> |
| 8 | * |
Rob Landley | e9a7a62 | 2006-09-22 02:52:41 +0000 | [diff] [blame] | 9 | * 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] | 10 | */ |
| 11 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 13 | #include "xregex.h" |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 14 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 15 | struct globals { |
Rob Landley | 5cd1ccd | 2006-05-21 18:33:27 +0000 | [diff] [blame] | 16 | int root_major, root_minor; |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 17 | }; |
| 18 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 19 | #define root_major (G.root_major) |
| 20 | #define root_minor (G.root_minor) |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 21 | |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 22 | /* Prevent infinite loops in /sys symlinks */ |
| 23 | #define MAX_SYSFS_DEPTH 3 |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 25 | /* We use additional 64+ bytes in make_device() */ |
| 26 | #define SCRATCH_SIZE 80 |
| 27 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 28 | #if ENABLE_FEATURE_MDEV_RENAME |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 29 | /* Builds an alias path. |
| 30 | * This function potentionally reallocates the alias parameter. |
| 31 | */ |
| 32 | static char *build_alias(char *alias, const char *device_name) |
| 33 | { |
| 34 | char *dest; |
| 35 | |
| 36 | /* ">bar/": rename to bar/device_name */ |
| 37 | /* ">bar[/]baz": rename to bar[/]baz */ |
| 38 | dest = strrchr(alias, '/'); |
| 39 | if (dest) { /* ">bar/[baz]" ? */ |
| 40 | *dest = '\0'; /* mkdir bar */ |
| 41 | bb_make_directory(alias, 0755, FILEUTILS_RECUR); |
| 42 | *dest = '/'; |
| 43 | if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */ |
| 44 | dest = alias; |
| 45 | alias = concat_path_file(alias, device_name); |
| 46 | free(dest); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return alias; |
| 51 | } |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 52 | #endif |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 53 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 54 | /* mknod in /dev based on a path like "/sys/block/hda/hda1" */ |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 55 | /* 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] | 56 | static void make_device(char *path, int delete) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 57 | { |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 58 | const char *device_name; |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 59 | int major, minor, type, len; |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 60 | int mode = 0660; |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 61 | #if ENABLE_FEATURE_MDEV_CONF |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 62 | struct bb_uidgid_t ugid = { 0, 0 }; |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 63 | parser_t *parser; |
| 64 | char *tokens[5]; |
| 65 | #endif |
| 66 | #if ENABLE_FEATURE_MDEV_EXEC |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 67 | char *command = NULL; |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 68 | #endif |
| 69 | #if ENABLE_FEATURE_MDEV_RENAME |
Mike Frysinger | f0044c4 | 2008-02-01 06:53:50 +0000 | [diff] [blame] | 70 | char *alias = NULL; |
Denis Vlasenko | a959a2a | 2008-05-22 21:35:16 +0000 | [diff] [blame] | 71 | char aliaslink = aliaslink; /* for compiler */ |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 72 | #endif |
| 73 | char *dev_maj_min = path + strlen(path); |
Mike Frysinger | f0044c4 | 2008-02-01 06:53:50 +0000 | [diff] [blame] | 74 | |
| 75 | /* Force the configuration file settings exactly. */ |
| 76 | umask(0); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 77 | |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 78 | /* Try to read major/minor string. Note that the kernel puts \n after |
| 79 | * 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] | 80 | * because sscanf() will stop at the first nondigit, which \n is. |
| 81 | * We also depend on path having writeable space after it. |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 82 | */ |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 83 | major = -1; |
Rob Landley | 10b36f9 | 2006-08-10 01:09:37 +0000 | [diff] [blame] | 84 | if (!delete) { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 85 | strcpy(dev_maj_min, "/dev"); |
| 86 | len = open_read_close(path, dev_maj_min + 1, 64); |
| 87 | *dev_maj_min++ = '\0'; |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 88 | if (len < 1) { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 89 | if (!ENABLE_FEATURE_MDEV_EXEC) |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 90 | return; |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 91 | /* no "dev" file, so just try to run script */ |
| 92 | *dev_maj_min = '\0'; |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 93 | } else if (sscanf(dev_maj_min, "%u:%u", &major, &minor) != 2) { |
| 94 | major = -1; |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 95 | } |
Rob Landley | 10b36f9 | 2006-08-10 01:09:37 +0000 | [diff] [blame] | 96 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 97 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 98 | /* Determine device name, type, major and minor */ |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 99 | device_name = bb_basename(path); |
Denis Vlasenko | aa8a601 | 2008-03-29 16:07:46 +0000 | [diff] [blame] | 100 | /* http://kernel.org/doc/pending/hotplug.txt says that only |
Denis Vlasenko | a87045c | 2008-07-12 10:28:41 +0000 | [diff] [blame] | 101 | * "/sys/block/..." is for block devices. "/sys/bus" etc is not. |
| 102 | * But since 2.6.25 block devices are also in /sys/class/block. |
| 103 | * We use strstr("/block/") to forestall future surprises. */ |
| 104 | type = S_IFCHR; |
| 105 | if (strstr(path, "/block/")) |
Denis Vlasenko | 4579e9c | 2008-05-08 21:49:28 +0000 | [diff] [blame] | 106 | type = S_IFBLK; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 107 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 108 | #if ENABLE_FEATURE_MDEV_CONF |
| 109 | parser = config_open2("/etc/mdev.conf", fopen_for_read); |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 110 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 111 | /* If we have config file, look up user settings */ |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 112 | while (config_read(parser, tokens, 4, 3, "# \t", PARSE_NORMAL)) { |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 113 | regmatch_t off[1 + 9*ENABLE_FEATURE_MDEV_RENAME_REGEXP]; |
| 114 | char *val; |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 115 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 116 | /* Fields: regex uid:gid mode [alias] [cmd] */ |
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 | /* 1st field: @<numeric maj,min>... */ |
| 119 | if (tokens[0][0] == '@') { |
| 120 | /* @major,minor[-last] */ |
| 121 | /* (useful when name is ambiguous: |
| 122 | * "/sys/class/usb/lp0" and |
| 123 | * "/sys/class/printer/lp0") */ |
| 124 | int cmaj, cmin0, cmin1, sc; |
| 125 | if (major < 0) |
| 126 | continue; /* no dev, no match */ |
| 127 | sc = sscanf(tokens[0], "@%u,%u-%u", &cmaj, &cmin0, &cmin1); |
| 128 | if (sc < 1 || major != cmaj |
| 129 | || (sc == 2 && minor != cmin0) |
| 130 | || (sc == 3 && (minor < cmin0 || minor > cmin1)) |
| 131 | ) { |
| 132 | continue; /* no match */ |
| 133 | } |
| 134 | } else { /* ... or regex to match device name */ |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 135 | regex_t match; |
| 136 | int result; |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 137 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 138 | /* Is this it? */ |
| 139 | xregcomp(&match, tokens[0], REG_EXTENDED); |
| 140 | result = regexec(&match, device_name, ARRAY_SIZE(off), off, 0); |
| 141 | regfree(&match); |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 142 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 143 | //bb_error_msg("matches:"); |
| 144 | //for (int i = 0; i < ARRAY_SIZE(off); i++) { |
| 145 | // if (off[i].rm_so < 0) continue; |
| 146 | // bb_error_msg("match %d: '%.*s'\n", i, |
| 147 | // (int)(off[i].rm_eo - off[i].rm_so), |
| 148 | // device_name + off[i].rm_so); |
| 149 | //} |
Denis Vlasenko | f2f3868 | 2008-03-29 17:26:10 +0000 | [diff] [blame] | 150 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 151 | /* If not this device, skip rest of line */ |
| 152 | /* (regexec returns whole pattern as "range" 0) */ |
| 153 | if (result || off[0].rm_so |
| 154 | || ((int)off[0].rm_eo != (int)strlen(device_name)) |
| 155 | ) { |
| 156 | continue; |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 157 | } |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 158 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 159 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 160 | /* This line matches: stop parsing the file |
| 161 | * after parsing the rest of fields */ |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 162 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 163 | /* 2nd field: uid:gid - device ownership */ |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 164 | parse_chown_usergroup_or_die(&ugid, tokens[1]); |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 165 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 166 | /* 3rd field: mode - device permissions */ |
| 167 | mode = strtoul(tokens[2], NULL, 8); |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 168 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 169 | val = tokens[3]; |
| 170 | /* 4th field (opt): >alias */ |
Denis Vlasenko | 32dcc53 | 2008-05-02 13:21:24 +0000 | [diff] [blame] | 171 | #if ENABLE_FEATURE_MDEV_RENAME |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 172 | if (!val) |
| 173 | break; |
| 174 | aliaslink = *val; |
| 175 | if (aliaslink == '>' || aliaslink == '=') { |
| 176 | char *s; |
Bernhard Reutner-Fischer | b62bd7b | 2008-07-22 07:33:53 +0000 | [diff] [blame] | 177 | #if ENABLE_FEATURE_MDEV_RENAME_REGEXP |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 178 | char *p; |
| 179 | unsigned i, n; |
Bernhard Reutner-Fischer | b62bd7b | 2008-07-22 07:33:53 +0000 | [diff] [blame] | 180 | #endif |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 181 | char *a = val; |
| 182 | s = strchr(val, ' '); |
| 183 | val = (s && s[1]) ? s+1 : NULL; |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 184 | #if ENABLE_FEATURE_MDEV_RENAME_REGEXP |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 185 | /* substitute %1..9 with off[1..9], if any */ |
| 186 | n = 0; |
| 187 | s = a; |
| 188 | while (*s) |
| 189 | if (*s++ == '%') |
| 190 | n++; |
Denis Vlasenko | f2f3868 | 2008-03-29 17:26:10 +0000 | [diff] [blame] | 191 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 192 | p = alias = xzalloc(strlen(a) + n * strlen(device_name)); |
| 193 | s = a + 1; |
| 194 | while (*s) { |
| 195 | *p = *s; |
| 196 | if ('%' == *s) { |
| 197 | i = (s[1] - '0'); |
| 198 | if (i <= 9 && off[i].rm_so >= 0) { |
| 199 | n = off[i].rm_eo - off[i].rm_so; |
| 200 | strncpy(p, device_name + off[i].rm_so, n); |
| 201 | p += n - 1; |
| 202 | s++; |
Denis Vlasenko | f2f3868 | 2008-03-29 17:26:10 +0000 | [diff] [blame] | 203 | } |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 204 | } |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 205 | p++; |
| 206 | s++; |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 207 | } |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 208 | #else |
| 209 | alias = xstrdup(a + 1); |
| 210 | #endif |
| 211 | } |
Denis Vlasenko | 32dcc53 | 2008-05-02 13:21:24 +0000 | [diff] [blame] | 212 | #endif /* ENABLE_FEATURE_MDEV_RENAME */ |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 213 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 214 | #if ENABLE_FEATURE_MDEV_EXEC |
| 215 | /* The rest (opt): command to run */ |
| 216 | if (!val) |
| 217 | break; |
| 218 | { |
| 219 | const char *s = "@$*"; |
| 220 | const char *s2 = strchr(s, *val); |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 221 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 222 | if (!s2) |
| 223 | bb_error_msg_and_die("bad line %u", parser->lineno); |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 224 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 225 | /* Correlate the position in the "@$*" with the delete |
| 226 | * step so that we get the proper behavior: |
| 227 | * @cmd: run on create |
| 228 | * $cmd: run on delete |
| 229 | * *cmd: run on both |
| 230 | */ |
| 231 | if ((s2 - s + 1) /*1/2/3*/ & /*1/2*/ (1 + delete)) { |
| 232 | command = xstrdup(val + 1); |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 233 | } |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 234 | } |
| 235 | #endif |
| 236 | /* end of field parsing */ |
| 237 | break; /* we found matching line, stop */ |
| 238 | } /* end of "while line is read from /etc/mdev.conf" */ |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 239 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 240 | config_close(parser); |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 241 | #endif /* ENABLE_FEATURE_MDEV_CONF */ |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 242 | |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 243 | if (!delete && major >= 0) { |
Mike Frysinger | f0044c4 | 2008-02-01 06:53:50 +0000 | [diff] [blame] | 244 | |
| 245 | if (ENABLE_FEATURE_MDEV_RENAME) |
| 246 | unlink(device_name); |
| 247 | |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 248 | if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST) |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 249 | bb_perror_msg_and_die("mknod %s", device_name); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 250 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 251 | if (major == root_major && minor == root_minor) |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 252 | symlink(device_name, "root"); |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 253 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 254 | #if ENABLE_FEATURE_MDEV_CONF |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 255 | chown(device_name, ugid.uid, ugid.gid); |
Mike Frysinger | f0044c4 | 2008-02-01 06:53:50 +0000 | [diff] [blame] | 256 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 257 | #if ENABLE_FEATURE_MDEV_RENAME |
| 258 | if (alias) { |
| 259 | alias = build_alias(alias, device_name); |
Mike Frysinger | f0044c4 | 2008-02-01 06:53:50 +0000 | [diff] [blame] | 260 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 261 | /* move the device, and optionally |
| 262 | * make a symlink to moved device node */ |
| 263 | if (rename(device_name, alias) == 0 && aliaslink == '>') |
| 264 | symlink(alias, device_name); |
Mike Frysinger | f0044c4 | 2008-02-01 06:53:50 +0000 | [diff] [blame] | 265 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 266 | free(alias); |
Mike Frysinger | f0044c4 | 2008-02-01 06:53:50 +0000 | [diff] [blame] | 267 | } |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 268 | #endif |
| 269 | #endif |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 270 | } |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 271 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 272 | #if ENABLE_FEATURE_MDEV_EXEC |
| 273 | if (command) { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 274 | /* setenv will leak memory, use putenv/unsetenv/free */ |
Denis Vlasenko | d6766c7 | 2007-06-08 16:18:15 +0000 | [diff] [blame] | 275 | char *s = xasprintf("MDEV=%s", device_name); |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 276 | putenv(s); |
Denis Vlasenko | d6766c7 | 2007-06-08 16:18:15 +0000 | [diff] [blame] | 277 | if (system(command) == -1) |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 278 | bb_perror_msg_and_die("can't run '%s'", command); |
Denis Vlasenko | d6766c7 | 2007-06-08 16:18:15 +0000 | [diff] [blame] | 279 | s[4] = '\0'; |
| 280 | unsetenv(s); |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 281 | free(s); |
| 282 | free(command); |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 283 | } |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 284 | #endif |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 285 | |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 286 | if (delete) { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 287 | unlink(device_name); |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 288 | /* At creation time, device might have been moved |
| 289 | * and a symlink might have been created. Undo that. */ |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 290 | #if ENABLE_FEATURE_MDEV_RENAME |
| 291 | if (alias) { |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 292 | alias = build_alias(alias, device_name); |
| 293 | unlink(alias); |
| 294 | free(alias); |
| 295 | } |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 296 | #endif |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 297 | } |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 300 | /* File callback for /sys/ traversal */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 301 | static int FAST_FUNC fileAction(const char *fileName, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 302 | struct stat *statbuf UNUSED_PARAM, |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 303 | void *userData, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 304 | int depth UNUSED_PARAM) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 305 | { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 306 | size_t len = strlen(fileName) - 4; /* can't underflow */ |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 307 | char *scratch = userData; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 308 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 309 | /* len check is for paranoid reasons */ |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 310 | if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX) |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 311 | return FALSE; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 312 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 313 | strcpy(scratch, fileName); |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 314 | scratch[len] = '\0'; |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 315 | make_device(scratch, 0); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 316 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 317 | return TRUE; |
| 318 | } |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 319 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 320 | /* Directory callback for /sys/ traversal */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 321 | static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM, |
| 322 | struct stat *statbuf UNUSED_PARAM, |
| 323 | void *userData UNUSED_PARAM, |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 324 | int depth) |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 325 | { |
| 326 | return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 329 | /* For the full gory details, see linux/Documentation/firmware_class/README |
| 330 | * |
| 331 | * Firmware loading works like this: |
| 332 | * - kernel sets FIRMWARE env var |
| 333 | * - userspace checks /lib/firmware/$FIRMWARE |
| 334 | * - userspace waits for /sys/$DEVPATH/loading to appear |
| 335 | * - userspace writes "1" to /sys/$DEVPATH/loading |
| 336 | * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data |
| 337 | * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading |
| 338 | * - kernel loads firmware into device |
| 339 | */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 340 | static void load_firmware(const char *const firmware, const char *const sysfs_path) |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 341 | { |
| 342 | int cnt; |
| 343 | int firmware_fd, loading_fd, data_fd; |
| 344 | |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 345 | /* check for /lib/firmware/$FIRMWARE */ |
| 346 | xchdir("/lib/firmware"); |
Mike Frysinger | 0e0639b | 2007-06-14 09:29:48 +0000 | [diff] [blame] | 347 | firmware_fd = xopen(firmware, O_RDONLY); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 348 | |
| 349 | /* in case we goto out ... */ |
| 350 | data_fd = -1; |
| 351 | |
| 352 | /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */ |
| 353 | xchdir(sysfs_path); |
| 354 | for (cnt = 0; cnt < 30; ++cnt) { |
| 355 | loading_fd = open("loading", O_WRONLY); |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 356 | if (loading_fd != -1) |
| 357 | goto loading; |
| 358 | sleep(1); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 359 | } |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 360 | goto out; |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 361 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 362 | loading: |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 363 | /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */ |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 364 | if (full_write(loading_fd, "1", 1) != 1) |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 365 | goto out; |
| 366 | |
| 367 | /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */ |
| 368 | data_fd = open("data", O_WRONLY); |
| 369 | if (data_fd == -1) |
| 370 | goto out; |
| 371 | cnt = bb_copyfd_eof(firmware_fd, data_fd); |
| 372 | |
| 373 | /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */ |
| 374 | if (cnt > 0) |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 375 | full_write(loading_fd, "0", 1); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 376 | else |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 377 | full_write(loading_fd, "-1", 2); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 378 | |
| 379 | out: |
| 380 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 381 | close(firmware_fd); |
| 382 | close(loading_fd); |
| 383 | close(data_fd); |
| 384 | } |
| 385 | } |
| 386 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 387 | int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 388 | int mdev_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 389 | { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 390 | RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 391 | |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 392 | /* We can be called as hotplug helper */ |
Denis Vlasenko | f4e6bd0 | 2008-05-31 18:27:58 +0000 | [diff] [blame] | 393 | /* Kernel cannot provide suitable stdio fds for us, do it ourself */ |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 394 | #if 1 |
| 395 | bb_sanitize_stdio(); |
| 396 | #else |
| 397 | /* Debug code */ |
Denis Vlasenko | f4e6bd0 | 2008-05-31 18:27:58 +0000 | [diff] [blame] | 398 | /* Replace LOGFILE by other file or device name if you need */ |
| 399 | #define LOGFILE "/dev/console" |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 400 | /* Just making sure fd 0 is not closed, |
| 401 | * we don't really intend to read from it */ |
| 402 | xmove_fd(xopen("/", O_RDONLY), STDIN_FILENO); |
Denis Vlasenko | f4e6bd0 | 2008-05-31 18:27:58 +0000 | [diff] [blame] | 403 | xmove_fd(xopen(LOGFILE, O_WRONLY|O_APPEND), STDOUT_FILENO); |
| 404 | xmove_fd(xopen(LOGFILE, O_WRONLY|O_APPEND), STDERR_FILENO); |
| 405 | #endif |
| 406 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 407 | xchdir("/dev"); |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 408 | |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 409 | if (argv[1] && !strcmp(argv[1], "-s")) { |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 410 | /* Scan: |
| 411 | * mdev -s |
| 412 | */ |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 413 | struct stat st; |
| 414 | |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 415 | xstat("/", &st); |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 416 | root_major = major(st.st_dev); |
| 417 | root_minor = minor(st.st_dev); |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 418 | |
Denis Vlasenko | 67075bb | 2008-07-06 17:00:49 +0000 | [diff] [blame] | 419 | /* ACTION_FOLLOWLINKS is needed since in newer kernels |
| 420 | * /sys/block/loop* (for example) are symlinks to dirs, |
| 421 | * not real directories. |
| 422 | * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs, |
| 423 | * but we can't enforce that on users) */ |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 424 | recursive_action("/sys/block", |
Denis Vlasenko | 67075bb | 2008-07-06 17:00:49 +0000 | [diff] [blame] | 425 | ACTION_RECURSE | ACTION_FOLLOWLINKS, |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 426 | fileAction, dirAction, temp, 0); |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 427 | recursive_action("/sys/class", |
Denis Vlasenko | 67075bb | 2008-07-06 17:00:49 +0000 | [diff] [blame] | 428 | ACTION_RECURSE | ACTION_FOLLOWLINKS, |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 429 | fileAction, dirAction, temp, 0); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 430 | } else { |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 431 | char *seq; |
| 432 | char *action; |
| 433 | char *env_path; |
| 434 | char seqbuf[sizeof(int)*3 + 2]; |
| 435 | int seqlen = seqlen; /* for compiler */ |
| 436 | |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 437 | /* Hotplug: |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 438 | * env ACTION=... DEVPATH=... [SEQNUM=...] mdev |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 439 | * ACTION can be "add" or "remove" |
| 440 | * DEVPATH is like "/block/sda" or "/class/input/mice" |
| 441 | */ |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 442 | action = getenv("ACTION"); |
| 443 | env_path = getenv("DEVPATH"); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 444 | if (!action || !env_path) |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 445 | bb_show_usage(); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 446 | |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 447 | seq = getenv("SEQNUM"); |
| 448 | if (seq) { |
| 449 | int timeout = 2000 / 32; |
| 450 | do { |
| 451 | seqlen = open_read_close("mdev.seq", seqbuf, sizeof(seqbuf-1)); |
| 452 | if (seqlen < 0) |
| 453 | break; |
| 454 | seqbuf[seqlen] = '\0'; |
| 455 | if (seqbuf[0] == '\n' /* seed file? */ |
| 456 | || strcmp(seq, seqbuf) == 0 /* correct idx? */ |
| 457 | ) { |
| 458 | break; |
| 459 | } |
| 460 | usleep(32*1000); |
| 461 | } while (--timeout); |
| 462 | } |
| 463 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 464 | snprintf(temp, PATH_MAX, "/sys%s", env_path); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 465 | if (!strcmp(action, "remove")) |
| 466 | make_device(temp, 1); |
| 467 | else if (!strcmp(action, "add")) { |
| 468 | make_device(temp, 0); |
| 469 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 470 | if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) { |
| 471 | char *fw = getenv("FIRMWARE"); |
| 472 | if (fw) |
| 473 | load_firmware(fw, temp); |
| 474 | } |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 475 | } |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 476 | |
| 477 | if (seq && seqlen >= 0) { |
| 478 | xopen_xwrite_close("mdev.seq", utoa(xatou(seq) + 1)); |
| 479 | } |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 482 | if (ENABLE_FEATURE_CLEAN_UP) |
| 483 | RELEASE_CONFIG_BUFFER(temp); |
| 484 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 485 | return 0; |
| 486 | } |