mirror of
https://github.com/qemu/qemu.git
synced 2025-08-15 22:31:15 +00:00
Add support for UNIX sockets in the abstract namespace
-----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEE2vOm/bJrYpEtDo4/vobrtBUQT98FAl7FKYMACgkQvobrtBUQ T98wJQ//VDovrRUEJSgm+G5OIXGz+t2ePR6SbZyMbDG8iwXkTqVO7t06LCjVvzof jAQkwUl6wl4Tjkp6JFdopg252gk0O/9H9kuafFQe7Rbq/RAbc5lYRrAjPt4lJ9j/ qncKtKvABKh06dzlH98XzgZPoZbVxua9gcUlQB9fWvsuMPppvepWdaxm+xNPoymW 35u/m1kLega4d5N/n21//Gw0Px0BpYxbrDIQ5UVwYpunqfwtvj9ixtyKVF7kCpbT JqdZnM6q4/86TRhw90DDlK2i7Hq0Hb8XhqgPck3zT6ddE9qUfEC8Ztxn1fIDqAwI bZs3lPfMNBbDu+u3T5pgll1VHr+WZ/5m0OfDEidcgkErfycB+xXmZtk6TxX6SjCK +ugbIjrey2arPIsb8Qpm2dElt5DvE//ylqzbAichXhaENuIpoE3A7NHSb42+icIq Aykr04PTYCWWu0vrfxt550ofXTkjT5SovydsVZ4kS9ttSYvvA7yfz9y/8NQmsJup 5rb7IoFqw+6VlWdzbMUgy+F/r0N7TmiTTQLA+fK6vbX/s6dXLmSy4fowJSb6KuPq CYgftEroce5YLjpbRWlfWFPk/GRPu2xeRsfgjAU6R/joNlzfWRwUm67TRVXFBeXk zf4s3bpSLUj2jtghWlqg6V2ufezI9Q+nZhnOZ/BBL8Jr6gFQu24= =kvhl -----END PGP SIGNATURE----- Merge remote-tracking branch 'remotes/berrange/tags/socket-next-pull-request' into staging Add support for UNIX sockets in the abstract namespace # gpg: Signature made Wed 20 May 2020 13:58:43 BST # gpg: using RSA key DAF3A6FDB26B62912D0E8E3FBE86EBB415104FDF # gpg: Good signature from "Daniel P. Berrange <dan@berrange.com>" [full] # gpg: aka "Daniel P. Berrange <berrange@redhat.com>" [full] # Primary key fingerprint: DAF3 A6FD B26B 6291 2D0E 8E3F BE86 EBB4 1510 4FDF * remotes/berrange/tags/socket-next-pull-request: qemu-options: updates for abstract unix sockets tests/util-sockets: add abstract unix socket cases qemu-sockets: add abstract UNIX domain socket support Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
commit
ae3aa5da96
@ -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:
|
||||||
|
@ -2938,7 +2938,7 @@ DEF("chardev", HAS_ARG, QEMU_OPTION_chardev,
|
|||||||
" [,server][,nowait][,telnet][,websocket][,reconnect=seconds][,mux=on|off]\n"
|
" [,server][,nowait][,telnet][,websocket][,reconnect=seconds][,mux=on|off]\n"
|
||||||
" [,logfile=PATH][,logappend=on|off][,tls-creds=ID][,tls-authz=ID] (tcp)\n"
|
" [,logfile=PATH][,logappend=on|off][,tls-creds=ID][,tls-authz=ID] (tcp)\n"
|
||||||
"-chardev socket,id=id,path=path[,server][,nowait][,telnet][,websocket][,reconnect=seconds]\n"
|
"-chardev socket,id=id,path=path[,server][,nowait][,telnet][,websocket][,reconnect=seconds]\n"
|
||||||
" [,mux=on|off][,logfile=PATH][,logappend=on|off] (unix)\n"
|
" [,mux=on|off][,logfile=PATH][,logappend=on|off][,abstract=on|off][,tight=on|off] (unix)\n"
|
||||||
"-chardev udp,id=id[,host=host],port=port[,localaddr=localaddr]\n"
|
"-chardev udp,id=id[,host=host],port=port[,localaddr=localaddr]\n"
|
||||||
" [,localport=localport][,ipv4][,ipv6][,mux=on|off]\n"
|
" [,localport=localport][,ipv4][,ipv6][,mux=on|off]\n"
|
||||||
" [,logfile=PATH][,logappend=on|off]\n"
|
" [,logfile=PATH][,logappend=on|off]\n"
|
||||||
@ -3105,9 +3105,13 @@ The available backends are:
|
|||||||
|
|
||||||
``nodelay`` disables the Nagle algorithm.
|
``nodelay`` disables the Nagle algorithm.
|
||||||
|
|
||||||
``unix options: path=path``
|
``unix options: path=path[,abstract=on|off][,tight=on|off]``
|
||||||
``path`` specifies the local path of the unix socket. ``path``
|
``path`` specifies the local path of the unix socket. ``path``
|
||||||
is required.
|
is required.
|
||||||
|
``abstract`` specifies the use of the abstract socket namespace,
|
||||||
|
rather than the filesystem. Optional, defaults to false.
|
||||||
|
``tight`` sets the socket length of abstract sockets to their minimum,
|
||||||
|
rather than the full sun_path length. Optional, defaults to true.
|
||||||
|
|
||||||
``-chardev udp,id=id[,host=host],port=port[,localaddr=localaddr][,localport=localport][,ipv4][,ipv6]``
|
``-chardev udp,id=id[,host=host],port=port[,localaddr=localaddr][,localport=localport][,ipv4][,ipv6]``
|
||||||
Sends all traffic from the guest to a remote host over UDP.
|
Sends all traffic from the guest to a remote host over UDP.
|
||||||
|
@ -227,6 +227,93 @@ static void test_socket_fd_pass_num_nocli(void)
|
|||||||
g_free(addr.u.fd.str);
|
g_free(addr.u.fd.str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef __linux__
|
||||||
|
static gchar *abstract_sock_name;
|
||||||
|
|
||||||
|
static gpointer unix_server_thread_func(gpointer user_data)
|
||||||
|
{
|
||||||
|
SocketAddress addr;
|
||||||
|
Error *err = NULL;
|
||||||
|
int fd = -1;
|
||||||
|
int connfd = -1;
|
||||||
|
struct sockaddr_un un;
|
||||||
|
socklen_t len = sizeof(un);
|
||||||
|
|
||||||
|
addr.type = SOCKET_ADDRESS_TYPE_UNIX;
|
||||||
|
addr.u.q_unix.path = abstract_sock_name;
|
||||||
|
addr.u.q_unix.tight = user_data != NULL;
|
||||||
|
addr.u.q_unix.abstract = true;
|
||||||
|
|
||||||
|
fd = socket_listen(&addr, 1, &err);
|
||||||
|
g_assert_cmpint(fd, >=, 0);
|
||||||
|
g_assert(fd_is_socket(fd));
|
||||||
|
|
||||||
|
connfd = accept(fd, (struct sockaddr *)&un, &len);
|
||||||
|
g_assert_cmpint(connfd, !=, -1);
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gpointer unix_client_thread_func(gpointer user_data)
|
||||||
|
{
|
||||||
|
SocketAddress addr;
|
||||||
|
Error *err = NULL;
|
||||||
|
int fd = -1;
|
||||||
|
|
||||||
|
addr.type = SOCKET_ADDRESS_TYPE_UNIX;
|
||||||
|
addr.u.q_unix.path = abstract_sock_name;
|
||||||
|
addr.u.q_unix.tight = user_data != NULL;
|
||||||
|
addr.u.q_unix.abstract = true;
|
||||||
|
|
||||||
|
fd = socket_connect(&addr, &err);
|
||||||
|
|
||||||
|
g_assert_cmpint(fd, >=, 0);
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_socket_unix_abstract_good(void)
|
||||||
|
{
|
||||||
|
GRand *r = g_rand_new();
|
||||||
|
|
||||||
|
abstract_sock_name = g_strdup_printf("unix-%d-%d", getpid(),
|
||||||
|
g_rand_int_range(r, 100, 1000));
|
||||||
|
|
||||||
|
/* non tight socklen serv and cli */
|
||||||
|
GThread *serv = g_thread_new("abstract_unix_server",
|
||||||
|
unix_server_thread_func,
|
||||||
|
NULL);
|
||||||
|
|
||||||
|
sleep(1);
|
||||||
|
|
||||||
|
GThread *cli = g_thread_new("abstract_unix_client",
|
||||||
|
unix_client_thread_func,
|
||||||
|
NULL);
|
||||||
|
|
||||||
|
g_thread_join(cli);
|
||||||
|
g_thread_join(serv);
|
||||||
|
|
||||||
|
/* tight socklen serv and cli */
|
||||||
|
serv = g_thread_new("abstract_unix_server",
|
||||||
|
unix_server_thread_func,
|
||||||
|
(gpointer)1);
|
||||||
|
|
||||||
|
sleep(1);
|
||||||
|
|
||||||
|
cli = g_thread_new("abstract_unix_client",
|
||||||
|
unix_client_thread_func,
|
||||||
|
(gpointer)1);
|
||||||
|
|
||||||
|
g_thread_join(cli);
|
||||||
|
g_thread_join(serv);
|
||||||
|
|
||||||
|
g_free(abstract_sock_name);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
@ -265,6 +352,11 @@ int main(int argc, char **argv)
|
|||||||
test_socket_fd_pass_num_nocli);
|
test_socket_fd_pass_num_nocli);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef __linux__
|
||||||
|
g_test_add_func("/util/socket/unix-abstract/good",
|
||||||
|
test_socket_unix_abstract_good);
|
||||||
|
#endif
|
||||||
|
|
||||||
end:
|
end:
|
||||||
return g_test_run();
|
return g_test_run();
|
||||||
}
|
}
|
||||||
|
@ -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