winlin

Merge branch 'srs.master'

@@ -54,9 +54,9 @@ @@ -54,9 +54,9 @@
54 54
55 55
56 #if EAGAIN != EWOULDBLOCK 56 #if EAGAIN != EWOULDBLOCK
57 -#define _IO_NOT_READY_ERROR ((errno == EAGAIN) || (errno == EWOULDBLOCK)) 57 + #define _IO_NOT_READY_ERROR ((errno == EAGAIN) || (errno == EWOULDBLOCK))
58 #else 58 #else
59 -#define _IO_NOT_READY_ERROR (errno == EAGAIN) 59 + #define _IO_NOT_READY_ERROR (errno == EAGAIN)
60 #endif 60 #endif
61 61
62 #define _LOCAL_MAXIOV 16 62 #define _LOCAL_MAXIOV 16
@@ -70,138 +70,147 @@ static void _st_netfd_free_aux_data(_st_netfd_t *fd); @@ -70,138 +70,147 @@ static void _st_netfd_free_aux_data(_st_netfd_t *fd);
70 70
71 int _st_io_init(void) 71 int _st_io_init(void)
72 { 72 {
73 - struct sigaction sigact;  
74 - struct rlimit rlim;  
75 - int fdlim; 73 + struct sigaction sigact;
  74 + struct rlimit rlim;
  75 + int fdlim;
76 76
77 - /* Ignore SIGPIPE */  
78 - sigact.sa_handler = SIG_IGN;  
79 - sigemptyset(&sigact.sa_mask);  
80 - sigact.sa_flags = 0;  
81 - if (sigaction(SIGPIPE, &sigact, NULL) < 0)  
82 - return -1;  
83 -  
84 - /* Set maximum number of open file descriptors */  
85 - if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)  
86 - return -1; 77 + /* Ignore SIGPIPE */
  78 + sigact.sa_handler = SIG_IGN;
  79 + sigemptyset(&sigact.sa_mask);
  80 + sigact.sa_flags = 0;
  81 + if (sigaction(SIGPIPE, &sigact, NULL) < 0) {
  82 + return -1;
  83 + }
87 84
88 - fdlim = (*_st_eventsys->fd_getlimit)();  
89 - if (fdlim > 0 && rlim.rlim_max > (rlim_t) fdlim) {  
90 - rlim.rlim_max = fdlim;  
91 - }  
92 - rlim.rlim_cur = rlim.rlim_max;  
93 - if (setrlimit(RLIMIT_NOFILE, &rlim) < 0)  
94 - return -1;  
95 - _st_osfd_limit = (int) rlim.rlim_max; 85 + /* Set maximum number of open file descriptors */
  86 + if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
  87 + return -1;
  88 + }
96 89
97 - return 0; 90 + fdlim = (*_st_eventsys->fd_getlimit)();
  91 + if (fdlim > 0 && rlim.rlim_max > (rlim_t) fdlim) {
  92 + rlim.rlim_max = fdlim;
  93 + }
  94 + rlim.rlim_cur = rlim.rlim_max;
  95 + if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) {
  96 + return -1;
  97 + }
  98 + _st_osfd_limit = (int) rlim.rlim_max;
  99 +
  100 + return 0;
