2014-09-08 13:17:48 +04:00
|
|
|
/*
|
|
|
|
* Copyright 6WIND S.A., 2014
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or
|
|
|
|
* (at your option) any later version. See the COPYING file in the
|
|
|
|
* top-level directory.
|
|
|
|
*/
|
|
|
|
|
2016-01-29 20:50:04 +03:00
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2016-03-20 20:16:19 +03:00
|
|
|
#include "qemu/cutils.h"
|
2018-02-01 14:18:46 +03:00
|
|
|
#include "qemu/option.h"
|
2014-09-08 13:17:48 +04:00
|
|
|
#include "ivshmem-server.h"
|
|
|
|
|
|
|
|
#define IVSHMEM_SERVER_DEFAULT_VERBOSE 0
|
|
|
|
#define IVSHMEM_SERVER_DEFAULT_FOREGROUND 0
|
|
|
|
#define IVSHMEM_SERVER_DEFAULT_PID_FILE "/var/run/ivshmem-server.pid"
|
|
|
|
#define IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH "/tmp/ivshmem_socket"
|
|
|
|
#define IVSHMEM_SERVER_DEFAULT_SHM_PATH "ivshmem"
|
2021-02-13 16:03:08 +03:00
|
|
|
#define IVSHMEM_SERVER_DEFAULT_SHM_SIZE (4 * 1024 * 1024)
|
2014-09-08 13:17:48 +04:00
|
|
|
#define IVSHMEM_SERVER_DEFAULT_N_VECTORS 1
|
|
|
|
|
|
|
|
/* used to quit on signal SIGTERM */
|
|
|
|
static int ivshmem_server_quit;
|
|
|
|
|
|
|
|
/* arguments given by the user */
|
|
|
|
typedef struct IvshmemServerArgs {
|
|
|
|
bool verbose;
|
|
|
|
bool foreground;
|
|
|
|
const char *pid_file;
|
|
|
|
const char *unix_socket_path;
|
|
|
|
const char *shm_path;
|
2016-03-15 21:34:18 +03:00
|
|
|
bool use_shm_open;
|
2014-09-08 13:17:48 +04:00
|
|
|
uint64_t shm_size;
|
|
|
|
unsigned n_vectors;
|
|
|
|
} IvshmemServerArgs;
|
|
|
|
|
|
|
|
static void
|
2016-03-15 21:34:17 +03:00
|
|
|
ivshmem_server_usage(const char *progname)
|
2014-09-08 13:17:48 +04:00
|
|
|
{
|
2016-03-15 21:34:17 +03:00
|
|
|
printf("Usage: %s [OPTION]...\n"
|
|
|
|
" -h: show this help\n"
|
|
|
|
" -v: verbose mode\n"
|
|
|
|
" -F: foreground mode (default is to daemonize)\n"
|
|
|
|
" -p <pid-file>: path to the PID file (used in daemon mode only)\n"
|
|
|
|
" default " IVSHMEM_SERVER_DEFAULT_PID_FILE "\n"
|
|
|
|
" -S <unix-socket-path>: path to the unix socket to listen to\n"
|
|
|
|
" default " IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH "\n"
|
2016-03-15 21:34:18 +03:00
|
|
|
" -M <shm-name>: POSIX shared memory object to use\n"
|
2016-03-15 21:34:17 +03:00
|
|
|
" default " IVSHMEM_SERVER_DEFAULT_SHM_PATH "\n"
|
2016-03-15 21:34:18 +03:00
|
|
|
" -m <dir-name>: where to create shared memory\n"
|
2016-03-15 21:34:17 +03:00
|
|
|
" -l <size>: size of shared memory in bytes\n"
|
|
|
|
" suffixes K, M and G can be used, e.g. 1K means 1024\n"
|
|
|
|
" default %u\n"
|
|
|
|
" -n <nvectors>: number of vectors\n"
|
|
|
|
" default %u\n",
|
|
|
|
progname, IVSHMEM_SERVER_DEFAULT_SHM_SIZE,
|
|
|
|
IVSHMEM_SERVER_DEFAULT_N_VECTORS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ivshmem_server_help(const char *progname)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Try '%s -h' for more information.\n", progname);
|
2014-09-08 13:17:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* parse the program arguments, exit on error */
|
|
|
|
static void
|
|
|
|
ivshmem_server_parse_args(IvshmemServerArgs *args, int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int c;
|
2023-05-22 22:04:29 +03:00
|
|
|
uint64_t v;
|
2015-12-18 18:35:27 +03:00
|
|
|
Error *err = NULL;
|
2014-09-08 13:17:48 +04:00
|
|
|
|
2016-03-15 21:34:18 +03:00
|
|
|
while ((c = getopt(argc, argv, "hvFp:S:m:M:l:n:")) != -1) {
|
2014-09-08 13:17:48 +04:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'h': /* help */
|
2016-03-15 21:34:17 +03:00
|
|
|
ivshmem_server_usage(argv[0]);
|
|
|
|
exit(0);
|
2014-09-08 13:17:48 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v': /* verbose */
|
|
|
|
args->verbose = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'F': /* foreground */
|
|
|
|
args->foreground = 1;
|
|
|
|
break;
|
|
|
|
|
2016-03-15 21:34:17 +03:00
|
|
|
case 'p': /* pid file */
|
2015-06-24 14:33:32 +03:00
|
|
|
args->pid_file = optarg;
|
2014-09-08 13:17:48 +04:00
|
|
|
break;
|
|
|
|
|
2016-03-15 21:34:17 +03:00
|
|
|
case 'S': /* unix socket path */
|
2015-06-24 14:33:32 +03:00
|
|
|
args->unix_socket_path = optarg;
|
2014-09-08 13:17:48 +04:00
|
|
|
break;
|
|
|
|
|
2016-03-15 21:34:18 +03:00
|
|
|
case 'M': /* shm name */
|
|
|
|
case 'm': /* dir name */
|
2015-06-24 14:33:32 +03:00
|
|
|
args->shm_path = optarg;
|
2016-03-15 21:34:18 +03:00
|
|
|
args->use_shm_open = c == 'M';
|
2014-09-08 13:17:48 +04:00
|
|
|
break;
|
|
|
|
|
2016-03-15 21:34:17 +03:00
|
|
|
case 'l': /* shm size */
|
qemu-option: Use returned bool to check for failure
The previous commit enables conversion of
foo(..., &err);
if (err) {
...
}
to
if (!foo(..., &err)) {
...
}
for QemuOpts functions that now return true / false on success /
error. Coccinelle script:
@@
identifier fun = {
opts_do_parse, parse_option_bool, parse_option_number,
parse_option_size, qemu_opt_parse, qemu_opt_rename, qemu_opt_set,
qemu_opt_set_bool, qemu_opt_set_number, qemu_opts_absorb_qdict,
qemu_opts_do_parse, qemu_opts_from_qdict_entry, qemu_opts_set,
qemu_opts_validate
};
expression list args, args2;
typedef Error;
Error *err;
@@
- fun(args, &err, args2);
- if (err)
+ if (!fun(args, &err, args2))
{
...
}
A few line breaks tidied up manually.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20200707160613.848843-15-armbru@redhat.com>
[Conflict with commit 0b6786a9c1 "block/amend: refactor qcow2 amend
options" resolved by rerunning Coccinelle on master's version]
2020-07-07 19:05:42 +03:00
|
|
|
if (!parse_option_size("shm_size", optarg, &args->shm_size,
|
|
|
|
&err)) {
|
2015-12-18 18:35:27 +03:00
|
|
|
error_report_err(err);
|
2016-03-15 21:34:17 +03:00
|
|
|
ivshmem_server_help(argv[0]);
|
|
|
|
exit(1);
|
2014-09-08 13:17:48 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-03-15 21:34:17 +03:00
|
|
|
case 'n': /* number of vectors */
|
2023-05-22 22:04:29 +03:00
|
|
|
if (parse_uint_full(optarg, 0, &v) < 0) {
|
2014-09-08 13:17:48 +04:00
|
|
|
fprintf(stderr, "cannot parse n_vectors\n");
|
2016-03-15 21:34:17 +03:00
|
|
|
ivshmem_server_help(argv[0]);
|
|
|
|
exit(1);
|
2014-09-08 13:17:48 +04:00
|
|
|
}
|
|
|
|
args->n_vectors = v;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-03-15 21:34:17 +03:00
|
|
|
ivshmem_server_usage(argv[0]);
|
|
|
|
exit(1);
|
2014-09-08 13:17:48 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args->n_vectors > IVSHMEM_SERVER_MAX_VECTORS) {
|
|
|
|
fprintf(stderr, "too many requested vectors (max is %d)\n",
|
|
|
|
IVSHMEM_SERVER_MAX_VECTORS);
|
2016-03-15 21:34:17 +03:00
|
|
|
ivshmem_server_help(argv[0]);
|
|
|
|
exit(1);
|
2014-09-08 13:17:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (args->verbose == 1 && args->foreground == 0) {
|
|
|
|
fprintf(stderr, "cannot use verbose in daemon mode\n");
|
2016-03-15 21:34:17 +03:00
|
|
|
ivshmem_server_help(argv[0]);
|
|
|
|
exit(1);
|
2014-09-08 13:17:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait for events on listening server unix socket and connected client
|
|
|
|
* sockets */
|
|
|
|
static int
|
|
|
|
ivshmem_server_poll_events(IvshmemServer *server)
|
|
|
|
{
|
|
|
|
fd_set fds;
|
|
|
|
int ret = 0, maxfd;
|
|
|
|
|
|
|
|
while (!ivshmem_server_quit) {
|
|
|
|
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
maxfd = 0;
|
|
|
|
ivshmem_server_get_fds(server, &fds, &maxfd);
|
|
|
|
|
|
|
|
ret = select(maxfd, &fds, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "select error: %s\n", strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ret == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ivshmem_server_handle_fds(server, &fds, maxfd) < 0) {
|
|
|
|
fprintf(stderr, "ivshmem_server_handle_fds() failed\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ivshmem_server_quit_cb(int signum)
|
|
|
|
{
|
|
|
|
ivshmem_server_quit = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
IvshmemServer server;
|
|
|
|
struct sigaction sa, sa_quit;
|
|
|
|
IvshmemServerArgs args = {
|
|
|
|
.verbose = IVSHMEM_SERVER_DEFAULT_VERBOSE,
|
|
|
|
.foreground = IVSHMEM_SERVER_DEFAULT_FOREGROUND,
|
|
|
|
.pid_file = IVSHMEM_SERVER_DEFAULT_PID_FILE,
|
|
|
|
.unix_socket_path = IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH,
|
|
|
|
.shm_path = IVSHMEM_SERVER_DEFAULT_SHM_PATH,
|
2016-03-15 21:34:18 +03:00
|
|
|
.use_shm_open = true,
|
2014-09-08 13:17:48 +04:00
|
|
|
.shm_size = IVSHMEM_SERVER_DEFAULT_SHM_SIZE,
|
|
|
|
.n_vectors = IVSHMEM_SERVER_DEFAULT_N_VECTORS,
|
|
|
|
};
|
|
|
|
int ret = 1;
|
|
|
|
|
2016-03-15 21:34:55 +03:00
|
|
|
/*
|
|
|
|
* Do not remove this notice without adding proper error handling!
|
|
|
|
* Start with handling ivshmem_server_send_one_msg() failure.
|
|
|
|
*/
|
|
|
|
printf("*** Example code, do not use in production ***\n");
|
|
|
|
|
2014-09-08 13:17:48 +04:00
|
|
|
/* parse arguments, will exit on error */
|
|
|
|
ivshmem_server_parse_args(&args, argc, argv);
|
|
|
|
|
|
|
|
/* Ignore SIGPIPE, see this link for more info:
|
|
|
|
* http://www.mail-archive.com/libevent-users@monkey.org/msg01606.html */
|
|
|
|
sa.sa_handler = SIG_IGN;
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
if (sigemptyset(&sa.sa_mask) == -1 ||
|
|
|
|
sigaction(SIGPIPE, &sa, 0) == -1) {
|
|
|
|
perror("failed to ignore SIGPIPE; sigaction");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
sa_quit.sa_handler = ivshmem_server_quit_cb;
|
|
|
|
sa_quit.sa_flags = 0;
|
|
|
|
if (sigemptyset(&sa_quit.sa_mask) == -1 ||
|
2019-08-03 16:22:04 +03:00
|
|
|
sigaction(SIGTERM, &sa_quit, 0) == -1 ||
|
|
|
|
sigaction(SIGINT, &sa_quit, 0) == -1) {
|
|
|
|
perror("failed to add signal handler; sigaction");
|
2014-09-08 13:17:48 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init the ivshms structure */
|
2016-03-15 21:34:18 +03:00
|
|
|
if (ivshmem_server_init(&server, args.unix_socket_path,
|
|
|
|
args.shm_path, args.use_shm_open,
|
2014-09-08 13:17:48 +04:00
|
|
|
args.shm_size, args.n_vectors, args.verbose) < 0) {
|
|
|
|
fprintf(stderr, "cannot init server\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start the ivshmem server (open shm & unix socket) */
|
|
|
|
if (ivshmem_server_start(&server) < 0) {
|
|
|
|
fprintf(stderr, "cannot bind\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* daemonize if asked to */
|
|
|
|
if (!args.foreground) {
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if (qemu_daemon(1, 1) < 0) {
|
|
|
|
fprintf(stderr, "cannot daemonize: %s\n", strerror(errno));
|
|
|
|
goto err_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write pid file */
|
|
|
|
fp = fopen(args.pid_file, "w");
|
|
|
|
if (fp == NULL) {
|
|
|
|
fprintf(stderr, "cannot write pid file: %s\n", strerror(errno));
|
|
|
|
goto err_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fp, "%d\n", (int) getpid());
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
ivshmem_server_poll_events(&server);
|
|
|
|
fprintf(stdout, "server disconnected\n");
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
err_close:
|
|
|
|
ivshmem_server_close(&server);
|
|
|
|
err:
|
|
|
|
return ret;
|
|
|
|
}
|