Merge "Revert "liblog: Add liblog test suite""
diff --git a/adb/Android.mk b/adb/Android.mk
index b616256..62f012c 100644
--- a/adb/Android.mk
+++ b/adb/Android.mk
@@ -129,7 +129,7 @@
 LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT_SBIN)
 LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)
 
-LOCAL_STATIC_LIBRARIES := liblog libcutils libc libmincrypt
+LOCAL_STATIC_LIBRARIES := liblog libcutils libc libmincrypt libselinux
 include $(BUILD_EXECUTABLE)
 
 
diff --git a/adb/file_sync_service.c b/adb/file_sync_service.c
index 0bb51f9..c2401b8 100644
--- a/adb/file_sync_service.c
+++ b/adb/file_sync_service.c
@@ -22,19 +22,32 @@
 #include <sys/types.h>
 #include <dirent.h>
 #include <utime.h>
+#include <unistd.h>
 
 #include <errno.h>
-
+#include <private/android_filesystem_config.h>
+#include <selinux/android.h>
 #include "sysdeps.h"
 
 #define TRACE_TAG  TRACE_SYNC
 #include "adb.h"
 #include "file_sync_service.h"
 
+/* TODO: use fs_config to configure permissions on /data */
+static bool is_on_system(const char *name) {
+    const char *SYSTEM = "/system/";
+    return (strncmp(SYSTEM, name, strlen(SYSTEM)) == 0);
+}
+
 static int mkdirs(char *name)
 {
     int ret;
     char *x = name + 1;
+    unsigned int uid, gid;
+    unsigned int mode = 0775;
+    uint64_t cap = 0;
+    uid = getuid();
+    gid = getgid();
 
     if(name[0] != '/') return -1;
 
@@ -42,11 +55,21 @@
         x = adb_dirstart(x);
         if(x == 0) return 0;
         *x = 0;
-        ret = adb_mkdir(name, 0775);
+        if (is_on_system(name)) {
+            fs_config(name, 1, &uid, &gid, &mode, &cap);
+        }
+        ret = adb_mkdir(name, mode);
         if((ret < 0) && (errno != EEXIST)) {
             D("mkdir(\"%s\") -> %s\n", name, strerror(errno));
             *x = '/';
             return ret;
+        } else if(ret == 0) {
+            ret = chown(name, uid, gid);
+            if (ret < 0) {
+                *x = '/';
+                return ret;
+            }
+            selinux_android_restorecon(name);
         }
         *x++ = '/';
     }
@@ -149,7 +172,8 @@
     return fail_message(s, strerror(errno));
 }
 
