From 8a9fc1704dcf3ec117a3bf37fd70a44a43873659 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Thu, 29 May 2025 23:39:12 -0400 Subject: lib9p: Use errnums internally --- lib9p/core_include/lib9p/core.h | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) (limited to 'lib9p/core_include') diff --git a/lib9p/core_include/lib9p/core.h b/lib9p/core_include/lib9p/core.h index 27553a3..7bee2f1 100644 --- a/lib9p/core_include/lib9p/core.h +++ b/lib9p/core_include/lib9p/core.h @@ -51,9 +51,7 @@ struct lib9p_ctx { uint32_t max_msg_size; /* state */ -#if CONFIG_9P_ENABLE_9P2000_u || CONFIG_9P_ENABLE_9P2000_L - lib9p_errno_t err_num; -#endif + _errnum err_num; [[gnu::nonstring]] char err_msg[CONFIG_9P_MAX_ERR_SIZE]; }; @@ -61,16 +59,10 @@ void lib9p_ctx_clear_error(struct lib9p_ctx *ctx); bool lib9p_ctx_has_error(struct lib9p_ctx *ctx); -#if CONFIG_9P_ENABLE_9P2000_u || CONFIG_9P_ENABLE_9P2000_L - #define _lib9p_set_err_num(ctx, linux_errno) do { (ctx)->err_num = linux_errno; } while (0) -#else - #define _lib9p_set_err_num(ctx, linux_errno) do { } while (0) -#endif - /** Write a -style error into ctx, return -1. */ -#define lib9p_error(ctx, linux_errno, ...) ({ \ +#define lib9p_error(ctx, libmisc_errno, ...) ({ \ if (!lib9p_ctx_has_error(ctx)) { \ - _lib9p_set_err_num(ctx, linux_errno); \ + (ctx)->err_num = libmisc_errno; \ struct fmt_buf _w = { \ .dat = (ctx)->err_msg, \ .cap = sizeof((ctx)->err_msg), \ @@ -105,12 +97,12 @@ void fmt_print_lib9p_msg(lo_interface fmt_dest w, struct lib9p_ctx *ctx, enum li * * @return required size, or -1 on error * - * @errno L_EOPNOTSUPP: message is an R-message - * @errno L_EOPNOTSUPP: message has unknown type - * @errno L_EBADMSG: message is wrong size for content - * @errno L_EILSEQ: message contains invalid UTF-8, or the UTF-8 contains a nul-byte - * @errno L_EBADMSG: message contains a bitfield with unknown bits - * @errno L_EMSGSIZE: would-be return value overflows SSIZE_MAX + * @errno E_POSIX_EOPNOTSUPP: message is an R-message + * @errno E_POSIX_EOPNOTSUPP: message has unknown type + * @errno E_POSIX_EBADMSG: message is wrong size for content + * @errno E_POSIX_EILSEQ: message contains invalid UTF-8, or the UTF-8 contains a nul-byte + * @errno E_POSIX_EBADMSG: message contains a bitfield with unknown bits + * @errno E_POSIX_EMSGSIZE: would-be return value overflows SSIZE_MAX */ ssize_t lib9p_Tmsg_validate(struct lib9p_ctx *ctx, uint8_t *net_bytes); @@ -145,7 +137,7 @@ void lib9p_Tmsg_unmarshal(struct lib9p_ctx *ctx, uint8_t *net_bytes, * @return ret : the buffer to encode to * @return whether there was an error (false=success, true=error) * - * @errno L_ERANGE: reply does not fit in ctx->max_msg_size + * @errno E_POSIX_ERANGE: reply does not fit in ctx->max_msg_size */ bool lib9p_Tmsg_marshal(struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body, struct lib9p_Tmsg_send_buf *ret); -- cgit v1.2.3-2-g168b From 945756b1b050bdf09d1119854cc5b22ad15efacd Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Thu, 29 May 2025 22:25:02 -0400 Subject: lib9p_core: Switch to use error.h --- lib9p/core_include/lib9p/core.h | 57 ++++++++++------------------------------- 1 file changed, 14 insertions(+), 43 deletions(-) (limited to 'lib9p/core_include') diff --git a/lib9p/core_include/lib9p/core.h b/lib9p/core_include/lib9p/core.h index 7bee2f1..9d92312 100644 --- a/lib9p/core_include/lib9p/core.h +++ b/lib9p/core_include/lib9p/core.h @@ -20,11 +20,6 @@ #include /* _after_ other includes, including config.h */ -#ifndef CONFIG_9P_MAX_ERR_SIZE - #error config.h must define CONFIG_9P_MAX_ERR_SIZE -#endif -static_assert(CONFIG_9P_MAX_ERR_SIZE <= UINT16_MAX); - /* constants ******************************************************************/ enum { @@ -49,32 +44,8 @@ struct lib9p_ctx { /* negotiated */ enum lib9p_version version; uint32_t max_msg_size; - - /* state */ - _errnum err_num; - [[gnu::nonstring]] char err_msg[CONFIG_9P_MAX_ERR_SIZE]; }; -void lib9p_ctx_clear_error(struct lib9p_ctx *ctx); - -bool lib9p_ctx_has_error(struct lib9p_ctx *ctx); - -/** Write a -style error into ctx, return -1. */ -#define lib9p_error(ctx, libmisc_errno, ...) ({ \ - if (!lib9p_ctx_has_error(ctx)) { \ - (ctx)->err_num = libmisc_errno; \ - struct fmt_buf _w = { \ - .dat = (ctx)->err_msg, \ - .cap = sizeof((ctx)->err_msg), \ - }; \ - lo_interface fmt_dest w = LO_BOX(fmt_dest, &_w); \ - fmt_print(w, __VA_ARGS__); \ - if (_w.len < _w.cap) \ - memset(_w.dat + _w.len, 0, _w.cap - _w.len); \ - } \ - -1; \ -}) - /* misc utilities *************************************************************/ uint32_t lib9p_version_min_Rerror_size(enum lib9p_version); @@ -92,10 +63,10 @@ void fmt_print_lib9p_msg(lo_interface fmt_dest w, struct lib9p_ctx *ctx, enum li * number may be larger than net_bytes due to (1) struct padding, (2) * array pointers. * - * @param ctx : negotiated protocol parameters, where to record errors + * @param ctx : negotiated protocol parameters * @param net_bytes : the complete request, starting with the "size[4]" * - * @return required size, or -1 on error + * @return required size, or an error * * @errno E_POSIX_EOPNOTSUPP: message is an R-message * @errno E_POSIX_EOPNOTSUPP: message has unknown type @@ -104,7 +75,7 @@ void fmt_print_lib9p_msg(lo_interface fmt_dest w, struct lib9p_ctx *ctx, enum li * @errno E_POSIX_EBADMSG: message contains a bitfield with unknown bits * @errno E_POSIX_EMSGSIZE: would-be return value overflows SSIZE_MAX */ -ssize_t lib9p_Tmsg_validate(struct lib9p_ctx *ctx, uint8_t *net_bytes); +size_t_or_error lib9p_Tmsg_validate(struct lib9p_ctx *ctx, uint8_t *net_bytes); /** * Unmarshal the 9P message `net_bytes` into the C struct `ret_body`. @@ -130,27 +101,27 @@ void lib9p_Tmsg_unmarshal(struct lib9p_ctx *ctx, uint8_t *net_bytes, * marshal bitfield bits that aren't in ctx->version; it applies a * version-specific mask to bitfields. * - * @param ctx : negotiated protocol parameters, where to record errors + * @param ctx : negotiated protocol parameters * @param typ : the message type * @param msg : the message to encode (`struct lib9p_msg_XXXX` according to `typ`) * * @return ret : the buffer to encode to - * @return whether there was an error (false=success, true=error) + * @return error * * @errno E_POSIX_ERANGE: reply does not fit in ctx->max_msg_size */ -bool lib9p_Tmsg_marshal(struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body, - struct lib9p_Tmsg_send_buf *ret); +error lib9p_Tmsg_marshal(struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body, + struct lib9p_Tmsg_send_buf *ret); /* main R-message functions ***************************************************/ /** Same as above, but for R-messages instead of T-messages. */ -ssize_t lib9p_Rmsg_validate(struct lib9p_ctx *ctx, uint8_t *net_bytes); +size_t_or_error lib9p_Rmsg_validate(struct lib9p_ctx *ctx, uint8_t *net_bytes); void lib9p_Rmsg_unmarshal(struct lib9p_ctx *ctx, uint8_t *net_bytes, enum lib9p_msg_type *ret_typ, void *ret_body); -bool lib9p_Rmsg_marshal(struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body, - struct lib9p_Rmsg_send_buf *ret); +error lib9p_Rmsg_marshal(struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body, + struct lib9p_Rmsg_send_buf *ret); /* `struct lib9p_stat` helpers ************************************************/ @@ -162,16 +133,16 @@ void fmt_print_lib9p_stat(lo_interface fmt_dest w, struct lib9p_ctx *ctx, struct /** * Validate a message's `stat` structure. * - * @param ctx : negotiated protocol parameters, where to record errors + * @param ctx : negotiated protocol parameters * @param net_bytes : network-encoded stat structure * @param net_size : the number of net_bytes that may be read * * @return ret_net_size : number of bytes consumed; <=net_size * @return ret_host_size : number of bytes that lib9p_stat_unmarshal would take - * @return whether there was an error + * @return error */ -bool lib9p_stat_validate(struct lib9p_ctx *ctx, uint32_t net_size, uint8_t *net_bytes, - uint32_t *ret_net_size, size_t *ret_host_size); +error lib9p_stat_validate(struct lib9p_ctx *ctx, uint32_t net_size, uint8_t *net_bytes, + uint32_t *ret_net_size, size_t *ret_host_size); /** * Unmarshal the 9P `net_bytes` into the C struct `ret_obj`. -- cgit v1.2.3-2-g168b