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
@@ -78,20 +78,23 @@ int _st_io_init(void) @@ -78,20 +78,23 @@ int _st_io_init(void)
78 sigact.sa_handler = SIG_IGN; 78 sigact.sa_handler = SIG_IGN;
79 sigemptyset(&sigact.sa_mask); 79 sigemptyset(&sigact.sa_mask);
80 sigact.sa_flags = 0; 80 sigact.sa_flags = 0;
81 - if (sigaction(SIGPIPE, &sigact, NULL) < 0) 81 + if (sigaction(SIGPIPE, &sigact, NULL) < 0) {
82 return -1; 82 return -1;
  83 + }
83 84
84 /* Set maximum number of open file descriptors */ 85 /* Set maximum number of open file descriptors */
85 - if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) 86 + if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
86 return -1; 87 return -1;
  88 + }
87 89
88 fdlim = (*_st_eventsys->fd_getlimit)(); 90 fdlim = (*_st_eventsys->fd_getlimit)();
89 if (fdlim > 0 && rlim.rlim_max > (rlim_t) fdlim) { 91 if (fdlim > 0 && rlim.rlim_max > (rlim_t) fdlim) {
90 rlim.rlim_max = fdlim; 92 rlim.rlim_max = fdlim;
91 } 93 }
92 rlim.rlim_cur = rlim.rlim_max; 94 rlim.rlim_cur = rlim.rlim_max;
93 - if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) 95 + if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) {
94 return -1; 96 return -1;
  97 + }
95 _st_osfd_limit = (int) rlim.rlim_max; 98 _st_osfd_limit = (int) rlim.rlim_max;
96 99
97 return 0; 100 return 0;
@@ -106,14 +109,17 @@ int st_getfdlimit(void) @@ -106,14 +109,17 @@ int st_getfdlimit(void)
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) 112 + if (!fd->inuse) {
110 return; 113 return;
  114 + }
111 115
112 fd->inuse = 0; 116 fd->inuse = 0;
113 - if (fd->aux_data) 117 + if (fd->aux_data) {
114 _st_netfd_free_aux_data(fd); 118 _st_netfd_free_aux_data(fd);
115 - if (fd->private_data && fd->destructor) 119 + }
  120 + if (fd->private_data && fd->destructor) {
116 (*(fd->destructor))(fd->private_data); 121 (*(fd->destructor))(fd->private_data);
  122 + }
117 fd->private_data = NULL; 123 fd->private_data = NULL;
118 fd->destructor = NULL; 124 fd->destructor = NULL;
119 fd->next = _st_netfd_freelist; 125 fd->next = _st_netfd_freelist;
@@ -126,17 +132,19 @@ static _st_netfd_t *_st_netfd_new(int osfd, int nonblock, int is_socket) @@ -126,17 +132,19 @@ static _st_netfd_t *_st_netfd_new(int osfd, int nonblock, int is_socket)
126 _st_netfd_t *fd; 132 _st_netfd_t *fd;
127 int flags = 1; 133 int flags = 1;
128 134
129 - if ((*_st_eventsys->fd_new)(osfd) < 0) 135 + if ((*_st_eventsys->fd_new)(osfd) < 0) {
130 return NULL; 136 return NULL;
  137 + }
131 138
132 if (_st_netfd_freelist) { 139 if (_st_netfd_freelist) {
133 fd = _st_netfd_freelist; 140 fd = _st_netfd_freelist;
134 _st_netfd_freelist = _st_netfd_freelist->next; 141 _st_netfd_freelist = _st_netfd_freelist->next;
135 } else { 142 } else {
136 fd = calloc(1, sizeof(_st_netfd_t)); 143 fd = calloc(1, sizeof(_st_netfd_t));
137 - if (!fd) 144 + if (!fd) {
138 return NULL; 145 return NULL;
139 } 146 }
  147 + }
