15
15
*/
16
16
17
17
#include < ctype.h>
18
- #include < errno.h>
19
18
#include < getopt.h>
20
19
#include < stdlib.h>
21
- #include < string.h>
22
20
23
- #include < iostream>
24
21
#include < string>
25
22
26
23
#include < android-base/file.h>
24
+ #include < android-base/logging.h>
27
25
#include < android-base/strings.h>
28
26
#include < modprobe/modprobe.h>
29
27
@@ -37,32 +35,31 @@ enum modprobe_mode {
37
35
};
38
36
39
37
void print_usage (void ) {
40
- std::cerr << " Usage:" << std::endl ;
41
- std::cerr << std::endl ;
38
+ LOG (INFO) << " Usage:" ;
39
+ LOG (INFO) ;
42
40
// -d option is required on Android
43
- std::cerr << " modprobe [options] -d DIR [--all=FILE|MODULE]..." << std::endl;
44
- std::cerr << " modprobe [options] -d DIR MODULE [symbol=value]..." << std::endl;
45
- std::cerr << std::endl;
46
- std::cerr << " Options:" << std::endl;
47
- std::cerr << " --all=FILE: FILE to acquire module names from" << std::endl;
48
- std::cerr << " -b, --use-blocklist: Apply blocklist to module names too" << std::endl;
49
- std::cerr << " -d, --dirname=DIR: Load modules from DIR, option may be used multiple times"
50
- << std::endl;
51
- std::cerr << " -D, --show-depends: Print dependencies for modules only, do not load"
52
- << std::endl;
53
- std::cerr << " -h, --help: Print this help" << std::endl;
54
- std::cerr << " -l, --list: List modules matching pattern" << std::endl;
55
- std::cerr << " -r, --remove: Remove MODULE (multiple modules may be specified)" << std::endl;
56
- std::cerr << " -q, --quiet: disable messages" << std::endl;
57
- std::cerr << " -v, --verbose: enable more messages" << std::endl;
58
- std::cerr << std::endl;
41
+ LOG (INFO) << " modprobe [options] -d DIR [--all=FILE|MODULE]..." ;
42
+ LOG (INFO) << " modprobe [options] -d DIR MODULE [symbol=value]..." ;
43
+ LOG (INFO);
44
+ LOG (INFO) << " Options:" ;
45
+ LOG (INFO) << " --all=FILE: FILE to acquire module names from" ;
46
+ LOG (INFO) << " -b, --use-blocklist: Apply blocklist to module names too" ;
47
+ LOG (INFO) << " -d, --dirname=DIR: Load modules from DIR, option may be used multiple times" ;
48
+ LOG (INFO) << " -D, --show-depends: Print dependencies for modules only, do not load" ;
49
+ LOG (INFO) << " -h, --help: Print this help" ;
50
+ LOG (INFO) << " -l, --list: List modules matching pattern" ;
51
+ LOG (INFO) << " -r, --remove: Remove MODULE (multiple modules may be specified)" ;
52
+ LOG (INFO) << " -s, --syslog: print to syslog also" ;
53
+ LOG (INFO) << " -q, --quiet: disable messages" ;
54
+ LOG (INFO) << " -v, --verbose: enable more messages, even more with a second -v" ;
55
+ LOG (INFO);
59
56
}
60
57
61
- #define check_mode () \
62
- if (mode != AddModulesMode) { \
63
- std::cerr << " Error, multiple mode flags specified" << std::endl ; \
64
- print_usage (); \
65
- return EXIT_FAILURE; \
58
+ #define check_mode () \
59
+ if (mode != AddModulesMode) { \
60
+ LOG (ERROR) << " multiple mode flags specified" ; \
61
+ print_usage (); \
62
+ return EXIT_FAILURE; \
66
63
}
67
64
68
65
std::string stripComments (const std::string& str) {
@@ -76,16 +73,28 @@ std::string stripComments(const std::string& str) {
76
73
/* NOTREACHED */
77
74
}
78
75
76
+ auto syslog = false ;
77
+
78
+ void MyLogger (android::base::LogId id, android::base::LogSeverity severity, const char * tag,
79
+ const char * file, unsigned int line, const char * message) {
80
+ android::base::StdioLogger (id, severity, tag, file, line, message);
81
+ if (syslog && message[0 ]) {
82
+ android::base::KernelLogger (id, severity, tag, file, line, message);
83
+ }
84
+ }
85
+
79
86
} // anonymous namespace
80
87
81
88
extern " C" int modprobe_main (int argc, char ** argv) {
89
+ android::base::InitLogging (argv, MyLogger);
90
+ android::base::SetMinimumLogSeverity (android::base::INFO);
91
+
82
92
std::vector<std::string> modules;
83
93
std::string module_parameters;
84
94
std::string mods;
85
95
std::vector<std::string> mod_dirs;
86
96
modprobe_mode mode = AddModulesMode;
87
97
bool blocklist = false ;
88
- bool verbose = false ;
89
98
int rv = EXIT_SUCCESS;
90
99
91
100
int opt;
@@ -102,19 +111,19 @@ extern "C" int modprobe_main(int argc, char** argv) {
102
111
{ " list" , no_argument, 0 , ' l' },
103
112
{ " quiet" , no_argument, 0 , ' q' },
104
113
{ " remove" , no_argument, 0 , ' r' },
114
+ { " syslog" , no_argument, 0 , ' s' },
105
115
{ " verbose" , no_argument, 0 , ' v' },
106
116
};
107
117
// clang-format on
108
- while ((opt = getopt_long (argc, argv, " a::bd:Dhlqrv " , long_options, &option_index)) != -1 ) {
118
+ while ((opt = getopt_long (argc, argv, " a::bd:Dhlqrsv " , long_options, &option_index)) != -1 ) {
109
119
switch (opt) {
110
120
case ' a' :
111
121
// toybox modprobe supported -a to load multiple modules, this
112
122
// is supported here by default, ignore flag if no argument.
113
123
check_mode ();
114
124
if (optarg == NULL ) break ;
115
125
if (!android::base::ReadFileToString (optarg , &mods)) {
116
- std::cerr << " Failed to open " << optarg << " : " << strerror (errno)
117
- << std::endl;
126
+ PLOG (ERROR) << " Failed to open " << optarg ;
118
127
rv = EXIT_FAILURE;
119
128
}
120
129
for (auto mod : android::base::Split (stripComments (mods), " \n " )) {
@@ -135,24 +144,33 @@ extern "C" int modprobe_main(int argc, char** argv) {
135
144
mode = ShowDependenciesMode;
136
145
break ;
137
146
case ' h' :
147
+ android::base::SetMinimumLogSeverity (android::base::INFO);
138
148
print_usage ();
139
- return EXIT_SUCCESS ;
149
+ return rv ;
140
150
case ' l' :
141
151
check_mode ();
142
152
mode = ListModulesMode;
143
153
break ;
144
154
case ' q' :
145
- verbose = false ;
155
+ android::base::SetMinimumLogSeverity (android::base::WARNING) ;
146
156
break ;
147
157
case ' r' :
148
158
check_mode ();
149
159
mode = RemoveModulesMode;
150
160
break ;
161
+ case ' s' :
162
+ syslog = true ;
163
+ break ;
151
164
case ' v' :
152
- verbose = true ;
165
+ if (android::base::GetMinimumLogSeverity () <= android::base::DEBUG) {
166
+ android::base::SetMinimumLogSeverity (android::base::VERBOSE);
167
+ } else {
168
+ android::base::SetMinimumLogSeverity (android::base::DEBUG);
169
+ }
153
170
break ;
154
171
default :
155
- std::cerr << " Unrecognized option: " << opt << std::endl;
172
+ LOG (ERROR) << " Unrecognized option: " << opt;
173
+ print_usage ();
156
174
return EXIT_FAILURE;
157
175
}
158
176
}
@@ -171,39 +189,33 @@ extern "C" int modprobe_main(int argc, char** argv) {
171
189
}
172
190
}
173
191
174
- if (verbose) {
175
- std::cout << " mode is " << mode << std::endl;
176
- std::cout << " verbose is " << verbose << std::endl;
177
- std::cout << " mod_dirs is: " << android::base::Join (mod_dirs, " " ) << std::endl;
178
- std::cout << " modules is: " << android::base::Join (modules, " " ) << std::endl;
179
- std::cout << " module parameters is: " << android::base::Join (module_parameters, " " )
180
- << std::endl;
181
- }
192
+ LOG (DEBUG) << " mode is " << mode;
193
+ LOG (DEBUG) << " mod_dirs is: " << android::base::Join (mod_dirs, " " );
194
+ LOG (DEBUG) << " modules is: " << android::base::Join (modules, " " );
195
+ LOG (DEBUG) << " module parameters is: " << android::base::Join (module_parameters, " " );
182
196
183
197
if (modules.empty ()) {
184
198
if (mode == ListModulesMode) {
185
199
// emulate toybox modprobe list with no pattern (list all)
186
200
modules.emplace_back (" *" );
187
201
} else {
188
- std::cerr << " No modules given." << std::endl ;
202
+ LOG (ERROR) << " No modules given." ;
189
203
print_usage ();
190
204
return EXIT_FAILURE;
191
205
}
192
206
}
193
207
if (mod_dirs.empty ()) {
194
- std::cerr << " No module configuration directories given." << std::endl ;
208
+ LOG (ERROR) << " No module configuration directories given." ;
195
209
print_usage ();
196
210
return EXIT_FAILURE;
197
211
}
198
212
if (parameter_count && modules.size () > 1 ) {
199
- std::cerr << " Only one module may be loaded when specifying module parameters."
200
- << std::endl;
213
+ LOG (ERROR) << " Only one module may be loaded when specifying module parameters." ;
201
214
print_usage ();
202
215
return EXIT_FAILURE;
203
216
}
204
217
205
218
Modprobe m (mod_dirs);
206
- m.EnableVerbose (verbose);
207
219
if (blocklist) {
208
220
m.EnableBlocklist (true );
209
221
}
@@ -212,19 +224,19 @@ extern "C" int modprobe_main(int argc, char** argv) {
212
224
switch (mode) {
213
225
case AddModulesMode:
214
226
if (!m.LoadWithAliases (module, true , module_parameters)) {
215
- std::cerr << " Failed to load module " << module << std::endl ;
227
+ PLOG (ERROR) << " Failed to load module " << module;
216
228
rv = EXIT_FAILURE;
217
229
}
218
230
break ;
219
231
case RemoveModulesMode:
220
232
if (!m.Remove (module)) {
221
- std::cerr << " Failed to remove module " << module << std::endl ;
233
+ PLOG (ERROR) << " Failed to remove module " << module;
222
234
rv = EXIT_FAILURE;
223
235
}
224
236
break ;
225
237
case ListModulesMode: {
226
238
std::vector<std::string> list = m.ListModules (module);
227
- std::cout << android::base::Join (list, " \n " ) << std::endl ;
239
+ LOG (INFO) << android::base::Join (list, " \n " );
228
240
break ;
229
241
}
230
242
case ShowDependenciesMode: {
@@ -235,17 +247,17 @@ extern "C" int modprobe_main(int argc, char** argv) {
235
247
rv = EXIT_FAILURE;
236
248
break ;
237
249
}
238
- std::cout << " Dependencies for " << module << " :" << std::endl ;
239
- std::cout << " Soft pre-dependencies:" << std::endl ;
240
- std::cout << android::base::Join (pre_deps, " \n " ) << std::endl ;
241
- std::cout << " Hard dependencies:" << std::endl ;
242
- std::cout << android::base::Join (deps, " \n " ) << std::endl ;
243
- std::cout << " Soft post-dependencies:" << std::endl ;
244
- std::cout << android::base::Join (post_deps, " \n " ) << std::endl ;
250
+ LOG (INFO) << " Dependencies for " << module << " :" ;
251
+ LOG (INFO) << " Soft pre-dependencies:" ;
252
+ LOG (INFO) << android::base::Join (pre_deps, " \n " );
253
+ LOG (INFO) << " Hard dependencies:" ;
254
+ LOG (INFO) << android::base::Join (deps, " \n " );
255
+ LOG (INFO) << " Soft post-dependencies:" ;
256
+ LOG (INFO) << android::base::Join (post_deps, " \n " );
245
257
break ;
246
258
}
247
259
default :
248
- std::cerr << " Bad mode" << std::endl ;
260
+ LOG (ERROR) << " Bad mode" ;
249
261
rv = EXIT_FAILURE;
250
262
}
251
263
}
0 commit comments