Remove backwards compatibility for old system properties.

No-one is directly upgrading from pre-K to O...

Also move more implementation details out of the header file.

Bug: http://b/33926793
Test: boots
Change-Id: I7a0936acbb1cea8a3b2cd6797ec53ba7e4a050f3
diff --git a/libc/Android.bp b/libc/Android.bp
index ec00f28..6c2ca17 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -12,7 +12,6 @@
     "bionic/sigblock.c",
     "bionic/siginterrupt.c",
     "bionic/sigsetmask.c",
-    "bionic/system_properties_compat.c",
     "stdio/fread.c",
     "stdio/parsefloat.c",
     "stdio/refill.c",
diff --git a/libc/bionic/system_properties.cpp b/libc/bionic/system_properties.cpp
index 96a4017..d5f48d2 100644
--- a/libc/bionic/system_properties.cpp
+++ b/libc/bionic/system_properties.cpp
@@ -25,6 +25,7 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -59,6 +60,14 @@
 #include "private/bionic_sdk_version.h"
 #include "private/libc_logging.h"
 
+static constexpr int PROP_FILENAME_MAX = 1024;
+
+static constexpr uint32_t PROP_AREA_MAGIC = 0x504f5250;
+static constexpr uint32_t PROP_AREA_VERSION = 0xfc6ed0ab;
+
+static constexpr size_t PA_SIZE = 128 * 1024;
+
+#define SERIAL_DIRTY(serial) ((serial) & 1)
 #define SERIAL_VALUE_LEN(serial) ((serial) >> 24)
 
 static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
@@ -199,31 +208,15 @@
     }
 };
 
+// This is public because it was exposed in the NDK. As of 2017-01, ~60 apps reference this symbol.
+// It's also used in a libnativehelper test.
+prop_area* __system_property_area__ = nullptr;
+
 static char property_filename[PROP_FILENAME_MAX] = PROP_FILENAME;
-static bool compat_mode = false;
 static size_t pa_data_size;
 static size_t pa_size;
 static bool initialized = false;
 
-// NOTE: This isn't static because system_properties_compat.c
-// requires it.
-prop_area *__system_property_area__ = NULL;
-
-static int get_fd_from_env(void)
-{
-    // This environment variable consistes of two decimal integer
-    // values separated by a ",". The first value is a file descriptor
-    // and the second is the size of the system properties area. The
-    // size is currently unused.
-    char *env = getenv("ANDROID_PROPERTY_WORKSPACE");
-
-    if (!env) {
-        return -1;
-    }
-
-    return atoi(env);
-}
-
 static prop_area* map_prop_area_rw(const char* filename, const char* context,
                                    bool* fsetxattr_failed) {
     /* dev is a tmpfs that we can use to carve a shared workspace
@@ -267,7 +260,6 @@
 
     pa_size = PA_SIZE;
     pa_data_size = pa_size - sizeof(prop_area);
-    compat_mode = false;
 
     void *const memory_area = mmap(NULL, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
     if (memory_area == MAP_FAILED) {
@@ -303,47 +295,20 @@
     }
 
     prop_area* pa = reinterpret_cast<prop_area*>(map_result);
-    if ((pa->magic() != PROP_AREA_MAGIC) ||
-        (pa->version() != PROP_AREA_VERSION &&
-         pa->version() != PROP_AREA_VERSION_COMPAT)) {
+    if ((pa->magic() != PROP_AREA_MAGIC) || (pa->version() != PROP_AREA_VERSION)) {
         munmap(pa, pa_size);
         return nullptr;
     }
 
-    if (pa->version() == PROP_AREA_VERSION_COMPAT) {
-        compat_mode = true;
-    }
-
     return pa;
 }
 
-static prop_area* map_prop_area(const char* filename, bool is_legacy) {
+static prop_area* map_prop_area(const char* filename) {
     int fd = open(filename, O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
-    bool close_fd = true;
-    if (fd == -1 && errno == ENOENT && is_legacy) {
-        /*
-         * For backwards compatibility, if the file doesn't
-         * exist, we use the environment to get the file descriptor.
-         * For security reasons, we only use this backup if the kernel
-         * returns ENOENT. We don't want to use the backup if the kernel
-         * returns other errors such as ENOMEM or ENFILE, since it
-         * might be possible for an external program to trigger this
-         * condition.
-         * Only do this for the legacy prop file, secured prop files
-         * do not have a backup
-         */
-        fd = get_fd_from_env();
-        close_fd = false;
-    }
-
-    if (fd < 0) {
-        return nullptr;
-    }
+    if (fd == -1) return nullptr;
 
     prop_area* map_result = map_fd_ro(fd);
