ehds commented on code in PR #3602:
URL: https://github.com/apache/brpc/pull/3602#discussion_r4267141042
##########
src/bvar/default_variables.cpp:
##########
@@ -350,29 +399,19 @@ static int get_fd_count(int limit) {
for (; dr.Next() && count <= limit + 3; ++count) {}
return count - 3 /* skipped ., .. and the fd in dr*/;
#elif defined(OS_MACOSX)
- // TODO(zhujiashun): following code will cause core dump with some
- // probability under mac when program exits. Fix it.
- /*
- static pid_t pid = getpid();
- std::ostringstream oss;
- char cmdbuf[128];
- snprintf(cmdbuf, sizeof(cmdbuf),
- "lsof -p %ld | grep -v \"txt\" | wc -l", (long)pid);
- if (butil::read_command_output(oss, cmdbuf) != 0) {
- LOG(ERROR) << "Fail to read open files";
- return -1;
+ if (limit <= 0) {
+ return 0;
}
- const std::string& result = oss.str();
- int count = 0;
- if (sscanf(result.c_str(), "%d", &count) != 1) {
- PLOG(WARNING) << "Fail to sscanf";
+ std::vector<proc_fdinfo> fds(limit);
+ errno = 0;
+ // This query opens no additional FD.
+ const int bytes = proc_pidinfo(getpid(), PROC_PIDLISTFDS, 0,
+ fds.data(), fds.size() *
sizeof(proc_fdinfo));
+ if (bytes < 0 || (bytes == 0 && errno != 0)) {
+ PLOG(WARNING) << "Fail to list own file descriptors";
return -1;
}
- // skipped . and first column line
- count = count - 2;
- return std::min(count, limit);
- */
- return 0;
+ return bytes / sizeof(proc_fdinfo);
Review Comment:
Added validation that the returned byte count is a multiple of
sizeof(proc_fdinfo) and does not exceed the buffer size. Unexpected lengths are
logged and return -1, allowing the reader to retain the last valid count.
Since the buffer holds exactly limit entries, a validated result cannot
exceed limit, so an additional clamp is unnecessary.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]