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