diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-06-06 23:00:33 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-06-06 23:00:33 -0600 |
commit | b5a29a4c57f09ed11b4591f99493fd57fdcee593 (patch) | |
tree | c558294e0237a31b2fca737437bfa82486834c4e | |
parent | ce8ae41d677875adb45d99c351bcba108fb82a44 (diff) | |
parent | 940ac0d72be1e6d13b924f3ca2d32bad5b41b6ab (diff) |
Merge branch 'lukeshu/misc'
-rw-r--r-- | cmd/sbc_harness/config/config.h | 2 | ||||
-rw-r--r-- | lib9p/CMakeLists.txt | 3 | ||||
-rw-r--r-- | lib9p/core.c | 1 | ||||
-rw-r--r-- | lib9p/core_gen/h.py | 2 | ||||
-rw-r--r-- | lib9p/core_include/lib9p/_core_generated.h | 2 | ||||
-rw-r--r-- | lib9p/core_include/lib9p/core.h | 8 | ||||
-rw-r--r-- | lib9p/tests/testclient-hangup.c | 100 | ||||
-rw-r--r-- | lib9p/tests/testclient-hangup.explog | 14 | ||||
-rw-r--r-- | lib9p/tests/testclient-sess.c | 2 | ||||
-rw-r--r-- | libmisc/tests/test_assert.c | 1 | ||||
-rw-r--r-- | libmisc/tests/test_log.c | 1 |
11 files changed, 122 insertions, 14 deletions
diff --git a/cmd/sbc_harness/config/config.h b/cmd/sbc_harness/config/config.h index ca23462..7a91001 100644 --- a/cmd/sbc_harness/config/config.h +++ b/cmd/sbc_harness/config/config.h @@ -49,8 +49,6 @@ #define CONFIG_9P_SRV_DEBUG 1 /* bool */ -#define CONFIG_9P_SRV_MAX_MSG_SIZE ((4*1024)+24) - /** * This max-msg-size is sized so that a Twrite message can return * 8KiB of data. diff --git a/lib9p/CMakeLists.txt b/lib9p/CMakeLists.txt index 2a2f858..3433417 100644 --- a/lib9p/CMakeLists.txt +++ b/lib9p/CMakeLists.txt @@ -48,6 +48,9 @@ if (ENABLE_TESTS) add_lib9p_executable("testclient-sess") add_lib9p_test("./testclient-sess") + add_lib9p_executable("testclient-hangup") + add_lib9p_test("./testclient-hangup") + set(cfg_matrix "CONFIG_9P_SRV_DEBUG;[0;1]" "CONFIG_9P_ENABLE_9P2000;[0;1]" diff --git a/lib9p/core.c b/lib9p/core.c index 942db05..d64e23a 100644 --- a/lib9p/core.c +++ b/lib9p/core.c @@ -4,7 +4,6 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -#include <stdarg.h> /* for va_* */ #include <string.h> /* for strlen(), strnlen(), strncpy(), memcmp(), memset() */ #include <libmisc/assert.h> /* for assert() */ diff --git a/lib9p/core_gen/h.py b/lib9p/core_gen/h.py index c7c832b..8c381f8 100644 --- a/lib9p/core_gen/h.py +++ b/lib9p/core_gen/h.py @@ -169,8 +169,6 @@ def gen_h(versions: set[str], typs: list[idl.UserType]) -> str: ret += """ /* config *********************************************************************/ - -#include "config.h" """ for ver in sorted(versions): ret += "\n" diff --git a/lib9p/core_include/lib9p/_core_generated.h b/lib9p/core_include/lib9p/_core_generated.h index d1ecd6e..de7795a 100644 --- a/lib9p/core_include/lib9p/_core_generated.h +++ b/lib9p/core_include/lib9p/_core_generated.h @@ -6,8 +6,6 @@ /* config *********************************************************************/ -#include "config.h" - #ifndef CONFIG_9P_ENABLE_9P2000 #error config.h must define CONFIG_9P_ENABLE_9P2000 #endif diff --git a/lib9p/core_include/lib9p/core.h b/lib9p/core_include/lib9p/core.h index 1fb71f1..27553a3 100644 --- a/lib9p/core_include/lib9p/core.h +++ b/lib9p/core_include/lib9p/core.h @@ -16,7 +16,9 @@ #include <libmisc/fmt.h> #define CONFIG_9P_ENABLE_uninitialized 1 -#include <lib9p/_core_generated.h> +#include "config.h" + +#include <lib9p/_core_generated.h> /* _after_ other includes, including config.h */ #ifndef CONFIG_9P_MAX_ERR_SIZE #error config.h must define CONFIG_9P_MAX_ERR_SIZE @@ -202,14 +204,12 @@ void lib9p_stat_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 max_net_size : the maximum network-encoded size to allow * @param obj : the message to encode * * @return ret_bytes: the buffer to encode into * @return the number of bytes written, or 0 if the stat object does not fit in max_net_size - * - * @errno L_ERANGE: reply does not fit in max_net_size */ uint32_t lib9p_stat_marshal(struct lib9p_ctx *ctx, uint32_t max_net_size, struct lib9p_stat *obj, uint8_t *ret_bytes); diff --git a/lib9p/tests/testclient-hangup.c b/lib9p/tests/testclient-hangup.c new file mode 100644 index 0000000..a819e80 --- /dev/null +++ b/lib9p/tests/testclient-hangup.c @@ -0,0 +1,100 @@ +/* lib9p/tests/testclient-hangup.c - Test the 9P `test_server`'s handling of TCP hangups + * + * Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include <arpa/inet.h> /* for htons(), inet_addr() */ +#include <errno.h> +#include <netinet/in.h> /* for struct sockaddr{,_in} */ +#include <stdlib.h> /* for atoi() */ +#include <sys/socket.h> /* for socket(), connect() */ +#include <sys/uio.h> /* for writev() */ +#include <unistd.h> /* for read() */ + +#define error __error +#include <error.h> +#undef error + +#include <lib9p/core.h> +#include <libmisc/assert.h> +#include <libmisc/endian.h> + +#define MAX_MSG_SIZE (8*1024) + +static void _send9p(int fd, struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body) { + struct lib9p_Tmsg_send_buf buf; + bool err = lib9p_Tmsg_marshal(ctx, typ, body, &buf); + assert(!err); + size_t exp = 0; + for (size_t i = 0; i < buf.iov_cnt; i++) + exp += buf.iov[i].iov_len; + ssize_t act = writev(fd, buf.iov, buf.iov_cnt); + if (act < 0) + __error(1, errno, "writev"); + assert((size_t)act == exp); +} + +#define send9p(typ, ...) _send9p(fd, &ctx, LIB9P_TYP_##typ, &((struct lib9p_msg_##typ){ __VA_ARGS__ })) + +static void _recv9p(int fd) { + uint8_t buf[MAX_MSG_SIZE]; + size_t goal = 4; + size_t done = 0; + while (done < goal) { + ssize_t n = read(fd, &buf[done], goal-done); + if (n < 0) + __error(1, errno, "read"); + done += n; + } + goal = uint32le_decode(buf); + assert(goal <= MAX_MSG_SIZE); + while (done < goal) { + ssize_t n = read(fd, &buf[done], goal-done); + if (n < 0) + __error(1, errno, "read"); + done += n; + } +} + +#define recv9p() _recv9p(fd) + +int main(int argc, char *argv[]) { + if (argc != 2) + __error(2, 0, "Usage: %s SERVER_PORT", argv[0]); + uint16_t server_port = atoi(argv[1]); + + union { + struct sockaddr gen; + struct sockaddr_in in; + } server_addr = {}; + server_addr.in.sin_family = AF_INET; + server_addr.in.sin_addr.s_addr = inet_addr("127.0.0.1"); + server_addr.in.sin_port = htons(server_port); + + int fd = socket(AF_INET, SOCK_STREAM, 0); + if (fd < 0) + __error(1, errno, "socket"); + if (connect(fd, &server_addr.gen, sizeof(server_addr)) < 0) + __error(1, errno, "connect"); + + struct lib9p_ctx ctx = { + .max_msg_size = 16*1024, + }; + + struct lib9p_s wname[1]; + + /**********************************************************************/ + + send9p(Tversion, .tag=0, .max_msg_size=(8*1024), .version=lib9p_str("9P2000")); + recv9p(); /* Rversion */ + ctx.version = LIB9P_VER_9P2000; + send9p(Tattach, .tag=0, .fid=0, .afid=LIB9P_FID_NOFID, .uname=lib9p_str("nobody"), .aname=lib9p_str("")); + recv9p(); /* Rattach */ + wname[0] = lib9p_str("shutdown"); send9p(Twalk, .tag=0, .fid=0, .newfid=0, .nwname=1, .wname=wname); + recv9p(); /* Rwalk */ + send9p(Topen, .tag=0, .fid=0, .mode=LIB9P_O_MODE_WRITE); + recv9p(); /* Ropen */ + send9p(Twrite, .tag=0, .fid=0, .offset=0, .count=2, .data="1\n"); + return 0; /* Hang up without waiting for Rwrite. */ +} diff --git a/lib9p/tests/testclient-hangup.explog b/lib9p/tests/testclient-hangup.explog new file mode 100644 index 0000000..568b0fc --- /dev/null +++ b/lib9p/tests/testclient-hangup.explog @@ -0,0 +1,14 @@ +# lib9p/tests/testclient-hangup.explog - Expected 9P logfile of testclient-hangup +# +# Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com> +# SPDX-License-Identifier: AGPL-3.0-or-later +> Tversion { tag=0 max_msg_size=8192 version="9P2000" } +< Rversion { tag=0 max_msg_size=4120 version="9P2000" } +> Tattach { tag=0 fid=0 afid=NOFID uname="nobody" aname="" unum=0 } +< Rattach { tag=0 qid={ type=(DIR) vers=1 path=1 } } +> Twalk { tag=0 fid=0 newfid=0 nwname=1 wname=[ "shutdown" ] } +< Rwalk { tag=0 nwqid=1 wqid=[ { type=(APPEND) vers=1 path=5 } ] } +> Topen { tag=0 fid=0 mode=(MODE_WRITE) } +< Ropen { tag=0 qid={ type=(APPEND) vers=1 path=5 } iounit=0 } +> Twrite { tag=0 fid=0 offset=0 count=2 data="1\n" } +< Rwrite { tag=0 count=2 } diff --git a/lib9p/tests/testclient-sess.c b/lib9p/tests/testclient-sess.c index eef9d2c..276f932 100644 --- a/lib9p/tests/testclient-sess.c +++ b/lib9p/tests/testclient-sess.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -#include <arpa/inet.h> /* for htons(), inet_addr() */ +#include <arpa/inet.h> /* for htons(), inet_addr() */ #include <errno.h> #include <error.h> #include <netinet/in.h> /* for struct sockaddr{,_in} */ diff --git a/libmisc/tests/test_assert.c b/libmisc/tests/test_assert.c index cdbc567..290b073 100644 --- a/libmisc/tests/test_assert.c +++ b/libmisc/tests/test_assert.c @@ -5,7 +5,6 @@ */ #include <setjmp.h> -#include <stdarg.h> /* for va_list, va_start(), va_end() */ #include <stdlib.h> #include <string.h> diff --git a/libmisc/tests/test_log.c b/libmisc/tests/test_log.c index ee762e2..6e7cdfd 100644 --- a/libmisc/tests/test_log.c +++ b/libmisc/tests/test_log.c @@ -4,7 +4,6 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -#include <stdarg.h> /* for va_list */ #include <stdio.h> /* for vsnprintf() */ #include <stdlib.h> /* for realloc(), free() */ #include <string.h> /* for strlen(), strcmp() */ |