我正在尝试编写一个可以查找URL的C程序,并且可以使用它的新版本,它应该能够更新自己.
我试过的方法:
forkout一个新的进程下载新的二进制文件说BINARY.tmp,我用来forkout的代码是:
int
forkout_cmd(char *cmdstr) {
pid_t pid;
char *cmd[4];
cmd[0] = "/bin/bash";
cmd[1] = "-c";
cmd[2] = cmdstr;
cmd[3] = NULL;
pid = vfork();
if( pid == -1 ) {
logmsg("Forking for upgradation failed.");
return -1;
}else if( pid == 0 ){
/* we are in child process */
execvp(cmd[0], cmd);
logmsg("execl failed while executing upgradation job.");
}else{
/* need not to wait for the child to complete. */
wait(NULL);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)新进程尝试覆盖原始BINARY
例如,您可能会考虑分叉的例程:
forkout_cmd("wget -O BINARY.tmp https://someurl.com/BINARY_LATEST; /bin/mv -f BINARY.tmp BINARY");
Run Code Online (Sandbox Code Playgroud)但是,覆盖失败,因为原始二进制文件仍在执行中,因此在磁盘上忙,有人可以在这里提供一些建议来克服这个问题.
提前致谢.