Skip to content

Commit

Permalink
Merge pull request #23 from isanae/new-parameters
Browse files Browse the repository at this point in the history
New parameters structure
  • Loading branch information
isanae authored Oct 11, 2019
2 parents 40959ce + 2d515e7 commit 4e476ec
Show file tree
Hide file tree
Showing 17 changed files with 365 additions and 92 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@
*.vcxproj.user
msbuild.log

/stderr.log
/stderr_32.log
/stdout.log
/stdout_32.log
27 changes: 19 additions & 8 deletions include/usvfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,20 @@ DLLEXPORT BOOL WINAPI VirtualLinkDirectoryStatic(LPCWSTR source, LPCWSTR destina
* connect to a virtual filesystem as a controller, without hooking the calling process. Please note that
* you can only be connected to one vfs, so this will silently disconnect from a previous vfs.
*/
[[deprecated("deprecated, use usvfsConnectVFS()")]]
DLLEXPORT BOOL WINAPI ConnectVFS(const USVFSParameters *parameters);

DLLEXPORT BOOL WINAPI usvfsConnectVFS(const usvfsParameters* p);

/**
* @brief create a new VFS. This is similar to ConnectVFS except it guarantees
* the vfs is reset before use.
*/
[[deprecated("deprecated, use usvfsCreateVFS()")]]
DLLEXPORT BOOL WINAPI CreateVFS(const USVFSParameters *parameters);

DLLEXPORT BOOL WINAPI usvfsCreateVFS(const usvfsParameters* p);

/**
* disconnect from a virtual filesystem. This removes hooks if necessary
*/
Expand Down Expand Up @@ -117,11 +123,6 @@ DLLEXPORT BOOL WINAPI CreateProcessHooked(
*/
DLLEXPORT bool WINAPI GetLogMessages(LPSTR buffer, size_t size, bool blocking = false);

/**
* @brief Used to change parameters which can be changed in runtime
*/
DLLEXPORT void WINAPI USVFSUpdateParams(LogLevel level, CrashDumpsType type);

/**
* retrieves a readable representation of the vfs tree
* @param buffer the buffer to write to. this may be null if you only want to determine the required
Expand Down Expand Up @@ -170,14 +171,24 @@ DLLEXPORT void WINAPI InitLogging(bool toLocal = false);
*/
DLLEXPORT void __cdecl InitHooks(LPVOID userData, size_t userDataSize);


[[deprecated("deprecated, use usvfsCreateParameters()")]]
DLLEXPORT void WINAPI USVFSInitParameters(USVFSParameters *parameters,
const char *instanceName,
bool debugMode,
LogLevel logLevel,
CrashDumpsType crashDumpsType,
const char *crashDumpsPath,
std::chrono::milliseconds delayProcess={});
const char *crashDumpsPath);

/**
* @brief Used to change parameters which can be changed in runtime
*/
[[deprecated("deprecated, use usvfsUpdateParameters()")]]
DLLEXPORT void WINAPI USVFSUpdateParams(LogLevel level, CrashDumpsType type);

// the only information used from the parameters are the crash dump type, log
// level and process delay
//
DLLEXPORT void WINAPI usvfsUpdateParameters(usvfsParameters* p);

DLLEXPORT int WINAPI CreateMiniDump(PEXCEPTION_POINTERS exceptionPtrs, CrashDumpsType type, const wchar_t* dumpPath);

Expand Down
21 changes: 19 additions & 2 deletions include/usvfsparameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ enum class CrashDumpsType : uint8_t {
Full
};

extern "C" {
extern "C"
{

// deprecated, use usvfsParameters and usvfsCreateParameters()
//
struct USVFSParameters {
char instanceName[65];
char currentSHMName[65];
Expand All @@ -41,7 +44,21 @@ struct USVFSParameters {
LogLevel logLevel{LogLevel::Debug};
CrashDumpsType crashDumpsType{CrashDumpsType::None};
char crashDumpsPath[260];
std::chrono::milliseconds delayProcess{0};
};


struct usvfsParameters;

DLLEXPORT usvfsParameters* usvfsCreateParameters();
DLLEXPORT usvfsParameters* usvfsDupeParameters(usvfsParameters* p);
DLLEXPORT void usvfsCopyParameters(const usvfsParameters* source, usvfsParameters* dest);
DLLEXPORT void usvfsFreeParameters(usvfsParameters* p);

DLLEXPORT void usvfsSetInstanceName(usvfsParameters* p, const char* name);
DLLEXPORT void usvfsSetDebugMode(usvfsParameters* p, BOOL debugMode);
DLLEXPORT void usvfsSetLogLevel(usvfsParameters* p, LogLevel level);
DLLEXPORT void usvfsSetCrashDumpType(usvfsParameters* p, CrashDumpsType type);
DLLEXPORT void usvfsSetCrashDumpPath(usvfsParameters* p, const char* path);
DLLEXPORT void usvfsSetProcessDelay(usvfsParameters* p, int milliseconds);

}
37 changes: 37 additions & 0 deletions include/usvfsparametersprivate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once
#include "usvfsparameters.h"

struct usvfsParameters
{
char instanceName[65];
char currentSHMName[65];
char currentInverseSHMName[65];
bool debugMode;
LogLevel logLevel{LogLevel::Debug};
CrashDumpsType crashDumpsType{CrashDumpsType::None};
char crashDumpsPath[260];
int delayProcessMs;

usvfsParameters();
usvfsParameters(const usvfsParameters&) = default;
usvfsParameters& operator=(const usvfsParameters&) = default;

usvfsParameters(
const char* instanceName,
const char* currentSHMName,
const char* currentInverseSHMName,
bool debugMode,
LogLevel logLevel,
CrashDumpsType crashDumpsType,
const char* crashDumpsPath,
int delayProcessMs);

usvfsParameters(const USVFSParameters& oldParams);

void setInstanceName(const char* name);
void setDebugMode(bool debugMode);
void setLogLevel(LogLevel level);
void setCrashDumpType(CrashDumpsType type);
void setCrashDumpPath(const char* path);
void setProcessDelay(int milliseconds);
};
62 changes: 28 additions & 34 deletions src/usvfs_dll/hookcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,41 +56,19 @@ void printBuffer(const char *buffer, size_t size)
}


USVFSParameters SharedParameters::makeLocal() const
usvfsParameters SharedParameters::makeLocal() const
{
USVFSParameters result;
USVFSInitParametersInt(&result, instanceName.c_str(),
currentSHMName.c_str(),
currentInverseSHMName.c_str(),
debugMode, logLevel, crashDumpsType,
crashDumpsPath.c_str(),
delayProcess);
return result;
}


void usvfs::USVFSInitParametersInt(USVFSParameters *parameters,
const char *instanceName,
const char *currentSHMName,
const char *currentInverseSHMName,
bool debugMode,
LogLevel logLevel,
CrashDumpsType crashDumpsType,
const char *crashDumpsPath,
std::chrono::milliseconds delayProcess)
{
parameters->debugMode = debugMode;
parameters->logLevel = logLevel;
parameters->crashDumpsType = crashDumpsType;
parameters->delayProcess = delayProcess;
strncpy_s(parameters->instanceName, instanceName, _TRUNCATE);
strncpy_s(parameters->currentSHMName, currentSHMName, _TRUNCATE);
strncpy_s(parameters->currentInverseSHMName, currentInverseSHMName, _TRUNCATE);
strncpy_s(parameters->crashDumpsPath, crashDumpsPath, _TRUNCATE);
return usvfsParameters(
instanceName.c_str(),
currentSHMName.c_str(),
currentInverseSHMName.c_str(),
debugMode, logLevel, crashDumpsType,
crashDumpsPath.c_str(),
delayProcess.count());
}


HookContext::HookContext(const USVFSParameters &params, HMODULE module)
HookContext::HookContext(const usvfsParameters& params, HMODULE module)
: m_ConfigurationSHM(bi::open_or_create, params.instanceName, 8192)
, m_Parameters(retrieveParameters(params))
, m_Tree(m_Parameters->currentSHMName.c_str(), 65536)
Expand Down Expand Up @@ -135,7 +113,7 @@ HookContext::~HookContext()
}
}

