LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-26-2012, 03:33 AM   #1
KillerCode
Member
 
Registered: Nov 2010
Posts: 57

Rep: Reputation: 0
Shutdown or Reboot PC?


i know the commands "shutdown" and "reboot", but they require root privilages.
but on any desktop enviroment (tested with GNOME and Xfce) the gui "Shutdown" buttons dont ask for root password when u click shutdown or reboot buttons.

is there anyway i can write a code in c to reboot or shutdown my linux box without requiring root privilages or password?

thanks
 
Old 01-26-2012, 04:50 AM   #2
eSelix
Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Kubuntu
Posts: 811

Rep: Reputation: 163Reputation: 163
Because Xserver was started on root account so it can do that. Anyway one of the simplest method is using sudo, just edit sudoers by "visudo", and add following line at the end
Code:
ALL ALL = NOPASSWD: /sbin/shutdown, /sbin/poweroff, /sbin/reboot
and add aliases to these commands like:
Code:
alias shutdown='sudo /usr/sbin/shutdown'
Sorry, I didn't notice you write in "Programming" section. This applies to normal user usage. I don't known how to and with what communicate to ask system about shutdown.

Last edited by eSelix; 01-26-2012 at 04:55 AM.
 
Old 01-26-2012, 06:45 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,555
Blog Entries: 3

Rep: Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816
I agree, sudo and aliases are the best option.

If you don't want or cannot use sudo, you can use C wrapper programs. (This is the Programming forum, after all -- I would not mention this in any other forum. Consider this just an interesting side note, not a suggestion on how to do it in a normal situation.)

poweroff.c:
Code:
#define _GNU_SOURCE
#include <unistd.h>

int main(int argc, char *argv[])
{
   if (setresgid(0, 0, 0)) return 126;
   if (setresuid(0, 0, 0)) return 125;
   execl("/sbin/shutdown", "shutdown", "-P", "now", NULL);
   return 127;
}
reboot.c:
Code:
#define _GNU_SOURCE
#include <unistd.h>

int main(int argc, char *argv[])
{
   if (setresgid(0, 0, 0)) return 126;
   if (setresuid(0, 0, 0)) return 125;
   execl("/sbin/shutdown", "shutdown", "-r", "now", NULL);
   return 127;
}
Compile and install as e.g. /usr/local/bin/user-reboot and /usr/local/bin/user-poweroff using
Code:
gcc poweroff.c -Wall -O3 -s -o user-poweroff
gcc reboot.c   -Wall -O3 -s -o user-reboot
sudo install -m 04750 -o root -g group user-poweroff /usr/local/bin/user-poweroff
sudo install -m 04750 -o root -g group user-reboot   /usr/local/bin/user-reboot
and any user belonging to the group group can now power off and reboot the computer.

The way this works is that the wrapper programs are set setuid-root (u+s, the 04000 in the mode), but only root and the group are allowed to run them.

The setresgid() and setresuid() calls in the wrappers are not required, actually, but I like to be thorough. The fact that the setuid bit sets the effective user id to root should suffice for /sbin/shutdown. The calls just leverage the effective rootness to set all ids to root.

If you want to name individual users, or more than one group, use POSIX ACLs. You'll need to add the acl mount option to the filesystem in /etc/fstab. Then, to allow user someuser and/or group somegroup, use
Code:
setfacl -m u:someuser:rx /usr/local/bin/user-{reboot,poweroff}
setfacl -m g:somegroup:rx /usr/local/bin/user-{reboot,poweroff}
As with sudo, you can add aliases for shutdown, poweroff and reboot to the user shell startup files (format and location depends on which shells they use). This way system stuff is not affected, only users see the added functionality.

One might be tempted to allow any user to execute the wrapper, but check the real user id in the C program. Do not do that; sudo was developed to do exactly that, and it does it extremely well. No need to reinvent the wheel.

Finally, if you need to reboot or shutdown from C code, just execute either sudo shutdown ... or the wrappers above, using
Code:
#include <unistd.h>

execl("/usr/bin/sudo", "sudo", "/usr/sbin/shutdown", "-r", "now", NULL); /* Reboot */
  or
execl("/usr/local/bin/user-reboot", "user-reboot", NULL);                /* Reboot */
  or
execl("/usr/bin/sudo", "sudo", "/usr/sbin/shutdown", "-P", "now", NULL); /* Poweroff */
  or
execl("/usr/local/bin/user-poweroff", "user-poweroff", NULL);            /* Poweroff */
Although normally you would fork() before calling the exec, in these cases your program should be ready to exit anyway, so instead of exiting, you could just call the poweroff/reboot function.

If you had an application suite, say an embedded device GUI, it might be better to use the wrapper programs instead of sudo. In that case, you'd use /usr/lib/application/poweroff and /usr/lib/application/reboot for the wrappers, only allowing access to the dedicated group that runs the application.

Last edited by Nominal Animal; 01-26-2012 at 06:59 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to shutdown/reboot server, no shutdown process running dctw Linux - Server 5 03-31-2010 05:46 AM
[FYI] "last -f <old wtmp> -x reboot shutdown" incorrect for last reboot and shutdown catkin Linux - General 1 03-25-2010 11:52 PM
shutdown but no reboot alma Suse/Novell 2 11-17-2005 11:11 PM
shutdown -h causes reboot obrienj Red Hat 1 09-11-2005 09:52 PM
Immediate shutdown/reboot Creep Linux - Hardware 4 03-07-2004 04:56 AM


All times are GMT -5. The time now is 05:21 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration