Merge "Make mkdirAndChown do what it claims to do"
diff --git a/adb/commandline.c b/adb/commandline.c
index eea882d..23e9ea4 100644
--- a/adb/commandline.c
+++ b/adb/commandline.c
@@ -29,7 +29,7 @@
 
 #include "sysdeps.h"
 
-#ifdef HAVE_TERMIO_H
+#if !defined(_WIN32)
 #include <termios.h>
 #endif
 
@@ -235,7 +235,18 @@
     return 1;
 }
 
-#ifdef HAVE_TERMIO_H
+#if defined(_WIN32)
+
+// Windows does not have <termio.h>.
+static void stdin_raw_init(int fd) {
+
+}
+
+static void stdin_raw_restore(int fd) {
+
+}
+
+#else
 static struct termios tio_save;
 
 static void stdin_raw_init(int fd)
@@ -309,11 +320,11 @@
     long total = 0;
 
     D("copy_to_file(%d -> %d)\n", inFd, outFd);
-#ifdef HAVE_TERMIO_H
+
     if (inFd == STDIN_FILENO) {
         stdin_raw_init(STDIN_FILENO);
     }
-#endif
+
     for (;;) {
         if (inFd == STDIN_FILENO) {
             len = unix_read(inFd, buf, BUFSIZE);
@@ -340,11 +351,11 @@
         }
         total += len;
     }
-#ifdef HAVE_TERMIO_H
+
     if (inFd == STDIN_FILENO) {
         stdin_raw_restore(STDIN_FILENO);
     }
-#endif
+
     D("copy_to_file() finished after %lu bytes\n", total);
     free(buf);
 }
@@ -385,9 +396,7 @@
             case '.':
                 if(state == 2) {
                     fprintf(stderr,"\n* disconnect *\n");
-#ifdef HAVE_TERMIO_H
                     stdin_raw_restore(fdi);
-#endif
                     exit(0);
                 }
             default:
@@ -419,14 +428,10 @@
     fds[0] = fd;
     fds[1] = fdi;
 
-#ifdef HAVE_TERMIO_H
     stdin_raw_init(fdi);
-#endif
     adb_thread_create(&thr, stdin_read_thread, fds);
     read_and_dump(fd);
-#ifdef HAVE_TERMIO_H
     stdin_raw_restore(fdi);
-#endif
     return 0;
 }
 
diff --git a/adb/get_my_path_darwin.c b/adb/get_my_path_darwin.c
index 5b95d15..65dd226 100644
--- a/adb/get_my_path_darwin.c
+++ b/adb/get_my_path_darwin.c
@@ -19,12 +19,12 @@
 
 void get_my_path(char *s, size_t maxLen)
 {
-    ProcessSerialNumber psn;
-    GetCurrentProcess(&psn);
-    CFDictionaryRef dict;
-    dict = ProcessInformationCopyDictionary(&psn, 0xffffffff);
-    CFStringRef value = (CFStringRef)CFDictionaryGetValue(dict,
-                CFSTR("CFBundleExecutable"));
-    CFStringGetCString(value, s, maxLen, kCFStringEncodingUTF8);
+    CFBundleRef mainBundle = CFBundleGetMainBundle();
+    CFURLRef executableURL = CFBundleCopyExecutableURL(mainBundle);
+    CFStringRef executablePathString = CFURLCopyFileSystemPath(executableURL, kCFURLPOSIXPathStyle);
+    CFRelease(executableURL);
+
+    CFStringGetCString(executablePathString, s, maxLen, kCFStringEncodingASCII);
+    CFRelease(executablePathString);
 }
 
diff --git a/adb/transport_usb.c b/adb/transport_usb.c
index ee6b637..3d19803 100644
--- a/adb/transport_usb.c
+++ b/adb/transport_usb.c
@@ -23,10 +23,6 @@
 #define  TRACE_TAG  TRACE_TRANSPORT
 #include "adb.h"
 
-#if ADB_HOST
-#include "usb_vendors.h"
-#endif
-
 #ifdef HAVE_BIG_ENDIAN
 #define H4(x)	(((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24)
 static inline void fix_endians(apacket *p)
@@ -131,18 +127,6 @@
 #if ADB_HOST
 int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol)
 {
-    unsigned i;
-    for (i = 0; i < vendorIdCount; i++) {
-        if (vid == vendorIds[i]) {
-            if (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS &&
-                    usb_protocol == ADB_PROTOCOL) {
-                return 1;
-            }
-
-            return 0;
-        }
-    }
-
-    return 0;
+    return (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS && usb_protocol == ADB_PROTOCOL);
 }
 #endif
diff --git a/adb/usb_vendors.c b/adb/usb_vendors.c
index b037268..1b73521 100755
--- a/adb/usb_vendors.c
+++ b/adb/usb_vendors.c
@@ -86,8 +86,6 @@
 #define VENDOR_ID_HARRIS        0x19A5
 // Hisense's USB Vendor ID
 #define VENDOR_ID_HISENSE       0x109b
-// Honeywell's USB Vendor ID
-#define VENDOR_ID_HONEYWELL     0x0c2e
 // HP's USB Vendor ID
 #define VENDOR_ID_HP            0x03f0
 // HTC's USB Vendor ID
@@ -232,7 +230,6 @@
     VENDOR_ID_HAIER,
     VENDOR_ID_HARRIS,
     VENDOR_ID_HISENSE,
-    VENDOR_ID_HONEYWELL,
     VENDOR_ID_HP,
     VENDOR_ID_HTC,
     VENDOR_ID_HUAWEI,
@@ -301,7 +298,7 @@
 int vendorIds[VENDOR_COUNT_MAX];
 unsigned vendorIdCount = 0;
 
