Done until djumbai-send
This commit is contained in:
parent
83bd6fb796
commit
da345fc422
8 changed files with 135 additions and 36 deletions
|
@ -21,18 +21,16 @@ int send_message(unsigned int sender, unsigned int receiver) {
|
|||
// Redirect stdin to read from pipe_to_child
|
||||
dup2(pipe_to_child[0], STDIN_FILENO);
|
||||
|
||||
|
||||
// Execute a command, for example, a simple "cat" command
|
||||
execlp("./bin/djumbai_client_send/djumbai_client_send", "djumbai_client_send", NULL);
|
||||
close(pipe_to_child[0]);
|
||||
|
||||
// If execlp fails
|
||||
perror("execlp");
|
||||
close(pipe_to_child[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
} else { // Parent process
|
||||
close(pipe_to_child[0]); // Close read end of pipe
|
||||
|
||||
printf("Please enter your message (Max of %ld bytes):\n", MAX_CONTENT_SIZE);
|
||||
printf("Please enter your message (Max of %ld bytes):\n", MAX_CONTENT_SIZE - 1);
|
||||
char content[MAX_CONTENT_SIZE];
|
||||
fgets(content, MAX_CONTENT_SIZE, stdin);
|
||||
|
||||
|
@ -42,13 +40,13 @@ int send_message(unsigned int sender, unsigned int receiver) {
|
|||
}
|
||||
|
||||
// Serialize the message
|
||||
unsigned char buffer[sizeof(struct Message)];
|
||||
if (serialize_message(&msg, sizeof(struct Message), buffer) == -1) {
|
||||
unsigned char buffer[MESSAGE_SIZE];
|
||||
if (serialize_message(&msg, MESSAGE_SIZE, buffer) == -1) {
|
||||
fprintf(stderr, "Error: Serialization failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
write(pipe_to_child[1], buffer, sizeof(buffer));
|
||||
write(pipe_to_child[1], buffer, MESSAGE_SIZE);
|
||||
|
||||
// Close the write end of the pipe
|
||||
close(pipe_to_child[1]);
|
||||
|
|
|
@ -33,8 +33,8 @@ int main() {
|
|||
} else { // Parent process
|
||||
close(pipe_to_child[0]); // Close read end of pipe
|
||||
|
||||
unsigned char buffer[sizeof(struct Message)];
|
||||
read(0, buffer, sizeof(struct Message));
|
||||
unsigned char buffer[MESSAGE_SIZE];
|
||||
read(0, buffer, MESSAGE_SIZE);
|
||||
write(pipe_to_child[1], buffer, sizeof(buffer));
|
||||
|
||||
// Close the write end of the pipe
|
||||
|
|
|
@ -1,8 +1,67 @@
|
|||
#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(){
|
||||
//Make stdin the FIFO
|
||||
//Read from FIFO
|
||||
//Make stdout the output FIFO
|
||||
//Write to another FIFO
|
||||
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 = "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);
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ int main() {
|
|||
//Change the root of the djumbai_enqueue process so it doesn't have access to anything else.
|
||||
chroot("/opt/djumbai/queue/");
|
||||
const char *message_queue_path =
|
||||
"mailqueue"; // Replace this with the path to your FIFO
|
||||
"mailqueue";
|
||||
|
||||
// Open the FIFO for writing
|
||||
int queue_fd;
|
||||
|
|
|
@ -1,3 +1,54 @@
|
|||
int main(){
|
||||
#include "../../libs/protocol/protocol.h"
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main() {
|
||||
|
||||
// Open the FIFO for writing
|
||||
const char *send_fifo_path = "fifos/send_fifo";
|
||||
if (access(send_fifo_path, F_OK) != -1) {
|
||||
// FIFO exists, delete it
|
||||
if (unlink(send_fifo_path) == -1) {
|
||||
perror("unlink");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("Existing FIFO deleted.\n");
|
||||
}
|
||||
|
||||
// Open the FIFO for reading
|
||||
if (mkfifo(send_fifo_path, 0420) == -1) {
|
||||
perror("mkfifo");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int send_fifo_fd;
|
||||
send_fifo_fd = open(send_fifo_path, O_RDONLY);
|
||||
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(send_fifo_fd, buffer, MESSAGE_SIZE);
|
||||
|
||||
// SPAWN THE CHILD PROCESS TO PUT THE MESSAGE IN THE USER'S QUEUE
|
||||
// CHECK IF THE MESSAGE IS DESTINED TO A GROUP
|
||||
// IF IT IS DESTINED TO A GROUP, START THE CHILD PROCESS WITH THE UID OF THE SENDER AND GID OF THE GROUP
|
||||
// THE SENDER MUST BE PART OF THE GROUP
|
||||
|
||||
memset(buffer, 0, MESSAGE_SIZE);
|
||||
}
|
||||
close(send_fifo_fd);
|
||||
unlink(send_fifo_path);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue