From 1ec20a086cb1da1d7455a36ea5d2ea4856426ea0 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Mon, 24 Jun 2013 18:42:21 -0700 Subject: [PATCH 1/2] bionic: use the size of the file to determine property area size On the reader size, don't assume that the property size is PA_SIZE, read it from the size of the file. Allows init to use a different property size without recompiling statically linked executables. (cherry picked from commit 285b42a04cbe8e627a75c9bfb3c7cb3f7b539267) Change-Id: I074204e9e6591b35faf7c1c58fb11ec162aff7bf --- libc/bionic/system_properties.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/libc/bionic/system_properties.c b/libc/bionic/system_properties.c index 481e6ae45..0d421956e 100644 --- a/libc/bionic/system_properties.c +++ b/libc/bionic/system_properties.c @@ -112,7 +112,8 @@ static char property_filename[PATH_MAX] = PROP_FILENAME; prop_area *__system_property_area__ = NULL; -const size_t PA_DATA_SIZE = PA_SIZE - sizeof(prop_area); +size_t pa_data_size; +size_t pa_size; static int get_fd_from_env(void) { @@ -153,11 +154,14 @@ static int map_prop_area_rw() if (ftruncate(fd, PA_SIZE) < 0) goto out; - pa = mmap(NULL, PA_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + pa_size = PA_SIZE; + pa_data_size = pa_size - sizeof(prop_area); + + pa = mmap(NULL, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(pa == MAP_FAILED) goto out; - memset(pa, 0, PA_SIZE); + memset(pa, 0, pa_size); pa->magic = PROP_AREA_MAGIC; pa->version = PROP_AREA_VERSION; /* reserve root node */ @@ -230,18 +234,20 @@ static int map_prop_area() if ((fd_stat.st_uid != 0) || (fd_stat.st_gid != 0) || ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0) - || (fd_stat.st_size < PA_SIZE) ) { + || (fd_stat.st_size < sizeof(prop_area)) ) { goto cleanup; } - prop_area *pa = mmap(NULL, PA_SIZE, PROT_READ, MAP_SHARED, fd, 0); + pa_size = fd_stat.st_size; + pa_data_size = pa_size - sizeof(prop_area); + prop_area *pa = mmap(NULL, pa_size, PROT_READ, MAP_SHARED, fd, 0); if (pa == MAP_FAILED) { goto cleanup; } if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION)) { - munmap(pa, PA_SIZE); + munmap(pa, pa_size); goto cleanup; } @@ -267,7 +273,7 @@ static void *new_prop_obj(size_t size, prop_off_t *off) prop_area *pa = __system_property_area__; size = ALIGN(size, sizeof(uint32_t)); - if (pa->bytes_used + size > PA_DATA_SIZE) + if (pa->bytes_used + size > pa_data_size) return NULL; *off = pa->bytes_used; @@ -310,7 +316,7 @@ static prop_info *new_prop_info(const char *name, uint8_t namelen, static void *to_prop_obj(prop_off_t off) { - if (off > PA_DATA_SIZE) + if (off > pa_data_size) return NULL; return __system_property_area__->data + off; From 5e9a086145764d64c0f7fbce5489d820aa7fdcd4 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Mon, 24 Jun 2013 18:36:39 -0700 Subject: [PATCH 2/2] bionic: add compatibility mode for properties Allow a new bionic to work with an old init property area by supporting the old format. (cherry picked from commit ad76c85b9ca587084089d086f954158bc0eae905) Change-Id: Ib496e818a62a5834d40c71eb4745783d998be893 --- libc/Android.mk | 1 + libc/bionic/system_properties.c | 19 +++- libc/bionic/system_properties_compat.c | 131 +++++++++++++++++++++++++ libc/include/sys/_system_properties.h | 10 ++ 4 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 libc/bionic/system_properties_compat.c diff --git a/libc/Android.mk b/libc/Android.mk index b239b745a..eba0a00f0 100644 --- a/libc/Android.mk +++ b/libc/Android.mk @@ -149,6 +149,7 @@ libc_common_src_files := \ bionic/strntoumax.c \ bionic/strtotimeval.c \ bionic/system_properties.c \ + bionic/system_properties_compat.c \ bionic/tcgetpgrp.c \ bionic/tcsetpgrp.c \ bionic/thread_atexit.c \ diff --git a/libc/bionic/system_properties.c b/libc/bionic/system_properties.c index 0d421956e..4c2e5a21f 100644 --- a/libc/bionic/system_properties.c +++ b/libc/bionic/system_properties.c @@ -109,6 +109,7 @@ typedef struct prop_bt prop_bt; static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME; static char property_filename[PATH_MAX] = PROP_FILENAME; +static bool compat_mode = false; prop_area *__system_property_area__ = NULL; @@ -156,6 +157,7 @@ static int map_prop_area_rw() pa_size = PA_SIZE; pa_data_size = pa_size - sizeof(prop_area); + compat_mode = false; pa = mmap(NULL, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(pa == MAP_FAILED) @@ -246,11 +248,16 @@ static int map_prop_area() goto cleanup; } - if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION)) { + if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION && + pa->version != PROP_AREA_VERSION_COMPAT)) { munmap(pa, pa_size); goto cleanup; } + if (pa->version == PROP_AREA_VERSION_COMPAT) { + compat_mode = true; + } + result = 0; __system_property_area__ = pa; @@ -425,6 +432,9 @@ static const prop_info *find_property(prop_bt *trie, const char *name, const prop_info *__system_property_find(const char *name) { + if (__predict_false(compat_mode)) { + return __system_property_find_compat(name); + } return find_property(root_node(), name, strlen(name), NULL, 0, false); } @@ -432,6 +442,10 @@ int __system_property_read(const prop_info *pi, char *name, char *value) { unsigned serial, len; + if (__predict_false(compat_mode)) { + return __system_property_read_compat(pi, name, value); + } + for(;;) { serial = pi->serial; while(SERIAL_DIRTY(serial)) { @@ -688,5 +702,8 @@ static int foreach_property(prop_off_t off, int __system_property_foreach(void (*propfn)(const prop_info *pi, void *cookie), void *cookie) { + if (__predict_false(compat_mode)) { + return __system_property_foreach_compat(propfn, cookie); + } return foreach_property(0, propfn, cookie); } diff --git a/libc/bionic/system_properties_compat.c b/libc/bionic/system_properties_compat.c new file mode 100644 index 000000000..6dbc4ccea --- /dev/null +++ b/libc/bionic/system_properties_compat.c @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * This file is only used to provide backwards compatibility to property areas + * created by old versions of init, which occurs when an ota runs. The updater + * binary is compiled statically against the newest bionic, but the recovery + * ramdisk may be using an old version of init. This can all be removed once + * OTAs from pre-K versions are no longer supported. + */ + +#include +#include + +#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ +#include + +#define TOC_NAME_LEN(toc) ((toc) >> 24) +#define TOC_TO_INFO(area, toc) ((prop_info_compat*) (((char*) area) + ((toc) & 0xFFFFFF))) + +struct prop_area_compat { + unsigned volatile count; + unsigned volatile serial; + unsigned magic; + unsigned version; + unsigned toc[1]; +}; + +typedef struct prop_area_compat prop_area_compat; + +struct prop_area; +typedef struct prop_area prop_area; + +struct prop_info_compat { + char name[PROP_NAME_MAX]; + unsigned volatile serial; + char value[PROP_VALUE_MAX]; +}; + +typedef struct prop_info_compat prop_info_compat; + +extern prop_area *__system_property_area__; + +const prop_info *__system_property_find_compat(const char *name) +{ + prop_area_compat *pa = (prop_area_compat *)__system_property_area__; + unsigned count = pa->count; + unsigned *toc = pa->toc; + unsigned len = strlen(name); + prop_info_compat *pi; + + if (len >= PROP_NAME_MAX) + return 0; + if (len < 1) + return 0; + + while(count--) { + unsigned entry = *toc++; + if(TOC_NAME_LEN(entry) != len) continue; + + pi = TOC_TO_INFO(pa, entry); + if(memcmp(name, pi->name, len)) continue; + + return (const prop_info *)pi; + } + + return 0; +} + +int __system_property_read_compat(const prop_info *_pi, char *name, char *value) +{ + unsigned serial, len; + const prop_info_compat *pi = (const prop_info_compat *)_pi; + + for(;;) { + serial = pi->serial; + while(SERIAL_DIRTY(serial)) { + __futex_wait((volatile void *)&pi->serial, serial, 0); + serial = pi->serial; + } + len = SERIAL_VALUE_LEN(serial); + memcpy(value, pi->value, len + 1); + if(serial == pi->serial) { + if(name != 0) { + strcpy(name, pi->name); + } + return len; + } + } +} + +int __system_property_foreach_compat( + void (*propfn)(const prop_info *pi, void *cookie), + void *cookie) +{ + prop_area_compat *pa = (prop_area_compat *)__system_property_area__; + unsigned i; + + for (i = 0; i < pa->count; i++) { + unsigned entry = pa->toc[i]; + prop_info_compat *pi = TOC_TO_INFO(pa, entry); + propfn((const prop_info *)pi, cookie); + } + + return 0; +} diff --git a/libc/include/sys/_system_properties.h b/libc/include/sys/_system_properties.h index 92e35e124..5eee7f097 100644 --- a/libc/include/sys/_system_properties.h +++ b/libc/include/sys/_system_properties.h @@ -38,6 +38,7 @@ typedef struct prop_msg prop_msg; #define PROP_AREA_MAGIC 0x504f5250 #define PROP_AREA_VERSION 0xfc6ed0ab +#define PROP_AREA_VERSION_COMPAT 0x45434f76 #define PROP_SERVICE_NAME "property_service" #define PROP_FILENAME "/dev/__properties__" @@ -129,6 +130,15 @@ unsigned int __system_property_serial(const prop_info *pi); ** successive call. */ unsigned int __system_property_wait_any(unsigned int serial); +/* Compatibility functions to support using an old init with a new libc, + ** mostly for the OTA updater binary. These can be deleted once OTAs from + ** a pre-K release no longer needed to be supported. */ +const prop_info *__system_property_find_compat(const char *name); +int __system_property_read_compat(const prop_info *pi, char *name, char *value); +int __system_property_foreach_compat( + void (*propfn)(const prop_info *pi, void *cookie), + void *cookie); + __END_DECLS #endif