Merge "Default to secure mode"
diff --git a/adb/adb.c b/adb/adb.c
index a03ee64..a91004c 100644
--- a/adb/adb.c
+++ b/adb/adb.c
@@ -1110,7 +1110,7 @@
             type = kTransportAny;
         } else if (!strncmp(service, "transport:", strlen("transport:"))) {
             service += strlen("transport:");
-            serial = strdup(service);
+            serial = service;
         }
 
         transport = acquire_one_transport(CS_ANY, type, serial, &error_string);
diff --git a/debuggerd/debuggerd.c b/debuggerd/debuggerd.c
index 892bc99..d03c214 100644
--- a/debuggerd/debuggerd.c
+++ b/debuggerd/debuggerd.c
@@ -593,19 +593,39 @@
      * debugger_signal_handler().
      */
     tid_attach_status = ptrace(PTRACE_ATTACH, tid, 0, 0);
+    int ptrace_error = errno;
 
-    TEMP_FAILURE_RETRY(write(fd, &tid, 1));
+    if (TEMP_FAILURE_RETRY(write(fd, &tid, 1)) != 1) {
+        XLOG("failed responding to client: %s\n",
+            strerror(errno));
+        goto done;
+    }
 
     if(tid_attach_status < 0) {
-        LOG("ptrace attach failed: %s\n", strerror(errno));
+        LOG("ptrace attach failed: %s\n", strerror(ptrace_error));
         goto done;
     }
 
     close(fd);
     fd = -1;
 
+    const int sleep_time_usec = 200000;         /* 0.2 seconds */
+    const int max_total_sleep_usec = 3000000;   /* 3 seconds */
+    int loop_limit = max_total_sleep_usec / sleep_time_usec;
     for(;;) {
-        n = waitpid(tid, &status, __WALL);
+        if (loop_limit-- == 0) {
+            LOG("timed out waiting for pid=%d tid=%d uid=%d to die\n",
+                cr.pid, tid, cr.uid);
+            goto done;
+        }
+        n = waitpid(tid, &status, __WALL | WNOHANG);
+
+        if (n == 0) {
+            /* not ready yet */
+            XLOG("not ready yet\n");
+            usleep(sleep_time_usec);
+            continue;
+        }
 
         if(n < 0) {
             if(errno == EAGAIN) continue;
@@ -734,8 +754,12 @@
         int fd;
 
         alen = sizeof(addr);
+        XLOG("waiting for connection\n");
         fd = accept(s, &addr, &alen);
-        if(fd < 0) continue;
+        if(fd < 0) {
+            XLOG("accept failed: %s\n", strerror(errno));
+            continue;
+        }
 
         fcntl(fd, F_SETFD, FD_CLOEXEC);
 
diff --git a/include/system/camera.h b/include/system/camera.h
index c404203..81ce4cb 100644
--- a/include/system/camera.h
+++ b/include/system/camera.h
@@ -80,10 +80,11 @@
     CAMERA_MSG_RAW_IMAGE = 0x0080,        // dataCallback
     CAMERA_MSG_COMPRESSED_IMAGE = 0x0100, // dataCallback
     CAMERA_MSG_RAW_IMAGE_NOTIFY = 0x0200, // dataCallback
-    // Face metadata. This can be combined with CAMERA_MSG_PREVIEW_FRAME in
-    // dataCallback. For example, the apps can request PREVIEW_FRAME and FACE.
-    // Or the apps can request only PREVIEW_FRAME or only FACE.
-    CAMERA_MSG_METADATA_FACE = 0x0400,    // dataCallback
+    // Preview frame metadata. This can be combined with
+    // CAMERA_MSG_PREVIEW_FRAME in dataCallback. For example, the apps can
+    // request FRAME and METADATA. Or the apps can request only FRAME or only
+    // METADATA.
+    CAMERA_MSG_PREVIEW_METADATA = 0x0400, // dataCallback
     CAMERA_MSG_ALL_MSGS = 0xFFFF
 };
 
@@ -180,39 +181,39 @@
      * sensor sees. The direction is not affected by the rotation or mirroring
      * of CAMERA_CMD_SET_DISPLAY_ORIENTATION.
      */
-    int rect[4];
+    int32_t rect[4];
 
     /**
      * The confidence level of the face. The range is 1 to 100. 100 is the
      * highest confidence. This is supported by both hardware and software
      * face detection.
      */
-    int score;
+    int32_t score;
 
     /**
      * An unique id per face while the face is visible to the tracker. If
      * the face leaves the field-of-view and comes back, it will get a new
      * id. If the value is 0, id is not supported.
      */
-    int id;
+    int32_t id;
 
     /**
      * The coordinates of the center of the left eye. The range is -1000 to
      * 1000. -2000, -2000 if this is not supported.
      */
-    int left_eye[2];
+    int32_t left_eye[2];
 
     /**
      * The coordinates of the center of the right eye. The range is -1000 to
      * 1000. -2000, -2000 if this is not supported.
      */
-    int right_eye[2];
+    int32_t right_eye[2];
 
     /**
      * The coordinates of the center of the mouth. The range is -1000 to 1000.
      * -2000, -2000 if this is not supported.
      */
-    int mouth[2];
+    int32_t mouth[2];
 
 } camera_face_t;
 
