- now = time(NULL);
- for(bl = blockers; bl; bl = nbl) {
- nbl = bl->n;
- ev = 0;
- if(FD_ISSET(bl->fd, &rfds))
- ev |= EV_READ;
- if(FD_ISSET(bl->fd, &wfds))
- ev |= EV_WRITE;
- if(FD_ISSET(bl->fd, &efds))
- ev = -1;
- if(ev != 0)
- resume(bl->th, ev);
- else if((bl->to != 0) && (bl->to <= now))
- resume(bl->th, 0);
+ }
+}
+
+static int mtclose(void *cookie)
+{
+ struct stdiofd *d = cookie;
+
+ close(d->fd);
+ if(d->rights >= 0)
+ close(d->rights);
+ if(d->sendrights >= 0)
+ close(d->sendrights);
+ free(d);
+ return(0);
+}
+
+FILE *mtstdopen(int fd, int issock, int timeout, char *mode, struct stdiofd **infop)
+{
+ struct stdiofd *d;
+ FILE *ret;
+ int r, w;
+
+ if(!strcmp(mode, "r")) {
+ r = 1; w = 0;
+ } else if(!strcmp(mode, "w")) {
+ r = 0; w = 1;
+ } else if(!strcmp(mode, "r+")) {
+ r = w = 1;
+ } else {
+ return(NULL);
+ }
+ omalloc(d);
+ d->fd = fd;
+ d->sock = issock;
+ d->timeout = timeout;
+ d->rights = d->sendrights = -1;
+ if(!(ret = funstdio(d, r?mtread:NULL, w?mtwrite:NULL, NULL, mtclose))) {
+ free(d);
+ return(NULL);
+ }
+ fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
+ if(infop)
+ *infop = d;
+ return(ret);
+}
+
+struct bufio *mtbioopen(int fd, int issock, int timeout, char *mode, struct stdiofd **infop)
+{
+ static struct bufioops ops = {
+ .read = mtread, .write = mtwrite, .close = mtclose,
+ };
+ struct stdiofd *d;
+ struct bufio *ret;
+
+ if(!strcmp(mode, "r")) {
+ } else if(!strcmp(mode, "w")) {
+ } else if(!strcmp(mode, "r+")) {
+ } else {
+ return(NULL);
+ }
+ omalloc(d);
+ d->fd = fd;
+ d->sock = issock;
+ d->timeout = timeout;
+ d->rights = d->sendrights = -1;
+ if(!(ret = bioopen(d, &ops))) {
+ free(d);
+ return(NULL);
+ }
+ fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
+ if(infop)
+ *infop = d;
+ return(ret);
+}
+
+struct pipe {
+ struct charbuf data;
+ size_t bufmax;
+ int closed;
+ struct muth *r, *w;
+};
+
+static void freepipe(struct pipe *p)
+{
+ buffree(p->data);
+ free(p);
+}
+
+static ssize_t piperead(void *pdata, void *buf, size_t len)
+{
+ struct pipe *p = pdata;
+ ssize_t ret;
+
+ while(p->data.d == 0) {
+ if(p->closed & 2)
+ return(0);
+ if(p->r) {
+ errno = EBUSY;
+ return(-1);