-    if (close_fd) {
-        close(fd);
-    }
+    close(fd);
 
     return map_result;
 }
@@ -616,6 +581,12 @@
   int last_error_;
 };
 
+struct prop_msg {
+    unsigned cmd;
+    char name[PROP_NAME_MAX];
+    char value[PROP_VALUE_MAX];
+};
+
 static int send_prop_msg(const prop_msg* msg) {
     PropertyServiceConnection connection;
     if (!connection.IsValid()) {
@@ -839,7 +810,7 @@
     if (access_rw) {
         pa_ = map_prop_area_rw(filename, context_, fsetxattr_failed);
     } else {
-        pa_ = map_prop_area(filename, false);
+        pa_ = map_prop_area(filename);
     }
     lock_.unlock();
     return pa_;
@@ -899,7 +870,7 @@
         __system_property_area__ =
             map_prop_area_rw(filename, "u:object_r:properties_serial:s0", fsetxattr_failed);
     } else {
-        __system_property_area__ = map_prop_area(filename, false);
+        __system_property_area__ = map_prop_area(filename);
     }
     return __system_property_area__;
 }
@@ -1098,7 +1069,7 @@
             return -1;
         }
     } else {
-        __system_property_area__ = map_prop_area(property_filename, true);
+        __system_property_area__ = map_prop_area(property_filename);
         if (!__system_property_area__) {
             return -1;
         }
@@ -1157,10 +1128,6 @@
         return nullptr;
     }
 
-    if (__predict_false(compat_mode)) {
-        return __system_property_find_compat(name);
-    }
-
     prop_area* pa = get_prop_area_for_name(name);
     if (!pa) {
         __libc_format_log(ANDROID_LOG_ERROR, "libc", "Access denied finding property \"%s\"", name);
@@ -1178,12 +1145,7 @@
     return atomic_load_explicit(non_const_s, mo);
 }
 
