Skip to content

Commit a1b93e8

Browse files
joshtriplettrafaeljw
authored andcommitted
ACPI: Add new tiny-power-button driver to directly signal init
Virtual machines often use an ACPI power button event to tell the machine to shut down gracefully. Provide an extremely lightweight "tiny power button" driver to handle this event by signaling init directly, rather than running a separate daemon (such as acpid or systemd-logind) that adds to startup time and VM image complexity. The kernel configuration defines the default signal to send init, and userspace can change this signal via a module parameter. Suggested-by: "Rafael J. Wysocki" <[email protected]> Signed-off-by: Josh Triplett <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent ac1cc6b commit a1b93e8

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

drivers/acpi/Kconfig

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,30 @@ config ACPI_BUTTON
190190
To compile this driver as a module, choose M here:
191191
the module will be called button.
192192

193+
config ACPI_TINY_POWER_BUTTON
194+
tristate "Tiny Power Button Driver"
195+
depends on !ACPI_BUTTON
196+
help
197+
This driver provides a tiny alternative to the ACPI Button driver.
198+
The tiny power button driver only handles the power button. Rather
199+
than notifying userspace via the input layer or a netlink event, this
200+
driver directly signals the init process to shut down.
201+
202+
This driver is particularly suitable for cloud and VM environments,
203+
which use a simulated power button to initiate a controlled poweroff,
204+
but which may not want to run a separate userspace daemon to process
205+
input events.
206+
207+
config ACPI_TINY_POWER_BUTTON_SIGNAL
208+
int "Tiny Power Button Signal"
209+
depends on ACPI_TINY_POWER_BUTTON
210+
default 38
211+
help
212+
Default signal to send to init in response to the power button.
213+
214+
Likely values here include 38 (SIGRTMIN+4) to power off, or 2
215+
(SIGINT) to simulate Ctrl+Alt+Del.
216+
193217
config ACPI_VIDEO
194218
tristate "Video"
195219
depends on X86 && BACKLIGHT_CLASS_DEVICE

drivers/acpi/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ obj-$(CONFIG_ACPI_IPMI) += acpi_ipmi.o
7171

7272
obj-$(CONFIG_ACPI_AC) += ac.o
7373
obj-$(CONFIG_ACPI_BUTTON) += button.o
74+
obj-$(CONFIG_ACPI_TINY_POWER_BUTTON) += tiny-power-button.o
7475
obj-$(CONFIG_ACPI_FAN) += fan.o
7576
obj-$(CONFIG_ACPI_VIDEO) += video.o
7677
obj-$(CONFIG_ACPI_TAD) += acpi_tad.o

drivers/acpi/tiny-power-button.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
#include <linux/module.h>
3+
#include <linux/sched/signal.h>
4+
#include <linux/acpi.h>
5+
#include <acpi/button.h>
6+
7+
ACPI_MODULE_NAME("tiny-power-button");
8+
MODULE_AUTHOR("Josh Triplett");
9+
MODULE_DESCRIPTION("ACPI Tiny Power Button Driver");
10+
MODULE_LICENSE("GPL");
11+
12+
static int power_signal __read_mostly = CONFIG_ACPI_TINY_POWER_BUTTON_SIGNAL;
13+
module_param(power_signal, int, 0644);
14+
MODULE_PARM_DESC(power_signal, "Power button sends this signal to init");
15+
16+
static const struct acpi_device_id tiny_power_button_device_ids[] = {
17+
{ ACPI_BUTTON_HID_POWER, 0 },
18+
{ ACPI_BUTTON_HID_POWERF, 0 },
19+
{ "", 0 },
20+
};
21+
MODULE_DEVICE_TABLE(acpi, tiny_power_button_device_ids);
22+
23+
static int acpi_noop_add_remove(struct acpi_device *device)
24+
{
25+
return 0;
26+
}
27+
28+
static void acpi_tiny_power_button_notify(struct acpi_device *device, u32 event)
29+
{
30+
kill_cad_pid(power_signal, 1);
31+
}
32+
33+
static struct acpi_driver acpi_tiny_power_button_driver = {
34+
.name = "tiny-power-button",
35+
.class = "tiny-power-button",
36+
.ids = tiny_power_button_device_ids,
37+
.ops = {
38+
.add = acpi_noop_add_remove,
39+
.remove = acpi_noop_add_remove,
40+
.notify = acpi_tiny_power_button_notify,
41+
},
42+
};
43+
44+
module_driver(acpi_tiny_power_button_driver,
45+
acpi_bus_register_driver,
46+
acpi_bus_unregister_driver);

0 commit comments

Comments
 (0)