Bug 904827 - systemd-inhibit doesn't work when called from pm-utils scripts (using Gnome)
systemd-inhibit doesn't work when called from pm-utils scripts (using Gnome)
Product: Fedora
Classification: Fedora
Component: systemd (Show other bugs)
Unspecified Unspecified
unspecified Severity unspecified
: ---
: ---
Assigned To: systemd-maint
Fedora Extras Quality Assurance
: Reopened
Depends On:
  Show dependency treegraph
Reported: 2013-01-27 14:08 EST by Hedayat Vatankhah
Modified: 2013-04-09 07:20 EDT (History)
8 users (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2013-04-09 07:20:38 EDT
Type: Bug
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description Hedayat Vatankhah 2013-01-27 14:08:33 EST
Description of problem:
Since Gnome no longer provides the possibility of inhibiting suspension when laptop's lid is closed (on AC power), I tried to do it myself. I want the system to suspend when on battery, but not suspend when on AC. Therefore, I added the following script in /etc/pm/power.d/01_lidclose.sh, so that it runs a systemd-inhibit command when using AC, and kills it when on battery. If I run the script manually, it works as expected and prevents suspends when the lid is closed. However, if I unplug AC and plug it again, the script is run by system (apparently, upower.service). also, systemd-inhibit shows that this inhibitor is running. So, it seems that everything is OK. However, if I close the lid, the laptop suspends.
The script (/etc/pm/power.d/01_lidclose.sh):


if on_ac_power; then
	systemd-inhibit --what=handle-lid-switch --who="AC Lid-Close-Suspend Inhibitor" --why="Since AC power is available" --mode=block sleep 1000d > /dev/null &
	echo $! > /run/lid_close.pid
	kill -9 $(cat /run/lid_close.pid)

exit 0

Output of 'journalctl -u systemd-logind.service -f' when the script is run manually:
ژانویه 27 22:24:11 hedayat-lap.hedayat-net systemd-logind[4992]: Lid closed.
ژانویه 27 22:24:17 hedayat-lap.hedayat-net systemd-logind[4992]: Lid opened.

When run automatically after unplugging and re-pluging AC:
ژانویه 27 22:24:34 hedayat-lap.hedayat-net systemd-logind[4992]: Lid closed.
ژانویه 27 22:24:34 hedayat-lap.hedayat-net systemd-logind[4992]: Suspending...
ژانویه 27 22:24:55 hedayat-lap.hedayat-net systemd-logind[4992]: Lid opened.

'systemd-inhibit --list' output when run in both cases:
[hedayat@hedayat-lap ~]% systemd-inhibit --list
WHAT                  WHO                  WHY                  MODE     UID    PID
handle-power-key:handle-suspend-key:handle-hibernate-key hedayat              GNOME handlin...sses block   1000   1347
sleep                 hedayat              GNOME needs t...reen delay   1000   1347
handle-lid-switch     AC Lid-Close-...itor Since AC powe...able block      0  22461

3 inhibitors listed.

Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:
1. Put the above script in /etc/pm/power.d/
2. Make sure laptop is running on battery
3. Connect AC
4. Verify that the inhibitor is listed in 'systemd-inhibit --list' output
5. Close the lid
Actual results:
Laptop suspends

Expected results:
Should no suspend

Additional info:
If you run the script manually while laptop is on AC instead of steps 2 and 3, laptop doesn't suspend.
Comment 1 Lukáš Nykrýn 2013-01-28 07:25:36 EST
Other way how to set this directly in gnome is
gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action  blank
Comment 2 Hedayat Vatankhah 2013-01-28 08:17:39 EST
It doesn't work too! If I close the lid, laptop suspends. This is my status after running the command:

[hedayat@hedayat-lap ~]% gsettings get org.gnome.settings-daemon.plugins.power lid-close-battery-action 
[hedayat@hedayat-lap ~]% gsettings get org.gnome.settings-daemon.plugins.power lid-close-ac-action 
Comment 3 Lennart Poettering 2013-03-07 09:40:40 EST
/etc/pm/power.d/ is not supported by logind's own suspend handling. Also, any hook directory like it is way too late to still inhibit the suspend.

Please file a bug against GNOME if the gsettings trick doesn't work.
Comment 4 Hedayat Vatankhah 2013-03-18 05:42:27 EDT
Why /etc/pm/power.d/ should be supported by logind?! It is supported by upower, and it does run the scripts. (Notice that the hook is NOT intended to be run when you want to suspend, or wake up from suspend. The hook is run when the AC cable is plugged or unplugged. And it does run my systemd-inhibit command. So, the problem is this: systemd-inhibit is being run, and systemd-inhibit --list shows that an inhibitor is running, but it doesn't inhibit suspension. 
Is it important who has run systemd-inhibit? Consider I write my own custom application to run systemd-inhibit when AC cable is plugged. Should it work or not? I guess it should work. So, why upower should not be able to inhibit suspension by running systemd-inhibit? Looks weird!
Comment 5 Lennart Poettering 2013-04-09 07:20:38 EDT
Note that the lid switch ignores any inhibtors by default, i.e. LidSwitchIgnoreInhibited=yes is the default. See logind.conf(5).

If the GNOME settings for controlling what happens if you close the lid don't work, then please file a bug against GNOME.

Note You need to log in before you can comment on or make changes to this bug.