2
0

io.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. * The contents of this file are subject to the Mozilla Public
  3. * License Version 1.1 (the "License"); you may not use this file
  4. * except in compliance with the License. You may obtain a copy of
  5. * the License at http://www.mozilla.org/MPL/
  6. *
  7. * Software distributed under the License is distributed on an "AS
  8. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9. * implied. See the License for the specific language governing
  10. * rights and limitations under the License.
  11. *
  12. * The Original Code is the Netscape Portable Runtime library.
  13. *
  14. * The Initial Developer of the Original Code is Netscape
  15. * Communications Corporation. Portions created by Netscape are
  16. * Copyright (C) 1994-2000 Netscape Communications Corporation. All
  17. * Rights Reserved.
  18. *
  19. * Contributor(s): Silicon Graphics, Inc.
  20. *
  21. * Portions created by SGI are Copyright (C) 2000-2001 Silicon
  22. * Graphics, Inc. All Rights Reserved.
  23. *
  24. * Alternatively, the contents of this file may be used under the
  25. * terms of the GNU General Public License Version 2 or later (the
  26. * "GPL"), in which case the provisions of the GPL are applicable
  27. * instead of those above. If you wish to allow use of your
  28. * version of this file only under the terms of the GPL and not to
  29. * allow others to use your version of this file under the MPL,
  30. * indicate your decision by deleting the provisions above and
  31. * replace them with the notice and other provisions required by
  32. * the GPL. If you do not delete the provisions above, a recipient
  33. * may use your version of this file under either the MPL or the
  34. * GPL.
  35. */
  36. /*
  37. * This file is derived directly from Netscape Communications Corporation,
  38. * and consists of extensive modifications made during the year(s) 1999-2000.
  39. */
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <sys/ioctl.h>
  45. #include <sys/uio.h>
  46. #include <sys/time.h>
  47. #include <sys/resource.h>
  48. #include <fcntl.h>
  49. #include <signal.h>
  50. #include <errno.h>
  51. #include "common.h"
  52. // Global stat.
  53. #if defined(DEBUG) && defined(DEBUG_STATS)
  54. unsigned long long _st_stat_recvfrom = 0;
  55. unsigned long long _st_stat_recvfrom_eagain = 0;
  56. unsigned long long _st_stat_sendto = 0;
  57. unsigned long long _st_stat_sendto_eagain = 0;
  58. unsigned long long _st_stat_read = 0;
  59. unsigned long long _st_stat_read_eagain = 0;
  60. unsigned long long _st_stat_readv = 0;
  61. unsigned long long _st_stat_readv_eagain = 0;
  62. unsigned long long _st_stat_writev = 0;
  63. unsigned long long _st_stat_writev_eagain = 0;
  64. unsigned long long _st_stat_recvmsg = 0;
  65. unsigned long long _st_stat_recvmsg_eagain = 0;
  66. unsigned long long _st_stat_sendmsg = 0;
  67. unsigned long long _st_stat_sendmsg_eagain = 0;
  68. #endif
  69. #if EAGAIN != EWOULDBLOCK
  70. #define _IO_NOT_READY_ERROR ((errno == EAGAIN) || (errno == EWOULDBLOCK))
  71. #else
  72. #define _IO_NOT_READY_ERROR (errno == EAGAIN)
  73. #endif
  74. #define _LOCAL_MAXIOV 16
  75. /* File descriptor object free list */
  76. static _st_netfd_t *_st_netfd_freelist = NULL;
  77. /* Maximum number of file descriptors that the process can open */
  78. static int _st_osfd_limit = -1;
  79. static void _st_netfd_free_aux_data(_st_netfd_t *fd);
  80. int _st_io_init(void)
  81. {
  82. struct sigaction sigact;
  83. struct rlimit rlim;
  84. int fdlim;
  85. /* Ignore SIGPIPE */
  86. sigact.sa_handler = SIG_IGN;
  87. sigemptyset(&sigact.sa_mask);
  88. sigact.sa_flags = 0;
  89. if (sigaction(SIGPIPE, &sigact, NULL) < 0)
  90. return -1;
  91. /* Set maximum number of open file descriptors */
  92. if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
  93. return -1;
  94. fdlim = (*_st_eventsys->fd_getlimit)();
  95. if (fdlim > 0 && rlim.rlim_max > (rlim_t) fdlim) {
  96. rlim.rlim_max = fdlim;
  97. }
  98. /**
  99. * by SRS, for osx.
  100. * when rlimit max is negative, for example, osx, use cur directly.
  101. * @see https://github.com/winlinvip/simple-rtmp-server/issues/336
  102. */
  103. if ((int)rlim.rlim_max < 0) {
  104. _st_osfd_limit = (int)(fdlim > 0? fdlim : rlim.rlim_cur);
  105. return 0;
  106. }
  107. rlim.rlim_cur = rlim.rlim_max;
  108. if (setrlimit(RLIMIT_NOFILE, &rlim) < 0)
  109. return -1;
  110. _st_osfd_limit = (int) rlim.rlim_max;
  111. return 0;
  112. }
  113. int st_getfdlimit(void)
  114. {
  115. return _st_osfd_limit;
  116. }
  117. void st_netfd_free(_st_netfd_t *fd)
  118. {
  119. if (!fd->inuse)
  120. return;
  121. fd->inuse = 0;
  122. if (fd->aux_data)
  123. _st_netfd_free_aux_data(fd);
  124. if (fd->private_data && fd->destructor)
  125. (*(fd->destructor))(fd->private_data);
  126. fd->private_data = NULL;
  127. fd->destructor = NULL;
  128. fd->next = _st_netfd_freelist;
  129. _st_netfd_freelist = fd;
  130. }
  131. static _st_netfd_t *_st_netfd_new(int osfd, int nonblock, int is_socket)
  132. {
  133. _st_netfd_t *fd;
  134. int flags = 1;
  135. if ((*_st_eventsys->fd_new)(osfd) < 0)
  136. return NULL;
  137. if (_st_netfd_freelist) {
  138. fd = _st_netfd_freelist;
  139. _st_netfd_freelist = _st_netfd_freelist->next;
  140. } else {
  141. fd = calloc(1, sizeof(_st_netfd_t));
  142. if (!fd)
  143. return NULL;
  144. }
  145. fd->osfd = osfd;
  146. fd->inuse = 1;
  147. fd->next = NULL;
  148. if (nonblock) {
  149. /* Use just one system call */
  150. if (is_socket && ioctl(osfd, FIONBIO, &flags) != -1)
  151. return fd;
  152. /* Do it the Posix way */
  153. if ((flags = fcntl(osfd, F_GETFL, 0)) < 0 ||
  154. fcntl(osfd, F_SETFL, flags | O_NONBLOCK) < 0) {
  155. st_netfd_free(fd);
  156. return NULL;
  157. }
  158. }
  159. return fd;
  160. }
  161. _st_netfd_t *st_netfd_open(int osfd)
  162. {
  163. return _st_netfd_new(osfd, 1, 0);
  164. }
  165. _st_netfd_t *st_netfd_open_socket(int osfd)
  166. {
  167. return _st_netfd_new(osfd, 1, 1);
  168. }
  169. int st_netfd_close(_st_netfd_t *fd)
  170. {
  171. if ((*_st_eventsys->fd_close)(fd->osfd) < 0)
  172. return -1;
  173. st_netfd_free(fd);
  174. return close(fd->osfd);
  175. }
  176. int st_netfd_fileno(_st_netfd_t *fd)
  177. {
  178. return (fd->osfd);
  179. }
  180. void st_netfd_setspecific(_st_netfd_t *fd, void *value, _st_destructor_t destructor)
  181. {
  182. if (value != fd->private_data) {
  183. /* Free up previously set non-NULL data value */
  184. if (fd->private_data && fd->destructor)
  185. (*(fd->destructor))(fd->private_data);
  186. }
  187. fd->private_data = value;
  188. fd->destructor = destructor;
  189. }
  190. void *st_netfd_getspecific(_st_netfd_t *fd)
  191. {
  192. return (fd->private_data);
  193. }
  194. /*
  195. * Wait for I/O on a single descriptor.
  196. */
  197. int st_netfd_poll(_st_netfd_t *fd, int how, st_utime_t timeout)
  198. {
  199. struct pollfd pd;
  200. int n;
  201. pd.fd = fd->osfd;
  202. pd.events = (short) how;
  203. pd.revents = 0;
  204. if ((n = st_poll(&pd, 1, timeout)) < 0)
  205. return -1;
  206. if (n == 0) {
  207. /* Timed out */
  208. errno = ETIME;
  209. return -1;
  210. }
  211. if (pd.revents & POLLNVAL) {
  212. errno = EBADF;
  213. return -1;
  214. }
  215. return 0;
  216. }
  217. #ifdef MD_ALWAYS_UNSERIALIZED_ACCEPT
  218. /* No-op */
  219. int st_netfd_serialize_accept(_st_netfd_t *fd)
  220. {
  221. fd->aux_data = NULL;
  222. return 0;
  223. }
  224. /* No-op */
  225. static void _st_netfd_free_aux_data(_st_netfd_t *fd)
  226. {
  227. fd->aux_data = NULL;
  228. }
  229. _st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen, st_utime_t timeout)
  230. {
  231. int osfd, err;
  232. _st_netfd_t *newfd;
  233. while ((osfd = accept(fd->osfd, addr, (socklen_t *)addrlen)) < 0) {
  234. if (errno == EINTR)
  235. continue;
  236. if (!_IO_NOT_READY_ERROR)
  237. return NULL;
  238. /* Wait until the socket becomes readable */
  239. if (st_netfd_poll(fd, POLLIN, timeout) < 0)
  240. return NULL;
  241. }
  242. /* On some platforms the new socket created by accept() inherits */
  243. /* the nonblocking attribute of the listening socket */
  244. #if defined (MD_ACCEPT_NB_INHERITED)
  245. newfd = _st_netfd_new(osfd, 0, 1);
  246. #elif defined (MD_ACCEPT_NB_NOT_INHERITED)
  247. newfd = _st_netfd_new(osfd, 1, 1);
  248. #else
  249. #error Unknown OS
  250. #endif
  251. if (!newfd) {
  252. err = errno;
  253. close(osfd);
  254. errno = err;
  255. }
  256. return newfd;
  257. }
  258. #else /* MD_ALWAYS_UNSERIALIZED_ACCEPT */
  259. /*
  260. * On some platforms accept() calls from different processes
  261. * on the same listen socket must be serialized.
  262. * The following code serializes accept()'s without process blocking.
  263. * A pipe is used as an inter-process semaphore.
  264. */
  265. int st_netfd_serialize_accept(_st_netfd_t *fd)
  266. {
  267. _st_netfd_t **p;
  268. int osfd[2], err;
  269. if (fd->aux_data) {
  270. errno = EINVAL;
  271. return -1;
  272. }
  273. if ((p = (_st_netfd_t **)calloc(2, sizeof(_st_netfd_t *))) == NULL)
  274. return -1;
  275. if (pipe(osfd) < 0) {
  276. free(p);
  277. return -1;
  278. }
  279. if ((p[0] = st_netfd_open(osfd[0])) != NULL && (p[1] = st_netfd_open(osfd[1])) != NULL && write(osfd[1], " ", 1) == 1) {
  280. fd->aux_data = p;
  281. return 0;
  282. }
  283. /* Error */
  284. err = errno;
  285. if (p[0])
  286. st_netfd_free(p[0]);
  287. if (p[1])
  288. st_netfd_free(p[1]);
  289. close(osfd[0]);
  290. close(osfd[1]);
  291. free(p);
  292. errno = err;
  293. return -1;
  294. }
  295. static void _st_netfd_free_aux_data(_st_netfd_t *fd)
  296. {
  297. _st_netfd_t **p = (_st_netfd_t **) fd->aux_data;
  298. st_netfd_close(p[0]);
  299. st_netfd_close(p[1]);
  300. free(p);
  301. fd->aux_data = NULL;
  302. }
  303. _st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen, st_utime_t timeout)
  304. {
  305. int osfd, err;
  306. _st_netfd_t *newfd;
  307. _st_netfd_t **p = (_st_netfd_t **) fd->aux_data;
  308. ssize_t n;
  309. char c;
  310. for ( ; ; ) {
  311. if (p == NULL) {
  312. osfd = accept(fd->osfd, addr, (socklen_t *)addrlen);
  313. } else {
  314. /* Get the lock */
  315. n = st_read(p[0], &c, 1, timeout);
  316. if (n < 0)
  317. return NULL;
  318. ST_ASSERT(n == 1);
  319. /* Got the lock */
  320. osfd = accept(fd->osfd, addr, (socklen_t *)addrlen);
  321. /* Unlock */
  322. err = errno;
  323. n = st_write(p[1], &c, 1, timeout);
  324. ST_ASSERT(n == 1);
  325. errno = err;
  326. }
  327. if (osfd >= 0)
  328. break;
  329. if (errno == EINTR)
  330. continue;
  331. if (!_IO_NOT_READY_ERROR)
  332. return NULL;
  333. /* Wait until the socket becomes readable */
  334. if (st_netfd_poll(fd, POLLIN, timeout) < 0)
  335. return NULL;
  336. }
  337. /* On some platforms the new socket created by accept() inherits */
  338. /* the nonblocking attribute of the listening socket */
  339. #if defined (MD_ACCEPT_NB_INHERITED)
  340. newfd = _st_netfd_new(osfd, 0, 1);
  341. #elif defined (MD_ACCEPT_NB_NOT_INHERITED)
  342. newfd = _st_netfd_new(osfd, 1, 1);
  343. #else
  344. #error Unknown OS
  345. #endif
  346. if (!newfd) {
  347. err = errno;
  348. close(osfd);
  349. errno = err;
  350. }
  351. return newfd;
  352. }
  353. #endif /* MD_ALWAYS_UNSERIALIZED_ACCEPT */
  354. int st_connect(_st_netfd_t *fd, const struct sockaddr *addr, int addrlen, st_utime_t timeout)
  355. {
  356. int n, err = 0;
  357. while (connect(fd->osfd, addr, addrlen) < 0) {
  358. if (errno != EINTR) {
  359. /*
  360. * On some platforms, if connect() is interrupted (errno == EINTR)
  361. * after the kernel binds the socket, a subsequent connect()
  362. * attempt will fail with errno == EADDRINUSE. Ignore EADDRINUSE
  363. * iff connect() was previously interrupted. See Rich Stevens'
  364. * "UNIX Network Programming," Vol. 1, 2nd edition, p. 413
  365. * ("Interrupted connect").
  366. */
  367. if (errno != EINPROGRESS && (errno != EADDRINUSE || err == 0))
  368. return -1;
  369. /* Wait until the socket becomes writable */
  370. if (st_netfd_poll(fd, POLLOUT, timeout) < 0)
  371. return -1;
  372. /* Try to find out whether the connection setup succeeded or failed */
  373. n = sizeof(int);
  374. if (getsockopt(fd->osfd, SOL_SOCKET, SO_ERROR, (char *)&err, (socklen_t *)&n) < 0)
  375. return -1;
  376. if (err) {
  377. errno = err;
  378. return -1;
  379. }
  380. break;
  381. }
  382. err = 1;
  383. }
  384. return 0;
  385. }
  386. ssize_t st_read(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout)
  387. {
  388. ssize_t n;
  389. #if defined(DEBUG) && defined(DEBUG_STATS)
  390. ++_st_stat_read;
  391. #endif
  392. while ((n = read(fd->osfd, buf, nbyte)) < 0) {
  393. if (errno == EINTR)
  394. continue;
  395. if (!_IO_NOT_READY_ERROR)
  396. return -1;
  397. #if defined(DEBUG) && defined(DEBUG_STATS)
  398. ++_st_stat_read_eagain;
  399. #endif
  400. /* Wait until the socket becomes readable */
  401. if (st_netfd_poll(fd, POLLIN, timeout) < 0)
  402. return -1;
  403. }
  404. return n;
  405. }
  406. int st_read_resid(_st_netfd_t *fd, void *buf, size_t *resid, st_utime_t timeout)
  407. {
  408. struct iovec iov, *riov;
  409. int riov_size, rv;
  410. iov.iov_base = buf;
  411. iov.iov_len = *resid;
  412. riov = &iov;
  413. riov_size = 1;
  414. rv = st_readv_resid(fd, &riov, &riov_size, timeout);
  415. *resid = iov.iov_len;
  416. return rv;
  417. }
  418. ssize_t st_readv(_st_netfd_t *fd, const struct iovec *iov, int iov_size, st_utime_t timeout)
  419. {
  420. ssize_t n;
  421. #if defined(DEBUG) && defined(DEBUG_STATS)
  422. ++_st_stat_readv;
  423. #endif
  424. while ((n = readv(fd->osfd, iov, iov_size)) < 0) {
  425. if (errno == EINTR)
  426. continue;
  427. if (!_IO_NOT_READY_ERROR)
  428. return -1;
  429. #if defined(DEBUG) && defined(DEBUG_STATS)
  430. ++_st_stat_readv_eagain;
  431. #endif
  432. /* Wait until the socket becomes readable */
  433. if (st_netfd_poll(fd, POLLIN, timeout) < 0)
  434. return -1;
  435. }
  436. return n;
  437. }
  438. int st_readv_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size, st_utime_t timeout)
  439. {
  440. ssize_t n;
  441. while (*iov_size > 0) {
  442. if (*iov_size == 1)
  443. n = read(fd->osfd, (*iov)->iov_base, (*iov)->iov_len);
  444. else
  445. n = readv(fd->osfd, *iov, *iov_size);
  446. if (n < 0) {
  447. if (errno == EINTR)
  448. continue;
  449. if (!_IO_NOT_READY_ERROR)
  450. return -1;
  451. } else if (n == 0)
  452. break;
  453. else {
  454. while ((size_t) n >= (*iov)->iov_len) {
  455. n -= (*iov)->iov_len;
  456. (*iov)->iov_base = (char *) (*iov)->iov_base + (*iov)->iov_len;
  457. (*iov)->iov_len = 0;
  458. (*iov)++;
  459. (*iov_size)--;
  460. if (n == 0)
  461. break;
  462. }
  463. if (*iov_size == 0)
  464. break;
  465. (*iov)->iov_base = (char *) (*iov)->iov_base + n;
  466. (*iov)->iov_len -= n;
  467. }
  468. /* Wait until the socket becomes readable */
  469. if (st_netfd_poll(fd, POLLIN, timeout) < 0)
  470. return -1;
  471. }
  472. return 0;
  473. }
  474. ssize_t st_read_fully(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout)
  475. {
  476. size_t resid = nbyte;
  477. return st_read_resid(fd, buf, &resid, timeout) == 0 ?
  478. (ssize_t) (nbyte - resid) : -1;
  479. }
  480. int st_write_resid(_st_netfd_t *fd, const void *buf, size_t *resid, st_utime_t timeout)
  481. {
  482. struct iovec iov, *riov;
  483. int riov_size, rv;
  484. iov.iov_base = (void *) buf; /* we promise not to modify buf */
  485. iov.iov_len = *resid;
  486. riov = &iov;
  487. riov_size = 1;
  488. rv = st_writev_resid(fd, &riov, &riov_size, timeout);
  489. *resid = iov.iov_len;
  490. return rv;
  491. }
  492. ssize_t st_write(_st_netfd_t *fd, const void *buf, size_t nbyte, st_utime_t timeout)
  493. {
  494. size_t resid = nbyte;
  495. return st_write_resid(fd, buf, &resid, timeout) == 0 ?
  496. (ssize_t) (nbyte - resid) : -1;
  497. }
  498. ssize_t st_writev(_st_netfd_t *fd, const struct iovec *iov, int iov_size, st_utime_t timeout)
  499. {
  500. ssize_t n, rv;
  501. size_t nleft, nbyte;
  502. int index, iov_cnt;
  503. struct iovec *tmp_iov;
  504. struct iovec local_iov[_LOCAL_MAXIOV];
  505. /* Calculate the total number of bytes to be sent */
  506. nbyte = 0;
  507. for (index = 0; index < iov_size; index++)
  508. nbyte += iov[index].iov_len;
  509. rv = (ssize_t)nbyte;
  510. nleft = nbyte;
  511. tmp_iov = (struct iovec *) iov; /* we promise not to modify iov */
  512. iov_cnt = iov_size;
  513. #if defined(DEBUG) && defined(DEBUG_STATS)
  514. ++_st_stat_writev;
  515. #endif
  516. while (nleft > 0) {
  517. if (iov_cnt == 1) {
  518. if (st_write(fd, tmp_iov[0].iov_base, nleft, timeout) != (ssize_t) nleft)
  519. rv = -1;
  520. break;
  521. }
  522. if ((n = writev(fd->osfd, tmp_iov, iov_cnt)) < 0) {
  523. if (errno == EINTR)
  524. continue;
  525. if (!_IO_NOT_READY_ERROR) {
  526. rv = -1;
  527. break;
  528. }
  529. } else {
  530. if ((size_t) n == nleft)
  531. break;
  532. nleft -= n;
  533. /* Find the next unwritten vector */
  534. n = (ssize_t)(nbyte - nleft);
  535. for (index = 0; (size_t) n >= iov[index].iov_len; index++)
  536. n -= iov[index].iov_len;
  537. if (tmp_iov == iov) {
  538. /* Must copy iov's around */
  539. if (iov_size - index <= _LOCAL_MAXIOV) {
  540. tmp_iov = local_iov;
  541. } else {
  542. tmp_iov = calloc(1, (iov_size - index) * sizeof(struct iovec));
  543. if (tmp_iov == NULL)
  544. return -1;
  545. }
  546. }
  547. /* Fill in the first partial read */
  548. tmp_iov[0].iov_base = &(((char *)iov[index].iov_base)[n]);
  549. tmp_iov[0].iov_len = iov[index].iov_len - n;
  550. index++;
  551. /* Copy the remaining vectors */
  552. for (iov_cnt = 1; index < iov_size; iov_cnt++, index++) {
  553. tmp_iov[iov_cnt].iov_base = iov[index].iov_base;
  554. tmp_iov[iov_cnt].iov_len = iov[index].iov_len;
  555. }
  556. }
  557. #if defined(DEBUG) && defined(DEBUG_STATS)
  558. ++_st_stat_writev_eagain;
  559. #endif
  560. /* Wait until the socket becomes writable */
  561. if (st_netfd_poll(fd, POLLOUT, timeout) < 0) {
  562. rv = -1;
  563. break;
  564. }
  565. }
  566. if (tmp_iov != iov && tmp_iov != local_iov)
  567. free(tmp_iov);
  568. return rv;
  569. }
  570. int st_writev_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size, st_utime_t timeout)
  571. {
  572. ssize_t n;
  573. #if defined(DEBUG) && defined(DEBUG_STATS)
  574. ++_st_stat_writev;
  575. #endif
  576. while (*iov_size > 0) {
  577. if (*iov_size == 1)
  578. n = write(fd->osfd, (*iov)->iov_base, (*iov)->iov_len);
  579. else
  580. n = writev(fd->osfd, *iov, *iov_size);
  581. if (n < 0) {
  582. if (errno == EINTR)
  583. continue;
  584. if (!_IO_NOT_READY_ERROR)
  585. return -1;
  586. } else {
  587. while ((size_t) n >= (*iov)->iov_len) {
  588. n -= (*iov)->iov_len;
  589. (*iov)->iov_base = (char *) (*iov)->iov_base + (*iov)->iov_len;
  590. (*iov)->iov_len = 0;
  591. (*iov)++;
  592. (*iov_size)--;
  593. if (n == 0)
  594. break;
  595. }
  596. if (*iov_size == 0)
  597. break;
  598. (*iov)->iov_base = (char *) (*iov)->iov_base + n;
  599. (*iov)->iov_len -= n;
  600. }
  601. #if defined(DEBUG) && defined(DEBUG_STATS)
  602. ++_st_stat_writev_eagain;
  603. #endif
  604. /* Wait until the socket becomes writable */
  605. if (st_netfd_poll(fd, POLLOUT, timeout) < 0)
  606. return -1;
  607. }
  608. return 0;
  609. }
  610. /*
  611. * Simple I/O functions for UDP.
  612. */
  613. int st_recvfrom(_st_netfd_t *fd, void *buf, int len, struct sockaddr *from, int *fromlen, st_utime_t timeout)
  614. {
  615. int n;
  616. #if defined(DEBUG) && defined(DEBUG_STATS)
  617. ++_st_stat_recvfrom;
  618. #endif
  619. while ((n = recvfrom(fd->osfd, buf, len, 0, from, (socklen_t *)fromlen)) < 0) {
  620. if (errno == EINTR)
  621. continue;
  622. if (!_IO_NOT_READY_ERROR)
  623. return -1;
  624. #if defined(DEBUG) && defined(DEBUG_STATS)
  625. ++_st_stat_recvfrom_eagain;
  626. #endif
  627. /* Wait until the socket becomes readable */
  628. if (st_netfd_poll(fd, POLLIN, timeout) < 0)
  629. return -1;
  630. }
  631. return n;
  632. }
  633. int st_sendto(_st_netfd_t *fd, const void *msg, int len, const struct sockaddr *to, int tolen, st_utime_t timeout)
  634. {
  635. int n;
  636. #if defined(DEBUG) && defined(DEBUG_STATS)
  637. ++_st_stat_sendto;
  638. #endif
  639. while ((n = sendto(fd->osfd, msg, len, 0, to, tolen)) < 0) {
  640. if (errno == EINTR)
  641. continue;
  642. if (!_IO_NOT_READY_ERROR)
  643. return -1;
  644. #if defined(DEBUG) && defined(DEBUG_STATS)
  645. ++_st_stat_sendto_eagain;
  646. #endif
  647. /* Wait until the socket becomes writable */
  648. if (st_netfd_poll(fd, POLLOUT, timeout) < 0)
  649. return -1;
  650. }
  651. return n;
  652. }
  653. int st_recvmsg(_st_netfd_t *fd, struct msghdr *msg, int flags, st_utime_t timeout)
  654. {
  655. int n;
  656. #if defined(DEBUG) && defined(DEBUG_STATS)
  657. ++_st_stat_recvmsg;
  658. #endif
  659. while ((n = recvmsg(fd->osfd, msg, flags)) < 0) {
  660. if (errno == EINTR)
  661. continue;
  662. if (!_IO_NOT_READY_ERROR)
  663. return -1;
  664. #if defined(DEBUG) && defined(DEBUG_STATS)
  665. ++_st_stat_recvmsg_eagain;
  666. #endif
  667. /* Wait until the socket becomes readable */
  668. if (st_netfd_poll(fd, POLLIN, timeout) < 0)
  669. return -1;
  670. }
  671. return n;
  672. }
  673. int st_sendmsg(_st_netfd_t *fd, const struct msghdr *msg, int flags, st_utime_t timeout)
  674. {
  675. int n;
  676. #if defined(DEBUG) && defined(DEBUG_STATS)
  677. ++_st_stat_sendmsg;
  678. #endif
  679. while ((n = sendmsg(fd->osfd, msg, flags)) < 0) {
  680. if (errno == EINTR)
  681. continue;
  682. if (!_IO_NOT_READY_ERROR)
  683. return -1;
  684. #if defined(DEBUG) && defined(DEBUG_STATS)
  685. ++_st_stat_sendmsg_eagain;
  686. #endif
  687. /* Wait until the socket becomes writable */
  688. if (st_netfd_poll(fd, POLLOUT, timeout) < 0)
  689. return -1;
  690. }
  691. return n;
  692. }
  693. /*
  694. * To open FIFOs or other special files.
  695. */
  696. _st_netfd_t *st_open(const char *path, int oflags, mode_t mode)
  697. {
  698. int osfd, err;
  699. _st_netfd_t *newfd;
  700. while ((osfd = open(path, oflags | O_NONBLOCK, mode)) < 0) {
  701. if (errno != EINTR)
  702. return NULL;
  703. }
  704. newfd = _st_netfd_new(osfd, 0, 0);
  705. if (!newfd) {
  706. err = errno;
  707. close(osfd);
  708. errno = err;
  709. }
  710. return newfd;
  711. }