Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 911257 - sys-apps/sysvinit-3.07: /sbin/halt always acts as if option -p was given
Summary: sys-apps/sysvinit-3.07: /sbin/halt always acts as if option -p was given
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal
Assignee: William Hubbs
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-07-26 15:25 UTC by Ulrich Müller
Modified: 2023-09-14 07:45 UTC (History)
4 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
sys-apps/sysvinit: Add postinst message about halt bugfix (0001-sys-apps-sysvinit-Add-postinst-message-about-halt-bu.patch,1.18 KB, patch)
2023-08-17 17:44 UTC, Ulrich Müller
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ulrich Müller gentoo-dev 2023-07-26 15:25:43 UTC
halt(8) says:

       -p     When  halting  the system, switch off the power. This is the de‐
              fault when halt is called as poweroff.

Now /sbin/halt without the -p option executes "shutdown -h", whereas with the -p option it executes "shutdown -h -P". AFAICS, the only difference between the two is that environment variable INIT_HALT will be set to "POWEROFF" in the second case (while it remains unset otherwise).

shutdown then calls /sbin/halt.sh which does:

    #!/bin/sh
    if [ "${INIT_HALT}" = HALT ]; then
        exec /sbin/halt -dhn
    else
        exec /sbin/poweroff -dhn
    fi

