/* For reading data from a socket - use it in the module task. */ int ipc_accept_buf_read(int server_fd, void *data, int data_len) { int fd, rc; if ((fd = ipc_accept(server_fd)) < 0) return -1; rc = ipc_read(fd, data, data_len); ipc_server_close(fd, rc); return rc; }
static void mgmt_event_handler(int accept_fd, int events, void *data) { int fd, err; struct mgmt_task *mtask; fd = ipc_accept(accept_fd); if (fd < 0) { eprintf("failed to accept a socket\n"); return; } err = ipc_perm(fd); if (err < 0) { eprintf("permission error\n"); goto out; } err = set_non_blocking(fd); if (err) { eprintf("failed to set a socket non-blocking\n"); goto out; } mtask = zalloc(sizeof(*mtask)); if (!mtask) { eprintf("can't allocate mtask\n"); goto out; } mtask->buf = zalloc(BUFSIZE); if (!mtask->buf) { eprintf("can't allocate mtask buffer\n"); free(mtask); goto out; } mtask->bsize = BUFSIZE; mtask->mtask_state = MTASK_STATE_HDR_RECV; err = tgt_event_add(fd, EPOLLIN, mtask_handler, mtask); if (err) { eprintf("failed to add a socket to epoll %d\n", fd); free(mtask->buf); free(mtask); goto out; } return; out: if (fd > 0) close(fd); return; }
static int io_poll_sockets(void) { fd_set rd, wr; int sel_val, ipc_listen_sock, nfound; int sockets = 0; struct timeval tv = { 2, 0 }; static time_t last_ipc_reinit = 0; /* * Try re-initializing ipc if the module isn't connected * and it was a while since we tried it. */ if (ipc.sock < 0 && last_ipc_reinit + 5 < time(NULL)) { ipc_reinit(); last_ipc_reinit = time(NULL); } ipc_listen_sock = ipc_listen_sock_desc(); sel_val = max(ipc.sock, ipc_listen_sock); FD_ZERO(&rd); FD_ZERO(&wr); if (ipc.sock >= 0) FD_SET(ipc.sock, &rd); if (ipc_listen_sock >= 0) FD_SET(ipc_listen_sock, &rd); sel_val = net_polling_helper(&rd, &wr, sel_val); if (sel_val < 0) return 0; nfound = select(sel_val + 1, &rd, &wr, NULL, &tv); if (nfound < 0) { lerr("select() returned %d (errno = %d): %s", nfound, errno, strerror(errno)); sleep(1); return -1; } if (ipc_listen_sock > 0 && FD_ISSET(ipc_listen_sock, &rd)) { linfo("Accepting inbound connection on ipc socket"); ipc_accept(); } else if (ipc.sock > 0 && FD_ISSET(ipc.sock, &rd)) { sockets++; ipc_reap_events(); } sockets += net_handle_polling_results(&rd, &wr); return 0; }
static void mgmt_event_handler(int accept_fd, int events, void *data) { int fd, err; struct mgmt_task *mtask; fd = ipc_accept(accept_fd); if (fd < 0) { eprintf("failed to accept a socket\n"); return; } err = ipc_perm(fd); if (err < 0) { eprintf("permission error\n"); goto out; } err = set_non_blocking(fd); if (err) { eprintf("failed to set a socket non-blocking\n"); goto out; } mtask = mtask_alloc(); if (!mtask) goto out; err = tgt_event_add(fd, EPOLLIN, mtask_recv_send_handler, mtask); if (err) { eprintf("failed to add a socket to epoll %d\n", fd); mtask_free(mtask); goto out; } return; out: if (fd > 0) close(fd); return; }