OSDN Git Service

Fix fd ownership issue in PrintNativeInfo/GetNativeInfo.
authorElliott Hughes <enh@google.com>
Mon, 7 Aug 2017 16:30:37 +0000 (09:30 -0700)
committerElliott Hughes <enh@google.com>
Mon, 7 Aug 2017 16:30:37 +0000 (09:30 -0700)
Bug: N/A
Test: ran tests
Change-Id: I10bdeeb38404583560685bf47c58f6770fc9050b
Signed-off-by: Ivan Maidanski <i.maidanski@samsung.com>
memory_replay/Android.mk
memory_replay/NativeInfo.cpp

index 2b299ad..b72f006 100644 (file)
@@ -17,6 +17,7 @@ LOCAL_MODULE := memory_replay
 LOCAL_MULTILIB := both
 LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
 LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64
+LOCAL_SHARED_LIBRARIES := libbase
 include $(BUILD_EXECUTABLE)
 
 include $(CLEAR_VARS)
@@ -27,6 +28,7 @@ LOCAL_MODULE_TAGS := debug
 LOCAL_MODULE := memory_replay
 LOCAL_MODULE_HOST_OS := linux
 LOCAL_LDLIBS := -lrt
+LOCAL_SHARED_LIBRARIES := libbase
 include $(BUILD_HOST_EXECUTABLE)
 
 memory_replay_test_src_files := \
index b88eaf6..18c832b 100644 (file)
@@ -25,6 +25,8 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+#include <android-base/unique_fd.h>
+
 #include "LineBuffer.h"
 #include "NativeInfo.h"
 
@@ -55,7 +57,6 @@ void GetNativeInfo(int smaps_fd, size_t* pss_bytes, size_t* va_bytes) {
       total_pss_bytes += native_pss_kB * 1024;
     }
   }
-  close(smaps_fd);
   *pss_bytes = total_pss_bytes;
   *va_bytes = total_va_bytes;
 }
@@ -64,7 +65,7 @@ void PrintNativeInfo(const char* preamble) {
   size_t pss_bytes;
   size_t va_bytes;
 
-  int smaps_fd = open("/proc/self/smaps", O_RDONLY);
+  android::base::unique_fd smaps_fd(open("/proc/self/smaps", O_RDONLY));
   if (smaps_fd == -1) {
     err(1, "Cannot open /proc/self/smaps: %s\n", strerror(errno));
   }
@@ -73,6 +74,4 @@ void PrintNativeInfo(const char* preamble) {
   printf("%sNative PSS: %zu bytes %0.2fMB\n", preamble, pss_bytes, pss_bytes/(1024*1024.0));
   printf("%sNative VA Space: %zu bytes %0.2fMB\n", preamble, va_bytes, va_bytes/(1024*1024.0));
   fflush(stdout);
-
-  close(smaps_fd);
 }