-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpsm_linux.c
More file actions
323 lines (244 loc) · 5.65 KB
/
psm_linux.c
File metadata and controls
323 lines (244 loc) · 5.65 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
** SPDX-License-Identifier: MIT
**
** Copyright (c) 2019 Toshiba Corporation
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <signal.h>
#include <errno.h>
#include <pthread.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include "psm.h"
static pthread_mutex_t psm_process_lock = PTHREAD_MUTEX_INITIALIZER;
#ifdef TESTLOG
#define LOG( ...) logging(__VA_ARGS__)
#else
#define LOG(...) do {} while(0)
#endif
static void logging(const char *format, ...) {
va_list va;
printf("[%d] ", getpid());
va_start(va, format);
vprintf(format, va);
va_end(va);
}
static void resetPSMHandle(PSMHandle h) {
LOG(" resetPSMHandle\n");
h->fd = -1;
h->pMap = NULL;
h->name[0] = '\0';
h->pAllocator = NULL;
h->length = 0;
h->proc = getpid();
}
static int openPSM(PSMHandle h, const char *name) {
int ret = 0;
LOG(" openPSM\n", h, name);
h->fd = open(name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (h->fd == -1) ret = 1;
LOG(" openPSM open fd %d\n", h->fd);
LOG(" openPSM ret %d\n", ret);
return ret;
}
static void closePSM(PSMHandle h) {
LOG(" closePSM\n");
if (h->fd != -1) {
LOG(" closePSM close fd %d\n", h->fd);
close(h->fd);
}
}
static void dupPSMHandle(PSMHandle h, PSMHandle rh) {
LOG(" dupPSMHandle\n");
int fd_tmp = h->fd;
memcpy(h, rh, sizeof(struct PSMHandleTag));
h->fd = fd_tmp;
}
static int mapPSM(PSMHandle h, size_t maplen, void *pReqAddress) {
int ret = 0;
LOG(" mapPSM\n");
h->pMap = mmap(pReqAddress, maplen, PROT_READ | PROT_WRITE, MAP_SHARED, h->fd, 0);
LOG(" mapPSM mmap %p\n", h->pMap);
if (h->pMap == MAP_FAILED) {
ret = 1;
h->pMap = NULL;
}
h->length = maplen;
if (pReqAddress != NULL && h->pMap != pReqAddress) {
LOG(" mapPSM cannot map into expected address\n");
LOG(" mapPSM munmap %p\n", h->pMap);
munmap(h->pMap, h->length);
h->pMap = NULL;
ret = 1;
}
return ret;
}
static void unmapPSM(PSMHandle h) {
LOG(" unmapPSM\n");
if (h->pMap != NULL) {
LOG(" unmapPSM munmap %p\n", h->pMap);
LOG(" unmapPSM len %ld\n", h->length);
munmap(h->pMap, h->length);
h->pMap = NULL;
}
}
static uint32_t getProcessID(PSMProcess *proc) {
return *proc;
}
static int lockPSM(PSMHandle h) {
struct flock flk;
int ret = 0;
int tret;
LOG(" lockPSM\n");
pthread_mutex_lock(&psm_process_lock);
memset(&flk, 0, sizeof(flk));
flk.l_type = F_WRLCK;
flk.l_whence = SEEK_SET;
flk.l_start = 0;
flk.l_len = 0;
tret = fcntl(h->fd, F_SETLKW, &flk);
if (tret < 0) ret = 1;
LOG(" lockPSM ret %d\n", ret);
return ret;
}
static int unlockPSM(PSMHandle h) {
struct flock flk;
int ret = 0;
LOG(" unlockPSM\n");
memset(&flk, 0, sizeof(flk));
flk.l_type = F_UNLCK;
flk.l_whence = SEEK_SET;
flk.l_start = 0;
flk.l_len = 0;
ret = fcntl(h->fd, F_SETLK, &flk);
if (ret < 0) ret = 1;
pthread_mutex_unlock(&psm_process_lock);
LOG(" unlockPSM result %d\n", ret);
return ret;
}
/* Prepare file size. */
static int preparePSMFile(PSMHandle h, size_t size) {
off_t pos;
ssize_t wsize;
char c = 0;
off_t org_pos;
int ret = 0;
LOG(" preparePSMFile\n", h, size);
org_pos = lseek(h->fd, 0, SEEK_CUR);
if (org_pos == -1) ret = 1;
pos = lseek(h->fd, size-1, SEEK_SET);
if (pos == -1) ret = 1;
wsize = write(h->fd, &c, sizeof(char));
if (wsize != sizeof(char)) ret = 1;
pos = lseek(h->fd, org_pos, SEEK_SET);
if (pos == -1) ret = 1;
ret = 0;
LOG(" preparePSMFile ret %d\n", ret);
return ret;
}
/* Empty file. */
static int emptyPSMFile(PSMHandle h) {
int ret = 0;
int tret;
LOG(" emptyPSMFile\n");
tret = ftruncate(h->fd, 0);
if (tret == -1) ret = 1;
LOG(" emptyPSMFile ret %d\n", ret);
return ret;
}
/* Remove file. */
static int removePSMFile(PSMHandle h) {
int ret = 0;
int tret;
LOG(" removePSMFile\n");
tret = remove(h->name);
if (tret == -1) ret = 1;
LOG(" removePSMFile ret %d\n", ret);
return ret;
}
/* Get file size from file descriptor. */
static int getPSMFileSize(PSMHandle h, size_t *size) {
int ret = 0;
struct stat st;
LOG(" getPSMFileSize\n");
assert( size );
if (stat(h->name, &st) != 0) {
ret = 1;
} else if (!S_ISREG(st.st_mode)) {
ret = 1;
}
*size = st.st_size;
LOG(" getPSMFileSize ret %d\n", ret);
return ret;
}
static void getCurrentProcess(PSMProcess* pProc) {
*pProc = getpid();
}
static int isEqualProcess(PSMProcess proc1, PSMProcess proc2) {
int ret = 0;
if (proc1 == proc2)
ret = 1;
return ret;
}
static int isExistProcess(PSMProcess proc) {
int ret = kill(proc, 0);
if (ret == 0) {
ret = 1;
} else {
assert(errno == ESRCH);
ret = 0;
}
return ret;
}
static void copyString(char *dest, const char *src, size_t n) {
strncpy(dest, src, n);
dest[n-1] = '\0';
}
static int normalizePath(char *dest, const char *src, size_t dn) {
int ret = 0;
char *rpath = NULL;
LOG(" normalizePath\n");
rpath = realpath(src, NULL);
if (rpath == NULL) {
/* error to get realpath */
ret = 1;
} else {
size_t len = strlen(rpath) + 1;
if (len > dn) {
/* too long name */
ret = 1;
} else {
memcpy(dest, rpath, len);
}
free(rpath);
}
LOG(" normalizePath result %d\n", ret);
return ret;
}
PSM_EXPORT PSMPlatformFunctionsType PSMPlatformFunctions = {
resetPSMHandle,
lockPSM,
unlockPSM,
preparePSMFile,
emptyPSMFile,
removePSMFile,
getPSMFileSize,
openPSM,
closePSM,
dupPSMHandle,
mapPSM,
unmapPSM,
copyString,
normalizePath,
getProcessID,
getCurrentProcess,
isEqualProcess,
isExistProcess,
logging,
};