diff --git a/fastboot/Android.mk b/fastboot/Android.mk index b0b23379a..e0f702d11 100644 --- a/fastboot/Android.mk +++ b/fastboot/Android.mk @@ -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 diff --git a/fastboot/fs.cpp b/fastboot/fs.cpp index 4bf7af832..fe5cbc3d7 100644 --- a/fastboot/fs.cpp +++ b/fastboot/fs.cpp @@ -23,11 +23,12 @@ #include #include +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(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(argv), const_cast(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(mke2fs_args.data())); + const std::string mke2fs_env = "MKE2FS_CONFIG=" + GetExecutableDirectory() + "/mke2fs.conf"; + std::vector 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 e2fsdroid_args = {e2fsdroid_path.c_str(), "-f", initial_dir.c_str(), fileName, nullptr}; - ret = exec_e2fs_cmd(e2fsdroid_args[0], const_cast(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(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;