icedb  version 0.5.1
Snow particle scattering database API
Namespaces | Functions | Variables
os_functions.cpp File Reference
#include <mutex>
#include <thread>
#include <ctime>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <memory>
#include "../icedb/error.h"
#include "../icedb/error_context.h"
#include "../icedb/misc/os_functions.h"
#include "../icedb/misc/os_functions.hpp"
#include "../icedb/util.h"
Include dependency graph for os_functions.cpp:

Go to the source code of this file.

Namespaces

 icedb
 
 icedb::os_functions
 
 icedb::os_functions::vars
 
 icedb::os_functions::win
 
 icedb::os_functions::unix
 

Functions

bool icedb::os_functions::populateOSstrings ()
 
bool ICEDB_pidExists (int pid, bool &res)
 
int ICEDB_getPID ()
 
int ICEDB_getPPID (int pid)
 
bool ICEDB_waitOnExitGetDefault ()
 
void ICEDB_waitOnExitSet (bool val)
 
bool ICEDB_waitOnExitGet ()
 
const char * ICEDB_getUserName ()
 
const char * ICEDB_getHostName ()
 
const char * ICEDB_getAppConfigDir ()
 
const char * ICEDB_getHomeDir ()
 
void ICEDB_free_enumModulesRes (ICEDB_enumModulesRes *p)
 
ICEDB_enumModulesResICEDB_enumModules (int pid)
 
char * ICEDB_findModuleByFunc (void *ptr, size_t sz, char *res)
 
void ICEDB_getLibDirI ()
 
char * ICEDB_getLibDir (size_t sz, char *res)
 
const char * ICEDB_getLibDirC ()
 
void ICEDB_getAppDirI ()
 
char * ICEDB_getAppDir (size_t sz, char *res)
 
const char * ICEDB_getAppDirC ()
 
void ICEDB_getCWDI ()
 
char * ICEDB_getCWD (size_t ssz, char *res)
 
const char * ICEDB_getCWDC ()
 
char * ICEDB_getLibPath (size_t sz, char *res)
 
const char * ICEDB_getLibPathC ()
 
char * ICEDB_getAppPath (size_t sz, char *res)
 
const char * ICEDB_getAppPathC ()
 
void ICEDB_getPluginDirI ()
 
char * ICEDB_getPluginDir (size_t sz, char *res)
 
const char * ICEDB_getPluginDirC ()
 
void ICEDB_libEntry (int, char **)
 Entry function that gets called when a debugged application first loads. More...
 
void ICEDB_libExit ()
 
void ICEDB_writeDebugString (const char *c)
 
bool icedb::os_functions::pidExists (int pid)
 
int icedb::os_functions::getPID ()
 
int icedb::os_functions::getPPID (int pid)
 
void icedb::os_functions::libEntry (int argc, char **argv)
 
void icedb::os_functions::waitOnExit (bool val)
 
bool icedb::os_functions::waitOnExit ()
 
const char * icedb::os_functions::getUserName ()
 
const char * icedb::os_functions::getHostName ()
 
const char * icedb::os_functions::getAppConfigDir ()
 
const char * icedb::os_functions::getHomeDir ()
 
const char * icedb::os_functions::getLibDir ()
 
const char * icedb::os_functions::getAppDir ()
 
const char * icedb::os_functions::getPluginDir ()
 
const char * icedb::os_functions::getLibPath ()
 
const char * icedb::os_functions::getAppPath ()
 
const char * icedb::os_functions::getCWD ()
 

Variables

std::mutex icedb::os_functions::vars::m_sys_names
 
std::string icedb::os_functions::vars::hostname
 
std::string icedb::os_functions::vars::username
 
std::string icedb::os_functions::vars::homeDir
 
std::string icedb::os_functions::vars::appConfigDir
 
std::string icedb::os_functions::vars::moduleCallbackBuffer
 
std::string icedb::os_functions::vars::libDir
 
std::string icedb::os_functions::vars::libPath
 
std::string icedb::os_functions::vars::appDir
 
std::string icedb::os_functions::vars::appPath
 
std::string icedb::os_functions::vars::CWD
 
std::string icedb::os_functions::vars::pluginDir
 
bool icedb::os_functions::vars::_consoleTerminated = false
 
std::vector< std::pair< std::string, std::string > > icedb::os_functions::vars::loadedModulesList
 
bool icedb::os_functions::vars::doWaitOnExit = false
 Private flag that determines if the app waits for the user to press 'Enter' to terminate it at the end of execution. More...
 
bool icedb::os_functions::vars::doWaitOnExitQueriedDefault = false
 
std::map< std::string, std::string > icedb::os_functions::vars::mmods
 

Function Documentation

◆ ICEDB_enumModules()

