diff options
| author | Michal Luczaj <mhal@rbox.co> | 2025-06-11 21:56:50 +0200 |
|---|---|---|
| committer | Jakub Kicinski <kuba@kernel.org> | 2025-06-17 14:50:27 -0700 |
| commit | d56a8dbff8fea1c644183e52a39b165757f7b151 (patch) | |
| tree | 05494e557ef8e5c4754bd4093c74ae9c736acd9b /tools/testing/vsock/util.c | |
| parent | Merge branch 'shradha_v6.16-rc1' of https://github.com/shradhagupta6/linux (diff) | |
| download | linux-d56a8dbff8fea1c644183e52a39b165757f7b151.tar.gz linux-d56a8dbff8fea1c644183e52a39b165757f7b151.zip | |
vsock/test: Introduce vsock_bind_try() helper
Create a socket and bind() it. If binding failed, gracefully return an
error code while preserving `errno`.
Base vsock_bind() on top of it.
Suggested-by: Stefano Garzarella <sgarzare@redhat.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Signed-off-by: Michal Luczaj <mhal@rbox.co>
Reviewed-by: Luigi Leonardi <leonardi@redhat.com>
Link: https://patch.msgid.link/20250611-vsock-test-inc-cov-v3-1-5834060d9c20@rbox.co
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Diffstat (limited to 'tools/testing/vsock/util.c')
| -rw-r--r-- | tools/testing/vsock/util.c | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c index 0c7e9cbcbc85..b7b3fb2221c1 100644 --- a/tools/testing/vsock/util.c +++ b/tools/testing/vsock/util.c @@ -121,15 +121,17 @@ bool vsock_wait_sent(int fd) return !ret; } -/* Create socket <type>, bind to <cid, port> and return the file descriptor. */ -int vsock_bind(unsigned int cid, unsigned int port, int type) +/* Create socket <type>, bind to <cid, port>. + * Return the file descriptor, or -1 on error. + */ +int vsock_bind_try(unsigned int cid, unsigned int port, int type) { struct sockaddr_vm sa = { .svm_family = AF_VSOCK, .svm_cid = cid, .svm_port = port, }; - int fd; + int fd, saved_errno; fd = socket(AF_VSOCK, type, 0); if (fd < 0) { @@ -138,6 +140,22 @@ int vsock_bind(unsigned int cid, unsigned int port, int type) } if (bind(fd, (struct sockaddr *)&sa, sizeof(sa))) { + saved_errno = errno; + close(fd); + errno = saved_errno; + fd = -1; + } + + return fd; +} + +/* Create socket <type>, bind to <cid, port> and return the file descriptor. */ +int vsock_bind(unsigned int cid, unsigned int port, int type) +{ + int fd; + + fd = vsock_bind_try(cid, port, type); + if (fd < 0) { perror("bind"); exit(EXIT_FAILURE); } |
