Compare commits
No commits in common. "API" and "main" have entirely different histories.
5
Makefile
5
Makefile
@ -22,7 +22,7 @@ endif
|
||||
SOURCES_DIR := ./sources
|
||||
BUILD_DIR := ./build
|
||||
|
||||
build: $(BUILD_DIR)/main.o $(BUILD_DIR)/sourcefs.o $(BUILD_DIR)/ui-socket.o
|
||||
build: $(BUILD_DIR)/main.o $(BUILD_DIR)/sourcefs.o
|
||||
$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $(BUILD_DIR)/icfs
|
||||
|
||||
$(BUILD_DIR)/main.o: $(SOURCES_DIR)/main.c
|
||||
@ -31,8 +31,5 @@ $(BUILD_DIR)/main.o: $(SOURCES_DIR)/main.c
|
||||
$(BUILD_DIR)/sourcefs.o: $(SOURCES_DIR)/sourcefs.c $(SOURCES_DIR)/sourcefs.h
|
||||
$(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:
|
||||
rm $(BUILD_DIR)/*
|
||||
|
@ -49,7 +49,6 @@
|
||||
#include <sys/file.h> /* flock(2) */
|
||||
|
||||
#include "sourcefs.h"
|
||||
#include "ui-socket.h"
|
||||
|
||||
const char *mountpoint = NULL;
|
||||
|
||||
@ -361,16 +360,6 @@ static int xmp_create(const char *path, mode_t mode,
|
||||
struct fuse_file_info *fi) {
|
||||
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);
|
||||
if (fd == -1)
|
||||
return -errno;
|
||||
@ -382,16 +371,6 @@ static int xmp_create(const char *path, mode_t mode,
|
||||
static int xmp_open(const char *path, struct fuse_file_info *fi) {
|
||||
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);
|
||||
if (fd == -1)
|
||||
return -errno;
|
||||
@ -684,13 +663,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
int ret = source_init(mountpoint);
|
||||
if (ret != 0) {
|
||||
perror("source_init");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
ret = init_ui_socket("/home/fedir/.icfs-sock");
|
||||
if (ret != 0) {
|
||||
perror("init_ui_socket");
|
||||
perror("Failed to initialize filesystem.");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
@ -1,116 +0,0 @@
|
||||
#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
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
|
||||
/*
|
||||
* 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
|
Loading…
Reference in New Issue
Block a user