io.c 19 KB

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