Verified Commit 06c72fe6 authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

Store CWD in preload context

During interception library startup the CWD of intercepted process is
stored in the PreloadContext object
parent c8ded6f4
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -3,11 +3,14 @@

#include <string>

#define PATH_MAX_LEN 4096 // 4k chars

bool is_relative_path(const std::string& path);
bool is_absolute_path(const std::string& path);
bool has_trailing_slash(const std::string& path);

std::string path_to_relative(const std::string& root_path, const std::string& complete_path);
std::string dirname(const std::string& path);
std::string get_current_working_dir();

#endif //IFS_PATH_UTIL_HPP
+4 −0
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ class PreloadContext {
    std::shared_ptr<Distributor> distributor_;
    std::shared_ptr<FsConfig> fs_conf_;

    std::string cwd_;
    std::string mountdir_;
    bool initialized_;

@@ -62,6 +63,9 @@ class PreloadContext {
    void mountdir(const std::string& path);
    std::string mountdir() const;

    void cwd(const std::string& path);
    std::string cwd() const;

    bool relativize_path(std::string& path) const;

    const std::shared_ptr<OpenFileMap>& file_map() const;
+18 −0
Original line number Diff line number Diff line
#include <global/path_util.hpp>
#include <unistd.h>
#include <system_error>
#include <cassert>


@@ -69,3 +71,19 @@ std::string dirname(const std::string& path) {
    }
    return path.substr(0, parent_path_size);
}

std::string get_current_working_dir() {
    char temp[PATH_MAX_LEN];
    if(getcwd(temp, PATH_MAX_LEN) == NULL) {
        throw std::system_error(errno,
                                std::system_category(),
                                "Failed to retrieve current working directory");
    }
    // getcwd could return "(unreachable)<PATH>" in some cases
    if(temp[0] != '/') {
        throw std::runtime_error(
                "Current working directory is unreachable");
    }
    return {temp};
}
+3 −0
Original line number Diff line number Diff line
#include <global/log_util.hpp>
#include <global/path_util.hpp>
#include <global/global_defs.hpp>
#include <global/configure.hpp>
#include <preload/preload.hpp>
@@ -305,6 +306,8 @@ void init_preload() {
    init_passthrough_if_needed();
    init_logging();
    CTX->log()->debug("Initialized logging subsystem");
    CTX->cwd(get_current_working_dir());
    CTX->log()->debug("Current working directory: '{}'", CTX->cwd());
    if (get_daemon_pid() == -1 || CTX->mountdir().empty()) {
        cerr << "ADA-FS daemon not running or mountdir could not be loaded. Check adafs_preload.log" << endl;
        CTX->log()->error("{}() Daemon not running or mountdir not set", __func__);
+8 −0
Original line number Diff line number Diff line
@@ -28,6 +28,14 @@ std::string PreloadContext::mountdir() const {
    return mountdir_;
}

void PreloadContext::cwd(const std::string& path) {
    cwd_ = path;
}

std::string PreloadContext::cwd() const {
    return cwd_;
}

bool PreloadContext::relativize_path(std::string& path) const {
    // Relativize path should be called only after the library constructor has been executed
    assert(initialized_);