ICEDB_enumModulesRes* ICEDB_enumModules ( int  pid)

Definition at line 607 of file os_functions.cpp.

References icedb::free(), icedb::os_functions::getPID(), ICEDB_COMPAT_strdup_s(), ICEDB_error_context_add_string2, ICEDB_error_context_create, ICEDB_error_getOSname, ICEDB_ERRORCODES_OS, ICEDB_malloc(), icedb::os_functions::vars::mmods, icedb::os_functions::vars::moduleCallbackBuffer, ICEDB_enumModulesRes::modules, and ICEDB_enumModulesRes::sz.

607  {
609 
610 #if defined(_WIN32)
611  HANDLE h = NULL, snapshot = NULL;
612  h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
613  std::shared_ptr<MODULEENTRY32> mod(new MODULEENTRY32);
614  if (!h || h == INVALID_HANDLE_VALUE) goto err;
615  snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid);
616  if (!snapshot || snapshot == INVALID_HANDLE_VALUE) goto err;
617  mod->dwSize = sizeof(MODULEENTRY32); // Annoying requirement
618  if (!Module32First(snapshot, mod.get())) goto err;
619  do {
620  std::string modName = icedb::os_functions::win::convertStr(mod->szModule);
621  std::string modPath = icedb::os_functions::win::convertStr(mod->szExePath);
622  icedb::os_functions::vars::mmods[modName] = modPath;
623  } while (Module32Next(snapshot, mod.get()));
624 
625  goto done;
626  err:
628  const char* cosname = ICEDB_error_getOSname();
629  ICEDB_error_context_add_string2(ctxerr, "OS_id", cosname);
630  if (!h || h == INVALID_HANDLE_VALUE)
631  ICEDB_error_context_add_string2(ctxerr, "Description", "Error in getting handle for process!");
632  else if (!snapshot || snapshot == INVALID_HANDLE_VALUE)
633  ICEDB_error_context_add_string2(ctxerr, "Description", "Cannot get handle snapshot!");
634  else ICEDB_error_context_add_string2(ctxerr, "Description", "Cannot list first modle!");
635  done:
636 
637  if (snapshot && snapshot != INVALID_HANDLE_VALUE) CloseHandle(snapshot);
638  if (h && h != INVALID_HANDLE_VALUE) CloseHandle(h);
639 #elif defined(__linux__) || defined(__unix__)
640  if (pid != icedb::os_functions::getPID()) {
642  const char* cosname = ICEDB_error_getOSname();
643  ICEDB_error_context_add_string2(ctxerr, "OS_id", cosname);
644  ICEDB_error_context_add_string2(ctxerr, "Description", "This function only works for the pid of the main process.");
645  return NULL;
646  }
647  if (!moduleCallbackBuffer.size()) {
648  dl_iterate_phdr(icedb::os_functions::unix::moduleCallback, NULL);
649  }
650 #elif defined(__APPLE__)
651  uint32_t count = _dyld_image_count();
652  for (uint32_t i=0; i<count; ++i) {
653  std::string modName(_dyld_get_image_name(i));
654  char cmodPath[2048];
655  char *ccmodPath = realpath(modName.c_str(), NULL);
656  if (ccmodPath != NULL) {
657  strncpy(cmodPath,ccmodPath,2048);
658  free(ccmodPath);
659  }
660  std::string modPath(ccmodPath);
661  icedb::os_functions::vars::mmods[modPath] = modPath;
662  }
663 #else
664 #endif
665  // Convert the map to the resultant structure.
667  char **nmods = (char**)ICEDB_malloc(p->sz*sizeof (char**));
668  size_t i = 0;
669  for (auto &s : mmods) {
670  nmods[i] = ICEDB_COMPAT_strdup_s(s.second.c_str(), s.second.size());
671  ++i;
672  }
673  p->modules = const_cast<const char**>(nmods);
674  return p;
675 }
std::map< std::string, std::string > mmods
const char ** modules
Definition: os_functions.h:25
std::string moduleCallbackBuffer
void free(T *obj)
Definition: util.hpp:21
DL_ICEDB ICEDB_error_getOSname_f ICEDB_error_getOSname
Definition: error.cpp:210
ICEDB_SYMBOL_SHARED char * ICEDB_COMPAT_strdup_s(const char *src, size_t srcSz)
Definition: util.cpp:42
ICEDB_SYMBOL_SHARED void * ICEDB_malloc(size_t numBytes)
Allocate memory in bytes. Generally this is just malloced, but a custom allocator may be substituted...
Definition: util.cpp:90
Error returned by an os subsystem (general)
Definition: errorCodes.h:17
#define ICEDB_error_context_add_string2
Definition: error_context.h:63
#define ICEDB_error_context_create(x)
Definition: error_context.h:35
Here is the call graph for this function:

