djumbai/src/djumbai_dequeue/djumbai_dequeue.c

67 lines
1.6 KiB
C

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "../../libs/protocol/protocol.h"
int main() {
// Change the root of the process so it doesn't have access to anything else.
chroot("/opt/djumbai/");
const char *message_queue_path = "/opt/djumbai/fifos/message_queue";
if (access(message_queue_path, F_OK) != -1) {
// FIFO exists, delete it
if (unlink(message_queue_path) == -1) {
perror("unlink");
exit(EXIT_FAILURE);
}
printf("Existing FIFO deleted.\n");
}
// Open the FIFO for reading
if (mkfifo(message_queue_path, 0420) == -1) {
perror("mkfifo");
exit(EXIT_FAILURE);
}
int message_queue_fd;
message_queue_fd = open(message_queue_path, O_RDONLY);
if (message_queue_fd == -1) {
if (errno == ENOENT) {
// FIFO does not exist
printf("FIFO '%s' does not exist. Exiting...\n", message_queue_path);
return 1;
} else {
perror("open");
return 1;
}
}
// Open the FIFO for writing
const char *send_fifo_path = "fifos/send_fifo";
int send_fifo_fd;
send_fifo_fd = open(send_fifo_path, O_WRONLY);
if (send_fifo_fd == -1) {
if (errno == ENOENT) {
// FIFO does not exist
printf("FIFO '%s' does not exist. Exiting...\n", send_fifo_path);
return 1;
} else {
perror("open");
return 1;
}
}
unsigned char buffer[MESSAGE_SIZE];
while(1){
read(message_queue_fd,buffer,MESSAGE_SIZE);
write(send_fifo_fd,buffer,MESSAGE_SIZE);
memset(buffer, 0, MESSAGE_SIZE);
}
close(message_queue_fd);
unlink(message_queue_path);
close(send_fifo_fd);
}