cleanup, fix ctl write

This commit is contained in:
Iris Lightshard 2024-01-30 22:53:42 -07:00
parent 5040da40fc
commit 4ed8a026bb
Signed by: nilix
GPG key ID: 688407174966CAF3
8 changed files with 38 additions and 132 deletions

View file

@ -8,7 +8,6 @@ OFILES=\
client.$O\
util.$O\
node.$O\
nodetable.$O\
HFILES=dat.h\
fns.h\

16
dat.h
View file

@ -10,7 +10,9 @@
#include <keyboard.h>
typedef enum {
CTL = 1
CTL = 0,
CPU = 1,
MEMORY = 2
} FileType;
typedef enum {
@ -88,15 +90,3 @@ typedef struct {
KuroMemory* memory;
Channel* status; /* chan(WidowStatus) */
} Node;
typedef struct NodeRef NodeRef;
struct NodeRef {
int fd[2];
uvlong id;
NodeRef* next;
};
typedef struct {
NodeRef* data[256];
} NodeTable;

5
fns.h
View file

@ -3,15 +3,12 @@ char* strcsw(char* s, char a, char b);
char* uvlong_to_hex(uvlong input, char* buf);
int ensure(char* path);
void start_9p();
void start_9p(Node* n);
void kuro9p_set_mtpt(char* mtpt);
void kuro9p_write(char* path, char* data, int len);
char* kuro9p_read(char* path, char* buf, int len);
void nodetbl_add(NodeTable* self, NodeRef* node);
void nodetbl_del(NodeTable* self, uvlong id);
void node_setup(Node* self, Handler* handlers, KuroMemory* memory);
void node_execute(Node* self);
void node_loop(void* data);

16
kuro.c
View file

@ -3,8 +3,9 @@
static char* mtpt;
int threadmaybackground(void) {
return 1;
void srvthread(void* arg) {
Node* vm = (Node*)arg;
start_9p(vm);
}
void threadmain(int argc, char **argv)
@ -13,15 +14,14 @@ void threadmain(int argc, char **argv)
if (mtpt == nil) {
sysfatal("KURO_MTPT not set");
}
kuro9p_set_mtpt(mtpt);
print("KURO_MTPT=%s\n", mtpt);
ARGBEGIN{
/* ARGBEGIN{
}ARGEND
}ARGEND */
threadcreate(start_9p, nil, 4096);
supervise_node(create_node(argc > 1 ? argv[0] : nil));
Node* vm = create_node(argc > 0 ? argv[0] : nil);
proccreate(srvthread, (void*)vm, 4096);
supervise_node(vm);
threadexitsall(0);
}

1
mkfile
View file

@ -9,7 +9,6 @@ OFILES=\
client.$O\
util.$O\
node.$O\
nodetable.$O\
HFILES=dat.h\
fns.h\

11
node.c
View file

@ -37,7 +37,6 @@ void node_execute(Node* self) {
Node* create_node(char* filename) {
initdraw(nil, nil, "kuro");
unlockdisplay(display);
KuroMemory* self = (KuroMemory*)malloc(sizeof(KuroMemory));
Node* node = (Node*)malloc(sizeof(Node));
@ -66,7 +65,7 @@ void supervise_node(Node* self) {
WindowStatus status;
Rune kbd;
if ((mctl = initmouse(nil, screen)) == nil) {
if ((mctl = initmouse(nil, self->memory->img)) == nil) {
sysfatal("couldn't initialize mctl");
}
if ((kctl = initkeyboard(nil)) == nil) {
@ -84,20 +83,20 @@ void supervise_node(Node* self) {
for (;;) {
switch (alt(alts)) {
case PORT_MOUSE:
print("got a mouse event");
print("got a mouse event\n");
break;
case PORT_RESIZE:
/*if (getwindow(display, Refnone) < 0)
sysfatal("couldn't resize");*/
print("got a resize event");
print("got a resize event\n");
break;
case PORT_KBD:
print("got a keypress");
print("got a keypress\n");
break;
case PORT_STATUS:
switch(status) {
case KURO_QUITS:
print("VM died - let's clean up the data");
print("VM died - let's clean up the data\n");
goto cleanup;
default:
break;

View file

@ -1,42 +0,0 @@
#include "dat.h"
#include "fns.h"
void nodetbl_add(NodeTable* self, NodeRef* node) {
int i;
NodeRef* n;
if (self && node) {
i = (int)(node->id%256);
n = self->data[i];
if (n && n->next) {
n = n->next;
}
if (n) {
n->next = node;
} else {
n = node;
}
}
}
void nodetbl_del(NodeTable* self, uvlong id) {
NodeRef* n;
NodeRef* nprev;
if (self) {
for(int i = 0; i <= 255; i++) {
n = self->data[i];
while (n && n->id != id && n->next) {
nprev = n;
n = n->next;
}
if (n->id == id) {
if (nprev) {
nprev->next = n->next;
}
free(n);
break;
}
}
}
}

74
srv.c
View file

@ -1,17 +1,7 @@
#include "dat.h"
#include "fns.h"
static NodeTable nodes;
static QLock id_lock;
static uvlong get_next_id(void) {
static uvlong id = 0;
uvlong next;
qlock(&id_lock);
next = ++id;
qunlock(&id_lock);
return next;
}
static Node* node;
Aux* create_aux(FileType t) {
Aux* self = (Aux*)malloc(sizeof(Aux));
@ -35,42 +25,13 @@ void kuro_read(Req* r) {
Aux* a = (Aux*)r->fid->file->aux;
switch (a->type) {
case CTL:
case MEMORY:
default:
respond(r, nil);
break;
}
}
void new_window(char* filename, int new) {
int rw_fd[2];
char client_fd[32] = {0};
char client_id[32] = {0};
uvlong id;
NodeRef* noderef;
pipe(rw_fd);
id = get_next_id();
noderef = (NodeRef*)malloc(sizeof(NodeRef));
noderef->id = id;
noderef->fd[0] = rw_fd[0];
noderef->fd[1] = rw_fd[1];
nodetbl_add(&nodes, noderef);
sprintf(client_fd, "%d", rw_fd[1]);
sprintf(client_id, "%d", id);
rfork(RFNAMEG);
char* a[] = { "kuro", "-p", client_fd, "-i", client_id, new ? "-n" : filename, new ? filename : 0, 0 };
exec("./kuro", a);
}
void newwindowthread(void* arg) {
char* filename = (char*)arg;
new_window(filename, 1);
}
void write_ctl(Req* r) {
char cmd[16] = {0};
char* c = r->ifcall.data;
@ -92,13 +53,11 @@ void write_ctl(Req* r) {
print("cmd: %s\n", cmd);
print("arg: %s\n", c);
if (strequ(cmd, "new")) {
threadcreate(newwindowthread, c, 256);
respond(r, nil);
} else {
print("unknown command...\n");
r->ofcall.count = r->ifcall.count;
respond(r, nil);
}
}
void kuro_write(Req* r) {
@ -107,6 +66,7 @@ void kuro_write(Req* r) {
case CTL:
write_ctl(r);
break;
case CPU:
default:
respond(r, nil);
break;
@ -119,21 +79,25 @@ char* mk_mtpt(char* parent, char* buf) {
return buf;
}
void start_9p() {
void start_9p(Node* n) {
node = n;
char* mtpt_base = getenv("KURO_MTPT");
if (mtpt_base) {
char mtpt[256] = "TESTING LIKE THIS SHOULD NOT BE NECESSARY; AYEYEYEYEYE";
if (mtpt_base && n) {
char mtpt[256] = {0};
mk_mtpt(mtpt_base, mtpt);
int i = ensure(mtpt);
print("%d result of ensure\n", i);
print("%s\n", mtpt);
if (ensure(mtpt)) {
print("kuro fsys: %s\n", mtpt);
} else {
sysfatal("couldn't create fsys at %s\n", mtpt);
}
Srv srv = { .read = kuro_read, .write = kuro_write };
Tree* tree = alloctree(nil, nil, DMDIR | 0777, fs_destroy_file);
srv.tree = tree;
closefile(createfile(tree->root, "ctl", nil, DMAPPEND | 0600, create_aux(CTL)));
closefile(tree->root);
/* TODO: figure out how to kill the server cleanly so we don't need MREPL */
closefile(createfile(tree->root, "ctl", nil, 0600, create_aux(CTL)));
threadpostmountsrv(&srv, nil, mtpt, MREPL | MCREATE);
threadexits(0);
}