2
0

io.c 20 KB

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