如何从 C++ 程序内部重新启动 Linux?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2678766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to restart Linux from inside a C++ program?
提问by Dave K
I have a Qt 4 GUI where I need to have a option in a drop-down menu that allows the user to choose to restart the computer. I realize this might seem redunant with the ability to restart the computer in other ways, but the choice needs to stay there. I've tried using system() to call the following:
我有一个 Qt 4 GUI,我需要在下拉菜单中有一个选项,允许用户选择重新启动计算机。我意识到这与以其他方式重新启动计算机的能力似乎是多余的,但选择需要留在那里。我尝试使用 system() 调用以下内容:
- a suid-root shell script
- a non-suid shell script
- a suid-root binary program
- 一个 suid-root shell 脚本
- 非 suid shell 脚本
- 一个 suid-root 二进制程序
and all of them just cause
所有这些都只是导致
reboot: must be superuser要打印。使用 system() 直接调用 reboot 可以做同样的事情。我并不是特别喜欢使用 system() 来做到这一点,但这似乎是最直接的选择。
How can I reboot the system from the GUI?
如何从 GUI 重新启动系统?
回答by Vilhelm Gray
The reboot
function is described in the Linux Programmer's Manual. Under glibc, you can pass the RB_AUTOBOOT
macro constant to perform the reboot.
该reboot
函数在Linux Programmer's Manual 中有描述。在glibc 下,您可以传递RB_AUTOBOOT
宏常量来执行重启。
Note that if reboot
is not preceded by a call to sync
, data may be lost.
请注意,如果reboot
前面没有调用sync
,则数据可能会丢失。
Using glibcin Linux:
在Linux 中使用glibc:
#include <unistd.h>
#include <sys/reboot.h>
sync();
reboot(RB_AUTOBOOT);
回答by Mehdi
In Linux:
在 Linux 中:
#define LINUX_REBOOT_CMD_POWER_OFF 0x4321fedc
sync();
reboot(LINUX_REBOOT_CMD_POWER_OFF);
回答by Joseph Salisbury
Have you tried running a shell script, using gksudo? Something like
您是否尝试过使用 gksudo 运行 shell 脚本?就像是
gksudo shutdown -r
With any luck, that should pull up a modal dialogue to get user credentials.
运气好的话,应该会弹出一个模态对话框来获取用户凭据。
回答by Mark B
suid-ing shell scripts is just dangerous as already mentioned (which is why that didn't work).
suid-ing shell 脚本就像已经提到的一样危险(这就是为什么它不起作用)。
I suspect that suid-ing the binary doesn't work because system spawns its subprocess with the user's actual uid and not the suid one, again for security reasons (it would let you substitute any binary for the one being called and run it as root).
我怀疑 suid-ing 二进制文件不起作用,因为系统使用用户的实际 uid 而不是 suid 生成其子进程,再次出于安全原因(它可以让您替换任何二进制文件来代替被调用的二进制文件并以 root 身份运行它)。
You could put a copy of reboot in a location protected such that only users you want have permission to can execute it, and then suid-root THAT.
您可以将 reboot 的副本放在受保护的位置,以便只有您希望有权执行它的用户才能执行它,然后 suid-root THAT。
Alternately give them sudoer privilege to execute JUST the command you care about and system out to something like "ksh -c 'sudo reboot'"
或者给他们 sudoer 特权来执行你关心的命令和系统输出到类似的东西 "ksh -c 'sudo reboot'"
回答by oxagast
This should do it on almost any linux system.
这应该在几乎所有 linux 系统上执行。
#include <unistd.h>
#include <sys/reboot.h>
int main () {
sync();
setuid(0);
reboot(RB_AUTOBOOT);
return(0);
}
Then just compile with gcc reboot.c -o reboot
and do chmod a+s reboot
on the binary. Then call reboot as any user and the system should reboot smoothly. The way you do this through your GUI varies, as in if your Desktop Environment was KDE for example, it's quite different than doing the same thing under Fluxbox.
然后只需编译gcc reboot.c -o reboot
并执行chmod a+s reboot
二进制文件。然后以任何用户身份调用reboot,系统应该可以顺利重启。您通过 GUI 执行此操作的方式各不相同,例如,如果您的桌面环境是 KDE,则它与在 Fluxbox 下执行相同操作的方式完全不同。
回答by el.pescado
In binary try to call
在二进制尝试调用
setuid (0);
before system() call.
在 system() 调用之前。
回答by frankster
how would you reboot the system from the command line on your system?
您将如何从系统上的命令行重新启动系统?
basically do
基本上做
system( <however you wouuld do it from the command line> );