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; }