fastboot: pass environment variables to exec_e2fs_cmd

Set MKE2FS_CONFIG to point to a config file that comes with mke2fs
binary.

Change-Id: I1d68726ff8baa0c00b930b32f66c54c298c46b81
This commit is contained in:
Jin Qian 2017-12-04 16:17:46 -08:00
parent 6740b9697e
commit 03c73e106f
2 changed files with 22 additions and 13 deletions

View file

@ -39,7 +39,7 @@ LOCAL_MODULE := fastboot
LOCAL_MODULE_TAGS := debug
LOCAL_MODULE_HOST_OS := darwin linux windows
LOCAL_CFLAGS += -Wall -Wextra -Werror -Wunreachable-code
LOCAL_REQUIRED_MODULES := mke2fs e2fsdroid make_f2fs
LOCAL_REQUIRED_MODULES := mke2fs e2fsdroid mke2fs.conf make_f2fs
LOCAL_SRC_FILES_linux := usb_linux.cpp
LOCAL_STATIC_LIBRARIES_linux := libselinux

View file

@ -23,11 +23,12 @@
#include <android-base/stringprintf.h>
#include <android-base/unique_fd.h>
using android::base::GetExecutableDirectory;
using android::base::StringPrintf;
using android::base::unique_fd;
#ifdef WIN32
static int exec_e2fs_cmd(const char* /*path*/, char* const argv[]) {
static int exec_e2fs_cmd(const char* /*path*/, const char** argv, const char** envp) {
std::string cmd;
int i = 0;
while (argv[i] != nullptr) {
@ -44,7 +45,13 @@ static int exec_e2fs_cmd(const char* /*path*/, char* const argv[]) {
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
SetEnvironmentVariableA("MKE2FS_CONFIG", "");
std::string env_str;
if (envp != nullptr) {
while (*envp != nullptr) {
env_str += std::string(*envp) + std::string("\0", 1);
envp++;
}
}
if (!CreateProcessA(nullptr, // No module name (use command line)
const_cast<char*>(cmd.c_str()), // Command line
@ -52,10 +59,10 @@ static int exec_e2fs_cmd(const char* /*path*/, char* const argv[]) {
nullptr, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
nullptr, // Use parent's environment block
nullptr, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
env_str.empty() ? nullptr : LPSTR(env_str.c_str()),
nullptr, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
) {
fprintf(stderr, "CreateProcess failed: %s\n",
android::base::SystemErrorCodeToString(GetLastError()).c_str());
@ -72,12 +79,11 @@ static int exec_e2fs_cmd(const char* /*path*/, char* const argv[]) {
return exit_code != 0;
}
#else
static int exec_e2fs_cmd(const char* path, char* const argv[]) {
static int exec_e2fs_cmd(const char* path, const char** argv, const char** envp) {
int status;
pid_t child;
if ((child = fork()) == 0) {
setenv("MKE2FS_CONFIG", "", 1);
execvp(path, argv);
execvpe(path, const_cast<char**>(argv), const_cast<char**>(envp));
_exit(EXIT_FAILURE);
}
if (child < 0) {
@ -131,7 +137,10 @@ static int generate_ext4_image(const char* fileName, long long partSize,
mke2fs_args.push_back(size_str.c_str());
mke2fs_args.push_back(nullptr);
int ret = exec_e2fs_cmd(mke2fs_args[0], const_cast<char**>(mke2fs_args.data()));
const std::string mke2fs_env = "MKE2FS_CONFIG=" + GetExecutableDirectory() + "/mke2fs.conf";
std::vector<const char*> mke2fs_envp = {mke2fs_env.c_str(), nullptr};
int ret = exec_e2fs_cmd(mke2fs_args[0], mke2fs_args.data(), mke2fs_envp.data());
if (ret != 0) {
fprintf(stderr, "mke2fs failed: %d\n", ret);
return -1;
@ -145,7 +154,7 @@ static int generate_ext4_image(const char* fileName, long long partSize,
std::vector<const char*> e2fsdroid_args = {e2fsdroid_path.c_str(), "-f", initial_dir.c_str(),
fileName, nullptr};
ret = exec_e2fs_cmd(e2fsdroid_args[0], const_cast<char**>(e2fsdroid_args.data()));
ret = exec_e2fs_cmd(e2fsdroid_args[0], e2fsdroid_args.data(), nullptr);
if (ret != 0) {
fprintf(stderr, "e2fsdroid failed: %d\n", ret);
return -1;
@ -173,7 +182,7 @@ static int generate_f2fs_image(const char* fileName, long long partSize, const s
mkf2fs_args.push_back(fileName);
mkf2fs_args.push_back(nullptr);
int ret = exec_e2fs_cmd(mkf2fs_args[0], const_cast<char**>(mkf2fs_args.data()));
int ret = exec_e2fs_cmd(mkf2fs_args[0], mkf2fs_args.data(), nullptr);
if (ret != 0) {
fprintf(stderr, "mkf2fs failed: %d\n", ret);
return -1;