void poll_handle_data(poll_t *pol) { int i; for (i = 1; i <= pol->max_ ; i++) { int fd = pol->clients_[i].fd; if(fd == -1) continue; char recvbuf[1024] = {0}; if(pol->clients_[i].revents & POLLIN) { int ret = readline(fd, recvbuf, 1024); if(ret == -1) ERR_EXIT("readline"); else if(ret == 0) { printf("client close\n"); poll_del_fd(pol, i); continue; } pol->handle_callback_(fd, recvbuf); } } }
void poll_handle_data(poll_t *_poll) { int i; char recvbuf[1024]; for(i = 1; i<= _poll->maxi_; ++i) { int peerfd = _poll->clients_[i].fd; if(peerfd == -1) continue; if (_poll->clients_[i].revents & POLLIN) { int ret = readline(peerfd, recvbuf, 1024); if(ret == -1) ERR_EXIT("readline"); else if(ret == 0) { printf("client close\n"); poll_del_fd(_poll, i); continue; } _poll->handle_callback_(peerfd, recvbuf); } } }
/** ** Call this to end the coprocess. Returns the exit code of the child. **/ int child_close(CHILD *handle) { int retstat = 1, done; /** ** This lets us close the most-recently-used coprocess gracefully without ** having a handle. Useful from registered signal/atexit handlers. **/ if (handle == NULL) handle = mru_handle; if ((mru_handle = handle) == NULL) return -1; /** If there's no child running, we're done **/ if (handle->cph_pid == 0) return 0; /** ... likewise for the stderr **/ (void) child_end(handle, CP_SHOW_ERR); /** provide a high-level dbg msg */ _dbg(F,L,2, "ending child %s (pid=%d) ...", handle->cph_cmd,handle->cph_pid); /** Optionally, tell the child explicitly to give up the ghost */ if (handle->cph_quit && *(handle->cph_quit)) { _dbg(F,L,4, "sending to pid %d: %s", handle->cph_pid, handle->cph_quit); (void) fputs(handle->cph_quit, handle->cph_down); } /** Remove the back and err file descriptors from npoll **/ poll_del_fd( fileno(handle->cph_back) ); poll_del_fd( fileno(handle->cph_err) ); /** Close the input pipe to the child, which should die gracefully. **/ if (fclose(handle->cph_down) == EOF || fclose(handle->cph_back) == EOF || fclose(handle->cph_err) == EOF) return -1; /** Reap the child. **/ while ((done = waitpid( handle->cph_pid, &retstat, WNOHANG)) <= 0) if (done < 0 && errno != EINTR) return -1; _dbg(F,L,3, "ended child %s (%d) d=%d r=%d", handle->cph_cmd, handle->cph_pid, done, retstat ); if (handle != NULL) { if (handle->cph_cmd) free(handle->cph_cmd); if (handle->cph_tag) free(handle->cph_tag); if (handle->cph_eot) free(handle->cph_eot); if (handle->cph_quit) free(handle->cph_quit); free(handle); } mru_handle = NULL; return _cp_retcode(retstat); }