Skip to content

Commit

Permalink
[EGD-7031] Remove UT deps to the production image
Browse files Browse the repository at this point in the history
Remove dependency to the production image by creating separate sysroot
for test assets alongside with a test image Test.img.

Signed-off-by: Marcin Smoczyński <smoczynski.marcin@gmail.com>
  • Loading branch information
mpsm committed Jul 27, 2021
1 parent f9c6e02 commit 8a767f3
Show file tree
Hide file tree
Showing 48 changed files with 447 additions and 285 deletions.
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ endif ()

add_subdirectory(third-party)

if (${PROJECT_TARGET} STREQUAL "TARGET_Linux")
add_subdirectory(board/linux/libiosyscalls)
endif()
add_subdirectory(board)

add_subdirectory(source)
add_subdirectory(module-platform)
Expand Down
2 changes: 2 additions & 0 deletions board/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_library(board STATIC)
add_subdirectory(${PROJECT_TARGET_NAME})
1 change: 1 addition & 0 deletions board/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(libiosyscalls)
30 changes: 19 additions & 11 deletions board/linux/libiosyscalls/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@ project(iosyscalls VERSION 1.0
DESCRIPTION "linux syscalls wrapper to our VFS"
)


set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/syscalls_stdio.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/syscalls_scan_family.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/syscalls_posix.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/syscalls_posix_dirent.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/iosyscalls.cpp
)


set(INCLUDES
"${CMAKE_CURRENT_SOURCE_DIR}/include"
"${CMAKE_SOURCE_DIR}/module-os/board/linux"
Expand All @@ -22,8 +12,26 @@ set(INCLUDES
"${CMAKE_SOURCE_DIR}/module-vfs/include/internal"
)

add_library(${PROJECT_NAME} SHARED)

target_sources(
${PROJECT_NAME}

add_library(${PROJECT_NAME} SHARED ${SOURCES})
PRIVATE
src/interface.cpp
src/iosyscalls-internal.hpp
src/iosyscalls.cpp
src/syscalls_posix_dirent.cpp
src/syscalls_posix.cpp
src/syscalls_real.hpp
src/syscalls_scan_family.cpp
src/syscalls_stdio.cpp

PUBLIC
include/debug.hpp
include/iosyscalls.h
include/iosyscalls.hpp
)

target_compile_options( ${PROJECT_NAME} PRIVATE "-Wno-nonnull-compare" )
get_target_property( target_options ${PROJECT_NAME} COMPILE_OPTIONS)
Expand Down
7 changes: 7 additions & 0 deletions board/linux/libiosyscalls/include/iosyscalls.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

void iosyscalls_set_image_name(const char *newImageName);
void iosyscalls_set_sysroot(const char *newSysroot);
57 changes: 3 additions & 54 deletions board/linux/libiosyscalls/include/iosyscalls.hpp
Original file line number Diff line number Diff line change
@@ -1,56 +1,5 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once
#include <stddef.h>
#include <stdio.h>
#include <purefs/vfs_subsystem.hpp>

struct __dirstream;

namespace vfsn::linux::internal
extern "C"
{
bool redirect_to_image();
bool is_image_handle(const FILE *fil);
bool is_image_fd(int fd);
bool redirect_to_image(const char *inpath);
const char *npath_translate(const char *inpath, char *buffer);

struct FILEX
{
int fd{0};
int error{0};
int ungetchar{-1};
};

int to_native_fd(int fd);
int to_image_fd(int fd);

FILEX *allocate_filex(int fd);
bool is_filex(const void *fd);
void remove_filex(FILEX *fil);

void add_DIR_to_image_list(__dirstream *indir);
void remove_DIR_from_image_list(__dirstream *indir);
bool is_image_DIR(__dirstream *indir);

template <class Base, typename T, typename... Args>
auto invoke_fs(T Base::*lfs_fun, Args &&... args)
-> decltype((static_cast<Base *>(nullptr)->*lfs_fun)(std::forward<Args>(args)...))
{
auto vfs = purefs::subsystem::vfs_core();
if (!vfs) {
errno = EIO;
return -1;
}
auto ret = (vfs.get()->*lfs_fun)(std::forward<Args>(args)...);
if (ret < 0) {
errno = -ret;
ret = -1;
}
else {
errno = 0;
}
return ret;
}
} // namespace vfsn::linux::internal
#include "iosyscalls.h"
}
19 changes: 19 additions & 0 deletions board/linux/libiosyscalls/src/interface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "iosyscalls-internal.hpp"

extern "C"
{
namespace vfs = vfsn::linux::internal;

void iosyscalls_set_sysroot(const char *newSysroot)
{
vfs::set_sysroot(newSysroot);
}

void iosyscalls_set_image_name(const char *newImageName)
{
vfs::set_image_path(newImageName);
}
}
58 changes: 58 additions & 0 deletions board/linux/libiosyscalls/src/iosyscalls-internal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once
#include <stddef.h>
#include <stdio.h>
#include <purefs/vfs_subsystem.hpp>

struct __dirstream;

