zzz: Rewrite to use new power device

Previous script called acpiconf(8) (or apm(8) if ACPI wasn't supported,
although this was anyway redundant because APMIO just uses ACPI now).

Since a new generic power management interface was introduced, this isn't
sufficient, as this would only work for ACPI systems and for ACPI S3 suspend
(so no way to select suspend-to-idle). Rewrite in C to take advantage of the
new power interface.

We may want to add a switch to manually override the kern.power.suspend sysctl,
which is otherwise what the power device uses to decide which suspend type to
switch to (suspend-to-idle or firmware suspend), but this will require us to
amend the power interface.

Reviewed by:	olce, imp, mhorne, ziaee
Tested by:	mhorne
Approved by:	olce, imp, mhorne, ziaee
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D56918
This commit is contained in:
Aymeric Wibo
2026-05-10 10:48:03 +02:00
parent 14b8531c4c
commit aba599a6cc
5 changed files with 92 additions and 87 deletions
+3 -1
View File
@@ -1,4 +1,6 @@
SCRIPTS=zzz.sh
.include <src.opts.mk>
PROG= zzz
MAN= zzz.8
.include <bsd.prog.mk>
+41 -45
View File
@@ -1,63 +1,59 @@
.\" Copyright (c) 2003 Nate Lawson
.\" All rights reserved.
.\"-
.\" SPDX-License-Identifier: BSD-2-Clause
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" Copyright (c) 2026 The FreeBSD Foundation
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\" This documentation was written by Aymeric Wibo <obiwac@freebsd.org> under
.\" sponsorship from the FreeBSD Foundation.
.\"
.Dd July 13, 2003
.Dd May 26, 2026
.Dt ZZZ 8
.Os
.Sh NAME
.Nm zzz
.Nd suspend an ACPI or APM system
.Nd suspend the system
.Sh SYNOPSIS
.Nm
.Sh DESCRIPTION
The
.Nm
utility
checks for
.Tn ACPI
or
.Tn APM
support and then suspends the system appropriately.
For
.Tn APM ,
utility suspends the system by writing a suspend transition request to
.Pa /dev/power .
.Pp
.Dl apm -z
.Pp
will be issued.
For
.Tn ACPI,
the configured suspend state will be looked up, checked to see
if it is supported and,
.Pp
.Dl acpiconf -s <state>
.Pp
will be issued.
The specific sleep method used
.Pq e.g.\& suspend-to-idle or firmware suspend
is determined by the
.Va kern.power.suspend
sysctl.
.Sh SEE ALSO
.Xr acpi 4 ,
.Xr apm 4 ,
.Xr acpiconf 8 ,
.Xr acpiconf 8
.Sh HISTORY
The
.Nm
utility first appeared as a shell script in
.Fx 5.1 .
It suspended the system by invoking
.Xr acpiconf 8
with the suspend state configured via
.Va hw.acpi.suspend_state ,
or
.Xr apm 8
on systems without ACPI support.
.Pp
It was rewritten in C in
.Fx 16.0
to use the generic power management interface provided by
.Pa /dev/power .
.Sh AUTHORS
This manual page was written by
The original
.Nm
script was written by
.An Mark Santcroos Aq Mt marks@ripe.net .
This manual page was originally written by
.An Nate Lawson Aq Mt njl@FreeBSD.org .
Both the current program and manual page were written by
.An Aymeric Wibo Aq Mt obiwac@FreeBSD.org ,
under sponsorship from the
.Fx
Foundation.
+47
View File
@@ -0,0 +1,47 @@
/*
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2026 The FreeBSD Foundation
*
* This software was developed by Aymeric Wibo <obiwac@freebsd.org>
* under sponsorship from the FreeBSD Foundation.
*/
#include <sys/power.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
static void
usage(void)
{
(void)fprintf(stderr, "usage: zzz\n");
exit(1);
}
int
main(int argc, char *argv[])
{
int powerfd;
enum power_transition trans;
(void)argv;
if (argc > 1)
usage();
powerfd = open(_PATH_DEVPOWER, O_RDWR);
if (powerfd < 0)
err(EX_OSFILE, "could not open power device");
trans = POWER_TRANSITION_SUSPEND;
if (ioctl(powerfd, PIOTRANSITION, &trans) != 0)
err(EX_IOERR, "could not request suspend transition");
return (EXIT_SUCCESS);
}
-41
View File
@@ -1,41 +0,0 @@
#!/bin/sh
#
# Suspend the system using either ACPI or APM.
# For APM, "apm -z" will be issued.
# For ACPI, the configured suspend state will be looked up, checked to see
# if it is supported, and "acpiconf -s <state>" will be issued.
#
# Mark Santcroos <marks@ripe.net>
#
PATH=/sbin:/usr/sbin:/usr/bin:/bin
ACPI_SUSPEND_STATE=hw.acpi.suspend_state
ACPI_SUPPORTED_STATES=hw.acpi.supported_sleep_state
APM_SUSPEND_DELAY=machdep.apm_suspend_delay
# Check for ACPI support
if sysctl $ACPI_SUSPEND_STATE >/dev/null 2>&1; then
# Get configured suspend state
SUSPEND_STATE=$(sysctl -n $ACPI_SUSPEND_STATE)
# Get list of supported suspend states
SUPPORTED_STATES=$(sysctl -n $ACPI_SUPPORTED_STATES)
# Check if the configured suspend state is supported by the system
if echo "$SUPPORTED_STATES" | grep "$SUSPEND_STATE" >/dev/null; then
# execute ACPI style suspend command
exec acpiconf -s "$SUSPEND_STATE"
else
echo "Requested suspend state $SUSPEND_STATE is not supported."
echo "Supported states: $SUPPORTED_STATES"
fi
# Check for APM support
elif sysctl $APM_SUSPEND_DELAY >/dev/null 2>&1; then
# Execute APM style suspend command
exec apm -z
else
echo "Error: no ACPI or APM suspend support found."
fi
exit 1