1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2026-01-16 05:20:59 +00:00
serenity/Kernel/FileSystem/SysFS/Subsystems/Kernel/PowerStateSwitch.cpp
Liav A 9b8b8c0e04 Kernel: Simplify reboot & poweroff code flow a bit
Instead of using ifdefs to use the correct platform-specific methods, we
can just use the same pattern we use for the microseconds_delay function
which has specific implementations for each Arch CPU subdirectory.

When linking a kernel image, the actual correct and platform-specific
power-state changing methods will be called in Firmware/PowerState.cpp
file.
2023-06-27 20:04:42 +02:00

67 lines
2.1 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Platform.h>
#include <Kernel/FileSystem/FileSystem.h>
#include <Kernel/FileSystem/SysFS/Subsystems/Kernel/PowerStateSwitch.h>
#include <Kernel/Firmware/ACPI/Parser.h>
#include <Kernel/Firmware/PowerState.h>
#include <Kernel/Sections.h>
#include <Kernel/TTY/ConsoleManagement.h>
#include <Kernel/Tasks/Process.h>
namespace Kernel {
mode_t SysFSPowerStateSwitchNode::permissions() const
{
return S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP;
}
UNMAP_AFTER_INIT NonnullRefPtr<SysFSPowerStateSwitchNode> SysFSPowerStateSwitchNode::must_create(SysFSDirectory const& parent_directory)
{
return adopt_ref_if_nonnull(new (nothrow) SysFSPowerStateSwitchNode(parent_directory)).release_nonnull();
}
UNMAP_AFTER_INIT SysFSPowerStateSwitchNode::SysFSPowerStateSwitchNode(SysFSDirectory const& parent_directory)
: SysFSComponent(parent_directory)
{
}
ErrorOr<void> SysFSPowerStateSwitchNode::truncate(u64 size)
{
// Note: This node doesn't store any useful data anyway, so we can safely
// truncate this to zero (essentially ignoring the request without failing).
if (size != 0)
return EPERM;
return {};
}
ErrorOr<size_t> SysFSPowerStateSwitchNode::write_bytes(off_t offset, size_t count, UserOrKernelBuffer const& data, OpenFileDescription*)
{
// Note: If we are in a jail, don't let the current process to change the power state.
if (Process::current().is_currently_in_jail())
return Error::from_errno(EPERM);
if (Checked<off_t>::addition_would_overflow(offset, count))
return Error::from_errno(EOVERFLOW);
if (offset > 0)
return Error::from_errno(EINVAL);
if (count > 1)
return Error::from_errno(EINVAL);
char buf[1];
TRY(data.read(buf, 1));
switch (buf[0]) {
case '1':
Firmware::reboot();
VERIFY_NOT_REACHED();
case '2':
Firmware::poweroff();
VERIFY_NOT_REACHED();
default:
return Error::from_errno(EINVAL);
}
}
}