SharedParameters *HookContext::retrieveParameters(const USVFSParameters &params)
SharedParameters *HookContext::retrieveParameters(const usvfsParameters& params)
{
std::pair<SharedParameters *, SharedMemoryT::size_type> res
= m_ConfigurationSHM.find<SharedParameters>("parameters");
Expand Down Expand Up @@ -182,13 +160,18 @@ void HookContext::setCrashDumpsType(CrashDumpsType type)
m_Parameters->crashDumpsType = type;
}

void HookContext::setDelayProcess(std::chrono::milliseconds delay)
{
m_Parameters->delayProcess = delay;
}

void HookContext::updateParameters() const
{
m_Parameters->currentSHMName = m_Tree.shmName().c_str();
m_Parameters->currentInverseSHMName = m_InverseTree.shmName().c_str();
}

USVFSParameters HookContext::callParameters() const
usvfsParameters HookContext::callParameters() const
{
updateParameters();
return m_Parameters->makeLocal();
Expand Down Expand Up @@ -308,7 +291,18 @@ void HookContext::unlockShared(const HookContext *instance)
instance->m_Mutex.signal();
}

extern "C" DLLEXPORT HookContext *__cdecl CreateHookContext(const USVFSParameters &params, HMODULE module)

// deprecated
//
extern "C" DLLEXPORT HookContext *__cdecl CreateHookContext(
const USVFSParameters &oldParams, HMODULE module)
{
const usvfsParameters p(oldParams);
return usvfsCreateHookContext(p, module);
}

