-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathshlink.c
More file actions
209 lines (172 loc) · 5.4 KB
/
shlink.c
File metadata and controls
209 lines (172 loc) · 5.4 KB
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
extern int verbose;
#ifdef T4SHLINKS
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "shlink.h"
static HANDLE hMapFile;
void *shlink_attach (const char *fnm, int size)
{
char szName[256];
void *addr = NULL;
strcpy (szName, "T4SharedLinks");
hMapFile = OpenFileMapping (
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
szName); // name of mapping object
if (hMapFile == NULL)
{
printf ("-E-EMU414: Could not open file mapping object %s (%d).\n",
szName,
GetLastError());
return NULL;
}
addr = (LPTSTR) MapViewOfFile (hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
size);
if (addr == NULL)
{
printf ("-E-EMU414: Could not map view of file (%d).\n", GetLastError());
CloseHandle(hMapFile);
return NULL;
}
if (verbose)
printf ("-I-EMU414: Attached SHMLinks at #%p.\n", addr);
return addr;
}
int shlink_detach (void *addr)
{
UnmapViewOfFile (addr);
CloseHandle(hMapFile);
return 0;
}
void *shlink_alloc (const char *fnm, int size)
{
char szName[256];
void *addr = NULL;
strcpy (szName, "T4SharedLinks");
hMapFile = CreateFileMapping (
INVALID_HANDLE_VALUE, // use paging file
0, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
size, // maximum object size (low-order DWORD)
szName); // name of mapping object
if (hMapFile == NULL)
{
printf ("-E-EMU414: Could not create file mapping object %s (%d).\n",
szName,
GetLastError ());
return NULL;
}
addr = (LPTSTR) MapViewOfFile (hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
size);
if (addr == NULL)
{
printf ("-E-EMU414: Could not map view of file (%d).\n",
GetLastError ());
CloseHandle (hMapFile);
return NULL;
}
if (verbose)
printf ("-I-EMU414: Allocated SHMLinks at #%p.\n", addr);
return addr;
}
int shlink_free (void)
{
return 0;
}
#else
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
extern int verbose;
int shmid;
void* shlink_attach (const char *fnm, int size)
{
key_t shmkey;
int ret;
void *addr;
shmkey = ftok(fnm, 1984);
if (shmkey == -1)
{
ret = errno;
printf ("-E-EMU414: Cannot access %s (%s).\n", fnm, strerror (ret));
return NULL;
}
shmid = shmget (shmkey, 0, 0);
if (shmid == -1)
{
ret = errno;
printf ("-E-EMU414: Cannot get SHMLinks (%s).\n", strerror (ret));
return NULL;
}
addr = shmat (shmid, 0, 0);
if (addr == (void *)-1)
{
ret = errno;
printf ("-E-EMU414: Unable to attach SHMLinks (%s).\n", strerror (ret));
return NULL;
}
if (verbose)
printf ("-I-EMU414: Attached SHMLinks at #%p.\n", addr);
return addr;
}
int shlink_detach (void *addr)
{
shmdt (addr);
return 0;
}
void* shlink_alloc (const char *fnm, int size)
{
key_t shmkey;
struct shmid_ds sbuf;
void *addr;
shmkey = ftok (fnm, 1984);
if (shmkey == -1)
{
printf ("-E-EMU414: Cannot access %s (%s).\n", fnm, strerror (errno));
return NULL;
}
shmid = shmget (shmkey, 0, 0);
if (shmid != -1)
{
printf ("-E-EMU414: SHMLinks already exists.\n");
return NULL;
}
shmid = shmget (shmkey, size, (SHM_R|SHM_W|(SHM_R>>3) |(SHM_W>>3)|IPC_CREAT));
if (shmid == -1)
{
printf ("-E-EMU414: Unable to create SHMLinks (%s).\n", strerror (errno));
return NULL;
}
addr = shmat (shmid, 0, 0);
if (addr == (void *)-1)
{
printf ("-E-EMU414: Unable to attach to SHMLinks (%s).\n", strerror (errno));
shmctl (shmid, (IPC_RMID), &sbuf);
return NULL;
}
memset (addr, 0, size);
if (verbose)
printf ("-I-EMU414: Allocated SHMLinks at #%p.\n", addr);
return addr;
}
int shlink_free (void)
{
struct shmid_ds sbuf;
return shmctl (shmid, (IPC_RMID), &sbuf);
}
#endif
#endif