Lines Matching full:client

15 #include "ivshmem-client.h"
18 #define IVSHMEM_CLIENT_DEBUG(client, fmt, ...) do { \ argument
19 if ((client)->verbose) { \
26 ivshmem_client_read_one_msg(IvshmemClient *client, int64_t *index, int *fd) in ivshmem_client_read_one_msg() argument
46 ret = recvmsg(client->sock_fd, &msg, 0); in ivshmem_client_read_one_msg()
48 IVSHMEM_CLIENT_DEBUG(client, "cannot read message: %s\n", in ivshmem_client_read_one_msg()
53 IVSHMEM_CLIENT_DEBUG(client, "lost connection to server\n"); in ivshmem_client_read_one_msg()
75 * client is freed */
77 ivshmem_client_free_peer(IvshmemClient *client, IvshmemClientPeer *peer) in ivshmem_client_free_peer() argument
81 QTAILQ_REMOVE(&client->peer_list, peer, next); in ivshmem_client_free_peer()
91 ivshmem_client_handle_server_msg(IvshmemClient *client) in ivshmem_client_handle_server_msg() argument
97 ret = ivshmem_client_read_one_msg(client, &peer_id, &fd); in ivshmem_client_handle_server_msg()
102 /* can return a peer or the local client */ in ivshmem_client_handle_server_msg()
103 peer = ivshmem_client_search_peer(client, peer_id); in ivshmem_client_handle_server_msg()
108 if (peer == NULL || peer == &client->local) { in ivshmem_client_handle_server_msg()
109 IVSHMEM_CLIENT_DEBUG(client, "receive delete for invalid " in ivshmem_client_handle_server_msg()
114 IVSHMEM_CLIENT_DEBUG(client, "delete peer id = %" PRId64 "\n", peer_id); in ivshmem_client_handle_server_msg()
115 ivshmem_client_free_peer(client, peer); in ivshmem_client_handle_server_msg()
124 QTAILQ_INSERT_TAIL(&client->peer_list, peer, next); in ivshmem_client_handle_server_msg()
125 IVSHMEM_CLIENT_DEBUG(client, "new peer id = %" PRId64 "\n", peer_id); in ivshmem_client_handle_server_msg()
129 IVSHMEM_CLIENT_DEBUG(client, " new vector %d (fd=%d) for peer id %" in ivshmem_client_handle_server_msg()
132 IVSHMEM_CLIENT_DEBUG(client, "Too many vectors received, failing"); in ivshmem_client_handle_server_msg()
142 /* init a new ivshmem client */
144 ivshmem_client_init(IvshmemClient *client, const char *unix_sock_path, in ivshmem_client_init() argument
151 memset(client, 0, sizeof(*client)); in ivshmem_client_init()
153 ret = snprintf(client->unix_sock_path, sizeof(client->unix_sock_path), in ivshmem_client_init()
156 if (ret < 0 || ret >= sizeof(client->unix_sock_path)) { in ivshmem_client_init()
157 IVSHMEM_CLIENT_DEBUG(client, "could not copy unix socket path\n"); in ivshmem_client_init()
162 client->local.vectors[i] = -1; in ivshmem_client_init()
165 QTAILQ_INIT(&client->peer_list); in ivshmem_client_init()
166 client->local.id = -1; in ivshmem_client_init()
168 client->notif_cb = notif_cb; in ivshmem_client_init()
169 client->notif_arg = notif_arg; in ivshmem_client_init()
170 client->verbose = verbose; in ivshmem_client_init()
171 client->shm_fd = -1; in ivshmem_client_init()
172 client->sock_fd = -1; in ivshmem_client_init()
179 ivshmem_client_connect(IvshmemClient *client) in ivshmem_client_connect() argument
185 IVSHMEM_CLIENT_DEBUG(client, "connect to client %s\n", in ivshmem_client_connect()
186 client->unix_sock_path); in ivshmem_client_connect()
188 client->sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); in ivshmem_client_connect()
189 if (client->sock_fd < 0) { in ivshmem_client_connect()
190 IVSHMEM_CLIENT_DEBUG(client, "cannot create socket: %s\n", in ivshmem_client_connect()
197 client->unix_sock_path); in ivshmem_client_connect()
199 IVSHMEM_CLIENT_DEBUG(client, "could not copy unix socket path\n"); in ivshmem_client_connect()
203 if (connect(client->sock_fd, (struct sockaddr *)&s_un, sizeof(s_un)) < 0) { in ivshmem_client_connect()
204 IVSHMEM_CLIENT_DEBUG(client, "cannot connect to %s: %s\n", s_un.sun_path, in ivshmem_client_connect()
210 if (ivshmem_client_read_one_msg(client, &tmp, &fd) < 0 || in ivshmem_client_connect()
212 IVSHMEM_CLIENT_DEBUG(client, "cannot read from server\n"); in ivshmem_client_connect()
217 if (ivshmem_client_read_one_msg(client, &client->local.id, &fd) < 0 || in ivshmem_client_connect()
218 client->local.id < 0 || fd != -1) { in ivshmem_client_connect()
219 IVSHMEM_CLIENT_DEBUG(client, "cannot read from server (2)\n"); in ivshmem_client_connect()
222 IVSHMEM_CLIENT_DEBUG(client, "our_id=%" PRId64 "\n", client->local.id); in ivshmem_client_connect()
226 if (ivshmem_client_read_one_msg(client, &tmp, &fd) < 0 || in ivshmem_client_connect()
231 IVSHMEM_CLIENT_DEBUG(client, "cannot read from server (3)\n"); in ivshmem_client_connect()
234 client->shm_fd = fd; in ivshmem_client_connect()
235 IVSHMEM_CLIENT_DEBUG(client, "shm_fd=%d\n", fd); in ivshmem_client_connect()
240 close(client->sock_fd); in ivshmem_client_connect()
241 client->sock_fd = -1; in ivshmem_client_connect()
247 ivshmem_client_close(IvshmemClient *client) in ivshmem_client_close() argument
252 IVSHMEM_CLIENT_DEBUG(client, "close client\n"); in ivshmem_client_close()
254 while ((peer = QTAILQ_FIRST(&client->peer_list)) != NULL) { in ivshmem_client_close()
255 ivshmem_client_free_peer(client, peer); in ivshmem_client_close()
258 close(client->shm_fd); in ivshmem_client_close()
259 client->shm_fd = -1; in ivshmem_client_close()
260 close(client->sock_fd); in ivshmem_client_close()
261 client->sock_fd = -1; in ivshmem_client_close()
262 client->local.id = -1; in ivshmem_client_close()
264 close(client->local.vectors[i]); in ivshmem_client_close()
265 client->local.vectors[i] = -1; in ivshmem_client_close()
267 client->local.vectors_count = 0; in ivshmem_client_close()
272 ivshmem_client_get_fds(const IvshmemClient *client, fd_set *fds, int *maxfd) in ivshmem_client_get_fds() argument
277 FD_SET(client->sock_fd, fds); in ivshmem_client_get_fds()
278 if (client->sock_fd >= *maxfd) { in ivshmem_client_get_fds()
279 *maxfd = client->sock_fd + 1; in ivshmem_client_get_fds()
282 for (vector = 0; vector < client->local.vectors_count; vector++) { in ivshmem_client_get_fds()
283 fd = client->local.vectors[vector]; in ivshmem_client_get_fds()
293 ivshmem_client_handle_event(IvshmemClient *client, const fd_set *cur, int maxfd) in ivshmem_client_handle_event() argument
300 peer = &client->local; in ivshmem_client_handle_event()
312 IVSHMEM_CLIENT_DEBUG(client, "invalid read size = %d\n", ret); in ivshmem_client_handle_event()
316 IVSHMEM_CLIENT_DEBUG(client, "received event on fd %d vector %d: %" in ivshmem_client_handle_event()
318 if (client->notif_cb != NULL) { in ivshmem_client_handle_event()
319 client->notif_cb(client, peer, i, client->notif_arg); in ivshmem_client_handle_event()
328 ivshmem_client_handle_fds(IvshmemClient *client, fd_set *fds, int maxfd) in ivshmem_client_handle_fds() argument
330 if (client->sock_fd < maxfd && FD_ISSET(client->sock_fd, fds) && in ivshmem_client_handle_fds()
331 ivshmem_client_handle_server_msg(client) < 0 && errno != EINTR) { in ivshmem_client_handle_fds()
332 IVSHMEM_CLIENT_DEBUG(client, "ivshmem_client_handle_server_msg() " in ivshmem_client_handle_fds()
335 } else if (ivshmem_client_handle_event(client, fds, maxfd) < 0 && in ivshmem_client_handle_fds()
337 IVSHMEM_CLIENT_DEBUG(client, "ivshmem_client_handle_event() failed\n"); in ivshmem_client_handle_fds()
346 ivshmem_client_notify(const IvshmemClient *client, in ivshmem_client_notify() argument
353 IVSHMEM_CLIENT_DEBUG(client, "invalid vector %u on peer %" PRId64 "\n", in ivshmem_client_notify()
358 IVSHMEM_CLIENT_DEBUG(client, "notify peer %" PRId64 in ivshmem_client_notify()
372 ivshmem_client_notify_all_vects(const IvshmemClient *client, in ivshmem_client_notify_all_vects() argument
379 if (ivshmem_client_notify(client, peer, vector) < 0) { in ivshmem_client_notify_all_vects()
389 ivshmem_client_notify_broadcast(const IvshmemClient *client) in ivshmem_client_notify_broadcast() argument
394 QTAILQ_FOREACH(peer, &client->peer_list, next) { in ivshmem_client_notify_broadcast()
395 if (ivshmem_client_notify_all_vects(client, peer) < 0) { in ivshmem_client_notify_broadcast()
405 ivshmem_client_search_peer(IvshmemClient *client, int64_t peer_id) in ivshmem_client_search_peer() argument
409 if (peer_id == client->local.id) { in ivshmem_client_search_peer()
410 return &client->local; in ivshmem_client_search_peer()
413 QTAILQ_FOREACH(peer, &client->peer_list, next) { in ivshmem_client_search_peer()
423 ivshmem_client_dump(const IvshmemClient *client) in ivshmem_client_dump() argument
429 peer = &client->local; in ivshmem_client_dump()
437 QTAILQ_FOREACH(peer, &client->peer_list, next) { in ivshmem_client_dump()