Merge "Add basic error checking to legacy property init" into android-4.4
diff --git a/legacy_properties.h b/legacy_properties.h
index 9eb6bc7..44cdd95 100644
--- a/legacy_properties.h
+++ b/legacy_properties.h
@@ -63,7 +63,7 @@
     char value[PROP_VALUE_MAX];
 };
 
-struct prop_msg 
+struct prop_msg
 {
     unsigned cmd;
     char name[PROP_NAME_MAX];
@@ -71,7 +71,7 @@
 };
 
 #define PROP_MSG_SETPROP 1
-    
+
 /*
 ** Rules:
 **
diff --git a/legacy_property_service.c b/legacy_property_service.c
index 0dc95ad..12865c4 100644
--- a/legacy_property_service.c
+++ b/legacy_property_service.c
@@ -33,13 +33,11 @@
 #include <sys/atomics.h>
 #include "legacy_property_service.h"
 
-
 static int persistent_properties_loaded = 0;
 static int property_area_inited = 0;
 
 static int property_set_fd = -1;
 
-
 typedef struct {
     void *data;
     size_t size;
@@ -203,9 +201,13 @@
     legacy_property_set(key, value);
 }
 
-void legacy_properties_init()
+int legacy_properties_init()
 {
-    init_property_area();
-    property_list(copy_property_to_legacy, 0);
-}
+    if(init_property_area() != 0)
+        return -1;
 
+    if(property_list(copy_property_to_legacy, 0) != 0)
+        return -1;
+
+    return 0;
+}
diff --git a/legacy_property_service.h b/legacy_property_service.h
index 172055f..d20bdef 100644
--- a/legacy_property_service.h
+++ b/legacy_property_service.h
@@ -20,6 +20,6 @@
 #include <stdbool.h>
 
 void legacy_get_property_workspace(int *fd, int *sz);
-void legacy_properties_init();
+int legacy_properties_init();
 
 #endif	/* _LEGACY_PROPERTY_H */
diff --git a/twinstall.cpp b/twinstall.cpp
index b3b9b77..5b32427 100644
--- a/twinstall.cpp
+++ b/twinstall.cpp
@@ -53,29 +53,48 @@
 
 static const char* properties_path = "/dev/__properties__";
 static const char* properties_path_renamed = "/dev/__properties_kk__";
+static bool legacy_props_env_initd = false;
+static bool legacy_props_path_modified = false;
 
-static void switch_to_legacy_properties()
+static int switch_to_legacy_properties()
 {
-	char tmp[32];
-	int propfd, propsz;
-	legacy_properties_init();
-	legacy_get_property_workspace(&propfd, &propsz);
-	sprintf(tmp, "%d,%d", dup(propfd), propsz);
-	setenv("ANDROID_PROPERTY_WORKSPACE", tmp, 1);
+	if (!legacy_props_env_initd) {
+		if (legacy_properties_init() != 0)
+			return -1;
+
+		char tmp[32];
+		int propfd, propsz;
+		legacy_get_property_workspace(&propfd, &propsz);
+		sprintf(tmp, "%d,%d", dup(propfd), propsz);
+		setenv("ANDROID_PROPERTY_WORKSPACE", tmp, 1);
+		legacy_props_env_initd = true;
+	}
 
 	if (TWFunc::Path_Exists(properties_path)) {
 		// hide real properties so that the updater uses the envvar to find the legacy format properties
-		if (rename(properties_path, properties_path_renamed) != 0)
-			LOGERR("Renaming properties failed: %s (assertions in old installers may fail)\n", strerror(errno));
+		if (rename(properties_path, properties_path_renamed) != 0) {
+			LOGERR("Renaming %s failed: %s\n", properties_path, strerror(errno));
+			return -1;
+		} else {
+			legacy_props_path_modified = true;
+		}
 	}
+
+	return 0;
 }
 
-static void switch_to_new_properties()
+static int switch_to_new_properties()
 {
 	if (TWFunc::Path_Exists(properties_path_renamed)) {
-		if (rename(properties_path_renamed, properties_path) != 0)
-			LOGERR("Restoring properties failed: %s\n", strerror(errno));
+		if (rename(properties_path_renamed, properties_path) != 0) {
+			LOGERR("Renaming %s failed: %s\n", properties_path_renamed, strerror(errno));
+			return -1;
+		} else {
+			legacy_props_path_modified = false;
+		}
 	}
+
+	return 0;
 }
 
 static int Run_Update_Binary(const char *path, ZipArchive *Zip, int* wipe_cache) {
@@ -143,6 +162,13 @@
 	}
 	mzCloseZipArchive(Zip);
 
+	/* Set legacy properties */
+	if (switch_to_legacy_properties() != 0) {
+		LOGERR("Legacy property environment did not initialize successfully. Properties may not be detected.\n");
+	} else {
+		LOGINFO("Legacy property environment initialized.\n");
+	}
+
 	pipe(pipe_fd);
 
 	args[0] = Temp_Binary.c_str();
@@ -155,7 +181,6 @@
 
 	pid_t pid = fork();
 	if (pid == 0) {
-		switch_to_legacy_properties();
 		close(pipe_fd[0]);
 		execve(Temp_Binary.c_str(), (char* const*)args, environ);
 		printf("E:Can't execute '%s'\n", Temp_Binary.c_str());
@@ -204,7 +229,16 @@
 	fclose(child_data);
 
 	waitpid(pid, &status, 0);
-	switch_to_new_properties();
+
+	/* Unset legacy properties */
+	if (legacy_props_path_modified) {
+		if (switch_to_new_properties() != 0) {
+			LOGERR("Legacy property environment did not disable successfully. Legacy properties may still be in use.\n");
+		} else {
+			LOGINFO("Legacy property environment disabled.\n");
+		}
+	}
+
 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
 		LOGERR("Error executing updater binary in zip '%s'\n", path);
 		return INSTALL_ERROR;