Linux shutdown/reboot process

Peter Teoh htmldeveloper at gmail.com
Tue May 15 01:55:41 EDT 2012


At the kernel, the file involved are kernel/sys.c (just a few functions):

/**
 *      kernel_halt - halt the system
 *
 *      Shutdown everything and perform a clean system halt.
 */
void kernel_halt(void)
{
        kernel_shutdown_prepare(SYSTEM_HALT);
        syscore_shutdown();
        printk(KERN_EMERG "System halted.\n");
        kmsg_dump(KMSG_DUMP_HALT);
        machine_halt();
}

EXPORT_SYMBOL_GPL(kernel_halt);

/**
 *      kernel_power_off - power_off the system
 *
 *      Shutdown everything and perform a clean system power_off.
 */
void kernel_power_off(void)
{
        kernel_shutdown_prepare(SYSTEM_POWER_OFF);
        if (pm_power_off_prepare)
                pm_power_off_prepare();
        disable_nonboot_cpus();
        syscore_shutdown();
        printk(KERN_EMERG "Power down.\n");
        kmsg_dump(KMSG_DUMP_POWEROFF);
        machine_power_off();
}
EXPORT_SYMBOL_GPL(kernel_power_off);

since it is exported, the symbol can be called from many different places
in the kernel, and thus many different ways of shutting down or hibernation
are possible.

On Tue, May 15, 2012 at 1:09 PM, Peter Teoh <htmldeveloper at gmail.com> wrote:

> i think your question is distro-specific, or at least it differs
> between sysvinit and systemd - which is the two main technique of
> starting up.
>
> For Fedora Core 17 beta (my system), it is running systemd:
>
> http://lwn.net/Articles/458789/
>
> and here it shows all the good points about systemd:
>
> http://0pointer.de/blog/projects/why.html
>
> (noting the shutdown features if u are focusing on the shutdown part).
>
> more info:
>
> https://fedoraproject.org/wiki/Systemd
>
> [PDF]
>
> Beyond Init: systemd - LinuxKongress 2010
>
> www.linux-kongress.org/2010/slides/systemd-poettering.pdf
>
> But as mentioned in document above, /dev/initctl is still supported,
> which is the traditional means of shutting down.
>
> In Ubuntu (which uses sysvinit, and shutting down is via
> /dev/initctl), u can get the source code for poweroff via:
>
> "apt-get source upstart"
>
> and then start navigating through the source code.
>
> Within util/shutdown.c you can see following:
>
> * struct request:
>  *
>  * This is the structure passed across /dev/initctl.
>  **/
> struct request {
>         int  magic;
>         int  cmd;
>         int  runlevel;
>         int  sleeptime;
>         char data[368];
> };
>
> /**
>  * sysvinit_shutdown:
>  *
>  * Attempt to shutdown a running sysvinit /sbin/init using its /dev/initctl
>  * socket.
>  **/
> static void
> sysvinit_shutdown (void)
> {
>         struct sigaction act;
>         struct request   request;
>         int              fd;
>
>         /* Fill in the magic values */
>         memset (&request, 0, sizeof (request));
>         request.magic = 0x03091969;
>         request.sleeptime = 5;
>         request.cmd = 1;
>
>         /* Select a runlevel based on the event name */
>
>
> There goes all the magic....or some part of it....
>
> On Mon, May 14, 2012 at 7:31 PM, Asutosh Das <das.asutosh at gmail.com>
> wrote:
> >
> > Hi List
> > I have a query about the linux shutdown or reboot process.
> >
> > Please can you let me know if the below sequence is correct:
> >
> > When reboot command is given on the shell:
> >
> > 1. all user-space process is frozen (by sending some signal ?? )
> > 2. all file-systems are remounted as read-only - (sysrq handler does
> this ?? )
> > 3. all drivers, whose shutdown handlers have been defined, are called
> > in the reverse order in which these drivers were initialized.
> >
> > I am not sure what happens after this.
> >
> > TIA.
> >
> >
> >
> > --
> > Thanks & Regards
> > ~/asd
> >
> > _______________________________________________
> > Kernelnewbies mailing list
> > Kernelnewbies at kernelnewbies.org
> > http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
>
>
>
> --
> Regards,
> Peter Teoh
>



-- 
Regards,
Peter Teoh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20120515/3a9eb831/attachment.html 


More information about the Kernelnewbies mailing list