"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 3 | * mdev - Mini udev for busybox |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 4 | * |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 5 | * Copyright 2005 Rob Landley <rob@landley.net> |
| 6 | * Copyright 2005 Frank Sorenson <frank@tuxrocks.com> |
| 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 9 | */ |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 10 | |
| 11 | //usage:#define mdev_trivial_usage |
| 12 | //usage: "[-s]" |
| 13 | //usage:#define mdev_full_usage "\n\n" |
| 14 | //usage: " -s Scan /sys and populate /dev during system boot\n" |
| 15 | //usage: "\n" |
| 16 | //usage: "It can be run by kernel as a hotplug helper. To activate it:\n" |
| 17 | //usage: " echo /sbin/mdev > /proc/sys/kernel/hotplug\n" |
| 18 | //usage: IF_FEATURE_MDEV_CONF( |
| 19 | //usage: "It uses /etc/mdev.conf with lines\n" |
| 20 | //usage: "[-]DEVNAME UID:GID PERM" |
| 21 | //usage: IF_FEATURE_MDEV_RENAME(" [>|=PATH]") |
| 22 | //usage: IF_FEATURE_MDEV_EXEC(" [@|$|*PROG]") |
| 23 | //usage: ) |
| 24 | //usage: |
| 25 | //usage:#define mdev_notes_usage "" |
| 26 | //usage: IF_FEATURE_MDEV_CONFIG( |
| 27 | //usage: "The mdev config file contains lines that look like:\n" |
| 28 | //usage: " hd[a-z][0-9]* 0:3 660\n\n" |
| 29 | //usage: "That's device name (with regex match), uid:gid, and permissions.\n\n" |
| 30 | //usage: IF_FEATURE_MDEV_EXEC( |
| 31 | //usage: "Optionally, that can be followed (on the same line) by a special character\n" |
| 32 | //usage: "and a command line to run after creating/before deleting the corresponding\n" |
| 33 | //usage: "device(s). The environment variable $MDEV indicates the active device node\n" |
| 34 | //usage: "(which is useful if it's a regex match). For example:\n\n" |
| 35 | //usage: " hdc root:cdrom 660 *ln -s $MDEV cdrom\n\n" |
| 36 | //usage: "The special characters are @ (run after creating), $ (run before deleting),\n" |
| 37 | //usage: "and * (run both after creating and before deleting). The commands run in\n" |
| 38 | //usage: "the /dev directory, and use system() which calls /bin/sh.\n\n" |
| 39 | //usage: ) |
| 40 | //usage: "Config file parsing stops on the first matching line. If no config\n" |
| 41 | //usage: "entry is matched, devices are created with default 0:0 660. (Make\n" |
| 42 | //usage: "the last line match .* to override this.)\n\n" |
| 43 | //usage: ) |
| 44 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 45 | #include "libbb.h" |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 46 | #include "xregex.h" |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 47 | |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 48 | /* "mdev -s" scans /sys/class/xxx, looking for directories which have dev |
| 49 | * file (it is of the form "M:m\n"). Example: /sys/class/tty/tty0/dev |
| 50 | * contains "4:0\n". Directory name is taken as device name, path component |
| 51 | * directly after /sys/class/ as subsystem. In this example, "tty0" and "tty". |
| 52 | * Then mdev creates the /dev/device_name node. |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 53 | * If /sys/class/.../dev file does not exist, mdev still may act |
| 54 | * on this device: see "@|$|*command args..." parameter in config file. |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 55 | * |
| 56 | * mdev w/o parameters is called as hotplug helper. It takes device |
| 57 | * and subsystem names from $DEVPATH and $SUBSYSTEM, extracts |
| 58 | * maj,min from "/sys/$DEVPATH/dev" and also examines |
| 59 | * $ACTION ("add"/"delete") and $FIRMWARE. |
| 60 | * |
| 61 | * If action is "add", mdev creates /dev/device_name similarly to mdev -s. |
| 62 | * (todo: explain "delete" and $FIRMWARE) |
| 63 | * |
| 64 | * If /etc/mdev.conf exists, it may modify /dev/device_name's properties. |
| 65 | * /etc/mdev.conf file format: |
| 66 | * |
| 67 | * [-][subsystem/]device user:grp mode [>|=path] [@|$|*command args...] |
| 68 | * [-]@maj,min[-min2] user:grp mode [>|=path] [@|$|*command args...] |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 69 | * [-]$envvar=val user:grp mode [>|=path] [@|$|*command args...] |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 70 | * |
| 71 | * Leading minus in 1st field means "don't stop on this line", otherwise |
| 72 | * search is stopped after the matching line is encountered. |
| 73 | * |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 74 | * The device name or "subsystem/device" combo is matched against 1st field |
| 75 | * (which is a regex), or maj,min is matched against 1st field, |
| 76 | * or specified environment variable (as regex) is matched against 1st field. |
| 77 | * |
| 78 | * $envvar=val format is useful for loading modules for hot-plugged devices |
| 79 | * which do not have driver loaded yet. In this case /sys/class/.../dev |
| 80 | * does not exist, but $MODALIAS is set to needed module's name |
| 81 | * (actually, an alias to it) by kernel. This rule instructs mdev |
| 82 | * to load the module and exit: |
| 83 | * $MODALIAS=.* 0:0 660 @modprobe "$MODALIAS" |
| 84 | * The kernel will generate another hotplug event when /sys/class/.../dev |
| 85 | * file appears. |
| 86 | * |
| 87 | * When line matches, the device node is created, chmod'ed and chown'ed, |
| 88 | * moved to path, and if >path, a symlink to moved node is created, |
| 89 | * all this if /sys/class/.../dev exists. |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 90 | * Examples: |
| 91 | * =loop/ - moves to /dev/loop |
| 92 | * >disk/sda%1 - moves to /dev/disk/sdaN, makes /dev/sdaN a symlink |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 93 | * |
| 94 | * Then "command args..." is executed (via sh -c 'command args...'). |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 95 | * @:execute on creation, $:on deletion, *:on both. |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 96 | * This happens regardless of /sys/class/.../dev existence. |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 97 | */ |
| 98 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 99 | struct globals { |
Rob Landley | 5cd1ccd | 2006-05-21 18:33:27 +0000 | [diff] [blame] | 100 | int root_major, root_minor; |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 101 | char *subsystem; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 102 | } FIX_ALIASING; |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 103 | #define G (*(struct globals*)&bb_common_bufsiz1) |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 104 | |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 105 | /* Prevent infinite loops in /sys symlinks */ |
| 106 | #define MAX_SYSFS_DEPTH 3 |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 107 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 108 | /* We use additional 64+ bytes in make_device() */ |
| 109 | #define SCRATCH_SIZE 80 |
| 110 | |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 111 | /* Builds an alias path. |
| 112 | * This function potentionally reallocates the alias parameter. |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 113 | * Only used for ENABLE_FEATURE_MDEV_RENAME |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 114 | */ |
| 115 | static char *build_alias(char *alias, const char *device_name) |
| 116 | { |
| 117 | char *dest; |
| 118 | |
| 119 | /* ">bar/": rename to bar/device_name */ |
| 120 | /* ">bar[/]baz": rename to bar[/]baz */ |
| 121 | dest = strrchr(alias, '/'); |
| 122 | if (dest) { /* ">bar/[baz]" ? */ |
| 123 | *dest = '\0'; /* mkdir bar */ |
| 124 | bb_make_directory(alias, 0755, FILEUTILS_RECUR); |
| 125 | *dest = '/'; |
| 126 | if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */ |
| 127 | dest = alias; |
| 128 | alias = concat_path_file(alias, device_name); |
| 129 | free(dest); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return alias; |
| 134 | } |
| 135 | |
Denys Vlasenko | 76f5e38 | 2009-05-04 21:59:05 +0200 | [diff] [blame] | 136 | /* mknod in /dev based on a path like "/sys/block/hda/hda1" |
| 137 | * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes |
| 138 | * after NUL, but we promise to not mangle (IOW: to restore if needed) |
| 139 | * path string. |
| 140 | * NB2: "mdev -s" may call us many times, do not leak memory/fds! |
| 141 | */ |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 142 | static void make_device(char *path, int delete) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 143 | { |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 144 | char *device_name, *subsystem_slash_devname; |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 145 | int major, minor, type, len; |
Bernhard Reutner-Fischer | 933993a | 2009-10-26 23:27:05 +0100 | [diff] [blame] | 146 | mode_t mode; |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 147 | parser_t *parser; |
Mike Frysinger | f0044c4 | 2008-02-01 06:53:50 +0000 | [diff] [blame] | 148 | |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 149 | /* Try to read major/minor string. Note that the kernel puts \n after |
| 150 | * the data, so we don't need to worry about null terminating the string |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 151 | * because sscanf() will stop at the first nondigit, which \n is. |
| 152 | * We also depend on path having writeable space after it. |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 153 | */ |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 154 | major = -1; |
Rob Landley | 10b36f9 | 2006-08-10 01:09:37 +0000 | [diff] [blame] | 155 | if (!delete) { |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 156 | char *dev_maj_min = path + strlen(path); |
| 157 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 158 | strcpy(dev_maj_min, "/dev"); |
| 159 | len = open_read_close(path, dev_maj_min + 1, 64); |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 160 | *dev_maj_min = '\0'; |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 161 | if (len < 1) { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 162 | if (!ENABLE_FEATURE_MDEV_EXEC) |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 163 | return; |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 164 | /* no "dev" file, but we can still run scripts |
| 165 | * based on device name */ |
| 166 | } else if (sscanf(++dev_maj_min, "%u:%u", &major, &minor) != 2) { |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 167 | major = -1; |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 168 | } |
Rob Landley | 10b36f9 | 2006-08-10 01:09:37 +0000 | [diff] [blame] | 169 | } |
Denys Vlasenko | f2860bf | 2010-08-16 14:26:15 +0200 | [diff] [blame] | 170 | /* else: for delete, -1 still deletes the node, but < -1 suppresses that */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 171 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 172 | /* Determine device name, type, major and minor */ |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 173 | device_name = (char*) bb_basename(path); |
Denis Vlasenko | aa8a601 | 2008-03-29 16:07:46 +0000 | [diff] [blame] | 174 | /* http://kernel.org/doc/pending/hotplug.txt says that only |
Denis Vlasenko | a87045c | 2008-07-12 10:28:41 +0000 | [diff] [blame] | 175 | * "/sys/block/..." is for block devices. "/sys/bus" etc is not. |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 176 | * But since 2.6.25 block devices are also in /sys/class/block. |
| 177 | * We use strstr("/block/") to forestall future surprises. */ |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 178 | type = S_IFCHR; |
Denys Vlasenko | 8d4a8d1 | 2010-03-16 18:37:19 +0100 | [diff] [blame] | 179 | if (strstr(path, "/block/") || (G.subsystem && strncmp(G.subsystem, "block", 5) == 0)) |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 180 | type = S_IFBLK; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 181 | |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 182 | /* Make path point to "subsystem/device_name" */ |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 183 | subsystem_slash_devname = NULL; |
| 184 | /* Check for coldplug invocations first */ |
| 185 | if (strncmp(path, "/sys/block/", 11) == 0) /* legacy case */ |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 186 | path += sizeof("/sys/") - 1; |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 187 | else if (strncmp(path, "/sys/class/", 11) == 0) |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 188 | path += sizeof("/sys/class/") - 1; |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 189 | else { |
| 190 | /* Example of a hotplug invocation: |
| 191 | * SUBSYSTEM="block" |
| 192 | * DEVPATH="/sys" + "/devices/virtual/mtd/mtd3/mtdblock3" |
| 193 | * ("/sys" is added by mdev_main) |
| 194 | * - path does not contain subsystem |
| 195 | */ |
| 196 | subsystem_slash_devname = concat_path_file(G.subsystem, device_name); |
| 197 | path = subsystem_slash_devname; |
| 198 | } |
Denis Vlasenko | 6dda5de | 2009-04-16 21:42:12 +0000 | [diff] [blame] | 199 | |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 200 | /* If we have config file, look up user settings */ |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 201 | if (ENABLE_FEATURE_MDEV_CONF) |
| 202 | parser = config_open2("/etc/mdev.conf", fopen_for_read); |
| 203 | |
| 204 | do { |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 205 | int keep_matching; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 206 | struct bb_uidgid_t ugid; |
| 207 | char *tokens[4]; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 208 | char *command = NULL; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 209 | char *alias = NULL; |
| 210 | char aliaslink = aliaslink; /* for compiler */ |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 211 | |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 212 | /* Defaults in case we won't match any line */ |
| 213 | ugid.uid = ugid.gid = 0; |
| 214 | keep_matching = 0; |
| 215 | mode = 0660; |
| 216 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 217 | if (ENABLE_FEATURE_MDEV_CONF |
| 218 | && config_read(parser, tokens, 4, 3, "# \t", PARSE_NORMAL) |
| 219 | ) { |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 220 | char *val; |
| 221 | char *str_to_match; |
| 222 | regmatch_t off[1 + 9 * ENABLE_FEATURE_MDEV_RENAME_REGEXP]; |
| 223 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 224 | val = tokens[0]; |
| 225 | keep_matching = ('-' == val[0]); |
| 226 | val += keep_matching; /* swallow leading dash */ |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 227 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 228 | /* Match against either "subsystem/device_name" |
| 229 | * or "device_name" alone */ |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 230 | str_to_match = strchr(val, '/') ? path : device_name; |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 231 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 232 | /* Fields: regex uid:gid mode [alias] [cmd] */ |
Denis Vlasenko | 6dda5de | 2009-04-16 21:42:12 +0000 | [diff] [blame] | 233 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 234 | if (val[0] == '@') { |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 235 | /* @major,minor[-minor2] */ |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 236 | /* (useful when name is ambiguous: |
| 237 | * "/sys/class/usb/lp0" and |
| 238 | * "/sys/class/printer/lp0") */ |
| 239 | int cmaj, cmin0, cmin1, sc; |
| 240 | if (major < 0) |
| 241 | continue; /* no dev, no match */ |
| 242 | sc = sscanf(val, "@%u,%u-%u", &cmaj, &cmin0, &cmin1); |
Denys Vlasenko | b38af7b | 2010-04-02 06:47:14 +0200 | [diff] [blame] | 243 | if (sc < 1 |
| 244 | || major != cmaj |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 245 | || (sc == 2 && minor != cmin0) |
| 246 | || (sc == 3 && (minor < cmin0 || minor > cmin1)) |
| 247 | ) { |
| 248 | continue; /* this line doesn't match */ |
| 249 | } |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 250 | goto line_matches; |
| 251 | } |
| 252 | if (val[0] == '$') { |
| 253 | /* regex to match an environment variable */ |
| 254 | char *eq = strchr(++val, '='); |
| 255 | if (!eq) |
| 256 | continue; |
| 257 | *eq = '\0'; |
| 258 | str_to_match = getenv(val); |
| 259 | if (!str_to_match) |
| 260 | continue; |
| 261 | str_to_match -= strlen(val) + 1; |
| 262 | *eq = '='; |
| 263 | } |
| 264 | /* else: regex to match [subsystem/]device_name */ |
| 265 | |
| 266 | { |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 267 | regex_t match; |
| 268 | int result; |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 269 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 270 | xregcomp(&match, val, REG_EXTENDED); |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 271 | result = regexec(&match, str_to_match, ARRAY_SIZE(off), off, 0); |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 272 | regfree(&match); |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 273 | //bb_error_msg("matches:"); |
| 274 | //for (int i = 0; i < ARRAY_SIZE(off); i++) { |
| 275 | // if (off[i].rm_so < 0) continue; |
| 276 | // bb_error_msg("match %d: '%.*s'\n", i, |
| 277 | // (int)(off[i].rm_eo - off[i].rm_so), |
| 278 | // device_name + off[i].rm_so); |
| 279 | //} |
| 280 | |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 281 | /* If no match, skip rest of line */ |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 282 | /* (regexec returns whole pattern as "range" 0) */ |
Denys Vlasenko | b38af7b | 2010-04-02 06:47:14 +0200 | [diff] [blame] | 283 | if (result |
| 284 | || off[0].rm_so |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 285 | || ((int)off[0].rm_eo != (int)strlen(str_to_match)) |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 286 | ) { |
| 287 | continue; /* this line doesn't match */ |
| 288 | } |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 289 | } |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 290 | line_matches: |
| 291 | /* This line matches. Stop parsing after parsing |
| 292 | * the rest the line unless keep_matching == 1 */ |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 293 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 294 | /* 2nd field: uid:gid - device ownership */ |
Denys Vlasenko | aa0a12d | 2009-05-02 16:33:10 +0200 | [diff] [blame] | 295 | if (get_uidgid(&ugid, tokens[1], 1) == 0) |
Denys Vlasenko | 1612c75 | 2009-11-07 03:33:05 +0100 | [diff] [blame] | 296 | bb_error_msg("unknown user/group %s on line %d", tokens[1], parser->lineno); |
Denis Vlasenko | f2f3868 | 2008-03-29 17:26:10 +0000 | [diff] [blame] | 297 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 298 | /* 3rd field: mode - device permissions */ |
Bernhard Reutner-Fischer | 933993a | 2009-10-26 23:27:05 +0100 | [diff] [blame] | 299 | bb_parse_mode(tokens[2], &mode); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 300 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 301 | val = tokens[3]; |
Denys Vlasenko | b38af7b | 2010-04-02 06:47:14 +0200 | [diff] [blame] | 302 | /* 4th field (opt): ">|=alias" or "!" to not create the node */ |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 303 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 304 | if (ENABLE_FEATURE_MDEV_RENAME && val) { |
Denys Vlasenko | b38af7b | 2010-04-02 06:47:14 +0200 | [diff] [blame] | 305 | char *a, *s, *st; |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 306 | |
Denys Vlasenko | b38af7b | 2010-04-02 06:47:14 +0200 | [diff] [blame] | 307 | a = val; |
| 308 | s = strchrnul(val, ' '); |
| 309 | st = strchrnul(val, '\t'); |
| 310 | if (st < s) |
| 311 | s = st; |
| 312 | st = (s[0] && s[1]) ? s+1 : NULL; |
| 313 | |
| 314 | aliaslink = a[0]; |
| 315 | if (aliaslink == '!' && s == a+1) { |
| 316 | val = st; |
| 317 | /* "!": suppress node creation/deletion */ |
Denys Vlasenko | f2860bf | 2010-08-16 14:26:15 +0200 | [diff] [blame] | 318 | major = -2; |
Denys Vlasenko | b38af7b | 2010-04-02 06:47:14 +0200 | [diff] [blame] | 319 | } |
| 320 | else if (aliaslink == '>' || aliaslink == '=') { |
| 321 | val = st; |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 322 | s[0] = '\0'; |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 323 | if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) { |
Denys Vlasenko | b38af7b | 2010-04-02 06:47:14 +0200 | [diff] [blame] | 324 | char *p; |
| 325 | unsigned i, n; |
| 326 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 327 | /* substitute %1..9 with off[1..9], if any */ |
| 328 | n = 0; |
| 329 | s = a; |
| 330 | while (*s) |
| 331 | if (*s++ == '%') |
| 332 | n++; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 333 | |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 334 | p = alias = xzalloc(strlen(a) + n * strlen(str_to_match)); |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 335 | s = a + 1; |
| 336 | while (*s) { |
| 337 | *p = *s; |
| 338 | if ('%' == *s) { |
| 339 | i = (s[1] - '0'); |
| 340 | if (i <= 9 && off[i].rm_so >= 0) { |
| 341 | n = off[i].rm_eo - off[i].rm_so; |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 342 | strncpy(p, str_to_match + off[i].rm_so, n); |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 343 | p += n - 1; |
| 344 | s++; |
| 345 | } |
| 346 | } |
| 347 | p++; |
| 348 | s++; |
| 349 | } |
| 350 | } else { |
| 351 | alias = xstrdup(a + 1); |
Denis Vlasenko | f2f3868 | 2008-03-29 17:26:10 +0000 | [diff] [blame] | 352 | } |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 356 | if (ENABLE_FEATURE_MDEV_EXEC && val) { |
| 357 | const char *s = "$@*"; |
| 358 | const char *s2 = strchr(s, val[0]); |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 359 | |
Denys Vlasenko | 5478aaf | 2009-07-02 13:17:10 +0200 | [diff] [blame] | 360 | if (!s2) { |
| 361 | bb_error_msg("bad line %u", parser->lineno); |
| 362 | if (ENABLE_FEATURE_MDEV_RENAME) |
| 363 | free(alias); |
| 364 | continue; |
| 365 | } |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 366 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 367 | /* Are we running this command now? |
| 368 | * Run $cmd on delete, @cmd on create, *cmd on both |
| 369 | */ |
Denys Vlasenko | b38af7b | 2010-04-02 06:47:14 +0200 | [diff] [blame] | 370 | if (s2 - s != delete) { |
| 371 | /* We are here if: '*', |
| 372 | * or: '@' and delete = 0, |
| 373 | * or: '$' and delete = 1 |
| 374 | */ |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 375 | command = xstrdup(val + 1); |
Denys Vlasenko | b38af7b | 2010-04-02 06:47:14 +0200 | [diff] [blame] | 376 | } |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 377 | } |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 378 | } |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 379 | |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 380 | /* End of field parsing */ |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 381 | |
| 382 | /* "Execute" the line we found */ |
Denys Vlasenko | 90db359 | 2009-07-02 13:07:11 +0200 | [diff] [blame] | 383 | { |
| 384 | const char *node_name; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 385 | |
Denys Vlasenko | 90db359 | 2009-07-02 13:07:11 +0200 | [diff] [blame] | 386 | node_name = device_name; |
Denys Vlasenko | 2f86ca1 | 2009-06-22 01:31:12 +0200 | [diff] [blame] | 387 | if (ENABLE_FEATURE_MDEV_RENAME && alias) |
Denys Vlasenko | 90db359 | 2009-07-02 13:07:11 +0200 | [diff] [blame] | 388 | node_name = alias = build_alias(alias, device_name); |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 389 | |
Denys Vlasenko | 90db359 | 2009-07-02 13:07:11 +0200 | [diff] [blame] | 390 | if (!delete && major >= 0) { |
| 391 | if (mknod(node_name, mode | type, makedev(major, minor)) && errno != EEXIST) |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 392 | bb_perror_msg("can't create '%s'", node_name); |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 393 | if (major == G.root_major && minor == G.root_minor) |
Denys Vlasenko | 90db359 | 2009-07-02 13:07:11 +0200 | [diff] [blame] | 394 | symlink(node_name, "root"); |
| 395 | if (ENABLE_FEATURE_MDEV_CONF) { |
| 396 | chmod(node_name, mode); |
| 397 | chown(node_name, ugid.uid, ugid.gid); |
| 398 | } |
| 399 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { |
| 400 | if (aliaslink == '>') |
| 401 | symlink(node_name, device_name); |
| 402 | } |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 403 | } |
Denys Vlasenko | 90db359 | 2009-07-02 13:07:11 +0200 | [diff] [blame] | 404 | |
| 405 | if (ENABLE_FEATURE_MDEV_EXEC && command) { |
| 406 | /* setenv will leak memory, use putenv/unsetenv/free */ |
| 407 | char *s = xasprintf("%s=%s", "MDEV", node_name); |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 408 | char *s1 = xasprintf("%s=%s", "SUBSYSTEM", G.subsystem); |
Denys Vlasenko | 90db359 | 2009-07-02 13:07:11 +0200 | [diff] [blame] | 409 | putenv(s); |
| 410 | putenv(s1); |
| 411 | if (system(command) == -1) |
Denys Vlasenko | 5478aaf | 2009-07-02 13:17:10 +0200 | [diff] [blame] | 412 | bb_perror_msg("can't run '%s'", command); |
Denys Vlasenko | dd8adde | 2010-06-24 05:00:50 +0200 | [diff] [blame] | 413 | bb_unsetenv_and_free(s1); |
| 414 | bb_unsetenv_and_free(s); |
Denys Vlasenko | 90db359 | 2009-07-02 13:07:11 +0200 | [diff] [blame] | 415 | free(command); |
| 416 | } |
| 417 | |
Denys Vlasenko | f2860bf | 2010-08-16 14:26:15 +0200 | [diff] [blame] | 418 | if (delete && major >= -1) { |
Denys Vlasenko | 90db359 | 2009-07-02 13:07:11 +0200 | [diff] [blame] | 419 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { |
| 420 | if (aliaslink == '>') |
| 421 | unlink(device_name); |
| 422 | } |
| 423 | unlink(node_name); |
| 424 | } |
| 425 | |
Denys Vlasenko | 2f86ca1 | 2009-06-22 01:31:12 +0200 | [diff] [blame] | 426 | if (ENABLE_FEATURE_MDEV_RENAME) |
| 427 | free(alias); |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 430 | /* We found matching line. |
| 431 | * Stop unless it was prefixed with '-' */ |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 432 | if (ENABLE_FEATURE_MDEV_CONF && !keep_matching) |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 433 | break; |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 434 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 435 | /* end of "while line is read from /etc/mdev.conf" */ |
| 436 | } while (ENABLE_FEATURE_MDEV_CONF); |
| 437 | |
| 438 | if (ENABLE_FEATURE_MDEV_CONF) |
| 439 | config_close(parser); |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 440 | free(subsystem_slash_devname); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 443 | /* File callback for /sys/ traversal */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 444 | static int FAST_FUNC fileAction(const char *fileName, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 445 | struct stat *statbuf UNUSED_PARAM, |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 446 | void *userData, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 447 | int depth UNUSED_PARAM) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 448 | { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 449 | size_t len = strlen(fileName) - 4; /* can't underflow */ |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 450 | char *scratch = userData; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 451 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 452 | /* len check is for paranoid reasons */ |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 453 | if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX) |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 454 | return FALSE; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 455 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 456 | strcpy(scratch, fileName); |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 457 | scratch[len] = '\0'; |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 458 | make_device(scratch, /*delete:*/ 0); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 459 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 460 | return TRUE; |
| 461 | } |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 462 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 463 | /* Directory callback for /sys/ traversal */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 464 | static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM, |
| 465 | struct stat *statbuf UNUSED_PARAM, |
| 466 | void *userData UNUSED_PARAM, |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 467 | int depth) |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 468 | { |
Denis Vlasenko | 32a3d08 | 2009-04-14 21:23:33 +0000 | [diff] [blame] | 469 | /* Extract device subsystem -- the name of the directory |
| 470 | * under /sys/class/ */ |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 471 | if (1 == depth) { |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 472 | free(G.subsystem); |
| 473 | G.subsystem = strrchr(fileName, '/'); |
| 474 | if (G.subsystem) |
| 475 | G.subsystem = xstrdup(G.subsystem + 1); |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 478 | return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 481 | /* For the full gory details, see linux/Documentation/firmware_class/README |
| 482 | * |
| 483 | * Firmware loading works like this: |
| 484 | * - kernel sets FIRMWARE env var |
| 485 | * - userspace checks /lib/firmware/$FIRMWARE |
| 486 | * - userspace waits for /sys/$DEVPATH/loading to appear |
| 487 | * - userspace writes "1" to /sys/$DEVPATH/loading |
| 488 | * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data |
| 489 | * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading |
| 490 | * - kernel loads firmware into device |
| 491 | */ |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 492 | static void load_firmware(const char *firmware, const char *sysfs_path) |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 493 | { |
| 494 | int cnt; |
| 495 | int firmware_fd, loading_fd, data_fd; |
| 496 | |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 497 | /* check for /lib/firmware/$FIRMWARE */ |
| 498 | xchdir("/lib/firmware"); |
Mike Frysinger | 0e0639b | 2007-06-14 09:29:48 +0000 | [diff] [blame] | 499 | firmware_fd = xopen(firmware, O_RDONLY); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 500 | |
| 501 | /* in case we goto out ... */ |
| 502 | data_fd = -1; |
| 503 | |
| 504 | /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */ |
| 505 | xchdir(sysfs_path); |
| 506 | for (cnt = 0; cnt < 30; ++cnt) { |
| 507 | loading_fd = open("loading", O_WRONLY); |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 508 | if (loading_fd != -1) |
| 509 | goto loading; |
| 510 | sleep(1); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 511 | } |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 512 | goto out; |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 513 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 514 | loading: |
Denis Vlasenko | 32a3d08 | 2009-04-14 21:23:33 +0000 | [diff] [blame] | 515 | /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */ |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 516 | if (full_write(loading_fd, "1", 1) != 1) |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 517 | goto out; |
| 518 | |
Denis Vlasenko | 32a3d08 | 2009-04-14 21:23:33 +0000 | [diff] [blame] | 519 | /* load firmware into /sys/$DEVPATH/data */ |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 520 | data_fd = open("data", O_WRONLY); |
| 521 | if (data_fd == -1) |
| 522 | goto out; |
| 523 | cnt = bb_copyfd_eof(firmware_fd, data_fd); |
| 524 | |
Denis Vlasenko | 32a3d08 | 2009-04-14 21:23:33 +0000 | [diff] [blame] | 525 | /* tell kernel result by "echo [0|-1] > /sys/$DEVPATH/loading" */ |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 526 | if (cnt > 0) |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 527 | full_write(loading_fd, "0", 1); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 528 | else |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 529 | full_write(loading_fd, "-1", 2); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 530 | |
| 531 | out: |
| 532 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 533 | close(firmware_fd); |
| 534 | close(loading_fd); |
| 535 | close(data_fd); |
| 536 | } |
| 537 | } |
| 538 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 539 | int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 540 | int mdev_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 541 | { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 542 | RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 543 | |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 544 | /* We can be called as hotplug helper */ |
Denis Vlasenko | f4e6bd0 | 2008-05-31 18:27:58 +0000 | [diff] [blame] | 545 | /* Kernel cannot provide suitable stdio fds for us, do it ourself */ |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 546 | bb_sanitize_stdio(); |
Denis Vlasenko | f4e6bd0 | 2008-05-31 18:27:58 +0000 | [diff] [blame] | 547 | |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 548 | /* Force the configuration file settings exactly */ |
| 549 | umask(0); |
| 550 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 551 | xchdir("/dev"); |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 552 | |
Denis Vlasenko | aafbae6 | 2009-04-13 13:33:02 +0000 | [diff] [blame] | 553 | if (argv[1] && strcmp(argv[1], "-s") == 0) { |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 554 | /* Scan: |
| 555 | * mdev -s |
| 556 | */ |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 557 | struct stat st; |
| 558 | |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 559 | xstat("/", &st); |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 560 | G.root_major = major(st.st_dev); |
| 561 | G.root_minor = minor(st.st_dev); |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 562 | |
Denis Vlasenko | 67075bb | 2008-07-06 17:00:49 +0000 | [diff] [blame] | 563 | /* ACTION_FOLLOWLINKS is needed since in newer kernels |
| 564 | * /sys/block/loop* (for example) are symlinks to dirs, |
| 565 | * not real directories. |
| 566 | * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs, |
Denis Vlasenko | d63cd1b | 2009-02-13 00:02:54 +0000 | [diff] [blame] | 567 | * but we can't enforce that on users) |
| 568 | */ |
| 569 | if (access("/sys/class/block", F_OK) != 0) { |
| 570 | /* Scan obsolete /sys/block only if /sys/class/block |
| 571 | * doesn't exist. Otherwise we'll have dupes. |
Denis Vlasenko | f1df35c | 2009-04-12 14:00:12 +0000 | [diff] [blame] | 572 | * Also, do not complain if it doesn't exist. |
| 573 | * Some people configure kernel to have no blockdevs. |
Denis Vlasenko | d63cd1b | 2009-02-13 00:02:54 +0000 | [diff] [blame] | 574 | */ |
| 575 | recursive_action("/sys/block", |
Denis Vlasenko | f1df35c | 2009-04-12 14:00:12 +0000 | [diff] [blame] | 576 | ACTION_RECURSE | ACTION_FOLLOWLINKS | ACTION_QUIET, |
Denis Vlasenko | d63cd1b | 2009-02-13 00:02:54 +0000 | [diff] [blame] | 577 | fileAction, dirAction, temp, 0); |
| 578 | } |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 579 | recursive_action("/sys/class", |
Denis Vlasenko | 67075bb | 2008-07-06 17:00:49 +0000 | [diff] [blame] | 580 | ACTION_RECURSE | ACTION_FOLLOWLINKS, |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 581 | fileAction, dirAction, temp, 0); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 582 | } else { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 583 | char *fw; |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 584 | char *seq; |
| 585 | char *action; |
| 586 | char *env_path; |
Bernhard Reutner-Fischer | 6531f09 | 2009-10-26 23:27:04 +0100 | [diff] [blame] | 587 | static const char keywords[] ALIGN1 = "remove\0add\0"; |
| 588 | enum { OP_remove = 0, OP_add }; |
| 589 | smalluint op; |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 590 | |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 591 | /* Hotplug: |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 592 | * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 593 | * ACTION can be "add" or "remove" |
| 594 | * DEVPATH is like "/block/sda" or "/class/input/mice" |
| 595 | */ |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 596 | action = getenv("ACTION"); |
| 597 | env_path = getenv("DEVPATH"); |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 598 | G.subsystem = getenv("SUBSYSTEM"); |
| 599 | if (!action || !env_path /*|| !G.subsystem*/) |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 600 | bb_show_usage(); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 601 | fw = getenv("FIRMWARE"); |
Bernhard Reutner-Fischer | 6531f09 | 2009-10-26 23:27:04 +0100 | [diff] [blame] | 602 | op = index_in_strings(keywords, action); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 603 | /* If it exists, does /dev/mdev.seq match $SEQNUM? |
| 604 | * If it does not match, earlier mdev is running |
| 605 | * in parallel, and we need to wait */ |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 606 | seq = getenv("SEQNUM"); |
| 607 | if (seq) { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 608 | int timeout = 2000 / 32; /* 2000 msec */ |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 609 | do { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 610 | int seqlen; |
| 611 | char seqbuf[sizeof(int)*3 + 2]; |
| 612 | |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 613 | seqlen = open_read_close("mdev.seq", seqbuf, sizeof(seqbuf-1)); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 614 | if (seqlen < 0) { |
| 615 | seq = NULL; |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 616 | break; |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 617 | } |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 618 | seqbuf[seqlen] = '\0'; |
| 619 | if (seqbuf[0] == '\n' /* seed file? */ |
| 620 | || strcmp(seq, seqbuf) == 0 /* correct idx? */ |
| 621 | ) { |
| 622 | break; |
| 623 | } |
| 624 | usleep(32*1000); |
| 625 | } while (--timeout); |
| 626 | } |
| 627 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 628 | snprintf(temp, PATH_MAX, "/sys%s", env_path); |
Bernhard Reutner-Fischer | 6531f09 | 2009-10-26 23:27:04 +0100 | [diff] [blame] | 629 | if (op == OP_remove) { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 630 | /* Ignoring "remove firmware". It was reported |
| 631 | * to happen and to cause erroneous deletion |
| 632 | * of device nodes. */ |
| 633 | if (!fw) |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 634 | make_device(temp, /*delete:*/ 1); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 635 | } |
Bernhard Reutner-Fischer | 6531f09 | 2009-10-26 23:27:04 +0100 | [diff] [blame] | 636 | else if (op == OP_add) { |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 637 | make_device(temp, /*delete:*/ 0); |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 638 | if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 639 | if (fw) |
| 640 | load_firmware(fw, temp); |
| 641 | } |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 642 | } |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 643 | |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 644 | if (seq) { |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 645 | xopen_xwrite_close("mdev.seq", utoa(xatou(seq) + 1)); |
| 646 | } |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 649 | if (ENABLE_FEATURE_CLEAN_UP) |
| 650 | RELEASE_CONFIG_BUFFER(temp); |
| 651 | |
Denis Vlasenko | aafbae6 | 2009-04-13 13:33:02 +0000 | [diff] [blame] | 652 | return EXIT_SUCCESS; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 653 | } |