OSDN Git Service

Do not send 0 byte string content.
authorDimitry Ivanov <dimitry@google.com>
Tue, 24 Jan 2017 20:39:33 +0000 (12:39 -0800)
committerDimitry Ivanov <dimitry@google.com>
Tue, 24 Jan 2017 22:19:03 +0000 (14:19 -0800)
Trying to send even 0 bytes to closed socket leads to
broken pipe error. Sometimes property service is just
quick enough and closes the socket between send(valuelen)
and send(value) in the case where valuelen is 0.

Bug: http://b/34670529
Test: adb reboot 20 times and make sure phone service did not fail
Test: run bionic-unit-tests --gtest_filter=prop*
Change-Id: I96f90ca6fe1790614e7efd3015bffed1ef1e9040

libc/bionic/system_properties.cpp
tests/system_properties_test2.cpp

index da71f09..96a4017 100644 (file)
@@ -575,6 +575,12 @@ class PropertyServiceConnection {
       return false;
     }
 
+    // Trying to send even 0 bytes to closed socket may lead to
+    // broken pipe (http://b/34670529).
+    if (valuelen == 0) {
+      return true;
+    }
+
     int result = TEMP_FAILURE_RETRY(send(fd_, value, valuelen, 0));
     return CheckSendRecvResult(result, valuelen);
   }
index 858bc17..0560960 100644 (file)
@@ -126,3 +126,21 @@ TEST(properties, smoke) {
 #endif // __BIONIC__
 }
 
+TEST(properties, empty_value) {
+#if defined(__BIONIC__)
+    char propvalue[PROP_VALUE_MAX];
+
+    std::stringstream ss;
+    ss << "debug.test." << getpid() << "." << NanoTime() << "." << "property_empty";
+    const std::string property_name = ss.str();
+
+    for (size_t i=0; i<1000; ++i) {
+      ASSERT_EQ(0, __system_property_set(property_name.c_str(), ""));
+      ASSERT_EQ(0, __system_property_get(property_name.c_str(), propvalue));
+      ASSERT_STREQ("", propvalue);
+    }
+
+#else // __BIONIC__
+    GTEST_LOG_(INFO) << "This test does nothing.\n";
+#endif // __BIONIC__
+}