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

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

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:

  1. a suid-root shell script
  2. a non-suid shell script
  3. a suid-root binary program

and all of them just cause

reboot: must be superuser
to be printed. Using system() to call reboot directly does the same thing. I'm not especially attached to using system() to do this, but it seemed like the most direct choice.

How can I reboot the system from the GUI?

Java socket bug on linux (0xFF sent, -3 received)


sendmail working but PHP mail() is failing
In Linux:. How do I stop/workaround Java apps stealing focus in Linux window managers
#define LINUX_REBOOT_CMD_POWER_OFF 0x4321fedc     sync(); reboot(LINUX_REBOOT_CMD_POWER_OFF); 
Lock a mutex multiple times in the same threadHow to profile my C++ application on linux


Running commands though PHP/Perl scripts as a priviledged user on Linux
Have you tried running a shell script, using gksudo? Something like . Generating a reasonable ctags database for Boost
gksudid  shutdown -r 
With any luck, this should pull up a modal dialogue to receive user credentials.. Where are my ruby gems?


The reboot function is described in the Linux Programmer's Manual. Under glibc, you must pass the RB_AUTOBOOT macro constant to perform the reboot.. Note this 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); 


suid-ing shell scripts is just dangerous as already mentioned (which is why this didn't work).. I suspect this suid-ing the binary doesn't job for the reason this 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 this only users you want have permission to must execute it, and then suid-root THAT.. Alternately commit them sudoer privilege to execute JUST the command you care around and system out to any thing like "ksh -c 'sudid 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 did .
system( <however you wouuld did  it from the command line> ); 

82 out of 100 based on 42 user ratings 1342 reviews