Skip to content

Commit

Permalink
Remove Posix specific platform code. Let's redirect to the system cal…
Browse files Browse the repository at this point in the history
…l instead
  • Loading branch information
X-Ryl669 committed Feb 16, 2021
1 parent ee5bcf6 commit b197dac
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 403 deletions.
1 change: 0 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
add_library(eMQTT5
src/Network/Clients/MQTTClient.cpp
$<$<PLATFORM_ID:WIN32>:src/Platform/Windows.cpp>
$<$<NOT:$<PLATFORM_ID:WIN32>>:src/Platform/Posix.cpp>

This comment has been minimized.

Copy link
@shinyaohtani

shinyaohtani Feb 16, 2021

You said we need Posix.cpp but you removed it. Is this ok?

This comment has been minimized.

Copy link
@X-Ryl669

X-Ryl669 Feb 16, 2021

Author Owner

Yes, the 4 method (free, realloc, malloc) are simple wrapper to the system's version. It's a useless cost, so I've rewritten them.

)

set_target_properties(eMQTT5 PROPERTIES
Expand Down
160 changes: 59 additions & 101 deletions lib/include/Platform/Platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,34 @@ namespace Platform
// When being build in embedded code, don't pay for a wrapper here, instead forward to the system's functions
inline void * malloc(size_t size) { return ::malloc(size); }
inline void * calloc(size_t a, size_t b) { return ::calloc(a,b); }
inline void free(void * p) { return ::free(p);
inline void free(void * p) { return ::free(p); }
inline void * realloc(void * p, size_t size) { return ::realloc(p, size); }

/** Get the current process name.
This does not rely on remembering the argv[0] since this does not exists on Windows.
This returns the name of executable used to run the process */
inline const char * getProcessName() {
{
static char * processName = NULL;
if (!processName)
{
#ifdef _LINUX
FILE * f = fopen("/proc/self/cmdline", "r");
if (f) {
char buffer[256];
processName = strdup(fgets(buffer, 256, f));
fclose(f);
}
#elif defined(_MAC)
processName = strdup(getprogname());
#else
return "program";
#endif
}
return processName;
}

return "this_program"; } // Poor workaround to avoid so many difference between platforms
#else
/** The simple malloc overload.
If you need to use another allocator, you should define this method
Expand Down Expand Up @@ -74,6 +100,10 @@ namespace Platform
@param size The required size of the new area in bytes
@warning Realloc is intrinsically unsafe to use, since it can leak memory in most case, use safeRealloc instead */
void * realloc(void * p, size_t size);
/** Get the current process name.
This does not rely on remembering the argv[0] since this does not exists on Windows.
This returns the name of executable used to run the process */
const char * getProcessName();
#endif

/** The safe realloc method.
Expand All @@ -94,58 +124,42 @@ namespace Platform

return other;
}
/** Ask for a hidden input that'll be stored in the UTF-8 buffer.
This requires a console.
Under Windows, this requires the process to be run from a command line.
This is typically required for asking a password.
New line are not retained in the output, if present.
@param prompt The prompt that's displayed on user console
@param buffer A pointer to a buffer that's at least (size) byte large
that'll be filled by the function
@param size On input, the buffer size, on output, it's set to the used buffer size
@return false if it can not hide the input, or if it can't get any char in it */
bool queryHiddenInput(const char * prompt, char * buffer, size_t & size);
/** Get the current process name.
This does not rely on remembering the argv[0] since this does not exists on Windows.
This returns the name of executable used to run the process */
const char * getProcessName();

inline bool isUnderDebugger()
{

inline bool isUnderDebugger()
{
#if (DEBUG==1)
#ifdef _WIN32
return (IsDebuggerPresent() == TRUE);
return (IsDebuggerPresent() == TRUE);
#elif defined(_LINUX)
static signed char testResult = 0;
if (testResult == 0)
{
testResult = (char) ptrace (PT_TRACE_ME, 0, 0, 0);
if (testResult >= 0)
{
ptrace (PT_DETACH, 0, (caddr_t) 1, 0);
testResult = 1;
}
}
static signed char testResult = 0;
if (testResult == 0)
{
testResult = (char) ptrace (PT_TRACE_ME, 0, 0, 0);
if (testResult >= 0)
{
ptrace (PT_DETACH, 0, (caddr_t) 1, 0);
testResult = 1;
}
}
return (testResult < 0);
#elif defined (_MAC)
static signed char testResult = 0;
if (testResult == 0)
{
struct kinfo_proc info;
int m[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
size_t sz = sizeof (info);
sysctl (m, 4, &info, &sz, 0, 0);
testResult = ((info.kp_proc.p_flag & P_TRACED) != 0) ? 1 : -1;
}
static signed char testResult = 0;
if (testResult == 0)
{
struct kinfo_proc info;
int m[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
size_t sz = sizeof (info);
sysctl (m, 4, &info, &sz, 0, 0);
testResult = ((info.kp_proc.p_flag & P_TRACED) != 0) ? 1 : -1;
}

return testResult > 0;
return testResult > 0;
#elif defined (NEXIO)
return true;
#endif
return true;
#endif
#endif
return false;
}
return false;
}

/** This is used to trigger the debugger when called */
inline void breakUnderDebugger()
Expand Down Expand Up @@ -193,64 +207,8 @@ namespace Platform
~FileIndexWrapper() { if (fd >= 0) close(fd); fd = -1; }
};

/** Turn the current process into a daemon.
Log will be redirected to syslog service,
Input and output file descriptor will be closed, and we'll detach from the
running terminal.
@warning If you intend to run a server or anything that does file-manipulation, please remember
that this is forking and the parent must call _exit() or std::quick_exit() and not exit() or return from main.
In the later case, the destructors will likely modify the file descriptors of the shared resources (with the child
daemon) and lead to hard to debug issues.
@param pathToPIDFile The path to the file containing the daemon PID (useful for system script typically)
@param syslogName The name of the syslog reported daemon
@param parent On parent process will be set to true, and false in child process
@return false for forking error */
bool daemonize(const char * pathToPIDFile, const char * syslogName, bool & parent);

/** Drop super user privileges.
After calling this function, the real id are the effective id and saved id.
Ancillary groups are also dropped.
@warning You must check the return for this function else if your program is compromised, you might leave escalation issues.
@param dropUserID If true, the real user ID will be used to overrride all other user ID
@param dropGroupID If true, the read group ID will be used to override all other group ID
@return true on success. */
bool dropPrivileges(const bool dropUserID = true, const bool dropGroupID = true);
#endif

/** This structure is used to load some code dynamically from a file on the filesystem */
class DynamicLibrary
{
// Members
private:
/** The library internal handle */
void * handle;

// Interface
public:
/** Load the given symbol out of this library
@param nameInUTF8 The name of the symbol. It's up to the caller to ensure cross platform name are used
@return A pointer on the loaded symbol, or 0 if not found */
void * loadSymbol(const char * nameInUTF8) const;
/** Load a symbol and cast it to the given format.
@param nameInUTF8 The name of the symbol. It's up to the caller to ensure cross platform name are used
@sa loadSymbol */
template <class T>
inline T loadSymbolAs(const char * nameInUTF8) const { return reinterpret_cast<T>(loadSymbol(nameInUTF8)); }
/** Get the platform expected file name for the given library name
@param libraryName The name of the library, excluding suffix (like .DLL, or .so).
@param outputName A pointer to a buffer that at least 10 bytes larger than the libraryName buffer. */
static void getPlatformName(const char * libraryName, char * outputName);
/** Check if the library has loaded correctly */
inline bool isLoaded() const { return handle != 0; }

// Construction and destruction
public:
/** The constructor */
DynamicLibrary(const char * pathToLibrary);
/** The destructor */
~DynamicLibrary();
};
}

#endif
5 changes: 3 additions & 2 deletions lib/include/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
#define _POSIX 1
#endif

#ifndef _WIN32
#define InlinePlatformCode
#endif


#ifdef _WIN32
Expand Down Expand Up @@ -135,8 +138,6 @@
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#else
#define InlinePlatformCode
#endif

#ifdef _WIN32
Expand Down
Loading

0 comments on commit b197dac

Please sign in to comment.