View Full Version: system reboot

C++ Learning Community > C++ for Linux > system reboot


Title: system reboot
Description: how?


IllegalEnzyme - June 8, 2005 09:49 AM (GMT)
ok i want my program to be able to reboot my comp, easy, i decided to use:

CODE
system("shutdown -r now");


but i need to be logged in as root for that to work, so:

CODE
system("su");
system("shutdown -r now");


however that suspends the user and so won't work! also i can't send the root password from the prog. is there any way of allowing a local user to to call the shutdown command?

Incubator - June 8, 2005 09:58 AM (GMT)
you can with sudo but thats not secure.

kdm/gdm can do it because they run as root

but every other child prog has to get the password from hte user.
That is why I must enter the password when I want to change some system settings in the admin mode in kcontrol

to check wether the caller of the prog is root or user use the getuid() function in one of the gnu c headers

0 = root and 1000 is my first user

if the user is already root then system("su"); should not be called

myork - June 8, 2005 02:16 PM (GMT)
CODE
system("su");
system("shutdown -r now");


Remember that each system() call spawns its own shell in which the command is executed. So the effect of the su command in the first call does not affect the second system command.

You could Use:

CODE
system("sudo shutdown -r now");


But that assumes that you have set up the sudo command for the current user correctly and allow him/her to call shutdown.

Assuming you dont want to give every user shutdown privaledges. You need to make the program execute as if it had been started by a privaledged user. To to this you need to set the suid flag.
CODE
chmod +s <fileName>


When this flag is set the program will execute with the same permissions as the owner of the file. So you will also need to do:

CODE
chown <PrivaledgedUser> <fileName>


You could set PrivaledgedUser as root (but that is a bad idea for the long term).

When you have it all working create a new user 'shutdownUser' and set the password '*' in the password file so that nobody can log in as this user. Do the above 'chown' so the file is owned by 'shutdownUser'

Then add 'shutdownUser' to the list of people that can execute shutdown in the sudoers list.




Hosted for free by InvisionFree