在更新程序重新启动自我 [英] Program restart self on update

查看:147
本文介绍了在更新程序重新启动自我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处检查,所以我不希望重复的问题。

I checked everywhere so I am hopefully not repeating a question.

我要便携更新功能添加到一些C code我写。该计划可能不会在任何特定的位置,我会preFER随时给单个二进制(无动态库加载)

I want to add a portable update feature to some C code I am writing. The program may not be in any specific location, and I would prefer to keep it to a single binary (No dynamic library loading)

然后,更新完成后,我希望程序能够重新启动(而不是一个循环,实际上是从硬盘重新加载)

Then after the update is complete, I want the program to be able to restart (not a loop, actually reload from the HDD)

有没有办法做到这在C在Linux上?

Is there any way to do this in C on Linux?

推荐答案

如果您知道程序保存在磁盘上,然后就可以执行exec()程序:

If you know where the program is saved on disk, then you can exec() the program:

char args[] = { "/opt/somewhere/bin/program", 0 };

execv(args[0], args);
fprintf(stderr, "Failed to reexecute %s\n", args[0]);
exit(1);

如果你不知道那里的程序是在磁盘上,或者使用 execvp()来寻找它的$ PATH,或找出来。在Linux上,使用的/ proc 文件系统 - 和的/ proc /自/ EXE 明确;它是一个符号链接到可执行文件,所以您需要使用<一个href=\"http://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html\"><$c$c>readlink()得到的值。当心:的readlink()不空终止读取字符串

If you don't know where the program is on disk, either use execvp() to search for it on $PATH, or find out. On Linux, use the /proc file system — and /proc/self/exe specifically; it is a symlink to the executable, so you would need to use readlink() to get the value. Beware: readlink() does not null terminate the string it reads.

如果你愿意,你可以安排通过它指示它更新后正在重新启动新工艺参数;因为你需要(在文件列表当前打开进行编辑,也许,或其他任何适当的信息和选项),我公司提供的最低限度的参数列表可以是复杂的。

If you want, you can arrange to pass an argument which indicates to the new process that it is being restarted after update; the bare minimum argument list I provided can be as complex as you need (a list of the files currently open for edit, perhaps, or any other appropriate information and options).

另外,不要忘了重新执行之前清理 - 干净地关闭所有打开的文件,例如。请记住,打开的文件描述符被执行的进程继承(除非你将它们标记为封闭的 EXEC FD_CLOEXEC O_CLOEXEC ),但新的进程不会知道他们在做什么,除非你告诉它(参数列表),所以它不能使用它们。他们将只是弄乱过程中无需帮助中最少的。

Also, don't forget to clean up before reexecuting — cleanly close any open files, for example. Remember, open file descriptors are inherited by the executed process (unless you mark them for closure on exec with FD_CLOEXEC or O_CLOEXEC), but the new process won't know what they're for unless you tell it (in the argument list) so it won't be able to use them. They'll just be cluttering up the process without helping in the least.

这篇关于在更新程序重新启动自我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