libxl: Add libxl__prepare_sockaddr_un() helper

There is going to be a few more users that want to use UNIX socket, this
helper is to prepare the `struct sockaddr_un` and check that the path
isn't too long.

Also start to use it in libxl_qmp.c.

Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Reviewed-by: Roger Pau Monné <roger.pau@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
This commit is contained in:
Anthony PERARD committed 2018-08-21 11:57:50 +01:00
1 parent e544b5978d
commit 0beaf66e4f
3 files changed
+23 -6

No files matched your search

+4
View File
@@ -47,6 +47,7 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/file.h>
#include <sys/ioctl.h>
@@ -4420,6 +4421,9 @@ static inline bool libxl__string_is_default(char **s)
{
return *s == NULL;
}
_hidden int libxl__prepare_sockaddr_un(libxl__gc *gc, struct sockaddr_un *un,
const char *path, const char *what);
#endif
/*
+4 -6
View File
@@ -428,6 +428,7 @@ static libxl__qmp_handler *qmp_init_handler(libxl__gc *gc, uint32_t domid)
static int qmp_open(libxl__qmp_handler *qmp, const char *qmp_socket_path,
int timeout)
{
GC_INIT(qmp->ctx);
int ret = -1;
int i = 0;
struct sockaddr_un addr;
@@ -447,13 +448,9 @@ static int qmp_open(libxl__qmp_handler *qmp, const char *qmp_socket_path,
goto out;
}
if (sizeof(addr.sun_path) <= strlen(qmp_socket_path)) {
ret = -1;
ret = libxl__prepare_sockaddr_un(gc, &addr, qmp_socket_path, "QMP socket");
if (ret)
goto out;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, qmp_socket_path, sizeof(addr.sun_path));
do {
ret = connect(qmp->qmp_fd, (struct sockaddr *) &addr, sizeof(addr));
@@ -471,6 +468,7 @@ static int qmp_open(libxl__qmp_handler *qmp, const char *qmp_socket_path,
out:
if (ret == -1 && qmp->qmp_fd > -1) close(qmp->qmp_fd);
GC_FREE;
return ret;
}
+15
View File
@@ -1234,6 +1234,21 @@ int libxl__random_bytes(libxl__gc *gc, uint8_t *buf, size_t len)
return ret;
}
int libxl__prepare_sockaddr_un(libxl__gc *gc,
struct sockaddr_un *un, const char *path,
const char *what)
{
if (sizeof(un->sun_path) <= strlen(path)) {
LOG(ERROR, "UNIX socket path '%s' is too long for %s", path, what);
LOG(DEBUG, "Path must be less than %zu bytes", sizeof(un->sun_path));
return ERROR_INVAL;
}
memset(un, 0, sizeof(struct sockaddr_un));
un->sun_family = AF_UNIX;
strncpy(un->sun_path, path, sizeof(un->sun_path));
return 0;
}
/*
* Local variables:
* mode: C
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.