-
Notifications
You must be signed in to change notification settings - Fork 43
/
shmem.c
451 lines (407 loc) · 11.8 KB
/
shmem.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/mman.h>
#include <errno.h>
#include <pthread.h>
#ifdef __ANDROID__
#include <android/log.h>
#include "sys/shm.h"
#ifdef NDEBUG
#define DBG(...) do {} while (0)
#else
#define DBG(...) __android_log_print(ANDROID_LOG_INFO, "shmem", __VA_ARGS__)
#endif
#else
#include <sys/shm.h>
#define DBG(format, ...) fprintf(stderr, format "\n", __VA_ARGS__)
#endif
#include "libancillary/ancillary.h"
#include "cutils/ashmem.h"
static pthread_t listening_thread_id = 0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
typedef struct
{
int id;
void *addr;
int descriptor;
size_t size;
int markedForDeletion;
} shmem_t;
static shmem_t *shmem = NULL;
static size_t shmem_amount = 0;
static size_t shmem_counter = 0;
static int sock = 0;
static int sockid = 0;
#define SOCKNAME "/dev/shm/%08x"
#define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
static int get_shmid(unsigned int index)
{
return sockid * 0x10000 + index;
}
static int get_sockid(int shmid)
{
return shmid / 0x10000;
}
static unsigned int get_index(int shmid)
{
return shmid % 0x10000;
}
static int shm_find_id(int shmid)
{
int i;
for (i = 0; i < shmem_amount; i++)
{
if (shmem[i].id == shmid)
return i;
}
DBG ("%s: cannot find shmid %x", __PRETTY_FUNCTION__, shmid);
return -1;
}
static void *listening_thread(void * arg)
{
struct sockaddr_un addr;
socklen_t len = sizeof(addr);
int sendsock;
DBG ("%s: thread started", __PRETTY_FUNCTION__);
while ((sendsock = accept (sock, (struct sockaddr *)&addr, &len)) != -1)
{
unsigned int shmid;
int idx;
if (recv (sendsock, &idx, sizeof(idx), 0) != sizeof(idx))
{
DBG ("%s: ERROR: recv() returned not %d bytes", __PRETTY_FUNCTION__, (int)sizeof(idx));
close (sendsock);
continue;
}
pthread_mutex_lock (&mutex);
shmid = get_shmid (idx);
idx = shm_find_id (shmid);
if (idx != -1)
{
if (ancil_send_fd (sendsock, shmem[idx].descriptor) != 0)
DBG ("%s: ERROR: ancil_send_fd() failed: %s", __PRETTY_FUNCTION__, strerror(errno));
}
else
DBG ("%s: ERROR: cannot find shmid 0x%x", __PRETTY_FUNCTION__, shmid);
pthread_mutex_unlock (&mutex);
close (sendsock);
len = sizeof(addr);
}
DBG ("%s: ERROR: listen() failed, thread stopped", __PRETTY_FUNCTION__);
return NULL;
}
/* Get shared memory segment. */
int shmget (key_t key, size_t size, int flags)
{
char buf[256];
int idx;
DBG ("%s: key %d size %zu flags 0%o (flags are ignored)", __PRETTY_FUNCTION__, key, size, flags);
if (key != IPC_PRIVATE)
{
DBG ("%s: key %d != IPC_PRIVATE, this is not supported", __PRETTY_FUNCTION__, key);
errno = EINVAL;
return -1;
}
if (!listening_thread_id)
{
int i;
sock = socket (AF_UNIX, SOCK_STREAM, 0);
if (!sock)
{
DBG ("%s: cannot create UNIX socket: %s", __PRETTY_FUNCTION__, strerror(errno));
errno = EINVAL;
return -1;
}
for (i = 0; i < 4096; i++)
{
struct sockaddr_un addr;
int len;
memset (&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
sockid = (getpid() + i) & 0xffff;
sprintf (&addr.sun_path[1], SOCKNAME, sockid);
len = sizeof(addr.sun_family) + strlen(&addr.sun_path[1]) + 1;
if (bind (sock, (struct sockaddr *)&addr, len) != 0)
{
//DBG ("%s: cannot bind UNIX socket %s: %s, trying next one, len %d", __PRETTY_FUNCTION__, &addr.sun_path[1], strerror(errno), len);
continue;
}
DBG ("%s: bound UNIX socket %s", __PRETTY_FUNCTION__, addr.sun_path + 1);
break;
}
if (i == 4096)
{
DBG ("%s: cannot bind UNIX socket, bailing out", __PRETTY_FUNCTION__);
sockid = 0;
errno = ENOMEM;
return -1;
}
if (listen (sock, 4) != 0)
{
DBG ("%s: listen failed", __PRETTY_FUNCTION__);
errno = ENOMEM;
return -1;
}
pthread_create (&listening_thread_id, NULL, &listening_thread, NULL);
}
pthread_mutex_lock (&mutex);
idx = shmem_amount;
sprintf (buf, SOCKNAME "-%d", sockid, idx);
shmem_amount ++;
shmem_counter = (shmem_counter + 1) & 0x7fff;
size_t shmid = shmem_counter;
shmem = realloc (shmem, shmem_amount * sizeof(shmem_t));
size = ROUND_UP(size, getpagesize ());
shmem[idx].size = size;
shmem[idx].descriptor = ashmem_create_region (buf, size);
shmem[idx].addr = NULL;
shmem[idx].id = get_shmid(shmid);
shmem[idx].markedForDeletion = 0;
if (shmem[idx].descriptor < 0)
{
DBG ("%s: ashmem_create_region() failed for size %zu: %s", __PRETTY_FUNCTION__, size, strerror(errno));
shmem_amount --;
shmem = realloc (shmem, shmem_amount * sizeof(shmem_t));
pthread_mutex_unlock (&mutex);
return -1;
}
DBG ("%s: ID %d shmid %x FD %d size %zu", __PRETTY_FUNCTION__, idx, get_shmid(shmid), shmem[idx].descriptor, shmem[idx].size);
/*
status = ashmem_set_prot_region (shmem[idx].descriptor, 0666);
if (status < 0)
{
DBG ("%s: ashmem_set_prot_region() failed for size %zu: %s %d", __PRETTY_FUNCTION__, size, strerror(status), status);
shmem_amount --;
shmem = realloc (shmem, shmem_amount * sizeof(shmem_t));
pthread_mutex_unlock (&mutex);
return -1;
}
*/
/*
status = ashmem_pin_region (shmem[idx].descriptor, 0, shmem[idx].size);
if (status < 0)
{
DBG ("%s: ashmem_pin_region() failed for size %zu: %s %d", __PRETTY_FUNCTION__, size, strerror(status), status);
shmem_amount --;
shmem = realloc (shmem, shmem_amount * sizeof(shmem_t));
pthread_mutex_unlock (&mutex);
return -1;
}
*/
pthread_mutex_unlock (&mutex);
return get_shmid(shmid);
}
/* Attach shared memory segment. */
void *shmat (int shmid, const void *shmaddr, int shmflg)
{
int idx;
int sid = get_sockid (shmid);
void *addr;
DBG ("%s: shmid %x shmaddr %p shmflg %d", __PRETTY_FUNCTION__, shmid, shmaddr, shmflg);
if (shmaddr != NULL)
{
DBG ("%s: shmaddr != NULL not supported", __PRETTY_FUNCTION__);
errno = EINVAL;
return (void *)-1;
}
pthread_mutex_lock (&mutex);
idx = shm_find_id (shmid);
if (idx == -1 && sid != sockid)
{
struct sockaddr_un addr;
int addrlen;
int recvsock;
int descriptor;
int size;
pthread_mutex_unlock (&mutex);
DBG ("%s: sockid %x", __PRETTY_FUNCTION__, sid);
idx = get_index (shmid);
memset (&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
sprintf (&addr.sun_path[1], SOCKNAME, sid);
addrlen = sizeof(addr.sun_family) + strlen(&addr.sun_path[1]) + 1;
DBG ("%s: addr %s", __PRETTY_FUNCTION__, &addr.sun_path[1]);
recvsock = socket (AF_UNIX, SOCK_STREAM, 0);
if (!recvsock)
{
DBG ("%s: cannot create UNIX socket: %s", __PRETTY_FUNCTION__, strerror(errno));
errno = EINVAL;
return (void *)-1;
}
if (connect (recvsock, (struct sockaddr *)&addr, addrlen) != 0)
{
DBG ("%s: cannot connect to UNIX socket %s: %s, len %d", __PRETTY_FUNCTION__, addr.sun_path + 1, strerror(errno), addrlen);
close (recvsock);
errno = EINVAL;
return (void *)-1;
}
DBG ("%s: connected to socket %s", __PRETTY_FUNCTION__, &addr.sun_path[1]);
if (send (recvsock, &idx, sizeof(idx), 0) != sizeof(idx))
{
DBG ("%s: send() failed on socket %s: %s", __PRETTY_FUNCTION__, addr.sun_path + 1, strerror(errno));
close (recvsock);
errno = EINVAL;
return (void *)-1;
}
if (ancil_recv_fd (recvsock, &descriptor) != 0)
{
DBG ("%s: ERROR: ancil_recv_fd() failed on socket %s: %s", __PRETTY_FUNCTION__, addr.sun_path + 1, strerror(errno));
close (recvsock);
errno = EINVAL;
return (void *)-1;
}
close (recvsock);
DBG ("%s: got FD %d", __PRETTY_FUNCTION__, descriptor);
size = ashmem_get_size_region(descriptor);
if (size == 0 || size == -1)
{
DBG ("%s: ERROR: ashmem_get_size_region() returned %d on socket %s: %s", __PRETTY_FUNCTION__, size, addr.sun_path + 1, strerror(errno));
errno = EINVAL;
return (void *)-1;
}
DBG ("%s: got size %d", __PRETTY_FUNCTION__, size);
pthread_mutex_lock (&mutex);
idx = shmem_amount;
shmem_amount ++;
shmem = realloc (shmem, shmem_amount * sizeof(shmem_t));
shmem[idx].id = shmid;
shmem[idx].descriptor = descriptor;
shmem[idx].size = size;
shmem[idx].addr = NULL;
shmem[idx].markedForDeletion = 0;
DBG ("%s: created new remote shmem ID %d shmid %x FD %d size %zu", __PRETTY_FUNCTION__, idx, shmid, shmem[idx].descriptor, shmem[idx].size);
}
if (idx == -1)
{
DBG ("%s: shmid %x does not exist", __PRETTY_FUNCTION__, shmid);
pthread_mutex_unlock (&mutex);
errno = EINVAL;
return (void *)-1;
}
if (shmem[idx].addr == NULL)
{
shmem[idx].addr = mmap(NULL, shmem[idx].size, PROT_READ | (shmflg == 0 ? PROT_WRITE : 0), MAP_SHARED, shmem[idx].descriptor, 0);
if (shmem[idx].addr == MAP_FAILED)
{
DBG ("%s: mmap() failed for ID %x FD %d: %s", __PRETTY_FUNCTION__, idx, shmem[idx].descriptor, strerror(errno));
shmem[idx].addr = NULL;
}
}
addr = shmem[idx].addr;
DBG ("%s: mapped addr %p for FD %d ID %d", __PRETTY_FUNCTION__, addr, shmem[idx].descriptor, idx);
pthread_mutex_unlock (&mutex);
return addr ? addr : (void *)-1;
}
static void delete_shmem(int idx)
{
if (shmem[idx].descriptor)
close (shmem[idx].descriptor);
shmem_amount --;
memmove (&shmem[idx], &shmem[idx+1], (shmem_amount - idx) * sizeof(shmem_t));
}
/* Detach shared memory segment. */
int shmdt (const void *shmaddr)
{
pthread_mutex_lock (&mutex);
int i;
for (i = 0; i < shmem_amount; i++)
{
if (shmem[i].addr == shmaddr)
{
if (munmap (shmem[i].addr, shmem[i].size) != 0)
DBG ("%s: munmap %p failed", __PRETTY_FUNCTION__, shmaddr);
shmem[i].addr = NULL;
DBG ("%s: unmapped addr %p for FD %d ID %d shmid %x", __PRETTY_FUNCTION__, shmaddr, shmem[i].descriptor, i, shmem[i].id);
if (shmem[i].markedForDeletion || get_sockid (shmem[i].id) != sockid)
{
DBG ("%s: deleting shmid %x", __PRETTY_FUNCTION__, shmem[i].id);
delete_shmem(i);
}
pthread_mutex_unlock (&mutex);
return 0;
}
}
pthread_mutex_unlock (&mutex);
DBG ("%s: invalid address %p", __PRETTY_FUNCTION__, shmaddr);
errno = EINVAL;
return -1;
}
static int shm_remove (int shmid)
{
int idx;
DBG ("%s: deleting shmid %x", __PRETTY_FUNCTION__, shmid);
pthread_mutex_lock (&mutex);
idx = shm_find_id (shmid);
if (idx == -1)
{
DBG ("%s: ERROR: shmid %x does not exist", __PRETTY_FUNCTION__, shmid);
pthread_mutex_unlock (&mutex);
errno = EINVAL;
return -1;
}
if (shmem[idx].addr)
{
DBG ("%s: shmid %x is still mapped to addr %p, it will be deleted on shmdt() call", __PRETTY_FUNCTION__, shmid, shmem[idx].addr);
// KDE lib creates shared memory segment, marks it for deletion, and then uses it as if it's not deleted
shmem[idx].markedForDeletion = 1;
pthread_mutex_unlock (&mutex);
return 0;
}
delete_shmem(idx);
pthread_mutex_unlock (&mutex);
return 0;
}
static int shm_stat (int shmid, struct shmid_ds *buf)
{
int idx;
pthread_mutex_lock (&mutex);
idx = shm_find_id (shmid);
if (idx == -1)
{
DBG ("%s: ERROR: shmid %x does not exist", __PRETTY_FUNCTION__, shmid);
pthread_mutex_unlock (&mutex);
errno = EINVAL;
return -1;
}
if (!buf)
{
DBG ("%s: ERROR: buf == NULL for shmid %x", __PRETTY_FUNCTION__, shmid);
pthread_mutex_unlock (&mutex);
errno = EINVAL;
return -1;
}
/* Report max permissive mode */
memset (buf, 0, sizeof(struct shmid_ds));
buf->shm_segsz = shmem[idx].size;
buf->shm_nattch = 1;
buf->shm_perm.__key = IPC_PRIVATE;
buf->shm_perm.uid = geteuid();
buf->shm_perm.gid = getegid();
buf->shm_perm.cuid = geteuid();
buf->shm_perm.cgid = getegid();
buf->shm_perm.mode = 0666;
buf->shm_perm.__seq = 1;
DBG ("%s: shmid %x size %d", __PRETTY_FUNCTION__, shmid, (int)buf->shm_segsz);
pthread_mutex_unlock (&mutex);
return 0;
}
/* Shared memory control operation. */
int shmctl (int shmid, int cmd, struct shmid_ds *buf)
{
//DBG ("%s: shmid %x cmd %d buf %p", __PRETTY_FUNCTION__, shmid, cmd, buf);
if (cmd == IPC_RMID)
{
return shm_remove (shmid);
}
if (cmd == IPC_STAT)
{
return shm_stat (shmid, buf);
}
DBG ("%s: cmd %d not implemented yet!", __PRETTY_FUNCTION__, cmd);
errno = EINVAL;
return -1;
}