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