resolved conflicts for merge of 5284bcff to gingerbread-plus-aosp

Change-Id: I108a0c32efb5add1fec41bfe76f041971801d48d
diff --git a/Devmapper.cpp b/Devmapper.cpp
index c9482bf..4d94e7b 100644
--- a/Devmapper.cpp
+++ b/Devmapper.cpp
@@ -19,6 +19,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <string.h>
+#include <stdlib.h>
 
 #include <sys/types.h>
 #include <sys/ioctl.h>
@@ -34,6 +35,8 @@
 
 #include "Devmapper.h"
 
+#define DEVMAPPER_BUFFER_SIZE 4096
+
 int Devmapper::dumpState(SocketClient *c) {
 
     char *buffer = (char *) malloc(1024 * 64);
@@ -43,7 +46,7 @@
     }
     memset(buffer, 0, (1024 * 64));
 
-    char *buffer2 = (char *) malloc(4096);
+    char *buffer2 = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
     if (!buffer2) {
         SLOGE("Error allocating memory (%s)", strerror(errno));
         free(buffer);
@@ -81,9 +84,9 @@
     do {
         n = (struct dm_name_list *) (((char *) n) + nxt);
 
-        memset(buffer2, 0, 4096);
+        memset(buffer2, 0, DEVMAPPER_BUFFER_SIZE);
         struct dm_ioctl *io2 = (struct dm_ioctl *) buffer2;
-        ioctlInit(io2, 4096, n->name, 0);
+        ioctlInit(io2, DEVMAPPER_BUFFER_SIZE, n->name, 0);
         if (ioctl(fd, DM_DEV_STATUS, io2)) {
             if (errno != ENXIO) {
                 SLOGE("DM_DEV_STATUS ioctl failed (%s)", strerror(errno));
@@ -120,12 +123,14 @@
     io->version[2] = 0;
     io->flags = flags;
     if (name) {
-        strncpy(io->name, name, sizeof(io->name));
+        int ret = strlcpy(io->name, name, sizeof(io->name));
+	if (ret >= sizeof(io->name))
+		abort();
     }
 }
 
 int Devmapper::lookupActive(const char *name, char *ubuffer, size_t len) {
-    char *buffer = (char *) malloc(4096);
+    char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
     if (!buffer) {
         SLOGE("Error allocating memory (%s)", strerror(errno));
         return -1;
@@ -140,7 +145,7 @@
 
     struct dm_ioctl *io = (struct dm_ioctl *) buffer;
  
-    ioctlInit(io, 4096, name, 0);
+    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
     if (ioctl(fd, DM_DEV_STATUS, io)) {
         if (errno != ENXIO) {
             SLOGE("DM_DEV_STATUS ioctl failed for lookup (%s)", strerror(errno));
@@ -159,7 +164,7 @@
 
 int Devmapper::create(const char *name, const char *loopFile, const char *key,
                       unsigned int numSectors, char *ubuffer, size_t len) {
-    char *buffer = (char *) malloc(4096);
+    char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
     if (!buffer) {
         SLOGE("Error allocating memory (%s)", strerror(errno));
         return -1;
@@ -175,7 +180,7 @@
     struct dm_ioctl *io = (struct dm_ioctl *) buffer;
  
     // Create the DM device
-    ioctlInit(io, 4096, name, 0);
+    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
 
     if (ioctl(fd, DM_DEV_CREATE, io)) {
         SLOGE("Error creating device mapping (%s)", strerror(errno));
@@ -185,7 +190,7 @@
     }
 
     // Set the legacy geometry
-    ioctlInit(io, 4096, name, 0);
+    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
 
     char *geoParams = buffer + sizeof(struct dm_ioctl);
     // bps=512 spc=8 res=32 nft=2 sec=8190 mid=0xf0 spt=63 hds=64 hid=0 bspf=8 rdcl=2 infs=1 bkbs=2
@@ -200,7 +205,7 @@
     }
 
     // Retrieve the device number we were allocated
-    ioctlInit(io, 4096, name, 0);
+    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
     if (ioctl(fd, DM_DEV_STATUS, io)) {
         SLOGE("Error retrieving devmapper status (%s)", strerror(errno));
         free(buffer);
@@ -215,17 +220,19 @@
     struct dm_target_spec *tgt;
     tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
 
-    ioctlInit(io, 4096, name, DM_STATUS_TABLE_FLAG);
+    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, DM_STATUS_TABLE_FLAG);
     io->target_count = 1;
     tgt->status = 0;
 
     tgt->sector_start = 0;
     tgt->length = numSectors;
 
-    strcpy(tgt->target_type, "crypt");
+    strlcpy(tgt->target_type, "crypt", sizeof(tgt->target_type));
 
     char *cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
-    sprintf(cryptParams, "twofish %s 0 %s 0", key, loopFile);
+    snprintf(cryptParams,
+            DEVMAPPER_BUFFER_SIZE - (sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec)),
+            "twofish %s 0 %s 0", key, loopFile);
     cryptParams += strlen(cryptParams) + 1;
     cryptParams = (char *) _align(cryptParams, 8);
     tgt->next = cryptParams - buffer;
@@ -238,7 +245,7 @@
     }
 
     // Resume the new table
-    ioctlInit(io, 4096, name, 0);
+    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
 
     if (ioctl(fd, DM_DEV_SUSPEND, io)) {
         SLOGE("Error Resuming (%s)", strerror(errno));
@@ -254,7 +261,7 @@
 }
 
 int Devmapper::destroy(const char *name) {
-    char *buffer = (char *) malloc(4096);
+    char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
     if (!buffer) {
         SLOGE("Error allocating memory (%s)", strerror(errno));
         return -1;
@@ -270,7 +277,7 @@
     struct dm_ioctl *io = (struct dm_ioctl *) buffer;
  
     // Create the DM device
-    ioctlInit(io, 4096, name, 0);
+    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
 
     if (ioctl(fd, DM_DEV_REMOVE, io)) {
         if (errno != ENXIO) {
diff --git a/DirectVolume.cpp b/DirectVolume.cpp
index a570a73..557b365 100644
--- a/DirectVolume.cpp
+++ b/DirectVolume.cpp
@@ -279,6 +279,7 @@
     int major = atoi(evt->findParam("MAJOR"));
     int minor = atoi(evt->findParam("MINOR"));
     char msg[255];
+    int state;
 
     SLOGD("Volume %s %s partition %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
 
@@ -288,7 +289,8 @@
      * the removal notification will be sent on the Disk
      * itself
      */
-    if (getState() != Volume::State_Mounted) {
+    state = getState();
+    if (state != Volume::State_Mounted && state != Volume::State_Shared) {
         return;
     }
         
@@ -313,6 +315,19 @@
         } else {
             SLOGD("Crisis averted");
         }
+    } else if (state == Volume::State_Shared) {
+        /* removed during mass storage */
+        snprintf(msg, sizeof(msg), "Volume %s bad removal (%d:%d)",
+                 getLabel(), major, minor);
+        mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
+                                             msg, false);
+
+        if (mVm->unshareVolume(getLabel(), "ums")) {
+            SLOGE("Failed to unshare volume on bad removal (%s)",
+                strerror(errno));
+        } else {
+            SLOGD("Crisis averted");
+        }
     }
 }
 
diff --git a/Loop.cpp b/Loop.cpp
index 98015e2..dbfd331 100644
--- a/Loop.cpp
+++ b/Loop.cpp
@@ -188,8 +188,8 @@
     struct loop_info64 li;
 
     memset(&li, 0, sizeof(li));
-    strncpy((char*) li.lo_crypt_name, id, LO_NAME_SIZE);
-    strncpy((char*) li.lo_file_name, loopFile, LO_NAME_SIZE);
+    strlcpy((char*) li.lo_crypt_name, id, LO_NAME_SIZE);
+    strlcpy((char*) li.lo_file_name, loopFile, LO_NAME_SIZE);
 
     if (ioctl(fd, LOOP_SET_STATUS64, &li) < 0) {
         SLOGE("Error setting loopback status (%s)", strerror(errno));
diff --git a/Volume.cpp b/Volume.cpp
index d2b87b6..ecf7dcd 100644
--- a/Volume.cpp
+++ b/Volume.cpp
@@ -218,6 +218,7 @@
 
     setState(Volume::State_Formatting);
 
+    int ret = -1;
     // Only initialize the MBR if we are formatting the entire device
     if (formatEntireDevice) {
         sprintf(devicePath, "/dev/block/vold/%d:%d",
@@ -241,10 +242,11 @@
         goto err;
     }
 
-    setState(Volume::State_Idle);
-    return 0;
+    ret = 0;
+
 err:
-    return -1;
+    setState(Volume::State_Idle);
+    return ret;
 }
 
 bool Volume::isMountpointMounted(const char *path) {
diff --git a/logwrapper.c b/logwrapper.c
index b7d2f68..13c076d 100644
--- a/logwrapper.c
+++ b/logwrapper.c
@@ -155,7 +155,6 @@
                 if (write(fd, text, strlen(text)) < 0) {
                     LOG(LOG_WARN, "logwrapper",
                         "Unable to background process (%s)", strerror(errno));
-                    close(fd);
                 }
                 close(fd);
             } else {
diff --git a/main.cpp b/main.cpp
index f97632b..9c45774 100644
--- a/main.cpp
+++ b/main.cpp
@@ -151,7 +151,8 @@
     }
 
     while(fgets(line, sizeof(line), fp)) {
-        char *next = line;
+        const char *delim = " \t";
+        char *save_ptr;
         char *type, *label, *mount_point;
 
         n++;
@@ -160,24 +161,24 @@
         if (line[0] == '#' || line[0] == '\0')
             continue;
 
-        if (!(type = strsep(&next, " \t"))) {
+        if (!(type = strtok_r(line, delim, &save_ptr))) {
             SLOGE("Error parsing type");
             goto out_syntax;
         }
-        if (!(label = strsep(&next, " \t"))) {
+        if (!(label = strtok_r(NULL, delim, &save_ptr))) {
             SLOGE("Error parsing label");
             goto out_syntax;
         }
-        if (!(mount_point = strsep(&next, " \t"))) {
+        if (!(mount_point = strtok_r(NULL, delim, &save_ptr))) {
             SLOGE("Error parsing mount point");
             goto out_syntax;
         }
 
         if (!strcmp(type, "dev_mount")) {
             DirectVolume *dv = NULL;
-            char *part, *sysfs_path;
+            char *part;
 
-            if (!(part = strsep(&next, " \t"))) {
+            if (!(part = strtok_r(NULL, delim, &save_ptr))) {
                 SLOGE("Error parsing partition");
                 goto out_syntax;
             }
@@ -192,7 +193,7 @@
                 dv = new DirectVolume(vm, label, mount_point, atoi(part));
             }
 
-            while((sysfs_path = strsep(&next, " \t"))) {
+            while (char *sysfs_path = strtok_r(NULL, delim, &save_ptr)) {
                 if (dv->addPath(sysfs_path)) {
                     SLOGE("Failed to add devpath %s to volume %s", sysfs_path,
                          label);
diff --git a/vdc.c b/vdc.c
index 4f94ad3..1eb674c 100644
--- a/vdc.c
+++ b/vdc.c
@@ -21,6 +21,7 @@
 #include <signal.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <stdlib.h>
 
 #include <sys/socket.h>
 #include <sys/select.h>
@@ -56,6 +57,7 @@
 static int do_cmd(int sock, int argc, char **argv) {
     char final_cmd[255] = { '\0' };
     int i;
+    int ret;
 
     for (i = 1; i < argc; i++) {
         char *cmp;
@@ -65,7 +67,9 @@
         else
             asprintf(&cmp, "\"%s\"%s", argv[i], (i == (argc -1)) ? "" : " ");
 
-        strcat(final_cmd, cmp);
+        ret = strlcat(final_cmd, cmp, sizeof(final_cmd));
+        if (ret >= sizeof(final_cmd))
+            abort();
         free(cmp);
     }