◆ ICEDB_findModuleByFunc()

char* ICEDB_findModuleByFunc ( void *  ptr,
size_t  sz,
char *  res 
)

Find the file that contains the symbol.

Parameters
ptris the symbol. Can be any pointer.
szis the size of the output buffer (bytes).
resis the output buffer, of size at least sz. Gets null terminated.
Returns
res for convenience

Definition at line 677 of file os_functions.cpp.

References ICEDB_COMPAT_strncpy_s(), and ICEDB_DEBUG_RAISE_EXCEPTION.

677  {
684  std::string modpath;
685 #if defined(_WIN32)
686  BOOL success = false;
687  if (ptr)
688  {
689  DWORD flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS;
690  LPCTSTR lpModuleName = (LPCTSTR)ptr;
691  HMODULE mod;
692  success = GetModuleHandleEx(flags, lpModuleName, &mod);
693  if (!success) return nullptr;
694  modpath = icedb::os_functions::win::GetModulePath(mod);
695  FreeLibrary(mod);
696  } else modpath = icedb::os_functions::win::GetModulePath(NULL);
697 #elif defined(__unix__) || defined(__APPLE__)
698  modpath = icedb::os_functions::unix::GetModulePath(ptr);
699 #else
701 #endif
702  ICEDB_COMPAT_strncpy_s(res, sz, modpath.c_str(), modpath.size());
703  return res;
704 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
ICEDB_BEGIN_DECL_C ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_strncpy_s(char *dest, size_t destSz, const char *src, size_t srcSz)
Definition: util.cpp:14
Here is the call graph for this function:

◆ ICEDB_free_enumModulesRes()

void ICEDB_free_enumModulesRes ( ICEDB_enumModulesRes p)

Definition at line 596 of file os_functions.cpp.

References ICEDB_free(), ICEDB_enumModulesRes::modules, and ICEDB_enumModulesRes::sz.

596  {
597  if (!p) return;
598  if (p->sz) {
599  for (int i = 0; i < p->sz; ++i) {
600  ICEDB_free((void*)p->modules[i]);
601  p->modules[i] = nullptr;
602  }
603  }
604  ICEDB_free(p->modules);
605  ICEDB_free(p);
606 }
const char ** modules
Definition: os_functions.h:25
ICEDB_SYMBOL_SHARED void ICEDB_free(void *obj)
Free memory region. Should not be double-freed.
Definition: util.cpp:97
Here is the call graph for this function:

◆ ICEDB_getAppConfigDir()

const char* ICEDB_getAppConfigDir ( )

Definition at line 587 of file os_functions.cpp.

References icedb::os_functions::vars::appConfigDir, and icedb::os_functions::populateOSstrings().

Referenced by icedb::os_functions::getAppConfigDir().

587  {
589  return appConfigDir.c_str();
590 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ICEDB_getAppDir()

char* ICEDB_getAppDir ( size_t  sz,
char *  res 
)

Definition at line 765 of file os_functions.cpp.

References icedb::os_functions::vars::appDir, ICEDB_COMPAT_strncpy_s(), and ICEDB_getAppDirI().

765  {
767  ICEDB_COMPAT_strncpy_s(res, sz, appDir.c_str(), appDir.size());
768  return res;
769 }
ICEDB_BEGIN_DECL_C ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_strncpy_s(char *dest, size_t destSz, const char *src, size_t srcSz)
Definition: util.cpp:14
void ICEDB_getAppDirI()
Here is the call graph for this function:

◆ ICEDB_getAppDirC()

const char* ICEDB_getAppDirC ( )

Definition at line 770 of file os_functions.cpp.

References icedb::os_functions::vars::appDir, and ICEDB_getAppDirI().

770  {
772  return appDir.c_str();
773 }
void ICEDB_getAppDirI()
Here is the call graph for this function:

◆ ICEDB_getAppDirI()

void ICEDB_getAppDirI ( )

Definition at line 723 of file os_functions.cpp.

References icedb::os_functions::vars::appDir, icedb::os_functions::vars::appPath, icedb::free(), ICEDB_DEBUG_RAISE_EXCEPTION, and ICEDB_getPID().

Referenced by icedb::os_functions::getAppDir(), icedb::os_functions::getAppPath(), ICEDB_getAppDir(), ICEDB_getAppDirC(), ICEDB_getAppPath(), and ICEDB_getAppPathC().

723  {
724  std::string &appd = appDir;
725 #if defined(_WIN32)
726  DWORD pid = (DWORD)ICEDB_getPID(); // int always fits in DWORD
727  std::string filename;
728  icedb::os_functions::win::getPathWIN32(pid, appPath, filename);
729  appd = appPath.substr(0, appPath.find_last_of("/\\"));
730 #elif defined(__APPLE__)
731  char exePath[PATH_MAX];
732  uint32_t len = sizeof(exePath);
733  if (_NSGetExecutablePath(exePath, &len) != 0) {
734  exePath[0] = '\0'; // buffer too small (!)
735  }
736  else {
737  // resolve symlinks, ., .. if possible
738  char *canonicalPath = realpath(exePath, NULL);
739  if (canonicalPath != NULL) {
740  strncpy(exePath, canonicalPath, len);
741  free(canonicalPath);
742  }
743  }
744  appPath = std::string(exePath);
745  appd = appPath.substr(0, appPath.find_last_of("/\\"));
746 #elif defined(__linux__)
747  char exePath[PATH_MAX];
748  ssize_t len = ::readlink("/proc/self/exe", exePath, sizeof(exePath));
749  if (len == -1 || len == sizeof(exePath)) len = 0;
750  exePath[len] = '\0';
751  appPath = std::string(exePath);
752  appd = appPath.substr(0, appPath.find_last_of("/\\"));
753 #elif defined(__unix__)
754  char exePath[2048];
755  int mib[4]; mib[0] = CTL_KERN; mib[1] = KERN_PROC; mib[2] = KERN_PROC_PATHNAME; mib[3] = -1;
756  size_t len = sizeof(exePath);
757  if (sysctl(mib, 4, exePath, &len, NULL, 0) != 0)
758  exePath[0] = '\0';
759  appPath = std::string(exePath);
760  appd = appPath.substr(0, appPath.find_last_of("/\\"));
761 #else
763 #endif
764 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
void free(T *obj)
Definition: util.hpp:21
int ICEDB_getPID()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ICEDB_getAppPath()

char* ICEDB_getAppPath ( size_t  sz,
char *  res 
)

Definition at line 813 of file os_functions.cpp.

References icedb::os_functions::vars::appPath, ICEDB_COMPAT_strncpy_s(), and ICEDB_getAppDirI().

813  {
815  ICEDB_COMPAT_strncpy_s(res, sz, appPath.c_str(), appPath.size());
816  return res;
817 }
ICEDB_BEGIN_DECL_C ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_strncpy_s(char *dest, size_t destSz, const char *src, size_t srcSz)
Definition: util.cpp:14
void ICEDB_getAppDirI()
Here is the call graph for this function:

◆ ICEDB_getAppPathC()

const char* ICEDB_getAppPathC ( )

Definition at line 818 of file os_functions.cpp.

References icedb::os_functions::vars::appPath, and ICEDB_getAppDirI().

818  {
819  ICEDB_getAppDirI();
820  return appPath.c_str();
821 }
void ICEDB_getAppDirI()
Here is the call graph for this function:

◆ ICEDB_getCWD()

char* ICEDB_getCWD ( size_t  ssz,
char *  res 
)

Definition at line 794 of file os_functions.cpp.

References icedb::os_functions::vars::CWD, ICEDB_COMPAT_strncpy_s(), and ICEDB_getCWDI().

794  {
795  ICEDB_getCWDI();
796  ICEDB_COMPAT_strncpy_s(res, ssz, CWD.c_str(), CWD.size());
797  return res;
798 }
ICEDB_BEGIN_DECL_C ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_strncpy_s(char *dest, size_t destSz, const char *src, size_t srcSz)
Definition: util.cpp:14
void ICEDB_getCWDI()
Here is the call graph for this function:

◆ ICEDB_getCWDC()

const char* ICEDB_getCWDC ( )

Definition at line 799 of file os_functions.cpp.

References icedb::os_functions::vars::CWD, and ICEDB_getCWDI().

799  {
800  ICEDB_getCWDI();
801  return CWD.c_str();
802 }
void ICEDB_getCWDI()
Here is the call graph for this function:

◆ ICEDB_getCWDI()

void ICEDB_getCWDI ( )

Definition at line 774 of file os_functions.cpp.

References icedb::os_functions::vars::CWD, ICEDB_DEBUG_RAISE_EXCEPTION, ICEDB_error_context_create, and ICEDB_ERRORCODES_OS.

Referenced by icedb::os_functions::getCWD(), ICEDB_getCWD(), and ICEDB_getCWDC().

774  {
775  std::string &cwd = CWD;
776 #if defined(_WIN32)
777  DWORD sz = GetCurrentDirectory(0, NULL);
778  LPTSTR cd = new TCHAR[sz];
779  DWORD result = GetCurrentDirectory(2500, cd);
780  cwd = std::string(cd);
781  delete[] cd;
782 #else
783  char ccwd[4096];
784  char* res = getcwd(ccwd, 4096);
785  if (!res) {
788 
789  return ;
790  }
791  cwd = std::string(ccwd);
792 #endif
793 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
Error returned by an os subsystem (general)
Definition: errorCodes.h:17
#define ICEDB_error_context_create(x)
Definition: error_context.h:35
Here is the caller graph for this function:

◆ ICEDB_getHomeDir()

const char* ICEDB_getHomeDir ( )

Definition at line 591 of file os_functions.cpp.

References icedb::os_functions::vars::homeDir, and icedb::os_functions::populateOSstrings().

Referenced by icedb::os_functions::getHomeDir().

591  {
593  return homeDir.c_str();
594 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ICEDB_getHostName()

const char* ICEDB_getHostName ( )

Definition at line 583 of file os_functions.cpp.

References icedb::os_functions::vars::hostname, and icedb::os_functions::populateOSstrings().

Referenced by icedb::os_functions::getHostName().

583  {
585  return hostname.c_str();
586 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ICEDB_getLibDir()

char* ICEDB_getLibDir ( size_t  sz,
char *  res 
)

Definition at line 714 of file os_functions.cpp.

References ICEDB_COMPAT_strncpy_s(), ICEDB_getLibDirI(), and icedb::os_functions::vars::libDir.

714  {
716  ICEDB_COMPAT_strncpy_s(res, sz, libDir.c_str(), libDir.size());
717  return res;
718 }
void ICEDB_getLibDirI()
ICEDB_BEGIN_DECL_C ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_strncpy_s(char *dest, size_t destSz, const char *src, size_t srcSz)
Definition: util.cpp:14
Here is the call graph for this function:

◆ ICEDB_getLibDirC()

const char* ICEDB_getLibDirC ( )

Definition at line 719 of file os_functions.cpp.

References ICEDB_getLibDirI(), and icedb::os_functions::vars::libDir.

719  {
721  return libDir.c_str();
722 }
void ICEDB_getLibDirI()
Here is the call graph for this function:

◆ ICEDB_getLibDirI()

void ICEDB_getLibDirI ( )

Definition at line 706 of file os_functions.cpp.

References icedb::os_functions::vars::libDir, and icedb::os_functions::vars::libPath.

Referenced by icedb::os_functions::getLibDir(), icedb::os_functions::getLibPath(), ICEDB_getLibDir(), ICEDB_getLibDirC(), ICEDB_getLibPath(), ICEDB_getLibPathC(), and ICEDB_getPluginDirI().

706  {
707 #if defined(_WIN32)
708  libPath = icedb::os_functions::win::GetModulePath(NULL);
709 #elif defined(__unix__) || defined(__APPLE__)
710  libPath = icedb::os_functions::unix::GetModulePath((void*)ICEDB_getLibDirI);
711 #endif
712  libDir = libPath.substr(0, libPath.find_last_of("/\\"));
713 }
void ICEDB_getLibDirI()
Here is the caller graph for this function:

◆ ICEDB_getLibPath()

char* ICEDB_getLibPath ( size_t  sz,
char *  res 
)

Definition at line 804 of file os_functions.cpp.

References ICEDB_COMPAT_strncpy_s(), ICEDB_getLibDirI(), and icedb::os_functions::vars::libPath.

804  {
806  ICEDB_COMPAT_strncpy_s(res, sz, libPath.c_str(), libPath.size());
807  return res;
808 }
void ICEDB_getLibDirI()
ICEDB_BEGIN_DECL_C ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_strncpy_s(char *dest, size_t destSz, const char *src, size_t srcSz)
Definition: util.cpp:14
Here is the call graph for this function:

◆ ICEDB_getLibPathC()

const char* ICEDB_getLibPathC ( )

Definition at line 809 of file os_functions.cpp.

References ICEDB_getLibDirI(), and icedb::os_functions::vars::libPath.

809  {
811  return libPath.c_str();
812 }
void ICEDB_getLibDirI()
Here is the call graph for this function:

◆ ICEDB_getPID()

int ICEDB_getPID ( )

Definition at line 481 of file os_functions.cpp.

References ICEDB_error_context_add_string2, ICEDB_error_context_create, ICEDB_error_getOSname, and ICEDB_ERRORCODES_UNIMPLEMENTED.

Referenced by icedb::os_functions::getPID(), and ICEDB_getAppDirI().

481  {
482 #if defined(__unix__)
483  return (int)getpid();
484 #elif defined(ICEDB_OS_WINDOWS)
485  DWORD pid = 0;
487  HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
488  if (NULL == h) return false;
489  PROCESSENTRY32 pe = { 0 };
490  pe.dwSize = sizeof(PROCESSENTRY32);
491  pid = GetCurrentProcessId();
492  CloseHandle(h);
493  return (int)pid;
494 #endif
496  const char* osname = ICEDB_error_getOSname();
497  ICEDB_error_context_add_string2(err, "OS_id", osname);
498  return -1;
499 }
Unimplemented function path.
Definition: errorCodes.h:18
DL_ICEDB ICEDB_error_getOSname_f ICEDB_error_getOSname
Definition: error.cpp:210
#define ICEDB_error_context_add_string2
Definition: error_context.h:63
#define ICEDB_error_context_create(x)
Definition: error_context.h:35
Here is the caller graph for this function:

◆ ICEDB_getPluginDir()

char* ICEDB_getPluginDir ( size_t  sz,
char *  res 
)

Definition at line 827 of file os_functions.cpp.

References ICEDB_COMPAT_strncpy_s(), ICEDB_getPluginDirI(), and icedb::os_functions::vars::pluginDir.

827  {
829  ICEDB_COMPAT_strncpy_s(res, sz, pluginDir.c_str(), pluginDir.size());
830  return res;
831 }
void ICEDB_getPluginDirI()
ICEDB_BEGIN_DECL_C ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_strncpy_s(char *dest, size_t destSz, const char *src, size_t srcSz)
Definition: util.cpp:14
Here is the call graph for this function:

◆ ICEDB_getPluginDirC()

const char* ICEDB_getPluginDirC ( )

Definition at line 832 of file os_functions.cpp.

References ICEDB_getPluginDirI(), and icedb::os_functions::vars::pluginDir.

832  {
834  return pluginDir.c_str();
835 }
void ICEDB_getPluginDirI()
Here is the call graph for this function:

◆ ICEDB_getPluginDirI()

void ICEDB_getPluginDirI ( )

Definition at line 823 of file os_functions.cpp.

References ICEDB_getLibDirI(), icedb::os_functions::vars::libDir, and icedb::os_functions::vars::pluginDir.

Referenced by icedb::os_functions::getPluginDir(), ICEDB_getPluginDir(), and ICEDB_getPluginDirC().

823  {
825  pluginDir = libDir + "/icedb-plugins";
826 }
void ICEDB_getLibDirI()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ICEDB_getPPID()

int ICEDB_getPPID ( int  pid)

Definition at line 501 of file os_functions.cpp.

References ICEDB_error_context_add_string2, ICEDB_error_context_create, ICEDB_error_getOSname, and ICEDB_ERRORCODES_UNIMPLEMENTED.

Referenced by icedb::os_functions::getPPID().

501  {
502 #if defined(__unix__)
503  return (int)getppid();
504 #elif defined(ICEDB_OS_WINDOWS)
505  DWORD Dpid = pid, ppid = 0;
506  HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
507  if (NULL == h) return false;
508  PROCESSENTRY32 pe = { 0 };
509  pe.dwSize = sizeof(PROCESSENTRY32);
510  if (!pid) Dpid = GetCurrentProcessId();
511  if (Process32First(h, &pe)) {
512  do {
513  if (pe.th32ProcessID == Dpid) {
514  ppid = pe.th32ParentProcessID;
515  //printf("PID: %i; PPID: %i\n", pid, pe.th32ParentProcessID);
516  }
517  } while (Process32Next(h, &pe));
518  }
519 
520  //std::cout << "Pid " << pid << "\nPPID " << ppid << std::endl;
521  CloseHandle(h);
522  return (int)ppid;
523 #endif
525  const char* osname = ICEDB_error_getOSname();
526  ICEDB_error_context_add_string2(err, "OS_id", osname);
527  return -1;
528 }
Unimplemented function path.
Definition: errorCodes.h:18
DL_ICEDB ICEDB_error_getOSname_f ICEDB_error_getOSname
Definition: error.cpp:210
#define ICEDB_error_context_add_string2
Definition: error_context.h:63
#define ICEDB_error_context_create(x)
Definition: error_context.h:35
Here is the caller graph for this function:

◆ ICEDB_getUserName()

const char* ICEDB_getUserName ( )

Definition at line 579 of file os_functions.cpp.

References icedb::os_functions::populateOSstrings(), and icedb::os_functions::vars::username.

Referenced by icedb::os_functions::getUserName().

579  {
581  return username.c_str();
582 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ICEDB_libEntry()

void ICEDB_libEntry ( int  ,
char **   
)

Entry function that gets called when a debugged application first loads.

This function gets called at the beginning of an application's execution (generally). It:

  • determines if the app should wait on exit (to keep the console open)
  • resets the console title in case any other library overrides it. A good example of this is the CERN ROOT image lobraries.
  • Overrides the console control key handlers on Windows. This lets a user exit with CTRL-C without the debug code causing the app to crash.

Definition at line 848 of file os_functions.cpp.

References ICEDB_libExit().

Referenced by icedb::os_functions::libEntry().

848  {
849 #ifdef _WIN32
850  // Get PID
851  DWORD pid = 0;
852  HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
853  if (NULL == h) return;
854  PROCESSENTRY32 pe = { 0 };
855  pe.dwSize = sizeof(PROCESSENTRY32);
856  pid = GetCurrentProcessId();
857  CloseHandle(h);
858 
859  // Get parent process name
860  h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION
861  //| PROCESS_VM_READ
862  , FALSE, pid);
863  if (NULL == h) return;
864  TCHAR szModName[600];
865  DWORD success = 0;
866  DWORD sz = sizeof(szModName) / sizeof(TCHAR);
867  success = QueryFullProcessImageName(h, 0, szModName, &sz);
868 
869  // Set Console Title
870  SetConsoleTitle(szModName);
871 
872 
873  // Also, set the window closing routine
874  // This allows for the user to click the X (or press ctrl-c)
875  // without causing a fault.
876  // The fault is because the window closes before the atexit
877  // functions can write output.
878  SetConsoleCtrlHandler(icedb::os_functions::win::_CloseHandlerRoutine, true);
879  CloseHandle(h);
880 #endif
881 
882 
883  // Set appexit
884  atexit(ICEDB_libExit);
885  // Static registration of i/o objects
886 
887  // Load the appropriate filesystem plugin
888  //std::string pDir(ICEDB_getPluginDirC());
889  //const size_t mangledNameSz = 200;
890  //char mangledName[mangledNameSz] = "";
891 #if defined(_WIN32)
892  //ICEDB_dll_name_mangle_simple("fs-win", mangledName, mangledNameSz);
893 #else
894  //ICEDB_dll_name_mangle_simple("fs-posix", mangledName, mangledNameSz);
895 #endif
896  //std::string testP = pDir + "/" + std::string(mangledName);
897  //ICEDB_load_plugin(testP.c_str());
898 
899 
900  // Query the filesystem to load the remaining plugins
901 
902 }
void ICEDB_libExit()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ICEDB_libExit()

void ICEDB_libExit ( )

Definition at line 904 of file os_functions.cpp.

References icedb::os_functions::vars::_consoleTerminated, and ICEDB_waitOnExitGet().

Referenced by ICEDB_libEntry().

904  {
905  using namespace std;
906  if (_consoleTerminated) return;
907  if (ICEDB_waitOnExitGet())
908  {
909  cerr << endl << "Program terminated. Press return to exit." << endl;
910  //std::getchar();
911  // Ignore to the end of file
912 //#ifdef max
913 //#undef max
914 //#endif
915  cin.clear();
916  //cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
917  std::string ln;
918  std::getline(cin, ln);
919  }
920 }
bool ICEDB_waitOnExitGet()
STL namespace.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ICEDB_pidExists()

bool ICEDB_pidExists ( int  pid,
bool &  res 
)

Definition at line 401 of file os_functions.cpp.

References ICEDB_error_context_add_string2, ICEDB_error_context_create, ICEDB_error_getOSname, ICEDB_ERRORCODES_OS, and ICEDB_ERRORCODES_UNIMPLEMENTED.

Referenced by icedb::os_functions::pidExists().

402 {
403  // Function needed because Qt is insufficient, and Windows / Unix have
404  // different methods of ascertaining this.
405 #if defined(ICEDB_OS_WINDOWS)
406  HANDLE h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
407  if (h)
408  {
409  DWORD code = 0;
410  if (GetExitCodeProcess(h, &code))
411  {
412  CloseHandle(h);
413  if (code == STILL_ACTIVE)
414  {
415  res = true;
416  return true;
417  }
418  else {
419  res = false;
420  return true;
421  }
422  }
423  else {
424  CloseHandle(h);
425  res = false;
426  return true;
427  }
428  CloseHandle(h);
429  res = true;
430  return true;
431  }
432  return false;
433 #elif defined(ICEDB_OS_LINUX)
434  // Need to check existence of directory /proc/pid
435  std::ostringstream pname;
436  pname << "/proc/" << pid;
437  if (icedb::os_functions::unix::dirExists(pname.str().c_str())) {
438  res = true;
439  return true;
440  }
441  res = false;
442  return true;
443 #elif defined(__FreeBSD__) || defined (__APPLE__)
444  // Works for both freebsd and mac os
445  res = false;
446  int mib[4];
447  size_t len;
448  struct kinfo_proc kp;
449 
450  /* Fill out the first three components of the mib */
451  len = 4;
452  sysctlnametomib("kern.proc.pid", mib, &len);
453  mib[3] = pid;
454  len = sizeof(kp);
455  int sres = sysctl(mib, 4, &kp, &len, NULL, 0);
456  if (sres == -1) {
457  // Either the pid does not exist, or some other error
458  if (errno == ENOENT) {
459  res = false; return true;
460  }
462  const char* osname = ICEDB_error_getOSname();
463  ICEDB_error_context_add_string2(err, "OS_id", osname);
464  const int buflen = 200;
465  char strerrbuf[buflen] = "\0";
466  snprintf(strerrbuf, buflen, "%d", errno);
467  ICEDB_error_context_add_string2(err, "errno", strerrbuf);
468 
469  // strerror_r will always yield a null-terminated string.
470  //int ebufres = strerror_r(err, strerrbuf, buflen);
471  }
472  else if ((sres == 0) && (len > 0)) res = true;
473  return true;
474 #endif
476  const char* osname = ICEDB_error_getOSname();
477  ICEDB_error_context_add_string2(err, "OS_id", osname);
478  return false;
479 }
Unimplemented function path.
Definition: errorCodes.h:18
DL_ICEDB ICEDB_error_getOSname_f ICEDB_error_getOSname
Definition: error.cpp:210
Error returned by an os subsystem (general)
Definition: errorCodes.h:17
#define ICEDB_error_context_add_string2
Definition: error_context.h:63
#define ICEDB_error_context_create(x)
Definition: error_context.h:35
Here is the caller graph for this function:

◆ ICEDB_waitOnExitGet()

bool ICEDB_waitOnExitGet ( )

Definition at line 570 of file os_functions.cpp.

References icedb::os_functions::vars::doWaitOnExit, icedb::os_functions::vars::doWaitOnExitQueriedDefault, ICEDB_waitOnExitGetDefault(), and icedb::os_functions::vars::m_sys_names.

Referenced by ICEDB_libExit(), and icedb::os_functions::waitOnExit().

570  {
571  std::lock_guard<std::mutex> lock(m_sys_names);
575  }
576  return doWaitOnExit;
577 }
bool doWaitOnExit
Private flag that determines if the app waits for the user to press &#39;Enter&#39; to terminate it at the en...
bool ICEDB_waitOnExitGetDefault()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ICEDB_waitOnExitGetDefault()

bool ICEDB_waitOnExitGetDefault ( )

Definition at line 530 of file os_functions.cpp.

Referenced by ICEDB_waitOnExitGet().

530  {
531 #if defined(ICEDB_OS_WINDOWS)
532  // Get pid and parent pid. Duplicated getPID and getPPID, but combines the calls.
533  DWORD pid = 0, ppid = 0;
534  HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
535  if (NULL == h) return false;
536  PROCESSENTRY32 pe = { 0 };
537  pe.dwSize = sizeof(PROCESSENTRY32);
538  pid = GetCurrentProcessId();
539  if (Process32First(h, &pe)) {
540  do {
541  if (pe.th32ProcessID == pid) {
542  ppid = pe.th32ParentProcessID;
543  }
544  } while (Process32Next(h, &pe));
545  }
546  CloseHandle(h);
547 
548  // Get parent process name
549  std::string filepath, filename;
550  icedb::os_functions::win::getPathWIN32(ppid, filepath, filename);
551 
552  //std::cout << filename.string() << std::endl;
553  // If run from cmd, no need to wait
554  if (filename == "cmd.exe") return false;
555  // Cygwin
556  if (filename == "bash.exe") return false;
557  if (filename == "tcsh.exe") return false;
558  // Don't need these due to end return. Just for reference.
559  //if (filename == "devenv.exe") return true;
560  //if (filename == "explorer.exe") return true;
561  return true;
562 #endif
563  return false;
564 }
Here is the caller graph for this function:

◆ ICEDB_waitOnExitSet()

void ICEDB_waitOnExitSet ( bool  val)

Definition at line 565 of file os_functions.cpp.

References icedb::os_functions::vars::doWaitOnExit, icedb::os_functions::vars::doWaitOnExitQueriedDefault, and icedb::os_functions::vars::m_sys_names.

Referenced by icedb::os_functions::waitOnExit().

565  {
566  std::lock_guard<std::mutex> lock(m_sys_names);
568  doWaitOnExit = val;
569 }
bool doWaitOnExit
Private flag that determines if the app waits for the user to press &#39;Enter&#39; to terminate it at the en...
Here is the caller graph for this function:

◆ ICEDB_writeDebugString()

void ICEDB_writeDebugString ( const char *  c)

Definition at line 922 of file os_functions.cpp.

References ICEDB_COMPAT_fprintf_s.

Referenced by icedb::logging::emit_log().

922  {
923 #ifdef _WIN32
924  OutputDebugStringA(c);
925 #else
926  // Just write to CERR
927  ICEDB_COMPAT_fprintf_s(stderr, "%s", c);
928 #endif
929 }
#define ICEDB_COMPAT_fprintf_s
Definition: util.h:26
Here is the caller graph for this function: