Skip to content

Commit

Permalink
fix(system_monitor): fix bugprone-exception-escape (#9781)
Browse files Browse the repository at this point in the history
* fix: bugprone-error

Signed-off-by: kobayu858 <yutaro.kobayashi@tier4.jp>

* fix: clippy

Signed-off-by: kobayu858 <yutaro.kobayashi@tier4.jp>

---------

Signed-off-by: kobayu858 <yutaro.kobayashi@tier4.jp>
  • Loading branch information
kobayu858 authored Jan 7, 2025
1 parent e2accc5 commit 3aa84a0
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 106 deletions.
83 changes: 46 additions & 37 deletions system/system_monitor/reader/hdd_reader/hdd_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <regex>
#include <string>
#include <utility>
Expand Down Expand Up @@ -655,48 +656,56 @@ void run(int port)

int main(int argc, char ** argv)
{
static struct option long_options[] = {
{"help", no_argument, nullptr, 'h'},
{"port", required_argument, nullptr, 'p'},
{nullptr, 0, nullptr, 0}};

// Parse command-line options
int c = 0;
int option_index = 0;
int port = PORT;
while ((c = getopt_long(argc, argv, "hp:", long_options, &option_index)) != -1) {
switch (c) {
case 'h':
usage();
return EXIT_SUCCESS;

case 'p':
try {
port = boost::lexical_cast<int>(optarg);
} catch (const boost::bad_lexical_cast & e) {
printf("Error: %s\n", e.what());
return EXIT_FAILURE;
}
break;

default:
break;
try {
static struct option long_options[] = {
{"help", no_argument, nullptr, 'h'},
{"port", required_argument, nullptr, 'p'},
{nullptr, 0, nullptr, 0}};

// Parse command-line options
int c = 0;
int option_index = 0;
int port = PORT;
while ((c = getopt_long(argc, argv, "hp:", long_options, &option_index)) != -1) {
switch (c) {
case 'h':
usage();
return EXIT_SUCCESS;

case 'p':
try {
port = boost::lexical_cast<int>(optarg);
} catch (const boost::bad_lexical_cast & e) {
printf("Error: %s\n", e.what());
return EXIT_FAILURE;
}
break;

default:
break;
}
}
}

// Put the program in the background
if (daemon(0, 0) < 0) {
printf("Failed to put the program in the background. %s\n", strerror(errno));
return errno;
}
// Put the program in the background
if (daemon(0, 0) < 0) {
printf("Failed to put the program in the background. %s\n", strerror(errno));
return errno;
}

// Open connection to system logger
openlog(nullptr, LOG_PID, LOG_DAEMON);
// Open connection to system logger
openlog(nullptr, LOG_PID, LOG_DAEMON);

run(port);
run(port);

// Close descriptor used to write to system logger
closelog();
// Close descriptor used to write to system logger
closelog();
} catch (const std::exception & e) {
std::cerr << "Exception in main(): " << e.what() << std::endl;
return EXIT_FAILURE;
} catch (...) {
std::cerr << "Unknown exception in main()" << std::endl;
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}
146 changes: 77 additions & 69 deletions system/system_monitor/reader/msr_reader/msr_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,90 +212,98 @@ void run(int port, const std::vector<std::string> & list)

int main(int argc, char ** argv)
{
static struct option long_options[] = {
{"help", no_argument, 0, 'h'}, {"port", required_argument, 0, 'p'}, {0, 0, 0, 0}};

// Parse command-line options
int c = 0;
int option_index = 0;
int port = PORT;
while ((c = getopt_long(argc, argv, "hp:", long_options, &option_index)) != -1) {
switch (c) {
case 'h':
usage();
return EXIT_SUCCESS;

case 'p':
try {
port = boost::lexical_cast<int>(optarg);
} catch (const boost::bad_lexical_cast & e) {
printf("Error: %s\n", e.what());
return EXIT_FAILURE;
}
break;
try {
static struct option long_options[] = {
{"help", no_argument, 0, 'h'}, {"port", required_argument, 0, 'p'}, {0, 0, 0, 0}};

// Parse command-line options
int c = 0;
int option_index = 0;
int port = PORT;
while ((c = getopt_long(argc, argv, "hp:", long_options, &option_index)) != -1) {
switch (c) {
case 'h':
usage();
return EXIT_SUCCESS;

case 'p':
try {
port = boost::lexical_cast<int>(optarg);
} catch (const boost::bad_lexical_cast & e) {
printf("Error: %s\n", e.what());
return EXIT_FAILURE;
}
break;

default:
break;
}
}

default:
break;
if (!fs::exists("/dev/cpu")) {
printf("Failed to access /dev/cpu.\n");
return EXIT_FAILURE;
}
}

if (!fs::exists("/dev/cpu")) {
printf("Failed to access /dev/cpu.\n");
return EXIT_FAILURE;
}
std::vector<std::string> list;
const fs::path root("/dev/cpu");

std::vector<std::string> list;
const fs::path root("/dev/cpu");
for (const fs::path & path : boost::make_iterator_range(
fs::recursive_directory_iterator(root), fs::recursive_directory_iterator())) {
if (fs::is_directory(path)) {
continue;
}

for (const fs::path & path : boost::make_iterator_range(
fs::recursive_directory_iterator(root), fs::recursive_directory_iterator())) {
if (fs::is_directory(path)) {
continue;
}
std::cmatch match;
const char * msr = path.generic_string().c_str();

std::cmatch match;
const char * msr = path.generic_string().c_str();
// /dev/cpu/[0-9]/msr ?
if (!std::regex_match(msr, match, std::regex(".*msr"))) {
continue;
}

// /dev/cpu/[0-9]/msr ?
if (!std::regex_match(msr, match, std::regex(".*msr"))) {
continue;
list.push_back(path.generic_string());
}

list.push_back(path.generic_string());
}
std::sort(list.begin(), list.end(), [](const std::string & c1, const std::string & c2) {
std::cmatch match;
const std::regex filter(".*/(\\d+)/msr");
int n1 = 0;
int n2 = 0;
if (std::regex_match(c1.c_str(), match, filter)) {
n1 = std::stoi(match[1].str());
}
if (std::regex_match(c2.c_str(), match, filter)) {
n2 = std::stoi(match[1].str());
}
return n1 < n2;
}); // NOLINT

std::sort(list.begin(), list.end(), [](const std::string & c1, const std::string & c2) {
std::cmatch match;
const std::regex filter(".*/(\\d+)/msr");
int n1 = 0;
int n2 = 0;
if (std::regex_match(c1.c_str(), match, filter)) {
n1 = std::stoi(match[1].str());
}
if (std::regex_match(c2.c_str(), match, filter)) {
n2 = std::stoi(match[1].str());
if (list.empty()) {
printf("No msr found in /dev/cpu.\n");
return EXIT_FAILURE;
}
return n1 < n2;
}); // NOLINT

if (list.empty()) {
printf("No msr found in /dev/cpu.\n");
return EXIT_FAILURE;
}

// Put the program in the background
if (daemon(0, 0) < 0) {
printf("Failed to put the program in the background. %s\n", strerror(errno));
return errno;
}
// Put the program in the background
if (daemon(0, 0) < 0) {
printf("Failed to put the program in the background. %s\n", strerror(errno));
return errno;
}

// Open connection to system logger
openlog(nullptr, LOG_PID, LOG_DAEMON);
// Open connection to system logger
openlog(nullptr, LOG_PID, LOG_DAEMON);

run(port, list);
run(port, list);

// Close descriptor used to write to system logger
closelog();
// Close descriptor used to write to system logger
closelog();
} catch (const std::exception & e) {
std::cerr << "Exception in main(): " << e.what() << std::endl;
return EXIT_FAILURE;
} catch (...) {
std::cerr << "Unknown exception in main()" << std::endl;
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

0 comments on commit 3aa84a0

Please sign in to comment.