Skip to content

Commit 220c1d5

Browse files
yabincGerrit Code Review
authored andcommitted
Merge "libprocinfo: return error msg instead of printing it."
2 parents 24f226d + 5f036ab commit 220c1d5

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

libprocinfo/include/procinfo/process.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,25 @@ struct ProcessInfo {
5656
};
5757

5858
// Parse the contents of /proc/<tid>/status into |process_info|.
59-
bool GetProcessInfo(pid_t tid, ProcessInfo* process_info);
59+
bool GetProcessInfo(pid_t tid, ProcessInfo* process_info, std::string* error = nullptr);
6060

6161
// Parse the contents of <fd>/status into |process_info|.
6262
// |fd| should be an fd pointing at a /proc/<pid> directory.
63-
bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info);
63+
bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info, std::string* error = nullptr);
6464

6565
// Fetch the list of threads from a given process's /proc/<pid> directory.
6666
// |fd| should be an fd pointing at a /proc/<pid> directory.
6767
template <typename Collection>
68-
auto GetProcessTidsFromProcPidFd(int fd, Collection* out) ->
68+
auto GetProcessTidsFromProcPidFd(int fd, Collection* out, std::string* error = nullptr) ->
6969
typename std::enable_if<sizeof(typename Collection::value_type) >= sizeof(pid_t), bool>::type {
7070
out->clear();
7171

7272
int task_fd = openat(fd, "task", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
7373
std::unique_ptr<DIR, int (*)(DIR*)> dir(fdopendir(task_fd), closedir);
7474
if (!dir) {
75-
PLOG(ERROR) << "failed to open task directory";
75+
if (error != nullptr) {
76+
*error = "failed to open task directory";
77+
}
7678
return false;
7779
}
7880

@@ -81,7 +83,9 @@ auto GetProcessTidsFromProcPidFd(int fd, Collection* out) ->
8183
if (strcmp(dent->d_name, ".") != 0 && strcmp(dent->d_name, "..") != 0) {
8284
pid_t tid;
8385
if (!android::base::ParseInt(dent->d_name, &tid, 1, std::numeric_limits<pid_t>::max())) {
84-
LOG(ERROR) << "failed to parse task id: " << dent->d_name;
86+
if (error != nullptr) {
87+
*error = std::string("failed to parse task id: ") + dent->d_name;
88+
}
8589
return false;
8690
}
8791

@@ -93,21 +97,25 @@ auto GetProcessTidsFromProcPidFd(int fd, Collection* out) ->
9397
}
9498

9599
template <typename Collection>
96-
auto GetProcessTids(pid_t pid, Collection* out) ->
100+
auto GetProcessTids(pid_t pid, Collection* out, std::string* error = nullptr) ->
97101
typename std::enable_if<sizeof(typename Collection::value_type) >= sizeof(pid_t), bool>::type {
98102
char task_path[PATH_MAX];
99103
if (snprintf(task_path, PATH_MAX, "/proc/%d", pid) >= PATH_MAX) {
100-
LOG(ERROR) << "task path overflow (pid = " << pid << ")";
104+
if (error != nullptr) {
105+
*error = "task path overflow (pid = " + std::to_string(pid) + ")";
106+
}
101107
return false;
102108
}
103109

104110
android::base::unique_fd fd(open(task_path, O_DIRECTORY | O_RDONLY | O_CLOEXEC));
105111
if (fd == -1) {
106-
PLOG(ERROR) << "failed to open " << task_path;
112+
if (error != nullptr) {
113+
*error = std::string("failed to open ") + task_path;
114+
}
107115
return false;
108116
}
109117

110-
return GetProcessTidsFromProcPidFd(fd.get(), out);
118+
return GetProcessTidsFromProcPidFd(fd.get(), out, error);
111119
}
112120

113121
#endif

libprocinfo/process.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,19 @@ using android::base::unique_fd;
3131
namespace android {
3232
namespace procinfo {
3333

34-
bool GetProcessInfo(pid_t tid, ProcessInfo* process_info) {
34+
bool GetProcessInfo(pid_t tid, ProcessInfo* process_info, std::string* error) {
3535
char path[PATH_MAX];
3636
snprintf(path, sizeof(path), "/proc/%d", tid);
3737

3838
unique_fd dirfd(open(path, O_DIRECTORY | O_RDONLY));
3939
if (dirfd == -1) {
40-
PLOG(ERROR) << "failed to open " << path;
40+
if (error != nullptr) {
41+
*error = std::string("failed to open ") + path;
42+
}
4143
return false;
4244
}
4345

44-
return GetProcessInfoFromProcPidFd(dirfd.get(), process_info);
46+
return GetProcessInfoFromProcPidFd(dirfd.get(), process_info, error);
4547
}
4648

4749
static ProcessState parse_state(const char* state) {
@@ -62,17 +64,21 @@ static ProcessState parse_state(const char* state) {
6264
}
6365
}
6466

65-
bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info) {
67+
bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info, std::string* error) {
6668
int status_fd = openat(fd, "status", O_RDONLY | O_CLOEXEC);
6769

6870
if (status_fd == -1) {
69-
PLOG(ERROR) << "failed to open status fd in GetProcessInfoFromProcPidFd";
71+
if (error != nullptr) {
72+
*error = "failed to open status fd in GetProcessInfoFromProcPidFd";
73+
}
7074
return false;
7175
}
7276

7377
std::unique_ptr<FILE, decltype(&fclose)> fp(fdopen(status_fd, "r"), fclose);
7478
if (!fp) {
75-
PLOG(ERROR) << "failed to open status file in GetProcessInfoFromProcPidFd";
79+
if (error != nullptr) {
80+
*error = "failed to open status file in GetProcessInfoFromProcPidFd";
81+
}
7682
close(status_fd);
7783
return false;
7884
}

0 commit comments

Comments
 (0)