qemu-sockets: add abstract UNIX domain socket support
unix_listen/connect_saddr now support abstract address types two aditional BOOL switches are introduced: tight: whether to set @addrlen to the minimal string length, or the maximum sun_path length. default is TRUE abstract: whether we use abstract address. default is FALSE cli example: -monitor unix:/tmp/unix.socket,abstract,tight=off OR -chardev socket,path=/tmp/unix.socket,id=unix1,abstract,tight=on Signed-off-by: xiaoqiang zhao <zxq_yx_007@163.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
parent
f2465433b4
commit
776b97d360
@ -1380,6 +1380,8 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
|
|||||||
const char *host = qemu_opt_get(opts, "host");
|
const char *host = qemu_opt_get(opts, "host");
|
||||||
const char *port = qemu_opt_get(opts, "port");
|
const char *port = qemu_opt_get(opts, "port");
|
||||||
const char *fd = qemu_opt_get(opts, "fd");
|
const char *fd = qemu_opt_get(opts, "fd");
|
||||||
|
bool tight = qemu_opt_get_bool(opts, "tight", true);
|
||||||
|
bool abstract = qemu_opt_get_bool(opts, "abstract", false);
|
||||||
SocketAddressLegacy *addr;
|
SocketAddressLegacy *addr;
|
||||||
ChardevSocket *sock;
|
ChardevSocket *sock;
|
||||||
|
|
||||||
@ -1431,6 +1433,8 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
|
|||||||
addr->type = SOCKET_ADDRESS_LEGACY_KIND_UNIX;
|
addr->type = SOCKET_ADDRESS_LEGACY_KIND_UNIX;
|
||||||
q_unix = addr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
|
q_unix = addr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
|
||||||
q_unix->path = g_strdup(path);
|
q_unix->path = g_strdup(path);
|
||||||
|
q_unix->tight = tight;
|
||||||
|
q_unix->abstract = abstract;
|
||||||
} else if (host) {
|
} else if (host) {
|
||||||
addr->type = SOCKET_ADDRESS_LEGACY_KIND_INET;
|
addr->type = SOCKET_ADDRESS_LEGACY_KIND_INET;
|
||||||
addr->u.inet.data = g_new(InetSocketAddress, 1);
|
addr->u.inet.data = g_new(InetSocketAddress, 1);
|
||||||
|
@ -939,6 +939,13 @@ QemuOptsList qemu_chardev_opts = {
|
|||||||
},{
|
},{
|
||||||
.name = "logappend",
|
.name = "logappend",
|
||||||
.type = QEMU_OPT_BOOL,
|
.type = QEMU_OPT_BOOL,
|
||||||
|
},{
|
||||||
|
.name = "tight",
|
||||||
|
.type = QEMU_OPT_BOOL,
|
||||||
|
.def_value_str = "on",
|
||||||
|
},{
|
||||||
|
.name = "abstract",
|
||||||
|
.type = QEMU_OPT_BOOL,
|
||||||
},
|
},
|
||||||
{ /* end of list */ }
|
{ /* end of list */ }
|
||||||
},
|
},
|
||||||
|
@ -73,12 +73,18 @@
|
|||||||
# Captures a socket address in the local ("Unix socket") namespace.
|
# Captures a socket address in the local ("Unix socket") namespace.
|
||||||
#
|
#
|
||||||
# @path: filesystem path to use
|
# @path: filesystem path to use
|
||||||
|
# @tight: pass a socket address length confined to the minimum length of the
|
||||||
|
# abstract string, rather than the full sockaddr_un record length
|
||||||
|
# (only matters for abstract sockets, default true). (Since 5.1)
|
||||||
|
# @abstract: whether this is an abstract address, default false. (Since 5.1)
|
||||||
#
|
#
|
||||||
# Since: 1.3
|
# Since: 1.3
|
||||||
##
|
##
|
||||||
{ 'struct': 'UnixSocketAddress',
|
{ 'struct': 'UnixSocketAddress',
|
||||||
'data': {
|
'data': {
|
||||||
'path': 'str' } }
|
'path': 'str',
|
||||||
|
'*tight': 'bool',
|
||||||
|
'*abstract': 'bool' } }
|
||||||
|
|
||||||
##
|
##
|
||||||
# @VsockSocketAddress:
|
# @VsockSocketAddress:
|
||||||
|
@ -863,6 +863,7 @@ static int unix_listen_saddr(UnixSocketAddress *saddr,
|
|||||||
char *pathbuf = NULL;
|
char *pathbuf = NULL;
|
||||||
const char *path;
|
const char *path;
|
||||||
size_t pathlen;
|
size_t pathlen;
|
||||||
|
size_t addrlen;
|
||||||
|
|
||||||
sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
|
sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
|
||||||
if (sock < 0) {
|
if (sock < 0) {
|
||||||
@ -879,9 +880,11 @@ static int unix_listen_saddr(UnixSocketAddress *saddr,
|
|||||||
}
|
}
|
||||||
|
|
||||||
pathlen = strlen(path);
|
pathlen = strlen(path);
|
||||||
if (pathlen > sizeof(un.sun_path)) {
|
if (pathlen > sizeof(un.sun_path) ||
|
||||||
|
(saddr->abstract && pathlen > (sizeof(un.sun_path) - 1))) {
|
||||||
error_setg(errp, "UNIX socket path '%s' is too long", path);
|
error_setg(errp, "UNIX socket path '%s' is too long", path);
|
||||||
error_append_hint(errp, "Path must be less than %zu bytes\n",
|
error_append_hint(errp, "Path must be less than %zu bytes\n",
|
||||||
|
saddr->abstract ? sizeof(un.sun_path) - 1 :
|
||||||
sizeof(un.sun_path));
|
sizeof(un.sun_path));
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
@ -903,7 +906,7 @@ static int unix_listen_saddr(UnixSocketAddress *saddr,
|
|||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unlink(path) < 0 && errno != ENOENT) {
|
if (!saddr->abstract && unlink(path) < 0 && errno != ENOENT) {
|
||||||
error_setg_errno(errp, errno,
|
error_setg_errno(errp, errno,
|
||||||
"Failed to unlink socket %s", path);
|
"Failed to unlink socket %s", path);
|
||||||
goto err;
|
goto err;
|
||||||
@ -911,9 +914,19 @@ static int unix_listen_saddr(UnixSocketAddress *saddr,
|
|||||||
|
|
||||||
memset(&un, 0, sizeof(un));
|
memset(&un, 0, sizeof(un));
|
||||||
un.sun_family = AF_UNIX;
|
un.sun_family = AF_UNIX;
|
||||||
memcpy(un.sun_path, path, pathlen);
|
addrlen = sizeof(un);
|
||||||
|
|
||||||
if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
|
if (saddr->abstract) {
|
||||||
|
un.sun_path[0] = '\0';
|
||||||
|
memcpy(&un.sun_path[1], path, pathlen);
|
||||||
|
if (saddr->tight) {
|
||||||
|
addrlen = offsetof(struct sockaddr_un, sun_path) + 1 + pathlen;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
memcpy(un.sun_path, path, pathlen);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bind(sock, (struct sockaddr *) &un, addrlen) < 0) {
|
||||||
error_setg_errno(errp, errno, "Failed to bind socket to %s", path);
|
error_setg_errno(errp, errno, "Failed to bind socket to %s", path);
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
@ -936,6 +949,7 @@ static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp)
|
|||||||
struct sockaddr_un un;
|
struct sockaddr_un un;
|
||||||
int sock, rc;
|
int sock, rc;
|
||||||
size_t pathlen;
|
size_t pathlen;
|
||||||
|
size_t addrlen;
|
||||||
|
|
||||||
if (saddr->path == NULL) {
|
if (saddr->path == NULL) {
|
||||||
error_setg(errp, "unix connect: no path specified");
|
error_setg(errp, "unix connect: no path specified");
|
||||||
@ -949,21 +963,32 @@ static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
pathlen = strlen(saddr->path);
|
pathlen = strlen(saddr->path);
|
||||||
if (pathlen > sizeof(un.sun_path)) {
|
if (pathlen > sizeof(un.sun_path) ||
|
||||||
|
(saddr->abstract && pathlen > (sizeof(un.sun_path) - 1))) {
|
||||||
error_setg(errp, "UNIX socket path '%s' is too long", saddr->path);
|
error_setg(errp, "UNIX socket path '%s' is too long", saddr->path);
|
||||||
error_append_hint(errp, "Path must be less than %zu bytes\n",
|
error_append_hint(errp, "Path must be less than %zu bytes\n",
|
||||||
|
saddr->abstract ? sizeof(un.sun_path) - 1 :
|
||||||
sizeof(un.sun_path));
|
sizeof(un.sun_path));
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(&un, 0, sizeof(un));
|
memset(&un, 0, sizeof(un));
|
||||||
un.sun_family = AF_UNIX;
|
un.sun_family = AF_UNIX;
|
||||||
memcpy(un.sun_path, saddr->path, pathlen);
|
addrlen = sizeof(un);
|
||||||
|
|
||||||
|
if (saddr->abstract) {
|
||||||
|
un.sun_path[0] = '\0';
|
||||||
|
memcpy(&un.sun_path[1], saddr->path, pathlen);
|
||||||
|
if (saddr->tight) {
|
||||||
|
addrlen = offsetof(struct sockaddr_un, sun_path) + 1 + pathlen;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
memcpy(un.sun_path, saddr->path, pathlen);
|
||||||
|
}
|
||||||
/* connect to peer */
|
/* connect to peer */
|
||||||
do {
|
do {
|
||||||
rc = 0;
|
rc = 0;
|
||||||
if (connect(sock, (struct sockaddr *) &un, sizeof(un)) < 0) {
|
if (connect(sock, (struct sockaddr *) &un, addrlen) < 0) {
|
||||||
rc = -errno;
|
rc = -errno;
|
||||||
}
|
}
|
||||||
} while (rc == -EINTR);
|
} while (rc == -EINTR);
|
||||||
|
Loading…
Reference in New Issue
Block a user