2019-02-28 08:31:04 +00:00
|
|
|
/*
|
|
|
|
Looking Glass - KVM FrameRelay (KVMFR) Client
|
|
|
|
Copyright (C) 2017-2019 Geoffrey McRae <geoff@hostfission.com>
|
|
|
|
https://looking-glass.hostfission.com
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 2 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
|
|
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
|
|
|
Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2019-05-09 09:07:23 +00:00
|
|
|
#include "interface/platform.h"
|
|
|
|
#include "common/debug.h"
|
2019-05-09 12:06:58 +00:00
|
|
|
#include "common/option.h"
|
2020-01-02 12:34:35 +00:00
|
|
|
#include "common/thread.h"
|
2019-05-09 09:07:23 +00:00
|
|
|
|
2019-03-02 09:22:35 +00:00
|
|
|
#include <assert.h>
|
2019-03-01 10:01:25 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mman.h>
|
2019-05-11 01:21:18 +00:00
|
|
|
#include <dirent.h>
|
2019-03-01 10:01:25 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2019-03-01 01:54:15 +00:00
|
|
|
#include <stdlib.h>
|
2019-03-01 10:01:25 +00:00
|
|
|
#include <string.h>
|
2019-03-01 01:54:15 +00:00
|
|
|
#include <pthread.h>
|
2019-03-02 09:31:33 +00:00
|
|
|
#include <signal.h>
|
2019-03-04 06:08:49 +00:00
|
|
|
#include <errno.h>
|
2019-03-01 01:54:15 +00:00
|
|
|
|
2019-03-01 10:01:25 +00:00
|
|
|
struct app
|
|
|
|
{
|
2019-04-11 07:15:17 +00:00
|
|
|
const char * executable;
|
2019-03-01 10:01:25 +00:00
|
|
|
unsigned int shmSize;
|
|
|
|
int shmFD;
|
|
|
|
void * shmMap;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct app app;
|
|
|
|
|
2019-03-02 09:31:33 +00:00
|
|
|
void sigHandler(int signo)
|
|
|
|
{
|
|
|
|
DEBUG_INFO("SIGINT");
|
|
|
|
app_quit();
|
|
|
|
}
|
|
|
|
|
2019-05-11 01:21:18 +00:00
|
|
|
static int uioOpenFile(const char * shmDevice, const char * file)
|
|
|
|
{
|
|
|
|
int len = snprintf(NULL, 0, "/sys/class/uio/%s/%s", shmDevice, file);
|
|
|
|
char * path = malloc(len + 1);
|
|
|
|
sprintf(path, "/sys/class/uio/%s/%s", shmDevice, file);
|
|
|
|
|
|
|
|
int fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
free(path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(path);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char * uioGetName(const char * shmDevice)
|
|
|
|
{
|
|
|
|
int fd = uioOpenFile(shmDevice, "name");
|
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
char * name = malloc(32);
|
|
|
|
int len = read(fd, name, 31);
|
|
|
|
if (len <= 0)
|
|
|
|
{
|
|
|
|
free(name);
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
name[len] = '\0';
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
while(len > 0 && name[len-1] == '\n')
|
|
|
|
{
|
|
|
|
--len;
|
|
|
|
name[len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int shmOpenDev(const char * shmDevice)
|
|
|
|
{
|
|
|
|
int len = snprintf(NULL, 0, "/dev/%s", shmDevice);
|
|
|
|
char * path = malloc(len + 1);
|
|
|
|
sprintf(path, "/dev/%s", shmDevice);
|
|
|
|
|
|
|
|
int fd = open(path, O_RDWR, (mode_t)0600);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to open: %s", path);
|
|
|
|
DEBUG_ERROR("Did you remmeber to modprobe the kvmfr module?");
|
|
|
|
free(path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(path);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2019-05-11 10:59:31 +00:00
|
|
|
static bool shmDeviceValidator(struct Option * opt, const char ** error)
|
2019-05-11 01:21:18 +00:00
|
|
|
{
|
2019-05-11 10:59:31 +00:00
|
|
|
char * name = uioGetName(opt->value.x_string);
|
2019-05-11 01:21:18 +00:00
|
|
|
if (!name)
|
|
|
|
{
|
2019-05-11 01:35:42 +00:00
|
|
|
*error = "Failed to get the uio device name";
|
2019-05-11 01:21:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(name, "KVMFR") != 0)
|
|
|
|
{
|
|
|
|
free(name);
|
2019-05-11 01:35:42 +00:00
|
|
|
*error = "Device is not a KVMFR device";
|
2019-05-11 01:21:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-12 06:14:25 +00:00
|
|
|
static StringList shmDeviceGetValues(struct Option * option)
|
2019-05-11 01:21:18 +00:00
|
|
|
{
|
2019-05-12 06:14:25 +00:00
|
|
|
StringList sl = stringlist_new(true);
|
|
|
|
|
2019-05-11 01:21:18 +00:00
|
|
|
DIR * d = opendir("/sys/class/uio");
|
|
|
|
if (!d)
|
2019-05-12 06:14:25 +00:00
|
|
|
return sl;
|
2019-05-11 01:21:18 +00:00
|
|
|
|
|
|
|
struct dirent * dir;
|
|
|
|
while((dir = readdir(d)) != NULL)
|
|
|
|
{
|
|
|
|
if (dir->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char * name = uioGetName(dir->d_name);
|
|
|
|
if (!name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcmp(name, "KVMFR") == 0)
|
2019-05-12 06:14:25 +00:00
|
|
|
stringlist_push(sl, strdup(dir->d_name));
|
2019-05-11 01:21:18 +00:00
|
|
|
|
|
|
|
free(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(d);
|
2019-05-12 06:14:25 +00:00
|
|
|
return sl;
|
2019-05-11 01:21:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 08:31:04 +00:00
|
|
|
int main(int argc, char * argv[])
|
|
|
|
{
|
2019-04-11 07:15:17 +00:00
|
|
|
app.executable = argv[0];
|
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
struct Option options[] =
|
2019-03-01 10:01:25 +00:00
|
|
|
{
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
2019-05-11 10:59:31 +00:00
|
|
|
.module = "os",
|
|
|
|
.name = "shmDevice",
|
|
|
|
.description = "The IVSHMEM device to use",
|
|
|
|
.type = OPTION_TYPE_STRING,
|
|
|
|
.value.x_string = "uio0",
|
|
|
|
.validator = shmDeviceValidator,
|
2019-05-12 06:14:25 +00:00
|
|
|
.getValues = shmDeviceGetValues
|
2019-05-09 12:06:58 +00:00
|
|
|
},
|
|
|
|
{0}
|
2019-03-01 10:01:25 +00:00
|
|
|
};
|
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
option_register(options);
|
2019-03-01 10:01:25 +00:00
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
int result = app_main(argc, argv);
|
|
|
|
os_shmemUnmap();
|
|
|
|
close(app.shmFD);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool app_init()
|
|
|
|
{
|
|
|
|
const char * shmDevice = option_get_string("os", "shmDevice");
|
2019-03-01 10:01:25 +00:00
|
|
|
|
2019-05-11 01:59:26 +00:00
|
|
|
// get the device size
|
|
|
|
int fd = uioOpenFile(shmDevice, "maps/map0/size");
|
|
|
|
if (fd < 0)
|
2019-03-01 10:11:44 +00:00
|
|
|
{
|
2019-05-11 01:59:26 +00:00
|
|
|
DEBUG_ERROR("Failed to open %s/size", shmDevice);
|
|
|
|
DEBUG_ERROR("Did you remmeber to modprobe the kvmfr module?");
|
|
|
|
return false;
|
2019-03-01 10:11:44 +00:00
|
|
|
}
|
|
|
|
|
2019-05-11 01:59:26 +00:00
|
|
|
char size[32];
|
|
|
|
int len = read(fd, size, sizeof(size) - 1);
|
|
|
|
if (len <= 0)
|
2019-03-01 10:01:25 +00:00
|
|
|
{
|
2019-05-11 01:59:26 +00:00
|
|
|
DEBUG_ERROR("Failed to read the device size");
|
2019-03-01 10:11:44 +00:00
|
|
|
close(fd);
|
2019-05-11 01:59:26 +00:00
|
|
|
return false;
|
2019-03-01 10:01:25 +00:00
|
|
|
}
|
2019-05-11 01:59:26 +00:00
|
|
|
size[len] = '\0';
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
app.shmSize = strtoul(size, NULL, 16);
|
2019-03-01 10:01:25 +00:00
|
|
|
|
|
|
|
// open the device
|
2019-05-11 01:59:26 +00:00
|
|
|
app.shmFD = shmOpenDev(shmDevice);
|
|
|
|
app.shmMap = MAP_FAILED;
|
|
|
|
if (app.shmFD < 0)
|
|
|
|
return false;
|
2019-03-01 10:17:16 +00:00
|
|
|
|
2019-05-11 01:59:26 +00:00
|
|
|
DEBUG_INFO("KVMFR Device : %s", shmDevice);
|
2019-03-01 10:01:25 +00:00
|
|
|
|
2019-03-02 09:31:33 +00:00
|
|
|
signal(SIGINT, sigHandler);
|
2019-05-09 12:06:58 +00:00
|
|
|
return true;
|
2019-02-28 08:31:04 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 07:15:17 +00:00
|
|
|
const char * os_getExecutable()
|
|
|
|
{
|
|
|
|
return app.executable;
|
|
|
|
}
|
|
|
|
|
2019-02-28 08:31:04 +00:00
|
|
|
unsigned int os_shmemSize()
|
|
|
|
{
|
2019-03-01 10:01:25 +00:00
|
|
|
return app.shmSize;
|
2019-02-28 08:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool os_shmemMmap(void **ptr)
|
|
|
|
{
|
2019-03-01 10:01:25 +00:00
|
|
|
if (app.shmMap == MAP_FAILED)
|
|
|
|
{
|
|
|
|
app.shmMap = mmap(0, app.shmSize, PROT_READ | PROT_WRITE, MAP_SHARED, app.shmFD, 0);
|
|
|
|
if (app.shmMap == MAP_FAILED)
|
|
|
|
{
|
2019-05-09 12:06:58 +00:00
|
|
|
const char * shmDevice = option_get_string("os", "shmDevice");
|
|
|
|
DEBUG_ERROR("Failed to map the shared memory device: %s", shmDevice);
|
2019-03-01 10:01:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*ptr = app.shmMap;
|
|
|
|
return true;
|
2019-02-28 08:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void os_shmemUnmap()
|
|
|
|
{
|
2019-03-01 10:01:25 +00:00
|
|
|
if (app.shmMap == MAP_FAILED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
munmap(app.shmMap, app.shmSize);
|
|
|
|
app.shmMap = MAP_FAILED;
|
2019-02-28 08:31:04 +00:00
|
|
|
}
|