dumpstate: always include last ANR as a separate entry.
For consistency, as well as to make tooling easier.
Most of this change is an extended yak-shave to try and close DumpData
FDs in a consistent manner. Several layers of abstraction were assuming
that it was their job to close input FDs, leading to double closes in some
cases. These double closes are benign prior to this change to dump a
given DumpData twice, given that dumpstate is single threaded.
Bug: 68188758
Test: adb bugreport
(cherry picked from commit 6b9516ced325e93880e9c8ef2793380866a1c579)
Merged-In: I6302f4cb553139c0c26defb4859c4eca5b18ec93
Change-Id: I54ba5e11135b6aefa97d95b287e00325b5867c8a
diff --git a/cmds/dumpstate/utils.cpp b/cmds/dumpstate/utils.cpp
index 93f4c22..a96a69d 100644
--- a/cmds/dumpstate/utils.cpp
+++ b/cmds/dumpstate/utils.cpp
@@ -628,7 +628,7 @@
struct dirent *d;
char *newpath = NULL;
const char *slash = "/";
- int fd, retval = 0;
+ int retval = 0;
if (!title.empty()) {
printf("------ %s (%s) ------\n", title.c_str(), dir);
@@ -670,13 +670,13 @@
}
continue;
}
- fd = TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
- if (fd < 0) {
- retval = fd;
+ android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC)));
+ if (fd.get() < 0) {
+ retval = -1;
printf("*** %s: %s\n", newpath, strerror(errno));
continue;
}
- (*dump_from_fd)(NULL, newpath, fd);
+ (*dump_from_fd)(NULL, newpath, fd.get());
}
closedir(dirp);
if (!title.empty()) {
@@ -695,11 +695,9 @@
int flags = fcntl(fd, F_GETFL);
if (flags == -1) {
printf("*** %s: failed to get flags on fd %d: %s\n", path, fd, strerror(errno));
- close(fd);
return -1;
} else if (!(flags & O_NONBLOCK)) {
printf("*** %s: fd must have O_NONBLOCK set.\n", path);
- close(fd);
return -1;
}
return DumpFileFromFdToFd(title, path, fd, STDOUT_FILENO, PropertiesHelper::IsDryRun());