@@ -212,90 +212,98 @@ void run(int port, const std::vector<std::string> & list)
212
212
213
213
int main (int argc, char ** argv)
214
214
{
215
- static struct option long_options[] = {
216
- {" help" , no_argument, 0 , ' h' }, {" port" , required_argument, 0 , ' p' }, {0 , 0 , 0 , 0 }};
217
-
218
- // Parse command-line options
219
- int c = 0 ;
220
- int option_index = 0 ;
221
- int port = PORT;
222
- while ((c = getopt_long (argc, argv, " hp:" , long_options, &option_index)) != -1 ) {
223
- switch (c) {
224
- case ' h' :
225
- usage ();
226
- return EXIT_SUCCESS;
227
-
228
- case ' p' :
229
- try {
230
- port = boost::lexical_cast<int >(optarg );
231
- } catch (const boost::bad_lexical_cast & e) {
232
- printf (" Error: %s\n " , e.what ());
233
- return EXIT_FAILURE;
234
- }
235
- break ;
215
+ try {
216
+ static struct option long_options[] = {
217
+ {" help" , no_argument, 0 , ' h' }, {" port" , required_argument, 0 , ' p' }, {0 , 0 , 0 , 0 }};
218
+
219
+ // Parse command-line options
220
+ int c = 0 ;
221
+ int option_index = 0 ;
222
+ int port = PORT;
223
+ while ((c = getopt_long (argc, argv, " hp:" , long_options, &option_index)) != -1 ) {
224
+ switch (c) {
225
+ case ' h' :
226
+ usage ();
227
+ return EXIT_SUCCESS;
228
+
229
+ case ' p' :
230
+ try {
231
+ port = boost::lexical_cast<int >(optarg );
232
+ } catch (const boost::bad_lexical_cast & e) {
233
+ printf (" Error: %s\n " , e.what ());
234
+ return EXIT_FAILURE;
235
+ }
236
+ break ;
237
+
238
+ default :
239
+ break ;
240
+ }
241
+ }
236
242
237
- default :
238
- break ;
243
+ if (!fs::exists (" /dev/cpu" )) {
244
+ printf (" Failed to access /dev/cpu.\n " );
245
+ return EXIT_FAILURE;
239
246
}
240
- }
241
247
242
- if (!fs::exists (" /dev/cpu" )) {
243
- printf (" Failed to access /dev/cpu.\n " );
244
- return EXIT_FAILURE;
245
- }
248
+ std::vector<std::string> list;
249
+ const fs::path root (" /dev/cpu" );
246
250
247
- std::vector<std::string> list;
248
- const fs::path root (" /dev/cpu" );
251
+ for (const fs::path & path : boost::make_iterator_range (
252
+ fs::recursive_directory_iterator (root), fs::recursive_directory_iterator ())) {
253
+ if (fs::is_directory (path)) {
254
+ continue ;
255
+ }
249
256
250
- for (const fs::path & path : boost::make_iterator_range (
251
- fs::recursive_directory_iterator (root), fs::recursive_directory_iterator ())) {
252
- if (fs::is_directory (path)) {
253
- continue ;
254
- }
257
+ std::cmatch match;
258
+ const char * msr = path.generic_string ().c_str ();
255
259
256
- std::cmatch match;
257
- const char * msr = path.generic_string ().c_str ();
260
+ // /dev/cpu/[0-9]/msr ?
261
+ if (!std::regex_match (msr, match, std::regex (" .*msr" ))) {
262
+ continue ;
263
+ }
258
264
259
- // /dev/cpu/[0-9]/msr ?
260
- if (!std::regex_match (msr, match, std::regex (" .*msr" ))) {
261
- continue ;
265
+ list.push_back (path.generic_string ());
262
266
}
263
267
264
- list.push_back (path.generic_string ());
265
- }
268
+ std::sort (list.begin (), list.end (), [](const std::string & c1, const std::string & c2) {
269
+ std::cmatch match;
270
+ const std::regex filter (" .*/(\\ d+)/msr" );
271
+ int n1 = 0 ;
272
+ int n2 = 0 ;
273
+ if (std::regex_match (c1.c_str (), match, filter)) {
274
+ n1 = std::stoi (match[1 ].str ());
275
+ }
276
+ if (std::regex_match (c2.c_str (), match, filter)) {
277
+ n2 = std::stoi (match[1 ].str ());
278
+ }
279
+ return n1 < n2;
280
+ }); // NOLINT
266
281
267
- std::sort (list.begin (), list.end (), [](const std::string & c1, const std::string & c2) {
268
- std::cmatch match;
269
- const std::regex filter (" .*/(\\ d+)/msr" );
270
- int n1 = 0 ;
271
- int n2 = 0 ;
272
- if (std::regex_match (c1.c_str (), match, filter)) {
273
- n1 = std::stoi (match[1 ].str ());
274
- }
275
- if (std::regex_match (c2.c_str (), match, filter)) {
276
- n2 = std::stoi (match[1 ].str ());
282
+ if (list.empty ()) {
283
+ printf (" No msr found in /dev/cpu.\n " );
284
+ return EXIT_FAILURE;
277
285
}
278
- return n1 < n2;
279
- }); // NOLINT
280
-
281
- if (list.empty ()) {
282
- printf (" No msr found in /dev/cpu.\n " );
283
- return EXIT_FAILURE;
284
- }
285
286
286
- // Put the program in the background
287
- if (daemon (0 , 0 ) < 0 ) {
288
- printf (" Failed to put the program in the background. %s\n " , strerror (errno));
289
- return errno;
290
- }
287
+ // Put the program in the background
288
+ if (daemon (0 , 0 ) < 0 ) {
289
+ printf (" Failed to put the program in the background. %s\n " , strerror (errno));
290
+ return errno;
291
+ }
291
292
292
- // Open connection to system logger
293
- openlog (nullptr , LOG_PID, LOG_DAEMON);
293
+ // Open connection to system logger
294
+ openlog (nullptr , LOG_PID, LOG_DAEMON);
294
295
295
- run (port, list);
296
+ run (port, list);
296
297
297
- // Close descriptor used to write to system logger
298
- closelog ();
298
+ // Close descriptor used to write to system logger
299
+ closelog ();
300
+ } catch (const std::exception & e) {
301
+ std::cerr << " Exception in main(): " << e.what () << std::endl;
302
+ return EXIT_FAILURE;
303
+ } catch (...) {
304
+ std::cerr << " Unknown exception in main()" << std::endl;
305
+ return EXIT_FAILURE;
306
+ }
299
307
300
308
return EXIT_SUCCESS;
301
309
}
0 commit comments