140 148
141 fd->osfd = osfd; 149 fd->osfd = osfd;
142 fd->inuse = 1; 150 fd->inuse = 1;
@@ -144,11 +152,11 @@ static _st_netfd_t *_st_netfd_new(int osfd, int nonblock, int is_socket) @@ -144,11 +152,11 @@ static _st_netfd_t *_st_netfd_new(int osfd, int nonblock, int is_socket)
144 152
145 if (nonblock) { 153 if (nonblock) {
146 /* Use just one system call */ 154 /* Use just one system call */
147 - if (is_socket && ioctl(osfd, FIONBIO, &flags) != -1) 155 + if (is_socket && ioctl(osfd, FIONBIO, &flags) != -1) {
148 return fd; 156 return fd;
  157 + }
149 /* Do it the Posix way */ 158 /* Do it the Posix way */
150 - if ((flags = fcntl(osfd, F_GETFL, 0)) < 0 ||  
151 - fcntl(osfd, F_SETFL, flags | O_NONBLOCK) < 0) { 159 + if ((flags = fcntl(osfd, F_GETFL, 0)) < 0 || fcntl(osfd, F_SETFL, flags | O_NONBLOCK) < 0) {
152 st_netfd_free(fd); 160 st_netfd_free(fd);
153 return NULL; 161 return NULL;
154 } 162 }
@@ -172,8 +180,9 @@ _st_netfd_t *st_netfd_open_socket(int osfd) @@ -172,8 +180,9 @@ _st_netfd_t *st_netfd_open_socket(int osfd)
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) 183 + if ((*_st_eventsys->fd_close)(fd->osfd) < 0) {
176 return -1; 184 return -1;
  185 + }
177 186
178 st_netfd_free(fd); 187 st_netfd_free(fd);
179 return close(fd->osfd); 188 return close(fd->osfd);
@@ -186,14 +195,14 @@ int st_netfd_fileno(_st_netfd_t *fd) @@ -186,14 +195,14 @@ int st_netfd_fileno(_st_netfd_t *fd)
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) { 200 if (value != fd->private_data) {
193 /* Free up previously set non-NULL data value */ 201 /* Free up previously set non-NULL data value */
194 - if (fd->private_data && fd->destructor) 202 + if (fd->private_data && fd->destructor) {
195 (*(fd->destructor))(fd->private_data); 203 (*(fd->destructor))(fd->private_data);
196 } 204 }
  205 + }
197 fd->private_data = value; 206 fd->private_data = value;
198 fd->destructor = destructor; 207 fd->destructor = destructor;
199 } 208 }
@@ -217,8 +226,9 @@ int st_netfd_poll(_st_netfd_t *fd, int how, st_utime_t timeout) @@ -217,8 +226,9 @@ int st_netfd_poll(_st_netfd_t *fd, int how, st_utime_t timeout)
217 pd.events = (short) how; 226 pd.events = (short) how;
218 pd.revents = 0; 227 pd.revents = 0;
219 228
220 - if ((n = st_poll(&pd, 1, timeout)) < 0) 229 + if ((n = st_poll(&pd, 1, timeout)) < 0) {
221 return -1; 230 return -1;
  231 + }
