Skip to content
Snippets Groups Projects
Commit 05cc2f99 authored by Ramon Nou's avatar Ramon Nou Committed by Ramon Nou
Browse files

Creating Gekko API example

parent 4a0811e8
No related branches found
No related tags found
No related merge requests found
......@@ -28,9 +28,17 @@
set (CMAKE_CXX_STANDARD 14)
add_executable(sfind sfind.cpp)
add_executable(gkfs_lib_example gkfs_lib_example.cpp)
target_link_libraries(gkfs_lib_example
PRIVATE gkfs_intercept
)
if(GKFS_INSTALL_TESTS)
install(TARGETS sfind
DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(TARGETS gkfs_lib_example
DESTINATION ${CMAKE_INSTALL_BINDIR}
)
endif()
/*
Copyright 2018-2022, Barcelona Supercomputing Center (BSC), Spain
Copyright 2015-2022, Johannes Gutenberg Universitaet Mainz, Germany
This software was partially supported by the
EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).
This software was partially supported by the
ADA-FS project under the SPPEXA project funded by the DFG.
This file is part of GekkoFS.
GekkoFS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GekkoFS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GekkoFS. If not, see <https://www.gnu.org/licenses/>.
SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <cmath>
#include <cstring>
#include <getopt.h>
#include <iostream>
#include <queue>
#include <regex.h>
#include <stdio.h>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits>
#include <cstdint>
#include <client/gkfs_functions.hpp>
using namespace std;
/* Function exported from GekkoFS LD_PRELOAD, code needs to be compiled with
* -fPIC */
extern "C" int gkfs_init()
__attribute__((weak));
extern "C" int gkfs_end()
__attribute__((weak));
void init_preload() {};
void destroy_preload() {};
void write_file(std::string filename){
// Opem File
int fd = gkfs::syscall::gkfs_open(filename,S_IRWXU,O_RDWR|O_CREAT);
cout << "FD open " << fd << endl;
char *buf = "testing";
// int size = gkfs::syscall::gkfs_write(fd, buf, 7);
// cout << "FD size" << size << endl;
gkfs::syscall::gkfs_close(fd);
}
void read_file(std::string filename){
int fdread = gkfs::syscall::gkfs_open(filename,S_IRWXU,O_RDONLY);
char *bufread = "TESTING\0";
int sizeread = gkfs::syscall::gkfs_read(fdread, bufread, 7);
cout << "Reading : " << sizeread << " --> " << bufread << endl;
gkfs::syscall::gkfs_close(fdread);
}
int main(int argc, char **argv) {
cout << "GekkoFS Client library test" << endl;
auto res = gkfs_init();
cout << "Init result " << res << endl;
//write_file ("/test.tmp");
read_file("/test.tmp");
res = gkfs_end();
cout << "End result " << res << endl;
}
......@@ -149,6 +149,9 @@ gkfs_getdents64(unsigned int fd, struct linux_dirent64* dirp,
int
gkfs_rmdir(const std::string& path);
int
gkfs_close(unsigned int fd);
#ifdef HAS_RENAME
int
gkfs_rename(const std::string& old_path, const std::string& new_path);
......
......@@ -47,5 +47,10 @@ init_preload() __attribute__((constructor));
void
destroy_preload() __attribute__((destructor));
extern "C" int
gkfs_init();
extern "C" int
gkfs_end();
#endif // IOINTERCEPT_PRELOAD_HPP
......@@ -1394,6 +1394,27 @@ gkfs_getdents64(unsigned int fd, struct linux_dirent64* dirp,
return written;
}
/**
* @brief Closes an fd. To be used externally
*
* @param fd
* @return int
*/
int gkfs_close(unsigned int fd) {
if(CTX->file_map()->exist(fd)) {
// No call to the daemon is required
CTX->file_map()->remove(fd);
return 0;
}
if(CTX->is_internal_fd(fd)) {
// the client application (for some reason) is trying to close an
// internal fd: ignore it
return 0;
}
return -1;
}
#ifdef HAS_SYMLINKS
#ifdef GKFS_ENABLE_UNUSED_FUNCTIONS
......
......@@ -91,17 +91,10 @@ hook_close(int fd) {
LOG(DEBUG, "{}() called with fd: {}", __func__, fd);
if(CTX->file_map()->exist(fd)) {
// No call to the daemon is required
CTX->file_map()->remove(fd);
auto ret = gkfs::syscall::gkfs_close(fd);
if (ret == 0)
return 0;
}
if(CTX->is_internal_fd(fd)) {
// the client application (for some reason) is trying to close an
// internal fd: ignore it
return 0;
}
return syscall_no_intercept_wrapper(SYS_close, fd);
}
......
......@@ -65,8 +65,10 @@ exit_error_msg(int errcode, const string& msg) {
// if we don't disable interception before calling ::exit()
// syscall hooks may find an inconsistent in shared state
// (e.g. the logger) and thus, crash
gkfs::preload::stop_interception();
CTX->disable_interception();
if(CTX->interception_enabled()) {
gkfs::preload::stop_interception();
CTX->disable_interception();
}
::exit(errcode);
}
......@@ -310,9 +312,39 @@ destroy_preload() {
ld_network_service.reset();
LOG(DEBUG, "RPC subsystem shut down");
gkfs::preload::stop_interception();
CTX->disable_interception();
LOG(DEBUG, "Syscall interception stopped");
if(CTX->interception_enabled()) {
gkfs::preload::stop_interception();
CTX->disable_interception();
LOG(DEBUG, "Syscall interception stopped");
}
LOG(INFO, "All subsystems shut down. Client shutdown complete.");
}
/**
* @brief External functions to call linking the library
*
*/
extern "C" int
gkfs_init() {
CTX->init_logging();
// from here ownwards it is safe to print messages
LOG(DEBUG, "Logging subsystem initialized");
gkfs::preload::init_environment();
return 4;
}
extern "C" int
gkfs_end() {
CTX->clear_hosts();
LOG(DEBUG, "Peer information deleted");
ld_network_service.reset();
LOG(DEBUG, "RPC subsystem shut down");
LOG(INFO, "All subsystems shut down. Client shutdown complete.");
return 4;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment