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/osdep.h"
10 #include "qemu-common.h"
12 #include "ivshmem-client.h"
14 #define IVSHMEM_CLIENT_DEFAULT_VERBOSE 0
15 #define IVSHMEM_CLIENT_DEFAULT_UNIX_SOCK_PATH "/tmp/ivshmem_socket"
17 typedef struct IvshmemClientArgs {
19 const char *unix_sock_path;
22 /* show ivshmem_client_usage and exit with given error code */
24 ivshmem_client_usage(const char *name, int code)
26 fprintf(stderr, "%s [opts]\n", name);
27 fprintf(stderr, " -h: show this help\n");
28 fprintf(stderr, " -v: verbose mode\n");
29 fprintf(stderr, " -S <unix_sock_path>: path to the unix socket\n"
31 " default=%s\n", IVSHMEM_CLIENT_DEFAULT_UNIX_SOCK_PATH);
35 /* parse the program arguments, exit on error */
37 ivshmem_client_parse_args(IvshmemClientArgs *args, int argc, char *argv[])
41 while ((c = getopt(argc, argv,
44 "S:" /* unix_sock_path */
49 ivshmem_client_usage(argv[0], 0);
52 case 'v': /* verbose */
56 case 'S': /* unix_sock_path */
57 args->unix_sock_path = optarg;
61 ivshmem_client_usage(argv[0], 1);
67 /* show command line help */
69 ivshmem_client_cmdline_help(void)
71 printf("dump: dump peers (including us)\n"
72 "int <peer> <vector>: notify one vector on a peer\n"
73 "int <peer> all: notify all vectors of a peer\n"
74 "int all: notify all vectors of all peers (excepting us)\n");
77 /* read stdin and handle commands */
79 ivshmem_client_handle_stdin_command(IvshmemClient *client)
81 IvshmemClientPeer *peer;
87 memset(buf, 0, sizeof(buf));
88 ret = read(0, buf, sizeof(buf) - 1);
94 while ((token = strsep(&s, "\n\r;")) != NULL) {
95 if (!strcmp(token, "")) {
98 if (!strcmp(token, "?")) {
99 ivshmem_client_cmdline_help();
101 if (!strcmp(token, "help")) {
102 ivshmem_client_cmdline_help();
103 } else if (!strcmp(token, "dump")) {
104 ivshmem_client_dump(client);
105 } else if (!strcmp(token, "int all")) {
106 ivshmem_client_notify_broadcast(client);
107 } else if (sscanf(token, "int %d %d", &peer_id, &vector) == 2) {
108 peer = ivshmem_client_search_peer(client, peer_id);
110 printf("cannot find peer_id = %d\n", peer_id);
113 ivshmem_client_notify(client, peer, vector);
114 } else if (sscanf(token, "int %d all", &peer_id) == 1) {
115 peer = ivshmem_client_search_peer(client, peer_id);
117 printf("cannot find peer_id = %d\n", peer_id);
120 ivshmem_client_notify_all_vects(client, peer);
122 printf("invalid command, type help\n");
131 /* listen on stdin (command line), on unix socket (notifications of new
132 * and dead peers), and on eventfd (IRQ request) */
134 ivshmem_client_poll_events(IvshmemClient *client)
142 FD_SET(0, &fds); /* add stdin in fd_set */
145 ivshmem_client_get_fds(client, &fds, &maxfd);
147 ret = select(maxfd, &fds, NULL, NULL, NULL);
149 if (errno == EINTR) {
153 fprintf(stderr, "select error: %s\n", strerror(errno));
160 if (FD_ISSET(0, &fds) &&
161 ivshmem_client_handle_stdin_command(client) < 0 && errno != EINTR) {
162 fprintf(stderr, "ivshmem_client_handle_stdin_command() failed\n");
166 if (ivshmem_client_handle_fds(client, &fds, maxfd) < 0) {
167 fprintf(stderr, "ivshmem_client_handle_fds() failed\n");
175 /* callback when we receive a notification (just display it) */
177 ivshmem_client_notification_cb(const IvshmemClient *client,
178 const IvshmemClientPeer *peer,
179 unsigned vect, void *arg)
183 printf("receive notification from peer_id=%" PRId64 " vector=%u\n",
188 main(int argc, char *argv[])
191 IvshmemClient client;
192 IvshmemClientArgs args = {
193 .verbose = IVSHMEM_CLIENT_DEFAULT_VERBOSE,
194 .unix_sock_path = IVSHMEM_CLIENT_DEFAULT_UNIX_SOCK_PATH,
197 /* parse arguments, will exit on error */
198 ivshmem_client_parse_args(&args, argc, argv);
200 /* Ignore SIGPIPE, see this link for more info:
202 sa.sa_handler = SIG_IGN;
204 if (sigemptyset(&sa.sa_mask) == -1 ||
205 sigaction(SIGPIPE, &sa, 0) == -1) {
206 perror("failed to ignore SIGPIPE; sigaction");
210 ivshmem_client_cmdline_help();
214 if (ivshmem_client_init(&client, args.unix_sock_path,
215 ivshmem_client_notification_cb, NULL,
217 fprintf(stderr, "cannot init client\n");
222 if (ivshmem_client_connect(&client) < 0) {
223 fprintf(stderr, "cannot connect to server, retry in 1 second\n");
228 fprintf(stdout, "listen on server socket %d\n", client.sock_fd);
230 if (ivshmem_client_poll_events(&client) == 0) {
234 /* disconnected from server, reset all peers */
235 fprintf(stdout, "disconnected from server\n");
237 ivshmem_client_close(&client);