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