2020-12-12 16:40:16 +05:00
|
|
|
#include "raw.h"
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
static bool running = true;
|
|
|
|
|
|
|
|
static void signal_handler(const int signo)
|
|
|
|
{
|
|
|
|
if (signo == SIGINT) {
|
|
|
|
running = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
signal(SIGINT, signal_handler);
|
|
|
|
|
|
|
|
printf("Open shared memory objects.\n");
|
|
|
|
|
|
|
|
int buffer1_shm_id = shm_open(
|
|
|
|
"/buffer1",
|
|
|
|
O_CREAT | O_EXCL | O_RDWR,
|
|
|
|
S_IRUSR | S_IWUSR
|
|
|
|
);
|
|
|
|
|
2020-12-12 22:25:41 +05:00
|
|
|
assert(buffer1_shm_id != -1);
|
2020-12-12 16:40:16 +05:00
|
|
|
|
|
|
|
printf("Truncate shared memory objects.\n");
|
|
|
|
|
2020-12-12 22:25:41 +05:00
|
|
|
assert(ftruncate(buffer1_shm_id, BUFFER1_SIZE) == 0);
|
2020-12-12 16:40:16 +05:00
|
|
|
|
|
|
|
printf("Create memory mappings.\n");
|
|
|
|
|
2020-12-12 22:25:41 +05:00
|
|
|
struct Queue *const queue = mmap(
|
2020-12-12 16:40:16 +05:00
|
|
|
NULL,
|
|
|
|
BUFFER1_SIZE,
|
2020-12-12 22:25:41 +05:00
|
|
|
PROT_READ | PROT_WRITE,
|
2020-12-12 16:40:16 +05:00
|
|
|
MAP_SHARED,
|
|
|
|
buffer1_shm_id,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
2020-12-12 22:25:41 +05:00
|
|
|
assert(queue != MAP_FAILED);
|
2020-12-12 16:40:16 +05:00
|
|
|
|
|
|
|
printf("Initialize queues.\n");
|
|
|
|
|
2020-12-12 23:26:29 +05:00
|
|
|
queue->read_offset = 0;
|
2020-12-12 16:40:16 +05:00
|
|
|
|
|
|
|
printf("Main loop.\n");
|
|
|
|
|
|
|
|
while (running) {
|
2020-12-12 23:26:29 +05:00
|
|
|
const struct Message *const message = (struct Message*)queue->data + queue->read_offset;
|
2020-12-12 16:40:16 +05:00
|
|
|
|
|
|
|
if (message->magic != BUFFER1_MAGIC) {
|
|
|
|
printf("No messages.\n");
|
|
|
|
sleep(1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-12 22:25:41 +05:00
|
|
|
if (message->size > BUFFER1_SIZE - sizeof(struct Queue)) {
|
2020-12-12 16:40:16 +05:00
|
|
|
printf("Message too big.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-12-12 23:26:29 +05:00
|
|
|
if (message->size > BUFFER1_SIZE - sizeof(struct Queue) - queue->read_offset) {
|
2020-12-12 16:40:16 +05:00
|
|
|
printf("Buffer return.\n");
|
2020-12-12 23:26:29 +05:00
|
|
|
queue->read_offset = 0;
|
2020-12-12 16:40:16 +05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-12 23:26:29 +05:00
|
|
|
queue->read_offset += message->size;
|
2020-12-12 16:40:16 +05:00
|
|
|
|
|
|
|
switch (message->type) {
|
|
|
|
case FINISH:
|
|
|
|
printf("Message: finish.\n");
|
|
|
|
running = false;
|
|
|
|
break;
|
|
|
|
case ONEBYTE:
|
|
|
|
printf("Message: 1 byte (%u)\n", *(unsigned char*)message->data);
|
|
|
|
break;
|
|
|
|
case NULLSTR:
|
|
|
|
printf("Message: null-terminated string (%s)\n", message->data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("Invalid message.\n");
|
|
|
|
running = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Destroy memory mappings.\n");
|
|
|
|
|
2020-12-12 22:25:41 +05:00
|
|
|
assert(munmap(queue, BUFFER1_SIZE) == 0);
|
2020-12-12 16:40:16 +05:00
|
|
|
|
|
|
|
printf("Unlink shared memory objects.\n");
|
|
|
|
|
2020-12-12 22:25:41 +05:00
|
|
|
assert(shm_unlink("/buffer1") == 0);
|
2020-12-12 16:40:16 +05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|