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//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 /status into |process_info|. // |fd| should be an fd pointing at a /proc/ 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/ directory. // |fd| should be an fd pointing at a /proc/ directory. template -auto GetProcessTidsFromProcPidFd(int fd, Collection* out) -> +auto GetProcessTidsFromProcPidFd(int fd, Collection* out, std::string* error = nullptr) -> typename std::enable_if= sizeof(pid_t), bool>::type { out->clear(); int task_fd = openat(fd, "task", O_DIRECTORY | O_RDONLY | O_CLOEXEC); std::unique_ptr 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::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 -auto GetProcessTids(pid_t pid, Collection* out) -> +auto GetProcessTids(pid_t pid, Collection* out, std::string* error = nullptr) -> typename std::enable_if= 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 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; }