mirror of
https://github.com/RGBCube/serenity
synced 2025-05-28 14:35:08 +00:00
Kernel+Userland: Introduce a new way to reboot and poweroff the machine
This change removes the halt and reboot syscalls, and create a new mechanism to change the power state of the machine. Instead of how power state was changed until now, put a SysFS node as writable only for the superuser, that with a defined value, can result in either reboot or poweroff. In the future, a power group can be assigned to this node (which will be the GroupID responsible for power management). This opens an opportunity to permit to shutdown/reboot without superuser permissions, so in the future, a userspace daemon can take control of this node to perform power management operations without superuser permissions, if we enforce different UserID/GroupID on that node.
This commit is contained in:
parent
06e95d0fd7
commit
8d0dbdeaac
11 changed files with 161 additions and 97 deletions
99
Kernel/Firmware/PowerStateSwitch.cpp
Normal file
99
Kernel/Firmware/PowerStateSwitch.cpp
Normal file
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* 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 <Kernel/FileSystem/FileSystem.h>
|
||||
#include <Kernel/Firmware/ACPI/Parser.h>
|
||||
#include <Kernel/Firmware/PowerStateSwitch.h>
|
||||
#include <Kernel/IO.h>
|
||||
#include <Kernel/Process.h>
|
||||
#include <Kernel/Sections.h>
|
||||
#include <Kernel/TTY/ConsoleManagement.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
mode_t PowerStateSwitchNode::permissions() const
|
||||
{
|
||||
return S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP;
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT NonnullRefPtr<PowerStateSwitchNode> PowerStateSwitchNode::must_create(FirmwareSysFSDirectory& firmware_directory)
|
||||
{
|
||||
return adopt_ref_if_nonnull(new (nothrow) PowerStateSwitchNode(firmware_directory)).release_nonnull();
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT PowerStateSwitchNode::PowerStateSwitchNode(FirmwareSysFSDirectory&)
|
||||
: SysFSComponent("power_state")
|
||||
{
|
||||
}
|
||||
|
||||
KResultOr<size_t> PowerStateSwitchNode::write_bytes(off_t offset, size_t count, UserOrKernelBuffer const& data, OpenFileDescription*)
|
||||
{
|
||||
if (Checked<off_t>::addition_would_overflow(offset, count))
|
||||
return EOVERFLOW;
|
||||
if (offset > 0)
|
||||
return EINVAL;
|
||||
if (count > 1)
|
||||
return EINVAL;
|
||||
|
||||
char buf[1];
|
||||
TRY(data.read(buf, 1));
|
||||
switch (buf[0]) {
|
||||
case '0':
|
||||
return EINVAL;
|
||||
case '1':
|
||||
reboot();
|
||||
VERIFY_NOT_REACHED();
|
||||
case '2':
|
||||
poweroff();
|
||||
VERIFY_NOT_REACHED();
|
||||
default:
|
||||
return EINVAL;
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
void PowerStateSwitchNode::reboot()
|
||||
{
|
||||
MutexLocker locker(Process::current().big_lock());
|
||||
|
||||
dbgln("acquiring FS locks...");
|
||||
FileSystem::lock_all();
|
||||
dbgln("syncing mounted filesystems...");
|
||||
FileSystem::sync();
|
||||
dbgln("attempting reboot via ACPI");
|
||||
if (ACPI::is_enabled())
|
||||
ACPI::Parser::the()->try_acpi_reboot();
|
||||
dbgln("attempting reboot via KB Controller...");
|
||||
IO::out8(0x64, 0xFE);
|
||||
dbgln("reboot attempts failed, applications will stop responding.");
|
||||
dmesgln("Reboot can't be completed. It's safe to turn off the computer!");
|
||||
Processor::halt();
|
||||
}
|
||||
|
||||
void PowerStateSwitchNode::poweroff()
|
||||
{
|
||||
MutexLocker locker(Process::current().big_lock());
|
||||
|
||||
ConsoleManagement::the().switch_to_debug();
|
||||
|
||||
dbgln("acquiring FS locks...");
|
||||
FileSystem::lock_all();
|
||||
dbgln("syncing mounted filesystems...");
|
||||
FileSystem::sync();
|
||||
dbgln("attempting system shutdown...");
|
||||
// QEMU Shutdown
|
||||
IO::out16(0x604, 0x2000);
|
||||
// If we're here, the shutdown failed. Try VirtualBox shutdown.
|
||||
IO::out16(0x4004, 0x3400);
|
||||
// VirtualBox shutdown failed. Try Bochs/Old QEMU shutdown.
|
||||
IO::out16(0xb004, 0x2000);
|
||||
dbgln("shutdown attempts failed, applications will stop responding.");
|
||||
dmesgln("Shutdown can't be completed. It's safe to turn off the computer!");
|
||||
Processor::halt();
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue