简体   繁体   English

linux覆盖运行二进制文件

[英]linux overwrite running binary

How would i do this in linux using C ? 我如何使用C在linux中执行此操作? I need to write update function that downloads update using wget, then replaces old file with an update, and then starts update file. 我需要编写使用wget下载更新的更新功能,然后用更新替换旧文件,然后启动更新文件。 One idea was using bash script but that didn't work out. 一个想法是使用bash脚本,但没有成功。 Any suggestions? 有什么建议么?

EDIT:here is code similiar to what i tried 编辑:这里的代码与我尝试的类似

char *p_name = "example"; /* name of the running executable */

void update(char *update_url)
{
        if(!fork())
                /* download file to temporary location */
                execlp("wget", "wget" "-q", update_url, "-O", "tmp", NULL);
        wait(NULL);
        FILE *fp = fopen("tmp.sh", "w");
        /* write bash script */
        fprintf(fp, "sleep 5\nmv tmp %s\nchmod +x %s\nrm tmp.sh",
                p_name, p_name);
        fclose(fp);
        execlp("bash", "bash", "tmp.sh", NULL);
}

Overwriting a running program doesn't actually overwrite the file, it basically creates a new file, while marking the old file to be removed when the program exits. 覆盖正在运行的程序实际上并不会覆盖该文件,它基本上会创建一个新文件,同时标记要在程序退出时删除的旧文件。

After your script have downloaded the new program, you have to kill the running program, and then restart it. 脚本下载新程序后,必须kill正在运行的程序,然后重新启动它。 It will start with the new program. 它将从新计划开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM