summaryrefslogtreecommitdiff
path: root/src/porting.cpp
diff options
context:
space:
mode:
authorkwolekr <kwolekr@minetest.net>2015-04-18 20:12:14 -0400
committerkwolekr <kwolekr@minetest.net>2015-04-19 16:52:58 -0400
commit386d69533095a6f36d5c3a98f2cd8de20a0132d4 (patch)
tree7f70adbb8079decd34168866c56ac77efe62e24c /src/porting.cpp
parentf1a41e4f77ce272e8e454710a7a168d95466742c (diff)
downloadminetest-386d69533095a6f36d5c3a98f2cd8de20a0132d4.tar.gz
minetest-386d69533095a6f36d5c3a98f2cd8de20a0132d4.tar.bz2
minetest-386d69533095a6f36d5c3a98f2cd8de20a0132d4.zip
Porting: Refactor initalizePaths()
Add support for Solaris and HP-UX Search additional potential procfs locations for current executable
Diffstat (limited to 'src/porting.cpp')
-rw-r--r--src/porting.cpp466
1 files changed, 285 insertions, 181 deletions
diff --git a/src/porting.cpp b/src/porting.cpp
index 812f148da..1834fb7c8 100644
--- a/src/porting.cpp
+++ b/src/porting.cpp
@@ -35,12 +35,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <unistd.h>
#include <sys/utsname.h>
#endif
-
+#if defined(__hpux)
+ #define _PSTAT64
+ #include <sys/pstat.h>
+#endif
#if !defined(_WIN32) && !defined(__APPLE__) && \
!defined(__ANDROID__) && !defined(SERVER)
#define XORG_USED
#endif
-
#ifdef XORG_USED
#include <X11/Xlib.h>
#include <X11/Xutil.h>
@@ -99,39 +101,29 @@ void signal_handler_init(void)
#else // _WIN32
#include <signal.h>
- BOOL WINAPI event_handler(DWORD sig)
- {
- switch(sig)
- {
- case CTRL_C_EVENT:
- case CTRL_CLOSE_EVENT:
- case CTRL_LOGOFF_EVENT:
- case CTRL_SHUTDOWN_EVENT:
-
- if(g_killed == false)
- {
- dstream<<DTIME<<"INFO: event_handler(): "
- <<"Ctrl+C, Close Event, Logoff Event or Shutdown Event, shutting down."<<std::endl;
- // Comment out for less clutter when testing scripts
- /*dstream<<DTIME<<"INFO: event_handler(): "
- <<"Printing debug stacks"<<std::endl;
- debug_stacks_print();*/
-
- g_killed = true;
- }
- else
- {
- (void)signal(SIGINT, SIG_DFL);
- }
-
- break;
- case CTRL_BREAK_EVENT:
- break;
+BOOL WINAPI event_handler(DWORD sig)
+{
+ switch (sig) {
+ case CTRL_C_EVENT:
+ case CTRL_CLOSE_EVENT:
+ case CTRL_LOGOFF_EVENT:
+ case CTRL_SHUTDOWN_EVENT:
+ if (g_killed == false) {
+ dstream << DTIME << "INFO: event_handler(): "
+ << "Ctrl+C, Close Event, Logoff Event or Shutdown Event,"
+ " shutting down." << std::endl;
+ g_killed = true;
+ } else {
+ (void)signal(SIGINT, SIG_DFL);
}
-
- return TRUE;
+ break;
+ case CTRL_BREAK_EVENT:
+ break;
}
+ return TRUE;
+}
+
void signal_handler_init(void)
{
SetConsoleCtrlHandler( (PHANDLER_ROUTINE)event_handler,TRUE);
@@ -143,7 +135,8 @@ void signal_handler_init(void)
/*
Multithreading support
*/
-int getNumberOfProcessors() {
+int getNumberOfProcessors()
+{
#if defined(_SC_NPROCESSORS_ONLN)
return sysconf(_SC_NPROCESSORS_ONLN);
@@ -177,7 +170,8 @@ int getNumberOfProcessors() {
#ifndef __ANDROID__
-bool threadBindToProcessor(threadid_t tid, int pnumber) {
+bool threadBindToProcessor(threadid_t tid, int pnumber)
+{
#if defined(_WIN32)
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, 0, tid);
@@ -201,7 +195,7 @@ bool threadBindToProcessor(threadid_t tid, int pnumber) {
#elif defined(__sun) || defined(sun)
return processor_bind(P_LWPID, MAKE_LWPID_PTHREAD(tid),
- pnumber, NULL) == 0;
+ pnumber, NULL) == 0;
#elif defined(_AIX)
@@ -212,7 +206,7 @@ bool threadBindToProcessor(threadid_t tid, int pnumber) {
pthread_spu_t answer;
return pthread_processor_bind_np(PTHREAD_BIND_ADVISORY_NP,
- &answer, pnumber, tid) == 0;
+ &answer, pnumber, tid) == 0;
#elif defined(__APPLE__)
@@ -221,7 +215,7 @@ bool threadBindToProcessor(threadid_t tid, int pnumber) {
thread_port_t threadport = pthread_mach_thread_np(tid);
tapol.affinity_tag = pnumber + 1;
return thread_policy_set(threadport, THREAD_AFFINITY_POLICY,
- (thread_policy_t)&tapol, THREAD_AFFINITY_POLICY_COUNT) == KERN_SUCCESS;
+ (thread_policy_t)&tapol, THREAD_AFFINITY_POLICY_COUNT) == KERN_SUCCESS;
#else
@@ -231,7 +225,8 @@ bool threadBindToProcessor(threadid_t tid, int pnumber) {
}
#endif
-bool threadSetPriority(threadid_t tid, int prio) {
+bool threadSetPriority(threadid_t tid, int prio)
+{
#if defined(_WIN32)
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, 0, tid);
@@ -286,14 +281,14 @@ void pathRemoveFile(char *path, char delim)
path[i] = 0;
}
-bool detectMSVCBuildDir(char *c_path)
+bool detectMSVCBuildDir(const std::string &path)
{
- std::string path(c_path);
const char *ends[] = {
"bin\\Release",
"bin\\Debug",
"bin\\Build",
- NULL};
+ NULL
+ };
return (removeStringEnd(path, ends) != "");
}
@@ -333,224 +328,322 @@ std::string get_sysinfo()
#endif
}
-void initializePaths()
+
+bool getCurrentWorkingDir(char *buf, size_t len)
{
-#if RUN_IN_PLACE
- /*
- Use relative paths if RUN_IN_PLACE
- */
+#ifdef _WIN32
+ DWORD ret = GetCurrentDirectory(len, buf);
+ return (ret != 0) && (ret <= len);
+#else
+ return getcwd(buf, len);
+#endif
+}
- infostream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
- /*
- Windows
- */
- #if defined(_WIN32)
+bool getExecPathFromProcfs(char *buf, size_t buflen)
+{
+#ifndef _WIN32
+ buflen--;
- const DWORD buflen = 1000;
- char buf[buflen];
- DWORD len;
+ ssize_t len;
+ if ((len = readlink("/proc/self/exe", buf, buflen)) == -1 &&
+ (len = readlink("/proc/curproc/file", buf, buflen)) == -1 &&
+ (len = readlink("/proc/curproc/exe", buf, buflen)) == -1)
+ return false;
- // Find path of executable and set path_share relative to it
- len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
- assert(len < buflen);
- pathRemoveFile(buf, '\\');
+ buf[len] = '\0';
+ return true;
+#else
+ return false;
+#endif
+}
- if(detectMSVCBuildDir(buf)){
- infostream<<"MSVC build directory detected"<<std::endl;
- path_share = std::string(buf) + "\\..\\..";
- path_user = std::string(buf) + "\\..\\..";
- }
- else{
- path_share = std::string(buf) + "\\..";
- path_user = std::string(buf) + "\\..";
- }
+//// Windows
+#if defined(_WIN32)
- /*
- Linux
- */
- #elif defined(linux) || defined(__linux)
+bool getCurrentExecPath(char *buf, size_t len)
+{
+ DWORD written = GetModuleFileNameA(NULL, buf, len);
+ if (written == 0 || written == len)
+ return false;
- char buf[BUFSIZ];
- memset(buf, 0, BUFSIZ);
- // Get path to executable
- FATAL_ERROR_IF(readlink("/proc/self/exe", buf, BUFSIZ-1) == -1, "Failed to get cwd");
+ return true;
+}
- pathRemoveFile(buf, '/');
- path_share = std::string(buf) + "/..";
- path_user = std::string(buf) + "/..";
+//// Linux
+#elif defined(linux) || defined(__linux) || defined(__linux__)
+
+bool getCurrentExecPath(char *buf, size_t len)
+{
+ if (!getExecPathFromProcfs(buf, len))
+ return false;
- /*
- OS X
- */
- #elif defined(__APPLE__)
+ return true;
+}
- CFBundleRef main_bundle = CFBundleGetMainBundle();
- CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
- char path[PATH_MAX];
- if (CFURLGetFileSystemRepresentation(resources_url, TRUE, (UInt8 *)path, PATH_MAX)) {
- path_share = std::string(path);
- path_user = std::string(path) + "/../User";
- } else {
- dstream << "WARNING: Could not determine bundle resource path" << std::endl;
- }
- CFRelease(resources_url);
- /*
- FreeBSD
- */
- #elif defined(__FreeBSD__)
+//// Mac OS X, Darwin
+#elif defined(__APPLE__)
+
+bool getCurrentExecPath(char *buf, size_t len)
+{
+ if (_NSGetExecutablePath(buf, &len) == -1)
+ return false;
+
+ return true;
+}
+
+
+//// FreeBSD, NetBSD, DragonFlyBSD
+#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
+
+bool getCurrentExecPath(char *buf, size_t len)
+{
+ // Try getting path from procfs first, since valgrind
+ // doesn't work with the latter
+ if (getExecPathFromProcfs(buf, len))
+ return true;
int mib[4];
- char buf[BUFSIZ];
- size_t len = sizeof(buf);
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PATHNAME;
mib[3] = -1;
- FATAL_ERROR_IF(sysctl(mib, 4, buf, &len, NULL, 0) == -1, "");
- pathRemoveFile(buf, '/');
+ if (sysctl(mib, 4, buf, &len, NULL, 0) == -1)
+ return false;
- path_share = std::string(buf) + "/..";
- path_user = std::string(buf) + "/..";
+ return true;
+}
- #else
- //TODO: Get path of executable. This assumes working directory is bin/
- dstream<<"WARNING: Relative path not properly supported on this platform"
- <<std::endl;
+//// Solaris
+#elif defined(__sun) || defined(sun)
- /* scriptapi no longer allows paths that start with "..", so assuming that
- the current working directory is bin/, strip off the last component. */
- char *cwd = getcwd(NULL, 0);
- pathRemoveFile(cwd, '/');
- path_share = std::string(cwd);
- path_user = std::string(cwd);
+bool getCurrentExecPath(char *buf, size_t len)
+{
+ const char *exec = getexecname();
+ if (exec == NULL)
+ return false;
- #endif
+ if (strlcpy(buf, exec, len) >= len)
+ return false;
+
+ return true;
+}
-#else // RUN_IN_PLACE
- /*
- Use platform-specific paths otherwise
- */
+// HP-UX
+#elif defined(__hpux)
- infostream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
+bool getCurrentExecPath(char *buf, size_t len)
+{
+ struct pst_status psts;
+
+ if (pstat_getproc(&psts, sizeof(psts), 0, getpid()) == -1)
+ return false;
- /*
- Windows
- */
- #if defined(_WIN32)
+ if (pstat_getpathname(buf, len, &psts.pst_fid_text) == -1)
+ return false;
+
+ return true;
+}
- const DWORD buflen = 1000; // FIXME: Surely there is a better way to do this
- char buf[buflen];
- DWORD len;
+
+#else
+
+bool getCurrentExecPath(char *buf, size_t len)
+{
+ return false;
+}
+
+#endif
+
+
+//// Windows
+#if defined(_WIN32)
+
+bool setSystemPaths()
+{
+ char buf[BUFSIZ];
// Find path of executable and set path_share relative to it
- len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
- FATAL_ERROR_IF(len >= buflen, "Overlow");
+ FATAL_ERROR_IF(!getCurrentExecPath(buf, sizeof(buf)),
+ "Failed to get current executable path");
pathRemoveFile(buf, '\\');
// Use ".\bin\.."
path_share = std::string(buf) + "\\..";
// Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
- len = GetEnvironmentVariable("APPDATA", buf, buflen);
- FATAL_ERROR_IF(len >= buflen, "Overlow");
+ DWORD len = GetEnvironmentVariable("APPDATA", buf, sizeof(buf));
+ FATAL_ERROR_IF(len == 0 || len > sizeof(buf), "Failed to get APPDATA");
+
path_user = std::string(buf) + DIR_DELIM + lowercase(PROJECT_NAME);
+ return true;
+}
- /*
- Linux
- */
- #elif defined(linux) || defined(__linux)
- // Get path to executable
- std::string bindir = "";
- {
- char buf[BUFSIZ];
- memset(buf, 0, BUFSIZ);
- if (readlink("/proc/self/exe", buf, BUFSIZ-1) == -1) {
- errorstream << "Unable to read bindir "<< std::endl;
-#ifndef __ANDROID__
- FATAL_ERROR("Unable to read bindir");
+//// Linux
+#elif defined(linux) || defined(__linux)
+
+bool setSystemPaths()
+{
+ char buf[BUFSIZ];
+
+ if (!getCurrentExecPath(buf, sizeof(buf))) {
+#ifdef __ANDROID__
+ errorstream << "Unable to read bindir "<< std::endl;
+#else
+ FATAL_ERROR("Unable to read bindir");
#endif
- } else {
- pathRemoveFile(buf, '/');
- bindir = buf;
- }
+ return false;
}
+ pathRemoveFile(buf, '/');
+ std::string bindir(buf);
+
// Find share directory from these.
// It is identified by containing the subdirectory "builtin".
std::list<std::string> trylist;
std::string static_sharedir = STATIC_SHAREDIR;
- if(static_sharedir != "" && static_sharedir != ".")
+ if (static_sharedir != "" && static_sharedir != ".")
trylist.push_back(static_sharedir);
- trylist.push_back(
- bindir + DIR_DELIM + ".." + DIR_DELIM + "share" + DIR_DELIM + lowercase(PROJECT_NAME));
- trylist.push_back(bindir + DIR_DELIM + "..");
+
+ trylist.push_back(bindir + DIR_DELIM ".." DIR_DELIM "share"
+ DIR_DELIM + lowercase(PROJECT_NAME));
+ trylist.push_back(bindir + DIR_DELIM "..");
+
#ifdef __ANDROID__
trylist.push_back(path_user);
#endif
- for(std::list<std::string>::const_iterator i = trylist.begin();
- i != trylist.end(); i++)
- {
+ for (std::list<std::string>::const_iterator
+ i = trylist.begin(); i != trylist.end(); i++) {
const std::string &trypath = *i;
- if(!fs::PathExists(trypath) || !fs::PathExists(trypath + DIR_DELIM + "builtin")){
- dstream<<"WARNING: system-wide share not found at \""
- <<trypath<<"\""<<std::endl;
+ if (!fs::PathExists(trypath) ||
+ !fs::PathExists(trypath + DIR_DELIM + "builtin")) {
+ dstream << "WARNING: system-wide share not found at \""
+ << trypath << "\""<< std::endl;
continue;
}
+
// Warn if was not the first alternative
- if(i != trylist.begin()){
- dstream<<"WARNING: system-wide share found at \""
- <<trypath<<"\""<<std::endl;
+ if (i != trylist.begin()) {
+ dstream << "WARNING: system-wide share found at \""
+ << trypath << "\"" << std::endl;
}
+
path_share = trypath;
break;
}
+
#ifndef __ANDROID__
- path_user = std::string(getenv("HOME")) + DIR_DELIM + "." + lowercase(PROJECT_NAME);
+ path_user = std::string(getenv("HOME")) + DIR_DELIM "."
+ + lowercase(PROJECT_NAME);
#endif
- /*
- OS X
- */
- #elif defined(__APPLE__)
+ return true;
+}
+
+
+//// Mac OS X
+#elif defined(__APPLE__)
+bool setSystemPaths()
+{
CFBundleRef main_bundle = CFBundleGetMainBundle();
CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
char path[PATH_MAX];
- if (CFURLGetFileSystemRepresentation(resources_url, TRUE, (UInt8 *)path, PATH_MAX)) {
+ if (CFURLGetFileSystemRepresentation(resources_url,
+ TRUE, (UInt8 *)path, PATH_MAX)) {
path_share = std::string(path);
} else {
dstream << "WARNING: Could not determine bundle resource path" << std::endl;
}
CFRelease(resources_url);
- path_user = std::string(getenv("HOME")) + "/Library/Application Support/" + lowercase(PROJECT_NAME);
+ path_user = std::string(getenv("HOME"))
+ + "/Library/Application Support/"
+ + lowercase(PROJECT_NAME);
+ return true;
+}
+
- #else // FreeBSD, and probably many other POSIX-like systems.
+#else
+bool setSystemPaths()
+{
path_share = STATIC_SHAREDIR;
- path_user = std::string(getenv("HOME")) + DIR_DELIM + "." + lowercase(PROJECT_NAME);
+ path_user = std::string(getenv("HOME")) + DIR_DELIM "."
+ + lowercase(PROJECT_NAME);
+ return true;
+}
- #endif
-#endif // RUN_IN_PLACE
-}
+#endif
-static irr::IrrlichtDevice *device;
-void initIrrlicht(irr::IrrlichtDevice *device_)
+void initializePaths()
{
- device = device_;
+#if RUN_IN_PLACE
+ char buf[BUFSIZ];
+
+ infostream << "Using relative paths (RUN_IN_PLACE)" << std::endl;
+
+ bool success =
+ getCurrentExecPath(buf, sizeof(buf)) ||
+ getExecPathFromProcfs(buf, sizeof(buf));
+
+ if (success) {
+ pathRemoveFile(buf, '/');
+ std::string execpath(buf);
+
+ path_share = execpath + DIR_DELIM "..";
+ path_user = execpath + DIR_DELIM "..";
+
+ if (detectMSVCBuildDir(execpath)) {
+ path_share += DIR_DELIM "..";
+ path_user += DIR_DELIM "..";
+ }
+ } else {
+ errorstream << "Failed to get paths by executable location, "
+ "trying cwd" << std::endl;
+
+ if (!getCurrentWorkingDir(buf, sizeof(buf)))
+ FATAL_ERROR("Ran out of methods to get paths");
+
+ size_t cwdlen = strlen(buf);
+ if (cwdlen >= 1 && buf[cwdlen - 1] == DIR_DELIM_CHAR) {
+ cwdlen--;
+ buf[cwdlen] = '\0';
+ }
+
+ if (cwdlen >= 4 && !strcmp(buf + cwdlen - 4, DIR_DELIM "bin"))
+ pathRemoveFile(buf, DIR_DELIM_CHAR);
+
+ std::string execpath(buf);
+
+ path_share = execpath;
+ path_user = execpath;
+ }
+
+#else
+ infostream << "Using system-wide paths (NOT RUN_IN_PLACE)" << std::endl;
+
+ if (!setSystemPaths())
+ errorstream << "Failed to get one or more system-wide path" << std::endl;
+
+#endif
+
+ infostream << "Detected share path: " << path_share << std::endl;
+ infostream << "Detected user path: " << path_user << std::endl;
}
+
+
void setXorgClassHint(const video::SExposedVideoData &video_data,
const std::string &name)
{
@@ -568,8 +661,20 @@ void setXorgClassHint(const video::SExposedVideoData &video_data,
#endif
}
+
+////
+//// Video/Display Information (Client-only)
+////
+
#ifndef SERVER
+static irr::IrrlichtDevice *device;
+
+void initIrrlicht(irr::IrrlichtDevice *device_)
+{
+ device = device_;
+}
+
v2u32 getWindowSize()
{
return device->getVideoDriver()->getScreenSize();
@@ -641,9 +746,8 @@ const char *getVideoDriverFriendlyName(irr::video::E_DRIVER_TYPE type)
return driver_names[type];
}
-
-#ifndef __ANDROID__
-#ifdef XORG_USED
+# ifndef __ANDROID__
+# ifdef XORG_USED
static float calcDisplayDensity()
{
@@ -679,12 +783,12 @@ float getDisplayDensity()
}
-#else
+# else // XORG_USED
float getDisplayDensity()
{
return g_settings->getFloat("screen_dpi")/96.0;
}
-#endif
+# endif // XORG_USED
v2u32 getDisplaySize()
{
@@ -695,8 +799,8 @@ v2u32 getDisplaySize()
return deskres;
}
-#endif
-#endif
+# endif // __ANDROID__
+#endif // SERVER
} //namespace porting