-static int handle_send_file(int s, char *path, mode_t mode, char *buffer)
+static int handle_send_file(int s, char *path, unsigned int uid,
+        unsigned int gid, mode_t mode, char *buffer)
 {
     syncmsg msg;
     unsigned int timestamp = 0;
@@ -157,8 +181,13 @@
 
     fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
     if(fd < 0 && errno == ENOENT) {
-        mkdirs(path);
-        fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
+        if(mkdirs(path) != 0) {
+            if(fail_errno(s))
+                return -1;
+            fd = -1;
+        } else {
+            fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
+        }
     }
     if(fd < 0 && errno == EEXIST) {
         fd = adb_open_mode(path, O_WRONLY, mode);
@@ -167,6 +196,16 @@
         if(fail_errno(s))
             return -1;
         fd = -1;
+    } else {
+        if(fchown(fd, uid, gid) != 0) {
+            fail_errno(s);
+            errno = 0;
+        }
+        /* fchown clears the setuid bit - restore it if present */
+        if(fchmod(fd, mode) != 0) {
+            fail_errno(s);
+            errno = 0;
+        }
     }
 
     for(;;) {
@@ -206,6 +245,7 @@
     if(fd >= 0) {
         struct utimbuf u;
         adb_close(fd);
+        selinux_android_restorecon(path);
         u.actime = timestamp;
         u.modtime = timestamp;
         utime(path, &u);
@@ -249,7 +289,10 @@
 
     ret = symlink(buffer, path);
     if(ret && errno == ENOENT) {
-        mkdirs(path);
+        if(mkdirs(path) != 0) {
+            fail_errno(s);
+            return -1;
+        }
         ret = symlink(buffer, path);
     }
     if(ret) {
@@ -277,7 +320,7 @@
 static int do_send(int s, char *path, char *buffer)
 {
     char *tmp;
-    mode_t mode;
+    unsigned int mode;
     int is_link, ret;
 
     tmp = strrchr(path,',');
@@ -288,7 +331,7 @@
 #ifndef HAVE_SYMLINKS
         is_link = 0;
 #else
-        is_link = S_ISLNK(mode);
+        is_link = S_ISLNK((mode_t) mode);
 #endif
         mode &= 0777;
     }
@@ -310,11 +353,23 @@
 #else
     {
 #endif
+        unsigned int uid, gid;
+        uint64_t cap = 0;
+        uid = getuid();
+        gid = getgid();
+
         /* copy user permission bits to "group" and "other" permissions */
         mode |= ((mode >> 3) & 0070);
         mode |= ((mode >> 3) & 0007);
 
-        ret = handle_send_file(s, path, mode, buffer);
+        tmp = path;
+        if(*tmp == '/') {
+            tmp++;
+        }
+        if (is_on_system(path)) {
+            fs_config(tmp, 0, &uid, &gid, &mode, &cap);
+        }
+        ret = handle_send_file(s, path, uid, gid, mode, buffer);
     }
 
     return ret;
diff --git a/adb/usb_vendors.c b/adb/usb_vendors.c
old mode 100644
new mode 100755
index 19b3022..b39bcc0
--- a/adb/usb_vendors.c
+++ b/adb/usb_vendors.c
@@ -140,7 +140,7 @@
 // Xiaomi's USB Vendor ID
 #define VENDOR_ID_XIAOMI        0x2717
 // BYD's USB Vendor ID
-#define VENDOR_ID_BYD           0x19D1
+#define VENDOR_ID_BYD           0x1D91
 // OUYA's USB Vendor ID
 #define VENDOR_ID_OUYA          0x2836
 // Haier's USB Vendor ID
diff --git a/include/system/window.h b/include/system/window.h
index 649bd71..588f9c6 100644
--- a/include/system/window.h
+++ b/include/system/window.h
@@ -22,7 +22,6 @@
 #include <limits.h>
 #include <stdint.h>
 #include <string.h>
-#include <sync/sync.h>
 #include <sys/cdefs.h>
 #include <system/graphics.h>
 #include <unistd.h>
diff --git a/libbacktrace/Android.mk b/libbacktrace/Android.mk
index eb55b47..a6b9c2b 100644
--- a/libbacktrace/Android.mk
+++ b/libbacktrace/Android.mk
@@ -22,8 +22,11 @@
 	libgccdemangle \
 	liblog \
 
-# To enable using libunwind on each arch, add it to the list below.
-ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),arm))
+# To enable using libunwind on each arch, add it to this list.
+libunwind_architectures :=
+#libunwind_architectures := arm
+
+ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),$(libunwind_architectures)))
 
 #----------------------------------------------------------------------------
 # The native libbacktrace library with libunwind.
diff --git a/liblog/Android.mk b/liblog/Android.mk
index 5dfbc06..e96f23a 100644
--- a/liblog/Android.mk
+++ b/liblog/Android.mk
@@ -35,8 +35,11 @@
 ifndef WITH_MINGW
     liblog_sources += \
         logprint.c \
-        log_read.c \
         event_tag_map.c
+ifneq ($(HOST_OS),darwin)
+    liblog_sources += \
+        log_read.c
+endif
 else
     liblog_sources += \
         uio.c
