install.cpp文件try_update_binary()函数安装过程分析

本文详细解析了Android系统更新安装时install.cpp中的try_update_binary函数,涉及查找并提取update-binary文件,创建临时文件,通过管道进行进程间通信,以及执行子进程来更新系统组件。主要步骤包括:检查update-binary文件是否存在,创建临时文件,从zip包中复制文件,建立管道用于父进程与子进程间的通信,并通过fork创建子进程执行更新操作。
static int
try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache) {
    const ZipEntry* binary_entry =
            mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);//lefty_lan:在zip格式的更新包文件中得到文件META-INF/com/google/android/update-binary的entry
    if (binary_entry == NULL) {
        mzCloseZipArchive(zip);
        return INSTALL_CORRUPT;//lefty_lan注:安装包中找不到update_binary文件
    }

    const char* binary = "/tmp/update_binary";
    unlink(binary);//lefty_lan注:清除软连接
    int fd = creat(binary, 0755);//lefty_lan注:创建空文件update_binary
    if (fd < 0) {
        mzCloseZipArchive(zip);
        LOGE("Can't make %s\n", binary);
        return INSTALL_ERROR; //lefty_lan注:创建失败,返回
    }
    bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);//lefty_lan注:把zip更新包中的update_binary文件保存到/tmp/update_binary文件中
    close(fd);
    mzCloseZipArchive(zip);

    if (!ok) {
        LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
        return INSTALL_ERROR;//lefty_lan注:拷贝失败,返回
    }

    int pipefd[2];
    pipe(pipefd);//lefty_lan注:创建管道

    // When executing the update binary contained in the package, the
    // arguments passed are:
    //
    //   - the version number for this interface
    //
    //   - an fd to which the program can write in order to update the
    //     progress bar.  The program can write single-line commands:
    //
    //        progress <frac> <secs>
    //            fill up the next <frac> part of of the progress bar
    //            over <secs> seconds.  If <secs> is zero, use
    //            set_progress commands to manually control the
    //            progress of this segment of the bar.
    //
    //        set_progress <frac>
    //            <frac> should be between 0.0 and 1.0; sets the
    //            progress bar within the segment defined by the most
    //            recent progress command.
    //
    //        firmware <"hboot"|"radio"> <filename>
    //            arrange to install the contents of <filename> in the
    //            given partition on reboot.
    //
    //            (API v2: <filename> may start with "PACKAGE:" to
    //            indicate taking a file from the OTA package.)
    //
    //            (API v3: this command no longer exists.)
    //
    //        ui_print <string>
    //            display <string> on the screen.
    //
    //        wipe_cache
    //            a wipe of cache will be performed following a successful
    //            installation.
    //
    //        clear_display
    //            turn off the text display.
    //
    //        enable_reboot
    //            packages can explicitly request that they want the user
    //            to be able to reboot during installation (useful for
    //            debugging packages that don't exit).
    //
    //   - the name of the package zip file.
    //

    const char** args = (const char**)malloc(sizeof(char*) * 5);
    args[0] = binary;
    args[1] = EXPAND(RECOVERY_API_VERSION);   // defined in Android.mk
    char* temp = (char*)malloc(10);
    sprintf(temp, "%d", pipefd[1]);
    args[2] = temp;
    args[3] = (char*)path;
    args[4] = NULL;

	//lefty_lan注:父进程关闭fd[1],子进程关闭fd[0],
	//lefty_lan注:管道只支持单向通信,
	//lefty_lan注:⽗进程可以从管道⾥读,⼦进程可以往管道⾥写,
	//lefty_lan注:管道是采⽤环形队列实现的,数据从写端流⼊,从读端流出,这样就实现了进程间通信
    pid_t pid = fork();//lefty_lan注:创建子进程
    if (pid == 0) {
        umask(022);
        close(pipefd[0]);//lefty_lan注:⼦进程关闭管道读端,只负责写入
        execv(binary, (char* const*)args);//lefty_lan注:在子进程中执行/tmp/update_binary文件
        fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
        _exit(-1);
    }
    close(pipefd[1]);//lefty_lan注:父进程关闭管道写端,只管读取

    *wipe_cache = false;

    char buffer[1024];
    FILE* from_child = fdopen(pipefd[0], "r");
    while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
        char* command = strtok(buffer, " \n");
        if (command == NULL) {
            continue;
        } else if (strcmp(command, "progress") == 0) {
            char* fraction_s = strtok(NULL, " \n");
            char* seconds_s = strtok(NULL, " \n");

            float fraction = strtof(fraction_s, NULL);
            int seconds = strtol(seconds_s, NULL, 10);

            ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
        } else if (strcmp(command, "set_progress") == 0) {
            char* fraction_s = strtok(NULL, " \n");
            float fraction = strtof(fraction_s, NULL);
            ui->SetProgress(fraction);
        } else if (strcmp(command, "ui_print") == 0) {
            char* str = strtok(NULL, "\n");
            if (str) {
                ui->Print("%s", str);
            } else {
                ui->Print("\n");
            }
            fflush(stdout);
        } else if (strcmp(command, "wipe_cache") == 0) {
            *wipe_cache = true;
        } else if (strcmp(command, "clear_display") == 0) {
            ui->SetBackground(RecoveryUI::NONE);
        } else if (strcmp(command, "enable_reboot") == 0) {
            // packages can explicitly request that they want the user
            // to be able to reboot during installation (useful for
            // debugging packages that don't exit).
            ui->SetEnableReboot(true);
        } else {
            LOGE("unknown command [%s]\n", command);
        }
    }
    fclose(from_child);

    int status;
    waitpid(pid, &status, 0);
    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
        LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
        return INSTALL_ERROR;
    }

    return INSTALL_SUCCESS;
}

