xref: /openbmc/qemu/contrib/ivshmem-server/main.c (revision 24bc242c91ae1d4db8de33b65de8c6666f975ad2)
1a75eb03bSDavid Marchand /*
2a75eb03bSDavid Marchand  * Copyright 6WIND S.A., 2014
3a75eb03bSDavid Marchand  *
4a75eb03bSDavid Marchand  * This work is licensed under the terms of the GNU GPL, version 2 or
5a75eb03bSDavid Marchand  * (at your option) any later version.  See the COPYING file in the
6a75eb03bSDavid Marchand  * top-level directory.
7a75eb03bSDavid Marchand  */
8a75eb03bSDavid Marchand 
9ccd241b5SPeter Maydell #include "qemu/osdep.h"
10da34e65cSMarkus Armbruster #include "qapi/error.h"
11f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
12922a01a0SMarkus Armbruster #include "qemu/option.h"
13a75eb03bSDavid Marchand #include "ivshmem-server.h"
14a75eb03bSDavid Marchand 
15a75eb03bSDavid Marchand #define IVSHMEM_SERVER_DEFAULT_VERBOSE        0
16a75eb03bSDavid Marchand #define IVSHMEM_SERVER_DEFAULT_FOREGROUND     0
17a75eb03bSDavid Marchand #define IVSHMEM_SERVER_DEFAULT_PID_FILE       "/var/run/ivshmem-server.pid"
18a75eb03bSDavid Marchand #define IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH "/tmp/ivshmem_socket"
19a75eb03bSDavid Marchand #define IVSHMEM_SERVER_DEFAULT_SHM_PATH       "ivshmem"
20a75eb03bSDavid Marchand #define IVSHMEM_SERVER_DEFAULT_SHM_SIZE       (4 * 1024 * 1024)
21a75eb03bSDavid Marchand #define IVSHMEM_SERVER_DEFAULT_N_VECTORS      1
22a75eb03bSDavid Marchand 
23a75eb03bSDavid Marchand /* used to quit on signal SIGTERM */
24a75eb03bSDavid Marchand static int ivshmem_server_quit;
25a75eb03bSDavid Marchand 
26a75eb03bSDavid Marchand /* arguments given by the user */
27a75eb03bSDavid Marchand typedef struct IvshmemServerArgs {
28a75eb03bSDavid Marchand     bool verbose;
29a75eb03bSDavid Marchand     bool foreground;
30a75eb03bSDavid Marchand     const char *pid_file;
31a75eb03bSDavid Marchand     const char *unix_socket_path;
32a75eb03bSDavid Marchand     const char *shm_path;
333625c739SMarkus Armbruster     bool use_shm_open;
34a75eb03bSDavid Marchand     uint64_t shm_size;
35a75eb03bSDavid Marchand     unsigned n_vectors;
36a75eb03bSDavid Marchand } IvshmemServerArgs;
37a75eb03bSDavid Marchand 
38a75eb03bSDavid Marchand static void
ivshmem_server_usage(const char * progname)39e3ad7296SMarkus Armbruster ivshmem_server_usage(const char *progname)
40a75eb03bSDavid Marchand {
41e3ad7296SMarkus Armbruster     printf("Usage: %s [OPTION]...\n"
42e3ad7296SMarkus Armbruster            "  -h: show this help\n"
43e3ad7296SMarkus Armbruster            "  -v: verbose mode\n"
44e3ad7296SMarkus Armbruster            "  -F: foreground mode (default is to daemonize)\n"
45e3ad7296SMarkus Armbruster            "  -p <pid-file>: path to the PID file (used in daemon mode only)\n"
46e3ad7296SMarkus Armbruster            "     default " IVSHMEM_SERVER_DEFAULT_PID_FILE "\n"
47e3ad7296SMarkus Armbruster            "  -S <unix-socket-path>: path to the unix socket to listen to\n"
48e3ad7296SMarkus Armbruster            "     default " IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH "\n"
493625c739SMarkus Armbruster            "  -M <shm-name>: POSIX shared memory object to use\n"
50e3ad7296SMarkus Armbruster            "     default " IVSHMEM_SERVER_DEFAULT_SHM_PATH "\n"
513625c739SMarkus Armbruster            "  -m <dir-name>: where to create shared memory\n"
52e3ad7296SMarkus Armbruster            "  -l <size>: size of shared memory in bytes\n"
53e3ad7296SMarkus Armbruster            "     suffixes K, M and G can be used, e.g. 1K means 1024\n"
54e3ad7296SMarkus Armbruster            "     default %u\n"
55e3ad7296SMarkus Armbruster            "  -n <nvectors>: number of vectors\n"
56e3ad7296SMarkus Armbruster            "     default %u\n",
57e3ad7296SMarkus Armbruster            progname, IVSHMEM_SERVER_DEFAULT_SHM_SIZE,
58e3ad7296SMarkus Armbruster            IVSHMEM_SERVER_DEFAULT_N_VECTORS);
59e3ad7296SMarkus Armbruster }
60a75eb03bSDavid Marchand 
61e3ad7296SMarkus Armbruster static void
ivshmem_server_help(const char * progname)62e3ad7296SMarkus Armbruster ivshmem_server_help(const char *progname)
63e3ad7296SMarkus Armbruster {
64e3ad7296SMarkus Armbruster     fprintf(stderr, "Try '%s -h' for more information.\n", progname);
65a75eb03bSDavid Marchand }
66a75eb03bSDavid Marchand 
67a75eb03bSDavid Marchand /* parse the program arguments, exit on error */
68a75eb03bSDavid Marchand static void
ivshmem_server_parse_args(IvshmemServerArgs * args,int argc,char * argv[])69a75eb03bSDavid Marchand ivshmem_server_parse_args(IvshmemServerArgs *args, int argc, char *argv[])
70a75eb03bSDavid Marchand {
71a75eb03bSDavid Marchand     int c;
72*bd1386ccSEric Blake     uint64_t v;
73533fdaedSMarkus Armbruster     Error *err = NULL;
74a75eb03bSDavid Marchand 
753625c739SMarkus Armbruster     while ((c = getopt(argc, argv, "hvFp:S:m:M:l:n:")) != -1) {
76a75eb03bSDavid Marchand 
77a75eb03bSDavid Marchand         switch (c) {
78a75eb03bSDavid Marchand         case 'h': /* help */
79e3ad7296SMarkus Armbruster             ivshmem_server_usage(argv[0]);
80e3ad7296SMarkus Armbruster             exit(0);
81a75eb03bSDavid Marchand             break;
82a75eb03bSDavid Marchand 
83a75eb03bSDavid Marchand         case 'v': /* verbose */
84a75eb03bSDavid Marchand             args->verbose = 1;
85a75eb03bSDavid Marchand             break;
86a75eb03bSDavid Marchand 
87a75eb03bSDavid Marchand         case 'F': /* foreground */
88a75eb03bSDavid Marchand             args->foreground = 1;
89a75eb03bSDavid Marchand             break;
90a75eb03bSDavid Marchand 
91e3ad7296SMarkus Armbruster         case 'p': /* pid file */
9245b00c44SMarc-André Lureau             args->pid_file = optarg;
93a75eb03bSDavid Marchand             break;
94a75eb03bSDavid Marchand 
95e3ad7296SMarkus Armbruster         case 'S': /* unix socket path */
9645b00c44SMarc-André Lureau             args->unix_socket_path = optarg;
97a75eb03bSDavid Marchand             break;
98a75eb03bSDavid Marchand 
993625c739SMarkus Armbruster         case 'M': /* shm name */
1003625c739SMarkus Armbruster         case 'm': /* dir name */
10145b00c44SMarc-André Lureau             args->shm_path = optarg;
1023625c739SMarkus Armbruster             args->use_shm_open = c == 'M';
103a75eb03bSDavid Marchand             break;
104a75eb03bSDavid Marchand 
105e3ad7296SMarkus Armbruster         case 'l': /* shm size */
106235e59cfSMarkus Armbruster             if (!parse_option_size("shm_size", optarg, &args->shm_size,
107235e59cfSMarkus Armbruster                                    &err)) {
108533fdaedSMarkus Armbruster                 error_report_err(err);
109e3ad7296SMarkus Armbruster                 ivshmem_server_help(argv[0]);
110e3ad7296SMarkus Armbruster                 exit(1);
111a75eb03bSDavid Marchand             }
112a75eb03bSDavid Marchand             break;
113a75eb03bSDavid Marchand 
114e3ad7296SMarkus Armbruster         case 'n': /* number of vectors */
115*bd1386ccSEric Blake             if (parse_uint_full(optarg, 0, &v) < 0) {
116a75eb03bSDavid Marchand                 fprintf(stderr, "cannot parse n_vectors\n");
117e3ad7296SMarkus Armbruster                 ivshmem_server_help(argv[0]);
118e3ad7296SMarkus Armbruster                 exit(1);
119a75eb03bSDavid Marchand             }
120a75eb03bSDavid Marchand             args->n_vectors = v;
121a75eb03bSDavid Marchand             break;
122a75eb03bSDavid Marchand 
123a75eb03bSDavid Marchand         default:
124e3ad7296SMarkus Armbruster             ivshmem_server_usage(argv[0]);
125e3ad7296SMarkus Armbruster             exit(1);
126a75eb03bSDavid Marchand             break;
127a75eb03bSDavid Marchand         }
128a75eb03bSDavid Marchand     }
129a75eb03bSDavid Marchand 
130a75eb03bSDavid Marchand     if (args->n_vectors > IVSHMEM_SERVER_MAX_VECTORS) {
131a75eb03bSDavid Marchand         fprintf(stderr, "too many requested vectors (max is %d)\n",
132a75eb03bSDavid Marchand                 IVSHMEM_SERVER_MAX_VECTORS);
133e3ad7296SMarkus Armbruster         ivshmem_server_help(argv[0]);
134e3ad7296SMarkus Armbruster         exit(1);
135a75eb03bSDavid Marchand     }
136a75eb03bSDavid Marchand 
137a75eb03bSDavid Marchand     if (args->verbose == 1 && args->foreground == 0) {
138a75eb03bSDavid Marchand         fprintf(stderr, "cannot use verbose in daemon mode\n");
139e3ad7296SMarkus Armbruster         ivshmem_server_help(argv[0]);
140e3ad7296SMarkus Armbruster         exit(1);
141a75eb03bSDavid Marchand     }
142a75eb03bSDavid Marchand }
143a75eb03bSDavid Marchand 
144a75eb03bSDavid Marchand /* wait for events on listening server unix socket and connected client
145a75eb03bSDavid Marchand  * sockets */
146a75eb03bSDavid Marchand static int
ivshmem_server_poll_events(IvshmemServer * server)147a75eb03bSDavid Marchand ivshmem_server_poll_events(IvshmemServer *server)
148a75eb03bSDavid Marchand {
149a75eb03bSDavid Marchand     fd_set fds;
150a75eb03bSDavid Marchand     int ret = 0, maxfd;
151a75eb03bSDavid Marchand 
152a75eb03bSDavid Marchand     while (!ivshmem_server_quit) {
153a75eb03bSDavid Marchand 
154a75eb03bSDavid Marchand         FD_ZERO(&fds);
155a75eb03bSDavid Marchand         maxfd = 0;
156a75eb03bSDavid Marchand         ivshmem_server_get_fds(server, &fds, &maxfd);
157a75eb03bSDavid Marchand 
158a75eb03bSDavid Marchand         ret = select(maxfd, &fds, NULL, NULL, NULL);
159a75eb03bSDavid Marchand 
160a75eb03bSDavid Marchand         if (ret < 0) {
161a75eb03bSDavid Marchand             if (errno == EINTR) {
162a75eb03bSDavid Marchand                 continue;
163a75eb03bSDavid Marchand             }
164a75eb03bSDavid Marchand 
165a75eb03bSDavid Marchand             fprintf(stderr, "select error: %s\n", strerror(errno));
166a75eb03bSDavid Marchand             break;
167a75eb03bSDavid Marchand         }
168a75eb03bSDavid Marchand         if (ret == 0) {
169a75eb03bSDavid Marchand             continue;
170a75eb03bSDavid Marchand         }
171a75eb03bSDavid Marchand 
172a75eb03bSDavid Marchand         if (ivshmem_server_handle_fds(server, &fds, maxfd) < 0) {
173a75eb03bSDavid Marchand             fprintf(stderr, "ivshmem_server_handle_fds() failed\n");
174a75eb03bSDavid Marchand             break;
175a75eb03bSDavid Marchand         }
176a75eb03bSDavid Marchand     }
177a75eb03bSDavid Marchand 
178a75eb03bSDavid Marchand     return ret;
179a75eb03bSDavid Marchand }
180a75eb03bSDavid Marchand 
181a75eb03bSDavid Marchand static void
ivshmem_server_quit_cb(int signum)182a75eb03bSDavid Marchand ivshmem_server_quit_cb(int signum)
183a75eb03bSDavid Marchand {
184a75eb03bSDavid Marchand     ivshmem_server_quit = 1;
185a75eb03bSDavid Marchand }
186a75eb03bSDavid Marchand 
187a75eb03bSDavid Marchand int
main(int argc,char * argv[])188a75eb03bSDavid Marchand main(int argc, char *argv[])
189a75eb03bSDavid Marchand {
190a75eb03bSDavid Marchand     IvshmemServer server;
191a75eb03bSDavid Marchand     struct sigaction sa, sa_quit;
192a75eb03bSDavid Marchand     IvshmemServerArgs args = {
193a75eb03bSDavid Marchand         .verbose = IVSHMEM_SERVER_DEFAULT_VERBOSE,
194a75eb03bSDavid Marchand         .foreground = IVSHMEM_SERVER_DEFAULT_FOREGROUND,
195a75eb03bSDavid Marchand         .pid_file = IVSHMEM_SERVER_DEFAULT_PID_FILE,
196a75eb03bSDavid Marchand         .unix_socket_path = IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH,
197a75eb03bSDavid Marchand         .shm_path = IVSHMEM_SERVER_DEFAULT_SHM_PATH,
1983625c739SMarkus Armbruster         .use_shm_open = true,
199a75eb03bSDavid Marchand         .shm_size = IVSHMEM_SERVER_DEFAULT_SHM_SIZE,
200a75eb03bSDavid Marchand         .n_vectors = IVSHMEM_SERVER_DEFAULT_N_VECTORS,
201a75eb03bSDavid Marchand     };
202a75eb03bSDavid Marchand     int ret = 1;
203a75eb03bSDavid Marchand 
204a335c6f2SMarkus Armbruster     /*
205a335c6f2SMarkus Armbruster      * Do not remove this notice without adding proper error handling!
206a335c6f2SMarkus Armbruster      * Start with handling ivshmem_server_send_one_msg() failure.
207a335c6f2SMarkus Armbruster      */
208a335c6f2SMarkus Armbruster     printf("*** Example code, do not use in production ***\n");
209a335c6f2SMarkus Armbruster 
210a75eb03bSDavid Marchand     /* parse arguments, will exit on error */
211a75eb03bSDavid Marchand     ivshmem_server_parse_args(&args, argc, argv);
212a75eb03bSDavid Marchand 
213a75eb03bSDavid Marchand     /* Ignore SIGPIPE, see this link for more info:
214a75eb03bSDavid Marchand      * http://www.mail-archive.com/libevent-users@monkey.org/msg01606.html */
215a75eb03bSDavid Marchand     sa.sa_handler = SIG_IGN;
216a75eb03bSDavid Marchand     sa.sa_flags = 0;
217a75eb03bSDavid Marchand     if (sigemptyset(&sa.sa_mask) == -1 ||
218a75eb03bSDavid Marchand         sigaction(SIGPIPE, &sa, 0) == -1) {
219a75eb03bSDavid Marchand         perror("failed to ignore SIGPIPE; sigaction");
220a75eb03bSDavid Marchand         goto err;
221a75eb03bSDavid Marchand     }
222a75eb03bSDavid Marchand 
223a75eb03bSDavid Marchand     sa_quit.sa_handler = ivshmem_server_quit_cb;
224a75eb03bSDavid Marchand     sa_quit.sa_flags = 0;
225a75eb03bSDavid Marchand     if (sigemptyset(&sa_quit.sa_mask) == -1 ||
2265c62979eSJan Kiszka         sigaction(SIGTERM, &sa_quit, 0) == -1 ||
2275c62979eSJan Kiszka         sigaction(SIGINT, &sa_quit, 0) == -1) {
2285c62979eSJan Kiszka         perror("failed to add signal handler; sigaction");
229a75eb03bSDavid Marchand         goto err;
230a75eb03bSDavid Marchand     }
231a75eb03bSDavid Marchand 
232a75eb03bSDavid Marchand     /* init the ivshms structure */
2333625c739SMarkus Armbruster     if (ivshmem_server_init(&server, args.unix_socket_path,
2343625c739SMarkus Armbruster                             args.shm_path, args.use_shm_open,
235a75eb03bSDavid Marchand                             args.shm_size, args.n_vectors, args.verbose) < 0) {
236a75eb03bSDavid Marchand         fprintf(stderr, "cannot init server\n");
237a75eb03bSDavid Marchand         goto err;
238a75eb03bSDavid Marchand     }
239a75eb03bSDavid Marchand 
240a75eb03bSDavid Marchand     /* start the ivshmem server (open shm & unix socket) */
241a75eb03bSDavid Marchand     if (ivshmem_server_start(&server) < 0) {
242a75eb03bSDavid Marchand         fprintf(stderr, "cannot bind\n");
243a75eb03bSDavid Marchand         goto err;
244a75eb03bSDavid Marchand     }
245a75eb03bSDavid Marchand 
246a75eb03bSDavid Marchand     /* daemonize if asked to */
247a75eb03bSDavid Marchand     if (!args.foreground) {
248a75eb03bSDavid Marchand         FILE *fp;
249a75eb03bSDavid Marchand 
250a75eb03bSDavid Marchand         if (qemu_daemon(1, 1) < 0) {
251a75eb03bSDavid Marchand             fprintf(stderr, "cannot daemonize: %s\n", strerror(errno));
252a75eb03bSDavid Marchand             goto err_close;
253a75eb03bSDavid Marchand         }
254a75eb03bSDavid Marchand 
255a75eb03bSDavid Marchand         /* write pid file */
256a75eb03bSDavid Marchand         fp = fopen(args.pid_file, "w");
257a75eb03bSDavid Marchand         if (fp == NULL) {
258a75eb03bSDavid Marchand             fprintf(stderr, "cannot write pid file: %s\n", strerror(errno));
259a75eb03bSDavid Marchand             goto err_close;
260a75eb03bSDavid Marchand         }
261a75eb03bSDavid Marchand 
262a75eb03bSDavid Marchand         fprintf(fp, "%d\n", (int) getpid());
263a75eb03bSDavid Marchand         fclose(fp);
264a75eb03bSDavid Marchand     }
265a75eb03bSDavid Marchand 
266a75eb03bSDavid Marchand     ivshmem_server_poll_events(&server);
267a75eb03bSDavid Marchand     fprintf(stdout, "server disconnected\n");
268a75eb03bSDavid Marchand     ret = 0;
269a75eb03bSDavid Marchand 
270a75eb03bSDavid Marchand err_close:
271a75eb03bSDavid Marchand     ivshmem_server_close(&server);
272a75eb03bSDavid Marchand err:
273a75eb03bSDavid Marchand     return ret;
274a75eb03bSDavid Marchand }
275