"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 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 24 | /* mknod in /dev based on a path like "/sys/block/hda/hda1" */ |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 25 | static void make_device(char *path, int delete) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 26 | { |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 27 | const char *device_name; |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 28 | int major, minor, type, len; |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 29 | int mode = 0660; |
| 30 | uid_t uid = 0; |
| 31 | gid_t gid = 0; |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 32 | char *temp = path + strlen(path); |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 33 | char *command = NULL; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 34 | |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 35 | /* Try to read major/minor string. Note that the kernel puts \n after |
| 36 | * the data, so we don't need to worry about null terminating the string |
| 37 | * because sscanf() will stop at the first nondigit, which \n is. We |
| 38 | * also depend on path having writeable space after it. */ |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 39 | |
Rob Landley | 10b36f9 | 2006-08-10 01:09:37 +0000 | [diff] [blame] | 40 | if (!delete) { |
| 41 | strcat(path, "/dev"); |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 42 | len = open_read_close(path, temp + 1, 64); |
Rob Landley | 10b36f9 | 2006-08-10 01:09:37 +0000 | [diff] [blame] | 43 | *temp++ = 0; |
Rob Landley | 10b36f9 | 2006-08-10 01:09:37 +0000 | [diff] [blame] | 44 | if (len < 1) return; |
| 45 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 46 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 47 | /* Determine device name, type, major and minor */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 49 | device_name = bb_basename(path); |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 50 | type = path[5]=='c' ? S_IFCHR : S_IFBLK; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 51 | |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 52 | /* If we have a config file, look up permissions for this device */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 53 | |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 54 | if (ENABLE_FEATURE_MDEV_CONF) { |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 55 | char *conf, *pos, *end; |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 56 | int line, fd; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 57 | |
| 58 | /* mmap the config file */ |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 59 | fd = open("/etc/mdev.conf", O_RDONLY); |
| 60 | if (fd < 0) |
| 61 | goto end_parse; |
| 62 | len = xlseek(fd, 0, SEEK_END); |
| 63 | conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); |
| 64 | close(fd); |
| 65 | if (!conf) |
| 66 | goto end_parse; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 67 | |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 68 | line = 0; |
| 69 | /* Loop through lines in mmaped file*/ |
| 70 | for (pos=conf; pos-conf<len;) { |
| 71 | int field; |
| 72 | char *end2; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 74 | line++; |
| 75 | /* find end of this line */ |
Denis Vlasenko | cf749bc | 2006-11-26 15:45:17 +0000 | [diff] [blame] | 76 | for (end=pos; end-conf<len && *end!='\n'; end++) |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 77 | ; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 78 | |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 79 | /* Three fields: regex, uid:gid, mode */ |
| 80 | for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC); |
| 81 | field++) |
| 82 | { |
| 83 | /* Skip whitespace */ |
| 84 | while (pos<end && isspace(*pos)) pos++; |
| 85 | if (pos==end || *pos=='#') break; |
| 86 | for (end2=pos; |
| 87 | end2<end && !isspace(*end2) && *end2!='#'; end2++) |
| 88 | ; |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 89 | |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 90 | if (field == 0) { |
| 91 | /* Regex to match this device */ |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 92 | |
Bernhard Reutner-Fischer | bb0baed | 2007-06-20 09:56:47 +0000 | [diff] [blame] | 93 | char *regex = xstrndup(pos, end2-pos); |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 94 | regex_t match; |
| 95 | regmatch_t off; |
| 96 | int result; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 97 | |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 98 | /* Is this it? */ |
| 99 | xregcomp(&match,regex, REG_EXTENDED); |
| 100 | result = regexec(&match, device_name, 1, &off, 0); |
| 101 | regfree(&match); |
Bernhard Reutner-Fischer | bb0baed | 2007-06-20 09:56:47 +0000 | [diff] [blame] | 102 | free(regex); |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 103 | |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 104 | /* If not this device, skip rest of line */ |
| 105 | if (result || off.rm_so |
| 106 | || off.rm_eo != strlen(device_name)) |
| 107 | break; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 108 | } |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 109 | if (field == 1) { |
| 110 | /* uid:gid */ |
| 111 | |
| 112 | char *s, *s2; |
| 113 | |
| 114 | /* Find : */ |
Denis Vlasenko | cf749bc | 2006-11-26 15:45:17 +0000 | [diff] [blame] | 115 | for (s=pos; s<end2 && *s!=':'; s++) |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 116 | ; |
| 117 | if (s == end2) break; |
| 118 | |
| 119 | /* Parse UID */ |
| 120 | uid = strtoul(pos, &s2, 10); |
| 121 | if (s != s2) { |
| 122 | struct passwd *pass; |
Bernhard Reutner-Fischer | bb0baed | 2007-06-20 09:56:47 +0000 | [diff] [blame] | 123 | char *_unam = xstrndup(pos, s-pos); |
| 124 | pass = getpwnam(_unam); |
| 125 | free(_unam); |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 126 | if (!pass) break; |
| 127 | uid = pass->pw_uid; |
| 128 | } |
| 129 | s++; |
| 130 | /* parse GID */ |
| 131 | gid = strtoul(s, &s2, 10); |
| 132 | if (end2 != s2) { |
| 133 | struct group *grp; |
Bernhard Reutner-Fischer | bb0baed | 2007-06-20 09:56:47 +0000 | [diff] [blame] | 134 | char *_grnam = xstrndup(s, end2-s); |
| 135 | grp = getgrnam(_grnam); |
| 136 | free(_grnam); |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 137 | if (!grp) break; |
| 138 | gid = grp->gr_gid; |
| 139 | } |
| 140 | } |
| 141 | if (field == 2) { |
| 142 | /* mode */ |
| 143 | |
| 144 | mode = strtoul(pos, &pos, 8); |
| 145 | if (pos != end2) break; |
| 146 | } |
| 147 | if (ENABLE_FEATURE_MDEV_EXEC && field == 3) { |
| 148 | // Command to run |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 149 | const char *s = "@$*"; |
| 150 | const char *s2; |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 151 | s2 = strchr(s, *pos++); |
| 152 | if (!s2) { |
| 153 | // Force error |
| 154 | field = 1; |
| 155 | break; |
| 156 | } |
| 157 | if ((s2-s+1) & (1<<delete)) |
| 158 | command = xstrndup(pos, end-pos); |
| 159 | } |
| 160 | |
| 161 | pos = end2; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 162 | } |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 163 | |
| 164 | /* Did everything parse happily? */ |
| 165 | |
| 166 | if (field > 2) break; |
| 167 | if (field) bb_error_msg_and_die("bad line %d",line); |
| 168 | |
| 169 | /* Next line */ |
| 170 | pos = ++end; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 171 | } |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 172 | munmap(conf, len); |
| 173 | end_parse: /* nothing */ ; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 174 | } |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 175 | |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 176 | umask(0); |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 177 | if (!delete) { |
Rob Landley | 10b36f9 | 2006-08-10 01:09:37 +0000 | [diff] [blame] | 178 | if (sscanf(temp, "%d:%d", &major, &minor) != 2) return; |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 179 | if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST) |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 180 | bb_perror_msg_and_die("mknod %s", device_name); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 181 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 182 | if (major == root_major && minor == root_minor) |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 183 | symlink(device_name, "root"); |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 184 | |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 185 | if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid); |
| 186 | } |
| 187 | if (command) { |
Denis Vlasenko | d6766c7 | 2007-06-08 16:18:15 +0000 | [diff] [blame] | 188 | /* setenv will leak memory, so use putenv */ |
| 189 | char *s = xasprintf("MDEV=%s", device_name); |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 190 | putenv(s); |
Denis Vlasenko | d6766c7 | 2007-06-08 16:18:15 +0000 | [diff] [blame] | 191 | if (system(command) == -1) |
| 192 | bb_perror_msg_and_die("cannot run %s", command); |
| 193 | s[4] = '\0'; |
| 194 | unsetenv(s); |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 195 | free(s); |
| 196 | free(command); |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 197 | } |
| 198 | if (delete) unlink(device_name); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 201 | /* File callback for /sys/ traversal */ |
| 202 | static int fileAction(const char *fileName, struct stat *statbuf, |
| 203 | void *userData, int depth) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 204 | { |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 205 | size_t len = strlen(fileName) - 4; |
| 206 | char *scratch = userData; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 207 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 208 | if (strcmp(fileName + len, "/dev")) |
| 209 | return FALSE; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 210 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 211 | strcpy(scratch, fileName); |
| 212 | scratch[len] = 0; |
| 213 | make_device(scratch, 0); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 214 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 215 | return TRUE; |
| 216 | } |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 217 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 218 | /* Directory callback for /sys/ traversal */ |
| 219 | static int dirAction(const char *fileName, struct stat *statbuf, |
| 220 | void *userData, int depth) |
| 221 | { |
| 222 | return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 225 | /* For the full gory details, see linux/Documentation/firmware_class/README |
| 226 | * |
| 227 | * Firmware loading works like this: |
| 228 | * - kernel sets FIRMWARE env var |
| 229 | * - userspace checks /lib/firmware/$FIRMWARE |
| 230 | * - userspace waits for /sys/$DEVPATH/loading to appear |
| 231 | * - userspace writes "1" to /sys/$DEVPATH/loading |
| 232 | * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data |
| 233 | * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading |
| 234 | * - kernel loads firmware into device |
| 235 | */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame^] | 236 | 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] | 237 | { |
| 238 | int cnt; |
| 239 | int firmware_fd, loading_fd, data_fd; |
| 240 | |
| 241 | /* check for $FIRMWARE from kernel */ |
| 242 | /* XXX: dont bother: open(NULL) works same as open("no-such-file") |
| 243 | * if (!firmware) |
| 244 | * return; |
| 245 | */ |
| 246 | |
| 247 | /* check for /lib/firmware/$FIRMWARE */ |
| 248 | xchdir("/lib/firmware"); |
Mike Frysinger | 0e0639b | 2007-06-14 09:29:48 +0000 | [diff] [blame] | 249 | firmware_fd = xopen(firmware, O_RDONLY); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 250 | |
| 251 | /* in case we goto out ... */ |
| 252 | data_fd = -1; |
| 253 | |
| 254 | /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */ |
| 255 | xchdir(sysfs_path); |
| 256 | for (cnt = 0; cnt < 30; ++cnt) { |
| 257 | loading_fd = open("loading", O_WRONLY); |
| 258 | if (loading_fd == -1) |
| 259 | sleep(1); |
| 260 | else |
| 261 | break; |
| 262 | } |
| 263 | if (loading_fd == -1) |
| 264 | goto out; |
| 265 | |
| 266 | /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */ |
| 267 | if (write(loading_fd, "1", 1) != 1) |
| 268 | goto out; |
| 269 | |
| 270 | /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */ |
| 271 | data_fd = open("data", O_WRONLY); |
| 272 | if (data_fd == -1) |
| 273 | goto out; |
| 274 | cnt = bb_copyfd_eof(firmware_fd, data_fd); |
| 275 | |
| 276 | /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */ |
| 277 | if (cnt > 0) |
| 278 | write(loading_fd, "0", 1); |
| 279 | else |
| 280 | write(loading_fd, "-1", 2); |
| 281 | |
| 282 | out: |
| 283 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 284 | close(firmware_fd); |
| 285 | close(loading_fd); |
| 286 | close(data_fd); |
| 287 | } |
| 288 | } |
| 289 | |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 290 | int mdev_main(int argc, char **argv); |
| 291 | int mdev_main(int argc, char **argv) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 292 | { |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 293 | char *action; |
| 294 | char *env_path; |
| 295 | RESERVE_CONFIG_BUFFER(temp,PATH_MAX); |
| 296 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 297 | xchdir("/dev"); |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 298 | |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 299 | /* Scan */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 300 | |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 301 | if (argc == 2 && !strcmp(argv[1],"-s")) { |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 302 | struct stat st; |
| 303 | |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 304 | xstat("/", &st); |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 305 | root_major = major(st.st_dev); |
| 306 | root_minor = minor(st.st_dev); |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 307 | |
| 308 | recursive_action("/sys/block", |
| 309 | ACTION_RECURSE | ACTION_FOLLOWLINKS, |
| 310 | fileAction, dirAction, temp, 0); |
| 311 | |
| 312 | recursive_action("/sys/class", |
| 313 | ACTION_RECURSE | ACTION_FOLLOWLINKS, |
| 314 | fileAction, dirAction, temp, 0); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 315 | |
| 316 | /* Hotplug */ |
| 317 | |
| 318 | } else { |
| 319 | action = getenv("ACTION"); |
| 320 | env_path = getenv("DEVPATH"); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 321 | if (!action || !env_path) |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 322 | bb_show_usage(); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 323 | |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 324 | sprintf(temp, "/sys%s", env_path); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 325 | if (!strcmp(action, "remove")) |
| 326 | make_device(temp, 1); |
| 327 | else if (!strcmp(action, "add")) { |
| 328 | make_device(temp, 0); |
| 329 | |
| 330 | if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) |
| 331 | load_firmware(getenv("FIRMWARE"), temp); |
| 332 | } |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 335 | if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 336 | return 0; |
| 337 | } |