From 9ccb3538dbe54e9ce444b374bff6664567fced0c Mon Sep 17 00:00:00 2001 From: Andreas Wieland Date: Mon, 24 Jun 2019 11:51:18 +0200 Subject: [PATCH] Issue: #176 - removes exit in inode2prog: * removes the exit calls in inode2prog which was called if an error occured while reading /proc//cmdline file. Instead an Exception is thrown with an error message * adds catch block for all exception while reading cmdline file --- src/inode2prog.cpp | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/inode2prog.cpp b/src/inode2prog.cpp index 5a39c69..dec0568 100644 --- a/src/inode2prog.cpp +++ b/src/inode2prog.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -86,8 +87,10 @@ static std::string read_file(int fd) { for (int length; (length = read(fd, buf, sizeof(buf))) > 0;) { if (length < 0) { - std::fprintf(stderr, "Error reading file: %s\n", std::strerror(errno)); - std::exit(34); + std::stringstream error; + error << "Error reading file:" << std::strerror(errno) << "\n"; + std::fprintf(stderr, "%s", error.str().c_str()); + throw error.str(); } content.append(buf, length); } @@ -99,18 +102,21 @@ static std::string read_file(const char *filepath) { int fd = open(filepath, O_RDONLY); if (fd < 0) { - std::fprintf(stderr, "Error opening %s: %s\n", filepath, - std::strerror(errno)); - std::exit(3); - return NULL; + std::stringstream error; + error << "Error opening " << filepath << ":" << std::strerror(errno) + << "\n"; + std::fprintf(stderr, "%s", error.str().c_str()); + throw error.str(); } std::string contents = read_file(fd); if (close(fd)) { - std::fprintf(stderr, "Error opening %s: %s\n", filepath, - std::strerror(errno)); - std::exit(34); + std::stringstream error; + error << "Error opening " << filepath << ":" << std::strerror(errno) + << "\n"; + std::fprintf(stderr, "%s", error.str().c_str()); + throw error.str(); } return contents; @@ -125,8 +131,11 @@ std::string getcmdline(pid_t pid) { bool replace_null = false; try { cmdline = read_file(filename); - } catch (int e) { - std::fprintf(stderr, "An exception occurred. Exception Nr %i \n", e); + } catch (const char *e) { + std::fprintf(stderr, "An exception occurred. Exception %s \n", e); + cmdline = ""; + } catch (...) { + std::fprintf(stderr, "An exception occurred while reading cmdline.\n"); cmdline = ""; }