Since INIT_HALT doesn't have the value "HALT" in either case, the script will always execute the else clause.
Comment 2 Mike Gilbert gentoo-dev 2023-07-26 15:40:51 UTC
Ah, but I guess halt doesn't call shutdown -H?
Comment 3 Ulrich Müller gentoo-dev 2023-07-26 16:01:39 UTC
(In reply to Mike Gilbert from comment #2)
> Ah, but I guess halt doesn't call shutdown -H?

It's this code in function do_shutdown (in halt.c):

	args[i++] = "shutdown";
	args[i++] = fl;
        if ( (! strcmp(fl, "-h") ) && (should_poweroff) )
           args[i++] = "-P";

So it calls "shutdown -h" or "shutdown -h -P", but never "shutdown -h -H".

Maybe halt.sh should test for [ "${INIT_HALT}" = POWEROFF ] instead?
Comment 4 Mike Gilbert gentoo-dev 2023-07-26 16:06:22 UTC
(In reply to Ulrich Müller from comment #3)
> Maybe halt.sh should test for [ "${INIT_HALT}" = POWEROFF ] instead?

Yeah, that seems reasonable.
Comment 5 Mike Gilbert gentoo-dev 2023-07-26 19:18:38 UTC
Updates from IRC:

 14:33:40 <@floppym> ulm: I guess one concern is that this might result in unexpected behavior for people who currently run "shutdown -h" instead of "poweroff" or "shutdown -h -P".
 14:34:31 <@floppym> ulm: It might make more sense to update halt.c to pass -H to shutdown.

 14:42:54 <@floppym> Maybe this instead? https://gist.github.com/floppym/0e0e029c93b61c7089620d023fb5798c

 15:02:05 <@ulm> floppym: I've tested you patch with "halt", "halt -p" and "poweroff"

 15:02:46 <@ulm> all three behave as expected, i.e. no power-off with plain halt, power-off with the other two


I will send a patch upstream and see what they have to say.
Comment 6 Larry the Git Cow gentoo-dev 2023-07-27 13:50:24 UTC
The bug has been closed via the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=07b943791fb87acef33c0e50ea196e63937d6ffb

commit 07b943791fb87acef33c0e50ea196e63937d6ffb
Author:     Mike Gilbert <floppym@gentoo.org>
AuthorDate: 2023-07-27 13:48:34 +0000
Commit:     Mike Gilbert <floppym@gentoo.org>
CommitDate: 2023-07-27 13:49:25 +0000

    sys-apps/sysvinit: prevent 'halt' from powering off the machine
    
    If you want to power off, use 'poweroff' or 'halt -p' instead.
    
    Closes: https://bugs.gentoo.org/911257
    Signed-off-by: Mike Gilbert <floppym@gentoo.org>

 sys-apps/sysvinit/files/sysvinit-3.07-halt.patch |  53 +++++++
 sys-apps/sysvinit/sysvinit-3.07-r1.ebuild        | 190 +++++++++++++++++++++++
 2 files changed, 243 insertions(+)
Comment 7 Kővágó, Zoltán 2023-08-17 16:40:21 UTC
Was this really necessary? Last night one of my computers didn't shut down, and it took me a while to figure out that now gentoo also adopted this braindead behavior that previously I only noticed with alpine and its busybox based init system. I've always shut down my gentoo machines with halt, and it worked fine in the last 17 or so years.
Or if this change is really necessary, could we have at least have a huge warning when merging sysvinit that the behavior of `halt` changed significantly in a patch release and if you don't want to inflate your electricity bill you should use `halt -p` or `poweroff` instead of halt?
Comment 8 Ulrich Müller gentoo-dev 2023-08-17 17:00:43 UTC
(In reply to Kővágó, Zoltán from comment #7)
> [...] braindead behavior [...]

The command now behaves as documented:

       -p     When  halting  the system, switch off the power. This is the
              default when halt is called as poweroff.

Conversely, I would conclude that it *doesn't* switch of the power when called without the -p option (because otherwise the option would be useless).

> [...] if you don't want to inflate your electricity bill you should use
> `halt -p` or `poweroff` instead of halt?

This is exactly what halt(8) says?
Comment 9 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2023-08-17 17:04:32 UTC
He's asking for a warning to reinforce that given the behaviour change.
Comment 10 Mike Gilbert gentoo-dev 2023-08-17 17:29:41 UTC
I have no objection to adding a warning, but I do not plan on adding it myself. Patches welcome.
Comment 11 Ulrich Müller gentoo-dev 2023-08-17 17:30:39 UTC
(In reply to Sam James from comment #9)
> He's asking for a warning to reinforce that given the behaviour change.

"Previously, the 'halt' command caused the system to power off, even if option -p was not given. This long-standing bug has been fixed, and the command now behaves as documented."?
Comment 12 Mike Gilbert gentoo-dev 2023-08-17 17:37:51 UTC
That language seems good to me.
Comment 13 Ulrich Müller gentoo-dev 2023-08-17 17:44:33 UTC
Created attachment 868089 [details, diff]
sys-apps/sysvinit: Add postinst message about halt bugfix
Comment 14 Ulrich Müller gentoo-dev 2023-08-17 17:46:14 UTC
Or should we revbump to -r2, so that current users of -r1 will also see the message immediately?
Comment 15 Mike Gilbert gentoo-dev 2023-08-17 19:16:05 UTC
Let's change it from elog to ewarn, and revbump the ebuild. Go ahead and push when ready.
Comment 16 Larry the Git Cow gentoo-dev 2023-08-17 19:26:43 UTC
The bug has been referenced in the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=53aae885d64f2284895359d9366641656d82b49e

commit 53aae885d64f2284895359d9366641656d82b49e
Author:     Ulrich Müller <ulm@gentoo.org>
AuthorDate: 2023-08-17 17:41:35 +0000
Commit:     Ulrich Müller <ulm@gentoo.org>
CommitDate: 2023-08-17 19:25:28 +0000

    sys-apps/sysvinit: Add postinst message about halt bugfix
    
    Bug: https://bugs.gentoo.org/911257#c7
    Signed-off-by: Ulrich Müller <ulm@gentoo.org>

 .../{sysvinit-3.07-r1.ebuild => sysvinit-3.07-r2.ebuild}         | 9 +++++++++
 1 file changed, 9 insertions(+)
Comment 17 Xavier Miller 2023-09-14 06:11:38 UTC
Hi!,

sys-power/acpid does shutdown -h if the init process it not openrc-init

So with sysvinit-3.0.8, it hangs in place of poweroff when pressing the power button.

I didn't inspected the other power utilities that react on power button, but it seems this new version of sysvinit aks to change those actions.

Do I need to file a bug for sys-power/acpid?
Comment 18 Ulrich Müller gentoo-dev 2023-09-14 07:29:02 UTC
(In reply to Xavier Miller from comment #17)
> Hi!,
> 
> sys-power/acpid does shutdown -h if the init process it not openrc-init
> 
> So with sysvinit-3.0.8, it hangs in place of poweroff when pressing the
> power button.
> 
> I didn't inspected the other power utilities that react on power button, but
> it seems this new version of sysvinit aks to change those actions.
> 
> Do I need to file a bug for sys-power/acpid?

shutdown -h (i.e. without the -H or -P option) still defaults to poweroff, because INIT_HALT is unset in /sbin/halt.sh. See comment #1 for a link to the relevant source code.
Comment 19 Ulrich Müller gentoo-dev 2023-09-14 07:45:20 UTC
Thinking about it, it would be somewhat cleaner if acpid called shutdown with an explicit -P option instead of relying on its (undocumented) default.

shutdown(8) says:

       -h     Halt or power off after shutdown. Usually used with the -P or -H
              flags, depending on whether we want to poweroff or  simply  stop
              the operating system.