Improved ui socket and made open and create send requests.
This commit is contained in:
parent
3cbe520916
commit
e0b69cfea1
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,104 +1,116 @@
|
|||||||
|
#define _GNU_SOURCE
|
||||||
#include "ui-socket.h"
|
#include "ui-socket.h"
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static int socket_fd = -1;
|
#define MAX_MESSAGE_SIZE 1024
|
||||||
|
|
||||||
|
// Mutex for thread safety
|
||||||
static pthread_mutex_t socket_mutex = PTHREAD_MUTEX_INITIALIZER;
|
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) {
|
int init_ui_socket(const char *filename) {
|
||||||
struct sockaddr_un addr;
|
if (!filename) {
|
||||||
int fd;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
// Create the socket
|
||||||
|
ui_socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
|
if (ui_socket_fd == -1) {
|
||||||
perror("socket");
|
perror("socket");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(&addr, 0, sizeof(addr));
|
// 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;
|
addr.sun_family = AF_UNIX;
|
||||||
strncpy(addr.sun_path, filename, sizeof(addr.sun_path) - 1);
|
strncpy(addr.sun_path, filename, sizeof(addr.sun_path) - 1);
|
||||||
|
|
||||||
if (unlink(filename) == -1 && errno != ENOENT) {
|
// Bind the socket
|
||||||
perror("unlink");
|
if (bind(ui_socket_fd, (struct sockaddr *)&addr,
|
||||||
close(fd);
|
sizeof(struct sockaddr_un)) == -1) {
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
|
|
||||||
perror("bind");
|
perror("bind");
|
||||||
close(fd);
|
close(ui_socket_fd);
|
||||||
|
ui_socket_fd = -1;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listen(fd, 5) == -1) {
|
// Listen for incoming connections
|
||||||
|
if (listen(ui_socket_fd, 5) == -1) {
|
||||||
perror("listen");
|
perror("listen");
|
||||||
close(fd);
|
close(ui_socket_fd);
|
||||||
|
ui_socket_fd = -1;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
socket_fd = fd;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ask_access(const char *filename, struct process_info pi) {
|
int ask_access(const char *filename, struct process_info pi) {
|
||||||
int client_fd;
|
if (!filename || ui_socket_fd == -1) {
|
||||||
struct sockaddr_un client_addr;
|
|
||||||
socklen_t client_len = sizeof(client_addr);
|
|
||||||
char request[1024];
|
|
||||||
char response[4];
|
|
||||||
ssize_t bytes_sent, bytes_received;
|
|
||||||
|
|
||||||
// Accept a connection from the GUI
|
|
||||||
if ((client_fd = accept(socket_fd, (struct sockaddr *)&client_addr,
|
|
||||||
&client_len)) == -1) {
|
|
||||||
perror("accept");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct the request message
|
// Lock the mutex for thread safety
|
||||||
snprintf(request, sizeof(request), "r%s;%d;%s;%d\0", filename, pi.PID,
|
|
||||||
pi.name, pi.UID);
|
|
||||||
|
|
||||||
// Lock the socket to ensure thread safety
|
|
||||||
pthread_mutex_lock(&socket_mutex);
|
pthread_mutex_lock(&socket_mutex);
|
||||||
|
|
||||||
// Send the request message to the GUI
|
int client_fd = accept(ui_socket_fd, NULL, NULL);
|
||||||
bytes_sent = send(client_fd, request, strlen(request), 0);
|
if (client_fd == -1) {
|
||||||
if (bytes_sent == -1) {
|
perror("accept");
|
||||||
perror("send");
|
|
||||||
pthread_mutex_unlock(&socket_mutex);
|
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);
|
close(client_fd);
|
||||||
|
pthread_mutex_unlock(&socket_mutex);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Receive the response from the GUI
|
// Receive the response from the GUI
|
||||||
bytes_received = recv(client_fd, response, sizeof(response) - 1, 0);
|
char response[2];
|
||||||
if (bytes_received == -1) {
|
if (recv(client_fd, response, sizeof(response), 0) == -1) {
|
||||||
perror("recv");
|
perror("recv");
|
||||||
pthread_mutex_unlock(&socket_mutex);
|
|
||||||
close(client_fd);
|
close(client_fd);
|
||||||
|
pthread_mutex_unlock(&socket_mutex);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
response[bytes_received] = '\0';
|
|
||||||
|
|
||||||
// Unlock the socket
|
close(client_fd);
|
||||||
pthread_mutex_unlock(&socket_mutex);
|
pthread_mutex_unlock(&socket_mutex);
|
||||||
|
|
||||||
// Close the client socket
|
// Process the response
|
||||||
close(client_fd);
|
if (response[0] == 'a' && response[1] == 'y') {
|
||||||
|
|
||||||
// Check the response
|
|
||||||
if (response[0] == 'a' && response[1] == 'y' && response[2] == '\0') {
|
|
||||||
return 0; // Access granted
|
return 0; // Access granted
|
||||||
} else if (response[0] == 'a' && response[1] == 'n' && response[2] == '\0') {
|
} else if (response[0] == 'a' && response[1] == 'n') {
|
||||||
return 1; // Access denied
|
return 1; // Access denied
|
||||||
} else {
|
}
|
||||||
// fprintf(stderr, "Invalid response from GUI: %s\n", response);
|
|
||||||
return -1; // Invalid response
|
return -1; // Invalid response
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user