summaryrefslogtreecommitdiffstats
path: root/libprocinfo
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2018-07-24 10:52:25 -0700
committerYabin Cui <yabinc@google.com>2018-07-24 14:34:06 -0700
commit5f036ab89bf04d5e048063d027defcf7ecb52871 (patch)
tree4e525390c24fa4b692aef50b4cec2af9410e956b /libprocinfo
parenta54c2938225bc45cf4fc93d929548bea103abd9f (diff)
downloadsystem_core-5f036ab89bf04d5e048063d027defcf7ecb52871.tar.gz
system_core-5f036ab89bf04d5e048063d027defcf7ecb52871.tar.bz2
system_core-5f036ab89bf04d5e048063d027defcf7ecb52871.zip
libprocinfo: return error msg instead of printing it.
So users can decide whether to print error msg or not. Bug: none Test: run libprocinfo_test. Change-Id: Ie1afcb58cb97493c7ffade6841b6eb7a800a3981
Diffstat (limited to 'libprocinfo')
-rw-r--r--libprocinfo/include/procinfo/process.h26
-rw-r--r--libprocinfo/process.cpp18
2 files changed, 29 insertions, 15 deletions
diff --git a/libprocinfo/include/procinfo/process.h b/libprocinfo/include/procinfo/process.h
index db56fc1a5..9278e1819 100644
--- a/libprocinfo/include/procinfo/process.h
+++ b/libprocinfo/include/procinfo/process.h
@@ -56,23 +56,25 @@ struct ProcessInfo {
};
// Parse the contents of /proc/<tid>/status into |process_info|.
-bool GetProcessInfo(pid_t tid, ProcessInfo* process_info);
+bool GetProcessInfo(pid_t tid, ProcessInfo* process_info, std::string* error = nullptr);
// Parse the contents of <fd>/status into |process_info|.
// |fd| should be an fd pointing at a /proc/<pid> directory.
-bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info);
+bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info, std::string* error = nullptr);
// Fetch the list of threads from a given process's /proc/<pid> directory.
// |fd| should be an fd pointing at a /proc/<pid> directory.
template <typename Collection>
-auto GetProcessTidsFromProcPidFd(int fd, Collection* out) ->
+auto GetProcessTidsFromProcPidFd(int fd, Collection* out, std::string* error = nullptr) ->
typename std::enable_if<sizeof(typename Collection::value_type) >= sizeof(pid_t), bool>::type {
out->clear();
int task_fd = openat(fd, "task", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
std::unique_ptr<DIR, int (*)(DIR*)> dir(fdopendir(task_fd), closedir);
if (!dir) {
- PLOG(ERROR) << "failed to open task directory";
+ if (error != nullptr) {
+ *error = "failed to open task directory";
+ }
return false;
}
@@ -81,7 +83,9 @@ auto GetProcessTidsFromProcPidFd(int fd, Collection* out) ->
if (strcmp(dent->d_name, ".") != 0 && strcmp(dent->d_name, "..") != 0) {
pid_t tid;
if (!android::base::ParseInt(dent->d_name, &tid, 1, std::numeric_limits<pid_t>::max())) {
- LOG(ERROR) << "failed to parse task id: " << dent->d_name;
+ if (error != nullptr) {
+ *error = std::string("failed to parse task id: ") + dent->d_name;
+ }
return false;
}
@@ -93,21 +97,25 @@ auto GetProcessTidsFromProcPidFd(int fd, Collection* out) ->
}
template <typename Collection>
-auto GetProcessTids(pid_t pid, Collection* out) ->
+auto GetProcessTids(pid_t pid, Collection* out, std::string* error = nullptr) ->
typename std::enable_if<sizeof(typename Collection::value_type) >= sizeof(pid_t), bool>::type {
char task_path[PATH_MAX];
if (snprintf(task_path, PATH_MAX, "/proc/%d", pid) >= PATH_MAX) {
- LOG(ERROR) << "task path overflow (pid = " << pid << ")";
+ if (error != nullptr) {
+ *error = "task path overflow (pid = " + std::to_string(pid) + ")";
+ }
return false;
}
android::base::unique_fd fd(open(task_path, O_DIRECTORY | O_RDONLY | O_CLOEXEC));
if (fd == -1) {
- PLOG(ERROR) << "failed to open " << task_path;
+ if (error != nullptr) {
+ *error = std::string("failed to open ") + task_path;
+ }
return false;
}
- return GetProcessTidsFromProcPidFd(fd.get(), out);
+ return GetProcessTidsFromProcPidFd(fd.get(), out, error);
}
#endif
diff --git a/libprocinfo/process.cpp b/libprocinfo/process.cpp
index 6e5be6e56..9194cf3d0 100644
--- a/libprocinfo/process.cpp
+++ b/libprocinfo/process.cpp
@@ -31,17 +31,19 @@ using android::base::unique_fd;
namespace android {
namespace procinfo {
-bool GetProcessInfo(pid_t tid, ProcessInfo* process_info) {
+bool GetProcessInfo(pid_t tid, ProcessInfo* process_info, std::string* error) {
char path[PATH_MAX];
snprintf(path, sizeof(path), "/proc/%d", tid);
unique_fd dirfd(open(path, O_DIRECTORY | O_RDONLY));
if (dirfd == -1) {
- PLOG(ERROR) << "failed to open " << path;
+ if (error != nullptr) {
+ *error = std::string("failed to open ") + path;
+ }
return false;
}
- return GetProcessInfoFromProcPidFd(dirfd.get(), process_info);
+ return GetProcessInfoFromProcPidFd(dirfd.get(), process_info, error);
}
static ProcessState parse_state(const char* state) {
@@ -62,17 +64,21 @@ static ProcessState parse_state(const char* state) {
}
}
-bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info) {
+bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info, std::string* error) {
int status_fd = openat(fd, "status", O_RDONLY | O_CLOEXEC);
if (status_fd == -1) {
- PLOG(ERROR) << "failed to open status fd in GetProcessInfoFromProcPidFd";
+ if (error != nullptr) {
+ *error = "failed to open status fd in GetProcessInfoFromProcPidFd";
+ }
return false;
}
std::unique_ptr<FILE, decltype(&fclose)> fp(fdopen(status_fd, "r"), fclose);
if (!fp) {
- PLOG(ERROR) << "failed to open status file in GetProcessInfoFromProcPidFd";
+ if (error != nullptr) {
+ *error = "failed to open status file in GetProcessInfoFromProcPidFd";
+ }
close(status_fd);
return false;
}