70a0949684d4169c8aa6fec6cb51944939ba49c5
[ashd.git] / lib / mtio.c
1 /*
2     ashd - A Sane HTTP Daemon
3     Copyright (C) 2008  Fredrik Tolf <fredrik@dolda2000.com>
4
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <sys/socket.h>
29
30 #include <log.h>
31 #include <utils.h>
32 #include <mt.h>
33 #include <mtio.h>
34
35 struct stdiofd {
36     int fd;
37     int sock;
38     int timeout;
39 };
40
41 static ssize_t mtread(void *cookie, char *buf, size_t len)
42 {
43     struct stdiofd *d = cookie;
44     int ev;
45     ssize_t ret;
46     
47     while(1) {
48         ret = read(d->fd, buf, len);
49         if((ret < 0) && (errno == EAGAIN)) {
50             ev = block(d->fd, EV_READ, d->timeout);
51             if(ev < 0) {
52                 /* If we just go on, we should get the real error. */
53                 continue;
54             } else if(ev == 0) {
55                 errno = ETIMEDOUT;
56                 return(-1);
57             } else {
58                 continue;
59             }
60         } else {
61             return(ret);
62         }
63     }
64 }
65
66 static ssize_t mtwrite(void *cookie, const char *buf, size_t len)
67 {
68     struct stdiofd *d = cookie;
69     int ev;
70     size_t off;
71     ssize_t ret;
72     
73     off = 0;
74     while(off < len) {
75         if(d->sock)
76             ret = send(d->fd, buf + off, len - off, MSG_DONTWAIT | MSG_NOSIGNAL);
77         else
78             ret = write(d->fd, buf + off, len - off);
79         if(ret < 0) {
80             if(errno == EAGAIN) {
81                 ev = block(d->fd, EV_WRITE, d->timeout);
82                 if(ev < 0) {
83                     /* If we just go on, we should get the real error. */
84                     continue;
85                 } else if(ev == 0) {
86                     errno = ETIMEDOUT;
87                     return(off);
88                 } else {
89                     continue;
90                 }
91             } else {
92                 return(off);
93             }
94         } else {
95             off += ret;
96         }
97     }
98     return(off);
99 }
100
101 static int mtclose(void *cookie)
102 {
103     struct stdiofd *d = cookie;
104     
105     close(d->fd);
106     free(d);
107     return(0);
108 }
109
110 #if defined(HAVE_GLIBC_STDIO)
111 static cookie_io_functions_t iofuns = {
112     .read = mtread,
113     .write = mtwrite,
114     .close = mtclose,
115 };
116
117 FILE *mtstdopen(int fd, int issock, int timeout, char *mode)
118 {
119     struct stdiofd *d;
120     FILE *ret;
121     
122     omalloc(d);
123     d->fd = fd;
124     d->sock = issock;
125     d->timeout = timeout;
126     ret = fopencookie(d, mode, iofuns);
127     if(!ret)
128         free(d);
129     else
130         fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
131     return(ret);
132 }
133 #elif defined(HAVE_BSD_STDIO)
134 static int bsd2mtread(void *cookie, char *buf, int len)
135 {
136     return(mtread(cookie, buf, len));
137 }
138
139 static int bsd2mtwrite(void *cookie, const char *buf, int len)
140 {
141     return(mtwrite(cookie, buf, len));
142 }
143
144 FILE *mtstdopen(int fd, int issock, int timeout, char *mode)
145 {
146     struct stdiofd *d;
147     FILE *ret;
148     int r, w;
149     
150     if(!strcmp(mode, "r")) {
151         r = 1; w = 0;
152     } else if(!strcmp(mode, "w")) {
153         r = 0; w = 1;
154     } else if(!strcmp(mode, "r+")) {
155         r = w = 1;
156     } else {
157         return(NULL);
158     }
159     omalloc(d);
160     d->fd = fd;
161     d->sock = issock;
162     d->timeout = timeout;
163     ret = funopen(d, r?bsd2mtread:NULL, w?bsd2mtwrite:NULL, NULL, mtclose);
164     if(!ret)
165         free(d);
166     else
167         fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
168     return(ret);
169 }
170 #else
171 #error "No stdio implementation for this system"
172 #endif