222 if (n == 0) { 232 if (n == 0) {
223 /* Timed out */ 233 /* Timed out */
224 errno = ETIME; 234 errno = ETIME;
@@ -232,7 +242,6 @@ int st_netfd_poll(_st_netfd_t *fd, int how, st_utime_t timeout) @@ -232,7 +242,6 @@ int st_netfd_poll(_st_netfd_t *fd, int how, st_utime_t timeout)
232 return 0; 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)
@@ -247,21 +256,23 @@ static void _st_netfd_free_aux_data(_st_netfd_t *fd) @@ -247,21 +256,23 @@ static void _st_netfd_free_aux_data(_st_netfd_t *fd)
247 fd->aux_data = NULL; 256 fd->aux_data = NULL;
248 } 257 }
249 258
250 -_st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen,  
251 - st_utime_t timeout) 259 +_st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen, st_utime_t timeout)
252 { 260 {
253 int osfd, err; 261 int osfd, err;
254 _st_netfd_t *newfd; 262 _st_netfd_t *newfd;
255 263
256 while ((osfd = accept(fd->osfd, addr, (socklen_t *)addrlen)) < 0) { 264 while ((osfd = accept(fd->osfd, addr, (socklen_t *)addrlen)) < 0) {
257 - if (errno == EINTR) 265 + if (errno == EINTR) {
258 continue; 266 continue;
259 - if (!_IO_NOT_READY_ERROR) 267 + }
  268 + if (!_IO_NOT_READY_ERROR) {
260 return NULL; 269 return NULL;
  270 + }
261 /* Wait until the socket becomes readable */ 271 /* Wait until the socket becomes readable */
262 - if (st_netfd_poll(fd, POLLIN, timeout) < 0) 272 + if (st_netfd_poll(fd, POLLIN, timeout) < 0) {
263 return NULL; 273 return NULL;
264 } 274 }
  275 + }
265 276
266 /* On some platforms the new socket created by accept() inherits */ 277 /* On some platforms the new socket created by accept() inherits */
267 /* the nonblocking attribute of the listening socket */ 278 /* the nonblocking attribute of the listening socket */
@@ -270,7 +281,7 @@ _st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen, @@ -270,7 +281,7 @@ _st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen,
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 286
276 if (!newfd) { 287 if (!newfd) {
@@ -298,24 +309,25 @@ int st_netfd_serialize_accept(_st_netfd_t *fd) @@ -298,24 +309,25 @@ int st_netfd_serialize_accept(_st_netfd_t *fd)
298 errno = EINVAL; 309 errno = EINVAL;
299 return -1; 310 return -1;
300 } 311 }
301 - if ((p = (_st_netfd_t **)calloc(2, sizeof(_st_netfd_t *))) == NULL) 312 + if ((p = (_st_netfd_t **)calloc(2, sizeof(_st_netfd_t *))) == NULL) {
302 return -1; 313 return -1;
  314 + }
303 if (pipe(osfd) < 0) { 315 if (pipe(osfd) < 0) {
304 free(p); 316 free(p);
305 return -1; 317 return -1;
306 } 318 }
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) { 319 + if ((p[0] = st_netfd_open(osfd[0])) != NULL && (p[1] = st_netfd_open(osfd[1])) != NULL && write(osfd[1], " ", 1) == 1) {
310 fd->aux_data = p; 320 fd->aux_data = p;
311 return 0; 321 return 0;
312 } 322 }
313 /* Error */ 323 /* Error */
314 err = errno; 324 err = errno;
315 - if (p[0]) 325 + if (p[0]) {
316 st_netfd_free(p[0]); 326 st_netfd_free(p[0]);
317 - if (p[1]) 327 + }
  328 + if (p[1]) {
318 st_netfd_free(p[1]); 329 st_netfd_free(p[1]);
  330 + }
319 close(osfd[0]); 331 close(osfd[0]);
320 close(osfd[1]); 332 close(osfd[1]);
321 free(p); 333 free(p);
@@ -334,8 +346,7 @@ static void _st_netfd_free_aux_data(_st_netfd_t *fd) @@ -334,8 +346,7 @@ static void _st_netfd_free_aux_data(_st_netfd_t *fd)
334 fd->aux_data = NULL; 346 fd->aux_data = NULL;
335 } 347 }
336 348
337 -_st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen,  
338 - st_utime_t timeout) 349 +_st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen, st_utime_t timeout)
339 { 350 {
340 int osfd, err; 351 int osfd, err;
341 _st_netfd_t *newfd; 352 _st_netfd_t *newfd;
@@ -349,8 +360,9 @@ _st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen, @@ -349,8 +360,9 @@ _st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen,
349 } else { 360 } else {
350 /* Get the lock */ 361 /* Get the lock */
351 n = st_read(p[0], &c, 1, timeout); 362 n = st_read(p[0], &c, 1, timeout);
352 - if (n < 0) 363 + if (n < 0) {
353 return NULL; 364 return NULL;
  365 + }
354 ST_ASSERT(n == 1); 366 ST_ASSERT(n == 1);
355 /* Got the lock */ 367 /* Got the lock */
356 osfd = accept(fd->osfd, addr, (socklen_t *)addrlen); 368 osfd = accept(fd->osfd, addr, (socklen_t *)addrlen);
@@ -360,16 +372,20 @@ _st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen, @@ -360,16 +372,20 @@ _st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen,
360 ST_ASSERT(n == 1); 372 ST_ASSERT(n == 1);
361 errno = err; 373 errno = err;
362 } 374 }
363 - if (osfd >= 0) 375 + if (osfd >= 0) {
364 break; 376 break;
365 - if (errno == EINTR) 377 + }
  378 + if (errno == EINTR) {
366 continue; 379 continue;
367 - if (!_IO_NOT_READY_ERROR) 380 + }
  381 + if (!_IO_NOT_READY_ERROR) {
368 return NULL; 382 return NULL;
  383 + }