98 } 101 }
99 102
100 103
101 int st_getfdlimit(void) 104 int st_getfdlimit(void)
102 { 105 {
103 - return _st_osfd_limit; 106 + return _st_osfd_limit;
104 } 107 }
105 108
106 109
107 void st_netfd_free(_st_netfd_t *fd) 110 void st_netfd_free(_st_netfd_t *fd)
108 { 111 {
109 - if (!fd->inuse)  
110 - return;  
111 -  
112 - fd->inuse = 0;  
113 - if (fd->aux_data)  
114 - _st_netfd_free_aux_data(fd);  
115 - if (fd->private_data && fd->destructor)  
116 - (*(fd->destructor))(fd->private_data);  
117 - fd->private_data = NULL;  
118 - fd->destructor = NULL;  
119 - fd->next = _st_netfd_freelist;  
120 - _st_netfd_freelist = fd; 112 + if (!fd->inuse) {
  113 + return;
  114 + }
  115 +
  116 + fd->inuse = 0;
  117 + if (fd->aux_data) {
  118 + _st_netfd_free_aux_data(fd);
  119 + }
  120 + if (fd->private_data && fd->destructor) {
  121 + (*(fd->destructor))(fd->private_data);
  122 + }
  123 + fd->private_data = NULL;
  124 + fd->destructor = NULL;
  125 + fd->next = _st_netfd_freelist;
  126 + _st_netfd_freelist = fd;
121 } 127 }
122 128
123 129
124 static _st_netfd_t *_st_netfd_new(int osfd, int nonblock, int is_socket) 130 static _st_netfd_t *_st_netfd_new(int osfd, int nonblock, int is_socket)
125 { 131 {
126 - _st_netfd_t *fd;  
127 - int flags = 1;  
128 -  
129 - if ((*_st_eventsys->fd_new)(osfd) < 0)  
130 - return NULL;  
131 -  
132 - if (_st_netfd_freelist) {  
133 - fd = _st_netfd_freelist;  
134 - _st_netfd_freelist = _st_netfd_freelist->next;  
135 - } else {  
136 - fd = calloc(1, sizeof(_st_netfd_t));  
137 - if (!fd)  
138 - return NULL;  
139 - }  
140 -  
141 - fd->osfd = osfd;  
142 - fd->inuse = 1;  
143 - fd->next = NULL;  
144 -  
145 - if (nonblock) {  
146 - /* Use just one system call */  
147 - if (is_socket && ioctl(osfd, FIONBIO, &flags) != -1)  
148 - return fd;  
149 - /* Do it the Posix way */  
150 - if ((flags = fcntl(osfd, F_GETFL, 0)) < 0 ||  
151 - fcntl(osfd, F_SETFL, flags | O_NONBLOCK) < 0) {  
152 - st_netfd_free(fd);  
153 - return NULL; 132 + _st_netfd_t *fd;
  133 + int flags = 1;
  134 +
  135 + if ((*_st_eventsys->fd_new)(osfd) < 0) {
  136 + return NULL;
154 } 137 }
155 - }  
156 -  
157 - return fd; 138 +
  139 + if (_st_netfd_freelist) {
  140 + fd = _st_netfd_freelist;
  141 + _st_netfd_freelist = _st_netfd_freelist->next;
  142 + } else {
  143 + fd = calloc(1, sizeof(_st_netfd_t));
  144 + if (!fd) {
  145 + return NULL;
  146 + }
  147 + }
  148 +
  149 + fd->osfd = osfd;
  150 + fd->inuse = 1;
  151 + fd->next = NULL;
  152 +
  153 + if (nonblock) {
  154 + /* Use just one system call */
  155 + if (is_socket && ioctl(osfd, FIONBIO, &flags) != -1) {
  156 + return fd;
  157 + }
  158 + /* Do it the Posix way */
  159 + if ((flags = fcntl(osfd, F_GETFL, 0)) < 0 || fcntl(osfd, F_SETFL, flags | O_NONBLOCK) < 0) {
  160 + st_netfd_free(fd);
  161 + return NULL;
  162 + }
  163 + }
  164 +
  165 + return fd;
158 } 166 }
159 167
160 168
161 _st_netfd_t *st_netfd_open(int osfd) 169 _st_netfd_t *st_netfd_open(int osfd)
162 { 170 {
163 - return _st_netfd_new(osfd, 1, 0); 171 + return _st_netfd_new(osfd, 1, 0);
164 } 172 }
165 173
166 174
167 _st_netfd_t *st_netfd_open_socket(int osfd) 175 _st_netfd_t *st_netfd_open_socket(int osfd)
168 { 176 {
169 - return _st_netfd_new(osfd, 1, 1); 177 + return _st_netfd_new(osfd, 1, 1);
170 } 178 }
171 179
172 180
173 int st_netfd_close(_st_netfd_t *fd) 181 int st_netfd_close(_st_netfd_t *fd)
174 { 182 {
175 - if ((*_st_eventsys->fd_close)(fd->osfd) < 0)  
176 - return -1; 183 + if ((*_st_eventsys->fd_close)(fd->osfd) < 0) {
  184 + return -1;
  185 + }
177 186
178 - st_netfd_free(fd);  
179 - return close(fd->osfd); 187 + st_netfd_free(fd);
  188 + return close(fd->osfd);
180 } 189 }
181 190
182 191
183 int st_netfd_fileno(_st_netfd_t *fd) 192 int st_netfd_fileno(_st_netfd_t *fd)
184 { 193 {
185 - return (fd->osfd); 194 + return (fd->osfd);
186 } 195 }
187 196
188 197
189 -void st_netfd_setspecific(_st_netfd_t *fd, void *value,  
190 - _st_destructor_t destructor) 198 +void st_netfd_setspecific(_st_netfd_t *fd, void *value, _st_destructor_t destructor)
191 { 199 {
192 - if (value != fd->private_data) {  
193 - /* Free up previously set non-NULL data value */  
194 - if (fd->private_data && fd->destructor)  
195 - (*(fd->destructor))(fd->private_data);  
196 - }  
197 - fd->private_data = value;  
198 - fd->destructor = destructor; 200 + if (value != fd->private_data) {
  201 + /* Free up previously set non-NULL data value */
  202 + if (fd->private_data && fd->destructor) {
  203 + (*(fd->destructor))(fd->private_data);
  204 + }
  205 + }
  206 + fd->private_data = value;
  207 + fd->destructor = destructor;
199 } 208 }
200 209
201 210
202 void *st_netfd_getspecific(_st_netfd_t *fd) 211 void *st_netfd_getspecific(_st_netfd_t *fd)
203 { 212 {
204 - return (fd->private_data); 213 + return (fd->private_data);
205 } 214 }
206 215
207 216
@@ -210,76 +219,78 @@ void *st_netfd_getspecific(_st_netfd_t *fd) @@ -210,76 +219,78 @@ void *st_netfd_getspecific(_st_netfd_t *fd)
210 */ 219 */
211 int st_netfd_poll(_st_netfd_t *fd, int how, st_utime_t timeout) 220 int st_netfd_poll(_st_netfd_t *fd, int how, st_utime_t timeout)
212 { 221 {
213 - struct pollfd pd;  
214 - int n;  
215 -  
216 - pd.fd = fd->osfd;  
217 - pd.events = (short) how;  
218 - pd.revents = 0;  
219 -  
220 - if ((n = st_poll(&pd, 1, timeout)) < 0)  
221 - return -1;  
222 - if (n == 0) {  
223 - /* Timed out */  
224 - errno = ETIME;  
225 - return -1;  
226 - }  
227 - if (pd.revents & POLLNVAL) {  
228 - errno = EBADF;  
229 - return -1;  
230 - }  
231 -  
232 - return 0; 222 + struct pollfd pd;
  223 + int n;
  224 +
  225 + pd.fd = fd->osfd;
  226 + pd.events = (short) how;
  227 + pd.revents = 0;
  228 +
  229 + if ((n = st_poll(&pd, 1, timeout)) < 0) {
  230 + return -1;
  231 + }
  232 + if (n == 0) {
  233 + /* Timed out */
  234 + errno = ETIME;
  235 + return -1;
  236 + }
  237 + if (pd.revents & POLLNVAL) {
  238 + errno = EBADF;
  239 + return -1;
  240 + }
  241 +
  242 + return 0;
233 } 243 }
234 244
235 -  
236 #ifdef MD_ALWAYS_UNSERIALIZED_ACCEPT 245 #ifdef MD_ALWAYS_UNSERIALIZED_ACCEPT
237 /* No-op */ 246 /* No-op */
238 int st_netfd_serialize_accept(_st_netfd_t *fd) 247 int st_netfd_serialize_accept(_st_netfd_t *fd)
239 { 248 {
240 - fd->aux_data = NULL;  
241 - return 0; 249 + fd->aux_data = NULL;
  250 + return 0;
242 } 251 }
243 252
244 /* No-op */ 253 /* No-op */
245 static void _st_netfd_free_aux_data(_st_netfd_t *fd) 254 static void _st_netfd_free_aux_data(_st_netfd_t *fd)
246 { 255 {
247 - fd->aux_data = NULL;  
248 -}  
249 -  
250 -_st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen,  
251 - st_utime_t timeout)  
252 -{  
253 - int osfd, err;  
254 - _st_netfd_t *newfd;  
255 -  
256 - while ((osfd = accept(fd->osfd, addr, (socklen_t *)addrlen)) < 0) {  
257 - if (errno == EINTR)  
258 - continue;  
259 - if (!_IO_NOT_READY_ERROR)  
260 - return NULL;  
261 - /* Wait until the socket becomes readable */  
262 - if (st_netfd_poll(fd, POLLIN, timeout) < 0)  
263 - return NULL;  
264 - }  
265 -  
266 - /* On some platforms the new socket created by accept() inherits */  
267 - /* the nonblocking attribute of the listening socket */ 256 + fd->aux_data = NULL;
  257 +}
  258 +
  259 +_st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen, st_utime_t timeout)
  260 +{
  261 + int osfd, err;
  262 + _st_netfd_t *newfd;
  263 +
  264 + while ((osfd = accept(fd->osfd, addr, (socklen_t *)addrlen)) < 0) {
  265 + if (errno == EINTR) {
  266 + continue;
  267 + }
  268 + if (!_IO_NOT_READY_ERROR) {
  269 + return NULL;
  270 + }
  271 + /* Wait until the socket becomes readable */
  272 + if (st_netfd_poll(fd, POLLIN, timeout) < 0) {
  273 + return NULL;
  274 + }
  275 + }
  276 +
  277 + /* On some platforms the new socket created by accept() inherits */
  278 + /* the nonblocking attribute of the listening socket */
268 #if defined (MD_ACCEPT_NB_INHERITED) 279 #if defined (MD_ACCEPT_NB_INHERITED)
269 - newfd = _st_netfd_new(osfd, 0, 1); 280 + newfd = _st_netfd_new(osfd, 0, 1);
270 #elif defined (MD_ACCEPT_NB_NOT_INHERITED) 281 #elif defined (MD_ACCEPT_NB_NOT_INHERITED)
271 - newfd = _st_netfd_new(osfd, 1, 1); 282 + newfd = _st_netfd_new(osfd, 1, 1);
272 #else 283 #else
273 -#error Unknown OS 284 + #error Unknown OS
274 #endif 285 #endif
275 -  
276 - if (!newfd) {  
277 - err = errno;  
278 - close(osfd);  
279 - errno = err;  
280 - }  
281 -  
282 - return newfd; 286 +
  287 + if (!newfd) {
  288 + err = errno;
  289 + close(osfd);
  290 + errno = err;
  291 + }
  292 +
  293 + return newfd;
283 } 294 }
284 295
285 #else /* MD_ALWAYS_UNSERIALIZED_ACCEPT */ 296 #else /* MD_ALWAYS_UNSERIALIZED_ACCEPT */
@@ -291,488 +302,507 @@ _st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen, @@ -291,488 +302,507 @@ _st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen,
291 */ 302 */
292 int st_netfd_serialize_accept(_st_netfd_t *fd) 303 int st_netfd_serialize_accept(_st_netfd_t *fd)
293 { 304 {
294 - _st_netfd_t **p;  
295 - int osfd[2], err;  
296 -  
297 - if (fd->aux_data) {  
298 - errno = EINVAL;  
299 - return -1;  
300 - }  
301 - if ((p = (_st_netfd_t **)calloc(2, sizeof(_st_netfd_t *))) == NULL)  
302 - return -1;  
303 - if (pipe(osfd) < 0) { 305 + _st_netfd_t **p;
  306 + int osfd[2], err;
  307 +
  308 + if (fd->aux_data) {
  309 + errno = EINVAL;
  310 + return -1;
  311 + }
  312 + if ((p = (_st_netfd_t **)calloc(2, sizeof(_st_netfd_t *))) == NULL) {
  313 + return -1;
  314 + }
  315 + if (pipe(osfd) < 0) {
  316 + free(p);
  317 + return -1;
  318 + }
  319 + if ((p[0] = st_netfd_open(osfd[0])) != NULL && (p[1] = st_netfd_open(osfd[1])) != NULL && write(osfd[1], " ", 1) == 1) {
  320 + fd->aux_data = p;
  321 + return 0;
  322 + }
  323 + /* Error */
  324 + err = errno;
  325 + if (p[0]) {
  326 + st_netfd_free(p[0]);
  327 + }
  328 + if (p[1]) {
  329 + st_netfd_free(p[1]);
  330 + }
  331 + close(osfd[0]);
  332 + close(osfd[1]);
304 free(p); 333 free(p);
  334 + errno = err;
  335 +
305 return -1; 336 return -1;
306 - }  
307 - if ((p[0] = st_netfd_open(osfd[0])) != NULL &&  
308 - (p[1] = st_netfd_open(osfd[1])) != NULL &&  
309 - write(osfd[1], " ", 1) == 1) {  
310 - fd->aux_data = p;  
311 - return 0;  
312 - }  
313 - /* Error */  
314 - err = errno;  
315 - if (p[0])  
316 - st_netfd_free(p[0]);  
317 - if (p[1])  
318 - st_netfd_free(p[1]);  
319 - close(osfd[0]);  
320 - close(osfd[1]);  
321 - free(p);  
322 - errno = err;  
323 -  
324 - return -1;  
325 } 337 }
326 338
327 static void _st_netfd_free_aux_data(_st_netfd_t *fd) 339 static void _st_netfd_free_aux_data(_st_netfd_t *fd)
328 { 340 {
329 - _st_netfd_t **p = (_st_netfd_t **) fd->aux_data;  
330 -  
331 - st_netfd_close(p[0]);  
332 - st_netfd_close(p[1]);  
333 - free(p);  
334 - fd->aux_data = NULL;  
335 -}  
336 -  
337 -_st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen,  
338 - st_utime_t timeout)  
339 -{  
340 - int osfd, err;  
341 - _st_netfd_t *newfd;  
342 - _st_netfd_t **p = (_st_netfd_t **) fd->aux_data;  
343 - ssize_t n;  
344 - char c;  
345 -  
346 - for ( ; ; ) {  
347 - if (p == NULL) {  
348 - osfd = accept(fd->osfd, addr, (socklen_t *)addrlen);  
349 - } else {  
350 - /* Get the lock */  
351 - n = st_read(p[0], &c, 1, timeout);  
352 - if (n < 0)  
353 - return NULL;  
354 - ST_ASSERT(n == 1);  
355 - /* Got the lock */  
356 - osfd = accept(fd->osfd, addr, (socklen_t *)addrlen);  
357 - /* Unlock */  
358 - err = errno;  
359 - n = st_write(p[1], &c, 1, timeout);  
360 - ST_ASSERT(n == 1);  
361 - errno = err;  
362 - }  
363 - if (osfd >= 0)  
364 - break;  
365 - if (errno == EINTR)  
366 - continue;  
367 - if (!_IO_NOT_READY_ERROR)  
368 - return NULL;  
369 - /* Wait until the socket becomes readable */  
370 - if (st_netfd_poll(fd, POLLIN, timeout) < 0)  
371 - return NULL;  
372 - }  
373 -  
374 - /* On some platforms the new socket created by accept() inherits */  
375 - /* the nonblocking attribute of the listening socket */ 341 + _st_netfd_t **p = (_st_netfd_t **) fd->aux_data;
  342 +
  343 + st_netfd_close(p[0]);
  344 + st_netfd_close(p[1]);
  345 + free(p);
  346 + fd->aux_data = NULL;
  347 +}
  348 +
  349 +_st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen, st_utime_t timeout)
  350 +{
  351 + int osfd, err;
  352 + _st_netfd_t *newfd;
  353 + _st_netfd_t **p = (_st_netfd_t **) fd->aux_data;
  354 + ssize_t n;
  355 + char c;
  356 +
  357 + for ( ; ; ) {
  358 + if (p == NULL) {
  359 + osfd = accept(fd->osfd, addr, (socklen_t *)addrlen);
  360 + } else {
  361 + /* Get the lock */
  362 + n = st_read(p[0], &c, 1, timeout);
  363 + if (n < 0) {
  364 + return NULL;
  365 + }
  366 + ST_ASSERT(n == 1);
  367 + /* Got the lock */
  368 + osfd = accept(fd->osfd, addr, (socklen_t *)addrlen);
  369 + /* Unlock */
  370 + err = errno;
  371 + n = st_write(p[1], &c, 1, timeout);
  372 + ST_ASSERT(n == 1);
  373 + errno = err;
  374 + }
  375 + if (osfd >= 0) {
  376 + break;
  377 + }
  378 + if (errno == EINTR) {
  379 + continue;
  380 + }
  381 + if (!_IO_NOT_READY_ERROR) {
  382 + return NULL;
  383 + }
  384 + /* Wait until the socket becomes readable */
  385 + if (st_netfd_poll(fd, POLLIN, timeout) < 0) {
  386 + return NULL;
  387 + }
  388 + }
  389 +
  390 + /* On some platforms the new socket created by accept() inherits */
  391 + /* the nonblocking attribute of the listening socket */
376 #if defined (MD_ACCEPT_NB_INHERITED) 392 #if defined (MD_ACCEPT_NB_INHERITED)
377 - newfd = _st_netfd_new(osfd, 0, 1); 393 + newfd = _st_netfd_new(osfd, 0, 1);
378 #elif defined (MD_ACCEPT_NB_NOT_INHERITED) 394 #elif defined (MD_ACCEPT_NB_NOT_INHERITED)
379 - newfd = _st_netfd_new(osfd, 1, 1); 395 + newfd = _st_netfd_new(osfd, 1, 1);
380 #else 396 #else
381 -#error Unknown OS 397 + #error Unknown OS
382 #endif 398 #endif
383 -  
384 - if (!newfd) {  
385 - err = errno;  
386 - close(osfd);  
387 - errno = err;  
388 - }  
389 -  
390 - return newfd; 399 +
  400 + if (!newfd) {
  401 + err = errno;
  402 + close(osfd);
  403 + errno = err;
  404 + }
  405 +
  406 + return newfd;
391 } 407 }
392 #endif /* MD_ALWAYS_UNSERIALIZED_ACCEPT */ 408 #endif /* MD_ALWAYS_UNSERIALIZED_ACCEPT */
393 409
394 410
395 -int st_connect(_st_netfd_t *fd, const struct sockaddr *addr, int addrlen,  
396 - st_utime_t timeout)  
397 -{  
398 - int n, err = 0;  
399 -  
400 - while (connect(fd->osfd, addr, addrlen) < 0) {  
401 - if (errno != EINTR) {  
402 - /*  
403 - * On some platforms, if connect() is interrupted (errno == EINTR)  
404 - * after the kernel binds the socket, a subsequent connect()  
405 - * attempt will fail with errno == EADDRINUSE. Ignore EADDRINUSE  
406 - * iff connect() was previously interrupted. See Rich Stevens'  
407 - * "UNIX Network Programming," Vol. 1, 2nd edition, p. 413  
408 - * ("Interrupted connect").  
409 - */  
410 - if (errno != EINPROGRESS && (errno != EADDRINUSE || err == 0))  
411 - return -1;  
412 - /* Wait until the socket becomes writable */  
413 - if (st_netfd_poll(fd, POLLOUT, timeout) < 0)  
414 - return -1;  
415 - /* Try to find out whether the connection setup succeeded or failed */  
416 - n = sizeof(int);  
417 - if (getsockopt(fd->osfd, SOL_SOCKET, SO_ERROR, (char *)&err,  
418 - (socklen_t *)&n) < 0)  
419 - return -1;  
420 - if (err) {  
421 - errno = err;  
422 - return -1;  
423 - }  
424 - break; 411 +int st_connect(_st_netfd_t *fd, const struct sockaddr *addr, int addrlen, st_utime_t timeout)
  412 +{
  413 + int n, err = 0;
  414 +
  415 + while (connect(fd->osfd, addr, addrlen) < 0) {
  416 + if (errno != EINTR) {
  417 + /*
  418 + * On some platforms, if connect() is interrupted (errno == EINTR)
  419 + * after the kernel binds the socket, a subsequent connect()
  420 + * attempt will fail with errno == EADDRINUSE. Ignore EADDRINUSE
  421 + * iff connect() was previously interrupted. See Rich Stevens'
  422 + * "UNIX Network Programming," Vol. 1, 2nd edition, p. 413
  423 + * ("Interrupted connect").
  424 + */
  425 + if (errno != EINPROGRESS && (errno != EADDRINUSE || err == 0)) {
  426 + return -1;
  427 + }
  428 + /* Wait until the socket becomes writable */
  429 + if (st_netfd_poll(fd, POLLOUT, timeout) < 0) {
  430 + return -1;
  431 + }
  432 + /* Try to find out whether the connection setup succeeded or failed */
  433 + n = sizeof(int);
  434 + if (getsockopt(fd->osfd, SOL_SOCKET, SO_ERROR, (char *)&err, (socklen_t *)&n) < 0) {
  435 + return -1;
  436 + }
  437 + if (err) {
  438 + errno = err;
  439 + return -1;
  440 + }
  441 + break;
  442 + }
  443 + err = 1;
425 } 444 }
426 - err = 1;  
427 - }  
428 -  
429 - return 0; 445 +
  446 + return 0;
430 } 447 }
431 448
432 449
433 ssize_t st_read(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout) 450 ssize_t st_read(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout)
434 { 451 {
435 - ssize_t n;  
436 -  
437 - while ((n = read(fd->osfd, buf, nbyte)) < 0) {  
438 - if (errno == EINTR)  
439 - continue;  
440 - if (!_IO_NOT_READY_ERROR)  
441 - return -1;  
442 - /* Wait until the socket becomes readable */  
443 - if (st_netfd_poll(fd, POLLIN, timeout) < 0)  
444 - return -1;  
445 - }  
446 -  
447 - return n; 452 + ssize_t n;
  453 +
  454 + while ((n = read(fd->osfd, buf, nbyte)) < 0) {
  455 + if (errno == EINTR) {
  456 + continue;
  457 + }
  458 + if (!_IO_NOT_READY_ERROR) {
  459 + return -1;
  460 + }
  461 + /* Wait until the socket becomes readable */
  462 + if (st_netfd_poll(fd, POLLIN, timeout) < 0) {
  463 + return -1;
  464 + }
  465 + }
  466 +
  467 + return n;
448 } 468 }
449 469
450 470
451 -int st_read_resid(_st_netfd_t *fd, void *buf, size_t *resid,  
452 - st_utime_t timeout) 471 +int st_read_resid(_st_netfd_t *fd, void *buf, size_t *resid, st_utime_t timeout)
453 { 472 {
454 - struct iovec iov, *riov;  
455 - int riov_size, rv;  
456 -  
457 - iov.iov_base = buf;  
458 - iov.iov_len = *resid;  
459 - riov = &iov;  
460 - riov_size = 1;  
461 - rv = st_readv_resid(fd, &riov, &riov_size, timeout);  
462 - *resid = iov.iov_len;  
463 - return rv; 473 + struct iovec iov, *riov;
  474 + int riov_size, rv;
  475 +
  476 + iov.iov_base = buf;
  477 + iov.iov_len = *resid;
  478 + riov = &iov;
  479 + riov_size = 1;
  480 + rv = st_readv_resid(fd, &riov, &riov_size, timeout);
  481 + *resid = iov.iov_len;
  482 + return rv;
464 } 483 }
465 484
466 485
467 -ssize_t st_readv(_st_netfd_t *fd, const struct iovec *iov, int iov_size,  
468 - st_utime_t timeout)  
469 -{  
470 - ssize_t n;  
471 -  
472 - while ((n = readv(fd->osfd, iov, iov_size)) < 0) {  
473 - if (errno == EINTR)  
474 - continue;  
475 - if (!_IO_NOT_READY_ERROR)  
476 - return -1;  
477 - /* Wait until the socket becomes readable */  
478 - if (st_netfd_poll(fd, POLLIN, timeout) < 0)  
479 - return -1;  
480 - }  
481 -  
482 - return n;  
483 -}  
484 -  
485 -int st_readv_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size,  
486 - st_utime_t timeout) 486 +ssize_t st_readv(_st_netfd_t *fd, const struct iovec *iov, int iov_size, st_utime_t timeout)
487 { 487 {
488 - ssize_t n;  
489 -  
490 - while (*iov_size > 0) {  
491 - if (*iov_size == 1)  
492 - n = read(fd->osfd, (*iov)->iov_base, (*iov)->iov_len);  
493 - else  
494 - n = readv(fd->osfd, *iov, *iov_size);  
495 - if (n < 0) {  
496 - if (errno == EINTR)  
497 - continue;  
498 - if (!_IO_NOT_READY_ERROR)  
499 - return -1;  
500 - } else if (n == 0)  
501 - break;  
502 - else {  
503 - while ((size_t) n >= (*iov)->iov_len) {  
504 - n -= (*iov)->iov_len;  
505 - (*iov)->iov_base = (char *) (*iov)->iov_base + (*iov)->iov_len;  
506 - (*iov)->iov_len = 0;  
507 - (*iov)++;  
508 - (*iov_size)--;  
509 - if (n == 0)  
510 - break;  
511 - }  
512 - if (*iov_size == 0)  
513 - break;  
514 - (*iov)->iov_base = (char *) (*iov)->iov_base + n;  
515 - (*iov)->iov_len -= n; 488 + ssize_t n;
  489 +
  490 + while ((n = readv(fd->osfd, iov, iov_size)) < 0) {
  491 + if (errno == EINTR) {
  492 + continue;
  493 + }
  494 + if (!_IO_NOT_READY_ERROR) {
  495 + return -1;
  496 + }
  497 + /* Wait until the socket becomes readable */
  498 + if (st_netfd_poll(fd, POLLIN, timeout) < 0) {
  499 + return -1;
  500 + }
516 } 501 }
517 - /* Wait until the socket becomes readable */  
518 - if (st_netfd_poll(fd, POLLIN, timeout) < 0)  
519 - return -1;  
520 - }  
521 -  
522 - return 0; 502 +
  503 + return n;
  504 +}
  505 +
  506 +int st_readv_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size, st_utime_t timeout)
  507 +{
  508 + ssize_t n;
  509 +
  510 + while (*iov_size > 0) {
  511 + if (*iov_size == 1) {
  512 + n = read(fd->osfd, (*iov)->iov_base, (*iov)->iov_len);
  513 + } else {
  514 + n = readv(fd->osfd, *iov, *iov_size);
  515 + }
  516 + if (n < 0) {
  517 + if (errno == EINTR) {
  518 + continue;
  519 + }
  520 + if (!_IO_NOT_READY_ERROR) {
  521 + return -1;
  522 + }
  523 + } else if (n == 0) {
  524 + break;
  525 + } else {
  526 + while ((size_t) n >= (*iov)->iov_len) {
  527 + n -= (*iov)->iov_len;
  528 + (*iov)->iov_base = (char *) (*iov)->iov_base + (*iov)->iov_len;
  529 + (*iov)->iov_len = 0;
  530 + (*iov)++;
  531 + (*iov_size)--;
  532 + if (n == 0) {
  533 + break;
  534 + }
  535 + }
  536 + if (*iov_size == 0) {
  537 + break;
  538 + }
  539 + (*iov)->iov_base = (char *) (*iov)->iov_base + n;
  540 + (*iov)->iov_len -= n;
  541 + }
  542 + /* Wait until the socket becomes readable */
  543 + if (st_netfd_poll(fd, POLLIN, timeout) < 0) {
  544 + return -1;
  545 + }
  546 + }
  547 +
  548 + return 0;
523 } 549 }
524 550
525 -  
526 -ssize_t st_read_fully(_st_netfd_t *fd, void *buf, size_t nbyte,  
527 - st_utime_t timeout) 551 +ssize_t st_read_fully(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout)
528 { 552 {
529 - size_t resid = nbyte;  
530 - return st_read_resid(fd, buf, &resid, timeout) == 0 ?  
531 - (ssize_t) (nbyte - resid) : -1; 553 + size_t resid = nbyte;
  554 + return st_read_resid(fd, buf, &resid, timeout) == 0 ? (ssize_t) (nbyte - resid) : -1;
532 } 555 }
533 556
534 -  
535 -int st_write_resid(_st_netfd_t *fd, const void *buf, size_t *resid,  
536 - st_utime_t timeout) 557 +int st_write_resid(_st_netfd_t *fd, const void *buf, size_t *resid, st_utime_t timeout)
537 { 558 {
538 - struct iovec iov, *riov;  
539 - int riov_size, rv;  
540 -  
541 - iov.iov_base = (void *) buf; /* we promise not to modify buf */  
542 - iov.iov_len = *resid;  
543 - riov = &iov;  
544 - riov_size = 1;  
545 - rv = st_writev_resid(fd, &riov, &riov_size, timeout);  
546 - *resid = iov.iov_len;  
547 - return rv; 559 + struct iovec iov, *riov;
  560 + int riov_size, rv;
  561 +
  562 + iov.iov_base = (void *) buf; /* we promise not to modify buf */
  563 + iov.iov_len = *resid;
  564 + riov = &iov;
  565 + riov_size = 1;
  566 + rv = st_writev_resid(fd, &riov, &riov_size, timeout);
  567 + *resid = iov.iov_len;
  568 + return rv;
548 } 569 }
549 570
550 -  
551 -ssize_t st_write(_st_netfd_t *fd, const void *buf, size_t nbyte,  
552 - st_utime_t timeout) 571 +ssize_t st_write(_st_netfd_t *fd, const void *buf, size_t nbyte, st_utime_t timeout)
553 { 572 {
554 - size_t resid = nbyte;  
555 - return st_write_resid(fd, buf, &resid, timeout) == 0 ?  
556 - (ssize_t) (nbyte - resid) : -1; 573 + size_t resid = nbyte;
  574 + return st_write_resid(fd, buf, &resid, timeout) == 0 ? (ssize_t) (nbyte - resid) : -1;
557 } 575 }
558 576
559 -  
560 -ssize_t st_writev(_st_netfd_t *fd, const struct iovec *iov, int iov_size,  
561 - st_utime_t timeout) 577 +ssize_t st_writev(_st_netfd_t *fd, const struct iovec *iov, int iov_size, st_utime_t timeout)
562 { 578 {
563 - ssize_t n, rv;  
564 - size_t nleft, nbyte;  
565 - int index, iov_cnt;  
566 - struct iovec *tmp_iov;  
567 - struct iovec local_iov[_LOCAL_MAXIOV];  
568 -  
569 - /* Calculate the total number of bytes to be sent */  
570 - nbyte = 0;  
571 - for (index = 0; index < iov_size; index++)  
572 - nbyte += iov[index].iov_len;  
573 -  
574 - rv = (ssize_t)nbyte;  
575 - nleft = nbyte;  
576 - tmp_iov = (struct iovec *) iov; /* we promise not to modify iov */  
577 - iov_cnt = iov_size;  
578 -  
579 - while (nleft > 0) {  
580 - if (iov_cnt == 1) {  
581 - if (st_write(fd, tmp_iov[0].iov_base, nleft, timeout) != (ssize_t) nleft)  
582 - rv = -1;  
583 - break; 579 + ssize_t n, rv;
  580 + size_t nleft, nbyte;
  581 + int index, iov_cnt;
  582 + struct iovec *tmp_iov;
  583 + struct iovec local_iov[_LOCAL_MAXIOV];
  584 +
  585 + /* Calculate the total number of bytes to be sent */
  586 + nbyte = 0;
  587 + for (index = 0; index < iov_size; index++) {
  588 + nbyte += iov[index].iov_len;
584 } 589 }
585 - if ((n = writev(fd->osfd, tmp_iov, iov_cnt)) < 0) {  
586 - if (errno == EINTR)  
587 - continue;  
588 - if (!_IO_NOT_READY_ERROR) {  
589 - rv = -1;  
590 - break;  
591 - }  
592 - } else {  
593 - if ((size_t) n == nleft)  
594 - break;  
595 - nleft -= n;  
596 - /* Find the next unwritten vector */  
597 - n = (ssize_t)(nbyte - nleft);  
598 - for (index = 0; (size_t) n >= iov[index].iov_len; index++)  
599 - n -= iov[index].iov_len;  
600 -  
601 - if (tmp_iov == iov) {  
602 - /* Must copy iov's around */  
603 - if (iov_size - index <= _LOCAL_MAXIOV) {  
604 - tmp_iov = local_iov;  
605 - } else {  
606 - tmp_iov = calloc(1, (iov_size - index) * sizeof(struct iovec));  
607 - if (tmp_iov == NULL)  
608 - return -1;  
609 - }  
610 - }  
611 -  
612 - /* Fill in the first partial read */  
613 - tmp_iov[0].iov_base = &(((char *)iov[index].iov_base)[n]);  
614 - tmp_iov[0].iov_len = iov[index].iov_len - n;  
615 - index++;  
616 - /* Copy the remaining vectors */  
617 - for (iov_cnt = 1; index < iov_size; iov_cnt++, index++) {  
618 - tmp_iov[iov_cnt].iov_base = iov[index].iov_base;  
619 - tmp_iov[iov_cnt].iov_len = iov[index].iov_len;  
620 - } 590 +
  591 + rv = (ssize_t)nbyte;
  592 + nleft = nbyte;
  593 + tmp_iov = (struct iovec *) iov; /* we promise not to modify iov */
  594 + iov_cnt = iov_size;
  595 +
  596 + while (nleft > 0) {
  597 + if (iov_cnt == 1) {
  598 + if (st_write(fd, tmp_iov[0].iov_base, nleft, timeout) != (ssize_t) nleft) {
  599 + rv = -1;
  600 + }
  601 + break;
  602 + }
  603 + if ((n = writev(fd->osfd, tmp_iov, iov_cnt)) < 0) {
  604 + if (errno == EINTR) {
  605 + continue;
  606 + }
  607 + if (!_IO_NOT_READY_ERROR) {
  608 + rv = -1;
  609 + break;
  610 + }
  611 + } else {
  612 + if ((size_t) n == nleft) {
  613 + break;
  614 + }
  615 + nleft -= n;
  616 + /* Find the next unwritten vector */
  617 + n = (ssize_t)(nbyte - nleft);
  618 + for (index = 0; (size_t) n >= iov[index].iov_len; index++) {
  619 + n -= iov[index].iov_len;
  620 + }
  621 +
  622 + if (tmp_iov == iov) {
  623 + /* Must copy iov's around */
  624 + if (iov_size - index <= _LOCAL_MAXIOV) {
  625 + tmp_iov = local_iov;
  626 + } else {
  627 + tmp_iov = calloc(1, (iov_size - index) * sizeof(struct iovec));
  628 + if (tmp_iov == NULL) {
  629 + return -1;
  630 + }
  631 + }
  632 + }
  633 +
  634 + /* Fill in the first partial read */
  635 + tmp_iov[0].iov_base = &(((char *)iov[index].iov_base)[n]);
  636 + tmp_iov[0].iov_len = iov[index].iov_len - n;
  637 + index++;
  638 + /* Copy the remaining vectors */
  639 + for (iov_cnt = 1; index < iov_size; iov_cnt++, index++) {
  640 + tmp_iov[iov_cnt].iov_base = iov[index].iov_base;
  641 + tmp_iov[iov_cnt].iov_len = iov[index].iov_len;
  642 + }
  643 + }
  644 + /* Wait until the socket becomes writable */
  645 + if (st_netfd_poll(fd, POLLOUT, timeout) < 0) {
  646 + rv = -1;
  647 + break;
  648 + }
621 } 649 }
622 - /* Wait until the socket becomes writable */  
623 - if (st_netfd_poll(fd, POLLOUT, timeout) < 0) {  
624 - rv = -1;  
625 - break; 650 +
  651 + if (tmp_iov != iov && tmp_iov != local_iov) {
  652 + free(tmp_iov);
626 } 653 }
627 - }  
628 -  
629 - if (tmp_iov != iov && tmp_iov != local_iov)  
630 - free(tmp_iov);  
631 -  
632 - return rv;  
633 -}  
634 -  
635 -  
636 -int st_writev_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size,  
637 - st_utime_t timeout)  
638 -{  
639 - ssize_t n;  
640 -  
641 - while (*iov_size > 0) {  
642 - if (*iov_size == 1)  
643 - n = write(fd->osfd, (*iov)->iov_base, (*iov)->iov_len);  
644 - else  
645 - n = writev(fd->osfd, *iov, *iov_size);  
646 - if (n < 0) {  
647 - if (errno == EINTR)  
648 - continue;  
649 - if (!_IO_NOT_READY_ERROR)  
650 - return -1;  
651 - } else {  
652 - while ((size_t) n >= (*iov)->iov_len) {  
653 - n -= (*iov)->iov_len;  
654 - (*iov)->iov_base = (char *) (*iov)->iov_base + (*iov)->iov_len;  
655 - (*iov)->iov_len = 0;  
656 - (*iov)++;  
657 - (*iov_size)--;  
658 - if (n == 0)  
659 - break;  
660 - }  
661 - if (*iov_size == 0)  
662 - break;  
663 - (*iov)->iov_base = (char *) (*iov)->iov_base + n;  
664 - (*iov)->iov_len -= n; 654 +
  655 + return rv;
  656 +}
  657 +
  658 +int st_writev_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size, st_utime_t timeout)
  659 +{
  660 + ssize_t n;
  661 +
  662 + while (*iov_size > 0) {
  663 + if (*iov_size == 1) {
  664 + n = write(fd->osfd, (*iov)->iov_base, (*iov)->iov_len);
  665 + } else {
  666 + n = writev(fd->osfd, *iov, *iov_size);
  667 + }
  668 + if (n < 0) {
  669 + if (errno == EINTR) {
  670 + continue;
  671 + }
  672 + if (!_IO_NOT_READY_ERROR) {
  673 + return -1;
  674 + }
  675 + } else {
  676 + while ((size_t) n >= (*iov)->iov_len) {
  677 + n -= (*iov)->iov_len;
  678 + (*iov)->iov_base = (char *) (*iov)->iov_base + (*iov)->iov_len;
  679 + (*iov)->iov_len = 0;
  680 + (*iov)++;
  681 + (*iov_size)--;
  682 + if (n == 0) {
  683 + break;
  684 + }
  685 + }
  686 + if (*iov_size == 0) {
  687 + break;
  688 + }
  689 + (*iov)->iov_base = (char *) (*iov)->iov_base + n;
  690 + (*iov)->iov_len -= n;
  691 + }
  692 + /* Wait until the socket becomes writable */
  693 + if (st_netfd_poll(fd, POLLOUT, timeout) < 0) {
  694 + return -1;
  695 + }
665 } 696 }
666 - /* Wait until the socket becomes writable */  
667 - if (st_netfd_poll(fd, POLLOUT, timeout) < 0)  
668 - return -1;  
669 - }  
670 -  
671 - return 0; 697 +
  698 + return 0;