C:\Users\Administrator\PyCharmMiscProject\.venv\Scripts\python.exe D:/Program Files/JetBrains/PyCharm 2025.2.3/plugins/python-ce/helpers/packaging_tool.py install cupy Collecting cupy Using cached cupy-13.6.0.tar.gz (3.3 MB) Installing build dependencies: started Installing build dependencies: finished with status 'done' Getting requirements to build wheel: started Getting requirements to build wheel: finished with status 'done' Preparing metadata (pyproject.toml): started Preparing metadata (pyproject.toml): finished with status 'done' Requirement already satisfied: numpy<2.6,>=1.22 in c:\users\administrator\pycharmmiscproject\.venv\lib\site-packages (from cupy) (2.3.4) Collecting fastrlock>=0.5 (from cupy) Using cached fastrlock-0.8.3-cp312-cp312-win_amd64.whl.metadata (7.9 kB) Using cached fastrlock-0.8.3-cp312-cp312-win_amd64.whl (31 kB) Building wheels for collected packages: cupy Building wheel for cupy (pyproject.toml): started Building wheel for cupy (pyproject.toml): finished with status 'error' Failed to build cupy error: subprocess-exited-with-error Building wheel for cupy (pyproject.toml) did not run successfully. exit code: 1 [73 lines of output] Clearing directory: C:\Users\Administrator\AppData\Local\Temp\pip-install-51_3qmlh\cupy_8ad1d1feb8f74bcd9b3c395c1deddc99\cupy\.data Generating CUPY_CACHE_KEY from header files... CUPY_CACHE_KEY (1717 files matching C:\Users\Administrator\AppData\Local\Temp\pip-install-51_3qmlh\cupy_8ad1d1feb8f74bcd9b3c395c1deddc99\cupy\_core\include\**): 0bb7da13cbc36729b5a184678063e5ebaecfdde7 Looking for NVTX from: ['C:\\Program Files\\NVIDIA Corporation\\Nsight Systems 2025.3.2\\target-windows-x64\\nvtx'] Using NVTX at: C:\Program Files\NVIDIA Corporation\Nsight Systems 2025.3.2\target-windows-x64\nvtx -------- Configuring Module: cuda -------- Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/ ************************************************** *** WARNING: Cannot check compute capability Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/ ************************************************** ************************************************************ * CuPy Configuration Summary * ************************************************************ Build Environment: Include directories: ['C:\\Users\\Administrator\\AppData\\Local\\Temp\\pip-install-51_3qmlh\\cupy_8ad1d1feb8f74bcd9b3c395c1deddc99\\cupy/_core/include\\cupy/_cccl/libcudacxx', 'C:\\Users\\Administrator\\AppData\\Local\\Temp\\pip-install-51_3qmlh\\cupy_8ad1d1feb8f74bcd9b3c395c1deddc99\\cupy/_core/include\\cupy/_cccl/thrust', 'C:\\Users\\Administrator\\AppData\\Local\\Temp\\pip-install-51_3qmlh\\cupy_8ad1d1feb8f74bcd9b3c395c1deddc99\\cupy/_core/include\\cupy/_cccl/cub', 'C:\\Users\\Administrator\\AppData\\Local\\Temp\\pip-install-51_3qmlh\\cupy_8ad1d1feb8f74bcd9b3c395c1deddc99\\cupy/_core/include', 'C:\\Program Files\\NVIDIA Corporation\\Nsight Systems 2025.3.2\\target-windows-x64\\nvtx\\include'] Library directories: [] nvcc command : (not found) hipcc command : (not found) Environment Variables: CFLAGS : (none) LDFLAGS : (none) LIBRARY_PATH : (none) CUDA_PATH : (none) NVCC : (none) HIPCC : (none) ROCM_HOME : (none) Modules: cuda : No -> Include files not found: ['cublas_v2.h', 'cuda.h', 'cuda_profiler_api.h', 'cuda_runtime.h', 'cufft.h', 'curand.h', 'cusparse.h'] -> Check your CFLAGS environment variable. ERROR: CUDA could not be found on your system. HINT: You are trying to build CuPy from source, which is NOT recommended for general use. Please consider using binary packages instead. Please refer to the Installation Guide for details: https://docs.cupy.dev/en/stable/install.html ************************************************************ Traceback (most recent call last): File "C:\Users\Administrator\PyCharmMiscProject\.venv\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 389, in <module> main() File "C:\Users\Administrator\PyCharmMiscProject\.venv\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 373, in main json_out["return_val"] = hook(**hook_input["kwargs"]) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Administrator\PyCharmMiscProject\.venv\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 280, in build_wheel return _build_backend().build_wheel( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Temp\pip-build-env-sqtmjs6t\overlay\Lib\site-packages\setuptools\build_meta.py", line 435, in build_wheel return _build(['bdist_wheel', '--dist-info-dir', str(metadata_directory)]) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Temp\pip-build-env-sqtmjs6t\overlay\Lib\site-packages\setuptools\build_meta.py", line 423, in _build return self._build_with_temp_dir( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Temp\pip-build-env-sqtmjs6t\overlay\Lib\site-packages\setuptools\build_meta.py", line 404, in _build_with_temp_dir self.run_setup() File "C:\Users\Administrator\AppData\Local\Temp\pip-build-env-sqtmjs6t\overlay\Lib\site-packages\setuptools\build_meta.py", line 317, in run_setup exec(code, locals()) File "<string>", line 59, in <module> File "C:\Users\Administrator\AppData\Local\Temp\pip-install-51_3qmlh\cupy_8ad1d1feb8f74bcd9b3c395c1deddc99\install\cupy_builder\cupy_setup_build.py", line 562, in get_ext_modules extensions = make_extensions(ctx, compiler, use_cython) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Temp\pip-install-51_3qmlh\cupy_8ad1d1feb8f74bcd9b3c395c1deddc99\install\cupy_builder\cupy_setup_build.py", line 403, in make_extensions raise Exception('Your CUDA environment is invalid. ' Exception: Your CUDA environment is invalid. Please check above error log. [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for cupy [notice] A new release of pip is available: 25.1.1 -> 25.3 [notice] To update, run: python.exe -m pip install --upgrade pip ERROR: Failed to build installable wheels for some pyproject.toml based projects (cupy) Process finished with exit code 1
最新发布
11-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值