369 /* Wait until the socket becomes readable */ 384 /* Wait until the socket becomes readable */
370 - if (st_netfd_poll(fd, POLLIN, timeout) < 0) 385 + if (st_netfd_poll(fd, POLLIN, timeout) < 0) {
371 return NULL; 386 return NULL;
372 } 387 }
  388 + }
373 389
374 /* On some platforms the new socket created by accept() inherits */ 390 /* On some platforms the new socket created by accept() inherits */
375 /* the nonblocking attribute of the listening socket */ 391 /* the nonblocking attribute of the listening socket */
@@ -378,7 +394,7 @@ _st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen, @@ -378,7 +394,7 @@ _st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen,
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 399
384 if (!newfd) { 400 if (!newfd) {
@@ -392,8 +408,7 @@ _st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen, @@ -392,8 +408,7 @@ _st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen,
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) 411 +int st_connect(_st_netfd_t *fd, const struct sockaddr *addr, int addrlen, st_utime_t timeout)
397 { 412 {
398 int n, err = 0; 413 int n, err = 0;
399 414
@@ -407,16 +422,18 @@ int st_connect(_st_netfd_t *fd, const struct sockaddr *addr, int addrlen, @@ -407,16 +422,18 @@ int st_connect(_st_netfd_t *fd, const struct sockaddr *addr, int addrlen,
407 * "UNIX Network Programming," Vol. 1, 2nd edition, p. 413 422 * "UNIX Network Programming," Vol. 1, 2nd edition, p. 413
408 * ("Interrupted connect"). 423 * ("Interrupted connect").
409 */ 424 */
410 - if (errno != EINPROGRESS && (errno != EADDRINUSE || err == 0)) 425 + if (errno != EINPROGRESS && (errno != EADDRINUSE || err == 0)) {
411 return -1; 426 return -1;
  427 + }
412 /* Wait until the socket becomes writable */ 428 /* Wait until the socket becomes writable */
413 - if (st_netfd_poll(fd, POLLOUT, timeout) < 0) 429 + if (st_netfd_poll(fd, POLLOUT, timeout) < 0) {
414 return -1; 430 return -1;
  431 + }
415 /* Try to find out whether the connection setup succeeded or failed */ 432 /* Try to find out whether the connection setup succeeded or failed */
416 n = sizeof(int); 433 n = sizeof(int);
417 - if (getsockopt(fd->osfd, SOL_SOCKET, SO_ERROR, (char *)&err,  
418 - (socklen_t *)&n) < 0) 434 + if (getsockopt(fd->osfd, SOL_SOCKET, SO_ERROR, (char *)&err, (socklen_t *)&n) < 0) {
419 return -1; 435 return -1;
  436 + }
420 if (err) { 437 if (err) {
421 errno = err; 438 errno = err;
422 return -1; 439 return -1;
@@ -435,21 +452,23 @@ ssize_t st_read(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout) @@ -435,21 +452,23 @@ ssize_t st_read(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout)
435 ssize_t n; 452 ssize_t n;
436 453
437 while ((n = read(fd->osfd, buf, nbyte)) < 0) { 454 while ((n = read(fd->osfd, buf, nbyte)) < 0) {
438 - if (errno == EINTR) 455 + if (errno == EINTR) {
439 continue; 456 continue;
440 - if (!_IO_NOT_READY_ERROR) 457 + }
  458 + if (!_IO_NOT_READY_ERROR) {
441 return -1; 459 return -1;
  460 + }
442 /* Wait until the socket becomes readable */ 461 /* Wait until the socket becomes readable */
443 - if (st_netfd_poll(fd, POLLIN, timeout) < 0) 462 + if (st_netfd_poll(fd, POLLIN, timeout) < 0) {
444 return -1; 463 return -1;
445 } 464 }
  465 + }
446 466
447 return n; 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; 473 struct iovec iov, *riov;
455 int riov_size, rv; 474 int riov_size, rv;
@@ -464,76 +483,78 @@ int st_read_resid(_st_netfd_t *fd, void *buf, size_t *resid, @@ -464,76 +483,78 @@ int st_read_resid(_st_netfd_t *fd, void *buf, size_t *resid,
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) 486 +ssize_t st_readv(_st_netfd_t *fd, const struct iovec *iov, int iov_size, st_utime_t timeout)
469 { 487 {
470 ssize_t n; 488 ssize_t n;
471 489
472 while ((n = readv(fd->osfd, iov, iov_size)) < 0) { 490 while ((n = readv(fd->osfd, iov, iov_size)) < 0) {
473 - if (errno == EINTR) 491 + if (errno == EINTR) {
474 continue; 492 continue;
475 - if (!_IO_NOT_READY_ERROR) 493 + }
  494 + if (!_IO_NOT_READY_ERROR) {
476 return -1; 495 return -1;
  496 + }
477 /* Wait until the socket becomes readable */ 497 /* Wait until the socket becomes readable */
478 - if (st_netfd_poll(fd, POLLIN, timeout) < 0) 498 + if (st_netfd_poll(fd, POLLIN, timeout) < 0) {
479 return -1; 499 return -1;
480 } 500 }
  501 + }
481 502
482 return n; 503 return n;
483 } 504 }
484 505
485 -int st_readv_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size,  
486 - st_utime_t timeout) 506 +int st_readv_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size, st_utime_t timeout)
487 { 507 {
488 ssize_t n; 508 ssize_t n;
489 509
490 while (*iov_size > 0) { 510 while (*iov_size > 0) {
491 - if (*iov_size == 1) 511 + if (*iov_size == 1) {
492 n = read(fd->osfd, (*iov)->iov_base, (*iov)->iov_len); 512 n = read(fd->osfd, (*iov)->iov_base, (*iov)->iov_len);
493 - else 513 + } else {
494 n = readv(fd->osfd, *iov, *iov_size); 514 n = readv(fd->osfd, *iov, *iov_size);
  515 + }
495 if (n < 0) { 516 if (n < 0) {
496 - if (errno == EINTR) 517 + if (errno == EINTR) {
497 continue; 518 continue;
498 - if (!_IO_NOT_READY_ERROR) 519 + }
  520 + if (!_IO_NOT_READY_ERROR) {
499 return -1; 521 return -1;
500 - } else if (n == 0) 522 + }
  523 + } else if (n == 0) {
501 break; 524 break;
502 - else { 525 + } else {
503 while ((size_t) n >= (*iov)->iov_len) { 526 while ((size_t) n >= (*iov)->iov_len) {
504 n -= (*iov)->iov_len; 527 n -= (*iov)->iov_len;
505 (*iov)->iov_base = (char *) (*iov)->iov_base + (*iov)->iov_len; 528 (*iov)->iov_base = (char *) (*iov)->iov_base + (*iov)->iov_len;
506 (*iov)->iov_len = 0; 529 (*iov)->iov_len = 0;
507 (*iov)++; 530 (*iov)++;
508 (*iov_size)--; 531 (*iov_size)--;
509 - if (n == 0) 532 + if (n == 0) {
510 break; 533 break;
511 } 534 }
512 - if (*iov_size == 0) 535 + }
  536 + if (*iov_size == 0) {
513 break; 537 break;
  538 + }
514 (*iov)->iov_base = (char *) (*iov)->iov_base + n; 539 (*iov)->iov_base = (char *) (*iov)->iov_base + n;
515 (*iov)->iov_len -= n; 540 (*iov)->iov_len -= n;
516 } 541 }
517 /* Wait until the socket becomes readable */ 542 /* Wait until the socket becomes readable */
518 - if (st_netfd_poll(fd, POLLIN, timeout) < 0) 543 + if (st_netfd_poll(fd, POLLIN, timeout) < 0) {
519 return -1; 544 return -1;
520 } 545 }
  546 + }
521 547
522 return 0; 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; 553 size_t resid = nbyte;
530 - return st_read_resid(fd, buf, &resid, timeout) == 0 ?  
531 - (ssize_t) (nbyte - resid) : -1; 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; 559 struct iovec iov, *riov;
539 int riov_size, rv; 560 int riov_size, rv;
@@ -547,18 +568,13 @@ int st_write_resid(_st_netfd_t *fd, const void *buf, size_t *resid, @@ -547,18 +568,13 @@ int st_write_resid(_st_netfd_t *fd, const void *buf, size_t *resid,
547 return rv; 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; 573 size_t resid = nbyte;
555 - return st_write_resid(fd, buf, &resid, timeout) == 0 ?  
556 - (ssize_t) (nbyte - resid) : -1; 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; 579 ssize_t n, rv;
564 size_t nleft, nbyte; 580 size_t nleft, nbyte;
@@ -568,8 +584,9 @@ ssize_t st_writev(_st_netfd_t *fd, const struct iovec *iov, int iov_size, @@ -568,8 +584,9 @@ ssize_t st_writev(_st_netfd_t *fd, const struct iovec *iov, int iov_size,
568 584
569 /* Calculate the total number of bytes to be sent */ 585 /* Calculate the total number of bytes to be sent */
570 nbyte = 0; 586 nbyte = 0;
571 - for (index = 0; index < iov_size; index++) 587 + for (index = 0; index < iov_size; index++) {
572 nbyte += iov[index].iov_len; 588 nbyte += iov[index].iov_len;
  589 + }
573 590
574 rv = (ssize_t)nbyte; 591 rv = (ssize_t)nbyte;
575 nleft = nbyte; 592 nleft = nbyte;
@@ -578,25 +595,29 @@ ssize_t st_writev(_st_netfd_t *fd, const struct iovec *iov, int iov_size, @@ -578,25 +595,29 @@ ssize_t st_writev(_st_netfd_t *fd, const struct iovec *iov, int iov_size,
578 595
579 while (nleft > 0) { 596 while (nleft > 0) {
580 if (iov_cnt == 1) { 597 if (iov_cnt == 1) {
581 - if (st_write(fd, tmp_iov[0].iov_base, nleft, timeout) != (ssize_t) nleft) 598 + if (st_write(fd, tmp_iov[0].iov_base, nleft, timeout) != (ssize_t) nleft) {
582 rv = -1; 599 rv = -1;
  600 + }
583 break; 601 break;
584 } 602 }
585 if ((n = writev(fd->osfd, tmp_iov, iov_cnt)) < 0) { 603 if ((n = writev(fd->osfd, tmp_iov, iov_cnt)) < 0) {
586 - if (errno == EINTR) 604 + if (errno == EINTR) {
587 continue; 605 continue;
  606 + }
588 if (!_IO_NOT_READY_ERROR) { 607 if (!_IO_NOT_READY_ERROR) {
589 rv = -1; 608 rv = -1;
590 break; 609 break;
591 } 610 }
592 } else { 611 } else {
593 - if ((size_t) n == nleft) 612 + if ((size_t) n == nleft) {
594 break; 613 break;
  614 + }
595 nleft -= n; 615 nleft -= n;
596 /* Find the next unwritten vector */ 616 /* Find the next unwritten vector */
597 n = (ssize_t)(nbyte - nleft); 617 n = (ssize_t)(nbyte - nleft);
598 - for (index = 0; (size_t) n >= iov[index].iov_len; index++) 618 + for (index = 0; (size_t) n >= iov[index].iov_len; index++) {
599 n -= iov[index].iov_len; 619 n -= iov[index].iov_len;
  620 + }
600 621
601 if (tmp_iov == iov) { 622 if (tmp_iov == iov) {
602 /* Must copy iov's around */ 623 /* Must copy iov's around */
@@ -604,10 +625,11 @@ ssize_t st_writev(_st_netfd_t *fd, const struct iovec *iov, int iov_size, @@ -604,10 +625,11 @@ ssize_t st_writev(_st_netfd_t *fd, const struct iovec *iov, int iov_size,
604 tmp_iov = local_iov; 625 tmp_iov = local_iov;
605 } else { 626 } else {
606 tmp_iov = calloc(1, (iov_size - index) * sizeof(struct iovec)); 627 tmp_iov = calloc(1, (iov_size - index) * sizeof(struct iovec));
607 - if (tmp_iov == NULL) 628 + if (tmp_iov == NULL) {
608 return -1; 629 return -1;
609 } 630 }
610 } 631 }
  632 + }
611 633
612 /* Fill in the first partial read */ 634 /* Fill in the first partial read */
613 tmp_iov[0].iov_base = &(((char *)iov[index].iov_base)[n]); 635 tmp_iov[0].iov_base = &(((char *)iov[index].iov_base)[n]);
@@ -626,28 +648,30 @@ ssize_t st_writev(_st_netfd_t *fd, const struct iovec *iov, int iov_size, @@ -626,28 +648,30 @@ ssize_t st_writev(_st_netfd_t *fd, const struct iovec *iov, int iov_size,
626 } 648 }
627 } 649 }
628 650
629 - if (tmp_iov != iov && tmp_iov != local_iov) 651 + if (tmp_iov != iov && tmp_iov != local_iov) {
630 free(tmp_iov); 652 free(tmp_iov);
  653 + }
631 654
632 return rv; 655 return rv;
633 } 656 }
634 657
635 -  
636 -int st_writev_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size,  
637 - st_utime_t timeout) 658 +int st_writev_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size, st_utime_t timeout)
638 { 659 {
639 ssize_t n; 660 ssize_t n;
640 661
641 while (*iov_size > 0) { 662 while (*iov_size > 0) {
642 - if (*iov_size == 1) 663 + if (*iov_size == 1) {
643 n = write(fd->osfd, (*iov)->iov_base, (*iov)->iov_len); 664 n = write(fd->osfd, (*iov)->iov_base, (*iov)->iov_len);
644 - else 665 + } else {
645 n = writev(fd->osfd, *iov, *iov_size); 666 n = writev(fd->osfd, *iov, *iov_size);
  667 + }
646 if (n < 0) { 668 if (n < 0) {
647 - if (errno == EINTR) 669 + if (errno == EINTR) {
648 continue; 670 continue;
649 - if (!_IO_NOT_READY_ERROR) 671 + }
  672 + if (!_IO_NOT_READY_ERROR) {
650 return -1; 673 return -1;
  674 + }
651 } else { 675 } else {
652 while ((size_t) n >= (*iov)->iov_len) { 676 while ((size_t) n >= (*iov)->iov_len) {
653 n -= (*iov)->iov_len; 677 n -= (*iov)->iov_len;
@@ -655,104 +679,109 @@ int st_writev_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size, @@ -655,104 +679,109 @@ int st_writev_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size,
655 (*iov)->iov_len = 0; 679 (*iov)->iov_len = 0;
656 (*iov)++; 680 (*iov)++;
657 (*iov_size)--; 681 (*iov_size)--;
658 - if (n == 0) 682 + if (n == 0) {
659 break; 683 break;
660 } 684 }
661 - if (*iov_size == 0) 685 + }
  686 + if (*iov_size == 0) {
662 break; 687 break;
  688 + }
663 (*iov)->iov_base = (char *) (*iov)->iov_base + n; 689 (*iov)->iov_base = (char *) (*iov)->iov_base + n;
664 (*iov)->iov_len -= n; 690 (*iov)->iov_len -= n;
665 } 691 }
666 /* Wait until the socket becomes writable */ 692 /* Wait until the socket becomes writable */
667 - if (st_netfd_poll(fd, POLLOUT, timeout) < 0) 693 + if (st_netfd_poll(fd, POLLOUT, timeout) < 0) {
668 return -1; 694 return -1;
669 } 695 }
  696 + }
670 697
671 return 0; 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) 704 +int st_recvfrom(_st_netfd_t *fd, void *buf, int len, struct sockaddr *from, int *fromlen, st_utime_t timeout)
680 { 705 {
681 int n; 706 int n;
682 707
683 - while ((n = recvfrom(fd->osfd, buf, len, 0, from, (socklen_t *)fromlen))  
684 - < 0) {  
685 - if (errno == EINTR) 708 + while ((n = recvfrom(fd->osfd, buf, len, 0, from, (socklen_t *)fromlen)) < 0) {
  709 + if (errno == EINTR) {
686 continue; 710 continue;
687 - if (!_IO_NOT_READY_ERROR) 711 + }
  712 + if (!_IO_NOT_READY_ERROR) {
688 return -1; 713 return -1;
  714 + }
689 /* Wait until the socket becomes readable */ 715 /* Wait until the socket becomes readable */
690 - if (st_netfd_poll(fd, POLLIN, timeout) < 0) 716 + if (st_netfd_poll(fd, POLLIN, timeout) < 0) {
691 return -1; 717 return -1;
692 } 718 }
  719 + }
693 720
694 return n; 721 return n;
695 } 722 }
696 723
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) 724 +int st_sendto(_st_netfd_t *fd, const void *msg, int len, const struct sockaddr *to, int tolen, st_utime_t timeout)
700 { 725 {
701 int n; 726 int n;
702 727
703 while ((n = sendto(fd->osfd, msg, len, 0, to, tolen)) < 0) { 728 while ((n = sendto(fd->osfd, msg, len, 0, to, tolen)) < 0) {
704 - if (errno == EINTR) 729 + if (errno == EINTR) {
705 continue; 730 continue;
706 - if (!_IO_NOT_READY_ERROR) 731 + }
  732 + if (!_IO_NOT_READY_ERROR) {
707 return -1; 733 return -1;
  734 + }
708 /* Wait until the socket becomes writable */ 735 /* Wait until the socket becomes writable */
709 - if (st_netfd_poll(fd, POLLOUT, timeout) < 0) 736 + if (st_netfd_poll(fd, POLLOUT, timeout) < 0) {
710 return -1; 737 return -1;
711 } 738 }
  739 + }
712 740
713 return n; 741 return n;
714 } 742 }
715 743
716 744
717 -int st_recvmsg(_st_netfd_t *fd, struct msghdr *msg, int flags,  
718 - st_utime_t timeout) 745 +int st_recvmsg(_st_netfd_t *fd, struct msghdr *msg, int flags, st_utime_t timeout)
719 { 746 {
720 int n; 747 int n;
721 748
722 while ((n = recvmsg(fd->osfd, msg, flags)) < 0) { 749 while ((n = recvmsg(fd->osfd, msg, flags)) < 0) {
723 - if (errno == EINTR) 750 + if (errno == EINTR) {
724 continue; 751 continue;
725 - if (!_IO_NOT_READY_ERROR) 752 + }
  753 + if (!_IO_NOT_READY_ERROR) {
726 return -1; 754 return -1;
  755 + }
727 /* Wait until the socket becomes readable */ 756 /* Wait until the socket becomes readable */
728 - if (st_netfd_poll(fd, POLLIN, timeout) < 0) 757 + if (st_netfd_poll(fd, POLLIN, timeout) < 0) {
729 return -1; 758 return -1;
730 } 759 }
  760 + }
731 761
732 return n; 762 return n;
733 } 763 }
734 764
735 -  
736 -int st_sendmsg(_st_netfd_t *fd, const struct msghdr *msg, int flags,  
737 - st_utime_t timeout) 765 +int st_sendmsg(_st_netfd_t *fd, const struct msghdr *msg, int flags, st_utime_t timeout)
738 { 766 {
739 int n; 767 int n;
740 768
741 while ((n = sendmsg(fd->osfd, msg, flags)) < 0) { 769 while ((n = sendmsg(fd->osfd, msg, flags)) < 0) {
742 - if (errno == EINTR) 770 + if (errno == EINTR) {
743 continue; 771 continue;
744 - if (!_IO_NOT_READY_ERROR) 772 + }
  773 + if (!_IO_NOT_READY_ERROR) {
745 return -1; 774 return -1;
  775 + }
746 /* Wait until the socket becomes writable */ 776 /* Wait until the socket becomes writable */
747 - if (st_netfd_poll(fd, POLLOUT, timeout) < 0) 777 + if (st_netfd_poll(fd, POLLOUT, timeout) < 0) {
748 return -1; 778 return -1;
749 } 779 }
  780 + }
750 781
751 return n; 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 */
@@ -762,9 +791,10 @@ _st_netfd_t *st_open(const char *path, int oflags, mode_t mode) @@ -762,9 +791,10 @@ _st_netfd_t *st_open(const char *path, int oflags, mode_t mode)
762 _st_netfd_t *newfd; 791 _st_netfd_t *newfd;
763 792
764 while ((osfd = open(path, oflags | O_NONBLOCK, mode)) < 0) { 793 while ((osfd = open(path, oflags | O_NONBLOCK, mode)) < 0) {
765 - if (errno != EINTR) 794 + if (errno != EINTR) {
766 return NULL; 795 return NULL;
767 } 796 }
  797 + }
768 798
769 newfd = _st_netfd_new(osfd, 0, 0); 799 newfd = _st_netfd_new(osfd, 0, 0);
770 if (!newfd) { 800 if (!newfd) {
@@ -97,8 +97,9 @@ int st_thread_setspecific(int key, void *value) @@ -97,8 +97,9 @@ int st_thread_setspecific(int key, void *value)
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) 100 + if (key < 0 || key >= key_max) {
101 return NULL; 101 return NULL;
  102 + }
102 103
103 return ((_ST_CURRENT_THREAD())->private_data[key]); 104 return ((_ST_CURRENT_THREAD())->private_data[key]);
104 } 105 }