]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
socket-util: add helper for querying the local AF_VSOCK CID
authorLennart Poettering <lennart@poettering.net>
Fri, 5 Jan 2024 15:33:58 +0000 (16:33 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 8 Jan 2024 09:26:34 +0000 (10:26 +0100)
src/basic/socket-util.c
src/basic/socket-util.h

index 9c87fb4ce81f5e47404ee26440b64197bc4b9559..9b991b6f694d43061686ac531ad0ca8659276d1f 100644 (file)
@@ -1753,3 +1753,18 @@ int socket_address_parse_vsock(SocketAddress *ret_address, const char *s) {
 
         return 0;
 }
+
+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)
+                return log_debug_errno(errno, "Failed to query local AF_VSOCK CID: %m");
+
+        return 0;
+}
index 39a6677c12b4de89e477683a1e116c054601cf27..47a9a35c953e13c4247811f86ffbbbbfe5151ff1 100644 (file)
@@ -389,3 +389,5 @@ int socket_address_parse_vsock(SocketAddress *ret_address, const char *s);
  * /proc/sys/net/core/somaxconn anyway, thus by setting this to unbounded we just make that sysctl file
  * authoritative. */
 #define SOMAXCONN_DELUXE INT_MAX
+
+int vsock_get_local_cid(unsigned *ret);