672 } 699 }
673 700
674 -  
675 /* 701 /*
676 * Simple I/O functions for UDP. 702 * Simple I/O functions for UDP.
677 */ 703 */
678 -int st_recvfrom(_st_netfd_t *fd, void *buf, int len, struct sockaddr *from,  
679 - int *fromlen, st_utime_t timeout)  
680 -{  
681 - int n;  
682 -  
683 - while ((n = recvfrom(fd->osfd, buf, len, 0, from, (socklen_t *)fromlen))  
684 - < 0) {  
685 - if (errno == EINTR)  
686 - continue;  
687 - if (!_IO_NOT_READY_ERROR)  
688 - return -1;  
689 - /* Wait until the socket becomes readable */  
690 - if (st_netfd_poll(fd, POLLIN, timeout) < 0)  
691 - return -1;  
692 - }  
693 -  
694 - return n;  
695 -}  
696 -  
697 -  
698 -int st_sendto(_st_netfd_t *fd, const void *msg, int len,  
699 - const struct sockaddr *to, int tolen, st_utime_t timeout)  
700 -{  
701 - int n;  
702 -  
703 - while ((n = sendto(fd->osfd, msg, len, 0, to, tolen)) < 0) {  
704 - if (errno == EINTR)  
705 - continue;  
706 - if (!_IO_NOT_READY_ERROR)  
707 - return -1;  
708 - /* Wait until the socket becomes writable */  
709 - if (st_netfd_poll(fd, POLLOUT, timeout) < 0)  
710 - return -1;  
711 - }  
712 -  
713 - return n;  
714 -}  
715 -  
716 -  
717 -int st_recvmsg(_st_netfd_t *fd, struct msghdr *msg, int flags,  
718 - st_utime_t timeout)  
719 -{  
720 - int n;  
721 -  
722 - while ((n = recvmsg(fd->osfd, msg, flags)) < 0) {  
723 - if (errno == EINTR)  
724 - continue;  
725 - if (!_IO_NOT_READY_ERROR)  
726 - return -1;  
727 - /* Wait until the socket becomes readable */  
728 - if (st_netfd_poll(fd, POLLIN, timeout) < 0)  
729 - return -1;  
730 - }  
731 -  
732 - return n;  
733 -}  
734 -  
735 -  
736 -int st_sendmsg(_st_netfd_t *fd, const struct msghdr *msg, int flags,  
737 - st_utime_t timeout)  
738 -{  
739 - int n;  
740 -  
741 - while ((n = sendmsg(fd->osfd, msg, flags)) < 0) {  
742 - if (errno == EINTR)  
743 - continue;  
744 - if (!_IO_NOT_READY_ERROR)  
745 - return -1;  
746 - /* Wait until the socket becomes writable */  
747 - if (st_netfd_poll(fd, POLLOUT, timeout) < 0)  
748 - return -1;  
749 - }  
750 -  
751 - return n; 704 +int st_recvfrom(_st_netfd_t *fd, void *buf, int len, struct sockaddr *from, int *fromlen, st_utime_t timeout)
  705 +{
  706 + int n;
  707 +
  708 + while ((n = recvfrom(fd->osfd, buf, len, 0, from, (socklen_t *)fromlen)) < 0) {
  709 + if (errno == EINTR) {
  710 + continue;
  711 + }
  712 + if (!_IO_NOT_READY_ERROR) {
  713 + return -1;
  714 + }
  715 + /* Wait until the socket becomes readable */
  716 + if (st_netfd_poll(fd, POLLIN, timeout) < 0) {
  717 + return -1;
  718 + }
  719 + }
  720 +
  721 + return n;
  722 +}
  723 +
  724 +int st_sendto(_st_netfd_t *fd, const void *msg, int len, const struct sockaddr *to, int tolen, st_utime_t timeout)
  725 +{
  726 + int n;
  727 +
  728 + while ((n = sendto(fd->osfd, msg, len, 0, to, tolen)) < 0) {
  729 + if (errno == EINTR) {
  730 + continue;
  731 + }
  732 + if (!_IO_NOT_READY_ERROR) {
  733 + return -1;
  734 + }
  735 + /* Wait until the socket becomes writable */
  736 + if (st_netfd_poll(fd, POLLOUT, timeout) < 0) {
  737 + return -1;
  738 + }
  739 + }
  740 +
  741 + return n;
  742 +}
  743 +
  744 +
  745 +int st_recvmsg(_st_netfd_t *fd, struct msghdr *msg, int flags, st_utime_t timeout)
  746 +{
  747 + int n;
  748 +
  749 + while ((n = recvmsg(fd->osfd, msg, flags)) < 0) {
  750 + if (errno == EINTR) {
  751 + continue;
  752 + }
  753 + if (!_IO_NOT_READY_ERROR) {
  754 + return -1;
  755 + }
  756 + /* Wait until the socket becomes readable */
  757 + if (st_netfd_poll(fd, POLLIN, timeout) < 0) {
  758 + return -1;
  759 + }
  760 + }
  761 +
  762 + return n;
  763 +}
  764 +
  765 +int st_sendmsg(_st_netfd_t *fd, const struct msghdr *msg, int flags, st_utime_t timeout)
  766 +{
  767 + int n;
  768 +
  769 + while ((n = sendmsg(fd->osfd, msg, flags)) < 0) {
  770 + if (errno == EINTR) {
  771 + continue;
  772 + }
  773 + if (!_IO_NOT_READY_ERROR) {
  774 + return -1;
  775 + }
  776 + /* Wait until the socket becomes writable */
  777 + if (st_netfd_poll(fd, POLLOUT, timeout) < 0) {
  778 + return -1;
  779 + }
  780 + }
  781 +
  782 + return n;
752 } 783 }
753 784
754 -  
755 -  
756 /* 785 /*
757 * To open FIFOs or other special files. 786 * To open FIFOs or other special files.
758 */ 787 */
759 _st_netfd_t *st_open(const char *path, int oflags, mode_t mode) 788 _st_netfd_t *st_open(const char *path, int oflags, mode_t mode)
760 { 789 {
761 - int osfd, err;  
762 - _st_netfd_t *newfd;  
763 -  
764 - while ((osfd = open(path, oflags | O_NONBLOCK, mode)) < 0) {  
765 - if (errno != EINTR)  
766 - return NULL;  
767 - }  
768 -  
769 - newfd = _st_netfd_new(osfd, 0, 0);  
770 - if (!newfd) {  
771 - err = errno;  
772 - close(osfd);  
773 - errno = err;  
774 - }  
775 -  
776 - return newfd; 790 + int osfd, err;
  791 + _st_netfd_t *newfd;
  792 +
  793 + while ((osfd = open(path, oflags | O_NONBLOCK, mode)) < 0) {
  794 + if (errno != EINTR) {
  795 + return NULL;
  796 + }
  797 + }
  798 +
  799 + newfd = _st_netfd_new(osfd, 0, 0);
  800 + if (!newfd) {
  801 + err = errno;
  802 + close(osfd);
  803 + errno = err;
  804 + }
  805 +
  806 + return newfd;
777 } 807 }
778 808
@@ -56,51 +56,52 @@ static int key_max = 0; @@ -56,51 +56,52 @@ static int key_max = 0;
56 */ 56 */
57 int st_key_create(int *keyp, _st_destructor_t destructor) 57 int st_key_create(int *keyp, _st_destructor_t destructor)
58 { 58 {
59 - if (key_max >= ST_KEYS_MAX) {  
60 - errno = EAGAIN;  
61 - return -1;  
62 - }  
63 -  
64 - *keyp = key_max++;  
65 - _st_destructors[*keyp] = destructor;  
66 -  
67 - return 0; 59 + if (key_max >= ST_KEYS_MAX) {
  60 + errno = EAGAIN;
  61 + return -1;
  62 + }
  63 +
  64 + *keyp = key_max++;
  65 + _st_destructors[*keyp] = destructor;
  66 +
  67 + return 0;
68 } 68 }
69 69
70 70
71 int st_key_getlimit(void) 71 int st_key_getlimit(void)
72 { 72 {
73 - return ST_KEYS_MAX; 73 + return ST_KEYS_MAX;
74 } 74 }
75 75
76 76
77 int st_thread_setspecific(int key, void *value) 77 int st_thread_setspecific(int key, void *value)
78 { 78 {
79 - _st_thread_t *me = _ST_CURRENT_THREAD();  
80 -  
81 - if (key < 0 || key >= key_max) {  
82 - errno = EINVAL;  
83 - return -1;  
84 - }  
85 -  
86 - if (value != me->private_data[key]) {  
87 - /* free up previously set non-NULL data value */  
88 - if (me->private_data[key] && _st_destructors[key]) {  
89 - (*_st_destructors[key])(me->private_data[key]); 79 + _st_thread_t *me = _ST_CURRENT_THREAD();
  80 +
  81 + if (key < 0 || key >= key_max) {
  82 + errno = EINVAL;
  83 + return -1;
90 } 84 }
91 - me->private_data[key] = value;  
92 - }  
93 -  
94 - return 0; 85 +
  86 + if (value != me->private_data[key]) {
  87 + /* free up previously set non-NULL data value */
  88 + if (me->private_data[key] && _st_destructors[key]) {
  89 + (*_st_destructors[key])(me->private_data[key]);
  90 + }
  91 + me->private_data[key] = value;
  92 + }
  93 +
  94 + return 0;
95 } 95 }
96 96
97 97
98 void *st_thread_getspecific(int key) 98 void *st_thread_getspecific(int key)
99 { 99 {
100 - if (key < 0 || key >= key_max)  
101 - return NULL;  
102 -  
103 - return ((_ST_CURRENT_THREAD())->private_data[key]); 100 + if (key < 0 || key >= key_max) {
  101 + return NULL;
  102 + }
  103 +
  104 + return ((_ST_CURRENT_THREAD())->private_data[key]);
104 } 105 }
105 106
106 107
@@ -109,13 +110,13 @@ void *st_thread_getspecific(int key) @@ -109,13 +110,13 @@ void *st_thread_getspecific(int key)
109 */ 110 */
110 void _st_thread_cleanup(_st_thread_t *thread) 111 void _st_thread_cleanup(_st_thread_t *thread)
111 { 112 {
112 - int key;  
113 -  
114 - for (key = 0; key < key_max; key++) {  
115 - if (thread->private_data[key] && _st_destructors[key]) {  
116 - (*_st_destructors[key])(thread->private_data[key]);  
117 - thread->private_data[key] = NULL; 113 + int key;
  114 +
  115 + for (key = 0; key < key_max; key++) {
  116 + if (thread->private_data[key] && _st_destructors[key]) {
  117 + (*_st_destructors[key])(thread->private_data[key]);
  118 + thread->private_data[key] = NULL;
  119 + }
118 } 120 }
119 - }  
120 } 121 }
121 122