namespace vfsn::linux::internal
{
void set_image_path(const char *newImageName);
void set_sysroot(const char *newSysroot);
bool redirect_to_image();
bool is_image_handle(const FILE *fil);
bool is_image_fd(int fd);
bool redirect_to_image(const char *inpath);
const char *npath_translate(const char *inpath, char *buffer);

struct FILEX
{
int fd{0};
int error{0};
int ungetchar{-1};
};

int to_native_fd(int fd);
int to_image_fd(int fd);

FILEX *allocate_filex(int fd);
bool is_filex(const void *fd);
void remove_filex(FILEX *fil);

void add_DIR_to_image_list(__dirstream *indir);
void remove_DIR_from_image_list(__dirstream *indir);
bool is_image_DIR(__dirstream *indir);

template <class Base, typename T, typename... Args>
auto invoke_fs(T Base::*lfs_fun, Args &&... args)
-> decltype((static_cast<Base *>(nullptr)->*lfs_fun)(std::forward<Args>(args)...))
{
auto vfs = purefs::subsystem::vfs_core();
if (!vfs) {
errno = EIO;
return -1;
}
auto ret = (vfs.get()->*lfs_fun)(std::forward<Args>(args)...);
if (ret < 0) {
errno = -ret;
ret = -1;
}
else {
errno = 0;
}
return ret;
}
} // namespace vfsn::linux::internal
95 changes: 65 additions & 30 deletions board/linux/libiosyscalls/src/iosyscalls.cpp
Original file line number Diff line number Diff line change
@@ -1,45 +1,55 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <iosyscalls.hpp>
#include <cstdlib>
#include <cstring>
#include <pthread.h>
#include <parallel_hashmap/phmap.h>
#include <stdio.h>
#include <dlfcn.h>
#include <stdarg.h>
#include "iosyscalls-internal.hpp"

#include <debug.hpp>
#include <parallel_hashmap/phmap.h>

#include <dirent.h>
#include <dlfcn.h>
#include <pthread.h>

#include <algorithm>

#include <cassert>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>

namespace
{
constexpr auto ENV_NAME = "IOSYSCALLS_REDIRECT_TO_IMAGE";
constexpr auto FIRST_FILEDESC = 64'566'756;
constexpr auto SYSROOT = "sysroot";
bool g_evaluated = false;
bool g_redirect = false;

constexpr const char *LINUX_PATHS[]{"/dev/",
"/etc/",
"/lib",
"/usr/share",
"/run/user",
"/home",
"/proc",
"PurePhone.img",
"MuditaOS.log",
"/tmp",
nullptr};
constexpr const char *LINUX_PATHS[]{
"/dev/", "/etc/", "/lib", "/usr/share", "/run/user", "/home", "/proc", "/tmp", "MuditaOS.log", nullptr};

constexpr const char *IMAGE_PATHS[]{"/sys", "/mfgconf", "assets", "country-codes.db", "Luts.bin", nullptr};
constexpr const char *IMAGE_PATHS[]{"/sys", "/mfgconf", "sys", "assets", "country-codes.db", "Luts.bin", nullptr};

pthread_mutex_t g_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
phmap::flat_hash_set<vfsn::linux::internal::FILEX *> g_fdlist;
phmap::flat_hash_set<DIR *> g_dirlist;
std::string imageFileName;
std::string sysroot = SYSROOT;
} // namespace

namespace vfsn::linux::internal
{
void set_sysroot(const char *newSysroot)
{
sysroot = newSysroot;
}

void set_image_path(const char *newImageName)
{
imageFileName = std::string(newImageName);
}

bool redirect_to_image()
{
if (!g_evaluated) {
Expand All @@ -52,33 +62,58 @@ namespace vfsn::linux::internal

bool redirect_to_image(const char *inpath)
{
if (!redirect_to_image())
if (!redirect_to_image()) {
return false;
}

for (auto path = LINUX_PATHS; *path; ++path)
if (std::strstr(inpath, *path) == inpath)
if (std::strstr(inpath, imageFileName.c_str()) == inpath) {
return false;
}

for (auto path = LINUX_PATHS; *path; ++path) {
if (std::strstr(inpath, *path) == inpath) {
return false;
}
}

return true;
}

const char *npath_translate(const char *inpath, char *buffer)
{
for (auto path = IMAGE_PATHS; *path; ++path)
auto inputPath = std::string(inpath);

for (auto path = IMAGE_PATHS; *path != 0; ++path) {
if (std::strstr(inpath, *path) == inpath) {
if (!std::strcmp(*path, "/mfgconf")) {
std::strncpy(buffer, "sys/", PATH_MAX);
std::strncpy(buffer + 3, inpath + 8, PATH_MAX - 3);
std::string outpath;

if (std::strcmp(*path, "/mfgconf") == 0) {
outpath = sysroot;
outpath += "/sys/";
outpath += inputPath;
}
else if (std::strcmp(*path, "sys") == 0) {
outpath = sysroot;
outpath += "/";
outpath += inputPath;
}
else if (*inpath == '/') {
std::strncpy(buffer, inpath + 1, PATH_MAX);
outpath = sysroot + inputPath;
}
else {
std::strncpy(buffer, "sys/current/", PATH_MAX);
std::strncpy(buffer + 12, inpath, PATH_MAX - 12);
outpath = sysroot;
outpath += "/sys/current/";
outpath += inputPath;
}

assert(outpath.size() < PATH_MAX);
std::copy_n(std::begin(outpath), outpath.size(), buffer);
buffer[outpath.size()] = '\0';

return buffer;
}
}

return inpath;
}

Expand Down
20 changes: 10 additions & 10 deletions board/linux/libiosyscalls/src/syscalls_posix.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <iosyscalls.hpp>
#include "iosyscalls-internal.hpp"

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/mount.h>
#include <sys/ioctl.h>
#include <poll.h>
#include <fcntl.h>
#include <errno.h>
#include <stdarg.h> // for va_*
#include <fcntl.h>
#include <limits.h> // for PATH_MAX
#include <poll.h>
#include <stdarg.h> // for va_*
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/types.h>
#include <unistd.h>

#include "syscalls_real.hpp"

Expand Down
2 changes: 1 addition & 1 deletion board/linux/libiosyscalls/src/syscalls_posix_dirent.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <iosyscalls.hpp>
#include "iosyscalls-internal.hpp"

#include <dirent.h>
#include <errno.h>
Expand Down
Loading

0 comments on commit 8a767f3

Please sign in to comment.