diff --git a/universal7885-common/apps/aidl-support/parts/default/Android.bp b/universal7885-common/apps/aidl-support/parts/default/Android.bp index cd355a7..2aa259b 100644 --- a/universal7885-common/apps/aidl-support/parts/default/Android.bp +++ b/universal7885-common/apps/aidl-support/parts/default/Android.bp @@ -5,7 +5,6 @@ cc_binary { "FlashLight.cpp", "Display.cpp", "Swap.cpp", - "SwapHelpers.cpp", "SmartCharge.cpp", "service.cpp", ], diff --git a/universal7885-common/apps/aidl-support/parts/default/Swap.cpp b/universal7885-common/apps/aidl-support/parts/default/Swap.cpp index aa6672f..b6d7434 100644 --- a/universal7885-common/apps/aidl-support/parts/default/Swap.cpp +++ b/universal7885-common/apps/aidl-support/parts/default/Swap.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2021 Eureka Team +// Copyright (C) 2022 Eureka Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -13,15 +13,98 @@ // limitations under the License. #include "Swap.h" +#include +#include +#include +#include #include #include #include +#include #include +#include #include #include +#include -extern int mkswap(const char *filename); -extern void mkfile(int filesize, const char *name); +namespace { + +/* XXX This needs to be obtained from kernel headers. See b/9336527 */ +struct linux_swap_header { + char bootbits[1024]; /* Space for disklabel etc. */ + u_int32_t version; + u_int32_t last_page; + u_int32_t nr_badpages; + unsigned char sws_uuid[16]; + unsigned char sws_volume[16]; + u_int32_t padding[117]; + u_int32_t badpages[1]; +}; + +void mkfile(int filesize, const char *path) { + std::vector empty(1024, 0); + std::ofstream ofs(std::string(path), std::ios::binary | std::ios::out); + + for (int i = 0; i < 1024 * filesize; i++) { + ofs.write(empty.data(), empty.size()); + } +} + +#define MAGIC_SWAP_HEADER "SWAPSPACE2" +#define MAGIC_SWAP_HEADER_LEN 10 +#define MIN_PAGES 10 + +int mkswap(const char *filename) { + int err = 0; + int fd; + ssize_t len; + off_t swap_size; + int pagesize; + struct linux_swap_header sw_hdr; + fd = open(filename, O_WRONLY | O_CLOEXEC); + if (fd < 0) { + err = errno; + return err; + } + pagesize = getpagesize(); + /* Determine the length of the swap file */ + swap_size = lseek(fd, 0, SEEK_END); + if (swap_size < MIN_PAGES * pagesize) { + err = -ENOSPC; + goto err; + } + if (lseek(fd, 0, SEEK_SET)) { + err = errno; + goto err; + } + memset(&sw_hdr, 0, sizeof(sw_hdr)); + sw_hdr.version = 1; + sw_hdr.last_page = (swap_size / pagesize) - 1; + len = write(fd, &sw_hdr, sizeof(sw_hdr)); + if (len != sizeof(sw_hdr)) { + err = errno; + goto err; + } + /* Write the magic header */ + if (lseek(fd, pagesize - MAGIC_SWAP_HEADER_LEN, SEEK_SET) < 0) { + err = errno; + goto err; + } + len = write(fd, MAGIC_SWAP_HEADER, MAGIC_SWAP_HEADER_LEN); + if (len != MAGIC_SWAP_HEADER_LEN) { + err = errno; + goto err; + } + if (fsync(fd) < 0) { + err = errno; + goto err; + } +err: + close(fd); + return err; +} + +} // namespace constexpr const char *SWAP_PATH = "/data/swap/swapfile"; @@ -29,9 +112,7 @@ namespace aidl::vendor::eureka::hardware::parts { static std::mutex thread_lock; -static inline bool swapfile_exist(void) { - return access(SWAP_PATH, F_OK) == 0; -} +static inline bool swapfile_exist(void) { return access(SWAP_PATH, F_OK) == 0; } static void makeFile(int32_t mSwapSize) { const std::lock_guard lock(thread_lock); @@ -40,7 +121,8 @@ static void makeFile(int32_t mSwapSize) { } ::ndk::ScopedAStatus SwapOnData::makeSwapFile(int32_t size) { - if (swapfile_exist()) return ::ndk::ScopedAStatus::ok(); + if (swapfile_exist()) + return ::ndk::ScopedAStatus::ok(); std::thread makefile_thread(makeFile, size); makefile_thread.detach(); @@ -53,7 +135,8 @@ static void rmswap(void) { } ::ndk::ScopedAStatus SwapOnData::removeSwapFile(void) { - if (!swapfile_exist()) return ::ndk::ScopedAStatus::ok(); + if (!swapfile_exist()) + return ::ndk::ScopedAStatus::ok(); std::thread rmswap_thread(rmswap); rmswap_thread.detach(); return ::ndk::ScopedAStatus::ok(); @@ -61,12 +144,15 @@ static void rmswap(void) { static void swapon_func(cb_t cb) { const std::lock_guard lock(thread_lock); - int res = swapon(SWAP_PATH, (10 << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK); - if (cb != nullptr) cb->respondToBool(res == 0); + int res = + swapon(SWAP_PATH, (10 << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK); + if (cb != nullptr) + cb->respondToBool(res == 0); } ::ndk::ScopedAStatus SwapOnData::setSwapOn(cb_t cb) { - if (!swapfile_exist()) return ::ndk::ScopedAStatus::ok(); + if (!swapfile_exist()) + return ::ndk::ScopedAStatus::ok(); std::thread swapon_thread(swapon_func, cb); swapon_thread.detach(); return ::ndk::ScopedAStatus::ok(); @@ -78,18 +164,19 @@ static void swapoff_func(void) { } ::ndk::ScopedAStatus SwapOnData::setSwapOff() { - if (!swapfile_exist()) return ::ndk::ScopedAStatus::ok(); + if (!swapfile_exist()) + return ::ndk::ScopedAStatus::ok(); std::thread swapoff_thread(swapoff_func); swapoff_thread.detach(); return ::ndk::ScopedAStatus::ok(); } ::ndk::ScopedAStatus SwapOnData::isMutexLocked(bool *_aidl_return) { - if (thread_lock.try_lock()) { - thread_lock.unlock(); - *_aidl_return = false; - } else { - *_aidl_return = true; + if (thread_lock.try_lock()) { + thread_lock.unlock(); + *_aidl_return = false; + } else { + *_aidl_return = true; } return ::ndk::ScopedAStatus::ok(); } diff --git a/universal7885-common/apps/aidl-support/parts/default/Swap.h b/universal7885-common/apps/aidl-support/parts/default/Swap.h index 3168b17..ea7cfed 100644 --- a/universal7885-common/apps/aidl-support/parts/default/Swap.h +++ b/universal7885-common/apps/aidl-support/parts/default/Swap.h @@ -18,7 +18,7 @@ namespace aidl::vendor::eureka::hardware::parts { -using cb_t = const std::shared_ptr&; +using cb_t = const std::shared_ptr &; struct SwapOnData : public BnSwapOnData { // Methods from ::aidl::vendor::eureka::hardware::parts::ISwapOnData @@ -29,4 +29,4 @@ struct SwapOnData : public BnSwapOnData { ::ndk::ScopedAStatus setSwapOff(void); ::ndk::ScopedAStatus isMutexLocked(bool *_aidl_return); }; -} // namespace vendor::eureka::hardware::parts::V1_0 +} // namespace aidl::vendor::eureka::hardware::parts diff --git a/universal7885-common/apps/aidl-support/parts/default/SwapHelpers.cpp b/universal7885-common/apps/aidl-support/parts/default/SwapHelpers.cpp deleted file mode 100644 index 20b06f3..0000000 --- a/universal7885-common/apps/aidl-support/parts/default/SwapHelpers.cpp +++ /dev/null @@ -1,82 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -/* XXX This needs to be obtained from kernel headers. See b/9336527 */ -struct linux_swap_header { - char bootbits[1024]; /* Space for disklabel etc. */ - u_int32_t version; - u_int32_t last_page; - u_int32_t nr_badpages; - unsigned char sws_uuid[16]; - unsigned char sws_volume[16]; - u_int32_t padding[117]; - u_int32_t badpages[1]; -}; -void mkfile(int filesize, const char *path) { - std::vector empty(1024, 0); - std::ofstream ofs(std::string(path), std::ios::binary | std::ios::out); - - for (int i = 0; i < 1024 * filesize; i++) { - ofs.write(&empty[0], empty.size()); - } -} -#define MAGIC_SWAP_HEADER "SWAPSPACE2" -#define MAGIC_SWAP_HEADER_LEN 10 -#define MIN_PAGES 10 -int mkswap(const char *filename) { - int err = 0; - int fd; - ssize_t len; - off_t swap_size; - int pagesize; - struct linux_swap_header sw_hdr; - fd = open(filename, O_WRONLY | O_CLOEXEC); - if (fd < 0) { - err = errno; - return err; - } - pagesize = getpagesize(); - /* Determine the length of the swap file */ - swap_size = lseek(fd, 0, SEEK_END); - if (swap_size < MIN_PAGES * pagesize) { - err = -ENOSPC; - goto err; - } - if (lseek(fd, 0, SEEK_SET)) { - err = errno; - goto err; - } - memset(&sw_hdr, 0, sizeof(sw_hdr)); - sw_hdr.version = 1; - sw_hdr.last_page = (swap_size / pagesize) - 1; - len = write(fd, &sw_hdr, sizeof(sw_hdr)); - if (len != sizeof(sw_hdr)) { - err = errno; - goto err; - } - /* Write the magic header */ - if (lseek(fd, pagesize - MAGIC_SWAP_HEADER_LEN, SEEK_SET) < 0) { - err = errno; - goto err; - } - len = write(fd, MAGIC_SWAP_HEADER, MAGIC_SWAP_HEADER_LEN); - if (len != MAGIC_SWAP_HEADER_LEN) { - err = errno; - goto err; - } - if (fsync(fd) < 0) { - err = errno; - goto err; - } -err: - close(fd); - return err; -}