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