assert(logs);
assert(l_file);
- int nfds;
- int r;
- struct fd_info *fdi = NULL;
int epoll_cnt = 0;
-
- int epollfd;
- struct epoll_event ev = { .events = EPOLLIN };
-
- epollfd = epoll_create1(0);
+ int epollfd = epoll_create1(0);
+ struct epoll_event *evs = NULL;
if (epollfd < 0) {
DBG("epoll_create failed: %m");
return;
}
- for (nfds = 0; nfds < fd_count; ++nfds) {
+ for (int nfds = 0; nfds < fd_count; ++nfds) {
if (!data_fds[nfds])
continue;
int fd = data_fds[nfds]->fd;
- ev.data.ptr = data_fds[nfds];
if (fd_set_flags(fd, O_NONBLOCK) < 0)
goto cleanup;
+ struct epoll_event ev = {
+ .data.ptr = data_fds[nfds],
+ .events = EPOLLIN,
+ };
epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev);
++epoll_cnt;
}
- while (epoll_cnt > 0) {
- if (!fdi && logs->dump == DUMP_CONTINUOUS)
- flush_logs(logs, l_file);
+ evs = calloc(epoll_cnt, sizeof *evs);
+ if (!evs) {
+ ERR("no memory for epoll array");
+ goto cleanup;
+ }
- nfds = epoll_wait(epollfd, &ev, 1, 100);
- if (nfds > 0)
- fdi = (struct fd_info*) ev.data.ptr;
- else {
- fdi = NULL;
- continue;
- }
+ while (epoll_cnt > 0) {
+ int nfds = epoll_wait(epollfd, evs, epoll_cnt, 100);
+ if (nfds < 0)
+ goto cleanup;
- r = fdi->ops->read(fdi);
- if (r < 0) {
- if (r == -EAGAIN)
- continue;
- else
- break;
- }
+ for (int i = 0; i < nfds; ++i) {
+ struct fd_info *fdi = evs[i].data.ptr;
+ int r = fdi->ops->read(fdi);
+ if (r < 0) {
+ if (r == -EAGAIN)
+ continue;
+ else
+ break;
+ }
- while (fdi->ops->has_log(fdi))
- if (fdi_push_log(fdi, logs, l_file))
- break;
+ while (fdi->ops->has_log(fdi))
+ if (fdi_push_log(fdi, logs, l_file))
+ break;
- if (r == 0) {
- epoll_ctl(epollfd, EPOLL_CTL_DEL, fdi->fd, NULL);
- --epoll_cnt;
+ if (r == 0) {
+ epoll_ctl(epollfd, EPOLL_CTL_DEL, fdi->fd, NULL);
+ --epoll_cnt;
+ }
}
+
+ if (logs->dump == DUMP_CONTINUOUS && !nfds)
+ flush_logs(logs, l_file);
}
if (logs->dump != DUMP_CONTINUOUS && logs->dump != DUMP_INFINITE)
flush_logs(logs, l_file);
cleanup:
+ free(evs);
close(epollfd);
}