diff options
Diffstat (limited to 'cmd/srv9p')
-rw-r--r-- | cmd/srv9p/CMakeLists.txt | 2 | ||||
-rw-r--r-- | cmd/srv9p/gnet.c | 33 | ||||
-rw-r--r-- | cmd/srv9p/gnet.h | 8 | ||||
-rw-r--r-- | cmd/srv9p/main.c | 30 | ||||
-rw-r--r-- | cmd/srv9p/static9p.c | 156 | ||||
-rw-r--r-- | cmd/srv9p/static9p.h | 10 |
6 files changed, 122 insertions, 117 deletions
diff --git a/cmd/srv9p/CMakeLists.txt b/cmd/srv9p/CMakeLists.txt index 61bd5c2..52e5171 100644 --- a/cmd/srv9p/CMakeLists.txt +++ b/cmd/srv9p/CMakeLists.txt @@ -18,7 +18,7 @@ target_include_directories(srv9p PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/config) target_link_libraries(srv9p libcr libcr_ipc - libnet + libmisc lib9p ) diff --git a/cmd/srv9p/gnet.c b/cmd/srv9p/gnet.c index 92e74c7..d7e8717 100644 --- a/cmd/srv9p/gnet.c +++ b/cmd/srv9p/gnet.c @@ -1,4 +1,4 @@ -/* srv9p/gnet.c - libnet implementation for libcr + GNU libc +/* srv9p/gnet.c - libmisc/net.h implementation for libcr + GNU libc * * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> * SPDX-Licence-Identifier: AGPL-3.0-or-later @@ -20,6 +20,7 @@ #include <signal.h> /* for siginfo_t, struct sigaction, enum sigval, sigaction(), SIGRTMIN, SIGRTMAX, SA_SIGINFO */ #include <libcr/coroutine.h> +#include <libmisc/vcall.h> #include "gnet.h" @@ -71,16 +72,16 @@ static inline bool RUN_PTHREAD(void *(*fn)(void *), void *args) { /* init() ( AKA socket(3) + listen(3) )****************************************/ -static struct libnet_conn *gnet_accept(struct libnet_listener *_listener); -static ssize_t gnet_read(struct libnet_conn *conn, void *buf, size_t count); -static ssize_t gnet_write(struct libnet_conn *conn, void *buf, size_t count); -static int gnet_close(struct libnet_conn *conn, bool rd, bool wr); +static implements_net_conn *gnet_accept(implements_net_listener *_listener); +static ssize_t gnet_read(implements_net_conn *conn, void *buf, size_t count); +static ssize_t gnet_write(implements_net_conn *conn, void *buf, size_t count); +static int gnet_close(implements_net_conn *conn, bool rd, bool wr); -static struct libnet_listener_vtable gnet_listener_vtable = { +static struct net_listener_vtable gnet_listener_vtable = { .accept = gnet_accept, }; -static struct libnet_conn_vtable gnet_conn_vtable = { +static struct net_conn_vtable gnet_conn_vtable = { .read = gnet_read, .write = gnet_write, .close = gnet_close, @@ -131,8 +132,8 @@ static void *_pthread_accept(void *_args) { return NULL; }; -static struct libnet_conn *gnet_accept(struct libnet_listener *_listener) { - struct gnet_listener *listener = (struct gnet_listener *)_listener; +static implements_net_conn *gnet_accept(implements_net_listener *_listener) { + struct gnet_listener *listener = VCALL_SELF(struct gnet_listener, implements_net_listener, _listener); assert(listener); int ret_connfd; @@ -147,7 +148,7 @@ static struct libnet_conn *gnet_accept(struct libnet_listener *_listener) { listener->active_conn.vtable = &gnet_conn_vtable; listener->active_conn.fd = ret_connfd; - return (struct libnet_conn *)&listener->active_conn; + return &listener->active_conn; } /* read() *********************************************************************/ @@ -172,8 +173,8 @@ static void *_pthread_read(void *_args) { return NULL; }; -static ssize_t gnet_read(struct libnet_conn *_conn, void *buf, size_t count) { - struct _gnet_conn *conn = (struct _gnet_conn *)_conn; +static ssize_t gnet_read(implements_net_conn *_conn, void *buf, size_t count) { + struct _gnet_conn *conn = VCALL_SELF(struct _gnet_conn, implements_net_conn, _conn); assert(conn); ssize_t ret; @@ -222,8 +223,8 @@ static void *_pthread_write(void *_args) { return NULL; }; -static ssize_t gnet_write(struct libnet_conn *_conn, void *buf, size_t count) { - struct _gnet_conn *conn = (struct _gnet_conn *)_conn; +static ssize_t gnet_write(implements_net_conn *_conn, void *buf, size_t count) { + struct _gnet_conn *conn = VCALL_SELF(struct _gnet_conn, implements_net_conn, _conn); assert(conn); ssize_t ret; @@ -244,8 +245,8 @@ static ssize_t gnet_write(struct libnet_conn *_conn, void *buf, size_t count) { /* close() ********************************************************************/ -static int gnet_close(struct libnet_conn *_conn, bool rd, bool wr) { - struct _gnet_conn *conn = (struct _gnet_conn *)_conn; +static int gnet_close(implements_net_conn *_conn, bool rd, bool wr) { + struct _gnet_conn *conn = VCALL_SELF(struct _gnet_conn, implements_net_conn, _conn); assert(conn); int how; diff --git a/cmd/srv9p/gnet.h b/cmd/srv9p/gnet.h index d43874f..01f2d40 100644 --- a/cmd/srv9p/gnet.h +++ b/cmd/srv9p/gnet.h @@ -1,4 +1,4 @@ -/* srv9p/gnet.h - libnet implementation for libcr + GNU libc +/* srv9p/gnet.h - libmisc/net.h implementation for libcr + GNU libc * * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> * SPDX-Licence-Identifier: AGPL-3.0-or-later @@ -9,16 +9,16 @@ #include <stdint.h> /* for uint16_6 */ -#include <libnet/libnet.h> +#include <libmisc/net.h> struct _gnet_conn { - struct libnet_conn_vtable *vtable; + implements_net_conn; int fd; }; struct gnet_listener { - struct libnet_listener_vtable *vtable; + implements_net_listener; int fd; struct _gnet_conn active_conn; diff --git a/cmd/srv9p/main.c b/cmd/srv9p/main.c index ae0e34e..d83fac4 100644 --- a/cmd/srv9p/main.c +++ b/cmd/srv9p/main.c @@ -8,7 +8,7 @@ #include <stdio.h> #include <libcr/coroutine.h> -#include <libnet/libnet.h> +#include <libmisc/net.h> #include <lib9p/srv.h> #include "static9p.h" @@ -29,8 +29,8 @@ /* file tree ******************************************************************/ -#define FILE_METADATA(NAME) { \ - .header = { .vtable = &static_file_vtable }, \ +#define FILE_COMMON(NAME) { \ + .vtable = &static_file_vtable, \ \ .u_name = "root", .u_num = 0, /* owner user */ \ .g_name = "root", .g_num = 0, /* owner group */ \ @@ -43,8 +43,8 @@ .mtime = 1728337904, \ } -#define DIR_METADATA(NAME) { \ - .header = { .vtable = &static_dir_vtable }, \ +#define DIR_COMMON(NAME) { \ + .vtable = &static_dir_vtable, \ \ .u_name = "root", .u_num = 0, /* owner user */ \ .g_name = "root", .g_num = 0, /* owner group */ \ @@ -59,28 +59,28 @@ #define STATIC_FILE(STRNAME, SYMNAME) \ &((static struct static_file){ \ - .metadata = FILE_METADATA(STRNAME), \ - .data_start = _binary_static_##SYMNAME##_start, \ - .data_end = _binary_static_##SYMNAME##_end, \ - }).metadata.header + ._static_common = FILE_COMMON(STRNAME), \ + .data_start = _binary_static_##SYMNAME##_start, \ + .data_end = _binary_static_##SYMNAME##_end, \ + }) static struct static_dir root = { - .metadata = DIR_METADATA(""), + ._static_common = DIR_COMMON(""), .members = { &((static struct static_dir){ - .metadata = DIR_METADATA("Documentation"), + ._static_common = DIR_COMMON("Documentation"), .members = { STATIC_FILE("x", Documentation_x), NULL }, - }).metadata.header, + }), STATIC_FILE("README.md", README_md), NULL, }, }; -static struct lib9p_srv_file *get_root(struct lib9p_srv_ctx *UNUSED(ctx), char *UNUSED(treename)) { - return &root.metadata.header; +static implements_lib9p_srv_file *get_root(struct lib9p_srv_ctx *UNUSED(ctx), char *UNUSED(treename)) { + return &root; } /* main ***********************************************************************/ @@ -94,7 +94,7 @@ static COROUTINE read_cr(void *_srv) { struct gnet_listener listener; gnet_listener_init(&listener, 9000); - lib9p_srv_read_cr(srv, (struct libnet_listener *)&listener); + lib9p_srv_read_cr(srv, &listener); cr_end(); } diff --git a/cmd/srv9p/static9p.c b/cmd/srv9p/static9p.c index f508ff1..47c07be 100644 --- a/cmd/srv9p/static9p.c +++ b/cmd/srv9p/static9p.c @@ -6,61 +6,63 @@ #include <assert.h> +#include <libmisc/vcall.h> + #include "static9p.h" #define UNUSED(name) /* name __attribute__((unused)) */ #define p9_str(cstr) ((struct lib9p_s){ .len = strlen(cstr), .utf8 = cstr }) #define p9_nulstr ((struct lib9p_s){ .len = 0, .utf8 = NULL }) -/* common metadata ************************************************************/ +/* common *********************************************************************/ -static struct lib9p_srv_file *static_metadata_clone(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { +static implements_lib9p_srv_file *static_common_clone(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { + _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_metadata *file = (struct static_metadata *)_file; - assert(file); - return &file->header; + return self; } -static void static_metadata_free(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { +static void static_common_free(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { + _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_metadata *file = (struct static_metadata *)_file; - assert(file); /* do nothing */ } -static uint32_t static_metadata_io(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file, lib9p_o_t UNUSED(flags)) { +static uint32_t static_common_io(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, lib9p_o_t UNUSED(flags)) { + _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_metadata *file = (struct static_metadata *)_file; - assert(file); return 0; } -static void static_metadata_wstat(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file, +static void static_common_wstat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, struct lib9p_stat UNUSED(new)) { + _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_metadata *file = (struct static_metadata *)_file; - assert(file); lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); } -static void static_metadata_remove(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { +static void static_common_remove(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { + _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_metadata *file = (struct static_metadata *)_file; - assert(file); lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); } /* dir ************************************************************************/ -static struct lib9p_stat static_dir_stat(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { +static struct lib9p_stat static_dir_stat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { + struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_dir *file = (struct static_dir *)_file; - assert(file); return (struct lib9p_stat){ .kern_type = 0, @@ -68,32 +70,32 @@ static struct lib9p_stat static_dir_stat(struct lib9p_srv_ctx *ctx, struct lib9p .file_qid = { .type = LIB9P_QT_DIR, .vers = 1, - .path = file->metadata.pathnum, + .path = self->pathnum, }, - .file_mode = LIB9P_DM_DIR | (file->metadata.perm & 0555), - .file_atime = file->metadata.atime, - .file_mtime = file->metadata.mtime, + .file_mode = LIB9P_DM_DIR | (self->perm & 0555), + .file_atime = self->atime, + .file_mtime = self->mtime, .file_size = 0, - .file_name = p9_str(file->metadata.name), - .file_owner_uid = p9_str(file->metadata.u_name), - .file_owner_gid = p9_str(file->metadata.g_name), - .file_last_modified_uid = p9_str(file->metadata.m_name), + .file_name = p9_str(self->name), + .file_owner_uid = p9_str(self->u_name), + .file_owner_gid = p9_str(self->g_name), + .file_last_modified_uid = p9_str(self->m_name), .file_extension = p9_nulstr, - .file_owner_n_uid = file->metadata.u_num, - .file_owner_n_gid = file->metadata.g_num, - .file_last_modified_n_uid = file->metadata.m_num, + .file_owner_n_uid = self->u_num, + .file_owner_n_gid = self->g_num, + .file_last_modified_n_uid = self->m_num, }; } -static struct lib9p_srv_file *static_dir_dopen(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir, +static implements_lib9p_srv_file *static_dir_dopen(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, char *childname) { + struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_dir *dir = (struct static_dir *)_dir; - assert(dir); - for (size_t i = 0; dir->members[i]; i++) { - struct lib9p_srv_file *filep = dir->members[i]; - struct lib9p_stat stat = filep->vtable->stat(ctx, filep); + for (size_t i = 0; self->members[i]; i++) { + implements_lib9p_srv_file *filep = self->members[i]; + struct lib9p_stat stat = VCALL(filep, stat, ctx); if (lib9p_ctx_has_error(&ctx->basectx)) break; lib9p_assert_stat(stat); @@ -105,30 +107,30 @@ static struct lib9p_srv_file *static_dir_dopen(struct lib9p_srv_ctx *ctx, struct return NULL; } -static struct lib9p_srv_file *static_dir_dcreate(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir, +static implements_lib9p_srv_file *static_dir_dcreate(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, char *UNUSED(childname), lib9p_dm_t UNUSED(perm), lib9p_o_t UNUSED(flags)) { + struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_dir *dir = (struct static_dir *)_dir; - assert(dir); lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); return NULL; } -static size_t static_dir_dread(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir, +static size_t static_dir_dread(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, uint8_t *buf, uint32_t byte_count, size_t _obj_offset) { + struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_dir *dir = (struct static_dir *)_dir; - assert(dir); uint32_t byte_offset = 0; size_t obj_offset = _obj_offset; - while (dir->members[obj_offset]) { - struct lib9p_srv_file *filep = dir->members[obj_offset]; - struct lib9p_stat stat = filep->vtable->stat(ctx, filep); + while (self->members[obj_offset]) { + implements_lib9p_srv_file *filep = self->members[obj_offset]; + struct lib9p_stat stat = VCALL(filep, stat, ctx); if (lib9p_ctx_has_error(&ctx->basectx)) break; lib9p_assert_stat(stat); @@ -147,13 +149,13 @@ static size_t static_dir_dread(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file } struct lib9p_srv_file_vtable static_dir_vtable = { - .clone = static_metadata_clone, - .free = static_metadata_free, + .clone = static_common_clone, + .free = static_common_free, - .io = static_metadata_io, + .io = static_common_io, .stat = static_dir_stat, - .wstat = static_metadata_wstat, - .remove = static_metadata_remove, + .wstat = static_common_wstat, + .remove = static_common_remove, .dopen = static_dir_dopen, .dcreate = static_dir_dcreate, @@ -166,16 +168,18 @@ struct lib9p_srv_file_vtable static_dir_vtable = { static inline size_t static_file_size(struct static_file *file) { assert(file); +#if __x86_64__ assert(file->data_start); +#endif if (!file->data_end) return file->data_size; return (size_t)((uintptr_t)file->data_end - (uintptr_t)file->data_start); } -static struct lib9p_stat static_file_stat(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { +static struct lib9p_stat static_file_stat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { + struct static_file *self = VCALL_SELF(struct static_file, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_file *file = (struct static_file *)_file; - assert(file); return (struct lib9p_stat){ .kern_type = 0, @@ -183,32 +187,32 @@ static struct lib9p_stat static_file_stat(struct lib9p_srv_ctx *ctx, struct lib9 .file_qid = { .type = LIB9P_QT_FILE, .vers = 1, - .path = file->metadata.pathnum, + .path = self->pathnum, }, - .file_mode = file->metadata.perm & 0444, - .file_atime = file->metadata.atime, - .file_mtime = file->metadata.mtime, - .file_size = (uint64_t)static_file_size(file), - .file_name = p9_str(file->metadata.name), - .file_owner_uid = p9_str(file->metadata.u_name), - .file_owner_gid = p9_str(file->metadata.g_name), - .file_last_modified_uid = p9_str(file->metadata.m_name), + .file_mode = self->perm & 0444, + .file_atime = self->atime, + .file_mtime = self->mtime, + .file_size = (uint64_t)static_file_size(self), + .file_name = p9_str(self->name), + .file_owner_uid = p9_str(self->u_name), + .file_owner_gid = p9_str(self->g_name), + .file_last_modified_uid = p9_str(self->m_name), .file_extension = p9_nulstr, - .file_owner_n_uid = file->metadata.u_num, - .file_owner_n_gid = file->metadata.g_num, - .file_last_modified_n_uid = file->metadata.m_num, + .file_owner_n_uid = self->u_num, + .file_owner_n_gid = self->g_num, + .file_last_modified_n_uid = self->m_num, }; } -static uint32_t static_file_pread(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file, +static uint32_t static_file_pread(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, void *buf, uint32_t byte_count, uint64_t byte_offset) { + struct static_file *self = VCALL_SELF(struct static_file, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_file *file = (struct static_file *)_file; - assert(file); - size_t data_size = static_file_size(file); + size_t data_size = static_file_size(self); if (byte_offset > (uint64_t)data_size) { lib9p_error(&ctx->basectx, @@ -220,18 +224,18 @@ static uint32_t static_file_pread(struct lib9p_srv_ctx *ctx, struct lib9p_srv_fi size_t end_off = beg_off + (size_t)byte_count; if (end_off > data_size) end_off = data_size; - memcpy(buf, &file->data_start[beg_off], end_off-beg_off); + memcpy(buf, &self->data_start[beg_off], end_off-beg_off); return (uint32_t)(end_off-beg_off); } struct lib9p_srv_file_vtable static_file_vtable = { - .clone = static_metadata_clone, - .free = static_metadata_free, + .clone = static_common_clone, + .free = static_common_free, - .io = static_metadata_io, + .io = static_common_io, .stat = static_file_stat, - .wstat = static_metadata_wstat, - .remove = static_metadata_remove, + .wstat = static_common_wstat, + .remove = static_common_remove, .pread = static_file_pread, .pwrite = NULL, diff --git a/cmd/srv9p/static9p.h b/cmd/srv9p/static9p.h index 92f5909..98b85a2 100644 --- a/cmd/srv9p/static9p.h +++ b/cmd/srv9p/static9p.h @@ -9,8 +9,8 @@ #include <lib9p/srv.h> -struct static_metadata { - struct lib9p_srv_file header; +typedef struct { + implements_lib9p_srv_file; char *u_name; uint32_t u_num; @@ -23,10 +23,10 @@ struct static_metadata { char *name; lib9p_dm_t perm; uint32_t atime, mtime; -}; +} _static_common; struct static_dir { - struct static_metadata metadata; + _static_common; /* NULL-terminated */ struct lib9p_srv_file *members[]; @@ -34,7 +34,7 @@ struct static_dir { struct static_file { - struct static_metadata metadata; + _static_common; char *data_start; /* must not be NULL */ char *data_end; /* may be NULL, in which case data_size is used */ |