-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathshm_win.c
264 lines (229 loc) · 7.54 KB
/
shm_win.c
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#ifdef _WIN32
#include <windows.h>
#include <winnt.h>
#include <stdio.h>
#include <conio.h>
#include <wchar.h>
#include "shm_win.h"
#include <sddl.h>
#define SHM_QUEUE_FILE L"SHM-QUEUE-DATA-"
#define SHM_QUEUE_META L"SHM-QUEUE-META-"
#define SHM_QUEUE_PRI_FILE L"SHM-QUEUE-PRI-DATA-"
#define SHM_QUEUE_PRI_META L"SHM-QUEUE-PRI-META-"
struct shmmeta_info
{
unsigned int size;
unsigned int reserved[63];
};
#define MAX_SHM_NUM 128
static HANDLE allHandles[MAX_SHM_NUM] = {NULL};
static HANDLE metaHandles[MAX_SHM_NUM] = {NULL};
static void *metaBuffers[MAX_SHM_NUM] = {NULL};
static unsigned int allSizes[MAX_SHM_NUM] = {0};
static unsigned int shmKeys[MAX_SHM_NUM] = {0};
static HANDLE shmget_by_name(const WCHAR *filename, unsigned long key, unsigned long size, int create)
{
WCHAR shm_name[256];
wsprintfW(shm_name, L"%ws%lu", filename, key);
HANDLE hMapFile;
if (create)
{
SECURITY_ATTRIBUTES sa;
SECURITY_DESCRIPTOR sd;
BOOL bRet = InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
if (!bRet)
{
wprintf(L"InitializeSecurityDescriptor failed (error=%d).\n", GetLastError());
return NULL;
}
bRet = SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE);
if (!bRet)
{
wprintf(L"SetSecurityDescriptorDacl failed (error=%d).\n", GetLastError());
return NULL;
}
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = &sd;
sa.bInheritHandle = FALSE;
hMapFile = CreateFileMappingW(
INVALID_HANDLE_VALUE, // use paging file
&sa, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
size, // maximum object size (low-order DWORD)
shm_name); // name of mapping object
if (hMapFile == NULL)
{
wprintf(L"Could not create file mapping object %ws (error=%d).\n", shm_name, GetLastError());
return NULL;
}
}
else
{
hMapFile = OpenFileMappingW(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
shm_name); // name of mapping object
if (hMapFile == NULL)
{
wprintf(L"Could not open file mapping object %ws (%d).\n", shm_name, GetLastError());
return NULL;
}
}
return hMapFile;
}
void *shmat_win(int shmid)
{
if (shmid < 0 || shmid >= MAX_SHM_NUM || allHandles[shmid] == 0)
{
wprintf(L"ShmID %d is invalid.\n", shmid);
return (void*)-1;
}
void *pBuf = (void *) MapViewOfFile(allHandles[shmid], // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
allSizes[shmid]);
if (pBuf == NULL)
{
wprintf(L"Could not map view of file (%d).\n", GetLastError());
return (void*)-1;
}
wprintf(L"Attached shmid %d with size %d successfully\n", shmid, allSizes[shmid]);
return pBuf;
}
static int shmget_ext(unsigned long key, unsigned long size, int create, BOOL isprivate)
{
if (key == 0)
{
isprivate = TRUE;
key = 0x70000000UL + (((unsigned long)time_win(NULL)) & 0xffff0000) + ((((unsigned long)rand()) & 0x0fff) << 16);
}
const WCHAR* metakey = isprivate ? SHM_QUEUE_PRI_META : SHM_QUEUE_META;
const WCHAR* datakey = isprivate ? SHM_QUEUE_PRI_FILE : SHM_QUEUE_FILE;
HANDLE h = shmget_by_name(metakey, key, sizeof(struct shmmeta_info), create);
if (h == NULL)
return -1;
struct shmmeta_info *meta = (struct shmmeta_info *)MapViewOfFile(h, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
sizeof(struct shmmeta_info));
if (meta == NULL)
{
wprintf(L"Could not map view of meta shm (%d).\n", GetLastError());
CloseHandle(h);
return -1;
}
if (create) // save size
{
meta->size = size;
wprintf(L"Write size of %d to meta shm\n", size);
}
else // open only
{
wprintf(L"Read size of %d from meta shm\n", meta->size);
if (size == 0)
size = meta->size;
else if (size != meta->size)
{
wprintf(L"Error: shm size %d is different from shm meta of %d.\n", size, meta->size);
UnmapViewOfFile(meta);
CloseHandle(h);
return -1;
}
}
HANDLE h2 = shmget_by_name(datakey, key, size, create);
if (h2 == NULL)
{
CloseHandle(h);
UnmapViewOfFile(meta);
return -1;
}
int shmid;
for (shmid=0; shmid < MAX_SHM_NUM; shmid++)
{
if (InterlockedCompareExchange64(allHandles+shmid, h2, 0) == 0)
{
allSizes[shmid] = size;
metaHandles[shmid] = h;
metaBuffers[shmid] = meta;
shmKeys[shmid] = key;
return shmid;
}
}
wprintf(L"Too many shm handles, max is %d.\n", MAX_SHM_NUM);
CloseHandle(h2);
UnmapViewOfFile(meta);
CloseHandle(h);
return -1;
}
int shmget_pri_win(unsigned long key, unsigned long size, int create)
{
return shmget_ext(key, size, create, TRUE);
}
int shmget_win(unsigned long key, unsigned long size, int create)
{
return shmget_ext(key, size, create, FALSE);
}
unsigned int shmgetkey(int shmid)
{
if (shmid < 0 || shmid >= MAX_SHM_NUM)
{
return 0;
}
return shmKeys[shmid];
}
void shmdt_win(void *pBuf)
{
UnmapViewOfFile(pBuf);
}
void shmclose_win(int shmid)
{
if (shmid < 0 || shmid >= MAX_SHM_NUM || allHandles[shmid] == 0)
{
return;
}
if (metaBuffers[shmid])
UnmapViewOfFile(metaBuffers[shmid]);
if (metaHandles[shmid])
CloseHandle(metaHandles[shmid]);
CloseHandle(allHandles[shmid]);
allSizes[shmid] = 0;
allHandles[shmid] = 0;
metaBuffers[shmid] = NULL;
metaHandles[shmid] = NULL;
}
int gettimeofday_win(struct timeval_win* tp, void* tzp)
{
// Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's
// This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
// until 00:00:00 January 1, 1970
static const unsigned long long EPOCH = ((unsigned long long)116444736000000000ULL);
SYSTEMTIME system_time;
FILETIME file_time;
unsigned long long time;
GetSystemTime(&system_time);
SystemTimeToFileTime(&system_time, &file_time);
time = ((unsigned long long)file_time.dwLowDateTime);
time += ((unsigned long long)file_time.dwHighDateTime) << 32;
tp->tv_sec = (long)((time - EPOCH) / 10000000L);
tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
return 0;
}
time_t time_win(time_t* t)
{
static const unsigned long long EPOCH = ((unsigned long long)116444736000000000ULL);
SYSTEMTIME system_time;
FILETIME file_time;
unsigned long long time;
GetSystemTime(&system_time);
SystemTimeToFileTime(&system_time, &file_time);
time = ((unsigned long long)file_time.dwLowDateTime);
time += ((unsigned long long)file_time.dwHighDateTime) << 32;
time_t sec = (long)((time - EPOCH) / 10000000L);
if (t)
*t = sec;
return sec;
}
#endif