X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=lib%2Fmtio.c;h=957cbc21b90cc1714689629e7a0f0c081005ba19;hb=2931529e223c46eb32293bd5b104a84ca4df5ac6;hp=c084473bdc8fab7b607e7bec0e6fb7ce51ecc9eb;hpb=d8aea4cfb5e8ed4e7aa5969f75099d5693b63ef1;p=ashd.git diff --git a/lib/mtio.c b/lib/mtio.c index c084473..957cbc2 100644 --- a/lib/mtio.c +++ b/lib/mtio.c @@ -31,12 +31,7 @@ #include #include #include - -struct stdiofd { - int fd; - int sock; - int timeout; -}; +#include static ssize_t mtread(void *cookie, void *buf, size_t len) { @@ -67,35 +62,26 @@ static ssize_t mtwrite(void *cookie, const void *buf, size_t len) { struct stdiofd *d = cookie; int ev; - size_t off; ssize_t ret; - off = 0; - while(off < len) { + while(1) { if(d->sock) - ret = send(d->fd, buf + off, len - off, MSG_DONTWAIT | MSG_NOSIGNAL); + ret = send(d->fd, buf, len, MSG_DONTWAIT | MSG_NOSIGNAL); else - ret = write(d->fd, buf + off, len - off); - if(ret < 0) { - if(errno == EAGAIN) { - ev = block(d->fd, EV_WRITE, d->timeout); - if(ev < 0) { - /* If we just go on, we should get the real error. */ - continue; - } else if(ev == 0) { - errno = ETIMEDOUT; - return(off); - } else { - continue; - } - } else { - return(off); + ret = write(d->fd, buf, len); + if((ret < 0) && (errno == EAGAIN)) { + ev = block(d->fd, EV_WRITE, d->timeout); + if(ev < 0) { + /* If we just go on, we should get the real error. */ + continue; + } else if(ev == 0) { + errno = ETIMEDOUT; + return(-1); } } else { - off += ret; + return(ret); } } - return(off); } static int mtclose(void *cookie) @@ -107,7 +93,7 @@ static int mtclose(void *cookie) return(0); } -FILE *mtstdopen(int fd, int issock, int timeout, char *mode) +FILE *mtstdopen(int fd, int issock, int timeout, char *mode, struct stdiofd **infop) { struct stdiofd *d; FILE *ret; @@ -126,11 +112,41 @@ FILE *mtstdopen(int fd, int issock, int timeout, char *mode) d->fd = fd; d->sock = issock; d->timeout = timeout; - ret = funstdio(d, r?mtread:NULL, w?mtwrite:NULL, NULL, mtclose); - if(!ret) + if(!(ret = funstdio(d, r?mtread:NULL, w?mtwrite:NULL, NULL, mtclose))) { free(d); - else - fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); + 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; + 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); } @@ -188,39 +204,32 @@ static int piperclose(void *pdata) static ssize_t pipewrite(void *pdata, const void *buf, size_t len) { struct pipe *p = pdata; - size_t off, part; + ssize_t ret; if(p->closed & 1) { errno = EPIPE; return(-1); } - off = 0; - while(off < len) { - while(p->data.d >= p->bufmax) { - if(p->w) { - errno = EBUSY; - return(-1); - } - if(p->closed & 1) { - if(off == 0) { - errno = EPIPE; - return(-1); - } - return(off); - } - p->w = current; - yield(); - p->w = NULL; + while(p->data.d >= p->bufmax) { + if(p->w) { + errno = EBUSY; + return(-1); + } + p->w = current; + yield(); + p->w = NULL; + if(p->closed & 1) { + errno = EPIPE; + return(-1); } - part = min(len - off, p->bufmax - p->data.d); - sizebuf(p->data, p->data.d + part); - memcpy(p->data.b + p->data.d, buf + off, part); - off += part; - p->data.d += part; - if(p->r) - resume(p->r, 0); } - return(off); + ret = min(len, p->bufmax - p->data.d); + sizebuf(p->data, p->data.d + ret); + memcpy(p->data.b + p->data.d, buf, ret); + p->data.d += ret; + if(p->r) + resume(p->r, 0); + return(ret); } static int pipewclose(void *pdata)