-int __system_property_read(const prop_info *pi, char *name, char *value)
-{
-    if (__predict_false(compat_mode)) {
-        return __system_property_read_compat(pi, name, value);
-    }
-
+int __system_property_read(const prop_info *pi, char *name, char *value) {
     while (true) {
         uint32_t serial = __system_property_serial(pi); // acquire semantics
         size_t len = SERIAL_VALUE_LEN(serial);
@@ -1217,14 +1179,6 @@
 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);
@@ -1444,17 +1398,12 @@
     return cookie.pi;
 }
 
-int __system_property_foreach(void (*propfn)(const prop_info *pi, void *cookie),
-        void *cookie)
+int __system_property_foreach(void (*propfn)(const prop_info *pi, void *cookie), void *cookie)
 {
     if (!__system_property_area__) {
         return -1;
     }
 
-    if (__predict_false(compat_mode)) {
-        return __system_property_foreach_compat(propfn, cookie);
-    }
-
     list_foreach(contexts, [propfn, cookie](context_node* l) {
         if (l->check_access_and_open()) {
             l->pa()->foreach(propfn, cookie);
diff --git a/libc/bionic/system_properties_compat.c b/libc/bionic/system_properties_compat.c
deleted file mode 100644
index ba73a2c..0000000
--- a/libc/bionic/system_properties_compat.c
+++ /dev/null
@@ -1,134 +0,0 @@
-/*
- * 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 <string.h>
-
-#include "private/bionic_futex.h"
-
-#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
-#include <sys/_system_properties.h>
-
-#define TOC_NAME_LEN(toc)       ((toc) >> 24)
-#define TOC_TO_INFO(area, toc)  ((prop_info_compat*) (((char*) area) + ((toc) & 0xFFFFFF)))
-#define SERIAL_VALUE_LEN(serial) ((serial) >> 24)
-
-struct prop_area_compat {
-    unsigned volatile count;
-    unsigned volatile serial;
-    unsigned magic;
-    unsigned version;
-    unsigned reserved[4];
-    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__;
-
-__LIBC_HIDDEN__ 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;
-}
-
-__LIBC_HIDDEN__ 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, NULL);
-            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;
-        }
-    }
-}
-
-__LIBC_HIDDEN__ 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 080280e..ffa6d2e 100644
--- a/libc/include/sys/_system_properties.h
+++ b/libc/include/sys/_system_properties.h
@@ -33,31 +33,14 @@
 
 #ifndef _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
 #error you should #include <sys/system_properties.h> instead
-#else
+#endif
+
 #include <sys/system_properties.h>
 
-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_MAX 1024
-#define PROP_FILENAME "/dev/__properties__"
-
-#define PA_SIZE         (128 * 1024)
-
-#define SERIAL_DIRTY(serial) ((serial) & 1)
-
 __BEGIN_DECLS
 
-struct prop_msg
-{
-    unsigned cmd;
-    char name[PROP_NAME_MAX];
-    char value[PROP_VALUE_MAX];
-};
+#define PROP_SERVICE_NAME "property_service"
+#define PROP_FILENAME "/dev/__properties__"
 
 #define PROP_MSG_SETPROP 1
 #define PROP_MSG_SETPROP2 0x00020001
@@ -74,34 +57,6 @@
 #define PROP_ERROR_SET_FAILED 0x0024
 
 /*
-** Rules:
-**
-** - there is only one writer, but many readers
-** - prop_area.count will never decrease in value
-** - once allocated, a prop_info's name will not change
-** - once allocated, a prop_info's offset will not change
-** - reading a value requires the following steps
-**   1. serial = pi->serial
-**   2. if SERIAL_DIRTY(serial), wait*, then goto 1
-**   3. memcpy(local, pi->value, SERIAL_VALUE_LEN(serial) + 1)
-**   4. if pi->serial != serial, goto 2
-**
-** - writing a value requires the following steps
-**   1. pi->serial = pi->serial | 1
-**   2. memcpy(pi->value, local_value, value_len)
-**   3. pi->serial = (value_len << 24) | ((pi->serial + 1) & 0xffffff)
-*/
-
-#define PROP_PATH_RAMDISK_DEFAULT  "/default.prop"
-#define PROP_PATH_VENDOR_DEFAULT   "/vendor/default.prop"
-#define PROP_PATH_ODM_DEFAULT      "/odm/default.prop"
-#define PROP_PATH_SYSTEM_BUILD     "/system/build.prop"
-#define PROP_PATH_VENDOR_BUILD     "/vendor/build.prop"
-#define PROP_PATH_ODM_BUILD        "/odm/build.prop"
-#define PROP_PATH_LOCAL_OVERRIDE   "/data/local.prop"
-#define PROP_PATH_FACTORY          "/factory/factory.prop"
-
-/*
 ** Map the property area from the specified filename.  This
 ** method is for testing only.
 */
@@ -146,8 +101,7 @@
 **
 ** Returns 0 on success, -1 if the property area is full.
 */
-int __system_property_add(const char *name, unsigned int namelen,
-			const char *value, unsigned int valuelen);
+int __system_property_add(const char *name, unsigned int namelen, const char *value, unsigned int valuelen);
 
 /* Update the value of a system property returned by
 ** __system_property_find.  Can only be done by a single process
@@ -171,15 +125,6 @@
 ** 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);
-
 /* Initialize the system properties area in read only mode.
  * Should be done by all processes that need to read system
  * properties.
@@ -191,4 +136,3 @@
 __END_DECLS
 
 #endif
-#endif