Merge pull request #177 from azak-azkaran/master

Issue: #176 - removes exit in inode2prog:
This commit is contained in:
Arnout Engelen
2019-06-24 13:06:49 +02:00
committed by GitHub

View File

@@ -30,6 +30,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <sstream>
#include <string> #include <string>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
@@ -86,8 +87,10 @@ static std::string read_file(int fd) {
for (int length; (length = read(fd, buf, sizeof(buf))) > 0;) { for (int length; (length = read(fd, buf, sizeof(buf))) > 0;) {
if (length < 0) { if (length < 0) {
std::fprintf(stderr, "Error reading file: %s\n", std::strerror(errno)); std::stringstream error;
std::exit(34); error << "Error reading file:" << std::strerror(errno) << "\n";
std::fprintf(stderr, "%s", error.str().c_str());
throw error.str();
} }
content.append(buf, length); content.append(buf, length);
} }
@@ -99,18 +102,21 @@ static std::string read_file(const char *filepath) {
int fd = open(filepath, O_RDONLY); int fd = open(filepath, O_RDONLY);
if (fd < 0) { if (fd < 0) {
std::fprintf(stderr, "Error opening %s: %s\n", filepath, std::stringstream error;
std::strerror(errno)); error << "Error opening " << filepath << ":" << std::strerror(errno)
std::exit(3); << "\n";
return NULL; std::fprintf(stderr, "%s", error.str().c_str());
throw error.str();
} }
std::string contents = read_file(fd); std::string contents = read_file(fd);
if (close(fd)) { if (close(fd)) {
std::fprintf(stderr, "Error opening %s: %s\n", filepath, std::stringstream error;
std::strerror(errno)); error << "Error opening " << filepath << ":" << std::strerror(errno)
std::exit(34); << "\n";
std::fprintf(stderr, "%s", error.str().c_str());
throw error.str();
} }
return contents; return contents;
@@ -125,8 +131,11 @@ std::string getcmdline(pid_t pid) {
bool replace_null = false; bool replace_null = false;
try { try {
cmdline = read_file(filename); cmdline = read_file(filename);
} catch (int e) { } catch (const char *e) {
std::fprintf(stderr, "An exception occurred. Exception Nr %i \n", e); std::fprintf(stderr, "An exception occurred. Exception %s \n", e);
cmdline = "";
} catch (...) {
std::fprintf(stderr, "An exception occurred while reading cmdline.\n");
cmdline = ""; cmdline = "";
} }