1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
/*******************************************************************************
* Copyright (c) 2000, 2009 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Silenio Quarti
*******************************************************************************/
#include "eclipseOS.h"
#include "eclipseShm.h"
static _TCHAR* ECLIPSE_UNITIALIZED = _T_ECLIPSE("ECLIPSE_UNINITIALIZED");
#ifdef _WIN32
#include <stdio.h>
#ifdef __MINGW32__
#include <stdlib.h>
#endif
int createSharedData(_TCHAR** id, int size) {
HANDLE mapHandle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, NULL);
if (mapHandle == 0) return -1;
if (id != NULL) {
*id = malloc(18 * sizeof(_TCHAR));
#ifdef WIN64
_stprintf(*id, _T_ECLIPSE("%lx_%I64x"), GetCurrentProcessId(), (DWORDLONG) mapHandle);
#else
_stprintf(*id, _T_ECLIPSE("%lx_%lx"), GetCurrentProcessId(), (DWORD) mapHandle);
#endif
}
/* set the shared data to "uninitialized" */
setSharedData(*id, ECLIPSE_UNITIALIZED);
return 0;
}
static int getShmID(const _TCHAR* id, LPDWORD processID, LPHANDLE handle) {
if (id != NULL && _tcslen(id) > 0) {
DWORD i1;
#ifdef WIN64
DWORDLONG i2;
if (_stscanf(id, _T_ECLIPSE("%lx_%I64x"), &i1, &i2) != 2) return -1;
#else
DWORD i2;
if (_stscanf(id, _T_ECLIPSE("%lx_%lx"), &i1, &i2) != 2) return -1;
#endif
*processID = (DWORD)i1;
*handle = (HANDLE)i2;
return 0;
}
return -1;
}
int destroySharedData(_TCHAR* id) {
DWORD processID;
HANDLE handle;
if (getShmID(id, &processID, &handle) == -1) return -1;
if (!CloseHandle(handle)) return -1;
return 0;
}
int getSharedData(_TCHAR* id, _TCHAR** data) {
_TCHAR *sharedData, *newData = NULL;
DWORD processID;
HANDLE handle, mapHandle = NULL, processHandle;
if (getShmID(id, &processID, &handle) == -1) return -1;
if (processID == GetCurrentProcessId()) {
mapHandle = handle;
} else {
processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processID);
if (processHandle == NULL) return -1;
DuplicateHandle(processHandle, handle, GetCurrentProcess(), &mapHandle, DUPLICATE_SAME_ACCESS, FALSE, DUPLICATE_SAME_ACCESS);
CloseHandle(processHandle);
}
if (mapHandle == NULL) return -1;
sharedData = MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, 0);
if (sharedData == NULL) return -1;
if (_tcscmp(sharedData, ECLIPSE_UNITIALIZED)== 0) return 0;
if (data != NULL) {
size_t length = (_tcslen(sharedData) + 1) * sizeof(_TCHAR);
newData = malloc(length);
memcpy(newData, sharedData, length);
}
if (!UnmapViewOfFile(sharedData)) {
free(newData);
return -1;
}
if (handle != mapHandle) {
CloseHandle(mapHandle);
}
*data = newData;
return 0;
}
int setSharedData(const _TCHAR* id, const _TCHAR* data) {
_TCHAR* sharedData;
DWORD processID;
HANDLE handle, mapHandle = NULL, processHandle;
if (getShmID(id, &processID, &handle) == -1) return -1;
if (processID == GetCurrentProcessId()) {
mapHandle = handle;
} else {
processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processID);
if (processHandle == NULL) return -1;
DuplicateHandle(processHandle, handle, GetCurrentProcess(), &mapHandle, DUPLICATE_SAME_ACCESS, FALSE, DUPLICATE_SAME_ACCESS);
CloseHandle(processHandle);
}
if (mapHandle == NULL) return -1;
sharedData = MapViewOfFile(mapHandle, FILE_MAP_WRITE, 0, 0, 0);
if (sharedData == NULL) return -1;
if (data != NULL) {
size_t length = (_tcslen(data) + 1) * sizeof(_TCHAR);
memcpy(sharedData, data, length);
} else {
memset(sharedData, 0, sizeof(_TCHAR));
}
if (!UnmapViewOfFile(sharedData)) {
return -1;
}
if (handle != mapHandle) {
CloseHandle(mapHandle);
}
return 0;
}
#else /* Unix like platforms */
#include <sys/shm.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include "eclipse-memcpy.h"
int createSharedData(char** id, int size) {
int shmid;
key_t key = getpid();
if ((shmid = shmget(key, size, IPC_CREAT | 0666)) < 0) {
return -1;
}
if (id != NULL) {
*id = malloc(9 * sizeof(char));
sprintf(*id, "%x", shmid);
}
setSharedData(*id, ECLIPSE_UNITIALIZED);
return 0;
}
static int getShmID(const char* id) {
int shmid = -1;
/* Determine the shared memory id. */
if (id != NULL && strlen(id) > 0) {
sscanf(id, "%x", &shmid);
}
return shmid;
}
int destroySharedData(char* id) {
int shmid = getShmID(id);
if (shmid == -1) return -1;
return shmctl(shmid, IPC_RMID, NULL);
}
int getSharedData( char* id, char** data ) {
char *sharedData, *newData = NULL;
int length;
int shmid = getShmID(id);
if (shmid == -1) return -1;
sharedData = shmat(shmid, (void *)0, 0);
if (sharedData == (char *)(-1)) return -1;
if (_tcscmp(sharedData, ECLIPSE_UNITIALIZED) == 0) return 0;
length = strlen(sharedData) + 1;
newData = malloc(length);
memcpy(newData, sharedData, length);
if (shmdt(sharedData) != 0) {
free(newData);
return -1;
}
*data = newData;
return 0;
}
int setSharedData(const char* id, const char* data) {
char* sharedData;
int length;
int shmid = getShmID(id);
if (shmid == -1) return -1;
sharedData = shmat(shmid, (void *)0, 0);
if (sharedData == (char *)(-1)) return -1;
if (data != NULL) {
length = strlen(data) + 1;
memcpy(sharedData, data, length);
} else {
memset(sharedData, 0, sizeof(char));
}
if (shmdt(sharedData) != 0) {
return -1;
}
return 0;
}
#endif /* Unix like platforms */
|