diff --git a/libsync/Android.mk b/libsync/Android.mk
index 73de069..626b762 100644
--- a/libsync/Android.mk
+++ b/libsync/Android.mk
@@ -5,6 +5,8 @@
 LOCAL_MODULE := libsync
 LOCAL_MODULE_TAGS := optional
 LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
 include $(BUILD_SHARED_LIBRARY)
 
 include $(CLEAR_VARS)
@@ -12,4 +14,5 @@
 LOCAL_MODULE := sync_test
 LOCAL_MODULE_TAGS := optional tests
 LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
 include $(BUILD_EXECUTABLE)
diff --git a/include/sync/sync.h b/libsync/include/sync/sync.h
similarity index 100%
rename from include/sync/sync.h
rename to libsync/include/sync/sync.h
diff --git a/include/sync/sw_sync.h b/libsync/sw_sync.h
similarity index 97%
rename from include/sync/sw_sync.h
rename to libsync/sw_sync.h
index 3bf4110..fda1c4c 100644
--- a/include/sync/sw_sync.h
+++ b/libsync/sw_sync.h
@@ -19,8 +19,6 @@
 #ifndef __SYS_CORE_SW_SYNC_H
 #define __SYS_CORE_SW_SYNC_H
 
-#include "sync.h"
-
 __BEGIN_DECLS
 
 /*
diff --git a/libsync/sync_test.c b/libsync/sync_test.c
index 386747a..ee9ea3c 100644
--- a/libsync/sync_test.c
+++ b/libsync/sync_test.c
@@ -23,6 +23,7 @@
 #include <unistd.h>
 
 #include <sync/sync.h>
+#include "sw_sync.h"
 
 pthread_mutex_t printf_mutex = PTHREAD_MUTEX_INITIALIZER;
 
diff --git a/libziparchive/Android.mk b/libziparchive/Android.mk
index 5d836a1..e754c3b 100644
--- a/libziparchive/Android.mk
+++ b/libziparchive/Android.mk
@@ -49,7 +49,7 @@
     -DGTEST_OS_LINUX_ANDROID \
     -DGTEST_HAS_STD_STRING
 LOCAL_SRC_FILES := zip_archive_test.cc
-LOCAL_LDFLAGS := -llog
+LOCAL_SHARED_LIBRARIES := liblog
 LOCAL_STATIC_LIBRARIES := libziparchive libz libgtest libgtest_main libutils
 include $(BUILD_NATIVE_TEST)
 
diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc
index 2b827b3..8436d49 100644
--- a/libziparchive/zip_archive.cc
+++ b/libziparchive/zip_archive.cc
@@ -818,7 +818,7 @@
     archive->hash_table_size, entryName, nameLen);
 
   if (ent < 0) {
-    ALOGD("Zip: Could not find entry %.*s", nameLen, entryName);
+    ALOGV("Zip: Could not find entry %.*s", nameLen, entryName);
     return ent;
   }
 
diff --git a/rootdir/init.rc b/rootdir/init.rc
index b2b904b..5aae3fb 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -13,6 +13,9 @@
     # Set init and its forked children's oom_adj.
     write /proc/1/oom_score_adj -1000
 
+    # Apply strict SELinux checking of PROT_EXEC on mmap/mprotect calls.
+    write /sys/fs/selinux/checkreqprot 0
+
     # Set the security context for the init process.
     # This should occur before anything else (e.g. ueventd) is started.
     setcon u:r:init:s0
diff --git a/toolbox/ls.c b/toolbox/ls.c
index 8467628..c740f84 100644
--- a/toolbox/ls.c
+++ b/toolbox/ls.c
@@ -316,6 +316,7 @@
     }
 
     if(lstat(pathname, &s) < 0) {
+        fprintf(stderr, "lstat '%s' failed: %s\n", pathname, strerror(errno));
         return -1;
     }