Skip to content

Commit

Permalink
Add basic error checking to legacy property init
Browse files Browse the repository at this point in the history
Let init and rename funcitons return success or failure values.

Change-Id: Ieed86cac8a0dcfd770a89dacc57fd306e7a6ad8d
  • Loading branch information
mdmower committed Mar 27, 2014
1 parent 02a6453 commit cdd3b33
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
4 changes: 2 additions & 2 deletions legacy_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ struct prop_info {
char value[PROP_VALUE_MAX];
};

struct prop_msg
struct prop_msg
{
unsigned cmd;
char name[PROP_NAME_MAX];
char value[PROP_VALUE_MAX];
};

#define PROP_MSG_SETPROP 1

/*
** Rules:
**
Expand Down
14 changes: 8 additions & 6 deletions legacy_property_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -203,9 +201,13 @@ static void copy_property_to_legacy(const char *key, const char *value, void *co
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;
}
2 changes: 1 addition & 1 deletion legacy_property_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
62 changes: 48 additions & 14 deletions twinstall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,48 @@ extern "C" {

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) {
Expand Down Expand Up @@ -143,6 +162,13 @@ static int Run_Update_Binary(const char *path, ZipArchive *Zip, int* wipe_cache)
}
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();
Expand All @@ -155,7 +181,6 @@ static int Run_Update_Binary(const char *path, ZipArchive *Zip, int* wipe_cache)

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());
Expand Down Expand Up @@ -204,7 +229,16 @@ static int Run_Update_Binary(const char *path, ZipArchive *Zip, int* wipe_cache)
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;
Expand Down

0 comments on commit cdd3b33

Please sign in to comment.