initiatesystemshudown equivalent in linux

In Windows, the function InitiateSystemShutdown  can shutdown or reboot the system. In linux, we can achive this with shutdown or reboot command running as root or sudo.
    However, when we pass such commands in linux, all the processes stop working.

   I am currently porting my source code from windows to linux. and InitiateSystemShutdown  system call is written an API, which is called by some modules.
      InitiateSystemShutdown  system calls returns status to caller and caller can take the action on the same.
    In linux, if shutdown or reboot commands fails, then my caller will get the status.
     
Questions:
  1)  How I can pass time parameter in shutdown or linux command. The parameter required in seconds. e.g. 5 seconds.
 
  2) Is there any check to know whether I have authority to shutdown / reboot the system. So that based on that authority I can return some status to caller
The parameter passed to InitiateSystemShutdown  functions are
 
BOOL WINAPI InitiateSystemShutdown(
  __in_opt  LPTSTR lpMachineName,
  __in_opt  LPTSTR lpMessage,
  __in      DWORD dwTimeout,
  __in      BOOL bForceAppsClosed,
  __in      BOOL bRebootAfterShutdown
);
 
Parameters
lpMachineName [in, optional] 
The network name of the computer to be shut down. If lpMachineName is NULL or an empty string, the function shuts down the local computer.
 
lpMessage [in, optional] 
The message to be displayed in the shutdown dialog box. This parameter can be NULL if no message is required. 
 
dwTimeout [in] 
The length of time that the shutdown dialog box should be displayed, in seconds. While this dialog box is displayed, the shutdown can be stopped by the AbortSystemShutdown function. 
 
If dwTimeout is not zero, InitiateSystemShutdown displays a dialog box on the specified computer. The dialog box displays the name of the user who called the function, displays the message specified by the lpMessage parameter, and prompts the user to log off. The dialog box beeps when it is created and remains on top of other windows in the system. The dialog box can be moved but not closed. A timer counts down the remaining time before a forced shutdown.
 
If dwTimeout is zero, the computer shuts down without displaying the dialog box, and the shutdown cannot be stopped by AbortSystemShutdown.
 
bForceAppsClosed [in] 
If this parameter is TRUE, applications with unsaved changes are to be forcibly closed. Note that this can result in data loss.
 
If this parameter is FALSE, the system displays a dialog box instructing the user to close the applications.
 
bRebootAfterShutdown [in] 
If this parameter is TRUE, the computer is to restart immediately after shutting down. If this parameter is FALSE, the system flushes all caches to disk and safely powers down the system.
 
Return Value
If the function succeeds, the return value is nonzero.
 
If the function fails, the return value is zero. To get extended error information, call GetLastError.

Open in new window

ashish_manglaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

techniousCommented:
shutdown t 5  r your computer will shutdown in 5 seconds
This command will cause the system to reboot in 5 seconds. Change the 5 to another value in seconds if desired.
0
techniousCommented:
shutdown -t 5 -r your computer will restart in 5 seconds

This command will cause the system to reboot in 5 seconds. Change the 5 to another value in seconds if desired.

(Sorry Word Hacked up my dashes)

shutdown -t 5 -r -k your computer will restart in 5 seconds

This command will complete if you have privileges, but will not actually initiate the restart. This command will only display the message to your terminal. You can also redirect your output if you want to use it in a script.
0
joolsCommented:
man shutdown gives you loads of info about the options...

or you could;
   sleep <number of seconds here>
   shutdown -r 0 (reboot)
   shutdown -h 0

I occasionally use sleep in scripts because it's easier to break out.
0
evilrixSenior Software Engineer (Avast)Commented:
http://linux.die.net/man/2/reboot

Description
The reboot() call reboots the system, or enables/disables the reboot keystroke (abbreviated CAD, since the default is Ctrl-Alt-Delete; it can be changed using loadkeys(1)).

This system call will fail (with EINVAL) unless magic equals LINUX_REBOOT_MAGIC1 (that is, 0xfee1dead) and magic2 equals LINUX_REBOOT_MAGIC2 (that is, 672274793). However, since 2.1.17 also LINUX_REBOOT_MAGIC2A (that is, 85072278) and since 2.1.97 also LINUX_REBOOT_MAGIC2B (that is, 369367448) and since 2.5.71 also LINUX_REBOOT_MAGIC2C (that is, 537993216) are permitted as value for magic2. (The hexadecimal values of these constants are meaningful.) The flag argument can have the following values:

LINUX_REBOOT_CMD_RESTART
    (RB_AUTOBOOT, 0x1234567). The message 'Restarting system.' is printed, and a default restart is performed immediately. If not preceded by a sync(2), data will be lost.
LINUX_REBOOT_CMD_HALT
    (RB_HALT_SYSTEM, 0xcdef0123; since 1.1.76). The message 'System halted.' is printed, and the system is halted. Control is given to the ROM monitor, if there is one. If not preceded by a sync(2), data will be lost.
LINUX_REBOOT_CMD_POWER_OFF
    (0x4321fedc; since 2.1.30). The message 'Power down.' is printed, the system is stopped, and all power is removed from the system, if possible. If not preceded by a sync(2), data will be lost.
LINUX_REBOOT_CMD_RESTART2
    (0xa1b2c3d4; since 2.1.30). The message 'Restarting system with command '%s'' is printed, and a restart (using the command string given in arg) is performed immediately. If not preceded by a sync(2), data will be lost.
LINUX_REBOOT_CMD_CAD_ON
    (RB_ENABLE_CAD, 0x89abcdef). CAD is enabled. This means that the CAD keystroke will immediately cause the action associated with LINUX_REBOOT_CMD_RESTART.
LINUX_REBOOT_CMD_CAD_OFF
    (RB_DISABLE_CAD, 0). CAD is disabled. This means that the CAD keystroke will cause a SIGINT signal to be sent to init (process 1), whereupon this process may decide upon a proper action (maybe: kill all processes, sync, reboot).

Only the superuser may use this function.
#include<stdlib.h>
#include<stdio.h>
#include<memory.h>
#include<time.h>
#include<ctype.h>
#include<unistd.h>
#include<linux/reboot.h>
 
int shut(int delay int action)
{
   sleep(delay);     
   return reboot(action);
}
 
int main(void)
{   
    int i=0;   
    printf("shutdowning...\n")
       
    int retval = shut(10, LINUX_REBOOT_CMD_RESTART);     
 
    // Handle if retval is error state
    
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fpmurphyCommented:
Note that your application needs the  CAP_SYS_BOOT capability for reboot() to succeed.  See man capabilities(7).  You can check what capabilities are available to your application before it calls reboot() using capget() or cap_get_proc().
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux Distributions

From novice to tech pro — start learning today.