-int get_adb_usb_ini(char* buff, size_t len);
+static int get_adb_usb_ini(char* buff, size_t len);
 
 void usb_vendors_init(void)
 {
@@ -351,7 +348,7 @@
 /* Utils methods */
 
 /* builds the path to the adb vendor id file. returns 0 if success */
-int build_path(char* buff, size_t len, const char* format, const char* home)
+static int build_path(char* buff, size_t len, const char* format, const char* home)
 {
     if (snprintf(buff, len, format, home, ANDROID_PATH, ANDROID_ADB_INI) >= (signed)len) {
         return 1;
@@ -361,7 +358,7 @@
 }
 
 /* fills buff with the path to the adb vendor id file. returns 0 if success */
-int get_adb_usb_ini(char* buff, size_t len)
+static int get_adb_usb_ini(char* buff, size_t len)
 {
 #ifdef _WIN32
     const char* home = getenv("ANDROID_SDK_HOME");
diff --git a/fastboot/util_osx.c b/fastboot/util_osx.c
index 26b832a..a8f5c31 100644
--- a/fastboot/util_osx.c
+++ b/fastboot/util_osx.c
@@ -31,14 +31,15 @@
 
 void get_my_path(char s[PATH_MAX])
 {
-    char *x;
-    ProcessSerialNumber psn;
-    GetCurrentProcess(&psn);
-    CFDictionaryRef dict;
-    dict = ProcessInformationCopyDictionary(&psn, 0xffffffff);
-    CFStringRef value = (CFStringRef)CFDictionaryGetValue(dict,
-                CFSTR("CFBundleExecutable"));
-    CFStringGetCString(value, s, PATH_MAX - 1, kCFStringEncodingUTF8);
+    CFBundleRef mainBundle = CFBundleGetMainBundle();
+    CFURLRef executableURL = CFBundleCopyExecutableURL(mainBundle);
+    CFStringRef executablePathString = CFURLCopyFileSystemPath(executableURL, kCFURLPOSIXPathStyle);
+    CFRelease(executableURL);
+
+    CFStringGetCString(executablePathString, s, PATH_MAX-1, kCFStringEncodingASCII);
+    CFRelease(executablePathString);
+
+	char *x;
     x = strrchr(s, '/');
     if(x) x[1] = 0;
 }
diff --git a/include/cutils/aref.h b/include/cutils/aref.h
index e40dc70..3bd36ea 100644
--- a/include/cutils/aref.h
+++ b/include/cutils/aref.h
@@ -20,7 +20,7 @@
 #include <stddef.h>
 #include <sys/cdefs.h>
 
-#include <cutils/atomic-inline.h>
+#include <cutils/atomic.h>
 
 __BEGIN_DECLS
 
diff --git a/include/cutils/atomic-inline.h b/include/cutils/atomic-inline.h
deleted file mode 100644
index 103c5b0..0000000
--- a/include/cutils/atomic-inline.h
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * Copyright (C) 2014 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef ANDROID_CUTILS_ATOMIC_H
-#include <cutils/atomic.h>
-#endif
diff --git a/include/cutils/trace.h b/include/cutils/trace.h
index 6ca8293..59ff6c1 100644
--- a/include/cutils/trace.h
+++ b/include/cutils/trace.h
@@ -25,8 +25,8 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <cutils/atomic.h>
 #include <cutils/compiler.h>
-#include <cutils/atomic-inline.h>
 
 __BEGIN_DECLS
 
diff --git a/include/utils/Compat.h b/include/utils/Compat.h
index 20a6920..0df40a1 100644
--- a/include/utils/Compat.h
+++ b/include/utils/Compat.h
@@ -44,6 +44,17 @@
 #endif
 
 /*
+ * Needed for cases where something should be constexpr if possible, but not
+ * being constexpr is fine if in pre-C++11 code (such as a const static float
+ * member variable).
+ */
+#if __cplusplus >= 201103L
+#define CONSTEXPR constexpr
+#else
+#define CONSTEXPR
+#endif
+
+/*
  * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
  * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
  * not already defined, then define it here.
diff --git a/libutils/FileMap.cpp b/libutils/FileMap.cpp
index 6d92f74..f49b4f9 100644
--- a/libutils/FileMap.cpp
+++ b/libutils/FileMap.cpp
@@ -194,9 +194,9 @@
 }
 
 // Provide guidance to the system.
+#if !defined(_WIN32)
 int FileMap::advise(MapAdvice advice)
 {
-#if HAVE_MADVISE
     int cc, sysAdvice;
 
     switch (advice) {
@@ -214,7 +214,11 @@
     if (cc != 0)
         ALOGW("madvise(%d) failed: %s\n", sysAdvice, strerror(errno));
     return cc;
-#else
-    return -1;
-#endif // HAVE_MADVISE
 }
+
+#else
+int FileMap::advise(MapAdvice /* advice */)
+{
+    return -1;
+}
+#endif
diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc
index d5d4700..40416a0 100644
--- a/libziparchive/zip_archive.cc
+++ b/libziparchive/zip_archive.cc
@@ -744,7 +744,7 @@
 // as a side effect of this call.
 static inline ssize_t ReadAtOffset(int fd, uint8_t* buf, size_t len,
                                    off64_t off) {
-#ifdef HAVE_PREAD
+#if !defined(_WIN32)
   return TEMP_FAILURE_RETRY(pread64(fd, buf, len, off));
 #else
   // The only supported platform that doesn't support pread at the moment
@@ -756,7 +756,7 @@
   }
 
   return TEMP_FAILURE_RETRY(read(fd, buf, len));
-#endif  // HAVE_PREAD
+#endif
 }
 
 static int32_t FindEntry(const ZipArchive* archive, const int ent,
diff --git a/logd/LogAudit.cpp b/logd/LogAudit.cpp
index 51feff3..ee2f32d 100644
--- a/logd/LogAudit.cpp
+++ b/logd/LogAudit.cpp
@@ -248,7 +248,7 @@
     if (fd < 0) {
         return fd;
     }
-    if (audit_set_pid(fd, getpid(), WAIT_YES) < 0) {
+    if (audit_setup(fd, getpid()) < 0) {
         audit_close(fd);
         fd = -1;
     }
diff --git a/logd/libaudit.c b/logd/libaudit.c
index ca88d1b..d00d579 100644
--- a/logd/libaudit.c
+++ b/logd/libaudit.c
@@ -162,7 +162,7 @@
     return rc;
 }
 
-int audit_set_pid(int fd, uint32_t pid, rep_wait_t wmode)
+int audit_setup(int fd, uint32_t pid)
 {
     int rc;
     struct audit_message rep;
@@ -176,7 +176,8 @@
      * and the the mask set to AUDIT_STATUS_PID
      */
     status.pid = pid;
-    status.mask = AUDIT_STATUS_PID;
+    status.mask = AUDIT_STATUS_PID | AUDIT_STATUS_RATE_LIMIT;
+    status.rate_limit = 20; // audit entries per second
 
     /* Let the kernel know this pid will be registering for audit events */
     rc = audit_send(fd, AUDIT_SET, &status, sizeof(status));
@@ -188,24 +189,21 @@
     /*
      * In a request where we need to wait for a response, wait for the message
      * and discard it. This message confirms and sync's us with the kernel.
-     * This daemon is now registered as the audit logger. Only wait if the
-     * wmode is != WAIT_NO
+     * This daemon is now registered as the audit logger.
+     *
+     * TODO
+     * If the daemon dies and restarts the message didn't come back,
+     * so I went to non-blocking and it seemed to fix the bug.
+     * Need to investigate further.
      */
-    if (wmode != WAIT_NO) {
-        /* TODO
-         * If the daemon dies and restarts the message didn't come back,
-         * so I went to non-blocking and it seemed to fix the bug.
-         * Need to investigate further.
-         */
-        audit_get_reply(fd, &rep, GET_REPLY_NONBLOCKING, 0);
-    }
+    audit_get_reply(fd, &rep, GET_REPLY_NONBLOCKING, 0);
 
     return 0;
 }
 
 int audit_open()
 {
-    return socket(PF_NETLINK, SOCK_RAW, NETLINK_AUDIT);
+    return socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_AUDIT);
 }
 
 int audit_get_reply(int fd, struct audit_message *rep, reply_t block, int peek)
diff --git a/logd/libaudit.h b/logd/libaudit.h
index cb114f9..b9e330d 100644
--- a/logd/libaudit.h
+++ b/logd/libaudit.h
@@ -37,11 +37,6 @@
     GET_REPLY_NONBLOCKING
 } reply_t;
 
