LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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
  Search this Thread
Old 01-22-2006, 09:45 AM   #1
eating
LQ Newbie
 
Registered: Jan 2006
Location: japan
Distribution: Red Hat Enterprise 4
Posts: 27

Rep: Reputation: 15
ExitWindowsEx equivalent in LINUX


Hi, everyone.

I`ve been using ExitWindowsEx() function to programmatically shutdown computers running Windows. Is there an equivalent function call (in C/C++) to shutdown linux computers?

I have tried init(), shutdown(), reboot()...these just doesnt work. =( I need a utility for "ordinary" users to shutdown the computer programmatically.

Thanks for the reply


eating
 
Old 01-22-2006, 12:38 PM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
I have a question, thinking about what you're trying to do here, can Windows really shutdown the computer from an application run by a normal user, or does the user need to be an "administrator"?
 
Old 01-22-2006, 03:48 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
eating -

The easiest approach would be something like
Code:
system ("shutdown -h now");
randyding -

Yes, the user would need to appropriate permissions (e.g. "administrator", "administrators group", "domain administrators", etc etc) to order to successfully perform a system shutdown. But I've seen simple 5-line C programs where a completely non-privileged user can cause Windows to blue-screen ;-)
 
Old 01-22-2006, 05:27 PM   #4
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Nice!
I did some testing on Redhat, if I system("/usr/bin/reboot") it works because its really a symlink to...
/usr/bin/reboot -> consolehelper
And this goes through the pam.d authentication, where it verifies you are logged into the machine locally before running it.
If I use /sbin/reboot instead it says "you must be root to do that".
Now the default behavior, on Redhat at least, is that shutdown doesn't work that way but you certainly could create the symlink yourself and add the appropriate /etc/pam.d/shutdown config file... then a normal user could shut down the computer.
Anyway its always been somewhat of a mystery to me why Redhat lets the reboot command instantly reboot the computer. I accidently did that once thinking I was logged into an embedded linux system that needed rebooting and did it to my workstation instead.
 
Old 01-22-2006, 10:46 PM   #5
eating
LQ Newbie
 
Registered: Jan 2006
Location: japan
Distribution: Red Hat Enterprise 4
Posts: 27

Original Poster
Rep: Reputation: 15
randyding, my code can forcefully shutdown windows computers.. even by a "guest" user.

paulsm4, thanks, i have tried
system ("shutdown now");
and it doesnt work even as a root. I havent tried
system ("shutdown -h now");
though. Also, is there any way to prompt the user to enter the root password to allow any user shutdown the computer?
 
Old 01-22-2006, 11:37 PM   #6
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Heh, why does that not surprise me about the windows "guest" behavior.
Anyway, I was trying to point out a possible solution for you.
Here is a step by step, I just tested it, and it does work (on my Redhat).

1. Create symbolic link
# ln -s /usr/bin/consolehelper /usr/bin/shutdown

2. Create pam config file /etc/pam.d/shutdown
Note I copied the contents of the reboot file which contained the following.
Code:
#%PAM-1.0
auth       sufficient   pam_rootok.so
auth       required     pam_console.so
#auth       required    pam_stack.so service=system-auth
account    required     pam_permit.so
3. Create privileged app file /etc/security/console.apps/shutdown
Note as far as I can tell the contents of this file does not matter.
It can probably be an empty file but must exist.
I copied the contents of the reboot file which contained the following.
Code:
FALLBACK=true
4. Now any USER applications that does the following will work as expected.
Code:
system("/usr/bin/shutdown -h now");
 
Old 01-23-2006, 08:57 AM   #7
eating
LQ Newbie
 
Registered: Jan 2006
Location: japan
Distribution: Red Hat Enterprise 4
Posts: 27

Original Poster
Rep: Reputation: 15
It WORKS!! Thanks for the detailed explanation, randyding (and to paulsm4). Seems like i'll visit this site more often.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
webcamXP equivalent for linux?? forgiste Linux - Networking 1 07-08-2005 04:26 PM
What's the Linux equivalent of PhotoShop? ShadowMetis Linux - Software 3 12-06-2004 11:46 PM
Is there a Powerpoint equivalent for Linux? Nukem Linux - General 4 11-06-2004 03:30 PM
What is the Linux equivalent of ADaware? studpenguin Linux - Newbie 4 03-28-2004 04:27 PM
TurboTax or equivalent for linux? pekuekfir Linux - General 5 09-17-2003 06:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:37 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration