Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 927
Dave K Avatar asked Apr 20 '10 21:04

Dave K


People also ask

How to restart or reboot Linux server from the command line?

How to Restart or Reboot Linux Server from the Command Line. Contents. Steps to Restart Linux using Command Prompt. Restarting Local Linux Operating System. Step 1: Open Terminal Window. Step 2: Use the shutdown Command. Alternative Option: Restart Linux with reboot Command. Reboot Remote Linux Server.

How do I restart a C++ program from terminal?

You can simply use terminal for starting the application. Like if you wish to start firefox simply typing in terminal will open your application. But if you wish to restart from your C++ script. This can be done by simply piping your command to shell.

How do I reboot the system using the -r command?

When used with the -r option, the shutdown command performs a system reboot: By default, the system will be rebooted after 1 minute, but you can specify the exact time when you want the system to be rebooted. The time argument can have two different formats.

How do I restart Linux with sudo?

The sudo command tells Linux to run the command as an administrator, so you may need to type your password. The –r switch at the end indicates that you want the machine to restart. Note: See our article for additional Linux shutdown command options. Many Linux versions do not require administrator privileges to reboot.


3 Answers

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);
like image 182
Vilhelm Gray Avatar answered Sep 29 '22 20:09

Vilhelm Gray


In Linux:

#define LINUX_REBOOT_CMD_POWER_OFF 0x4321fedc   

sync();
reboot(LINUX_REBOOT_CMD_POWER_OFF);
like image 25
Mehdi Avatar answered Sep 29 '22 22:09

Mehdi


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.

like image 28
Joseph Salisbury Avatar answered Sep 29 '22 20:09

Joseph Salisbury