From 43997540358e3cf522aa0ee96b0ab09406fb692b Mon Sep 17 00:00:00 2001 From: terryguan Date: Thu, 5 Sep 2024 17:35:41 -0700 Subject: [PATCH] init: add a swapoff built-in command Enables a method for swapping off certain block devices or files. This will be used before hibernation occurs. Bug: 339688542 Test: Manual, verified that calling swapoff from a init file swapsoff location that is specified Change-Id: I212a6f303a023c3e440b557caae82ad3904ac9c9 --- init/README.md | 4 +++- init/builtins.cpp | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/init/README.md b/init/README.md index 0bb26e891..3a2da90d4 100644 --- a/init/README.md +++ b/init/README.md @@ -745,6 +745,9 @@ provides the `aidl_lazy_test_1` interface. fstab.${ro.hardware} or fstab.${ro.hardware.platform} will be scanned for under /odm/etc, /vendor/etc, or / at runtime, in that order. +`swapoff ` +> Stops swapping to the file or block device specified by path. + `symlink ` > Create a symbolic link at _path_ with the value _target_ @@ -788,7 +791,6 @@ provides the `aidl_lazy_test_1` interface. If the file does not exist, it will be created. If it does exist, it will be truncated. Properties are expanded within _content_. - Imports ------- `import ` diff --git a/init/builtins.cpp b/init/builtins.cpp index 606ea8c0a..f743a30e0 100644 --- a/init/builtins.cpp +++ b/init/builtins.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -1316,6 +1317,13 @@ static Result do_enter_default_mount_ns(const BuiltinArguments& args) { return {}; } +static Result do_swapoff(const BuiltinArguments& args) { + if (!swapoff(args[1].c_str())) { + return ErrnoError() << "swapoff() failed"; + } + return {}; +} + // Builtin-function-map start const BuiltinFunctionMap& GetBuiltinFunctionMap() { constexpr std::size_t kMax = std::numeric_limits::max(); @@ -1372,6 +1380,7 @@ const BuiltinFunctionMap& GetBuiltinFunctionMap() { {"start", {1, 1, {false, do_start}}}, {"stop", {1, 1, {false, do_stop}}}, {"swapon_all", {0, 1, {false, do_swapon_all}}}, + {"swapoff", {1, 1, {false, do_swapoff}}}, {"enter_default_mount_ns", {0, 0, {false, do_enter_default_mount_ns}}}, {"symlink", {2, 2, {true, do_symlink}}}, {"sysclktz", {1, 1, {false, do_sysclktz}}},