From 45ee79014dc8e12ba71c660caf26e033b1113ee6 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Mon, 6 Jan 2020 00:20:30 +1100 Subject: [PATCH] [common] added back support for shared memory files --- VERSION | 2 +- client/src/config.c | 18 ----------- client/src/main.h | 2 -- common/src/platform/linux/ivshmem.c | 50 ++++++++++++++++++++++++----- 4 files changed, 43 insertions(+), 29 deletions(-) diff --git a/VERSION b/VERSION index bbd1b1ec..d3612f22 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -B1-51-g127113a59b+1 \ No newline at end of file +B1-52-g0dc0e6490c+1 \ No newline at end of file diff --git a/client/src/config.c b/client/src/config.c index 2d899ba0..ec07f844 100644 --- a/client/src/config.c +++ b/client/src/config.c @@ -52,22 +52,6 @@ static struct Option options[] = .type = OPTION_TYPE_STRING, .value.x_string = NULL, }, - { - .module = "app", - .name = "shmFile", - .description = "The path to the shared memory file", - .shortopt = 'f', - .type = OPTION_TYPE_STRING, - .value.x_string = "/dev/shm/looking-glass", - }, - { - .module = "app", - .name = "shmSize", - .description = "Specify the size in MB of the shared memory file (0 = detect)", - .shortopt = 'L', - .type = OPTION_TYPE_INT, - .value.x_int = 0, - }, { .module = "app", .name = "renderer", @@ -387,8 +371,6 @@ bool config_load(int argc, char * argv[]) } // setup the application params for the basic types - params.shmFile = option_get_string("app", "shmFile" ); - params.shmSize = option_get_int ("app", "shmSize" ) * 1048576; params.cursorPollInterval = option_get_int ("app", "cursorPollInterval"); params.framePollInterval = option_get_int ("app", "framePollInterval" ); diff --git a/client/src/main.h b/client/src/main.h index 73350132..9fa9e9ff 100644 --- a/client/src/main.h +++ b/client/src/main.h @@ -93,8 +93,6 @@ struct AppParams bool center; int x, y; unsigned int w, h; - const char * shmFile; - unsigned int shmSize; unsigned int fpsLimit; bool showFPS; bool useSpiceInput; diff --git a/common/src/platform/linux/ivshmem.c b/common/src/platform/linux/ivshmem.c index 6d0928d4..d37fc37e 100644 --- a/common/src/platform/linux/ivshmem.c +++ b/common/src/platform/linux/ivshmem.c @@ -82,6 +82,18 @@ static char * uioGetName(const char * shmDevice) static bool ivshmemDeviceValidator(struct Option * opt, const char ** error) { + // if it's not a uio device, it must be a file on disk + if (strlen(opt->value.x_string) > 3 && memcmp(opt->value.x_string, "uio", 3) != 0) + { + struct stat st; + if (stat(opt->value.x_string, &st) != 0) + { + *error = "Invalid path to the ivshmem file specified"; + return false; + } + return true; + } + char * name = uioGetName(opt->value.x_string); if (!name) { @@ -133,11 +145,12 @@ void ivshmemOptionsInit() struct Option options[] = { { - .module = "os", - .name = "shmDevice", - .description = "The IVSHMEM device to use", + .module = "app", + .name = "shmFile", + .shortopt = 'f', + .description = "The path to the shared memory file, or the name of the uio device to use, ie: uio0", .type = OPTION_TYPE_STRING, - .value.x_string = "uio0", + .value.x_string = "/dev/shm/looking-glass", .validator = ivshmemDeviceValidator, .getValues = ivshmemDeviceGetValues }, @@ -151,12 +164,15 @@ bool ivshmemOpen(struct IVSHMEM * dev) { assert(dev); - const char * shmDevice = option_get_string("os", "shmDevice"); + const char * shmDevice = option_get_string("app", "shmFile"); unsigned int devSize; int devFD; + dev->opaque = NULL; + DEBUG_INFO("KVMFR Device : %s", shmDevice); + if (strlen(shmDevice) > 3 && memcmp(shmDevice, "uio", 3) == 0) { // get the device size int fd = uioOpenFile(shmDevice, "maps/map0/size"); @@ -178,9 +194,7 @@ bool ivshmemOpen(struct IVSHMEM * dev) size[len] = '\0'; close(fd); devSize = strtoul(size, NULL, 16); - } - { char * path; alloc_sprintf(&path, "/dev/%s", shmDevice); devFD = open(path, O_RDWR, (mode_t)0600); @@ -193,11 +207,27 @@ bool ivshmemOpen(struct IVSHMEM * dev) } free(path); } + else + { + struct stat st; + if (stat(shmDevice, &st) != 0) + { + DEBUG_ERROR("Failed to stat: %s", shmDevice); + return false; + } + + devSize = st.st_size; + devFD = open(shmDevice, O_RDWR, (mode_t)0600); + if (devFD < 0) + { + DEBUG_ERROR("Failed to open: %s", shmDevice); + return false; + } + } void * map = mmap(0, devSize, PROT_READ | PROT_WRITE, MAP_SHARED, devFD, 0); if (map == MAP_FAILED) { - const char * shmDevice = option_get_string("os", "shmDevice"); DEBUG_ERROR("Failed to map the shared memory device: %s", shmDevice); return false; } @@ -217,6 +247,9 @@ void ivshmemClose(struct IVSHMEM * dev) { assert(dev); + if (!dev->opaque) + return; + struct IVSHMEMInfo * info = (struct IVSHMEMInfo *)dev->opaque; @@ -224,4 +257,5 @@ void ivshmemClose(struct IVSHMEM * dev) close(info->fd); free(info); + dev->opaque = NULL; } \ No newline at end of file