aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jan Klötzke <jan@kloetzke.net>2019-05-03 22:35:50 +0200
committerGravatar Denys Vlasenko <vda.linux@googlemail.com>2019-06-03 12:55:30 +0200
commitd04b7261735788eea5e4d6d4c8c7159f17942581 (patch)
tree8961c5272efafa213a8ae9cd1621b8cedd8f4d13
parentf3634584d0fdeb4ae9e2fe0f5971d45b77e40296 (diff)
downloadbusybox-d04b7261735788eea5e4d6d4c8c7159f17942581.tar.gz
busybox-d04b7261735788eea5e4d6d4c8c7159f17942581.tar.bz2
mdev: move action processing into separate functions
This purely moves code from main() to separate functions for better extensibility. Signed-off-by: Jan Klötzke <jan@kloetzke.net> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--util-linux/mdev.c154
1 files changed, 82 insertions, 72 deletions
diff --git a/util-linux/mdev.c b/util-linux/mdev.c
index 63b55536a..8205a260a 100644
--- a/util-linux/mdev.c
+++ b/util-linux/mdev.c
@@ -1047,6 +1047,86 @@ static void signal_mdevs(unsigned my_pid)
}
}
+static void process_action(char *temp, unsigned my_pid)
+{
+ char *fw;
+ char *seq;
+ char *action;
+ char *env_devname;
+ char *env_devpath;
+ unsigned seqnum = seqnum; /* for compiler */
+ int seq_fd;
+ smalluint op;
+
+ /* Hotplug:
+ * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev
+ * ACTION can be "add", "remove", "change"
+ * DEVPATH is like "/block/sda" or "/class/input/mice"
+ */
+ env_devname = getenv("DEVNAME"); /* can be NULL */
+ G.subsystem = getenv("SUBSYSTEM");
+ action = getenv("ACTION");
+ env_devpath = getenv("DEVPATH");
+ if (!action || !env_devpath /*|| !G.subsystem*/)
+ bb_show_usage();
+ fw = getenv("FIRMWARE");
+ seq = getenv("SEQNUM");
+ op = index_in_strings(keywords, action);
+
+ open_mdev_log(seq, my_pid);
+
+ seq_fd = -1;
+ if (seq) {
+ seqnum = atoll(seq);
+ seq_fd = wait_for_seqfile(seqnum);
+ }
+
+ dbg1("%s "
+ "ACTION:%s SEQNUM:%s SUBSYSTEM:%s DEVNAME:%s DEVPATH:%s"
+ "%s%s",
+ curtime(),
+ action, seq, G.subsystem, env_devname, env_devpath,
+ fw ? " FW:" : "", fw ? fw : ""
+ );
+
+ snprintf(temp, PATH_MAX, "/sys%s", env_devpath);
+ if (op == OP_remove) {
+ /* Ignoring "remove firmware". It was reported
+ * to happen and to cause erroneous deletion
+ * of device nodes. */
+ if (!fw)
+ make_device(env_devname, temp, op);
+ }
+ else {
+ make_device(env_devname, temp, op);
+ if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
+ if (op == OP_add && fw)
+ load_firmware(fw, temp);
+ }
+ }
+
+ if (seq_fd >= 0) {
+ xwrite_str(seq_fd, utoa(seqnum + 1));
+ signal_mdevs(my_pid);
+ }
+}
+
+static void initial_scan(char *temp)
+{
+ struct stat st;
+
+ xstat("/", &st);
+ G.root_major = major(st.st_dev);
+ G.root_minor = minor(st.st_dev);
+
+ putenv((char*)"ACTION=add");
+
+ /* Create all devices from /sys/dev hierarchy */
+ recursive_action("/sys/dev",
+ ACTION_RECURSE | ACTION_FOLLOWLINKS,
+ fileAction, dirAction, temp, 0);
+}
+
int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int mdev_main(int argc UNUSED_PARAM, char **argv)
{
@@ -1071,86 +1151,16 @@ int mdev_main(int argc UNUSED_PARAM, char **argv)
/*
* Scan: mdev -s
*/
- struct stat st;
-
#if ENABLE_FEATURE_MDEV_CONF
/* Same as xrealloc_vector(NULL, 4, 0): */
G.rule_vec = xzalloc((1 << 4) * sizeof(*G.rule_vec));
#endif
- xstat("/", &st);
- G.root_major = major(st.st_dev);
- G.root_minor = minor(st.st_dev);
- putenv((char*)"ACTION=add");
-
- /* Create all devices from /sys/dev hierarchy */
- recursive_action("/sys/dev",
- ACTION_RECURSE | ACTION_FOLLOWLINKS,
- fileAction, dirAction, temp, 0);
+ initial_scan(temp);
} else {
- char *fw;
- char *seq;
- char *action;
- char *env_devname;
- char *env_devpath;
- unsigned my_pid;
- unsigned seqnum = seqnum; /* for compiler */
- int seq_fd;
- smalluint op;
-
- /* Hotplug:
- * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev
- * ACTION can be "add", "remove", "change"
- * DEVPATH is like "/block/sda" or "/class/input/mice"
- */
- env_devname = getenv("DEVNAME"); /* can be NULL */
- G.subsystem = getenv("SUBSYSTEM");
- action = getenv("ACTION");
- env_devpath = getenv("DEVPATH");
- if (!action || !env_devpath /*|| !G.subsystem*/)
- bb_show_usage();
- fw = getenv("FIRMWARE");
- seq = getenv("SEQNUM");
- op = index_in_strings(keywords, action);
-
- my_pid = getpid();
- open_mdev_log(seq, my_pid);
-
- seq_fd = -1;
- if (seq) {
- seqnum = atoll(seq);
- seq_fd = wait_for_seqfile(seqnum);
- }
-
- dbg1("%s "
- "ACTION:%s SUBSYSTEM:%s DEVNAME:%s DEVPATH:%s"
- "%s%s",
- curtime(),
- action, G.subsystem, env_devname, env_devpath,
- fw ? " FW:" : "", fw ? fw : ""
- );
-
- snprintf(temp, PATH_MAX, "/sys%s", env_devpath);
- if (op == OP_remove) {
- /* Ignoring "remove firmware". It was reported
- * to happen and to cause erroneous deletion
- * of device nodes. */
- if (!fw)
- make_device(env_devname, temp, op);
- }
- else {
- make_device(env_devname, temp, op);
- if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
- if (op == OP_add && fw)
- load_firmware(fw, temp);
- }
- }
+ process_action(temp, getpid());
dbg1("%s exiting", curtime());
- if (seq_fd >= 0) {
- xwrite_str(seq_fd, utoa(seqnum + 1));
- signal_mdevs(my_pid);
- }
}
if (ENABLE_FEATURE_CLEAN_UP)