-typedef enum {
-    WAIT_NO,
-    WAIT_YES
-} rep_wait_t;
-
 /* type == AUDIT_SIGNAL_INFO */
 struct audit_sig_info {
     uid_t uid;
@@ -92,12 +87,10 @@
  *  The fd returned by a call to audit_open()
  * @param pid
  *  The pid whom to set as the reciever of audit messages
- * @param wmode
- *  Whether or not to block on the underlying socket io calls.
  * @return
  *  This function returns 0 on success, -errno on error.
  */
-extern int  audit_set_pid(int fd, uint32_t pid, rep_wait_t wmode);
+extern int  audit_setup(int fd, uint32_t pid);
 
 __END_DECLS
 
diff --git a/toolbox/mkswap.c b/toolbox/mkswap.c
index 0904152..ad66353 100644
--- a/toolbox/mkswap.c
+++ b/toolbox/mkswap.c
@@ -1,11 +1,13 @@
+#include <fcntl.h>
+#include <linux/fs.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <unistd.h>
+#include <sys/stat.h>
 #include <sys/swap.h>
 #include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
 
-/* XXX This needs to be obtained from kernel headers. See b/9336527 */
+/* This is not in a uapi header. */
 struct linux_swap_header {
     char            bootbits[1024]; /* Space for disklabel etc. */
     uint32_t        version;
@@ -23,71 +25,67 @@
 
 int mkswap_main(int argc, char **argv)
 {
-    int err = 0;
-    int fd;
-    ssize_t len;
-    off_t swap_size;
-    int pagesize;
-    struct linux_swap_header sw_hdr;
-
     if (argc != 2) {
         fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
-        return -EINVAL;
+        return EXIT_FAILURE;
     }
 
-    fd = open(argv[1], O_WRONLY);
+    int fd = open(argv[1], O_RDWR);
     if (fd < 0) {
-        err = errno;
-        fprintf(stderr, "Cannot open %s\n", argv[1]);
-        return err;
+        fprintf(stderr, "Cannot open %s: %s\n", argv[1], strerror(errno));
+        return EXIT_FAILURE;
     }
 
-    pagesize = getpagesize();
     /* Determine the length of the swap file */
-    swap_size = lseek(fd, 0, SEEK_END);
-    if (swap_size < MIN_PAGES * pagesize) {
-        fprintf(stderr, "Swap file needs to be at least %dkB\n",
-            (MIN_PAGES * pagesize) >> 10);
-        err = -ENOSPC;
-        goto err;
+    off64_t swap_size;
+    struct stat sb;
+    if (fstat(fd, &sb)) {
+        fprintf(stderr, "Couldn't fstat file: %s\n", strerror(errno));
+        return EXIT_FAILURE;
     }
-    if (lseek(fd, 0, SEEK_SET)) {
-        err = errno;
-        fprintf(stderr, "Can't seek to the beginning of the file\n");
-        goto err;
+    if (S_ISBLK(sb.st_mode)) {
+        if (ioctl(fd, BLKGETSIZE64, &swap_size) < 0) {
+            fprintf(stderr, "Couldn't determine block device size: %s\n", strerror(errno));
+            return EXIT_FAILURE;
+        }
+    } else {
+        swap_size = sb.st_size;
     }
 
+    int pagesize = getpagesize();
+    if (swap_size < MIN_PAGES * pagesize) {
+        fprintf(stderr, "Swap file needs to be at least %d KiB\n", (MIN_PAGES * pagesize) >> 10);
+        return EXIT_FAILURE;
+    }
+
+    struct linux_swap_header sw_hdr;
     memset(&sw_hdr, 0, sizeof(sw_hdr));
     sw_hdr.version = 1;
     sw_hdr.last_page = (swap_size / pagesize) - 1;
 
-    len = write(fd, &sw_hdr, sizeof(sw_hdr));
+    ssize_t len = write(fd, &sw_hdr, sizeof(sw_hdr));
     if (len != sizeof(sw_hdr)) {
-        err = errno;
-        fprintf(stderr, "Failed to write swap header into %s\n", argv[1]);
-        goto err;
+        fprintf(stderr, "Failed to write swap header into %s: %s\n", argv[1], strerror(errno));
+        return EXIT_FAILURE;
     }
 
     /* Write the magic header */
     if (lseek(fd, pagesize - MAGIC_SWAP_HEADER_LEN, SEEK_SET) < 0) {
-        err = errno;
-        fprintf(stderr, "Failed to seek into %s\n", argv[1]);
-        goto err;
+        fprintf(stderr, "Failed to seek into %s: %s\n", argv[1], strerror(errno));
+        return EXIT_FAILURE;
     }
 
     len = write(fd, MAGIC_SWAP_HEADER, MAGIC_SWAP_HEADER_LEN);
     if (len != MAGIC_SWAP_HEADER_LEN) {
-        err = errno;
-        fprintf(stderr, "Failed to write magic swap header into %s\n", argv[1]);
-        goto err;
+        fprintf(stderr, "Failed to write magic swap header into %s: %s\n", argv[1], strerror(errno));
+        return EXIT_FAILURE;
     }
 
     if (fsync(fd) < 0) {
-        err = errno;
-        fprintf(stderr, "Failed to sync %s\n", argv[1]);
-        goto err;
+        fprintf(stderr, "Failed to sync %s: %s\n", argv[1], strerror(errno));
+        return EXIT_FAILURE;
     }
-err:
+
     close(fd);
-    return err;
+    return EXIT_SUCCESS;
 }
diff --git a/toolbox/swapoff.c b/toolbox/swapoff.c
index d8f6a00..477494e 100644
--- a/toolbox/swapoff.c
+++ b/toolbox/swapoff.c
@@ -13,7 +13,7 @@
 
     err = swapoff(argv[1]);
     if (err) {
-        fprintf(stderr, "swapoff failed for %s\n", argv[1]);
+        fprintf(stderr, "swapoff failed for %s: %s\n", argv[1], strerror(errno));
     }
 
     return err;
diff --git a/toolbox/swapon.c b/toolbox/swapon.c
index 150701a..51b4ff1 100644
--- a/toolbox/swapon.c
+++ b/toolbox/swapon.c
@@ -59,7 +59,7 @@
 
     err = swapon(argv[argc - 1], flags);
     if (err) {
-        fprintf(stderr, "swapon failed for %s\n", argv[argc - 1]);
+        fprintf(stderr, "swapon failed for %s: %s\n", argv[argc - 1], strerror(errno));
     }
 
     return err;