Skip to content

Commit 3d246cc

Browse files
Mark SalyzynGerrit Code Review
Mark Salyzyn
authored and
Gerrit Code Review
committed
Merge "modprobe: add --all=modules.load flag"
2 parents d894b46 + 6c9a1e4 commit 3d246cc

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

toolbox/modprobe.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
*/
1616

1717
#include <ctype.h>
18+
#include <errno.h>
1819
#include <getopt.h>
1920
#include <stdlib.h>
21+
#include <string.h>
2022

2123
#include <iostream>
24+
#include <string>
2225

2326
#include <android-base/file.h>
2427
#include <android-base/strings.h>
@@ -37,10 +40,11 @@ void print_usage(void) {
3740
std::cerr << "Usage:" << std::endl;
3841
std::cerr << std::endl;
3942
// -d option is required on Android
40-
std::cerr << " modprobe [options] -d DIR MODULE..." << std::endl;
43+
std::cerr << " modprobe [options] -d DIR [--all=FILE|MODULE]..." << std::endl;
4144
std::cerr << " modprobe [options] -d DIR MODULE [symbol=value]..." << std::endl;
4245
std::cerr << std::endl;
4346
std::cerr << "Options:" << std::endl;
47+
std::cerr << " --all=FILE: FILE to acquire module names from" << std::endl;
4448
std::cerr << " -b, --use-blocklist: Apply blocklist to module names too" << std::endl;
4549
std::cerr << " -d, --dirname=DIR: Load modules from DIR, option may be used multiple times"
4650
<< std::endl;
@@ -61,11 +65,23 @@ void print_usage(void) {
6165
return EXIT_FAILURE; \
6266
}
6367

68+
std::string stripComments(const std::string& str) {
69+
for (std::string rv = str;;) {
70+
auto comment = rv.find('#');
71+
if (comment == std::string::npos) return rv;
72+
auto end = rv.find('\n', comment);
73+
if (end != std::string::npos) end = end - comment;
74+
rv.erase(comment, end);
75+
}
76+
/* NOTREACHED */
77+
}
78+
6479
} // anonymous namespace
6580

6681
extern "C" int modprobe_main(int argc, char** argv) {
6782
std::vector<std::string> modules;
6883
std::string module_parameters;
84+
std::string mods;
6985
std::vector<std::string> mod_dirs;
7086
modprobe_mode mode = AddModulesMode;
7187
bool blocklist = false;
@@ -78,7 +94,7 @@ extern "C" int modprobe_main(int argc, char** argv) {
7894
// OEMs to transition from toybox.
7995
// clang-format off
8096
static struct option long_options[] = {
81-
{ "all", no_argument, 0, 'a' },
97+
{ "all", optional_argument, 0, 'a' },
8298
{ "use-blocklist", no_argument, 0, 'b' },
8399
{ "dirname", required_argument, 0, 'd' },
84100
{ "show-depends", no_argument, 0, 'D' },
@@ -89,12 +105,24 @@ extern "C" int modprobe_main(int argc, char** argv) {
89105
{ "verbose", no_argument, 0, 'v' },
90106
};
91107
// clang-format on
92-
while ((opt = getopt_long(argc, argv, "abd:Dhlqrv", long_options, &option_index)) != -1) {
108+
while ((opt = getopt_long(argc, argv, "a::bd:Dhlqrv", long_options, &option_index)) != -1) {
93109
switch (opt) {
94110
case 'a':
95111
// toybox modprobe supported -a to load multiple modules, this
96-
// is supported here by default, ignore flag
112+
// is supported here by default, ignore flag if no argument.
97113
check_mode();
114+
if (optarg == NULL) break;
115+
if (!android::base::ReadFileToString(optarg, &mods)) {
116+
std::cerr << "Failed to open " << optarg << ": " << strerror(errno)
117+
<< std::endl;
118+
rv = EXIT_FAILURE;
119+
}
120+
for (auto mod : android::base::Split(stripComments(mods), "\n")) {
121+
mod = android::base::Trim(mod);
122+
if (mod == "") continue;
123+
if (std::find(modules.begin(), modules.end(), mod) != modules.end()) continue;
124+
modules.emplace_back(mod);
125+
}
98126
break;
99127
case 'b':
100128
blocklist = true;

0 commit comments

Comments
 (0)