socket: Add backlog parameter to socket_listen

Current parameter was always one.  We continue with that value for now
in all callers.

Signed-off-by: Juan Quintela <quintela@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
---
Moved trace to socket_listen
This commit is contained in:
Juan Quintela 2019-08-19 14:48:21 +02:00
parent 3483534ec3
commit e5b6353cf2
6 changed files with 34 additions and 20 deletions

View File

@ -41,7 +41,7 @@ int unix_connect(const char *path, Error **errp);
SocketAddress *socket_parse(const char *str, Error **errp); SocketAddress *socket_parse(const char *str, Error **errp);
int socket_connect(SocketAddress *addr, Error **errp); int socket_connect(SocketAddress *addr, Error **errp);
int socket_listen(SocketAddress *addr, Error **errp); int socket_listen(SocketAddress *addr, int num, Error **errp);
void socket_listen_cleanup(int fd, Error **errp); void socket_listen_cleanup(int fd, Error **errp);
int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp); int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp);

View File

@ -202,7 +202,7 @@ int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
int fd; int fd;
trace_qio_channel_socket_listen_sync(ioc, addr); trace_qio_channel_socket_listen_sync(ioc, addr);
fd = socket_listen(addr, errp); fd = socket_listen(addr, 1, errp);
if (fd < 0) { if (fd < 0) {
trace_qio_channel_socket_listen_fail(ioc); trace_qio_channel_socket_listen_fail(ioc);
return -1; return -1;

View File

@ -215,7 +215,7 @@ static gboolean ga_channel_open(GAChannel *c, const gchar *path,
return false; return false;
} }
fd = socket_listen(addr, &local_err); fd = socket_listen(addr, 1, &local_err);
qapi_free_SocketAddress(addr); qapi_free_SocketAddress(addr);
if (local_err != NULL) { if (local_err != NULL) {
g_critical("%s", error_get_pretty(local_err)); g_critical("%s", error_get_pretty(local_err));

View File

@ -93,7 +93,7 @@ static void test_socket_fd_pass_name_good(void)
g_assert_cmpint(fd, !=, mon_fd); g_assert_cmpint(fd, !=, mon_fd);
close(fd); close(fd);
fd = socket_listen(&addr, &error_abort); fd = socket_listen(&addr, 1, &error_abort);
g_assert_cmpint(fd, !=, -1); g_assert_cmpint(fd, !=, -1);
g_assert_cmpint(fd, !=, mon_fd); g_assert_cmpint(fd, !=, mon_fd);
close(fd); close(fd);
@ -124,7 +124,7 @@ static void test_socket_fd_pass_name_bad(void)
g_assert_cmpint(fd, ==, -1); g_assert_cmpint(fd, ==, -1);
error_free_or_abort(&err); error_free_or_abort(&err);
fd = socket_listen(&addr, &err); fd = socket_listen(&addr, 1, &err);
g_assert_cmpint(fd, ==, -1); g_assert_cmpint(fd, ==, -1);
error_free_or_abort(&err); error_free_or_abort(&err);
@ -151,7 +151,7 @@ static void test_socket_fd_pass_name_nomon(void)
g_assert_cmpint(fd, ==, -1); g_assert_cmpint(fd, ==, -1);
error_free_or_abort(&err); error_free_or_abort(&err);
fd = socket_listen(&addr, &err); fd = socket_listen(&addr, 1, &err);
g_assert_cmpint(fd, ==, -1); g_assert_cmpint(fd, ==, -1);
error_free_or_abort(&err); error_free_or_abort(&err);
@ -174,7 +174,7 @@ static void test_socket_fd_pass_num_good(void)
fd = socket_connect(&addr, &error_abort); fd = socket_connect(&addr, &error_abort);
g_assert_cmpint(fd, ==, sfd); g_assert_cmpint(fd, ==, sfd);
fd = socket_listen(&addr, &error_abort); fd = socket_listen(&addr, 1, &error_abort);
g_assert_cmpint(fd, ==, sfd); g_assert_cmpint(fd, ==, sfd);
g_free(addr.u.fd.str); g_free(addr.u.fd.str);
@ -197,7 +197,7 @@ static void test_socket_fd_pass_num_bad(void)
g_assert_cmpint(fd, ==, -1); g_assert_cmpint(fd, ==, -1);
error_free_or_abort(&err); error_free_or_abort(&err);
fd = socket_listen(&addr, &err); fd = socket_listen(&addr, 1, &err);
g_assert_cmpint(fd, ==, -1); g_assert_cmpint(fd, ==, -1);
error_free_or_abort(&err); error_free_or_abort(&err);
@ -220,7 +220,7 @@ static void test_socket_fd_pass_num_nocli(void)
g_assert_cmpint(fd, ==, -1); g_assert_cmpint(fd, ==, -1);
error_free_or_abort(&err); error_free_or_abort(&err);
fd = socket_listen(&addr, &err); fd = socket_listen(&addr, 1, &err);
g_assert_cmpint(fd, ==, -1); g_assert_cmpint(fd, ==, -1);
error_free_or_abort(&err); error_free_or_abort(&err);

View File

@ -31,6 +31,7 @@
#include "qapi/qobject-input-visitor.h" #include "qapi/qobject-input-visitor.h"
#include "qapi/qobject-output-visitor.h" #include "qapi/qobject-output-visitor.h"
#include "qemu/cutils.h" #include "qemu/cutils.h"
#include "trace.h"
#ifndef AI_ADDRCONFIG #ifndef AI_ADDRCONFIG
# define AI_ADDRCONFIG 0 # define AI_ADDRCONFIG 0
@ -207,6 +208,7 @@ static int try_bind(int socket, InetSocketAddress *saddr, struct addrinfo *e)
static int inet_listen_saddr(InetSocketAddress *saddr, static int inet_listen_saddr(InetSocketAddress *saddr,
int port_offset, int port_offset,
int num,
Error **errp) Error **errp)
{ {
struct addrinfo ai,*res,*e; struct addrinfo ai,*res,*e;
@ -309,7 +311,7 @@ static int inet_listen_saddr(InetSocketAddress *saddr,
goto listen_failed; goto listen_failed;
} }
} else { } else {
if (!listen(slisten, 1)) { if (!listen(slisten, num)) {
goto listen_ok; goto listen_ok;
} }
if (errno != EADDRINUSE) { if (errno != EADDRINUSE) {
@ -774,6 +776,7 @@ static int vsock_connect_saddr(VsockSocketAddress *vaddr, Error **errp)
} }
static int vsock_listen_saddr(VsockSocketAddress *vaddr, static int vsock_listen_saddr(VsockSocketAddress *vaddr,
int num,
Error **errp) Error **errp)
{ {
struct sockaddr_vm svm; struct sockaddr_vm svm;
@ -795,7 +798,7 @@ static int vsock_listen_saddr(VsockSocketAddress *vaddr,
return -1; return -1;
} }
if (listen(slisten, 1) != 0) { if (listen(slisten, num) != 0) {
error_setg_errno(errp, errno, "Failed to listen on socket"); error_setg_errno(errp, errno, "Failed to listen on socket");
closesocket(slisten); closesocket(slisten);
return -1; return -1;
@ -836,6 +839,7 @@ static int vsock_connect_saddr(VsockSocketAddress *vaddr, Error **errp)
} }
static int vsock_listen_saddr(VsockSocketAddress *vaddr, static int vsock_listen_saddr(VsockSocketAddress *vaddr,
int num,
Error **errp) Error **errp)
{ {
vsock_unsupported(errp); vsock_unsupported(errp);
@ -853,6 +857,7 @@ static int vsock_parse(VsockSocketAddress *addr, const char *str,
#ifndef _WIN32 #ifndef _WIN32
static int unix_listen_saddr(UnixSocketAddress *saddr, static int unix_listen_saddr(UnixSocketAddress *saddr,
int num,
Error **errp) Error **errp)
{ {
struct sockaddr_un un; struct sockaddr_un un;
@ -914,7 +919,7 @@ static int unix_listen_saddr(UnixSocketAddress *saddr,
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;
} }
if (listen(sock, 1) < 0) { if (listen(sock, num) < 0) {
error_setg_errno(errp, errno, "Failed to listen on socket"); error_setg_errno(errp, errno, "Failed to listen on socket");
goto err; goto err;
} }
@ -981,6 +986,7 @@ static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp)
#else #else
static int unix_listen_saddr(UnixSocketAddress *saddr, static int unix_listen_saddr(UnixSocketAddress *saddr,
int num,
Error **errp) Error **errp)
{ {
error_setg(errp, "unix sockets are not available on windows"); error_setg(errp, "unix sockets are not available on windows");
@ -1004,7 +1010,7 @@ int unix_listen(const char *str, Error **errp)
saddr = g_new0(UnixSocketAddress, 1); saddr = g_new0(UnixSocketAddress, 1);
saddr->path = g_strdup(str); saddr->path = g_strdup(str);
sock = unix_listen_saddr(saddr, errp); sock = unix_listen_saddr(saddr, 1, errp);
qapi_free_UnixSocketAddress(saddr); qapi_free_UnixSocketAddress(saddr);
return sock; return sock;
} }
@ -1061,9 +1067,13 @@ fail:
return NULL; return NULL;
} }
static int socket_get_fd(const char *fdstr, Error **errp) static int socket_get_fd(const char *fdstr, int num, Error **errp)
{ {
int fd; int fd;
if (num != 1) {
error_setg_errno(errp, EINVAL, "socket_get_fd: too many connections");
return -1;
}
if (cur_mon) { if (cur_mon) {
fd = monitor_get_fd(cur_mon, fdstr, errp); fd = monitor_get_fd(cur_mon, fdstr, errp);
if (fd < 0) { if (fd < 0) {
@ -1099,7 +1109,7 @@ int socket_connect(SocketAddress *addr, Error **errp)
break; break;
case SOCKET_ADDRESS_TYPE_FD: case SOCKET_ADDRESS_TYPE_FD:
fd = socket_get_fd(addr->u.fd.str, errp); fd = socket_get_fd(addr->u.fd.str, 1, errp);
break; break;
case SOCKET_ADDRESS_TYPE_VSOCK: case SOCKET_ADDRESS_TYPE_VSOCK:
@ -1112,25 +1122,26 @@ int socket_connect(SocketAddress *addr, Error **errp)
return fd; return fd;
} }
int socket_listen(SocketAddress *addr, Error **errp) int socket_listen(SocketAddress *addr, int num, Error **errp)
{ {
int fd; int fd;
trace_socket_listen(num);
switch (addr->type) { switch (addr->type) {
case SOCKET_ADDRESS_TYPE_INET: case SOCKET_ADDRESS_TYPE_INET:
fd = inet_listen_saddr(&addr->u.inet, 0, errp); fd = inet_listen_saddr(&addr->u.inet, 0, num, errp);
break; break;
case SOCKET_ADDRESS_TYPE_UNIX: case SOCKET_ADDRESS_TYPE_UNIX:
fd = unix_listen_saddr(&addr->u.q_unix, errp); fd = unix_listen_saddr(&addr->u.q_unix, num, errp);
break; break;
case SOCKET_ADDRESS_TYPE_FD: case SOCKET_ADDRESS_TYPE_FD:
fd = socket_get_fd(addr->u.fd.str, errp); fd = socket_get_fd(addr->u.fd.str, num, errp);
break; break;
case SOCKET_ADDRESS_TYPE_VSOCK: case SOCKET_ADDRESS_TYPE_VSOCK:
fd = vsock_listen_saddr(&addr->u.vsock, errp); fd = vsock_listen_saddr(&addr->u.vsock, num, errp);
break; break;
default: default:

View File

@ -64,6 +64,9 @@ lockcnt_futex_wait(const void *lockcnt, int val) "lockcnt %p waiting on %d"
lockcnt_futex_wait_resume(const void *lockcnt, int new) "lockcnt %p after wait: %d" lockcnt_futex_wait_resume(const void *lockcnt, int new) "lockcnt %p after wait: %d"
lockcnt_futex_wake(const void *lockcnt) "lockcnt %p waking up one waiter" lockcnt_futex_wake(const void *lockcnt) "lockcnt %p waking up one waiter"
# qemu-sockets.c
socket_listen(int num) "backlog: %d"
# qemu-thread-common.h # qemu-thread-common.h
qemu_mutex_lock(void *mutex, const char *file, const int line) "waiting on mutex %p (%s:%d)" qemu_mutex_lock(void *mutex, const char *file, const int line) "waiting on mutex %p (%s:%d)"
qemu_mutex_locked(void *mutex, const char *file, const int line) "taken mutex %p (%s:%d)" qemu_mutex_locked(void *mutex, const char *file, const int line) "taken mutex %p (%s:%d)"