ACPI is the acronym for Advanced Configuration and Power Interface; as a standard, it was first implemented in the year 1996, as a successor to APM (Advanced Power Management). As a main feature, it brought the ability to handle power management at the operating system level, whereas before it was handled in BIOS. Some ACPI events on Linux are, by default, handled via systemd-logind, but more complex configurations can be achieved by installing and running the acpid service.
In this article we see how to configure systemd-logind and how to handle more specific ACPI events via the acpid daemon.
In this tutorial you will learn:
- How some ACPI events are handled by systemd-logind
- How to install and start the acpid service
- How to handle ACPI events
|Category||Requirements, Conventions or Software Version Used|
|Other||Root privileges to perform administrative tasks|
|Conventions||# – requires given linux-commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux-commands to be executed as a regular non-privileged user
ACPI events handled by systemd-logind
In recent years, systemd has become the standard init system on Linux. After some controversies, practically all major distribution adopted it, except some that explicitly exist for the sake of avoiding it, such as Devuan, a Debian derivative. One of the major critics that is moved against Systemd is that it doesn’t respect the Unix philosophy of “doing one thing, and doing it well”. In time, indeed, Systemd was extended to perform other tasks than simply handling the system init, such as managing user logins: this is done via the
The systemd-logind service, among the other things, is responsible for creating and managing sessions IDs, providing polkit access to system-level operations such as shutdown or suspend, and handling ACPI events such as the behavior of the power button. What actions should be performed when such events occur, can be configured by editing the
/etc/systemd/logind.conffile. Let’s take a look at its content:
[Login] [...] #HandlePowerKey=poweroff #HandleSuspendKey=suspend #HandleHibernateKey=hibernate #HandleLidSwitch=suspend #HandleLidSwitchExternalPower=suspend #HandleLidSwitchDocked=ignore #HandleRebootKey=reboot [...]
We truncated the output of the file for convenience, in order to show the most relevant lines. By default all the lines in the file are commented, and are reported with the default values. The reported directives control how systemd-logind handles the power, suspend, hibernate keys and the lid switch on machines when this event can occur, such as notebooks. Some of the values which can be assigned to the directives are:
The syntax to be used in the file is pretty self-explanatory: with the
HandlePowerKey=poweroff line, for example, we instruct systemd-logind to poweroff the machine when the power button is pressed, and to suspend the system to ram when the suspend key is pressed (
HandleSuspendKey=suspend). In the same way, we can make so that a specific event is ignored by using
ignore as the value of the corresponding line, as by default is done for
Some of the actions which can be handled by systemd-logind, in the most used desktop environments, are managed by graphical power managers. To avoid conflicts or redundant actions, such as the system being suspended two times when the suspend button is pressed, such applications inhibits systemd-logind handling of said events.
If we need to manage acpi events not covered by systemd-logind, with more complex or custom actions, we need to change strategy, install and run the
acpid daemon. Let’s see how to proceed.
Installing the acpid daemon
Installing the acpid daemon is very easy, since the related package is included in the repositories of the major Linux distributions. To install the package on Fedora, for example, we run the following command:
$ sudo dnf install acpid
To install the same package on Archlinux, instead, we run:
$ sudo pacman -Sy acpid
Finally, on Debian and Debian-based distributions, to install the acpid package we use the
$ sudo apt install acpid
After the installation is complete, we need to start the acpid service and enable it at boot, so it is started automatically each time we start our system. We can perform both actions with one single command:
$ sudo systemctl enable --now acpid
Handling ACPI events
With the acpid service running, when we need to configure an action to be performed on a specific event, the very first thing we need to do is to check how said event is recognized by the system (the same event can be recognized in
different ways on different machines: this depends on how ACPI is implemented). The
acpi_listen utility is designed for this specific purpose and is installed as part of the acpid package, so it is ready to use.
To identify an event we need to launch the utility in a terminal emulator, trigger the event, and see how it is read by ACPI. Let’s see a practical example. Suppose we want to perform an action on a laptop when the
Fnkey is pressed together with the
F3key. First, we invoke
We should obtain a blinking cursor; this means that system is waiting and ready to display how events are read. Now we can press the key combination. Immediately, the information are printed onscreen. In this case, on the machine I am using, I obtain the following result:
hotkey ATK0100:00 00000050 00000001
In the output, we can see four columns: the first one represents the device class, the second one is the kernel name for the “device”, as it appears under the
/sys/bus/acpi/devices directory. The third column reports the event code. Finally, the fourth column value assume a different meaning depending on the type of event. Once we know an event code, we can associate an action to it.
Associating an event with an action
Associating an ACPI event with an action it’s quite easy. It basically involves two files: the first is the one parsed by the acpid service (by default it must be created inside the
/etc/acpi/events directory) and is where we specify the code of the event to be handled and the path of the script which contains the commands that should be executed; the second is the script itself. Where the scripts are placed in the fileystem varies depending on the distribution. In Fedora, for example, we have the following directories by default:
├── actions │ └── power.sh └── events ├── powerconf └── videoconf
As we already said, the
/etc/acpi/events directory contains the files which handles the events. The
/etc/acpi/actions directory, instead, contains the scripts associated with the events. By default, as you can see, the
power.sh scripts are installed, and they manage the handling of the power button.
On Debian, files are arranged differently. The
/etc/acpi/actions directory doesn’t exist:
/etc/acpi ├── events │ └── powerbtn-acpi-support └── powerbtn-acpi-support.sh
On Archlinux things are handled differently: there is only one file by default, in which all events are handled:
# Pass all events to our one handler script event=.* action=/etc/acpi/handler.sh %e
What makes the file handle all events is the
.* regular expression. The code of the events are than passed as argument to the
/etc/acpi/handler.sh file (the
%e string will be replaced by the event code). In the script, the actions to be executed are selected depending on the latter, in a case statement.
How files are arranged, however, doesn’t matter that much. It doesn’t change how things work. Let’s manage the event we talked about previously: since an hibernate key doesn’t exist on the machine I am using, I will make so that when the
Fn+f3key combination is pressed, the system is put into hibernation. As a first thing let’s create the event file, we will call it
hibernateconf; it will contain only two lines:
event=hotkey ATK0100:00 00000050 action=/etc/acpi/actions/hibernate.sh
In the first line we specify the event code as value of event: in this case we simply used the literal code we discovered by using the acpi_listen utility. In the second line we specify the path of the script which should be executed:
One important thing to remember is that if using SELinux (on Fedora it is active by default), we must make sure the “event” file has the appropriate context associated to it, otherwise the event will not be handled. To know what context should be applied to the file, we can take a look at the existing files in the
/etc/acpi/eventsdirectory as reference. All we have to do is to run
$ ls -lZ /etc/acpi/events -rw-r--r--. 1 root root system_u:object_r:etc_t:s0 168 Oct 5 21:27 powerconf -rw-r--r--. 1 root root system_u:object_r:etc_t:s0 236 Oct 5 21:27 videoconf
As we can see, on Fedora, the two existing files in the /etc/acpi/events directory have the
system_u:object_r:etc_t context. To apply the same context to our
hibernateconf file, we can use them as reference with the
$ sudo chcon --reference /etc/acpi/events/powerconf /etc/acpi/events/hibernateconf
The script invoked by the event file will contain the following lines:
#!/bin/bash systemctl hibernate
In the script we simply use
systemctl to hibernate the system. All that remains to do is to make the script executable:
$ sudo chmod +x /etc/acpi/actions/hibernate.sh
For our new configurations to become effective, we need to restart the acpid daemon:
$ sudo systemctl restart acpid.service
When we press
Fn+F3, the system should now be hibernated.
In this article we saw how ACPI events can be handled on Linux. We saw how basic events like the press of the power button, the suspend key and the closing of a laptop lid are managed by systemd-logind and can be configured via the /etc/systemd/logind.conf file. We also saw how to handle custom events by installing the acpid daemon on some of the most used Linux distributions.