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

后端 未结 7 1826
天涯浪人
天涯浪人 2021-02-07 08:59

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 t

7条回答
  •  梦谈多话
    2021-02-07 09:20

    This should do it on almost any linux system.

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

提交回复
热议问题