void
ioset_init(void)
{
- assert(engine == NULL);
+ if (engine) /* someone beat us to it */
+ return;
#if WITH_IOSET_KQUEUE
if (!engine && io_engine_kqueue.init())
log_module(MAIN_LOG, LOG_ERROR, "Somebody called ioset_add(%d) on a negative fd!", fd);
return 0;
}
+ if (!engine)
+ ioset_init();
res = calloc(1, sizeof(*res));
if (!res)
return 0;
res->fd = fd;
ioq_init(&res->send, 1024);
ioq_init(&res->recv, 1024);
- engine->add(res);
flags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, flags|O_NONBLOCK);
+ flags = fcntl(fd, F_GETFD);
+ fcntl(fd, F_SETFD, flags|FD_CLOEXEC);
+ engine->add(res);
return res;
}
case EHOSTUNREACH:
case ECONNREFUSED:
ioset_close(io_fd, 1);
- engine->update(io_fd);
return NULL;
}
}
return io_fd;
}
+void ioset_update(struct io_fd *fd) {
+ engine->update(fd);
+}
+
static void
ioset_try_write(struct io_fd *fd) {
int res;
- unsigned int req = ioq_get_avail(&fd->send);
+ unsigned int req;
+
+ req = ioq_get_avail(&fd->send);
res = write(fd->fd, fd->send.buf+fd->send.get, req);
if (res < 0) {
switch (errno) {
active_fd = NULL;
if (fdp->destroy_cb)
fdp->destroy_cb(fdp);
- if (fdp->send.get != fdp->send.put) {
+ if (fdp->send.get != fdp->send.put && (os_close & 2)) {
int flags;
flags = fcntl(fdp->fd, F_GETFL);
}
free(fdp->send.buf);
free(fdp->recv.buf);
- if (os_close)
+ if (os_close & 1)
close(fdp->fd);
engine->remove(fdp);
free(fdp);
if (fd->recv.put == fd->recv.size)
fd->recv.put = 0;
fdnum = fd->fd;
- while (fd->wants_reads && (fd->line_len > 0)) {
+ while (fd->line_len > 0) {
struct io_fd *old_active;
int died = 0;