| 1 |
/* |
| 2 |
* Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2024 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 21 апр. 2024 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/ipc/SharedMem.h> |
| 23 |
|
| 24 |
#include <lsp-plug.in/runtime/version.h> |
| 25 |
|
| 26 |
#include <lsp-plug.in/common/atomic.h> |
| 27 |
#include <lsp-plug.in/common/debug.h> |
| 28 |
#include <lsp-plug.in/common/status.h> |
| 29 |
#include <lsp-plug.in/io/File.h> |
| 30 |
#include <lsp-plug.in/runtime/LSPString.h> |
| 31 |
#include <lsp-plug.in/runtime/system.h> |
| 32 |
#include <lsp-plug.in/runtime/uuid.h> |
| 33 |
|
| 34 |
#ifdef PLATFORM_WINDOWS |
| 35 |
#include <windows.h> |
| 36 |
#include <memoryapi.h> |
| 37 |
#include <fileapi.h> |
| 38 |
|
| 39 |
// Windows headers define macro for a type (sick)... We need to avoid it. |
| 40 |
#ifdef uuid_t |
| 41 |
#undef uuid_t |
| 42 |
#endif /* uuid_t */ |
| 43 |
#else |
| 44 |
#include <errno.h> |
| 45 |
#include <fcntl.h> |
| 46 |
#include <sys/mman.h> |
| 47 |
#include <sys/stat.h> |
| 48 |
#include <sys/types.h> |
| 49 |
#include <unistd.h> |
| 50 |
#endif /* PLATFORM_WINDOWS */ |
| 51 |
|
| 52 |
namespace lsp |
| 53 |
{ |
| 54 |
namespace ipc |
| 55 |
{ |
| 56 |
struct shared_context_t |
| 57 |
{ |
| 58 |
uatomic_t nReferences; // Number of references to this object |
| 59 |
void *pData; // Pointer to the mapped memory buffer |
| 60 |
wsize_t nSize; // Original segment size |
| 61 |
size_t nMapOffset; // Mapping offset |
| 62 |
size_t nMapSize; // Mapping size |
| 63 |
size_t nMode; // Open mode |
| 64 |
LSPString sPath; // Path to the shared memory segment |
| 65 |
|
| 66 |
// System-specific data |
| 67 |
#ifdef PLATFORM_WINDOWS |
| 68 |
LSPString sLock; // Lock name |
| 69 |
HANDLE hFD; // File descriptor |
| 70 |
HANDLE hMapping; // File mapping |
| 71 |
#else |
| 72 |
int hFD; // File descriptor |
| 73 |
#endif /* PLATFORM_WINDOWS */ |
| 74 |
}; |
| 75 |
|
| 76 |
SharedMem::SharedMem() |
| 77 |
{ |
| 78 |
pContext = NULL; |
| 79 |
} |
| 80 |
|
| 81 |
SharedMem::SharedMem(const SharedMem & src) |
| 82 |
{ |
| 83 |
pContext = src.pContext; |
| 84 |
if (pContext != NULL) |
| 85 |
atomic_add(&pContext->nReferences, 1); |
| 86 |
} |
| 87 |
|
| 88 |
SharedMem::SharedMem(SharedMem && src) |
| 89 |
{ |
| 90 |
pContext = src.pContext; |
| 91 |
src.pContext = NULL; |
| 92 |
} |
| 93 |
|
| 94 |
SharedMem::~SharedMem() |
| 95 |
{ |
| 96 |
release_context(); |
| 97 |
} |
| 98 |
|
| 99 |
void SharedMem::release_context() |
| 100 |
{ |
| 101 |
if (pContext == NULL) |
| 102 |
return; |
| 103 |
if (atomic_add(&pContext->nReferences, -1) != 1) |
| 104 |
return; |
| 105 |
|
| 106 |
close_context(pContext); |
| 107 |
delete pContext; |
| 108 |
pContext = NULL; |
| 109 |
} |
| 110 |
|
| 111 |
SharedMem & SharedMem::operator = (const SharedMem & src) |
| 112 |
{ |
| 113 |
release_context(); |
| 114 |
|
| 115 |
pContext = src.pContext; |
| 116 |
if (pContext != NULL) |
| 117 |
atomic_add(&pContext->nReferences, 1); |
| 118 |
|
| 119 |
return *this; |
| 120 |
} |
| 121 |
|
| 122 |
SharedMem & SharedMem::operator = (SharedMem && src) |
| 123 |
{ |
| 124 |
release_context(); |
| 125 |
|
| 126 |
pContext = src.pContext; |
| 127 |
src.pContext = NULL; |
| 128 |
|
| 129 |
return *this; |
| 130 |
} |
| 131 |
|
| 132 |
bool SharedMem::allocate_context() |
| 133 |
{ |
| 134 |
if (pContext != NULL) |
| 135 |
return true; |
| 136 |
shared_context_t *ctx = new shared_context_t; |
| 137 |
if (ctx == NULL) |
| 138 |
return false; |
| 139 |
|
| 140 |
ctx->nReferences = 1; |
| 141 |
ctx->pData = NULL; |
| 142 |
ctx->nSize = 0; |
| 143 |
ctx->nMapOffset = 0; |
| 144 |
ctx->nMapSize = 0; |
| 145 |
ctx->nMode = 0; |
| 146 |
|
| 147 |
#ifdef PLATFORM_WINDOWS |
| 148 |
ctx->hFD = INVALID_HANDLE_VALUE; |
| 149 |
ctx->hMapping = INVALID_HANDLE_VALUE; |
| 150 |
#else |
| 151 |
ctx->hFD = -1; |
| 152 |
#endif /* PLATFORM_WINDOWS */ |
| 153 |
|
| 154 |
pContext = ctx; |
| 155 |
|
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
status_t SharedMem::create(LSPString *name, const LSPString *postfix, size_t mode, size_t size) |
| 160 |
{ |
| 161 |
LSPString tmp; |
| 162 |
lsp::uuid_t uuid; |
| 163 |
char utext[48]; |
| 164 |
|
| 165 |
if (opened()) |
| 166 |
return STATUS_OPENED; |
| 167 |
|
| 168 |
if ((mode & (SHM_READ | SHM_WRITE)) == 0) |
| 169 |
return STATUS_BAD_ARGUMENTS; |
| 170 |
mode |= SHM_CREATE; |
| 171 |
|
| 172 |
const size_t postfix_len = (postfix != NULL) ? postfix->length() : 0; |
| 173 |
if (!tmp.reserve(36 + postfix_len)) |
| 174 |
return STATUS_NO_MEM; |
| 175 |
|
| 176 |
while (true) |
| 177 |
{ |
| 178 |
// Form the name of the shared segment |
| 179 |
generate_uuid(&uuid); |
| 180 |
format_uuid_dashed(utext, &uuid, false); |
| 181 |
|
| 182 |
if (!tmp.set_ascii(utext)) |
| 183 |
return STATUS_NO_MEM; |
| 184 |
if (postfix_len > 0) |
| 185 |
{ |
| 186 |
if (!tmp.append(postfix)) |
| 187 |
return STATUS_NO_MEM; |
| 188 |
} |
| 189 |
|
| 190 |
// Try to create new shared segment |
| 191 |
status_t res = open(&tmp, mode, size); |
| 192 |
if (res == STATUS_OK) |
| 193 |
break; |
| 194 |
else if (res != STATUS_ALREADY_EXISTS) |
| 195 |
return res; |
| 196 |
} |
| 197 |
|
| 198 |
tmp.swap(name); |
| 199 |
|
| 200 |
return STATUS_OK; |
| 201 |
} |
| 202 |
|
| 203 |
status_t SharedMem::create(LSPString *name, const char *postfix, size_t mode, size_t size) |
| 204 |
{ |
| 205 |
LSPString tmp; |
| 206 |
lsp::uuid_t uuid; |
| 207 |
char utext[48]; |
| 208 |
|
| 209 |
if (opened()) |
| 210 |
return STATUS_OPENED; |
| 211 |
|
| 212 |
if ((mode & (SHM_READ | SHM_WRITE)) == 0) |
| 213 |
return STATUS_BAD_ARGUMENTS; |
| 214 |
mode |= SHM_CREATE; |
| 215 |
|
| 216 |
const size_t postfix_len = (postfix != NULL) ? strlen(postfix) : 0; |
| 217 |
if (!tmp.reserve(36 + postfix_len)) |
| 218 |
return STATUS_NO_MEM; |
| 219 |
|
| 220 |
while (true) |
| 221 |
{ |
| 222 |
// Form the name of the shared segment |
| 223 |
// For MacOS there is a serious limitation on the shared memory |
| 224 |
// segment name, so we use Base64 with custom table instead. |
| 225 |
generate_uuid(&uuid); |
| 226 |
#ifdef PLATFORM_MACOSX |
| 227 |
format_uuid_base64(utext, &uuid); |
| 228 |
#else |
| 229 |
format_uuid_dashed(utext, &uuid, false); |
| 230 |
#endif /* PLATFORM_MACOSX */ |
| 231 |
|
| 232 |
if (!tmp.set_ascii(utext)) |
| 233 |
return STATUS_NO_MEM; |
| 234 |
if (postfix_len > 0) |
| 235 |
{ |
| 236 |
if (!tmp.append_utf8(postfix)) |
| 237 |
return STATUS_NO_MEM; |
| 238 |
} |
| 239 |
|
| 240 |
// Try to create new shared segment |
| 241 |
status_t res = open(&tmp, mode, size); |
| 242 |
if (res == STATUS_OK) |
| 243 |
break; |
| 244 |
else if (res != STATUS_ALREADY_EXISTS) |
| 245 |
return res; |
| 246 |
} |
| 247 |
|
| 248 |
tmp.swap(name); |
| 249 |
|
| 250 |
return STATUS_OK; |
| 251 |
} |
| 252 |
|
| 253 |
status_t SharedMem::open(const char *name, size_t mode, size_t size) |
| 254 |
{ |
| 255 |
if (name == NULL) |
| 256 |
return STATUS_BAD_ARGUMENTS; |
| 257 |
if ((mode & (SHM_READ | SHM_WRITE)) == 0) |
| 258 |
return STATUS_BAD_ARGUMENTS; |
| 259 |
if (opened()) |
| 260 |
return STATUS_OPENED; |
| 261 |
|
| 262 |
if (!allocate_context()) |
| 263 |
return STATUS_NO_MEM; |
| 264 |
|
| 265 |
#ifdef PLATFORM_WINDOWS |
| 266 |
// Form the name of the temporary file |
| 267 |
status_t res = system::get_system_temporary_dir(&pContext->sPath); |
| 268 |
if (res != STATUS_OK) |
| 269 |
return res; |
| 270 |
if (!pContext->sPath.append(FILE_SEPARATOR_C)) |
| 271 |
return STATUS_NO_MEM; |
| 272 |
if (!pContext->sPath.append_utf8(name)) |
| 273 |
return STATUS_NO_MEM; |
| 274 |
|
| 275 |
// Form the name of shared mapping |
| 276 |
if (!pContext->sLock.set_ascii("Local" FILE_SEPARATOR_S)) |
| 277 |
return STATUS_NO_MEM; |
| 278 |
if (!pContext->sLock.append_utf8(name)) |
| 279 |
return STATUS_NO_MEM; |
| 280 |
|
| 281 |
#else |
| 282 |
// For portable use, a shared memory object should be identified by a name of the form /somename; |
| 283 |
// that is, a null-terminated string of up to NAME_MAX (i.e., 255) characters consisting of an initial |
| 284 |
// slash, followed by one or more characters, none of which are slashes. |
| 285 |
pContext->sPath.clear(); |
| 286 |
if (!pContext->sPath.append(FILE_SEPARATOR_C)) |
| 287 |
return STATUS_NO_MEM; |
| 288 |
if (!pContext->sPath.append_utf8(name)) |
| 289 |
return STATUS_NO_MEM; |
| 290 |
#endif /* PLATFORM_WINDOWS */ |
| 291 |
|
| 292 |
return open_context(pContext, mode, size); |
| 293 |
} |
| 294 |
|
| 295 |
status_t SharedMem::open(const LSPString *name, size_t mode, size_t size) |
| 296 |
{ |
| 297 |
if (name == NULL) |
| 298 |
return STATUS_BAD_ARGUMENTS; |
| 299 |
if ((mode & (SHM_READ | SHM_WRITE)) == 0) |
| 300 |
return STATUS_BAD_ARGUMENTS; |
| 301 |
if (opened()) |
| 302 |
return STATUS_OPENED; |
| 303 |
|
| 304 |
if (!allocate_context()) |
| 305 |
return STATUS_NO_MEM; |
| 306 |
|
| 307 |
#ifdef PLATFORM_WINDOWS |
| 308 |
// Form the name of the temporary file |
| 309 |
status_t res = system::get_system_temporary_dir(&pContext->sPath); |
| 310 |
if (res != STATUS_OK) |
| 311 |
return res; |
| 312 |
if (!pContext->sPath.append(FILE_SEPARATOR_C)) |
| 313 |
return STATUS_NO_MEM; |
| 314 |
if (!pContext->sPath.append(name)) |
| 315 |
return STATUS_NO_MEM; |
| 316 |
|
| 317 |
// Form the name of shared mapping |
| 318 |
if (!pContext->sLock.set_ascii("Local" FILE_SEPARATOR_S)) |
| 319 |
return STATUS_NO_MEM; |
| 320 |
if (!pContext->sLock.append(name)) |
| 321 |
return STATUS_NO_MEM; |
| 322 |
#else |
| 323 |
// For portable use, a shared memory object should be identified by a name of the form /somename; |
| 324 |
// that is, a null-terminated string of up to NAME_MAX (i.e., 255) characters consisting of an initial |
| 325 |
// slash, followed by one or more characters, none of which are slashes. |
| 326 |
pContext->sPath.clear(); |
| 327 |
if (!pContext->sPath.append(FILE_SEPARATOR_C)) |
| 328 |
return STATUS_NO_MEM; |
| 329 |
if (!pContext->sPath.append(name)) |
| 330 |
return STATUS_NO_MEM; |
| 331 |
#endif /* PLATFORM_WINDOWS */ |
| 332 |
|
| 333 |
return open_context(pContext, mode, size); |
| 334 |
} |
| 335 |
|
| 336 |
status_t SharedMem::close_context(shared_context_t *ctx) |
| 337 |
{ |
| 338 |
status_t res = STATUS_OK; |
| 339 |
if (ctx == NULL) |
| 340 |
return res; |
| 341 |
|
| 342 |
res = update_status(res, unmap_context(ctx)); |
| 343 |
res = update_status(res, close_file(ctx)); |
| 344 |
|
| 345 |
return res; |
| 346 |
} |
| 347 |
|
| 348 |
status_t SharedMem::close_file(shared_context_t *ctx) |
| 349 |
{ |
| 350 |
status_t res = STATUS_OK; |
| 351 |
|
| 352 |
#ifdef PLATFORM_WINDOWS |
| 353 |
lsp_finally { |
| 354 |
ctx->sPath.truncate(); |
| 355 |
ctx->sLock.truncate(); |
| 356 |
}; |
| 357 |
|
| 358 |
if (ctx->hMapping != INVALID_HANDLE_VALUE) |
| 359 |
{ |
| 360 |
if (!CloseHandle(ctx->hMapping)) |
| 361 |
res = update_status(res, STATUS_IO_ERROR); |
| 362 |
ctx->hMapping = INVALID_HANDLE_VALUE; |
| 363 |
} |
| 364 |
|
| 365 |
const bool is_open = (ctx->hFD != INVALID_HANDLE_VALUE); |
| 366 |
if (is_open) |
| 367 |
{ |
| 368 |
if (!CloseHandle(ctx->hFD)) |
| 369 |
res = update_status(res, STATUS_IO_ERROR); |
| 370 |
ctx->hFD = INVALID_HANDLE_VALUE; |
| 371 |
} |
| 372 |
|
| 373 |
// Need to release system structures? |
| 374 |
// For windows, last one who closed the shared resource can unlink it |
| 375 |
if ((is_open) && (!(ctx->nMode & SHM_PERSIST))) |
| 376 |
unlink_file(ctx); |
| 377 |
#else |
| 378 |
lsp_finally { |
| 379 |
ctx->sPath.truncate(); |
| 380 |
}; |
| 381 |
|
| 382 |
// Close file handle and remove it if persistent mode was specified |
| 383 |
const bool is_open = (ctx->hFD >= 0); |
| 384 |
if (is_open) |
| 385 |
{ |
| 386 |
if (::close(ctx->hFD) < 0) |
| 387 |
res = STATUS_IO_ERROR; |
| 388 |
ctx->hFD = -1; |
| 389 |
} |
| 390 |
|
| 391 |
// Need to release system structures? |
| 392 |
// For POSIX, those who created the shared resource |
| 393 |
if ((is_open) && ((ctx->nMode & (SHM_CREATE | SHM_PERSIST)) == SHM_CREATE)) |
| 394 |
res = update_status(res, unlink_file(ctx)); |
| 395 |
|
| 396 |
#endif /* PLATFORM_WINDOWS */ |
| 397 |
|
| 398 |
return res; |
| 399 |
} |
| 400 |
|
| 401 |
status_t SharedMem::unmap_context(shared_context_t *ctx) |
| 402 |
{ |
| 403 |
if (ctx->pData == NULL) |
| 404 |
return STATUS_OK; |
| 405 |
lsp_finally { ctx->pData = NULL; }; |
| 406 |
|
| 407 |
#ifdef PLATFORM_WINDOWS |
| 408 |
if (!FlushViewOfFile(ctx->pData, ctx->nMapSize)) |
| 409 |
return STATUS_IO_ERROR; |
| 410 |
if (!UnmapViewOfFile(ctx->pData)) |
| 411 |
return STATUS_IO_ERROR; |
| 412 |
|
| 413 |
return STATUS_OK; |
| 414 |
#else |
| 415 |
if (munmap(ctx->pData, ctx->nMapSize) >= 0) |
| 416 |
return STATUS_OK; |
| 417 |
|
| 418 |
const int error = errno; |
| 419 |
switch (error) |
| 420 |
{ |
| 421 |
case EACCES: return STATUS_PERMISSION_DENIED; |
| 422 |
case EAGAIN: return STATUS_RETRY; |
| 423 |
case EPERM: return STATUS_PERMISSION_DENIED; |
| 424 |
case EFBIG: return STATUS_TOO_BIG; |
| 425 |
case EEXIST: return STATUS_ALREADY_EXISTS; |
| 426 |
case ENOMEM: return STATUS_NO_MEM; |
| 427 |
case EOVERFLOW: return STATUS_OVERFLOW; |
| 428 |
default: break; |
| 429 |
} |
| 430 |
return STATUS_IO_ERROR; |
| 431 |
#endif /* PLATFORM_WINDOWS */ |
| 432 |
} |
| 433 |
|
| 434 |
status_t SharedMem::unlink_file(shared_context_t *ctx) |
| 435 |
{ |
| 436 |
#ifdef PLATFORM_WINDOWS |
| 437 |
const WCHAR *path = ctx->sPath.get_utf16(); |
| 438 |
if (path == NULL) |
| 439 |
return STATUS_NO_MEM; |
| 440 |
|
| 441 |
if (!DeleteFileW(path)) |
| 442 |
return STATUS_IO_ERROR; |
| 443 |
#else |
| 444 |
const char *path = ctx->sPath.get_native(); |
| 445 |
if (path == NULL) |
| 446 |
return STATUS_NO_MEM; |
| 447 |
|
| 448 |
if (shm_unlink(path) < 0) |
| 449 |
return STATUS_IO_ERROR; |
| 450 |
#endif /* PLATFORM_WINDOWS */ |
| 451 |
|
| 452 |
return STATUS_OK; |
| 453 |
} |
| 454 |
|
| 455 |
status_t SharedMem::open_context(shared_context_t *ctx, size_t mode, size_t size) |
| 456 |
{ |
| 457 |
lsp_finally { |
| 458 |
close_context(ctx); |
| 459 |
}; |
| 460 |
|
| 461 |
#ifdef PLATFORM_WINDOWS |
| 462 |
const WCHAR *path = ctx->sPath.get_utf16(); |
| 463 |
if (path == NULL) |
| 464 |
return STATUS_NO_MEM; |
| 465 |
const WCHAR *lock = ctx->sLock.get_utf16(); |
| 466 |
if (lock == NULL) |
| 467 |
return STATUS_NO_MEM; |
| 468 |
|
| 469 |
DWORD prot_flags = ((mode & SHM_EXEC) != 0) ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE; |
| 470 |
DWORD file_access = 0; |
| 471 |
if ((mode & SHM_READ) != 0) |
| 472 |
file_access |= GENERIC_READ; |
| 473 |
if ((mode & SHM_WRITE) != 0) |
| 474 |
file_access |= GENERIC_READ | GENERIC_WRITE; |
| 475 |
if ((mode & SHM_EXEC) != 0) |
| 476 |
file_access |= GENERIC_EXECUTE; |
| 477 |
|
| 478 |
LARGE_INTEGER l_size; |
| 479 |
l_size.QuadPart = size; |
| 480 |
|
| 481 |
if (mode & SHM_CREATE) |
| 482 |
{ |
| 483 |
if (mode & SHM_PERSIST) |
| 484 |
{ |
| 485 |
// lsp_trace("CreateFileW(access=0x%x, CREATE_NEW)", file_access); |
| 486 |
ctx->hFD = CreateFileW( |
| 487 |
path, // lpFileName |
| 488 |
file_access, // dwDesiredAccess |
| 489 |
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode |
| 490 |
NULL, // lpSecurityAttributes |
| 491 |
CREATE_NEW, // dwCreationDisposition |
| 492 |
FILE_ATTRIBUTE_ARCHIVE, // dwFlagsAndAttributes |
| 493 |
NULL); // hTemplateFile |
| 494 |
|
| 495 |
if (ctx->hFD == INVALID_HANDLE_VALUE) |
| 496 |
{ |
| 497 |
DWORD error = GetLastError(); |
| 498 |
switch (error) |
| 499 |
{ |
| 500 |
case ERROR_FILE_EXISTS: return STATUS_ALREADY_EXISTS; |
| 501 |
case ERROR_ALREADY_EXISTS: return STATUS_ALREADY_EXISTS; |
| 502 |
case ERROR_FILE_NOT_FOUND: return STATUS_NOT_FOUND; |
| 503 |
case ERROR_ACCESS_DENIED: return STATUS_PERMISSION_DENIED; |
| 504 |
default: break; |
| 505 |
} |
| 506 |
return STATUS_IO_ERROR; |
| 507 |
} |
| 508 |
|
| 509 |
// Resize the file |
| 510 |
if (!SetFilePointerEx(ctx->hFD, l_size, NULL, FILE_BEGIN)) |
| 511 |
return STATUS_IO_ERROR; |
| 512 |
if (!SetEndOfFile(ctx->hFD)) |
| 513 |
return STATUS_IO_ERROR; |
| 514 |
} |
| 515 |
else |
| 516 |
ctx->hFD = INVALID_HANDLE_VALUE; |
| 517 |
|
| 518 |
// lsp_trace("CreateFileMappingW(fd=%p, access=0x%x, size=%d)", ctx->hFD, prot_flags, l_size.LowPart); |
| 519 |
ctx->hMapping = CreateFileMappingW( |
| 520 |
ctx->hFD, // hFile |
| 521 |
NULL, // lpFileMappingAttributes |
| 522 |
prot_flags, // flProtect |
| 523 |
l_size.HighPart, // dwMaximumSizeHigh |
| 524 |
l_size.LowPart, // dwMaximumSizeLow |
| 525 |
lock); // lpName |
| 526 |
|
| 527 |
if ((ctx->hMapping == INVALID_HANDLE_VALUE) || (ctx->hMapping == NULL)) |
| 528 |
{ |
| 529 |
ctx->hMapping = INVALID_HANDLE_VALUE; |
| 530 |
DWORD error = GetLastError(); |
| 531 |
switch (error) |
| 532 |
{ |
| 533 |
case ERROR_FILE_EXISTS: return STATUS_ALREADY_EXISTS; |
| 534 |
case ERROR_ACCESS_DENIED: return STATUS_PERMISSION_DENIED; |
| 535 |
default: break; |
| 536 |
} |
| 537 |
return STATUS_IO_ERROR; |
| 538 |
} |
| 539 |
} |
| 540 |
else |
| 541 |
{ |
| 542 |
// Try to open file mapping |
| 543 |
// lsp_trace("OpenFileMappingW(access=0x%x, size=%d)", FILE_MAP_ALL_ACCESS, l_size.LowPart); |
| 544 |
ctx->hMapping = OpenFileMappingW(FILE_MAP_ALL_ACCESS, TRUE, lock); |
| 545 |
if ((ctx->hMapping == INVALID_HANDLE_VALUE) || (ctx->hMapping == NULL)) |
| 546 |
{ |
| 547 |
DWORD error = GetLastError(); |
| 548 |
switch (error) |
| 549 |
{ |
| 550 |
case ERROR_FILE_EXISTS: return STATUS_ALREADY_EXISTS; |
| 551 |
case ERROR_ACCESS_DENIED: return STATUS_PERMISSION_DENIED; |
| 552 |
case ERROR_FILE_NOT_FOUND: |
| 553 |
if (!(mode & SHM_PERSIST)) |
| 554 |
return STATUS_NOT_FOUND; |
| 555 |
break; |
| 556 |
default: return STATUS_IO_ERROR; |
| 557 |
} |
| 558 |
|
| 559 |
// There is no file mapping to open, try to create it |
| 560 |
if (mode & SHM_PERSIST) |
| 561 |
{ |
| 562 |
// lsp_trace("CreateFileW(access=0x%x, OPEN_EXISTING)", file_access); |
| 563 |
|
| 564 |
ctx->hFD = CreateFileW( |
| 565 |
path, // lpFileName |
| 566 |
file_access, // dwDesiredAccess |
| 567 |
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode |
| 568 |
NULL, // lpSecurityAttributes |
| 569 |
OPEN_EXISTING, // dwCreationDisposition |
| 570 |
FILE_ATTRIBUTE_ARCHIVE, // dwFlagsAndAttributes |
| 571 |
NULL); // hTemplateFile |
| 572 |
|
| 573 |
if (ctx->hFD == INVALID_HANDLE_VALUE) |
| 574 |
{ |
| 575 |
DWORD error = GetLastError(); |
| 576 |
switch (error) |
| 577 |
{ |
| 578 |
case ERROR_FILE_EXISTS: return STATUS_ALREADY_EXISTS; |
| 579 |
case ERROR_ALREADY_EXISTS: return STATUS_ALREADY_EXISTS; |
| 580 |
case ERROR_FILE_NOT_FOUND: return STATUS_NOT_FOUND; |
| 581 |
case ERROR_ACCESS_DENIED: return STATUS_PERMISSION_DENIED; |
| 582 |
default: break; |
| 583 |
} |
| 584 |
return STATUS_IO_ERROR; |
| 585 |
} |
| 586 |
|
| 587 |
// Determine file size |
| 588 |
if (!GetFileSizeEx(ctx->hFD, &l_size)) |
| 589 |
return STATUS_IO_ERROR; |
| 590 |
size = l_size.QuadPart; |
| 591 |
} |
| 592 |
else |
| 593 |
ctx->hFD = INVALID_HANDLE_VALUE; |
| 594 |
|
| 595 |
// lsp_trace("CreateFileMappingW(fd=%p, access=0x%x, size=%d)", ctx->hFD, prot_flags, l_size.LowPart); |
| 596 |
ctx->hMapping = CreateFileMappingW( |
| 597 |
ctx->hFD, // hFile |
| 598 |
NULL, // lpFileMappingAttributes |
| 599 |
prot_flags, // flProtect |
| 600 |
l_size.HighPart, // dwMaximumSizeHigh |
| 601 |
l_size.LowPart, // dwMaximumSizeLow |
| 602 |
lock); // lpName |
| 603 |
|
| 604 |
if ((ctx->hMapping == INVALID_HANDLE_VALUE) || (ctx->hMapping == NULL)) |
| 605 |
{ |
| 606 |
ctx->hMapping = INVALID_HANDLE_VALUE; |
| 607 |
DWORD error = GetLastError(); |
| 608 |
switch (error) |
| 609 |
{ |
| 610 |
case ERROR_FILE_EXISTS: return STATUS_ALREADY_EXISTS; |
| 611 |
case ERROR_ACCESS_DENIED: return STATUS_PERMISSION_DENIED; |
| 612 |
default: break; |
| 613 |
} |
| 614 |
return STATUS_IO_ERROR; |
| 615 |
} |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
#else |
| 620 |
// Form the open mode |
| 621 |
int o_flags = 0; |
| 622 |
if ((mode & (SHM_READ | SHM_WRITE)) == 0) |
| 623 |
return STATUS_BAD_ARGUMENTS; |
| 624 |
|
| 625 |
if ((mode & SHM_WRITE) != 0) |
| 626 |
o_flags = O_RDWR; |
| 627 |
else |
| 628 |
o_flags = O_RDONLY; |
| 629 |
if (mode & SHM_CREATE) |
| 630 |
o_flags |= O_CREAT | O_EXCL; |
| 631 |
|
| 632 |
// Get the path |
| 633 |
const char *path = ctx->sPath.get_native(); |
| 634 |
if (path == NULL) |
| 635 |
return STATUS_NO_MEM; |
| 636 |
|
| 637 |
// Create and initialize context |
| 638 |
ctx->nMode = mode & (~SHM_PERSIST); |
| 639 |
|
| 640 |
static constexpr int open_mode = |
| 641 |
S_IRUSR | S_IWUSR | |
| 642 |
S_IRGRP | S_IWGRP | |
| 643 |
S_IROTH | S_IWOTH; |
| 644 |
|
| 645 |
// Open/create shared memory segment |
| 646 |
const int fd = shm_open(path, o_flags, open_mode); |
| 647 |
if (fd < 0) |
| 648 |
{ |
| 649 |
const int error = errno; |
| 650 |
switch (error) |
| 651 |
{ |
| 652 |
case EACCES: return STATUS_PERMISSION_DENIED; |
| 653 |
case EEXIST: return STATUS_ALREADY_EXISTS; |
| 654 |
case EINVAL: return STATUS_INVALID_VALUE; |
| 655 |
case EMFILE: return STATUS_OVERFLOW; |
| 656 |
case ENAMETOOLONG: return STATUS_OVERFLOW; |
| 657 |
case ENFILE: return STATUS_OVERFLOW; |
| 658 |
case ENOENT: return STATUS_NOT_FOUND; |
| 659 |
default: break; |
| 660 |
} |
| 661 |
return STATUS_IO_ERROR; |
| 662 |
} |
| 663 |
ctx->hFD = fd; |
| 664 |
|
| 665 |
// Resize memory segment if it was created |
| 666 |
if (mode & SHM_CREATE) |
| 667 |
{ |
| 668 |
if (ftruncate(ctx->hFD, size) < 0) |
| 669 |
{ |
| 670 |
const int error = errno; |
| 671 |
switch (error) |
| 672 |
{ |
| 673 |
case EACCES: return STATUS_PERMISSION_DENIED; |
| 674 |
case EPERM: return STATUS_PERMISSION_DENIED; |
| 675 |
case EFBIG: return STATUS_TOO_BIG; |
| 676 |
default: break; |
| 677 |
} |
| 678 |
return STATUS_IO_ERROR; |
| 679 |
} |
| 680 |
} |
| 681 |
else |
| 682 |
{ |
| 683 |
io::fattr_t attr; |
| 684 |
status_t res = io::File::stat(fd, &attr); |
| 685 |
if (res != STATUS_OK) |
| 686 |
return res; |
| 687 |
|
| 688 |
size = attr.size; |
| 689 |
} |
| 690 |
|
| 691 |
#endif /* PLATFORM_WINDOWS */ |
| 692 |
|
| 693 |
// Now we are ready to deploy new context |
| 694 |
ctx->nMode = mode; |
| 695 |
ctx->nSize = size; |
| 696 |
|
| 697 |
// Prevent context from being closed |
| 698 |
ctx = NULL; |
| 699 |
|
| 700 |
return STATUS_OK; |
| 701 |
} |
| 702 |
|
| 703 |
status_t SharedMem::close() |
| 704 |
{ |
| 705 |
status_t res = close_context(pContext); |
| 706 |
return res; |
| 707 |
} |
| 708 |
|
| 709 |
status_t SharedMem::map_context(shared_context_t *ctx, size_t offset, size_t size) |
| 710 |
{ |
| 711 |
#ifdef PLATFORM_WINDOWS |
| 712 |
DWORD map_access = 0; |
| 713 |
if ((ctx->nMode & SHM_READ) != 0) |
| 714 |
map_access |= FILE_MAP_READ; |
| 715 |
if ((ctx->nMode & SHM_WRITE) != 0) |
| 716 |
map_access |= FILE_MAP_WRITE; |
| 717 |
if ((ctx->nMode & SHM_EXEC) != 0) |
| 718 |
map_access |= FILE_MAP_EXECUTE; |
| 719 |
|
| 720 |
LARGE_INTEGER l_offset; |
| 721 |
l_offset.QuadPart = offset; |
| 722 |
|
| 723 |
// lsp_trace("MapViewOfFile(%p, 0x%x, %d, %d)", ctx->hMapping, map_access, l_offset.LowPart, size); |
| 724 |
void *addr = MapViewOfFile( |
| 725 |
ctx->hMapping, // hFileMappingObject, |
| 726 |
map_access, // dwDesiredAccess, |
| 727 |
l_offset.HighPart, // dwFileOffsetHigh, |
| 728 |
l_offset.LowPart, // dwFileOffsetLow, |
| 729 |
size); // dwNumberOfBytesToMap |
| 730 |
|
| 731 |
if (addr == NULL) |
| 732 |
{ |
| 733 |
DWORD error = GetLastError(); |
| 734 |
switch (error) |
| 735 |
{ |
| 736 |
case ERROR_FILE_EXISTS: return STATUS_ALREADY_EXISTS; |
| 737 |
case ERROR_ACCESS_DENIED: return STATUS_PERMISSION_DENIED; |
| 738 |
default: break; |
| 739 |
} |
| 740 |
|
| 741 |
return STATUS_IO_ERROR; |
| 742 |
} |
| 743 |
|
| 744 |
// Unmap previously mapped address |
| 745 |
if (ctx->pData != NULL) |
| 746 |
UnmapViewOfFile(ctx->pData); |
| 747 |
|
| 748 |
#else |
| 749 |
int prot_flags = 0; |
| 750 |
if ((ctx->nMode & SHM_READ) != 0) |
| 751 |
prot_flags |= PROT_READ; |
| 752 |
if ((ctx->nMode & SHM_WRITE) != 0) |
| 753 |
prot_flags |= PROT_WRITE; |
| 754 |
if ((ctx->nMode & SHM_EXEC) != 0) |
| 755 |
prot_flags |= PROT_EXEC; |
| 756 |
|
| 757 |
// Map new memory address |
| 758 |
void *addr = mmap(0, size, prot_flags, MAP_SHARED, ctx->hFD, offset); |
| 759 |
if (addr == MAP_FAILED) |
| 760 |
{ |
| 761 |
const int error = errno; |
| 762 |
switch (error) |
| 763 |
{ |
| 764 |
case EACCES: return STATUS_PERMISSION_DENIED; |
| 765 |
case EAGAIN: return STATUS_RETRY; |
| 766 |
case EPERM: return STATUS_PERMISSION_DENIED; |
| 767 |
case EFBIG: return STATUS_TOO_BIG; |
| 768 |
case EEXIST: return STATUS_ALREADY_EXISTS; |
| 769 |
case ENOMEM: return STATUS_NO_MEM; |
| 770 |
case EOVERFLOW: return STATUS_OVERFLOW; |
| 771 |
default: break; |
| 772 |
} |
| 773 |
return STATUS_IO_ERROR; |
| 774 |
} |
| 775 |
|
| 776 |
// Unmap previously mapped address |
| 777 |
if (ctx->pData != NULL) |
| 778 |
munmap(ctx->pData, ctx->nMapSize); |
| 779 |
|
| 780 |
#endif /* PLATFORM_WINDOWS */ |
| 781 |
|
| 782 |
// Commit new state |
| 783 |
ctx->pData = addr; |
| 784 |
ctx->nMapOffset = offset; |
| 785 |
ctx->nMapSize = size; |
| 786 |
|
| 787 |
return STATUS_OK; |
| 788 |
} |
| 789 |
|
| 790 |
status_t SharedMem::map(size_t offset, size_t size) |
| 791 |
{ |
| 792 |
if (!opened()) |
| 793 |
return STATUS_CLOSED; |
| 794 |
|
| 795 |
return map_context(pContext, offset, size); |
| 796 |
} |
| 797 |
|
| 798 |
status_t SharedMem::unmap() |
| 799 |
{ |
| 800 |
// Check state |
| 801 |
if (!opened()) |
| 802 |
return STATUS_CLOSED; |
| 803 |
if (!mapped()) |
| 804 |
return STATUS_NOT_MAPPED; |
| 805 |
|
| 806 |
// Unmap memory |
| 807 |
return unmap_context(pContext); |
| 808 |
} |
| 809 |
|
| 810 |
void *SharedMem::data() |
| 811 |
{ |
| 812 |
return (pContext != NULL) ? pContext->pData : NULL; |
| 813 |
} |
| 814 |
|
| 815 |
const void *SharedMem::data() const |
| 816 |
{ |
| 817 |
return (pContext != NULL) ? pContext->pData : NULL; |
| 818 |
} |
| 819 |
|
| 820 |
ssize_t SharedMem::map_offset() const |
| 821 |
{ |
| 822 |
if (!opened()) |
| 823 |
return -STATUS_CLOSED; |
| 824 |
if (!mapped()) |
| 825 |
return -STATUS_NOT_MAPPED; |
| 826 |
|
| 827 |
return pContext->nMapOffset; |
| 828 |
} |
| 829 |
|
| 830 |
ssize_t SharedMem::map_size() const |
| 831 |
{ |
| 832 |
if (!opened()) |
| 833 |
return -STATUS_CLOSED; |
| 834 |
if (!mapped()) |
| 835 |
return -STATUS_NOT_MAPPED; |
| 836 |
|
| 837 |
return pContext->nMapSize; |
| 838 |
} |
| 839 |
|
| 840 |
wssize_t SharedMem::size() const |
| 841 |
{ |
| 842 |
if (!opened()) |
| 843 |
return -STATUS_CLOSED; |
| 844 |
|
| 845 |
return pContext->nSize; |
| 846 |
} |
| 847 |
|
| 848 |
bool SharedMem::opened() const |
| 849 |
{ |
| 850 |
if (pContext == NULL) |
| 851 |
return false; |
| 852 |
#ifdef PLATFORM_WINDOWS |
| 853 |
if ((pContext->hFD == INVALID_HANDLE_VALUE) && (pContext->hMapping == INVALID_HANDLE_VALUE)) |
| 854 |
return false; |
| 855 |
#else |
| 856 |
if (pContext->hFD < 0) |
| 857 |
return false; |
| 858 |
#endif /* PLATFORM_WINDOWS */ |
| 859 |
|
| 860 |
return true; |
| 861 |
} |
| 862 |
|
| 863 |
bool SharedMem::mapped() const |
| 864 |
{ |
| 865 |
return (pContext != NULL) ? pContext->pData != NULL : false; |
| 866 |
} |
| 867 |
|
| 868 |
} /* namespace ipc */ |
| 869 |
} /* namespace lsp */ |
| 870 |
|