I came up with a solution that may prove useful to someone else:
I noticed that the system log (journalctl) lists the power-button device:
# journalctl -b-0 --unit=systemd-logind | grep -m 1 '/dev/input'
Jul 07 21:23:58 sm-1 systemd-logind[2167]: Watching system buttons on /dev/input/event3 (Power Button)
So I wrote a shell script to extract the power-button device and then call a python script to monitor the power-button for events:
[CODE]BUTTON_DEVICE=$(journalctl -b-0 --unit=systemd-logind | grep -m 1 ‘/dev/input’ | cut -f10 -d\ )
systemd-inhibit --what=handle-power-key --mode=block /usr/local/sbin/power-button.py $BUTTON_DEVICE[/CODE]
Notice that we have to use systemd-inhibit to block systemd-logind from processing the power button.
Recall, my goal is to require a user to press the power-button twice within five seconds to activate. Here is my python script:
[CODE]#!/usr/bin/env python
$COPYRIGHT$
import struct
import time
import sys
import syslog
import os
TRIGGER_INTERVAL = 5
SuperMicro power button
infile_path = sys.argv[1] if len(sys.argv) > 1 else “0”
#long int, long int, unsigned short, unsigned short, unsigned int
FORMAT = ‘llHHI’
EVENT_SIZE = struct.calcsize(FORMAT)
#open file in binary mode
in_file = open(infile_path, “rb”)
last_event_time = None
event = in_file.read(EVENT_SIZE)
while event:
(tv_sec, tv_usec, type, code, value) = struct.unpack(FORMAT, event)
if type == 1 and code == 116 and value == 1:
if last_event_time and tv_sec - last_event_time <= TRIGGER_INTERVAL:
syslog.syslog('Okay, you really mean it. Shutting down')
os.system('/usr/bin/systemctl start systemd-poweroff.service')
else:
last_event_time = tv_sec
syslog.syslog('Someone just pressed the power button!')
event = in_file.read(EVENT_SIZE)
in_file.close()
[/CODE]
Resources:
[LIST]
[]https://wiki.archlinux.org/index.php/Power_management#Power_management_with_systemd
[]http://stackoverflow.com/questions/5060710/format-of-dev-input-event/16682549
[/LIST]