2 * Copyright 6WIND S.A., 2014
4 * This work is licensed under the terms of the GNU GPL, version 2 or
5 * (at your option) any later version. See the COPYING file in the
9 #include "qemu-common.h"
11 #include "ivshmem-server.h"
13 #define IVSHMEM_SERVER_DEFAULT_VERBOSE 0
14 #define IVSHMEM_SERVER_DEFAULT_FOREGROUND 0
15 #define IVSHMEM_SERVER_DEFAULT_PID_FILE "/var/run/ivshmem-server.pid"
16 #define IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH "/tmp/ivshmem_socket"
17 #define IVSHMEM_SERVER_DEFAULT_SHM_PATH "ivshmem"
18 #define IVSHMEM_SERVER_DEFAULT_SHM_SIZE (4*1024*1024)
19 #define IVSHMEM_SERVER_DEFAULT_N_VECTORS 1
21 /* used to quit on signal SIGTERM */
22 static int ivshmem_server_quit
;
24 /* arguments given by the user */
25 typedef struct IvshmemServerArgs
{
29 const char *unix_socket_path
;
35 /* show ivshmem_server_usage and exit with given error code */
37 ivshmem_server_usage(const char *name
, int code
)
39 fprintf(stderr
, "%s [opts]\n", name
);
40 fprintf(stderr
, " -h: show this help\n");
41 fprintf(stderr
, " -v: verbose mode\n");
42 fprintf(stderr
, " -F: foreground mode (default is to daemonize)\n");
43 fprintf(stderr
, " -p <pid_file>: path to the PID file (used in daemon\n"
45 " Default=%s\n", IVSHMEM_SERVER_DEFAULT_SHM_PATH
);
46 fprintf(stderr
, " -S <unix_socket_path>: path to the unix socket\n"
48 " Default=%s\n", IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH
);
49 fprintf(stderr
, " -m <shm_path>: path to the shared memory.\n"
50 " The path corresponds to a POSIX shm name or a\n"
51 " hugetlbfs mount point.\n"
52 " default=%s\n", IVSHMEM_SERVER_DEFAULT_SHM_PATH
);
53 fprintf(stderr
, " -l <size>: size of shared memory in bytes. The suffix\n"
54 " K, M and G can be used (ex: 1K means 1024).\n"
55 " default=%u\n", IVSHMEM_SERVER_DEFAULT_SHM_SIZE
);
56 fprintf(stderr
, " -n <n_vects>: number of vectors.\n"
57 " default=%u\n", IVSHMEM_SERVER_DEFAULT_N_VECTORS
);
62 /* parse the program arguments, exit on error */
64 ivshmem_server_parse_args(IvshmemServerArgs
*args
, int argc
, char *argv
[])
70 while ((c
= getopt(argc
, argv
,
75 "S:" /* unix_socket_path */
83 ivshmem_server_usage(argv
[0], 0);
86 case 'v': /* verbose */
90 case 'F': /* foreground */
94 case 'p': /* pid_file */
95 args
->pid_file
= optarg
;
98 case 'S': /* unix_socket_path */
99 args
->unix_socket_path
= optarg
;
102 case 'm': /* shm_path */
103 args
->shm_path
= optarg
;
106 case 'l': /* shm_size */
107 parse_option_size("shm_size", optarg
, &args
->shm_size
, &err
);
109 error_report_err(err
);
110 ivshmem_server_usage(argv
[0], 1);
114 case 'n': /* n_vectors */
115 if (parse_uint_full(optarg
, &v
, 0) < 0) {
116 fprintf(stderr
, "cannot parse n_vectors\n");
117 ivshmem_server_usage(argv
[0], 1);
123 ivshmem_server_usage(argv
[0], 1);
128 if (args
->n_vectors
> IVSHMEM_SERVER_MAX_VECTORS
) {
129 fprintf(stderr
, "too many requested vectors (max is %d)\n",
130 IVSHMEM_SERVER_MAX_VECTORS
);
131 ivshmem_server_usage(argv
[0], 1);
134 if (args
->verbose
== 1 && args
->foreground
== 0) {
135 fprintf(stderr
, "cannot use verbose in daemon mode\n");
136 ivshmem_server_usage(argv
[0], 1);
140 /* wait for events on listening server unix socket and connected client
143 ivshmem_server_poll_events(IvshmemServer
*server
)
148 while (!ivshmem_server_quit
) {
152 ivshmem_server_get_fds(server
, &fds
, &maxfd
);
154 ret
= select(maxfd
, &fds
, NULL
, NULL
, NULL
);
157 if (errno
== EINTR
) {
161 fprintf(stderr
, "select error: %s\n", strerror(errno
));
168 if (ivshmem_server_handle_fds(server
, &fds
, maxfd
) < 0) {
169 fprintf(stderr
, "ivshmem_server_handle_fds() failed\n");
178 ivshmem_server_quit_cb(int signum
)
180 ivshmem_server_quit
= 1;
184 main(int argc
, char *argv
[])
186 IvshmemServer server
;
187 struct sigaction sa
, sa_quit
;
188 IvshmemServerArgs args
= {
189 .verbose
= IVSHMEM_SERVER_DEFAULT_VERBOSE
,
190 .foreground
= IVSHMEM_SERVER_DEFAULT_FOREGROUND
,
191 .pid_file
= IVSHMEM_SERVER_DEFAULT_PID_FILE
,
192 .unix_socket_path
= IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH
,
193 .shm_path
= IVSHMEM_SERVER_DEFAULT_SHM_PATH
,
194 .shm_size
= IVSHMEM_SERVER_DEFAULT_SHM_SIZE
,
195 .n_vectors
= IVSHMEM_SERVER_DEFAULT_N_VECTORS
,
199 /* parse arguments, will exit on error */
200 ivshmem_server_parse_args(&args
, argc
, argv
);
202 /* Ignore SIGPIPE, see this link for more info:
203 * http://www.mail-archive.com/libevent-users@monkey.org/msg01606.html */
204 sa
.sa_handler
= SIG_IGN
;
206 if (sigemptyset(&sa
.sa_mask
) == -1 ||
207 sigaction(SIGPIPE
, &sa
, 0) == -1) {
208 perror("failed to ignore SIGPIPE; sigaction");
212 sa_quit
.sa_handler
= ivshmem_server_quit_cb
;
213 sa_quit
.sa_flags
= 0;
214 if (sigemptyset(&sa_quit
.sa_mask
) == -1 ||
215 sigaction(SIGTERM
, &sa_quit
, 0) == -1) {
216 perror("failed to add SIGTERM handler; sigaction");
220 /* init the ivshms structure */
221 if (ivshmem_server_init(&server
, args
.unix_socket_path
, args
.shm_path
,
222 args
.shm_size
, args
.n_vectors
, args
.verbose
) < 0) {
223 fprintf(stderr
, "cannot init server\n");
227 /* start the ivshmem server (open shm & unix socket) */
228 if (ivshmem_server_start(&server
) < 0) {
229 fprintf(stderr
, "cannot bind\n");
233 /* daemonize if asked to */
234 if (!args
.foreground
) {
237 if (qemu_daemon(1, 1) < 0) {
238 fprintf(stderr
, "cannot daemonize: %s\n", strerror(errno
));
243 fp
= fopen(args
.pid_file
, "w");
245 fprintf(stderr
, "cannot write pid file: %s\n", strerror(errno
));
249 fprintf(fp
, "%d\n", (int) getpid());
253 ivshmem_server_poll_events(&server
);
254 fprintf(stdout
, "server disconnected\n");
258 ivshmem_server_close(&server
);