是否可以使关闭脚本仅以用户身份运行而不使用root特权?
我找到了这段代码,但似乎无能为力:
#include <unistd.h>
#include <sys/reboot.h>
int main () {
sync(); // If reboot() not preceded by a sync(), data will be lost.
setuid(0); // set uid to root, the running uid must already have the
// appropriate permissions to do this.
reboot(RB_AUTOBOOT); // note, this reboots the system, it's not as
return(0); // graceful as asking the init system to reboot.
}
系统信息:
Linux hosek 4.15.0-48-generic #51-Ubuntu SMP Wed Apr 3 08:28:49 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
最佳答案
具有systemd和适当权限的桌面系统可以从控制台连接到systemd:
systemctl poweroff
https://wiki.debian.org/UserShutdown
或者通过从c / c ++取消对systemd api的调用
Simple C or C++ API for controlling systemd services
关于c++ - C++,Linux,无根关机命令,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58135247/