universal7885: interfaces: Do not use hardcoded cmd

Use standard C/C++ functions
This commit is contained in:
roynatech2544 2022-05-21 02:28:47 +00:00
commit 22caa5374c
3 changed files with 89 additions and 12 deletions

View file

@ -8,6 +8,7 @@ cc_binary {
"FlashLight.cpp",
"Display.cpp",
"Swap.cpp",
"SwapHelpers.cpp",
"service.cpp",
],
shared_libs: [

View file

@ -15,11 +15,15 @@
#include "Swap.h"
#include <fstream>
#include <iostream>
#include <sys/swap.h>
#include <stdio.h>
static int mSwapSize = 100;
extern int mkswap (const std::string filename);
extern void mkfile(const int filesize, const std::string name);
#define SWAP_PATH "/data/swap/swapfile"
using namespace std;
namespace vendor::eureka::hardware::parts::V1_0 {
Return<void> SwapOnData::setSwapSize(int32_t size) {
@ -28,21 +32,15 @@ Return<void> SwapOnData::setSwapSize(int32_t size) {
}
Return<void> SwapOnData::setSwapOn() {
string cmd = string("dd if=/dev/zero of=") + string(SWAP_PATH) +
string(" bs=") + std::to_string(mSwapSize) + "M count=10";
system(cmd.c_str());
cmd = string("mkswap ") + string(SWAP_PATH);
system(cmd.c_str());
cmd = string("swapon -p 99 ") + string(SWAP_PATH);
system(cmd.c_str());
mkfile(mSwapSize * 1024 * 1024 * 10, SWAP_PATH);
mkswap(SWAP_PATH);
swapon(SWAP_PATH, (10 << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK);
return Void();
}
Return<void> SwapOnData::setSwapOff() {
std::string cmd = string("swapoff ") + string(SWAP_PATH);
system(cmd.c_str());
cmd = string("rm ") + string(SWAP_PATH);
system(cmd.c_str());
swapoff(SWAP_PATH);
remove(SWAP_PATH);
return Void();
}

View file

@ -0,0 +1,78 @@
#include <cstring>
#include <errno.h>
#include <fcntl.h>
#include <iostream>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/swap.h>
#include <sys/types.h>
#include <unistd.h>
/* 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(const int filesize, const std::string& name){
FILE *fp = fopen(name.c_str(), "we");
fseek(fp, filesize , SEEK_SET);
fputc('\0', fp);
fclose(fp);
}
#define MAGIC_SWAP_HEADER "SWAPSPACE2"
#define MAGIC_SWAP_HEADER_LEN 10
#define MIN_PAGES 10
int mkswap(const std::string& filename) {
int err = 0;
int fd;
ssize_t len;
off_t swap_size;
int pagesize;
struct linux_swap_header sw_hdr;
fd = open(filename.c_str(), 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;
}