extern "C" DLLEXPORT usvfs::HookContext* WINAPI usvfsCreateHookContext(
const usvfsParameters& params, HMODULE module)
{
return new HookContext(params, module);
}
36 changes: 17 additions & 19 deletions src/usvfs_dll/hookcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ along with usvfs. If not, see <http://www.gnu.org/licenses/>.
#include "dllimport.h"
#include "semaphore.h"
#include <usvfsparameters.h>
#include <usvfsparametersprivate.h>
#include <directory_tree.h>
#include <exceptionex.h>
#include <winapi.h>
Expand All @@ -42,17 +43,6 @@ along with usvfs. If not, see <http://www.gnu.org/licenses/>.
namespace usvfs
{

void USVFSInitParametersInt(USVFSParameters *parameters,
const char *instanceName,
const char *currentSHMName,
const char *currentInverseSHMName,
bool debugMode,
LogLevel logLevel,
CrashDumpsType crashDumpsType,
const char *crashDumpsPath,
std::chrono::milliseconds delayProcess);


typedef shared::VoidAllocatorT::rebind<DWORD>::other DWORDAllocatorT;
typedef shared::VoidAllocatorT::rebind<shared::StringT>::other StringAllocatorT;

Expand All @@ -78,7 +68,7 @@ struct SharedParameters {

SharedParameters &operator=(const SharedParameters &reference) = delete;

SharedParameters(const USVFSParameters &reference,
SharedParameters(const usvfsParameters& reference,
const shared::VoidAllocatorT &allocator)
: instanceName(reference.instanceName, allocator)
, currentSHMName(reference.currentSHMName, allocator)
Expand All @@ -87,15 +77,15 @@ struct SharedParameters {
, logLevel(reference.logLevel)
, crashDumpsType(reference.crashDumpsType)
, crashDumpsPath(reference.crashDumpsPath, allocator)
, delayProcess(reference.delayProcess)
, delayProcess(reference.delayProcessMs)
, userCount(1)
, processBlacklist(allocator)
, processList(allocator)
, forcedLibraries(allocator)
{
}

DLLEXPORT USVFSParameters makeLocal() const;
DLLEXPORT usvfsParameters makeLocal() const;

shared::StringT instanceName;
shared::StringT currentSHMName;
Expand Down Expand Up @@ -127,7 +117,7 @@ class HookContext
typedef unsigned int DataIDT;

public:
HookContext(const USVFSParameters &params, HMODULE module);
HookContext(const usvfsParameters& params, HMODULE module);

HookContext(const HookContext &reference) = delete;

Expand Down Expand Up @@ -180,7 +170,7 @@ class HookContext
/**
* @return the parameters passed in on dll initialisation
*/
USVFSParameters callParameters() const;
usvfsParameters callParameters() const;

/**
* @return true if usvfs is running in debug mode
Expand Down Expand Up @@ -228,6 +218,7 @@ class HookContext

void setLogLevel(LogLevel level);
void setCrashDumpsType(CrashDumpsType type);
void setDelayProcess(std::chrono::milliseconds delay);

void updateParameters() const;

Expand All @@ -239,7 +230,7 @@ class HookContext
static void unlock(HookContext *instance);
static void unlockShared(const HookContext *instance);

SharedParameters *retrieveParameters(const USVFSParameters &params);
SharedParameters* retrieveParameters(const usvfsParameters& params);

private:
static HookContext *s_Instance;
Expand All @@ -261,12 +252,19 @@ class HookContext
// mutable std::recursive_mutex m_Mutex;
mutable RecursiveBenaphore m_Mutex;
};
}

} // namespace


// exposed only to unit tests for easier testability
extern "C" DLLEXPORT usvfs::HookContext *__cdecl CreateHookContext(
extern "C" [[deprecated("deprecated, use usvfsCreateHookContext()")]]
DLLEXPORT usvfs::HookContext *__cdecl CreateHookContext(
const USVFSParameters &params, HMODULE module);

extern "C" DLLEXPORT usvfs::HookContext* WINAPI usvfsCreateHookContext(
const usvfsParameters& params, HMODULE module);


class PreserveGetLastError
{
public:
Expand Down
2 changes: 1 addition & 1 deletion src/usvfs_dll/hookmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace usvfs {
HookManager *HookManager::s_Instance = nullptr;


HookManager::HookManager(const USVFSParameters &params, HMODULE module)
HookManager::HookManager(const usvfsParameters& params, HMODULE module)
: m_Context(params, module)
{
if (s_Instance != nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion src/usvfs_dll/hookmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class HookManager
{
public:

HookManager(const USVFSParameters &params, HMODULE module);
HookManager(const usvfsParameters& params, HMODULE module);
~HookManager();

HookManager(const HookManager &reference) = delete;
Expand Down
2 changes: 1 addition & 1 deletion src/usvfs_dll/hooks/kernel32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ BOOL WINAPI usvfs::hook_CreateProcessInternalW(
LPWSTR cend = nullptr;

std::wstring dllPath;
USVFSParameters callParameters;
usvfsParameters callParameters;

{ // scope for context lock
auto context = READ_CONTEXT();
Expand Down
Loading

0 comments on commit 4e476ec

Please sign in to comment.