git: aba599a6cc55 - main - zzz: Rewrite to use new power device
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Tue, 26 May 2026 23:06:07 UTC
The branch main has been updated by obiwac:
URL: https://cgit.FreeBSD.org/src/commit/?id=aba599a6cc550bf209b42a81a3cdb3f6e94d0613
commit aba599a6cc550bf209b42a81a3cdb3f6e94d0613
Author: Aymeric Wibo <obiwac@FreeBSD.org>
AuthorDate: 2026-05-10 08:48:03 +0000
Commit: Aymeric Wibo <obiwac@FreeBSD.org>
CommitDate: 2026-05-26 23:05:45 +0000
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
---
include/paths.h | 1 +
usr.sbin/zzz/Makefile | 4 ++-
usr.sbin/zzz/zzz.8 | 86 ++++++++++++++++++++++++---------------------------
usr.sbin/zzz/zzz.c | 47 ++++++++++++++++++++++++++++
usr.sbin/zzz/zzz.sh | 41 ------------------------
5 files changed, 92 insertions(+), 87 deletions(-)
diff --git a/include/paths.h b/include/paths.h
index 26924bcdba91..9157c17524c7 100644
--- a/include/paths.h
+++ b/include/paths.h
@@ -54,6 +54,7 @@
#define _PATH_DEFTAPE "/dev/sa0"
#define _PATH_DEVGPIOC "/dev/gpioc"
#define _PATH_DEVNULL "/dev/null"
+#define _PATH_DEVPOWER "/dev/power"
#define _PATH_DEVZERO "/dev/zero"
#define _PATH_DRUM "/dev/drum"
#define _PATH_ESDB "/usr/share/i18n/esdb"
diff --git a/usr.sbin/zzz/Makefile b/usr.sbin/zzz/Makefile
index 4d1a2dd9dc34..6ce3e03fa6cf 100644
--- a/usr.sbin/zzz/Makefile
+++ b/usr.sbin/zzz/Makefile
@@ -1,4 +1,6 @@
-SCRIPTS=zzz.sh
+.include <src.opts.mk>
+
+PROG= zzz
MAN= zzz.8
.include <bsd.prog.mk>
diff --git a/usr.sbin/zzz/zzz.8 b/usr.sbin/zzz/zzz.8
index c2799c28e177..e7df72c1e830 100644
--- a/usr.sbin/zzz/zzz.8
+++ b/usr.sbin/zzz/zzz.8
@@ -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 ,
-.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,
+utility suspends the system by writing a suspend transition request to
+.Pa /dev/power .
.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.
diff --git a/usr.sbin/zzz/zzz.c b/usr.sbin/zzz/zzz.c
new file mode 100644
index 000000000000..a014537720cf
--- /dev/null
+++ b/usr.sbin/zzz/zzz.c
@@ -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);
+}
diff --git a/usr.sbin/zzz/zzz.sh b/usr.sbin/zzz/zzz.sh
deleted file mode 100644
index dde36e7984c6..000000000000
--- a/usr.sbin/zzz/zzz.sh
+++ /dev/null
@@ -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