-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipc_com.c
613 lines (510 loc) · 14.9 KB
/
ipc_com.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*
MIT License
Copyright (c) 2020 Johan Svensson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "ipc_com.h"
#include <fcntl.h>
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <semaphore.h>
#include <stdint.h>
#define MAX_NAME_LEN 50
#define OPEN_META_RETRIES 30
#define SHM_CREATE_OFLAGS (O_CREAT | O_EXCL |O_RDWR)
#define SHM_READ_OFLAGS (O_RDWR)
#define SHM_MODE (S_IRUSR | S_IWUSR)
#define MMAP_PROT (PROT_READ | PROT_WRITE)
#define MMAP_FLAGS (MAP_SHARED)
#ifdef DEBUG
#define log_debug(...) printf(__VA_ARGS__)
#else
#define log_debug(...)
#endif
struct ipc_occupied {
int allocated;
int sz;
};
struct ipc_fd_queue {
int fd_payload;
int fd_occupied;
int fd_locking;
int fd_queue_meta;
};
struct ipc_fds {
struct ipc_fd_queue write;
struct ipc_fd_queue read;
int fd_queue_meta;
};
struct ipc_queue_locking
{
sem_t sem_empty;
sem_t sem_full;
pthread_mutex_t mutex_lock;
};
struct ipc_queue_meta_queue
{
char file_payload[MAX_NAME_LEN+15];
char file_occupied[MAX_NAME_LEN+15];
char file_locking[MAX_NAME_LEN+15];
};
struct ipc_queue_meta
{
char file_queue_meta_read[MAX_NAME_LEN+15];
char file_queue_meta_write[MAX_NAME_LEN+15];
struct ipc_queue_meta_queue meta_server_read;
struct ipc_queue_meta_queue meta_server_write;
int queue_total_sz;
int queue_item_sz;
int queue_cnt;
int initialized;
};
struct ipc_queue_data
{
struct ipc_queue_locking *shm_locking; /* shm */
struct ipc_occupied *shm_occupied; /* shm */
void *shm_payload; /* shm */
int setup_finished; /* 1 = OK*/
};
struct ipc_data
{
struct ipc_fds fds;
char file_shm[MAX_NAME_LEN];
char file_queue_meta[MAX_NAME_LEN+15];
struct ipc_queue_meta *metadata; /* shm */
struct ipc_queue_data read;
struct ipc_queue_data write;
enum NODE_TYPE type;
int payload_sz;
int queue_cnt;
int total_sz;
callback cb;
pthread_t run_thread;
int run;
};
static int create_shm(int *fd, char *filename, void **set_addr, int sz)
{
int ret;
*fd = shm_open(filename, SHM_CREATE_OFLAGS, SHM_MODE);
if(*fd < 0) {
perror("could not shm_open");
return -1;
}
ret = ftruncate(*fd,sz);
if(ret) {
perror("could not shm_open");
goto error;
}
*set_addr = mmap(NULL, sz, MMAP_PROT, MMAP_FLAGS, *fd, 0);
return ret;
error:
close(*fd);
return ret;
}
static int open_shm(int *fd, char *filename, void **set_addr, int sz)
{
*fd = shm_open(filename, SHM_READ_OFLAGS, SHM_MODE);
if(*fd < 0) {
perror("could not shm_open");
return -1;
}
*set_addr = mmap(NULL, sz, MMAP_PROT, MMAP_FLAGS, *fd, 0);
return 0;
}
static void *thread_main(void *arg)
{
struct ipc_data *data = arg;
struct timespec ts;
int ret;
struct ipc_occupied *occupied_ptr;
/* here we wait */
while(data->run) {
if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
return NULL;
ts.tv_sec += 2;
log_debug("main() about to call sem_timedwait()\n");
while ((ret = sem_timedwait(&data->read.shm_locking->sem_empty, &ts)) == -1
&& errno == EINTR)
continue; /* Restart if interrupted by handler */
/* Check what happened */
if (ret == -1) {
if (errno == ETIMEDOUT) {
log_debug("sem_timedwait() timed out\n");
continue;
} else {
perror("sem_timedwait");
return NULL;
}
}
log_debug("sem_timedwait() succeeded\n");
/* do stuff */
pthread_mutex_lock(&data->read.shm_locking->mutex_lock);
log_debug("FINDING ITEM\n");
occupied_ptr = data->read.shm_occupied;
uint8_t *shm_payload = data->read.shm_payload;
for(int i = 0; i < data->queue_cnt; ++i) {
if(1 == occupied_ptr->allocated) {
/* item */
log_debug("CALLING CALLBACK\n");
data->cb(shm_payload, occupied_ptr->sz);
occupied_ptr->allocated = 0;
occupied_ptr->sz = 0;
if (sem_post(&data->read.shm_locking->sem_full) == -1)
perror("Error with sem_post");
}
shm_payload += data->payload_sz;
}
pthread_mutex_unlock(&data->read.shm_locking->mutex_lock);
/* end */
}
return NULL;
}
static int init_locking(struct ipc_data *data, struct ipc_queue_locking *lock)
{
int ret;
int shared = 1;
ret = sem_init(&lock->sem_empty, shared, 0);
if(ret)
return -1;
ret = sem_init(&lock->sem_full, shared, data->queue_cnt);
if(ret)
return -1;
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setrobust(&attr, 1);
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&lock->mutex_lock, &attr);
return 0;
}
static int create_and_init_queues(struct ipc_data *data)
{
int ret;
snprintf(data->metadata->meta_server_write.file_payload,
sizeof(data->metadata->meta_server_write.file_payload),
"%s_w_p", data->file_shm);
snprintf(data->metadata->meta_server_write.file_locking,
sizeof(data->metadata->meta_server_write.file_locking),
"%s_w_l", data->file_shm);
snprintf(data->metadata->meta_server_write.file_occupied,
sizeof(data->metadata->meta_server_write.file_occupied),
"%s_w_o", data->file_shm);
snprintf(data->metadata->meta_server_read.file_payload,
sizeof(data->metadata->meta_server_read.file_payload),
"%s_r_p", data->file_shm);
snprintf(data->metadata->meta_server_read.file_locking,
sizeof(data->metadata->meta_server_read.file_locking),
"%s_r_l", data->file_shm);
snprintf(data->metadata->meta_server_read.file_occupied,
sizeof(data->metadata->meta_server_read.file_occupied),
"%s_r_o", data->file_shm);
ret = create_shm(&data->fds.write.fd_payload,
data->metadata->meta_server_write.file_payload,
&data->write.shm_payload, data->payload_sz*data->queue_cnt);
if(ret)
return ret;
ret = create_shm(&data->fds.write.fd_locking,
data->metadata->meta_server_write.file_locking,
(void**)&data->write.shm_locking, sizeof(struct ipc_queue_locking));
if(ret)
return ret;
ret = create_shm(&data->fds.write.fd_occupied,
data->metadata->meta_server_write.file_occupied,
(void**)&data->write.shm_occupied,
sizeof(struct ipc_occupied)*data->queue_cnt);
if(ret)
return ret;
ret = create_shm(&data->fds.read.fd_payload,
data->metadata->meta_server_read.file_payload,
&data->read.shm_payload, data->payload_sz*data->queue_cnt);
if(ret)
return ret;
ret = create_shm(&data->fds.read.fd_locking,
data->metadata->meta_server_read.file_locking,
(void**)&data->read.shm_locking, sizeof(struct ipc_queue_locking));
if(ret)
return ret;
ret = create_shm(&data->fds.read.fd_occupied,
data->metadata->meta_server_read.file_occupied,
(void**)&data->read.shm_occupied,
sizeof(struct ipc_occupied)*data->queue_cnt);
if(ret)
return ret;
ret = init_locking(data, data->write.shm_locking);
if(ret)
return ret;
ret = init_locking(data, data->read.shm_locking);
return ret;
}
static int open_queues(struct ipc_data *data)
{
/* Clients do the opposite than server */
int ret;
ret = open_shm(&data->fds.read.fd_payload,
data->metadata->meta_server_read.file_payload,
&data->write.shm_payload, data->payload_sz*data->queue_cnt);
if(ret)
return ret;
ret = open_shm(&data->fds.read.fd_locking,
data->metadata->meta_server_read.file_locking,
(void**)&data->write.shm_locking, sizeof(struct ipc_queue_locking));
if(ret)
return ret;
ret = open_shm(&data->fds.read.fd_occupied,
data->metadata->meta_server_read.file_occupied,
(void**)&data->write.shm_occupied,
sizeof(struct ipc_occupied)*data->queue_cnt);
if(ret)
return ret;
ret = open_shm(&data->fds.write.fd_payload,
data->metadata->meta_server_write.file_payload,
&data->read.shm_payload, data->payload_sz*data->queue_cnt);
if(ret)
return ret;
ret = open_shm(&data->fds.write.fd_locking,
data->metadata->meta_server_write.file_locking,
(void**)&data->read.shm_locking, sizeof(struct ipc_queue_locking));
if(ret)
return ret;
ret = open_shm(&data->fds.write.fd_occupied,
data->metadata->meta_server_write.file_occupied,
(void**)&data->read.shm_occupied,
sizeof(struct ipc_occupied)*data->queue_cnt);
return ret;
}
static int open_meta(struct ipc_data *data)
{
int ret;
int retries = 0;
do {
usleep(10000); /* Give some time of there is some concurrency */
ret = open_shm(&data->fds.fd_queue_meta, data->file_queue_meta,
(void**)&data->metadata, sizeof(struct ipc_queue_meta));
retries++;
} while(-1 == ret && retries < OPEN_META_RETRIES);
if(ret)
return ret;
while(!data->metadata->initialized)
usleep(1000);
return ret;
}
static int create_and_init_meta(struct ipc_data *data)
{
int ret;
ret = create_shm(&data->fds.fd_queue_meta, data->file_queue_meta,
(void**)&data->metadata, sizeof(struct ipc_queue_meta));
if(ret)
return ret;
data->metadata->queue_item_sz = data->payload_sz;
data->metadata->queue_cnt = data->queue_cnt;
data->metadata->queue_total_sz = data->payload_sz * data->queue_cnt;
return ret;
}
static int init_meta(struct ipc_data *data)
{
int ret;
snprintf(data->file_queue_meta, sizeof(data->file_queue_meta),
"%s_meta", data->file_shm);
switch(data->type) {
case NODE_TYPE_CLIENT:
ret = open_meta(data);
if(
data->payload_sz != data->metadata->queue_item_sz ||
data->queue_cnt != data->metadata->queue_cnt
)
return -1;
if(ret)
return ret;
ret = open_queues(data);
if(ret)
return ret;
return 0;
case NODE_TYPE_SERVER:
ret = create_and_init_meta(data);
if(ret)
return ret;
create_and_init_queues(data);
if(ret)
return ret;
data->metadata->initialized = 1;
return 0;
default: return -1;
}
}
int ipc_com_init(struct ipc_data **data, callback cb, char *shm_file,
enum NODE_TYPE type, int payload_sz, int queue_cnt)
{
struct ipc_data *new_data;
int ret;
new_data = calloc(1, sizeof(*new_data));
if(!data)
return -ENOMEM;
new_data->run = 1; /* can be started running */
new_data->type = type;
new_data->payload_sz = payload_sz;
new_data->queue_cnt = queue_cnt;
new_data->total_sz = payload_sz * queue_cnt;
new_data->cb = cb;
snprintf(new_data->file_shm, MAX_NAME_LEN, "/%s", shm_file);
ret = init_meta(new_data);
if(ret) {
perror("ERROR HERE");
return ret;
}
*data = new_data;
return 0;
}
int ipc_com_destroy(struct ipc_data *data)
{
if(data->type == NODE_TYPE_CLIENT) {
free(data);
return 0;
}
/* when server is going down, everything is freed */
shm_unlink(data->metadata->meta_server_write.file_payload);
close(data->fds.write.fd_payload);
shm_unlink(data->metadata->meta_server_write.file_locking);
close(data->fds.write.fd_locking);
shm_unlink(data->metadata->meta_server_write.file_occupied);
close(data->fds.write.fd_occupied);
shm_unlink(data->metadata->meta_server_read.file_payload);
close(data->fds.read.fd_payload);
shm_unlink(data->metadata->meta_server_read.file_locking);
close(data->fds.write.fd_locking);
shm_unlink(data->metadata->meta_server_read.file_occupied);
close(data->fds.write.fd_occupied);
shm_unlink(data->metadata->file_queue_meta_write);
close(data->fds.write.fd_queue_meta);
shm_unlink(data->metadata->file_queue_meta_read);
close(data->fds.read.fd_queue_meta);
shm_unlink(data->file_queue_meta);
close(data->fds.fd_queue_meta);
free(data);
return 0;
}
int ipc_com_start(struct ipc_data *data)
{
int ret;
ret = pthread_create(&data->run_thread, NULL, &thread_main, data);
return ret;
}
int ipc_com_stop(struct ipc_data *data)
{
int ret;
data->run = 0;
ret = pthread_join(data->run_thread, NULL);
return ret;
}
int ipc_com_item_send(struct ipc_data *data, void *payload, int sz)
{
int ret;
struct ipc_occupied *occupied_ptr;
ret = sem_wait(&data->write.shm_locking->sem_full);
pthread_mutex_lock(&data->write.shm_locking->mutex_lock);
if(sz > data->payload_sz)
abort();
occupied_ptr = data->write.shm_occupied;
uint8_t *shm_payload = data->write.shm_payload;
for(int i = 0; i < data->queue_cnt; ++i) {
if(0 == occupied_ptr->allocated) {
/* empty */
occupied_ptr->allocated = 1;
occupied_ptr->sz = sz;
memcpy(shm_payload, payload, sz);
break;
}
shm_payload += data->payload_sz;
}
pthread_mutex_unlock(&data->write.shm_locking->mutex_lock);
ret = sem_post(&data->write.shm_locking->sem_empty);
return ret;
}
//int ipc_com_item_reserve(struct ipc_data *data, void *payload, int *msgSize)
//{
// int ret;
// struct ipc_occupied *occupied_ptr;
// ret = sem_wait(&data->write.shm_locking->sem_full);
// pthread_mutex_lock(&data->write.shm_locking->mutex_lock);
//
// occupied_ptr = data->write.shm_occupied;
// uint8_t *shm_payload = data->write.shm_payload;
// for(int i = 0; i < data->queue_cnt; ++i) {
// if(0 == occupied_ptr->allocated) {
// /* empty */
// occupied_ptr->allocated = 1;
// occupied_ptr->sz = sz;
// memcpy(shm_payload, payload, sz);
// break;
// }
// shm_payload += data->payload_sz;
// }
// pthread_mutex_unlock(&data->write.shm_locking->mutex_lock);
// ret = sem_post(&data->write.shm_locking->sem_empty);
// return ret;
//}
// This is just for testing
/*
static void cb_method(void *payload, int sz)
{
printf("CALLBACK FUNCTION\n");
}
int main(int argc, char * argv[])
{
struct ipc_data *data;
int ret;
enum NODE_TYPE nt = NODE_TYPE_SERVER;
if(argc == 2)
nt = NODE_TYPE_CLIENT;
ret = ipc_com_init(&data, &cb_method, "server_name", nt, 10, 2);
if(ret) {
printf("init failed...\n");
return -1;
}
printf("OL\n");
switch(nt){
case NODE_TYPE_CLIENT: {
uint8_t payload = 0xfe;
int sz = sizeof(payload);
for(int i = 0 ; i < 100 ; ++i) {
printf("Sending %d\n", i);
ipc_com_item_send(data, &payload, sz);
}
}
break;
case NODE_TYPE_SERVER: {
ipc_com_start(data);
for(int i = 0; i < 3; ++i){
usleep(10000000);
}
}
break;
}
ipc_com_stop(data);
ipc_com_destroy(data);
// int ret;
// ret = init();
// if(ret)
// return ret;
}
*/