llpluginsharedmemory.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /**
  2. * @file llpluginsharedmemory.cpp
  3. * LLPluginSharedMemory manages a shared memory segment for use by the
  4. * LLPlugin API.
  5. *
  6. * $LicenseInfo:firstyear=2008&license=viewergpl$
  7. *
  8. * Copyright (c) 2008-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #include "linden_common.h"
  34. #if !LL_WINDOWS
  35. # include <unistd.h> // For getpid()
  36. #endif
  37. #include "llpluginsharedmemory.h"
  38. // on Mac and Linux, we use the native shm_open/mmap interface by using
  39. // #define USE_SHM_OPEN_SHARED_MEMORY 1
  40. // in the appropriate sections below.
  41. // For Windows, use:
  42. // #define USE_WIN32_SHARED_MEMORY 1
  43. // If we ever want to fall back to the apr implementation for a platform, use:
  44. // #define USE_APR_SHARED_MEMORY 1
  45. #if LL_WINDOWS
  46. //# define USE_APR_SHARED_MEMORY 1
  47. # define USE_WIN32_SHARED_MEMORY 1
  48. #elif LL_DARWIN
  49. # define USE_SHM_OPEN_SHARED_MEMORY 1
  50. #elif LL_LINUX
  51. # define USE_SHM_OPEN_SHARED_MEMORY 1
  52. #endif
  53. #define APR_SHARED_MEMORY_DIR_STRING "LLPlugin_"
  54. #if LL_WINDOWS
  55. # define APR_SHARED_MEMORY_PREFIX_STRING "C:\\LLPlugin_"
  56. // Apparently using the "Global\\" prefix here only works from
  57. // administrative accounts under Vista. Other options I've seen referenced
  58. // are "Local\\" and "Session\\".
  59. # define WIN32_SHARED_MEMORY_PREFIX_STRING "Local\\LL_"
  60. #else
  61. // mac and linux
  62. # define APR_SHARED_MEMORY_PREFIX_STRING "/tmp/LLPlugin_"
  63. # define SHM_OPEN_SHARED_MEMORY_PREFIX_STRING "/LL"
  64. #endif
  65. #if USE_APR_SHARED_MEMORY
  66. # include "llapr.h"
  67. # include "apr_shm.h"
  68. #elif USE_SHM_OPEN_SHARED_MEMORY
  69. # include <sys/fcntl.h>
  70. # include <sys/mman.h>
  71. # include <errno.h>
  72. #elif USE_WIN32_SHARED_MEMORY
  73. # include <windows.h>
  74. #endif // USE_APR_SHARED_MEMORY
  75. int LLPluginSharedMemory::sSegmentNumber = 0;
  76. std::string LLPluginSharedMemory::createName()
  77. {
  78. std::stringstream newname;
  79. #if LL_WINDOWS
  80. newname << GetCurrentProcessId();
  81. #else // LL_WINDOWS
  82. newname << getpid();
  83. #endif // LL_WINDOWS
  84. newname << "_" << sSegmentNumber++;
  85. return newname.str();
  86. }
  87. class LLPluginSharedMemoryPlatformImpl
  88. {
  89. public:
  90. LLPluginSharedMemoryPlatformImpl();
  91. #if USE_APR_SHARED_MEMORY
  92. apr_shm_t* mAprSharedMemory;
  93. #elif USE_SHM_OPEN_SHARED_MEMORY
  94. int mSharedMemoryFD;
  95. #elif USE_WIN32_SHARED_MEMORY
  96. HANDLE mMapFile;
  97. #endif
  98. };
  99. // Constructor. Creates a shared memory segment.
  100. LLPluginSharedMemory::LLPluginSharedMemory()
  101. : mImpl(new LLPluginSharedMemoryPlatformImpl),
  102. mSize(0),
  103. mMappedAddress(NULL),
  104. mNeedsDestroy(false)
  105. {
  106. }
  107. // Destructor. Uses destroy() and detach() to ensure shared memory segment is
  108. // cleaned up.
  109. LLPluginSharedMemory::~LLPluginSharedMemory()
  110. {
  111. if (mNeedsDestroy)
  112. {
  113. destroy();
  114. }
  115. else
  116. {
  117. detach();
  118. }
  119. unlink();
  120. delete mImpl;
  121. }
  122. #if USE_APR_SHARED_MEMORY
  123. // MARK: apr implementation
  124. LLPluginSharedMemoryPlatformImpl::LLPluginSharedMemoryPlatformImpl()
  125. {
  126. mAprSharedMemory = NULL;
  127. }
  128. bool LLPluginSharedMemory::map()
  129. {
  130. mMappedAddress = apr_shm_baseaddr_get(mImpl->mAprSharedMemory);
  131. return mMappedAddress != NULL;
  132. }
  133. bool LLPluginSharedMemory::unmap()
  134. {
  135. // This is a no-op under apr.
  136. return true;
  137. }
  138. bool LLPluginSharedMemory::close()
  139. {
  140. // This is a no-op under apr.
  141. return true;
  142. }
  143. bool LLPluginSharedMemory::unlink()
  144. {
  145. // This is a no-op under apr.
  146. return true;
  147. }
  148. bool LLPluginSharedMemory::create(size_t size)
  149. {
  150. mName = APR_SHARED_MEMORY_PREFIX_STRING;
  151. char* env = getenv("TMP");
  152. #if LL_WINDOWS
  153. if (!env)
  154. {
  155. env = getenv("TEMP");
  156. }
  157. if (env)
  158. {
  159. mName.assign(env);
  160. size_t length = mName.length();
  161. if (length)
  162. {
  163. if (mName[length - 1] != '\\')
  164. {
  165. mName += '\\';
  166. }
  167. mName += APR_SHARED_MEMORY_DIR_STRING;
  168. }
  169. else
  170. {
  171. mName = APR_SHARED_MEMORY_PREFIX_STRING;
  172. }
  173. }
  174. #else
  175. if (!env)
  176. {
  177. env = getenv("TMPDIR");
  178. }
  179. if (env)
  180. {
  181. mName.assign(env);
  182. size_t length = mName.length();
  183. if (length)
  184. {
  185. if (mName[length - 1] != '/')
  186. {
  187. mName += '/';
  188. }
  189. mName += APR_SHARED_MEMORY_DIR_STRING;
  190. }
  191. else
  192. {
  193. mName = APR_SHARED_MEMORY_PREFIX_STRING;
  194. }
  195. }
  196. #endif
  197. mName += createName();
  198. mSize = size;
  199. apr_status_t status = apr_shm_create(&(mImpl->mAprSharedMemory), mSize,
  200. mName.c_str(), gAPRPoolp);
  201. if (ll_apr_warn_status(status))
  202. {
  203. return false;
  204. }
  205. mNeedsDestroy = true;
  206. return map();
  207. }
  208. bool LLPluginSharedMemory::destroy()
  209. {
  210. if (mImpl->mAprSharedMemory)
  211. {
  212. apr_status_t status = apr_shm_destroy(mImpl->mAprSharedMemory);
  213. if (ll_apr_warn_status(status))
  214. {
  215. // *TODO: Is this a fatal error ? I think not...
  216. }
  217. mImpl->mAprSharedMemory = NULL;
  218. }
  219. return true;
  220. }
  221. bool LLPluginSharedMemory::attach(const std::string& name, size_t size)
  222. {
  223. mName = name;
  224. mSize = size;
  225. apr_status_t status = apr_shm_attach(&(mImpl->mAprSharedMemory),
  226. mName.c_str(), gAPRPoolp);
  227. if (ll_apr_warn_status(status))
  228. {
  229. return false;
  230. }
  231. return map();
  232. }
  233. bool LLPluginSharedMemory::detach()
  234. {
  235. if (mImpl->mAprSharedMemory)
  236. {
  237. apr_status_t status = apr_shm_detach(mImpl->mAprSharedMemory);
  238. if (ll_apr_warn_status(status))
  239. {
  240. // *TODO: Is this a fatal error ? I think not...
  241. }
  242. mImpl->mAprSharedMemory = NULL;
  243. }
  244. return true;
  245. }
  246. #elif USE_SHM_OPEN_SHARED_MEMORY
  247. // MARK: shm_open/mmap implementation
  248. LLPluginSharedMemoryPlatformImpl::LLPluginSharedMemoryPlatformImpl()
  249. {
  250. mSharedMemoryFD = -1;
  251. }
  252. bool LLPluginSharedMemory::map()
  253. {
  254. mMappedAddress = ::mmap(NULL, mSize, PROT_READ | PROT_WRITE, MAP_SHARED,
  255. mImpl->mSharedMemoryFD, 0);
  256. if (!mMappedAddress)
  257. {
  258. return false;
  259. }
  260. LL_DEBUGS("Plugin") << "memory mapped at " << mMappedAddress << LL_ENDL;
  261. return true;
  262. }
  263. bool LLPluginSharedMemory::unmap()
  264. {
  265. if (mMappedAddress)
  266. {
  267. LL_DEBUGS("Plugin") << "calling munmap(" << mMappedAddress << ", "
  268. << mSize << ")" << LL_ENDL;
  269. if (::munmap(mMappedAddress, mSize) == -1)
  270. {
  271. // *TODO: Is this a fatal error ? I think not...
  272. }
  273. mMappedAddress = NULL;
  274. }
  275. return true;
  276. }
  277. bool LLPluginSharedMemory::close()
  278. {
  279. if (mImpl->mSharedMemoryFD != -1)
  280. {
  281. LL_DEBUGS("Plugin") << "calling close(" << mImpl->mSharedMemoryFD
  282. << ")" << LL_ENDL;
  283. if (::close(mImpl->mSharedMemoryFD) == -1)
  284. {
  285. // *TODO: Is this a fatal error ? I think not...
  286. }
  287. mImpl->mSharedMemoryFD = -1;
  288. }
  289. return true;
  290. }
  291. bool LLPluginSharedMemory::unlink()
  292. {
  293. if (!mName.empty())
  294. {
  295. if (::shm_unlink(mName.c_str()) == -1)
  296. {
  297. return false;
  298. }
  299. }
  300. return true;
  301. }
  302. bool LLPluginSharedMemory::create(size_t size)
  303. {
  304. mName = SHM_OPEN_SHARED_MEMORY_PREFIX_STRING;
  305. mName += createName();
  306. mSize = size;
  307. // Preemptive unlink, just in case something didn't get cleaned up.
  308. unlink();
  309. mImpl->mSharedMemoryFD = ::shm_open(mName.c_str(),
  310. O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  311. if (mImpl->mSharedMemoryFD == -1)
  312. {
  313. return false;
  314. }
  315. mNeedsDestroy = true;
  316. if (::ftruncate(mImpl->mSharedMemoryFD, mSize) == -1)
  317. {
  318. return false;
  319. }
  320. return map();
  321. }
  322. bool LLPluginSharedMemory::destroy()
  323. {
  324. unmap();
  325. close();
  326. return true;
  327. }
  328. bool LLPluginSharedMemory::attach(const std::string& name, size_t size)
  329. {
  330. mName = name;
  331. mSize = size;
  332. mImpl->mSharedMemoryFD = ::shm_open(mName.c_str(), O_RDWR,
  333. S_IRUSR | S_IWUSR);
  334. if (mImpl->mSharedMemoryFD == -1)
  335. {
  336. return false;
  337. }
  338. // unlink here so the segment will be cleaned up automatically after the
  339. // last close.
  340. unlink();
  341. return map();
  342. }
  343. bool LLPluginSharedMemory::detach()
  344. {
  345. unmap();
  346. close();
  347. return true;
  348. }
  349. #elif USE_WIN32_SHARED_MEMORY
  350. // MARK: Win32 CreateFileMapping-based implementation
  351. // Reference: http://msdn.microsoft.com/en-us/library/aa366551(VS.85).aspx
  352. LLPluginSharedMemoryPlatformImpl::LLPluginSharedMemoryPlatformImpl()
  353. {
  354. mMapFile = NULL;
  355. }
  356. bool LLPluginSharedMemory::map()
  357. {
  358. mMappedAddress = MapViewOfFile(mImpl->mMapFile, // handle to map object
  359. FILE_MAP_ALL_ACCESS, // r/w permission
  360. 0, 0, mSize);
  361. if (mMappedAddress == NULL)
  362. {
  363. llwarns << "MapViewOfFile failed: " << GetLastError() << llendl;
  364. return false;
  365. }
  366. LL_DEBUGS("Plugin") << "memory mapped at " << mMappedAddress << LL_ENDL;
  367. return true;
  368. }
  369. bool LLPluginSharedMemory::unmap()
  370. {
  371. if (mMappedAddress)
  372. {
  373. UnmapViewOfFile(mMappedAddress);
  374. mMappedAddress = NULL;
  375. }
  376. return true;
  377. }
  378. bool LLPluginSharedMemory::close()
  379. {
  380. if (mImpl->mMapFile)
  381. {
  382. CloseHandle(mImpl->mMapFile);
  383. mImpl->mMapFile = NULL;
  384. }
  385. return true;
  386. }
  387. bool LLPluginSharedMemory::unlink()
  388. {
  389. // This is a no-op on Windows.
  390. return true;
  391. }
  392. bool LLPluginSharedMemory::create(size_t size)
  393. {
  394. mName = WIN32_SHARED_MEMORY_PREFIX_STRING;
  395. mName += createName();
  396. mSize = size;
  397. mImpl->mMapFile = CreateFileMappingA(INVALID_HANDLE_VALUE, // use paging file
  398. NULL, // default security
  399. PAGE_READWRITE, // read/write access
  400. 0, // max. object size
  401. mSize, // buffer size
  402. mName.c_str()); // name of mapping object
  403. if (!mImpl->mMapFile)
  404. {
  405. llwarns << "CreateFileMapping failed: " << GetLastError() << llendl;
  406. return false;
  407. }
  408. mNeedsDestroy = true;
  409. return map();
  410. }
  411. bool LLPluginSharedMemory::destroy()
  412. {
  413. unmap();
  414. close();
  415. return true;
  416. }
  417. bool LLPluginSharedMemory::attach(const std::string& name, size_t size)
  418. {
  419. mName = name;
  420. mSize = size;
  421. mImpl->mMapFile = OpenFileMappingA(FILE_MAP_ALL_ACCESS, // read/write access
  422. FALSE, // do not inherit the name
  423. mName.c_str()); // name of mapping object
  424. if (!mImpl->mMapFile)
  425. {
  426. llwarns << "OpenFileMapping failed: " << GetLastError() << llendl;
  427. return false;
  428. }
  429. return map();
  430. }
  431. bool LLPluginSharedMemory::detach()
  432. {
  433. unmap();
  434. close();
  435. return true;
  436. }
  437. #endif