Revert "Remove limit of system property name length"
This reverts commit 5c1ce278f31bc6fc338b9b951f1adc7c2b343d7c.
Bug: http://b/33926793
Bug: http://b/34670529
Change-Id: I0dc4a8ae55576c69b34b2958d8e664f7066b9c54
diff --git a/libc/bionic/system_properties.cpp b/libc/bionic/system_properties.cpp
index da71f09..5aefbaf 100644
--- a/libc/bionic/system_properties.cpp
+++ b/libc/bionic/system_properties.cpp
@@ -59,10 +59,8 @@
#include "private/bionic_sdk_version.h"
#include "private/libc_logging.h"
-#define SERIAL_VALUE_LEN(serial) ((serial) >> 24)
-
static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
-static const char* kServiceVersionPropertyName = "ro.property_service.version";
+
/*
* Properties are stored in a hybrid trie/binary tree structure.
@@ -83,7 +81,8 @@
// Represents a node in the trie.
struct prop_bt {
- uint32_t namelen;
+ uint8_t namelen;
+ uint8_t reserved[3];
// The property trie is updated only by the init process (single threaded) which provides
// property service. And it can be read by multiple threads at the same time.
@@ -108,7 +107,7 @@
char name[0];
- prop_bt(const char *name, const uint32_t name_length) {
+ prop_bt(const char *name, const uint8_t name_length) {
this->namelen = name_length;
memcpy(this->name, name, name_length);
this->name[name_length] = '\0';
@@ -141,9 +140,9 @@
private:
void *allocate_obj(const size_t size, uint_least32_t *const off);
- prop_bt *new_prop_bt(const char *name, uint32_t namelen, uint_least32_t *const off);
- prop_info *new_prop_info(const char *name, uint32_t namelen,
- const char *value, uint32_t valuelen,
+ prop_bt *new_prop_bt(const char *name, uint8_t namelen, uint_least32_t *const off);
+ prop_info *new_prop_info(const char *name, uint8_t namelen,
+ const char *value, uint8_t valuelen,
uint_least32_t *const off);
void *to_prop_obj(uint_least32_t off);
prop_bt *to_prop_bt(atomic_uint_least32_t *off_p);
@@ -152,11 +151,11 @@
prop_bt *root_node();
prop_bt *find_prop_bt(prop_bt *const bt, const char *name,
- uint32_t namelen, bool alloc_if_needed);
+ uint8_t namelen, bool alloc_if_needed);
const prop_info *find_property(prop_bt *const trie, const char *name,
- uint32_t namelen, const char *value,
- uint32_t valuelen, bool alloc_if_needed);
+ uint8_t namelen, const char *value,
+ uint8_t valuelen, bool alloc_if_needed);
bool foreach_property(prop_bt *const trie,
void (*propfn)(const prop_info *pi, void *cookie),
@@ -174,20 +173,19 @@
struct prop_info {
atomic_uint_least32_t serial;
- // we need to keep this buffer around because the property
- // value can be modified whereas name is constant.
char value[PROP_VALUE_MAX];
char name[0];
- prop_info(const char *name, uint32_t namelen, const char *value, uint32_t valuelen) {
+ prop_info(const char *name, const uint8_t namelen, const char *value,
+ const uint8_t valuelen) {
memcpy(this->name, name, namelen);
this->name[namelen] = '\0';
atomic_init(&this->serial, valuelen << 24);
memcpy(this->value, value, valuelen);
this->value[valuelen] = '\0';
}
- private:
- DISALLOW_IMPLICIT_CONSTRUCTORS(prop_info);
+private:
+ DISALLOW_COPY_AND_ASSIGN(prop_info);
};
struct find_nth_cookie {
@@ -360,7 +358,7 @@
return data_ + *off;
}
-prop_bt *prop_area::new_prop_bt(const char *name, uint32_t namelen, uint_least32_t *const off)
+prop_bt *prop_area::new_prop_bt(const char *name, uint8_t namelen, uint_least32_t *const off)
{
uint_least32_t new_offset;
void *const p = allocate_obj(sizeof(prop_bt) + namelen + 1, &new_offset);
@@ -373,8 +371,8 @@
return NULL;
}
-prop_info *prop_area::new_prop_info(const char *name, uint32_t namelen,
- const char *value, uint32_t valuelen, uint_least32_t *const off)
+prop_info *prop_area::new_prop_info(const char *name, uint8_t namelen,
+ const char *value, uint8_t valuelen, uint_least32_t *const off)
{
uint_least32_t new_offset;
void* const p = allocate_obj(sizeof(prop_info) + namelen + 1, &new_offset);
@@ -410,8 +408,8 @@
return reinterpret_cast<prop_bt*>(to_prop_obj(0));
}
-static int cmp_prop_name(const char *one, uint32_t one_len, const char *two,
- uint32_t two_len)
+static int cmp_prop_name(const char *one, uint8_t one_len, const char *two,
+ uint8_t two_len)
{
if (one_len < two_len)
return -1;
@@ -422,7 +420,7 @@
}
prop_bt *prop_area::find_prop_bt(prop_bt *const bt, const char *name,
- uint32_t namelen, bool alloc_if_needed)
+ uint8_t namelen, bool alloc_if_needed)
{
prop_bt* current = bt;
@@ -473,7 +471,7 @@
}
const prop_info *prop_area::find_property(prop_bt *const trie, const char *name,
- uint32_t namelen, const char *value, uint32_t valuelen,
+ uint8_t namelen, const char *value, uint8_t valuelen,
bool alloc_if_needed)
{
if (!trie) return NULL;
@@ -483,7 +481,7 @@
while (true) {
const char *sep = strchr(remaining_name, '.');
const bool want_subtree = (sep != NULL);
- const uint32_t substr_size = (want_subtree) ?
+ const uint8_t substr_size = (want_subtree) ?
sep - remaining_name : strlen(remaining_name);
if (!substr_size) {
@@ -533,93 +531,28 @@
}
}
-class PropertyServiceConnection {
- public:
- PropertyServiceConnection() : last_error_(0) {
- fd_ = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
- if (fd_ == -1) {
- last_error_ = errno;
- return;
+static int send_prop_msg(const prop_msg *msg)
+{
+ const int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ if (fd == -1) {
+ return -1;
}
const size_t namelen = strlen(property_service_socket);
+
sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
strlcpy(addr.sun_path, property_service_socket, sizeof(addr.sun_path));
addr.sun_family = AF_LOCAL;
socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
-
- if (TEMP_FAILURE_RETRY(connect(fd_, reinterpret_cast<sockaddr*>(&addr), alen)) == -1) {
- close(fd_);
- fd_ = -1;
- last_error_ = errno;
+ if (TEMP_FAILURE_RETRY(connect(fd, reinterpret_cast<sockaddr*>(&addr), alen)) < 0) {
+ close(fd);
+ return -1;
}
- }
-
- bool IsValid() {
- return fd_ != -1;
- }
-
- int GetLastError() {
- return last_error_;
- }
-
- bool SendUint32(uint32_t value) {
- int result = TEMP_FAILURE_RETRY(send(fd_, &value, sizeof(value), 0));
- return CheckSendRecvResult(result, sizeof(value));
- }
-
- bool SendString(const char* value) {
- uint32_t valuelen = strlen(value);
- if (!SendUint32(valuelen)) {
- return false;
- }
-
- int result = TEMP_FAILURE_RETRY(send(fd_, value, valuelen, 0));
- return CheckSendRecvResult(result, valuelen);
- }
-
- bool RecvInt32(int32_t* value) {
- int result = TEMP_FAILURE_RETRY(recv(fd_, value, sizeof(*value), MSG_WAITALL));
- return CheckSendRecvResult(result, sizeof(*value));
- }
-
- int GetFd() {
- return fd_;
- }
-
- ~PropertyServiceConnection() {
- if (fd_ != -1) {
- close(fd_);
- }
- }
- private:
- bool CheckSendRecvResult(int result, int expected_len) {
- if (result == -1) {
- last_error_ = errno;
- } else if (result != expected_len) {
- last_error_ = -1;
- } else {
- last_error_ = 0;
- }
-
- return last_error_ == 0;
- }
-
- int fd_;
- int last_error_;
-};
-
-static int send_prop_msg(const prop_msg* msg) {
- PropertyServiceConnection connection;
- if (!connection.IsValid()) {
- return connection.GetLastError();
- }
-
- int result = -1;
- int fd = connection.GetFd();
const int num_bytes = TEMP_FAILURE_RETRY(send(fd, msg, sizeof(prop_msg), 0));
+
+ int result = -1;
if (num_bytes == sizeof(prop_msg)) {
// We successfully wrote to the property server but now we
// wait for the property server to finish its work. It
@@ -645,13 +578,11 @@
// ms so callers who do read-after-write can reliably see
// what they've written. Most of the time.
// TODO: fix the system properties design.
- __libc_format_log(ANDROID_LOG_WARN, "libc",
- "Property service has timed out while trying to set \"%s\" to \"%s\"",
- msg->name, msg->value);
result = 0;
}
}
+ close(fd);
return result;
}
@@ -1193,136 +1124,51 @@
atomic_thread_fence(memory_order_acquire);
if (serial ==
load_const_atomic(&(pi->serial), memory_order_relaxed)) {
- if (name != nullptr) {
- size_t namelen = strlcpy(name, pi->name, PROP_NAME_MAX);
- if(namelen >= PROP_NAME_MAX) {
- __libc_format_log(ANDROID_LOG_ERROR, "libc",
- "The property name length for \"%s\" is >= %d;"
- " please use __system_property_read_callback"
- " to read this property. (the name is truncated to \"%s\")",
- pi->name, PROP_NAME_MAX - 1, name);
- }
+ if (name != 0) {
+ strcpy(name, pi->name);
}
return len;
}
}
}
-void __system_property_read_callback(const prop_info* pi,
- void (*callback)(void* cookie, const char* name, const char* value),
- void* cookie) {
- // TODO (dimitry): do we need compat mode for this function?
- if (__predict_false(compat_mode)) {
- char value_buf[PROP_VALUE_MAX];
- __system_property_read_compat(pi, nullptr, value_buf);
- callback(cookie, pi->name, value_buf);
- return;
- }
-
- while (true) {
- uint32_t serial = __system_property_serial(pi); // acquire semantics
- size_t len = SERIAL_VALUE_LEN(serial);
- char value_buf[len+1];
-
- memcpy(value_buf, pi->value, len);
- value_buf[len] = '\0';
-
- // TODO: see todo in __system_property_read function
- atomic_thread_fence(memory_order_acquire);
- if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
- callback(cookie, pi->name, value_buf);
- return;
- }
- }
-}
-
int __system_property_get(const char *name, char *value)
{
const prop_info *pi = __system_property_find(name);
if (pi != 0) {
- return __system_property_read(pi, nullptr, value);
+ return __system_property_read(pi, 0, value);
} else {
value[0] = 0;
return 0;
}
}
-static constexpr uint32_t kProtocolVersion1 = 1;
-static constexpr uint32_t kProtocolVersion2 = 2; // current
-
-static atomic_uint_least32_t g_propservice_protocol_version = 0;
-
-static void detect_protocol_version() {
- char value[PROP_VALUE_MAX];
- if (__system_property_get(kServiceVersionPropertyName, value) == 0) {
- g_propservice_protocol_version = kProtocolVersion1;
- __libc_format_log(ANDROID_LOG_WARN, "libc",
- "Using old property service protocol (\"%s\" is not set)",
- kServiceVersionPropertyName);
- } else {
- uint32_t version = static_cast<uint32_t>(atoll(value));
- if (version >= kProtocolVersion2) {
- g_propservice_protocol_version = kProtocolVersion2;
- } else {
- __libc_format_log(ANDROID_LOG_WARN, "libc",
- "Using old property service protocol (\"%s\"=\"%s\")",
- kServiceVersionPropertyName, value);
- g_propservice_protocol_version = kProtocolVersion1;
- }
- }
-}
-
-int __system_property_set(const char* key, const char* value) {
- if (key == nullptr) return -1;
- if (value == nullptr) value = "";
+int __system_property_set(const char *key, const char *value)
+{
+ if (key == 0) return -1;
+ if (value == 0) value = "";
+ if (strlen(key) >= PROP_NAME_MAX) return -1;
if (strlen(value) >= PROP_VALUE_MAX) return -1;
- if (g_propservice_protocol_version == 0) {
- detect_protocol_version();
+ prop_msg msg;
+ memset(&msg, 0, sizeof msg);
+ msg.cmd = PROP_MSG_SETPROP;
+ strlcpy(msg.name, key, sizeof msg.name);
+ strlcpy(msg.value, value, sizeof msg.value);
+
+ const int err = send_prop_msg(&msg);
+ if (err < 0) {
+ return err;
}
- int result = -1;
- if (g_propservice_protocol_version == kProtocolVersion1) {
- // Old protocol does not support long names
- if (strlen(key) >= PROP_NAME_MAX) return -1;
-
- prop_msg msg;
- memset(&msg, 0, sizeof msg);
- msg.cmd = PROP_MSG_SETPROP;
- strlcpy(msg.name, key, sizeof msg.name);
- strlcpy(msg.value, value, sizeof msg.value);
-
- result = send_prop_msg(&msg);
- } else {
- // Use proper protocol
- PropertyServiceConnection connection;
- if (connection.IsValid() &&
- connection.SendUint32(PROP_MSG_SETPROP2) &&
- connection.SendString(key) &&
- connection.SendString(value) &&
- connection.RecvInt32(&result)) {
- if (result != PROP_SUCCESS) {
- __libc_format_log(ANDROID_LOG_WARN, "libc",
- "Unable to set property \"%s\" to \"%s\": error code: 0x%x",
- key, value, result);
- }
- } else {
- result = connection.GetLastError();
- __libc_format_log(ANDROID_LOG_WARN, "libc",
- "Unable to set property \"%s\" to \"%s\": error code: 0x%x (%s)",
- key, value, result, strerror(result));
- }
- }
-
- return result != 0 ? -1 : 0;
+ return 0;
}
int __system_property_update(prop_info *pi, const char *value, unsigned int len)
{
- if (len >= PROP_VALUE_MAX) {
+ if (len >= PROP_VALUE_MAX)
return -1;
- }
prop_area* pa = __system_property_area__;
@@ -1337,8 +1183,7 @@
// used memory_order_relaxed atomics, and use the analogous
// counterintuitive fence.
atomic_thread_fence(memory_order_release);
- strlcpy(pi->value, value, len + 1);
-
+ memcpy(pi->value, value, len + 1);
atomic_store_explicit(
&pi->serial,
(len << 24) | ((serial + 1) & 0xffffff),
@@ -1357,13 +1202,12 @@
int __system_property_add(const char *name, unsigned int namelen,
const char *value, unsigned int valuelen)
{
- if (valuelen >= PROP_VALUE_MAX) {
+ if (namelen >= PROP_NAME_MAX)
return -1;
- }
-
- if (namelen < 1) {
+ if (valuelen >= PROP_VALUE_MAX)
return -1;
- }
+ if (namelen < 1)
+ return -1;
if (!__system_property_area__) {
return -1;
@@ -1377,9 +1221,8 @@
}
bool ret = pa->add(name, namelen, value, valuelen);
- if (!ret) {
+ if (!ret)
return -1;
- }
// There is only a single mutator, but we want to make sure that
// updates are visible to a reader waiting for the update.