@@ -223,7 +224,7 @@
     /**
      * The number of detected faces in the frame.
      */
-    int number_of_faces;
+    int32_t number_of_faces;
 
     /**
      * An array of the detected faces. The length is number_of_faces. The list
diff --git a/include/system/window.h b/include/system/window.h
index 4d519c5..959bd23 100644
--- a/include/system/window.h
+++ b/include/system/window.h
@@ -216,8 +216,8 @@
 /* valid operations for the (*perform)() hook */
 enum {
     NATIVE_WINDOW_SET_USAGE                 =  0,
-    NATIVE_WINDOW_CONNECT                   =  1,
-    NATIVE_WINDOW_DISCONNECT                =  2,
+    NATIVE_WINDOW_CONNECT                   =  1,   /* deprecated */
+    NATIVE_WINDOW_DISCONNECT                =  2,   /* deprecated */
     NATIVE_WINDOW_SET_CROP                  =  3,
     NATIVE_WINDOW_SET_BUFFER_COUNT          =  4,
     NATIVE_WINDOW_SET_BUFFERS_GEOMETRY      =  5,   /* deprecated */
@@ -228,9 +228,11 @@
     NATIVE_WINDOW_SET_SCALING_MODE          = 10,
     NATIVE_WINDOW_LOCK                      = 11,   /* private */
     NATIVE_WINDOW_UNLOCK_AND_POST           = 12,   /* private */
+    NATIVE_WINDOW_API_CONNECT               = 13,   /* private */
+    NATIVE_WINDOW_API_DISCONNECT            = 14,   /* private */
 };
 
-/* parameter for NATIVE_WINDOW_[DIS]CONNECT */
+/* parameter for NATIVE_WINDOW_[API_][DIS]CONNECT */
 enum {
     /* Buffers will be queued by EGL via eglSwapBuffers after being filled using
      * OpenGL ES.
@@ -388,8 +390,8 @@
      *
      * The valid operations are:
      *     NATIVE_WINDOW_SET_USAGE
-     *     NATIVE_WINDOW_CONNECT
-     *     NATIVE_WINDOW_DISCONNECT
+     *     NATIVE_WINDOW_CONNECT               (deprecated)
+     *     NATIVE_WINDOW_DISCONNECT            (deprecated)
      *     NATIVE_WINDOW_SET_CROP
      *     NATIVE_WINDOW_SET_BUFFER_COUNT
      *     NATIVE_WINDOW_SET_BUFFERS_GEOMETRY  (deprecated)
@@ -400,6 +402,8 @@
      *     NATIVE_WINDOW_SET_SCALING_MODE
      *     NATIVE_WINDOW_LOCK                   (private)
      *     NATIVE_WINDOW_UNLOCK_AND_POST        (private)
+     *     NATIVE_WINDOW_API_CONNECT            (private)
+     *     NATIVE_WINDOW_API_DISCONNECT         (private)
      *
      */
 
@@ -442,28 +446,16 @@
     return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage);
 }
 
