io.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. #if EAGAIN != EWOULDBLOCK
  53. #define _IO_NOT_READY_ERROR ((errno == EAGAIN) || (errno == EWOULDBLOCK))
  54. #else
  55. #define _IO_NOT_READY_ERROR (errno == EAGAIN)
  56. #endif
  57. #define _LOCAL_MAXIOV 16
  58. /* File descriptor object free list */
  59. static _st_netfd_t *_st_netfd_freelist = NULL;
  60. /* Maximum number of file descriptors that the process can open */
  61. static int _st_osfd_limit = -1;
  62. static void _st_netfd_free_aux_data(_st_netfd_t *fd);
  63. int _st_io_init(void)
  64. {
  65. struct sigaction sigact;
  66. struct rlimit rlim;
  67. int fdlim;
  68. /* Ignore SIGPIPE */
  69. sigact.sa_handler = SIG_IGN;
  70. sigemptyset(&sigact.sa_mask);
  71. sigact.sa_flags = 0;
  72. if (sigaction(SIGPIPE, &sigact, NULL) < 0)
  73. return -1;
  74. /* Set maximum number of open file descriptors */
  75. if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
  76. return -1;
  77. fdlim = (*_st_eventsys->fd_getlimit)();
  78. if (fdlim > 0 && rlim.rlim_max > (rlim_t) fdlim) {
  79. rlim.rlim_max = fdlim;
  80. }
  81. /**
  82. * by SRS, for osx.
  83. * when rlimit max is negative, for example, osx, use cur directly.
  84. * @see https://github.com/winlinvip/simple-rtmp-server/issues/336
  85. */
  86. if ((int)rlim.rlim_max < 0) {
  87. _st_osfd_limit = (int)(fdlim > 0? fdlim : rlim.rlim_cur);
  88. return 0;
  89. }
  90. rlim.rlim_cur = rlim.rlim_max;
  91. if (setrlimit(RLIMIT_NOFILE, &rlim) < 0)
  92. return -1;
  93. _st_osfd_limit = (int) rlim.rlim_max;
  94. return 0;
  95. }
  96. int st_getfdlimit(void)
  97. {
  98. return _st_osfd_limit;
  99. }
  100. void st_netfd_free(_st_netfd_t *fd)
  101. {
  102. if (!fd->inuse)
  103. return;
  104. fd->inuse = 0;
  105. if (fd->aux_data)
  106. _st_netfd_free_aux_data(fd);
  107. if (fd->private_data && fd->destructor)
  108. (*(fd->destructor))(fd->private_data);
  109. fd->private_data = NULL;
  110. fd->destructor = NULL;
  111. fd->next = _st_netfd_freelist;
  112. _st_netfd_freelist = fd;
  113. }
  114. static _st_netfd_t *_st_netfd_new(int osfd, int nonblock, int is_socket)
  115. {
  116. _st_netfd_t *fd;
  117. int flags = 1;
  118. if ((*_st_eventsys->fd_new)(osfd) < 0)
  119. return NULL;
  120. if (_st_netfd_freelist) {
  121. fd = _st_netfd_freelist;
  122. _st_netfd_freelist = _st_netfd_freelist->next;
  123. } else {
  124. fd = calloc(1, sizeof(_st_netfd_t));
  125. if (!fd)
  126. return NULL;
  127. }
  128. fd->osfd = osfd;
  129. fd->inuse = 1;
  130. fd->next = NULL;
  131. if (nonblock) {
  132. /* Use just one system call */
  133. if (is_socket && ioctl(osfd, FIONBIO, &flags) != -1)
  134. return fd;
  135. /* Do it the Posix way */
  136. if ((flags = fcntl(osfd, F_GETFL, 0)) < 0 ||
  137. fcntl(osfd, F_SETFL, flags | O_NONBLOCK) < 0) {
  138. st_netfd_free(fd);
  139. return NULL;
  140. }
  141. }
  142. return fd;
  143. }
  144. _st_netfd_t *st_netfd_open(int osfd)
  145. {
  146. return _st_netfd_new(osfd, 1, 0);
  147. }
  148. _st_netfd_t *st_netfd_open_socket(int osfd)
  149. {
  150. return _st_netfd_new(osfd, 1, 1);
  151. }
  152. int st_netfd_close(_st_netfd_t *fd)
  153. {
  154. if ((*_st_eventsys->fd_close)(fd->osfd) < 0)
  155. return -1;
  156. st_netfd_free(fd);
  157. return close(fd->osfd);
  158. }
  159. int st_netfd_fileno(_st_netfd_t *fd)
  160. {
  161. return (fd->osfd);
  162. }
  163. void st_netfd_setspecific(_st_netfd_t *fd, void *value, _st_destructor_t destructor)
  164. {
  165. if (value != fd->private_data) {
  166. /* Free up previously set non-NULL data value */
  167. if (fd->private_data && fd->destructor)
  168. (*(fd->destructor))(fd->private_data);
  169. }
  170. fd->private_data = value;
  171. fd->destructor = destructor;
  172. }
  173. void *st_netfd_getspecific(_st_netfd_t *fd)
  174. {
  175. return (fd->private_data);
  176. }
  177. /*
  178. * Wait for I/O on a single descriptor.
  179. */
  180. int st_netfd_poll(_st_netfd_t *fd, int how, st_utime_t timeout)
  181. {
  182. struct pollfd pd;
  183. int n;
  184. pd.fd = fd->osfd;
  185. pd.events = (short) how;
  186. pd.revents = 0;
  187. if ((n = st_poll(&pd, 1, timeout)) < 0)
  188. return -1;
  189. if (n == 0) {
  190. /* Timed out */
  191. errno = ETIME;
  192. return -1;
  193. }
  194. if (pd.revents & POLLNVAL) {
  195. errno = EBADF;
  196. return -1;
  197. }
  198. return 0;
  199. }
  200. #ifdef MD_ALWAYS_UNSERIALIZED_ACCEPT
  201. /* No-op */
  202. int st_netfd_serialize_accept(_st_netfd_t *fd)
  203. {
  204. fd->aux_data = NULL;
  205. return 0;
  206. }
  207. /* No-op */
  208. static void _st_netfd_free_aux_data(_st_netfd_t *fd)
  209. {
  210. fd->aux_data = NULL;
  211. }
  212. _st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen, st_utime_t timeout)
  213. {
  214. int osfd, err;
  215. _st_netfd_t *newfd;
  216. while ((osfd = accept(fd->osfd, addr, (socklen_t *)addrlen)) < 0) {
  217. if (errno == EINTR)
  218. continue;
  219. if (!_IO_NOT_READY_ERROR)
  220. return NULL;
  221. /* Wait until the socket becomes readable */
  222. if (st_netfd_poll(fd, POLLIN, timeout) < 0)
  223. return NULL;
  224. }
  225. /* On some platforms the new socket created by accept() inherits */
  226. /* the nonblocking attribute of the listening socket */
  227. #if defined (MD_ACCEPT_NB_INHERITED)
  228. newfd = _st_netfd_new(osfd, 0, 1);
  229. #elif defined (MD_ACCEPT_NB_NOT_INHERITED)
  230. newfd = _st_netfd_new(osfd, 1, 1);
  231. #else
  232. #error Unknown OS
  233. #endif
  234. if (!newfd) {
  235. err = errno;
  236. close(osfd);
  237. errno = err;
  238. }
  239. return newfd;
  240. }
  241. #else /* MD_ALWAYS_UNSERIALIZED_ACCEPT */
  242. /*
  243. * On some platforms accept() calls from different processes
  244. * on the same listen socket must be serialized.
  245. * The following code serializes accept()'s without process blocking.
  246. * A pipe is used as an inter-process semaphore.
  247. */
  248. int st_netfd_serialize_accept(_st_netfd_t *fd)
  249. {
  250. _st_netfd_t **p;
  251. int osfd[2], err;
  252. if (fd->aux_data) {
  253. errno = EINVAL;
  254. return -1;
  255. }
  256. if ((p = (_st_netfd_t **)calloc(2, sizeof(_st_netfd_t *))) == NULL)
  257. return -1;
  258. if (pipe(osfd) < 0) {
  259. free(p);
  260. return -1;
  261. }
  262. if ((p[0] = st_netfd_open(osfd[0])) != NULL && (p[1] = st_netfd_open(osfd[1])) != NULL && write(osfd[1], " ", 1) == 1) {
  263. fd->aux_data = p;
  264. return 0;
  265. }
  266. /* Error */
  267. err = errno;
  268. if (p[0])
  269. st_netfd_free(p[0]);
  270. if (p[1])
  271. st_netfd_free(p[1]);
  272. close(osfd[0]);
  273. close(osfd[1]);
  274. free(p);
  275. errno = err;
  276. return -1;
  277. }
  278. static void _st_netfd_free_aux_data(_st_netfd_t *fd)
  279. {
  280. _st_netfd_t **p = (_st_netfd_t **) fd->aux_data;
  281. st_netfd_close(p[0]);
  282. st_netfd_close(p[1]);
  283. free(p);
  284. fd->aux_data = NULL;
  285. }
  286. _st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen, st_utime_t timeout)
  287. {
  288. int osfd, err;
  289. _st_netfd_t *newfd;
  290. _st_netfd_t **p = (_st_netfd_t **) fd->aux_data;
  291. ssize_t n;
  292. char c;
  293. for ( ; ; ) {
  294. if (p == NULL) {
  295. osfd = accept(fd->osfd, addr, (socklen_t *)addrlen);
  296. } else {
  297. /* Get the lock */
  298. n = st_read(p[0], &c, 1, timeout);
  299. if (n < 0)
  300. return NULL;
  301. ST_ASSERT(n == 1);
  302. /* Got the lock */
  303. osfd = accept(fd->osfd, addr, (socklen_t *)addrlen);
  304. /* Unlock */
  305. err = errno;
  306. n = st_write(p[1], &c, 1, timeout);
  307. ST_ASSERT(n == 1);
  308. errno = err;
  309. }
  310. if (osfd >= 0)
  311. break;
  312. if (errno == EINTR)
  313. continue;
  314. if (!_IO_NOT_READY_ERROR)
  315. return NULL;
  316. /* Wait until the socket becomes readable */
  317. if (st_netfd_poll(fd, POLLIN, timeout) < 0)
  318. return NULL;
  319. }
  320. /* On some platforms the new socket created by accept() inherits */
  321. /* the nonblocking attribute of the listening socket */
  322. #if defined (MD_ACCEPT_NB_INHERITED)
  323. newfd = _st_netfd_new(osfd, 0, 1);
  324. #elif defined (MD_ACCEPT_NB_NOT_INHERITED)
  325. newfd = _st_netfd_new(osfd, 1, 1);
  326. #else
  327. #error Unknown OS
  328. #endif
  329. if (!newfd) {
  330. err = errno;
  331. close(osfd);
  332. errno = err;
  333. }
  334. return newfd;
  335. }
  336. #endif /* MD_ALWAYS_UNSERIALIZED_ACCEPT */
  337. int st_connect(_st_netfd_t *fd, const struct sockaddr *addr, int addrlen, st_utime_t timeout)
  338. {
  339. int n, err = 0;
  340. while (connect(fd->osfd, addr, addrlen) < 0) {
  341. if (errno != EINTR) {
  342. /*
  343. * On some platforms, if connect() is interrupted (errno == EINTR)
  344. * after the kernel binds the socket, a subsequent connect()
  345. * attempt will fail with errno == EADDRINUSE. Ignore EADDRINUSE
  346. * iff connect() was previously interrupted. See Rich Stevens'
  347. * "UNIX Network Programming," Vol. 1, 2nd edition, p. 413
  348. * ("Interrupted connect").
  349. */
  350. if (errno != EINPROGRESS && (errno != EADDRINUSE || err == 0))
  351. return -1;
  352. /* Wait until the socket becomes writable */
  353. if (st_netfd_poll(fd, POLLOUT, timeout) < 0)
  354. return -1;
  355. /* Try to find out whether the connection setup succeeded or failed */
  356. n = sizeof(int);
  357. if (getsockopt(fd->osfd, SOL_SOCKET, SO_ERROR, (char *)&err, (socklen_t *)&n) < 0)
  358. return -1;
  359. if (err) {
  360. errno = err;
  361. return -1;
  362. }
  363. break;
  364. }
  365. err = 1;
  366. }
  367. return 0;
  368. }
  369. ssize_t st_read(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout)
  370. {
  371. ssize_t n;
  372. while ((n = read(fd->osfd, buf, nbyte)) < 0) {
  373. if (errno == EINTR)
  374. continue;
  375. if (!_IO_NOT_READY_ERROR)
  376. return -1;
  377. /* Wait until the socket becomes readable */
  378. if (st_netfd_poll(fd, POLLIN, timeout) < 0)
  379. return -1;
  380. }
  381. return n;
  382. }
  383. int st_read_resid(_st_netfd_t *fd, void *buf, size_t *resid, st_utime_t timeout)
  384. {
  385. struct iovec iov, *riov;
  386. int riov_size, rv;
  387. iov.iov_base = buf;
  388. iov.iov_len = *resid;
  389. riov = &iov;
  390. riov_size = 1;
  391. rv = st_readv_resid(fd, &riov, &riov_size, timeout);
  392. *resid = iov.iov_len;
  393. return rv;
  394. }
  395. ssize_t st_readv(_st_netfd_t *fd, const struct iovec *iov, int iov_size, st_utime_t timeout)
  396. {
  397. ssize_t n;
  398. while ((n = readv(fd->osfd, iov, iov_size)) < 0) {
  399. if (errno == EINTR)
  400. continue;
  401. if (!_IO_NOT_READY_ERROR)
  402. return -1;
  403. /* Wait until the socket becomes readable */
  404. if (st_netfd_poll(fd, POLLIN, timeout) < 0)
  405. return -1;
  406. }
  407. return n;
  408. }
  409. int st_readv_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size, st_utime_t timeout)
  410. {
  411. ssize_t n;
  412. while (*iov_size > 0) {
  413. if (*iov_size == 1)
  414. n = read(fd->osfd, (*iov)->iov_base, (*iov)->iov_len);
  415. else
  416. n = readv(fd->osfd, *iov, *iov_size);
  417. if (n < 0) {
  418. if (errno == EINTR)
  419. continue;
  420. if (!_IO_NOT_READY_ERROR)
  421. return -1;
  422. } else if (n == 0)
  423. break;
  424. else {
  425. while ((size_t) n >= (*iov)->iov_len) {
  426. n -= (*iov)->iov_len;
  427. (*iov)->iov_base = (char *) (*iov)->iov_base + (*iov)->iov_len;
  428. (*iov)->iov_len = 0;
  429. (*iov)++;
  430. (*iov_size)--;
  431. if (n == 0)
  432. break;
  433. }
  434. if (*iov_size == 0)
  435. break;
  436. (*iov)->iov_base = (char *) (*iov)->iov_base + n;
  437. (*iov)->iov_len -= n;
  438. }
  439. /* Wait until the socket becomes readable */
  440. if (st_netfd_poll(fd, POLLIN, timeout) < 0)
  441. return -1;
  442. }
  443. return 0;
  444. }
  445. ssize_t st_read_fully(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout)
  446. {
  447. size_t resid = nbyte;
  448. return st_read_resid(fd, buf, &resid, timeout) == 0 ?
  449. (ssize_t) (nbyte - resid) : -1;
  450. }
  451. int st_write_resid(_st_netfd_t *fd, const void *buf, size_t *resid, st_utime_t timeout)
  452. {
  453. struct iovec iov, *riov;
  454. int riov_size, rv;
  455. iov.iov_base = (void *) buf; /* we promise not to modify buf */
  456. iov.iov_len = *resid;
  457. riov = &iov;
  458. riov_size = 1;
  459. rv = st_writev_resid(fd, &riov, &riov_size, timeout);
  460. *resid = iov.iov_len;
  461. return rv;
  462. }
  463. ssize_t st_write(_st_netfd_t *fd, const void *buf, size_t nbyte, st_utime_t timeout)
  464. {
  465. size_t resid = nbyte;
  466. return st_write_resid(fd, buf, &resid, timeout) == 0 ?
  467. (ssize_t) (nbyte - resid) : -1;
  468. }
  469. ssize_t st_writev(_st_netfd_t *fd, const struct iovec *iov, int iov_size, st_utime_t timeout)
  470. {
  471. ssize_t n, rv;
  472. size_t nleft, nbyte;
  473. int index, iov_cnt;
  474. struct iovec *tmp_iov;
  475. struct iovec local_iov[_LOCAL_MAXIOV];
  476. /* Calculate the total number of bytes to be sent */
  477. nbyte = 0;
  478. for (index = 0; index < iov_size; index++)
  479. nbyte += iov[index].iov_len;
  480. rv = (ssize_t)nbyte;
  481. nleft = nbyte;
  482. tmp_iov = (struct iovec *) iov; /* we promise not to modify iov */
  483. iov_cnt = iov_size;
  484. while (nleft > 0) {
  485. if (iov_cnt == 1) {
  486. if (st_write(fd, tmp_iov[0].iov_base, nleft, timeout) != (ssize_t) nleft)
  487. rv = -1;
  488. break;
  489. }
  490. if ((n = writev(fd->osfd, tmp_iov, iov_cnt)) < 0) {
  491. if (errno == EINTR)
  492. continue;
  493. if (!_IO_NOT_READY_ERROR) {
  494. rv = -1;
  495. break;
  496. }
  497. } else {
  498. if ((size_t) n == nleft)
  499. break;
  500. nleft -= n;
  501. /* Find the next unwritten vector */
  502. n = (ssize_t)(nbyte - nleft);
  503. for (index = 0; (size_t) n >= iov[index].iov_len; index++)
  504. n -= iov[index].iov_len;
  505. if (tmp_iov == iov) {
  506. /* Must copy iov's around */
  507. if (iov_size - index <= _LOCAL_MAXIOV) {
  508. tmp_iov = local_iov;
  509. } else {
  510. tmp_iov = calloc(1, (iov_size - index) * sizeof(struct iovec));
  511. if (tmp_iov == NULL)
  512. return -1;
  513. }
  514. }
  515. /* Fill in the first partial read */
  516. tmp_iov[0].iov_base = &(((char *)iov[index].iov_base)[n]);
  517. tmp_iov[0].iov_len = iov[index].iov_len - n;
  518. index++;
  519. /* Copy the remaining vectors */
  520. for (iov_cnt = 1; index < iov_size; iov_cnt++, index++) {
  521. tmp_iov[iov_cnt].iov_base = iov[index].iov_base;
  522. tmp_iov[iov_cnt].iov_len = iov[index].iov_len;
  523. }
  524. }
  525. /* Wait until the socket becomes writable */
  526. if (st_netfd_poll(fd, POLLOUT, timeout) < 0) {
  527. rv = -1;
  528. break;
  529. }
  530. }
  531. if (tmp_iov != iov && tmp_iov != local_iov)
  532. free(tmp_iov);
  533. return rv;
  534. }
  535. int st_writev_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size, st_utime_t timeout)
  536. {
  537. ssize_t n;
  538. while (*iov_size > 0) {
  539. if (*iov_size == 1)
  540. n = write(fd->osfd, (*iov)->iov_base, (*iov)->iov_len);
  541. else
  542. n = writev(fd->osfd, *iov, *iov_size);
  543. if (n < 0) {
  544. if (errno == EINTR)
  545. continue;
  546. if (!_IO_NOT_READY_ERROR)
  547. return -1;
  548. } else {
  549. while ((size_t) n >= (*iov)->iov_len) {
  550. n -= (*iov)->iov_len;
  551. (*iov)->iov_base = (char *) (*iov)->iov_base + (*iov)->iov_len;
  552. (*iov)->iov_len = 0;
  553. (*iov)++;
  554. (*iov_size)--;
  555. if (n == 0)
  556. break;
  557. }
  558. if (*iov_size == 0)
  559. break;
  560. (*iov)->iov_base = (char *) (*iov)->iov_base + n;
  561. (*iov)->iov_len -= n;
  562. }
  563. /* Wait until the socket becomes writable */
  564. if (st_netfd_poll(fd, POLLOUT, timeout) < 0)
  565. return -1;
  566. }
  567. return 0;
  568. }
  569. /*
  570. * Simple I/O functions for UDP.
  571. */
  572. int st_recvfrom(_st_netfd_t *fd, void *buf, int len, struct sockaddr *from, int *fromlen, st_utime_t timeout)
  573. {
  574. int n;
  575. while ((n = recvfrom(fd->osfd, buf, len, 0, from, (socklen_t *)fromlen)) < 0) {
  576. if (errno == EINTR)
  577. continue;
  578. if (!_IO_NOT_READY_ERROR)
  579. return -1;
  580. /* Wait until the socket becomes readable */
  581. if (st_netfd_poll(fd, POLLIN, timeout) < 0)
  582. return -1;
  583. }
  584. return n;
  585. }
  586. int st_sendto(_st_netfd_t *fd, const void *msg, int len, const struct sockaddr *to, int tolen, st_utime_t timeout)
  587. {
  588. int n;
  589. while ((n = sendto(fd->osfd, msg, len, 0, to, tolen)) < 0) {
  590. if (errno == EINTR)
  591. continue;
  592. if (!_IO_NOT_READY_ERROR)
  593. return -1;
  594. /* Wait until the socket becomes writable */
  595. if (st_netfd_poll(fd, POLLOUT, timeout) < 0)
  596. return -1;
  597. }
  598. return n;
  599. }
  600. int st_recvmsg(_st_netfd_t *fd, struct msghdr *msg, int flags, st_utime_t timeout)
  601. {
  602. int n;
  603. while ((n = recvmsg(fd->osfd, msg, flags)) < 0) {
  604. if (errno == EINTR)
  605. continue;
  606. if (!_IO_NOT_READY_ERROR)
  607. return -1;
  608. /* Wait until the socket becomes readable */
  609. if (st_netfd_poll(fd, POLLIN, timeout) < 0)
  610. return -1;
  611. }
  612. return n;
  613. }
  614. int st_sendmsg(_st_netfd_t *fd, const struct msghdr *msg, int flags, st_utime_t timeout)
  615. {
  616. int n;
  617. while ((n = sendmsg(fd->osfd, msg, flags)) < 0) {
  618. if (errno == EINTR)
  619. continue;
  620. if (!_IO_NOT_READY_ERROR)
  621. return -1;
  622. /* Wait until the socket becomes writable */
  623. if (st_netfd_poll(fd, POLLOUT, timeout) < 0)
  624. return -1;
  625. }
  626. return n;
  627. }
  628. /*
  629. * To open FIFOs or other special files.
  630. */
  631. _st_netfd_t *st_open(const char *path, int oflags, mode_t mode)
  632. {
  633. int osfd, err;
  634. _st_netfd_t *newfd;
  635. while ((osfd = open(path, oflags | O_NONBLOCK, mode)) < 0) {
  636. if (errno != EINTR)
  637. return NULL;
  638. }
  639. newfd = _st_netfd_new(osfd, 0, 0);
  640. if (!newfd) {
  641. err = errno;
  642. close(osfd);
  643. errno = err;
  644. }
  645. return newfd;
  646. }