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"
11 #include "ivshmem-client.h"
13 #define IVSHMEM_CLIENT_DEFAULT_VERBOSE 0
14 #define IVSHMEM_CLIENT_DEFAULT_UNIX_SOCK_PATH "/tmp/ivshmem_socket"
16 typedef struct IvshmemClientArgs
{
18 const char *unix_sock_path
;
21 /* show ivshmem_client_usage and exit with given error code */
23 ivshmem_client_usage(const char *name
, int code
)
25 fprintf(stderr
, "%s [opts]\n", name
);
26 fprintf(stderr
, " -h: show this help\n");
27 fprintf(stderr
, " -v: verbose mode\n");
28 fprintf(stderr
, " -S <unix_sock_path>: path to the unix socket\n"
30 " default=%s\n", IVSHMEM_CLIENT_DEFAULT_UNIX_SOCK_PATH
);
34 /* parse the program arguments, exit on error */
36 ivshmem_client_parse_args(IvshmemClientArgs
*args
, int argc
, char *argv
[])
40 while ((c
= getopt(argc
, argv
,
43 "S:" /* unix_sock_path */
48 ivshmem_client_usage(argv
[0], 0);
51 case 'v': /* verbose */
55 case 'S': /* unix_sock_path */
56 args
->unix_sock_path
= optarg
;
60 ivshmem_client_usage(argv
[0], 1);
66 /* show command line help */
68 ivshmem_client_cmdline_help(void)
70 printf("dump: dump peers (including us)\n"
71 "int <peer> <vector>: notify one vector on a peer\n"
72 "int <peer> all: notify all vectors of a peer\n"
73 "int all: notify all vectors of all peers (excepting us)\n");
76 /* read stdin and handle commands */
78 ivshmem_client_handle_stdin_command(IvshmemClient
*client
)
80 IvshmemClientPeer
*peer
;
86 memset(buf
, 0, sizeof(buf
));
87 ret
= read(0, buf
, sizeof(buf
) - 1);
93 while ((token
= strsep(&s
, "\n\r;")) != NULL
) {
94 if (!strcmp(token
, "")) {
97 if (!strcmp(token
, "?")) {
98 ivshmem_client_cmdline_help();
100 if (!strcmp(token
, "help")) {
101 ivshmem_client_cmdline_help();
102 } else if (!strcmp(token
, "dump")) {
103 ivshmem_client_dump(client
);
104 } else if (!strcmp(token
, "int all")) {
105 ivshmem_client_notify_broadcast(client
);
106 } else if (sscanf(token
, "int %d %d", &peer_id
, &vector
) == 2) {
107 peer
= ivshmem_client_search_peer(client
, peer_id
);
109 printf("cannot find peer_id = %d\n", peer_id
);
112 ivshmem_client_notify(client
, peer
, vector
);
113 } else if (sscanf(token
, "int %d all", &peer_id
) == 1) {
114 peer
= ivshmem_client_search_peer(client
, peer_id
);
116 printf("cannot find peer_id = %d\n", peer_id
);
119 ivshmem_client_notify_all_vects(client
, peer
);
121 printf("invalid command, type help\n");
130 /* listen on stdin (command line), on unix socket (notifications of new
131 * and dead peers), and on eventfd (IRQ request) */
133 ivshmem_client_poll_events(IvshmemClient
*client
)
141 FD_SET(0, &fds
); /* add stdin in fd_set */
144 ivshmem_client_get_fds(client
, &fds
, &maxfd
);
146 ret
= select(maxfd
, &fds
, NULL
, NULL
, NULL
);
148 if (errno
== EINTR
) {
152 fprintf(stderr
, "select error: %s\n", strerror(errno
));
159 if (FD_ISSET(0, &fds
) &&
160 ivshmem_client_handle_stdin_command(client
) < 0 && errno
!= EINTR
) {
161 fprintf(stderr
, "ivshmem_client_handle_stdin_command() failed\n");
165 if (ivshmem_client_handle_fds(client
, &fds
, maxfd
) < 0) {
166 fprintf(stderr
, "ivshmem_client_handle_fds() failed\n");
174 /* callback when we receive a notification (just display it) */
176 ivshmem_client_notification_cb(const IvshmemClient
*client
,
177 const IvshmemClientPeer
*peer
,
178 unsigned vect
, void *arg
)
182 printf("receive notification from peer_id=%" PRId64
" vector=%u\n",
187 main(int argc
, char *argv
[])
190 IvshmemClient client
;
191 IvshmemClientArgs args
= {
192 .verbose
= IVSHMEM_CLIENT_DEFAULT_VERBOSE
,
193 .unix_sock_path
= IVSHMEM_CLIENT_DEFAULT_UNIX_SOCK_PATH
,
196 /* parse arguments, will exit on error */
197 ivshmem_client_parse_args(&args
, argc
, argv
);
199 /* Ignore SIGPIPE, see this link for more info:
200 * http://www.mail-archive.com/libevent-users@monkey.org/msg01606.html */
201 sa
.sa_handler
= SIG_IGN
;
203 if (sigemptyset(&sa
.sa_mask
) == -1 ||
204 sigaction(SIGPIPE
, &sa
, 0) == -1) {
205 perror("failed to ignore SIGPIPE; sigaction");
209 ivshmem_client_cmdline_help();
213 if (ivshmem_client_init(&client
, args
.unix_sock_path
,
214 ivshmem_client_notification_cb
, NULL
,
216 fprintf(stderr
, "cannot init client\n");
221 if (ivshmem_client_connect(&client
) < 0) {
222 fprintf(stderr
, "cannot connect to server, retry in 1 second\n");
227 fprintf(stdout
, "listen on server socket %d\n", client
.sock_fd
);
229 if (ivshmem_client_poll_events(&client
) == 0) {
233 /* disconnected from server, reset all peers */
234 fprintf(stdout
, "disconnected from server\n");
236 ivshmem_client_close(&client
);