From 3c9564cb3ed9fd4f063a76322f7f487e0afd3f27 Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Wed, 6 Mar 2024 08:43:04 -0500 Subject: [PATCH] Use environment variable for the socket. The default behavior is to create a socket file at /tmp/guestfs-inspect.sock. If XDG_RUNTIME_DIR is set, that will be used instead of /tmp. One could instead specify GUESTFS_INSPECT_ENDPOINT, which requires the full endpoint, as specified by: https://libzmq.readthedocs.io/en/zeromq3-x/zmq_connect.html You will likely want to use ipc://, but you can probably use tcp:// or (e)pgm:// if that is appropriate. --- libguestfs-inspect.h | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/libguestfs-inspect.h b/libguestfs-inspect.h index 38d97db..3eebd2e 100644 --- a/libguestfs-inspect.h +++ b/libguestfs-inspect.h @@ -26,10 +26,27 @@ struct guestfs_inpsect_command { }; static char *endpoint() { - // TODO: This should be based on GUESTFS_INSPECT_ENDPOINT, or XDG_RUNTIME_DIR if the former is not set, and default to a sensible path if neither are set. - const char* ep = "ipc:///tmp/guestfs-inspect.sock"; - char *res = malloc(strlen(ep) + 1); - strcpy(res, ep); + const char *guestfs_inspect_endpoint = getenv("GUESTFS_INSPECT_ENDPOINT"); + const char *socket_file_name = "/guestfs-inspect.sock"; + char *res = NULL; + if (guestfs_inspect_endpoint) { + res = malloc(strlen(guestfs_inspect_endpoint) + 1); + strcpy(res, guestfs_inspect_endpoint); + } else { + const char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR"); + if (xdg_runtime_dir) { + res = malloc(strlen(xdg_runtime_dir) + strlen(socket_file_name) + strlen("ipc://") + 1); + strcpy(res, "ipc://"); + strcat(res, xdg_runtime_dir); + strcat(res, socket_file_name); + } else { + const char *tmp_dir = "/tmp"; + res = malloc(strlen(tmp_dir) + strlen(socket_file_name) + strlen("ipc://") + 1); + strcpy(res, "ipc://"); + strcat(res, tmp_dir); + strcat(res, socket_file_name); + } + } return res; }