From 59c8110ac6dc966583736fb4327fecc60c255906 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 23 May 2024 23:33:38 +0200 Subject: [PATCH 1/2] socket-util: make return parameter for socket_address_parse_vsock() optional --- src/basic/socket-util.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index 6e304e840d2..5849799f05d 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -1757,13 +1757,12 @@ int socket_address_parse_vsock(SocketAddress *ret_address, const char *s) { int vsock_get_local_cid(unsigned *ret) { _cleanup_close_ int vsock_fd = -EBADF; - assert(ret); - vsock_fd = open("/dev/vsock", O_RDONLY|O_CLOEXEC); if (vsock_fd < 0) return log_debug_errno(errno, "Failed to open /dev/vsock: %m"); - if (ioctl(vsock_fd, IOCTL_VM_SOCKETS_GET_LOCAL_CID, ret) < 0) + unsigned tmp; + if (ioctl(vsock_fd, IOCTL_VM_SOCKETS_GET_LOCAL_CID, ret ?: &tmp) < 0) return log_debug_errno(errno, "Failed to query local AF_VSOCK CID: %m"); return 0; From cb869969d1169effdef5ca2438fb3ea0fc5e0666 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 23 May 2024 23:34:39 +0200 Subject: [PATCH 2/2] machined: initialize CID of '.host' pseudo-machine to 1 (i.e. the loopback vsock address) --- src/machine/machined.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/machine/machined.c b/src/machine/machined.c index 398375bc5e2..60badbf5d07 100644 --- a/src/machine/machined.c +++ b/src/machine/machined.c @@ -26,6 +26,7 @@ #include "process-util.h" #include "service-util.h" #include "signal-util.h" +#include "socket-util.h" #include "special.h" static Manager* manager_unref(Manager *m); @@ -143,6 +144,11 @@ static int manager_add_host_machine(Manager *m) { t->leader = TAKE_PIDREF(pidref); t->id = mid; + /* If vsock is available, let's expose the loopback CID for the local host (and not the actual local + * CID, in order to return a ideally constant record for the host) */ + if (vsock_get_local_cid(/* ret= */ NULL) >= 0) + t->vsock_cid = VMADDR_CID_LOCAL; + t->root_directory = TAKE_PTR(rd); t->unit = TAKE_PTR(unit);