PageRenderTime 189ms CodeModel.GetById 177ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llvfs/lldir_solaris.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 290 lines | 210 code | 44 blank | 36 comment | 48 complexity | d00309b5788c2f774f3f6d8b154bb5d6 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file fmodwrapper.cpp
  3. * @brief dummy source file for building a shared library to wrap libfmod.a
  4. *
  5. * $LicenseInfo:firstyear=2005&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "lldir_solaris.h"
  28. #include "llerror.h"
  29. #include "llrand.h"
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <unistd.h>
  33. #include <glob.h>
  34. #include <pwd.h>
  35. #include <sys/utsname.h>
  36. #define _STRUCTURED_PROC 1
  37. #include <sys/procfs.h>
  38. #include <fcntl.h>
  39. static std::string getCurrentUserHome(char* fallback)
  40. {
  41. const uid_t uid = getuid();
  42. struct passwd *pw;
  43. char *result_cstr = fallback;
  44. pw = getpwuid(uid);
  45. if ((pw != NULL) && (pw->pw_dir != NULL))
  46. {
  47. result_cstr = (char*) pw->pw_dir;
  48. }
  49. else
  50. {
  51. llinfos << "Couldn't detect home directory from passwd - trying $HOME" << llendl;
  52. const char *const home_env = getenv("HOME"); /* Flawfinder: ignore */
  53. if (home_env)
  54. {
  55. result_cstr = (char*) home_env;
  56. }
  57. else
  58. {
  59. llwarns << "Couldn't detect home directory! Falling back to " << fallback << llendl;
  60. }
  61. }
  62. return std::string(result_cstr);
  63. }
  64. LLDir_Solaris::LLDir_Solaris()
  65. {
  66. mDirDelimiter = "/";
  67. mCurrentDirIndex = -1;
  68. mCurrentDirCount = -1;
  69. mDirp = NULL;
  70. char tmp_str[LL_MAX_PATH]; /* Flawfinder: ignore */
  71. if (getcwd(tmp_str, LL_MAX_PATH) == NULL)
  72. {
  73. strcpy(tmp_str, "/tmp");
  74. llwarns << "Could not get current directory; changing to "
  75. << tmp_str << llendl;
  76. if (chdir(tmp_str) == -1)
  77. {
  78. llerrs << "Could not change directory to " << tmp_str << llendl;
  79. }
  80. }
  81. mExecutableFilename = "";
  82. mExecutablePathAndName = "";
  83. mExecutableDir = strdup(tmp_str);
  84. mWorkingDir = strdup(tmp_str);
  85. mAppRODataDir = strdup(tmp_str);
  86. mOSUserDir = getCurrentUserHome(tmp_str);
  87. mOSUserAppDir = "";
  88. mLindenUserDir = "";
  89. char path [LL_MAX_PATH]; /* Flawfinder: ignore */
  90. sprintf(path, "/proc/%d/psinfo", (int)getpid());
  91. int proc_fd = -1;
  92. if((proc_fd = open(path, O_RDONLY)) == -1){
  93. llwarns << "unable to open " << path << llendl;
  94. return;
  95. }
  96. psinfo_t proc_psinfo;
  97. if(read(proc_fd, &proc_psinfo, sizeof(psinfo_t)) != sizeof(psinfo_t)){
  98. llwarns << "Unable to read " << path << llendl;
  99. close(proc_fd);
  100. return;
  101. }
  102. close(proc_fd);
  103. mExecutableFilename = strdup(proc_psinfo.pr_fname);
  104. llinfos << "mExecutableFilename = [" << mExecutableFilename << "]" << llendl;
  105. sprintf(path, "/proc/%d/path/a.out", (int)getpid());
  106. char execpath[LL_MAX_PATH];
  107. if(readlink(path, execpath, LL_MAX_PATH) == -1){
  108. llwarns << "Unable to read link from " << path << llendl;
  109. return;
  110. }
  111. char *p = execpath; // nuke trash in link, if any exists
  112. int i = 0;
  113. while(*p != NULL && ++i < LL_MAX_PATH && isprint((int)(*p++)));
  114. *p = NULL;
  115. mExecutablePathAndName = strdup(execpath);
  116. llinfos << "mExecutablePathAndName = [" << mExecutablePathAndName << "]" << llendl;
  117. //NOTE: Why force people to cd into the package directory?
  118. // Look for SECONDLIFE env variable and use it, if set.
  119. char *dcf = getenv("SECONDLIFE");
  120. if(dcf != NULL){
  121. (void)strcpy(path, dcf);
  122. (void)strcat(path, "/bin"); //NOTE: make sure we point at the bin
  123. mExecutableDir = strdup(path);
  124. }else{
  125. // plunk a null at last '/' to get exec dir
  126. char *s = execpath + strlen(execpath) -1;
  127. while(*s != '/' && s != execpath){
  128. --s;
  129. }
  130. if(s != execpath){
  131. *s = (char)NULL;
  132. mExecutableDir = strdup(execpath);
  133. llinfos << "mExecutableDir = [" << mExecutableDir << "]" << llendl;
  134. }
  135. }
  136. mLLPluginDir = mExecutableDir + mDirDelimiter + "llplugin";
  137. // *TODO: don't use /tmp, use $HOME/.secondlife/tmp or something.
  138. mTempDir = "/tmp";
  139. }
  140. LLDir_Solaris::~LLDir_Solaris()
  141. {
  142. }
  143. // Implementation
  144. void LLDir_Solaris::initAppDirs(const std::string &app_name,
  145. const std::string& app_read_only_data_dir)
  146. {
  147. // Allow override so test apps can read newview directory
  148. if (!app_read_only_data_dir.empty())
  149. {
  150. mAppRODataDir = app_read_only_data_dir;
  151. }
  152. mAppName = app_name;
  153. std::string upper_app_name(app_name);
  154. LLStringUtil::toUpper(upper_app_name);
  155. char* app_home_env = getenv((upper_app_name + "_USER_DIR").c_str()); /* Flawfinder: ignore */
  156. if (app_home_env)
  157. {
  158. // user has specified own userappdir i.e. $SECONDLIFE_USER_DIR
  159. mOSUserAppDir = app_home_env;
  160. }
  161. else
  162. {
  163. // traditionally on unixoids, MyApp gets ~/.myapp dir for data
  164. mOSUserAppDir = mOSUserDir;
  165. mOSUserAppDir += "/";
  166. mOSUserAppDir += ".";
  167. std::string lower_app_name(app_name);
  168. LLStringUtil::toLower(lower_app_name);
  169. mOSUserAppDir += lower_app_name;
  170. }
  171. // create any directories we expect to write to.
  172. int res = LLFile::mkdir(mOSUserAppDir);
  173. if (res == -1)
  174. {
  175. if (errno != EEXIST)
  176. {
  177. llwarns << "Couldn't create app user dir " << mOSUserAppDir << llendl;
  178. llwarns << "Default to base dir" << mOSUserDir << llendl;
  179. mOSUserAppDir = mOSUserDir;
  180. }
  181. }
  182. res = LLFile::mkdir(getExpandedFilename(LL_PATH_LOGS,""));
  183. if (res == -1)
  184. {
  185. if (errno != EEXIST)
  186. {
  187. llwarns << "Couldn't create LL_PATH_LOGS dir " << getExpandedFilename(LL_PATH_LOGS,"") << llendl;
  188. }
  189. }
  190. res = LLFile::mkdir(getExpandedFilename(LL_PATH_USER_SETTINGS,""));
  191. if (res == -1)
  192. {
  193. if (errno != EEXIST)
  194. {
  195. llwarns << "Couldn't create LL_PATH_USER_SETTINGS dir " << getExpandedFilename(LL_PATH_USER_SETTINGS,"") << llendl;
  196. }
  197. }
  198. res = LLFile::mkdir(getExpandedFilename(LL_PATH_CACHE,""));
  199. if (res == -1)
  200. {
  201. if (errno != EEXIST)
  202. {
  203. llwarns << "Couldn't create LL_PATH_CACHE dir " << getExpandedFilename(LL_PATH_CACHE,"") << llendl;
  204. }
  205. }
  206. mCAFile = getExpandedFilename(LL_PATH_APP_SETTINGS, "CA.pem");
  207. }
  208. U32 LLDir_Solaris::countFilesInDir(const std::string &dirname, const std::string &mask)
  209. {
  210. U32 file_count = 0;
  211. glob_t g;
  212. std::string tmp_str;
  213. tmp_str = dirname;
  214. tmp_str += mask;
  215. if(glob(tmp_str.c_str(), GLOB_NOSORT, NULL, &g) == 0)
  216. {
  217. file_count = g.gl_pathc;
  218. globfree(&g);
  219. }
  220. return (file_count);
  221. }
  222. std::string LLDir_Solaris::getCurPath()
  223. {
  224. char tmp_str[LL_MAX_PATH]; /* Flawfinder: ignore */
  225. if (getcwd(tmp_str, LL_MAX_PATH) == NULL)
  226. {
  227. llwarns << "Could not get current directory" << llendl;
  228. tmp_str[0] = '\0';
  229. }
  230. return tmp_str;
  231. }
  232. BOOL LLDir_Solaris::fileExists(const std::string &filename) const
  233. {
  234. struct stat stat_data;
  235. // Check the age of the file
  236. // Now, we see if the files we've gathered are recent...
  237. int res = stat(filename.c_str(), &stat_data);
  238. if (!res)
  239. {
  240. return TRUE;
  241. }
  242. else
  243. {
  244. return FALSE;
  245. }
  246. }