-/*
- * native_window_connect(..., NATIVE_WINDOW_API_EGL)
- * Must be called by EGL when the window is made current.
- * Returns -EINVAL if for some reason the window cannot be connected, which
- * can happen if it's connected to some other API.
- */
+/* deprecated. Always returns 0. Don't call. */
 static inline int native_window_connect(
-        struct ANativeWindow* window, int api)
-{
-    return window->perform(window, NATIVE_WINDOW_CONNECT, api);
+        struct ANativeWindow* window, int api) {
+    return 0;
 }
 
-/*
- * native_window_disconnect(..., NATIVE_WINDOW_API_EGL)
- * Must be called by EGL when the window is made not current.
- * An error is returned if for instance the window wasn't connected in the
- * first place.
- */
+/* deprecated. Always returns 0. Don't call. */
 static inline int native_window_disconnect(
-        struct ANativeWindow* window, int api)
-{
-    return window->perform(window, NATIVE_WINDOW_DISCONNECT, api);
+        struct ANativeWindow* window, int api) {
+    return 0;
 }
 
 /*
@@ -591,6 +583,32 @@
             mode);
 }
 
+
+/*
+ * native_window_api_connect(..., int api)
+ * connects an API to this window. only one API can be connected at a time.
+ * Returns -EINVAL if for some reason the window cannot be connected, which
+ * can happen if it's connected to some other API.
+ */
+static inline int native_window_api_connect(
+        struct ANativeWindow* window, int api)
+{
+    return window->perform(window, NATIVE_WINDOW_API_CONNECT, api);
+}
+
+/*
+ * native_window_api_disconnect(..., int api)
+ * disconnect the API from this window.
+ * An error is returned if for instance the window wasn't connected in the
+ * first place.
+ */
+static inline int native_window_api_disconnect(
+        struct ANativeWindow* window, int api)
+{
+    return window->perform(window, NATIVE_WINDOW_API_DISCONNECT, api);
+}
+
+
 __END_DECLS
 
 #endif /* SYSTEM_CORE_INCLUDE_ANDROID_WINDOW_H */
diff --git a/libnetutils/dhcpclient.c b/libnetutils/dhcpclient.c
index 5039e26..4f2d1c1 100644
--- a/libnetutils/dhcpclient.c
+++ b/libnetutils/dhcpclient.c
@@ -197,7 +197,7 @@
         }
         switch(opt) {
         case OPT_SUBNET_MASK:
-            if (optlen >= 4) info->prefixLength = ipv4NetmaskToPrefixLength((int)x);
+            if (optlen >= 4) info->prefixLength = ipv4NetmaskToPrefixLength(*((uint32_t*)x));
             break;
         case OPT_GATEWAY:
             if (optlen >= 4) memcpy(&info->gateway, x, 4);
diff --git a/libnl_2/attr.c b/libnl_2/attr.c
index 9467668..d416350 100644
--- a/libnl_2/attr.c
+++ b/libnl_2/attr.c
@@ -87,8 +87,17 @@
 /* Finalize nesting of attributes. */
 int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
 {
+	struct nlattr *container;
+
+	/* Adjust nested attribute container size */
+	container = (unsigned char *) start - sizeof(struct nlattr);
+	container->nla_len = (unsigned char *) \
+		nlmsg_tail(nlmsg_hdr(msg)) - (unsigned char *)container;
+
+	/* Fix attribute size */
 	start->nla_len = (unsigned char *) \
 		nlmsg_tail(nlmsg_hdr(msg)) - (unsigned char *)start;
+
 	return 0;
 }
 
diff --git a/libnl_2/socket.c b/libnl_2/socket.c
index 3bcf210..ce54f19 100644
--- a/libnl_2/socket.c
+++ b/libnl_2/socket.c
@@ -91,6 +91,7 @@
 void nl_socket_free(struct nl_sock *sk)
 {
 	nl_cb_put(sk->s_cb);
+	close(sk->s_fd);
 	free(sk);
 }