int sys_select (int maxfd, fd_set * fds, struct timeval *tval) { fd_set fds2; int counter = 0; int found = 0; FD_ZERO (&fds2); while (1) { int i; for (i = 0; i < maxfd; i++) { if (FD_ISSET (i, fds) && pollfd (i) > 0) { found++; FD_SET (i, &fds2); } } if (found) { memcpy ((void *) fds, (void *) &fds2, sizeof (fds2)); return (found); } if (tval && tval->tv_sec < counter) return (0); sleep (1); counter++; } }
std::string NetlinkIO::read() { const unsigned int timeout = 1000; // millisec pollfd pf = pollfd(); pf.fd = sock_fd_; pf.events = POLLIN; while (true) { if (poll(&pf, 1, timeout) <= 0) { continue; } if (pf.revents & POLLIN) { return do_read(); } return ""; } tnt::Log::info(colors::green, "==> Exiting NetlinkIO::read loop <=="); }
static int evtchn_suspend(checkpoint_state* s) { int rc; rc = xc_evtchn_notify(s->xce, s->suspend_evtchn); if (rc < 0) { snprintf(errbuf, sizeof(errbuf), "failed to notify suspend event channel: %d", rc); s->errstr = errbuf; return -1; } do if (!(rc = pollfd(s, xc_evtchn_fd(s->xce)))) rc = xc_evtchn_pending(s->xce); while (rc >= 0 && rc != s->suspend_evtchn); if (rc <= 0) return -1; if (xc_evtchn_unmask(s->xce, s->suspend_evtchn) < 0) { snprintf(errbuf, sizeof(errbuf), "failed to unmask suspend notification channel: %d", rc); s->errstr = errbuf; return -1; } return 0; }
std::string GnashPluginScriptObject::readPlayer(int fd) { std::string empty; if (fd <= 0) { log_error("Invalid fd passed"); return empty; } // Wait for some data from the player int bytes = 0; pollfd pfds[1] = { pollfd() }; pfds[0].fd = fd; pfds[0].events = POLLIN; int rv = poll(pfds, 1 /* arraySize */, 2000 /* ms */); // No data yet if (rv <= 0) { return empty; } #ifndef _WIN32 rv = ioctl(fd, FIONREAD, &bytes); #else rv = ioctlSocket(fd, FIONREAD, &bytes); #endif if (rv < 0) { log_error("FIONREAD ioctl failed, unable to get network buffer length"); return empty; } log_debug("There are %d bytes in the network buffer", bytes); if (bytes <= 0) { return empty; } char buf[bytes]; int ret = ::read(fd, buf, bytes); if (ret <= 0 || ret > bytes) { return empty; } return std::string(buf, ret); }
/* returns -1 on error or death, 0 if domain is running, 1 if suspended */ static int check_shutdown(checkpoint_state* s) { unsigned int count; int xsfd; char **vec; char buf[16]; xc_dominfo_t info; xsfd = xs_fileno(s->xsh); /* loop on watch if it fires for another domain */ while (1) { if (pollfd(s, xsfd) < 0) return -1; vec = xs_read_watch(s->xsh, &count); if (s->watching_shutdown == 1) { s->watching_shutdown = 2; return 0; } if (!vec) { fprintf(stderr, "empty watch fired\n"); continue; } snprintf(buf, sizeof(buf), "%d", s->domid); if (!strcmp(vec[XS_WATCH_TOKEN], buf)) break; } if (xc_domain_getinfo(s->xch, s->domid, 1, &info) != 1 || info.domid != s->domid) { snprintf(errbuf, sizeof(errbuf), "error getting info for domain %u", s->domid); s->errstr = errbuf; return -1; } if (!info.shutdown) { snprintf(errbuf, sizeof(errbuf), "domain %u not shut down", s->domid); s->errstr = errbuf; return 0; } if (info.shutdown_reason != SHUTDOWN_suspend) return -1; return 1; }
bool NetlinkIO::try_read(std::string& data) { const unsigned int timeout = 10; // millisec pollfd pf = pollfd(); pf.fd = sock_fd_; pf.events = POLLIN; if (poll(&pf, 1, timeout) <= 0) { return false; } if (pf.revents & POLLIN) { data = do_read(); return true; } return false; }
void NetlinkProtocol::rx_loop() { try { const unsigned int timeout = 1000; // millisec pollfd pf = pollfd(); pf.fd = rx_sock_; pf.events = POLLIN; while (running_) { if (poll(&pf, 1, timeout) <= 0) { continue; } if (pf.revents & POLLIN) { parse_data(); } else if (pf.revents & POLLNVAL) { break; } else { tnt::Log::warning(colors::red, "NetlinkProtocol: rx_sock_ socket state flags: ", pf.revents); } } } catch (...) { assert(false); } }
bool MsgConnection::Open(const char* hostname, int port) { if(m_socket != -1) { return false; } char service[10]; snprintf(service, sizeof(service), "%i", port); struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; struct addrinfo* result; if(getaddrinfo(hostname, service, &hints, &result) != 0) { return false; } // loop through results struct addrinfo* info = result; int sockfd = -1; while(sockfd == -1 && info != NULL) { sockfd = socket(info->ai_family, info->ai_socktype, info->ai_protocol); // try to connect if(sockfd != -1) { break; } info = info->ai_next; } if(sockfd == -1) { freeaddrinfo(result); return false; } setsock_nonblock(sockfd); int rc = 0; if(connect(sockfd, info->ai_addr, info->ai_addrlen) == -1) { if(sockerror() == EINPROGRESS || sockerror() == SEWOULDBLOCK) { if(!pollfd(sockfd, m_timeout, false)) { freeaddrinfo(result); close(sockfd); return false; } socklen_t optlen = sizeof(int); getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (sockval_t*)&rc, &optlen); } else { rc = sockerror(); } } if(rc != 0) { freeaddrinfo(result); close(sockfd); return false; } setsock_nonblock(sockfd, false); int val = 1; setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (sockval_t*)&val, sizeof(val)); setsock_keepalive(sockfd); setsockopt(sockfd, SOL_SOCKET, SO_PRIORITY, &m_priority, sizeof(m_priority)); freeaddrinfo(result); m_hostname = hostname; m_port = port; m_connectionlost = false; m_aborting = false; m_socket = sockfd; return true; }