2021-05-31 04:47:02 +03:00
|
|
|
/**
|
|
|
|
* @file kernel/vfs/packetfs.c
|
|
|
|
* @brief Packet-based multiple-client IPC mechanism. aka PEX
|
|
|
|
*
|
|
|
|
* Provides a server-client packet-based IPC socket system for
|
|
|
|
* userspace applications. Primarily used by the compositor to
|
|
|
|
* communicate with clients.
|
|
|
|
*
|
|
|
|
* Care must be taken to ensure that this is backed by an atomic
|
|
|
|
* stream; the legacy pseudo-pipe interface is used at the moment.
|
|
|
|
*
|
|
|
|
* @bug We leak kernel heap addresses directly to userspace as the
|
|
|
|
* client identifiers in PEX messages. We should probably do
|
|
|
|
* something else. I'm also reasonably certain a server can
|
|
|
|
* just throw a random address at the PEX API?
|
|
|
|
*
|
|
|
|
* @copyright
|
2018-03-16 15:56:19 +03:00
|
|
|
* This file is part of ToaruOS and is released under the terms
|
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
2021-05-31 04:47:02 +03:00
|
|
|
* Copyright (C) 2014-2021 K. Lange
|
2018-03-16 15:56:19 +03:00
|
|
|
*/
|
2021-05-31 04:47:02 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
2021-06-02 12:35:50 +03:00
|
|
|
#include <kernel/assert.h>
|
2021-05-31 04:47:02 +03:00
|
|
|
#include <kernel/types.h>
|
|
|
|
#include <kernel/printf.h>
|
|
|
|
#include <kernel/string.h>
|
|
|
|
#include <kernel/vfs.h>
|
2018-03-19 05:38:11 +03:00
|
|
|
#include <kernel/pipe.h>
|
2021-05-31 04:47:02 +03:00
|
|
|
#include <kernel/spinlock.h>
|
|
|
|
#include <kernel/process.h>
|
2018-03-19 05:38:11 +03:00
|
|
|
|
2021-09-12 08:04:11 +03:00
|
|
|
extern void pipe_destroy(fs_node_t * node);
|
|
|
|
|
2018-03-19 05:38:11 +03:00
|
|
|
#include <sys/ioctl.h>
|
2018-03-16 15:56:19 +03:00
|
|
|
|
|
|
|
#define MAX_PACKET_SIZE 1024
|
2021-05-31 04:47:02 +03:00
|
|
|
#define debug_print(x, ...) do { if (0) {printf("packetfs.c [%s] ", #x); printf(__VA_ARGS__); printf("\n"); } } while (0)
|
2018-03-16 15:56:19 +03:00
|
|
|
|
|
|
|
typedef struct packet_manager {
|
|
|
|
/* uh, nothing, lol */
|
|
|
|
list_t * exchanges;
|
|
|
|
spin_lock_t lock;
|
|
|
|
} pex_t;
|
|
|
|
|
|
|
|
typedef struct packet_exchange {
|
|
|
|
char * name;
|
|
|
|
char fresh;
|
|
|
|
spin_lock_t lock;
|
|
|
|
fs_node_t * server_pipe;
|
|
|
|
list_t * clients;
|
2021-09-12 07:23:38 +03:00
|
|
|
pex_t * parent;
|
2018-03-16 15:56:19 +03:00
|
|
|
} pex_ex_t;
|
|
|
|
|
|
|
|
typedef struct packet_client {
|
|
|
|
pex_ex_t * parent;
|
|
|
|
fs_node_t * pipe;
|
|
|
|
} pex_client_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct packet {
|
|
|
|
pex_client_t * source;
|
|
|
|
size_t size;
|
|
|
|
uint8_t data[];
|
|
|
|
} packet_t;
|
|
|
|
|
|
|
|
typedef struct server_write_header {
|
|
|
|
pex_client_t * target;
|
|
|
|
uint8_t data[];
|
|
|
|
} header_t;
|
|
|
|
|
2022-03-06 10:20:55 +03:00
|
|
|
static ssize_t receive_packet(pex_ex_t * exchange, fs_node_t * socket, packet_t ** out) {
|
|
|
|
ssize_t r;
|
2021-06-02 12:35:50 +03:00
|
|
|
do {
|
|
|
|
r = read_fs(socket, 0, sizeof(struct packet *), (uint8_t*)out);
|
|
|
|
} while (r == 0);
|
2022-03-06 10:20:55 +03:00
|
|
|
if (r < 0) return r;
|
2021-06-02 12:35:50 +03:00
|
|
|
assert(r == sizeof(struct packet*));
|
|
|
|
assert((uintptr_t)*out >= 0xFFFFff0000000000UL && (uintptr_t)*out < 0xffffff1fc0000000UL);
|
2022-03-06 10:20:55 +03:00
|
|
|
return r;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void send_to_server(pex_ex_t * p, pex_client_t * c, size_t size, void * data) {
|
|
|
|
size_t p_size = size + sizeof(struct packet);
|
|
|
|
packet_t * packet = malloc(p_size);
|
2021-05-31 04:47:02 +03:00
|
|
|
if ((uintptr_t)c < 0x800000000) {
|
2021-06-02 01:37:01 +03:00
|
|
|
printf("suspicious pex client received: %p\n", (char*)c);
|
2021-05-31 04:47:02 +03:00
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
|
|
|
|
packet->source = c;
|
|
|
|
packet->size = size;
|
|
|
|
|
|
|
|
if (size) {
|
|
|
|
memcpy(packet->data, data, size);
|
|
|
|
}
|
|
|
|
|
2021-06-02 01:37:01 +03:00
|
|
|
write_fs(p->server_pipe, 0, sizeof(struct packet*), (uint8_t*)&packet);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int send_to_client(pex_ex_t * p, pex_client_t * c, size_t size, void * data) {
|
|
|
|
size_t p_size = size + sizeof(struct packet);
|
|
|
|
|
|
|
|
/* Verify there is space on the client */
|
2021-06-02 01:37:01 +03:00
|
|
|
if (pipe_unsize(c->pipe) < (int)sizeof(struct packet*)) {
|
2018-03-16 15:56:19 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
if ((uintptr_t)c < 0x800000000) {
|
2021-06-02 01:37:01 +03:00
|
|
|
printf("suspicious pex client received: %p\n", (char*)c);
|
2021-05-31 04:47:02 +03:00
|
|
|
}
|
|
|
|
|
2018-03-16 15:56:19 +03:00
|
|
|
packet_t * packet = malloc(p_size);
|
|
|
|
|
|
|
|
memcpy(packet->data, data, size);
|
|
|
|
packet->source = NULL;
|
|
|
|
packet->size = size;
|
|
|
|
|
2021-06-02 01:37:01 +03:00
|
|
|
write_fs(c->pipe, 0, sizeof(struct packet*), (uint8_t*)&packet);
|
2018-03-16 15:56:19 +03:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static pex_client_t * create_client(pex_ex_t * p) {
|
|
|
|
pex_client_t * out = malloc(sizeof(pex_client_t));
|
|
|
|
out->parent = p;
|
|
|
|
out->pipe = make_pipe(4096);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-06-06 14:25:34 +03:00
|
|
|
static ssize_t read_server(fs_node_t * node, off_t offset, size_t size, uint8_t * buffer) {
|
2018-03-16 15:56:19 +03:00
|
|
|
pex_ex_t * p = (pex_ex_t *)node->device;
|
|
|
|
debug_print(INFO, "[pex] server read(...)");
|
|
|
|
|
2021-06-02 12:35:50 +03:00
|
|
|
packet_t * packet = NULL;
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2022-03-06 10:20:55 +03:00
|
|
|
ssize_t response_size = receive_packet(p, p->server_pipe, &packet);
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2022-03-06 10:20:55 +03:00
|
|
|
if (response_size < 0) return response_size;
|
2021-06-02 12:35:50 +03:00
|
|
|
if (!packet) return -1;
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
debug_print(INFO, "Server recevied packet of size %zu, was waiting for at most %lu", packet->size, size);
|
2018-03-16 15:56:19 +03:00
|
|
|
|
|
|
|
if (packet->size + sizeof(packet_t) > size) {
|
2021-06-02 01:37:01 +03:00
|
|
|
printf("pex: read in server would be incomplete\n");
|
2018-03-16 15:56:19 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buffer, packet, packet->size + sizeof(packet_t));
|
2021-06-06 14:25:34 +03:00
|
|
|
ssize_t out = packet->size + sizeof(packet_t);
|
2018-03-16 15:56:19 +03:00
|
|
|
|
|
|
|
free(packet);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-06-06 14:25:34 +03:00
|
|
|
static ssize_t write_server(fs_node_t * node, off_t offset, size_t size, uint8_t * buffer) {
|
2018-03-16 15:56:19 +03:00
|
|
|
pex_ex_t * p = (pex_ex_t *)node->device;
|
|
|
|
debug_print(INFO, "[pex] server write(...)");
|
|
|
|
|
|
|
|
header_t * head = (header_t *)buffer;
|
|
|
|
|
|
|
|
if (size - sizeof(header_t) > MAX_PACKET_SIZE) {
|
2021-06-02 01:37:01 +03:00
|
|
|
printf("pex: server write is too big\n");
|
2018-03-16 15:56:19 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (head->target == NULL) {
|
|
|
|
/* Brodcast packet */
|
|
|
|
spin_lock(p->lock);
|
|
|
|
foreach(f, p->clients) {
|
2021-05-31 04:47:02 +03:00
|
|
|
debug_print(INFO, "Sending to client %p", f->value);
|
2018-03-16 15:56:19 +03:00
|
|
|
send_to_client(p, (pex_client_t *)f->value, size - sizeof(header_t), head->data);
|
|
|
|
}
|
|
|
|
spin_unlock(p->lock);
|
|
|
|
debug_print(INFO, "Done broadcasting to clients.");
|
|
|
|
return size;
|
|
|
|
} else if (head->target->parent != p) {
|
2021-05-31 04:47:02 +03:00
|
|
|
debug_print(WARNING, "[pex] Invalid packet from server? (pid=%d)", this_core->current_process->id);
|
2018-03-16 15:56:19 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
return send_to_client(p, head->target, size - sizeof(header_t), head->data) + sizeof(header_t);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-06-06 14:25:34 +03:00
|
|
|
static int ioctl_server(fs_node_t * node, unsigned long request, void * argp) {
|
2018-03-16 15:56:19 +03:00
|
|
|
pex_ex_t * p = (pex_ex_t *)node->device;
|
|
|
|
|
|
|
|
switch (request) {
|
|
|
|
case IOCTL_PACKETFS_QUEUED:
|
|
|
|
return pipe_size(p->server_pipe);
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-06 14:25:34 +03:00
|
|
|
static ssize_t read_client(fs_node_t * node, off_t offset, size_t size, uint8_t * buffer) {
|
2018-03-16 15:56:19 +03:00
|
|
|
pex_client_t * c = (pex_client_t *)node->inode;
|
|
|
|
if (c->parent != node->device) {
|
2021-06-02 01:37:01 +03:00
|
|
|
printf("pex: Invalid device endpoint on client read?\n");
|
2021-09-12 07:23:38 +03:00
|
|
|
return -EINVAL;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
debug_print(INFO, "[pex] client read(...)");
|
|
|
|
|
2021-06-02 12:35:50 +03:00
|
|
|
packet_t * packet = NULL;
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2022-03-06 10:20:55 +03:00
|
|
|
ssize_t response_size = receive_packet(c->parent, c->pipe, &packet);
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2022-03-06 10:20:55 +03:00
|
|
|
if (response_size < 0) return response_size;
|
2021-09-12 07:23:38 +03:00
|
|
|
if (!packet) return -EIO;
|
2021-06-02 12:35:50 +03:00
|
|
|
|
2018-03-16 15:56:19 +03:00
|
|
|
if (packet->size > size) {
|
2021-06-02 01:37:01 +03:00
|
|
|
printf("pex: Client is not reading enough bytes to hold packet of size %zu\n", packet->size);
|
2021-09-12 07:23:38 +03:00
|
|
|
return -EINVAL;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buffer, &packet->data, packet->size);
|
2021-06-06 14:25:34 +03:00
|
|
|
ssize_t out = packet->size;
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
debug_print(INFO, "[pex] Client received packet of size %zu", packet->size);
|
2021-06-02 01:37:01 +03:00
|
|
|
if (out == 0) {
|
|
|
|
printf("pex: packet is empty?\n");
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
|
|
|
|
free(packet);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-06-06 14:25:34 +03:00
|
|
|
static ssize_t write_client(fs_node_t * node, off_t offset, size_t size, uint8_t * buffer) {
|
2018-03-16 15:56:19 +03:00
|
|
|
pex_client_t * c = (pex_client_t *)node->inode;
|
|
|
|
if (c->parent != node->device) {
|
|
|
|
debug_print(WARNING, "[pex] Invalid device endpoint on client write?");
|
2021-09-12 07:23:38 +03:00
|
|
|
return -EINVAL;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
debug_print(INFO, "[pex] client write(...)");
|
|
|
|
|
|
|
|
if (size > MAX_PACKET_SIZE) {
|
2021-05-31 04:47:02 +03:00
|
|
|
debug_print(WARNING, "Size of %lu is too big.", size);
|
2021-09-12 07:23:38 +03:00
|
|
|
return -EINVAL;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
debug_print(INFO, "Sending packet of size %lu to parent", size);
|
2018-03-16 15:56:19 +03:00
|
|
|
send_to_server(c->parent, c, size, buffer);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2021-06-06 14:25:34 +03:00
|
|
|
static int ioctl_client(fs_node_t * node, unsigned long request, void * argp) {
|
2018-03-16 15:56:19 +03:00
|
|
|
pex_client_t * c = (pex_client_t *)node->inode;
|
|
|
|
|
|
|
|
switch (request) {
|
|
|
|
case IOCTL_PACKETFS_QUEUED:
|
|
|
|
return pipe_size(c->pipe);
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void close_client(fs_node_t * node) {
|
|
|
|
pex_client_t * c = (pex_client_t *)node->inode;
|
|
|
|
pex_ex_t * p = c->parent;
|
|
|
|
|
2021-09-12 08:04:11 +03:00
|
|
|
if (p) {
|
|
|
|
debug_print(WARNING, "Closing packetfs client: %p:%p", (void*)p, (void*)c);
|
|
|
|
spin_lock(p->lock);
|
|
|
|
node_t * n = list_find(p->clients, c);
|
|
|
|
if (n && n->owner == p->clients) {
|
|
|
|
list_delete(p->clients, n);
|
|
|
|
free(n);
|
|
|
|
}
|
|
|
|
spin_unlock(p->lock);
|
|
|
|
char tmp[1];
|
|
|
|
send_to_server(p, c, 0, tmp);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-09-12 08:04:11 +03:00
|
|
|
pipe_destroy(c->pipe);
|
|
|
|
free(c->pipe);
|
2018-03-16 15:56:19 +03:00
|
|
|
free(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wait_server(fs_node_t * node, void * process) {
|
|
|
|
pex_ex_t * p = (pex_ex_t *)node->device;
|
|
|
|
return selectwait_fs(p->server_pipe, process);
|
|
|
|
}
|
|
|
|
static int check_server(fs_node_t * node) {
|
|
|
|
pex_ex_t * p = (pex_ex_t *)node->device;
|
|
|
|
return selectcheck_fs(p->server_pipe);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wait_client(fs_node_t * node, void * process) {
|
|
|
|
pex_client_t * c = (pex_client_t *)node->inode;
|
|
|
|
return selectwait_fs(c->pipe, process);
|
|
|
|
}
|
|
|
|
static int check_client(fs_node_t * node) {
|
|
|
|
pex_client_t * c = (pex_client_t *)node->inode;
|
|
|
|
return selectcheck_fs(c->pipe);
|
|
|
|
}
|
|
|
|
|
2021-09-12 07:23:38 +03:00
|
|
|
|
|
|
|
static void close_server(fs_node_t * node) {
|
|
|
|
pex_ex_t * ex = (pex_ex_t *)node->device;
|
|
|
|
pex_t * p = ex->parent;
|
|
|
|
|
|
|
|
spin_lock(p->lock);
|
|
|
|
|
|
|
|
node_t * lnode = list_find(p->exchanges, ex);
|
|
|
|
|
|
|
|
/* Remove from exchange list */
|
|
|
|
if (lnode) {
|
|
|
|
list_delete(p->exchanges, lnode);
|
|
|
|
free(lnode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Tell all clients we have disconnected */
|
|
|
|
spin_lock(ex->lock);
|
2021-09-12 08:04:11 +03:00
|
|
|
while (ex->clients->length) {
|
|
|
|
node_t * f = list_pop(ex->clients);
|
2021-09-12 07:23:38 +03:00
|
|
|
pex_client_t * client = (pex_client_t*)f->value;
|
|
|
|
send_to_client(ex, client, 0, NULL);
|
|
|
|
client->parent = NULL;
|
2021-09-12 08:04:11 +03:00
|
|
|
free(f);
|
2021-09-12 07:23:38 +03:00
|
|
|
}
|
|
|
|
spin_unlock(ex->lock);
|
|
|
|
|
2021-09-12 08:04:11 +03:00
|
|
|
free(ex->clients);
|
|
|
|
pipe_destroy(ex->server_pipe);
|
|
|
|
free(ex->server_pipe);
|
|
|
|
node->device = NULL;
|
|
|
|
free(ex);
|
2021-09-12 07:23:38 +03:00
|
|
|
|
|
|
|
spin_unlock(p->lock);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-16 15:56:19 +03:00
|
|
|
static void open_pex(fs_node_t * node, unsigned int flags) {
|
|
|
|
pex_ex_t * t = (pex_ex_t *)(node->device);
|
|
|
|
|
|
|
|
debug_print(NOTICE, "Opening packet exchange %s with flags 0x%x", t->name, flags);
|
|
|
|
|
2021-09-12 07:23:38 +03:00
|
|
|
if ((flags & O_CREAT) && (flags & O_EXCL)) {
|
|
|
|
if (!t->fresh) {
|
|
|
|
return; /* Address in use; this should be handled by kopen... */
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
t->fresh = 0;
|
|
|
|
node->inode = 0;
|
|
|
|
/* Set up the server side */
|
|
|
|
node->read = read_server;
|
|
|
|
node->write = write_server;
|
|
|
|
node->ioctl = ioctl_server;
|
2021-09-12 07:23:38 +03:00
|
|
|
node->close = close_server;
|
2018-03-16 15:56:19 +03:00
|
|
|
node->selectcheck = check_server;
|
|
|
|
node->selectwait = wait_server;
|
|
|
|
debug_print(INFO, "[pex] Server launched: %s", t->name);
|
2021-05-31 04:47:02 +03:00
|
|
|
debug_print(INFO, "fs_node = %p", (void*)node);
|
2021-09-12 07:23:38 +03:00
|
|
|
} else {
|
2018-03-16 15:56:19 +03:00
|
|
|
pex_client_t * client = create_client(t);
|
|
|
|
node->inode = (uintptr_t)client;
|
|
|
|
|
|
|
|
node->read = read_client;
|
|
|
|
node->write = write_client;
|
|
|
|
node->ioctl = ioctl_client;
|
|
|
|
node->close = close_client;
|
|
|
|
|
|
|
|
node->selectcheck = check_client;
|
|
|
|
node->selectwait = wait_client;
|
|
|
|
|
|
|
|
list_insert(t->clients, client);
|
|
|
|
|
|
|
|
/* XXX: Send plumbing message to server for new client connection */
|
2021-05-31 04:47:02 +03:00
|
|
|
debug_print(INFO, "[pex] Client connected: %s:%lx", t->name, node->inode);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static struct dirent * readdir_packetfs(fs_node_t *node, uint64_t index) {
|
2018-03-16 15:56:19 +03:00
|
|
|
pex_t * p = (pex_t *)node->device;
|
|
|
|
unsigned int i = 0;
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
debug_print(INFO, "[pex] readdir(%lu)", index);
|
2018-03-16 15:56:19 +03:00
|
|
|
|
|
|
|
if (index == 0) {
|
|
|
|
struct dirent * out = malloc(sizeof(struct dirent));
|
|
|
|
memset(out, 0x00, sizeof(struct dirent));
|
2021-06-05 12:01:48 +03:00
|
|
|
out->d_ino = 0;
|
|
|
|
strcpy(out->d_name, ".");
|
2018-03-16 15:56:19 +03:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == 1) {
|
|
|
|
struct dirent * out = malloc(sizeof(struct dirent));
|
|
|
|
memset(out, 0x00, sizeof(struct dirent));
|
2021-06-05 12:01:48 +03:00
|
|
|
out->d_ino = 0;
|
|
|
|
strcpy(out->d_name, "..");
|
2018-03-16 15:56:19 +03:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
index -= 2;
|
|
|
|
|
|
|
|
if (index >= p->exchanges->length) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock(p->lock);
|
|
|
|
|
|
|
|
foreach(f, p->exchanges) {
|
|
|
|
if (i == index) {
|
|
|
|
spin_unlock(p->lock);
|
|
|
|
pex_ex_t * t = (pex_ex_t *)f->value;
|
|
|
|
struct dirent * out = malloc(sizeof(struct dirent));
|
|
|
|
memset(out, 0x00, sizeof(struct dirent));
|
2021-06-05 12:01:48 +03:00
|
|
|
out->d_ino = (uint64_t)t;
|
|
|
|
strcpy(out->d_name, t->name);
|
2018-03-16 15:56:19 +03:00
|
|
|
return out;
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock(p->lock);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fs_node_t * file_from_pex(pex_ex_t * pex) {
|
|
|
|
fs_node_t * fnode = malloc(sizeof(fs_node_t));
|
|
|
|
memset(fnode, 0x00, sizeof(fs_node_t));
|
|
|
|
fnode->inode = 0;
|
|
|
|
strcpy(fnode->name, pex->name);
|
|
|
|
fnode->device = pex;
|
|
|
|
fnode->mask = 0666;
|
|
|
|
fnode->flags = FS_CHARDEVICE;
|
|
|
|
fnode->open = open_pex;
|
|
|
|
return fnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fs_node_t * finddir_packetfs(fs_node_t * node, char * name) {
|
|
|
|
if (!name) return NULL;
|
|
|
|
pex_t * p = (pex_t *)node->device;
|
|
|
|
|
|
|
|
debug_print(INFO, "[pex] finddir(%s)", name);
|
|
|
|
|
|
|
|
spin_lock(p->lock);
|
|
|
|
|
|
|
|
foreach(f, p->exchanges) {
|
|
|
|
pex_ex_t * t = (pex_ex_t *)f->value;
|
|
|
|
if (!strcmp(name, t->name)) {
|
|
|
|
spin_unlock(p->lock);
|
|
|
|
return file_from_pex(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock(p->lock);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-06-06 14:25:34 +03:00
|
|
|
static int create_packetfs(fs_node_t *parent, char *name, mode_t permission) {
|
2018-07-18 09:35:52 +03:00
|
|
|
if (!name) return -EINVAL;
|
2018-03-16 15:56:19 +03:00
|
|
|
|
|
|
|
pex_t * p = (pex_t *)parent->device;
|
|
|
|
|
|
|
|
debug_print(NOTICE, "[pex] create(%s)", name);
|
|
|
|
|
|
|
|
spin_lock(p->lock);
|
|
|
|
|
|
|
|
foreach(f, p->exchanges) {
|
|
|
|
pex_ex_t * t = (pex_ex_t *)f->value;
|
|
|
|
if (!strcmp(name, t->name)) {
|
|
|
|
spin_unlock(p->lock);
|
|
|
|
/* Already exists */
|
2018-07-18 09:35:52 +03:00
|
|
|
return -EEXIST;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make it */
|
|
|
|
pex_ex_t * new_exchange = malloc(sizeof(pex_ex_t));
|
|
|
|
|
|
|
|
new_exchange->name = strdup(name);
|
|
|
|
new_exchange->fresh = 1;
|
2021-05-31 04:47:02 +03:00
|
|
|
new_exchange->clients = list_create("pex clients",new_exchange);
|
2018-03-16 15:56:19 +03:00
|
|
|
new_exchange->server_pipe = make_pipe(4096);
|
2021-09-12 07:23:38 +03:00
|
|
|
new_exchange->parent = p;
|
2018-03-16 15:56:19 +03:00
|
|
|
|
|
|
|
spin_init(new_exchange->lock);
|
|
|
|
/* XXX Create exchange server pipe */
|
|
|
|
|
|
|
|
list_insert(p->exchanges, new_exchange);
|
|
|
|
|
|
|
|
spin_unlock(p->lock);
|
|
|
|
|
2018-07-18 09:35:52 +03:00
|
|
|
return 0;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_pex(pex_ex_t * p) {
|
|
|
|
/* XXX */
|
|
|
|
}
|
|
|
|
|
2018-07-18 09:35:52 +03:00
|
|
|
static int unlink_packetfs(fs_node_t *parent, char *name) {
|
|
|
|
if (!name) return -EINVAL;
|
2018-03-16 15:56:19 +03:00
|
|
|
|
|
|
|
pex_t * p = (pex_t *)parent->device;
|
|
|
|
|
|
|
|
debug_print(NOTICE, "[pex] unlink(%s)", name);
|
|
|
|
|
|
|
|
int i = -1, j = 0;
|
|
|
|
|
|
|
|
spin_lock(p->lock);
|
|
|
|
|
|
|
|
foreach(f, p->exchanges) {
|
|
|
|
pex_ex_t * t = (pex_ex_t *)f->value;
|
|
|
|
if (!strcmp(name, t->name)) {
|
|
|
|
destroy_pex(t);
|
|
|
|
i = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= 0) {
|
|
|
|
list_remove(p->exchanges, i);
|
2018-07-18 09:35:52 +03:00
|
|
|
} else {
|
|
|
|
spin_unlock(p->lock);
|
|
|
|
return -ENOENT;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock(p->lock);
|
2018-07-18 09:35:52 +03:00
|
|
|
|
|
|
|
return 0;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static fs_node_t * packetfs_manager(void) {
|
|
|
|
pex_t * pex = malloc(sizeof(pex_t));
|
2021-05-31 04:47:02 +03:00
|
|
|
pex->exchanges = list_create("pex exchanges",pex);
|
2018-03-16 15:56:19 +03:00
|
|
|
|
|
|
|
spin_init(pex->lock);
|
|
|
|
|
|
|
|
fs_node_t * fnode = malloc(sizeof(fs_node_t));
|
|
|
|
memset(fnode, 0x00, sizeof(fs_node_t));
|
|
|
|
fnode->inode = 0;
|
|
|
|
strcpy(fnode->name, "pex");
|
|
|
|
fnode->device = pex;
|
|
|
|
fnode->mask = 0777;
|
|
|
|
fnode->flags = FS_DIRECTORY;
|
|
|
|
fnode->readdir = readdir_packetfs;
|
|
|
|
fnode->finddir = finddir_packetfs;
|
|
|
|
fnode->create = create_packetfs;
|
|
|
|
fnode->unlink = unlink_packetfs;
|
|
|
|
|
|
|
|
return fnode;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
void packetfs_initialize(void) {
|
2018-03-16 15:56:19 +03:00
|
|
|
fs_node_t * packet_mgr = packetfs_manager();
|
|
|
|
vfs_mount("/dev/pex", packet_mgr);
|
|
|
|
}
|