diff options
Diffstat (limited to 'lib9p')
-rw-r--r-- | lib9p/include/lib9p/srv.h | 97 | ||||
-rw-r--r-- | lib9p/srv.c | 91 | ||||
-rwxr-xr-x | lib9p/tests/runtest | 2 | ||||
-rw-r--r-- | lib9p/tests/test_server/main.c | 179 |
4 files changed, 192 insertions, 177 deletions
diff --git a/lib9p/include/lib9p/srv.h b/lib9p/include/lib9p/srv.h index e9d2d7b..83aabc0 100644 --- a/lib9p/include/lib9p/srv.h +++ b/lib9p/include/lib9p/srv.h @@ -11,7 +11,9 @@ #include <libcr_ipc/rpc.h> #include <libcr_ipc/chan.h> #include <libhw/generic/net.h> +#include <libmisc/assert.h> #include <libmisc/private.h> +#include <libobj/obj.h> #include <lib9p/9p.h> @@ -35,48 +37,53 @@ int lib9p_srv_acknowledge_flush(struct lib9p_srv_ctx *ctx); /* interface definitions ******************************************************/ -struct lib9p_srv_file_vtable; - -typedef struct { - struct lib9p_srv_file_vtable *vtable; -} implements_lib9p_srv_file; - -struct lib9p_srv_file_vtable { - /* all - resource management */ - void (*free )(implements_lib9p_srv_file *); /* must not error */ - struct lib9p_qid (*qid )(implements_lib9p_srv_file *); /* must not error */ - uint32_t (*chio )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *, - bool rd, bool wr, bool trunc); - - /* all - syscalls */ - struct lib9p_stat (*stat )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *); - void (*wstat )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *, - struct lib9p_stat new); - void (*remove )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *); - - /* directories - base */ - implements_lib9p_srv_file *(*dopen )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *, - struct lib9p_s childname); - implements_lib9p_srv_file *(*dcreate)(implements_lib9p_srv_file *, struct lib9p_srv_ctx *, - struct lib9p_s childname, - lib9p_dm_t perm, lib9p_o_t flags); - - /* directories - once opened */ - size_t /* <- obj cnt */ (*dread )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *, - uint8_t *buf, - uint32_t byte_count, /* <- num bytes */ - size_t obj_offset); /* <- starting at this object */ - - /* non-directories - once opened */ - uint32_t (*pread )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *, - void *buf, - uint32_t byte_count, - uint64_t byte_offset); - uint32_t (*pwrite )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *, - void *buf, - uint32_t byte_count, - uint64_t byte_offset); -}; +#define lib9p_srv_file_LO_IFACE \ + /* all - resource management */ \ + LO_FUNC(void , free ) /* must not error */ \ + LO_FUNC(struct lib9p_qid , qid ) /* must not error */ \ + LO_FUNC(uint32_t , chio , struct lib9p_srv_ctx *, \ + bool rd, bool wr, \ + bool trunc) \ + \ + /* all - syscalls */ \ + LO_FUNC(struct lib9p_stat , stat , struct lib9p_srv_ctx *) \ + LO_FUNC(void , wstat , struct lib9p_srv_ctx *, \ + struct lib9p_stat new) \ + LO_FUNC(void , remove , struct lib9p_srv_ctx *) \ + \ + /* directories - base */ \ + LO_FUNC(lo_interface lib9p_srv_file, dopen , struct lib9p_srv_ctx *, \ + struct lib9p_s childname) \ + LO_FUNC(lo_interface lib9p_srv_file, dcreate, struct lib9p_srv_ctx *, \ + struct lib9p_s childname, \ + lib9p_dm_t perm, \ + lib9p_o_t flags) \ + \ + /* directories - once opened */ \ + LO_FUNC(size_t /* <- obj cnt */ , dread , struct lib9p_srv_ctx *, \ + uint8_t *buf, \ + /* num bytes -> */ uint32_t byte_count, \ + /* starting at this object -> */ size_t obj_offset) \ + \ + /* non-directories - once opened */ \ + LO_FUNC(uint32_t , pread , struct lib9p_srv_ctx *, \ + void *buf, \ + uint32_t byte_count, \ + uint64_t byte_offset) \ + LO_FUNC(uint32_t , pwrite , struct lib9p_srv_ctx *, \ + void *buf, \ + uint32_t byte_count, \ + uint64_t byte_offset) +LO_INTERFACE(lib9p_srv_file) + +#define LIB9P_SRV_NOTDIR(TYP, NAM) \ + static lo_interface lib9p_srv_file NAM##_dopen (TYP *, struct lib9p_srv_ctx *, struct lib9p_s) { assert_notreached("not a directory"); } \ + static lo_interface lib9p_srv_file NAM##_dcreate(TYP *, struct lib9p_srv_ctx *, struct lib9p_s, lib9p_dm_t, lib9p_o_t) { assert_notreached("not a directory"); } \ + static size_t NAM##_dread (TYP *, struct lib9p_srv_ctx *, uint8_t *, uint32_t, size_t) { assert_notreached("not a directory"); } + +#define LIB9P_SRV_NOTFILE(TYP, NAM) \ + static uint32_t NAM##_pread (TYP *, struct lib9p_srv_ctx *, void *, uint32_t, uint64_t) { assert_notreached("not a file"); } \ + static uint32_t NAM##_pwrite(TYP *, struct lib9p_srv_ctx *, void *, uint32_t, uint64_t) { assert_notreached("not a file"); } /* main server entrypoints ****************************************************/ @@ -84,8 +91,8 @@ CR_RPC_DECLARE(_lib9p_srv_reqch, struct _lib9p_srv_req *, bool) struct lib9p_srv { /* Things you provide */ - void /*TODO*/ (*auth )(struct lib9p_srv_ctx *, struct lib9p_s treename); /* optional */ - implements_lib9p_srv_file *(*rootdir)(struct lib9p_srv_ctx *, struct lib9p_s treename); + void /*TODO*/ (*auth )(struct lib9p_srv_ctx *, struct lib9p_s treename); /* optional */ + lo_interface lib9p_srv_file (*rootdir)(struct lib9p_srv_ctx *, struct lib9p_s treename); /* For internal use */ BEGIN_PRIVATE(LIB9P_SRV_H) @@ -105,7 +112,7 @@ struct lib9p_srv { * @errno LINUX_ERANGE R-message does not fit into max_msg_size */ -[[noreturn]] void lib9p_srv_read_cr(struct lib9p_srv *srv, implements_net_stream_listener *listener); +[[noreturn]] void lib9p_srv_read_cr(struct lib9p_srv *srv, lo_interface net_stream_listener listener); COROUTINE lib9p_srv_write_cr(void *_srv); #endif /* _LIB9P_SRV_H_ */ diff --git a/lib9p/srv.c b/lib9p/srv.c index 47dd78d..0e58068 100644 --- a/lib9p/srv.c +++ b/lib9p/srv.c @@ -12,7 +12,6 @@ #include <libcr_ipc/mutex.h> #include <libcr_ipc/select.h> #include <libmisc/assert.h> -#include <libmisc/vcall.h> #include <libhw/generic/net.h> #define LOG_NAME 9P_SRV @@ -54,7 +53,7 @@ int lib9p_srv_acknowledge_flush(struct lib9p_srv_ctx *ctx) { typedef typeof( ((struct lib9p_qid){}).path ) srv_path_t; struct srv_pathinfo { - implements_lib9p_srv_file *file; + lo_interface lib9p_srv_file file; srv_path_t parent_dir; /* References from other srv_pathinfos (via .parent_dir) or @@ -108,7 +107,7 @@ struct _srv_fidinfo { struct _srv_conn { /* immutable */ struct lib9p_srv *parent_srv; - implements_net_stream_conn *fd; + lo_interface net_stream_conn fd; cid_t reader; /* the lib9p_srv_read_cr() coroutine */ /* mutable */ cr_mutex_t writelock; @@ -197,7 +196,7 @@ static void respond_error(struct _lib9p_srv_req *req) { &host, req->net_bytes); cr_mutex_lock(&sess->parent_conn->writelock); - r = VCALL(sess->parent_conn->fd, write, + r = LO_CALL(sess->parent_conn->fd, write, req->net_bytes, uint32le_decode(req->net_bytes)); cr_mutex_unlock(&sess->parent_conn->writelock); if (r < 0) @@ -206,12 +205,12 @@ static void respond_error(struct _lib9p_srv_req *req) { /* read coroutine *************************************************************/ -static bool read_at_least(implements_net_stream_conn *fd, uint8_t *buf, size_t goal, size_t *done) { +static bool read_at_least(lo_interface net_stream_conn fd, uint8_t *buf, size_t goal, size_t *done) { assert(buf); assert(goal); assert(done); while (*done < goal) { - ssize_t r = VCALL(fd, read, &buf[*done], CONFIG_9P_MAX_MSG_SIZE - *done); + ssize_t r = LO_CALL(fd, read, &buf[*done], CONFIG_9P_MAX_MSG_SIZE - *done); if (r < 0) { nonrespond_errorf("read: %s", net_strerror(-r)); return true; @@ -227,12 +226,12 @@ static bool read_at_least(implements_net_stream_conn *fd, uint8_t *buf, size_t g static void handle_message(struct _lib9p_srv_req *ctx); -[[noreturn]] void lib9p_srv_read_cr(struct lib9p_srv *srv, implements_net_stream_listener *listener) { +[[noreturn]] void lib9p_srv_read_cr(struct lib9p_srv *srv, lo_interface net_stream_listener listener) { uint8_t buf[CONFIG_9P_MAX_MSG_SIZE]; assert(srv); assert(srv->rootdir); - assert(listener); + assert(!LO_IS_NULL(listener)); srv->readers++; @@ -241,10 +240,10 @@ static void handle_message(struct _lib9p_srv_req *ctx); for (;;) { struct _srv_conn conn = { .parent_srv = srv, - .fd = VCALL(listener, accept), + .fd = LO_CALL(listener, accept), .reader = cr_getcid(), }; - if (!conn.fd) { + if (LO_IS_NULL(conn.fd)) { nonrespond_errorf("accept: error"); srv->readers--; if (srv->readers == 0) @@ -305,12 +304,12 @@ static void handle_message(struct _lib9p_srv_req *ctx); _lib9p_srv_reqch_send_req(&srv->_reqch, &req); } close: - VCALL(conn.fd, close, true, sess.reqs.len == 0); + LO_CALL(conn.fd, close, true, sess.reqs.len == 0); if (sess.reqs.len) { sess.closing = true; cr_pause_and_yield(); assert(sess.reqs.len == 0); - VCALL(conn.fd, close, true, true); + LO_CALL(conn.fd, close, true, true); } } } @@ -438,8 +437,8 @@ static void handle_message(struct _lib9p_srv_req *ctx) { goto write; cr_mutex_lock(&ctx->parent_sess->parent_conn->writelock); - VCALL(ctx->parent_sess->parent_conn->fd, write, - ctx->net_bytes, uint32le_decode(ctx->net_bytes)); + LO_CALL(ctx->parent_sess->parent_conn->fd, write, + ctx->net_bytes, uint32le_decode(ctx->net_bytes)); cr_mutex_unlock(&ctx->parent_sess->parent_conn->writelock); } } @@ -470,15 +469,15 @@ static inline bool srv_util_check_perm(struct _lib9p_srv_req *ctx, struct lib9p_ * Returns a pointer to the stored pathinfo. */ static inline struct srv_pathinfo *srv_util_pathsave(struct _lib9p_srv_req *ctx, - implements_lib9p_srv_file *file, + lo_interface lib9p_srv_file file, srv_path_t parent_path) { assert(ctx); - assert(file); + assert(!LO_IS_NULL(file)); - struct lib9p_qid qid = VCALL(file, qid); + struct lib9p_qid qid = LO_CALL(file, qid); struct srv_pathinfo *pathinfo = pathmap_load(&ctx->parent_sess->paths, qid.path); if (pathinfo) - assert(pathinfo->file == file); + assert(LO_EQ(pathinfo->file, file)); else { pathinfo = pathmap_store(&ctx->parent_sess->paths, qid.path, (struct srv_pathinfo){ @@ -512,14 +511,14 @@ static inline void srv_util_pathfree(struct _lib9p_srv_req *ctx, srv_path_t path if (pathinfo->gc_refcount == 0) { if (pathinfo->parent_dir != path) srv_util_pathfree(ctx, pathinfo->parent_dir); - VCALL(pathinfo->file, free); + LO_CALL(pathinfo->file, free); pathmap_del(&ctx->parent_sess->paths, path); } } static inline bool srv_util_pathisdir(struct srv_pathinfo *pathinfo) { assert(pathinfo); - return VCALL(pathinfo->file, qid).type & LIB9P_QT_DIR; + return LO_CALL(pathinfo->file, qid).type & LIB9P_QT_DIR; } /** @@ -532,7 +531,7 @@ static inline struct _srv_fidinfo *srv_util_fidsave(struct _lib9p_srv_req *ctx, assert(fid != LIB9P_FID_NOFID); assert(pathinfo); - struct lib9p_qid qid = VCALL(pathinfo->file, qid); + struct lib9p_qid qid = LO_CALL(pathinfo->file, qid); struct _srv_fidinfo *fidinfo = fidmap_load(&ctx->parent_sess->fids, fid); if (fidinfo) { @@ -703,12 +702,12 @@ static void handle_Tattach(struct _lib9p_srv_req *ctx, } /* 1. File object */ - implements_lib9p_srv_file *root_file = srv->rootdir(&ctx->ctx, req->aname); - assert((root_file == NULL) == lib9p_ctx_has_error(&ctx->ctx.basectx)); + lo_interface lib9p_srv_file root_file = srv->rootdir(&ctx->ctx, req->aname); + assert(LO_IS_NULL(root_file) == lib9p_ctx_has_error(&ctx->ctx.basectx)); if (lib9p_ctx_has_error(&ctx->ctx.basectx)) return; - struct lib9p_qid root_qid = VCALL(root_file, qid); + struct lib9p_qid root_qid = LO_CALL(root_file, qid); assert(root_qid.type & LIB9P_QT_DIR); /* 2. pathinfo */ @@ -770,37 +769,37 @@ static void handle_Twalk(struct _lib9p_srv_req *ctx, break; } - implements_lib9p_srv_file *member_file = VCALL(pathinfo->file, dopen, &ctx->ctx, req->wname[resp->nwqid]); - assert((member_file == NULL) == lib9p_ctx_has_error(&ctx->ctx.basectx)); + lo_interface lib9p_srv_file member_file = LO_CALL(pathinfo->file, dopen, &ctx->ctx, req->wname[resp->nwqid]); + assert(LO_IS_NULL(member_file) == lib9p_ctx_has_error(&ctx->ctx.basectx)); if (lib9p_ctx_has_error(&ctx->ctx.basectx)) break; - new_pathinfo = srv_util_pathsave(ctx, member_file, VCALL(pathinfo->file, qid).path); + new_pathinfo = srv_util_pathsave(ctx, member_file, LO_CALL(pathinfo->file, qid).path); } if (srv_util_pathisdir(new_pathinfo)) { - struct lib9p_stat stat = VCALL(new_pathinfo->file, stat, &ctx->ctx); + struct lib9p_stat stat = LO_CALL(new_pathinfo->file, stat, &ctx->ctx); if (lib9p_ctx_has_error(&ctx->ctx.basectx)) break; lib9p_stat_assert(stat); if (!srv_util_check_perm(ctx, &stat, 0b001)) { lib9p_error(&ctx->ctx.basectx, LINUX_EACCES, "you do not have execute permission on that directory"); - srv_util_pathfree(ctx, VCALL(new_pathinfo->file, qid).path); + srv_util_pathfree(ctx, LO_CALL(new_pathinfo->file, qid).path); break; } } - resp->wqid[resp->nwqid] = VCALL(new_pathinfo->file, qid); + resp->wqid[resp->nwqid] = LO_CALL(new_pathinfo->file, qid); - srv_util_pathfree(ctx, VCALL(pathinfo->file, qid).path); + srv_util_pathfree(ctx, LO_CALL(pathinfo->file, qid).path); pathinfo = new_pathinfo; } if (resp->nwqid == req->nwname) { if (!srv_util_fidsave(ctx, req->newfid, pathinfo, req->newfid == req->fid)) - srv_util_pathfree(ctx, VCALL(pathinfo->file, qid).path); + srv_util_pathfree(ctx, LO_CALL(pathinfo->file, qid).path); } else { assert(lib9p_ctx_has_error(&ctx->ctx.basectx)); - srv_util_pathfree(ctx, VCALL(pathinfo->file, qid).path); + srv_util_pathfree(ctx, LO_CALL(pathinfo->file, qid).path); if (resp->nwqid > 0) lib9p_ctx_clear_error(&ctx->ctx.basectx); } @@ -844,7 +843,7 @@ static void handle_Topen(struct _lib9p_srv_req *ctx, if (reqmode & LIB9P_O_RCLOSE) { struct srv_pathinfo *parent = pathmap_load(&ctx->parent_sess->paths, pathinfo->parent_dir); assert(parent); - struct lib9p_stat parent_stat = VCALL(parent->file, stat, &ctx->ctx); + struct lib9p_stat parent_stat = LO_CALL(parent->file, stat, &ctx->ctx); if (lib9p_ctx_has_error(&ctx->ctx.basectx)) return; lib9p_stat_assert(parent_stat); @@ -855,7 +854,7 @@ static void handle_Topen(struct _lib9p_srv_req *ctx, } fidflags = fidflags | FIDFLAG_RCLOSE; } - struct lib9p_stat stat = VCALL(pathinfo->file, stat, &ctx->ctx); + struct lib9p_stat stat = LO_CALL(pathinfo->file, stat, &ctx->ctx); if (lib9p_ctx_has_error(&ctx->ctx.basectx)) return; lib9p_stat_assert(stat); @@ -893,10 +892,10 @@ static void handle_Topen(struct _lib9p_srv_req *ctx, /* Actually make the call. */ if ((reqmode & LIB9P_O_TRUNC) || (rd && !pathinfo->rd_refcount) || (wr && !pathinfo->wr_refcount) ) { - iounit = VCALL(pathinfo->file, chio, &ctx->ctx, - fidflags & FIDFLAG_OPEN_R, - fidflags & FIDFLAG_OPEN_W, - reqmode & LIB9P_O_TRUNC); + iounit = LO_CALL(pathinfo->file, chio, &ctx->ctx, + fidflags & FIDFLAG_OPEN_R, + fidflags & FIDFLAG_OPEN_W, + reqmode & LIB9P_O_TRUNC); if (lib9p_ctx_has_error(&ctx->ctx.basectx)) return; } @@ -962,7 +961,7 @@ static void handle_Tread(struct _lib9p_srv_req *ctx, return; } /* Do it. */ - size_t num = VCALL(pathinfo->file, dread, &ctx->ctx, (uint8_t *)resp->data, req->count, idx); + size_t num = LO_CALL(pathinfo->file, dread, &ctx->ctx, (uint8_t *)resp->data, req->count, idx); /* Translate object-count back to byte-count. */ uint32_t len = 0; for (size_t i = 0; i < num; i++) { @@ -975,7 +974,7 @@ static void handle_Tread(struct _lib9p_srv_req *ctx, fidinfo->dir_idx = idx+num; fidinfo->dir_off = req->offset + len; } else - resp->count = VCALL(pathinfo->file, pread, &ctx->ctx, resp->data, req->count, req->offset); + resp->count = LO_CALL(pathinfo->file, pread, &ctx->ctx, resp->data, req->count, req->offset); } static void handle_Twrite(struct _lib9p_srv_req *ctx, @@ -1001,7 +1000,7 @@ static void handle_Twrite(struct _lib9p_srv_req *ctx, assert(pathinfo); /* Do it. */ - resp->count = VCALL(pathinfo->file, pwrite, &ctx->ctx, req->data, req->count, req->offset); + resp->count = LO_CALL(pathinfo->file, pwrite, &ctx->ctx, req->data, req->count, req->offset); } static void clunkremove(struct _lib9p_srv_req *ctx, lib9p_fid_t fid, bool remove) { @@ -1024,17 +1023,17 @@ static void clunkremove(struct _lib9p_srv_req *ctx, lib9p_fid_t fid, bool remove } struct srv_pathinfo *parent = pathmap_load(&ctx->parent_sess->paths, pathinfo->parent_dir); assert(parent); - struct lib9p_stat parent_stat = VCALL(parent->file, stat, &ctx->ctx); + struct lib9p_stat parent_stat = LO_CALL(parent->file, stat, &ctx->ctx); if (!srv_util_check_perm(ctx, &parent_stat, 0b010)) { lib9p_error(&ctx->ctx.basectx, LINUX_EACCES, "you do not have write permission on the parent directory"); goto clunk; } - VCALL(pathinfo->file, remove, &ctx->ctx); + LO_CALL(pathinfo->file, remove, &ctx->ctx); } clunk: - srv_util_pathfree(ctx, VCALL(pathinfo->file, qid).path); + srv_util_pathfree(ctx, LO_CALL(pathinfo->file, qid).path); fidmap_del(&ctx->parent_sess->fids, fid); } @@ -1069,7 +1068,7 @@ static void handle_Tstat(struct _lib9p_srv_req *ctx, struct srv_pathinfo *pathinfo = pathmap_load(&ctx->parent_sess->paths, fidinfo->path); assert(pathinfo); - resp->stat = VCALL(pathinfo->file, stat, &ctx->ctx); + resp->stat = LO_CALL(pathinfo->file, stat, &ctx->ctx); if (!lib9p_ctx_has_error(&ctx->ctx.basectx)) lib9p_stat_assert(resp->stat); } diff --git a/lib9p/tests/runtest b/lib9p/tests/runtest index bb83a83..0966000 100755 --- a/lib9p/tests/runtest +++ b/lib9p/tests/runtest @@ -43,7 +43,7 @@ expect_lines \ out=$("${client[@]}" stat 'Documentation/x') expect_lines \ - "'x' 'root' 'root' 'root' q (0000000000000008 1 ) m 0444 at 1728337905 mt 1728337904 l 4 t 0 d 0" + "'x' 'root' 'root' 'root' q (0000000000000001 1 ) m 0444 at 1728337905 mt 1728337904 l 4 t 0 d 0" out=$("${client[@]}" write 'shutdown' <<<1) expect_lines '' diff --git a/lib9p/tests/test_server/main.c b/lib9p/tests/test_server/main.c index 07fc74b..10f67a3 100644 --- a/lib9p/tests/test_server/main.c +++ b/lib9p/tests/test_server/main.c @@ -10,6 +10,7 @@ #include <libcr/coroutine.h> #include <libhw/generic/net.h> #include <libhw/generic/alarmclock.h> +#include <libhw/host_alarmclock.h> #include <libhw/host_net.h> #include <libmisc/macro.h> #include <util9p/static.h> @@ -26,7 +27,7 @@ /* globals ********************************************************************/ -static implements_lib9p_srv_file *get_root(struct lib9p_srv_ctx *, struct lib9p_s); +static lo_interface lib9p_srv_file get_root(struct lib9p_srv_ctx *, struct lib9p_s); const char *hexdig = "0123456789abcdef"; @@ -42,31 +43,31 @@ struct { /* api ************************************************************************/ struct api_file { - implements_lib9p_srv_file; - uint64_t pathnum; }; +LO_IMPLEMENTATION_H(lib9p_srv_file, struct api_file, api) +LO_IMPLEMENTATION_C(lib9p_srv_file, struct api_file, api, static) -static void api_free(implements_lib9p_srv_file *) {} -static uint32_t api_chio(implements_lib9p_srv_file *, struct lib9p_srv_ctx *, bool, bool, bool) { return 0; } - -static void api_wstat(implements_lib9p_srv_file *, struct lib9p_srv_ctx *ctx, struct lib9p_stat) { lib9p_error(&ctx->basectx, LINUX_EROFS, "cannot wstat API file"); } -static void api_remove(implements_lib9p_srv_file *, struct lib9p_srv_ctx *ctx) { lib9p_error(&ctx->basectx, LINUX_EROFS, "cannot remove API file"); } - -static struct lib9p_qid api_qid(implements_lib9p_srv_file *_self) { - struct api_file *self = VCALL_SELF(struct api_file, implements_lib9p_srv_file, _self); +static void api_free(struct api_file *self) { + assert(self); +} +static struct lib9p_qid api_qid(struct api_file *self) { assert(self); - return (struct lib9p_qid){ .type = LIB9P_QT_FILE, .vers = 1, .path = self->pathnum, }; } -static struct lib9p_stat api_stat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *) { - struct api_file *self = VCALL_SELF(struct api_file, implements_lib9p_srv_file, _self); +static uint32_t api_chio(struct api_file *self, struct lib9p_srv_ctx *ctx, bool, bool, bool) { assert(self); + assert(ctx); + return 0; +} +static struct lib9p_stat api_stat(struct api_file *self, struct lib9p_srv_ctx *ctx) { + assert(self); + assert(ctx); return (struct lib9p_stat){ .kern_type = 0, .kern_dev = 0, @@ -85,85 +86,89 @@ static struct lib9p_stat api_stat(implements_lib9p_srv_file *_self, struct lib9p .file_last_modified_n_uid = 0, }; } -static uint32_t api_pwrite(implements_lib9p_srv_file *, struct lib9p_srv_ctx *, void *, uint32_t byte_count, uint64_t) { +static void api_wstat(struct api_file *self, struct lib9p_srv_ctx *ctx, struct lib9p_stat) { + assert(self); + assert(ctx); + lib9p_error(&ctx->basectx, LINUX_EROFS, "cannot wstat API file"); +} +static void api_remove(struct api_file *self, struct lib9p_srv_ctx *ctx) { + assert(self); + assert(ctx); + lib9p_error(&ctx->basectx, LINUX_EROFS, "cannot remove API file"); +} + +LIB9P_SRV_NOTDIR(struct api_file, api) + +static uint32_t api_pwrite(struct api_file *self, struct lib9p_srv_ctx *ctx, void *buf, uint32_t byte_count, uint64_t LM_UNUSED(offset)) { + assert(self); + assert(ctx); + assert(buf); if (byte_count == 0) return 0; for (int i = 0; i < CONFIG_SRV9P_NUM_CONNS; i++) - VCALL(&globals.listeners[i], close); + LO_CALL(lo_box_hostnet_tcplist_as_net_stream_listener(&globals.listeners[i]), close); return byte_count; } - -static struct lib9p_srv_file_vtable api_file_vtable = { - .free = api_free, - .qid = api_qid, - .chio = api_chio, - - .stat = api_stat, - .wstat = api_wstat, - .remove = api_remove, - - .pread = NULL, - .pwrite = api_pwrite, -}; +static uint32_t api_pread(struct api_file *, struct lib9p_srv_ctx *, void *, uint32_t, uint64_t) { + assert_notreached("not readable"); +} /* file tree ******************************************************************/ -#define FILE_COMMON(NAME) { \ - .vtable = &util9p_static_file_vtable, \ - \ - .u_name = "root", .u_num = 0, /* owner user */ \ - .g_name = "root", .g_num = 0, /* owner group */ \ - .m_name = "root", .m_num = 0, /* last-modified-by user */ \ - \ - .pathnum = __COUNTER__, \ - .name = NAME, \ - .perm = 0444, \ - .atime = 1728337905, \ - .mtime = 1728337904, \ - } - -#define DIR_COMMON(NAME) { \ - .vtable = &util9p_static_dir_vtable, \ - \ - .u_name = "root", .u_num = 0, /* owner user */ \ - .g_name = "root", .g_num = 0, /* owner group */ \ - .m_name = "root", .m_num = 0, /* last-modified-by user */ \ - \ - .pathnum = __COUNTER__, \ - .name = NAME, \ - .perm = 0555, \ - .atime = 1728337905, \ - .mtime = 1728337904, \ - } - -#define STATIC_FILE(STRNAME, SYMNAME) \ - &((static struct util9p_static_file){ \ - ._util9p_static_common = FILE_COMMON(STRNAME), \ - .data_start = _binary_static_##SYMNAME##_start, \ - .data_end = _binary_static_##SYMNAME##_end, \ +#define _box(nam, obj) \ + ((struct lib9p_srv_file){ \ + .self = obj, \ + .vtable = (void*)&_lo_##nam##_lib9p_srv_file_vtable, \ }) - -static struct util9p_static_dir root = { - ._util9p_static_common = DIR_COMMON(""), - .members = { - &((static struct util9p_static_dir){ - ._util9p_static_common = DIR_COMMON("Documentation"), - .members = { - STATIC_FILE("x", Documentation_x), - NULL - }, - }), - STATIC_FILE("README.md", README_md), - &((struct api_file){ - .vtable = &api_file_vtable, - .pathnum = __COUNTER__, - }), - NULL, - }, -}; - -static implements_lib9p_srv_file *get_root(struct lib9p_srv_ctx *LM_UNUSED(ctx), struct lib9p_s LM_UNUSED(treename)) { - return &root; +#define lo_box_util9p_static_file_as_lib9p_srv_file(obj) _box(util9p_static_file, obj) +#define lo_box_util9p_static_dir_as_lib9p_srv_file(obj) _box(util9p_static_dir, obj) +#define lo_box_api_as_lib9p_srv_file(obj) _box(api, obj) + +enum { PATH_BASE = __COUNTER__ }; +#define PATH_COUNTER __COUNTER__ - PATH_BASE + +#define STATIC_FILE(STRNAME, SYMNAME) \ + lo_box_util9p_static_file_as_lib9p_srv_file(&((struct util9p_static_file){ \ + ._util9p_static_common = { \ + .u_name = "root", .u_num = 0, /* owner user */ \ + .g_name = "root", .g_num = 0, /* owner group */ \ + .m_name = "root", .m_num = 0, /* last-modified-by user */ \ + \ + .pathnum = PATH_COUNTER, \ + .name = STRNAME, \ + .perm = 0444, \ + .atime = 1728337905, \ + .mtime = 1728337904, \ + }, \ + .data_start = _binary_static_##SYMNAME##_start, \ + .data_end = _binary_static_##SYMNAME##_end, \ + })) + +#define STATIC_DIR(STRNAME, ...) \ + lo_box_util9p_static_dir_as_lib9p_srv_file(&((struct util9p_static_dir){ \ + ._util9p_static_common = { \ + .u_name = "root", .u_num = 0, /* owner user */ \ + .g_name = "root", .g_num = 0, /* owner group */ \ + .m_name = "root", .m_num = 0, /* last-modified-by user */ \ + \ + .pathnum = PATH_COUNTER, \ + .name = STRNAME, \ + .perm = 0555, \ + .atime = 1728337905, \ + .mtime = 1728337904, \ + }, \ + .members = { __VA_ARGS__ __VA_OPT__(,) LO_NULL(lib9p_srv_file) }, \ + })) + +struct lib9p_srv_file root = + STATIC_DIR("", + STATIC_DIR("Documentation", + STATIC_FILE("x", Documentation_x)), + STATIC_FILE("README.md", README_md), + lo_box_api_as_lib9p_srv_file(&(struct api_file){.pathnum = PATH_COUNTER})); + +static lo_interface lib9p_srv_file get_root(struct lib9p_srv_ctx *LM_UNUSED(ctx), struct lib9p_s LM_UNUSED(treename)) { + return root; } /* main ***********************************************************************/ @@ -174,7 +179,7 @@ static COROUTINE read_cr(void *_i) { hostnet_tcp_listener_init(&globals.listeners[i], 9000); - lib9p_srv_read_cr(&globals.srv, &globals.listeners[i]); + lib9p_srv_read_cr(&globals.srv, lo_box_hostnet_tcplist_as_net_stream_listener(&globals.listeners[i])); cr_end(); } @@ -199,6 +204,10 @@ static COROUTINE init_cr(void *) { } int main() { + struct hostclock clock_monotonic = { + .clock_id = CLOCK_MONOTONIC, + }; + bootclock = lo_box_hostclock_as_alarmclock(&clock_monotonic); coroutine_add("init", init_cr, NULL); coroutine_main(); return 0; |