Compare commits

..

4 Commits
main ... API

Author SHA1 Message Date
e0b69cfea1 Improved ui socket and made open and create send requests. 2024-12-25 17:07:52 +01:00
3cbe520916 Edited Makefile to compite ui socket. 2024-12-25 17:06:56 +01:00
e2014f03f1 Basic socket communication 2024-12-25 11:00:35 +01:00
dadcc6476b Added the ui-socket.h.
Two new issues to solve:

* Should the ui communication component also be the one that manages
  permissions?
* The format of data sent (protocol) needs definition.
2024-12-20 08:48:21 +01:00
4 changed files with 172 additions and 2 deletions

View File

@ -22,7 +22,7 @@ endif
SOURCES_DIR := ./sources SOURCES_DIR := ./sources
BUILD_DIR := ./build BUILD_DIR := ./build
build: $(BUILD_DIR)/main.o $(BUILD_DIR)/sourcefs.o build: $(BUILD_DIR)/main.o $(BUILD_DIR)/sourcefs.o $(BUILD_DIR)/ui-socket.o
$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $(BUILD_DIR)/icfs $(CC) $(CFLAGS) $^ $(LDFLAGS) -o $(BUILD_DIR)/icfs
$(BUILD_DIR)/main.o: $(SOURCES_DIR)/main.c $(BUILD_DIR)/main.o: $(SOURCES_DIR)/main.c
@ -31,5 +31,8 @@ $(BUILD_DIR)/main.o: $(SOURCES_DIR)/main.c
$(BUILD_DIR)/sourcefs.o: $(SOURCES_DIR)/sourcefs.c $(SOURCES_DIR)/sourcefs.h $(BUILD_DIR)/sourcefs.o: $(SOURCES_DIR)/sourcefs.c $(SOURCES_DIR)/sourcefs.h
$(CC) $(CFLAGS) -c $< $(LDFLAGS) -o $@ $(CC) $(CFLAGS) -c $< $(LDFLAGS) -o $@
$(BUILD_DIR)/ui-socket.o: $(SOURCES_DIR)/ui-socket.c $(SOURCES_DIR)/ui-socket.h
$(CC) $(CFLAGS) -c $< $(LDFLAGS) -o $@
clean: clean:
rm $(BUILD_DIR)/* rm $(BUILD_DIR)/*

View File

@ -49,6 +49,7 @@
#include <sys/file.h> /* flock(2) */ #include <sys/file.h> /* flock(2) */
#include "sourcefs.h" #include "sourcefs.h"
#include "ui-socket.h"
const char *mountpoint = NULL; const char *mountpoint = NULL;
@ -360,6 +361,16 @@ static int xmp_create(const char *path, mode_t mode,
struct fuse_file_info *fi) { struct fuse_file_info *fi) {
int fd; int fd;
struct process_info pi;
struct fuse_context *fc = fuse_get_context();
pi.PID = fc->pid;
pi.UID = fc->uid;
pi.name = "";
printf("%d", ask_access(path, pi));
fd = source_create(path, fi->flags, mode); fd = source_create(path, fi->flags, mode);
if (fd == -1) if (fd == -1)
return -errno; return -errno;
@ -371,6 +382,16 @@ static int xmp_create(const char *path, mode_t mode,
static int xmp_open(const char *path, struct fuse_file_info *fi) { static int xmp_open(const char *path, struct fuse_file_info *fi) {
int fd; int fd;
struct process_info pi;
struct fuse_context *fc = fuse_get_context();
pi.PID = fc->pid;
pi.UID = fc->uid;
pi.name = "";
printf("%d", ask_access(path, pi));
fd = source_open(path, fi->flags); fd = source_open(path, fi->flags);
if (fd == -1) if (fd == -1)
return -errno; return -errno;
@ -663,7 +684,13 @@ int main(int argc, char *argv[]) {
int ret = source_init(mountpoint); int ret = source_init(mountpoint);
if (ret != 0) { if (ret != 0) {
perror("Failed to initialize filesystem."); perror("source_init");
exit(EXIT_FAILURE);
}
ret = init_ui_socket("/home/fedir/.icfs-sock");
if (ret != 0) {
perror("init_ui_socket");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }

116
sources/ui-socket.c Normal file
View File

@ -0,0 +1,116 @@
#define _GNU_SOURCE
#include "ui-socket.h"
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#define MAX_MESSAGE_SIZE 1024
// Mutex for thread safety
static pthread_mutex_t socket_mutex = PTHREAD_MUTEX_INITIALIZER;
static int ui_socket_fd = -1; // Global socket file descriptor
int init_ui_socket(const char *filename) {
if (!filename) {
return -1;
}
// Create the socket
ui_socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (ui_socket_fd == -1) {
perror("socket");
return -1;
}
// Remove the socket file if it already exists
if (unlink(filename) == -1 &&
errno != ENOENT) { // ENOENT means the file does not exist, which is fine
perror("unlink");
close(ui_socket_fd);
ui_socket_fd = -1;
return -1;
}
// Set up the socket address structure
struct sockaddr_un addr;
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, filename, sizeof(addr.sun_path) - 1);
// Bind the socket
if (bind(ui_socket_fd, (struct sockaddr *)&addr,
sizeof(struct sockaddr_un)) == -1) {
perror("bind");
close(ui_socket_fd);
ui_socket_fd = -1;
return -1;
}
// Listen for incoming connections
if (listen(ui_socket_fd, 5) == -1) {
perror("listen");
close(ui_socket_fd);
ui_socket_fd = -1;
return -1;
}
return 0;
}
int ask_access(const char *filename, struct process_info pi) {
if (!filename || ui_socket_fd == -1) {
return -1;
}
// Lock the mutex for thread safety
pthread_mutex_lock(&socket_mutex);
int client_fd = accept(ui_socket_fd, NULL, NULL);
if (client_fd == -1) {
perror("accept");
pthread_mutex_unlock(&socket_mutex);
return -1;
}
// Prepare the message to send to the GUI
char message[MAX_MESSAGE_SIZE];
int len_filename = strlen(filename);
int len_name = strlen(pi.name);
snprintf(message, sizeof(message), "r%04d%s%04d%04d%s%04d", len_filename,
filename, pi.PID, len_name, pi.name, pi.UID);
// Send the message to the GUI
if (send(client_fd, message, strlen(message), 0) == -1) {
perror("send");
close(client_fd);
pthread_mutex_unlock(&socket_mutex);
return -1;
}
// Receive the response from the GUI
char response[2];
if (recv(client_fd, response, sizeof(response), 0) == -1) {
perror("recv");
close(client_fd);
pthread_mutex_unlock(&socket_mutex);
return -1;
}
close(client_fd);
pthread_mutex_unlock(&socket_mutex);
// Process the response
if (response[0] == 'a' && response[1] == 'y') {
return 0; // Access granted
} else if (response[0] == 'a' && response[1] == 'n') {
return 1; // Access denied
}
return -1; // Invalid response
}

24
sources/ui-socket.h Normal file
View File

@ -0,0 +1,24 @@
/*
* Interface for controlling communication with the UI.
*/
#ifndef UI_SOCKET_H
#define UI_SOCKET_H
#include <sys/types.h>
struct process_info {
pid_t PID;
const char *name;
uid_t UID;
};
// For default socket location, set socket_path = NULL.
int init_ui_socket(const char *socket_path);
// TODO: design an interface for asking user for permission.
int ask_access(const char *filename, struct process_info pi);
#endif // !UI_SOCKET_H