How to restart Linux from inside a C++ program?

我是研究僧i 提交于 2019-12-03 05:37:57

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.

Note that if reboot is not preceded by a call to sync, data may be lost.

Using glibc in Linux:

#include <unistd.h>
#include <sys/reboot.h>

sync();
reboot(RB_AUTOBOOT);
Mehdi

In Linux:

#define LINUX_REBOOT_CMD_POWER_OFF 0x4321fedc   

sync();
reboot(LINUX_REBOOT_CMD_POWER_OFF);

Have you tried running a shell script, using gksudo? Something like

gksudo shutdown -r

With any luck, that should pull up a modal dialogue to get user credentials.

suid-ing shell scripts is just dangerous as already mentioned (which is why that didn't work).

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).

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.

Alternately give them sudoer privilege to execute JUST the command you care about and system out to something like "ksh -c 'sudo reboot'"

In binary try to call

setuid (0);

before system() call.

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> );

This should do it on almost any linux system.

#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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!