descriptor_ops.ipp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. //
  2. // detail/impl/descriptor_ops.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_DETAIL_IMPL_DESCRIPTOR_OPS_IPP
  11. #define ASIO_DETAIL_IMPL_DESCRIPTOR_OPS_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include <cerrno>
  17. #include "asio/detail/descriptor_ops.hpp"
  18. #include "asio/error.hpp"
  19. #if !defined(ASIO_WINDOWS) \
  20. && !defined(ASIO_WINDOWS_RUNTIME) \
  21. && !defined(__CYGWIN__)
  22. #include "asio/detail/push_options.hpp"
  23. namespace asio {
  24. namespace detail {
  25. namespace descriptor_ops {
  26. int open(const char* path, int flags, asio::error_code& ec)
  27. {
  28. int result = ::open(path, flags);
  29. get_last_error(ec, result < 0);
  30. return result;
  31. }
  32. int open(const char* path, int flags,
  33. unsigned mode, asio::error_code& ec)
  34. {
  35. int result = ::open(path, flags, mode);
  36. get_last_error(ec, result < 0);
  37. return result;
  38. }
  39. int close(int d, state_type& state, asio::error_code& ec)
  40. {
  41. int result = 0;
  42. if (d != -1)
  43. {
  44. result = ::close(d);
  45. get_last_error(ec, result < 0);
  46. if (result != 0
  47. && (ec == asio::error::would_block
  48. || ec == asio::error::try_again))
  49. {
  50. // According to UNIX Network Programming Vol. 1, it is possible for
  51. // close() to fail with EWOULDBLOCK under certain circumstances. What
  52. // isn't clear is the state of the descriptor after this error. The one
  53. // current OS where this behaviour is seen, Windows, says that the socket
  54. // remains open. Therefore we'll put the descriptor back into blocking
  55. // mode and have another attempt at closing it.
  56. #if defined(__SYMBIAN32__) || defined(__EMSCRIPTEN__)
  57. int flags = ::fcntl(d, F_GETFL, 0);
  58. if (flags >= 0)
  59. ::fcntl(d, F_SETFL, flags & ~O_NONBLOCK);
  60. #else // defined(__SYMBIAN32__) || defined(__EMSCRIPTEN__)
  61. ioctl_arg_type arg = 0;
  62. ::ioctl(d, FIONBIO, &arg);
  63. #endif // defined(__SYMBIAN32__) || defined(__EMSCRIPTEN__)
  64. state &= ~non_blocking;
  65. result = ::close(d);
  66. get_last_error(ec, result < 0);
  67. }
  68. }
  69. return result;
  70. }
  71. bool set_user_non_blocking(int d, state_type& state,
  72. bool value, asio::error_code& ec)
  73. {
  74. if (d == -1)
  75. {
  76. ec = asio::error::bad_descriptor;
  77. return false;
  78. }
  79. #if defined(__SYMBIAN32__) || defined(__EMSCRIPTEN__)
  80. int result = ::fcntl(d, F_GETFL, 0);
  81. get_last_error(ec, result < 0);
  82. if (result >= 0)
  83. {
  84. int flag = (value ? (result | O_NONBLOCK) : (result & ~O_NONBLOCK));
  85. result = ::fcntl(d, F_SETFL, flag);
  86. get_last_error(ec, result < 0);
  87. }
  88. #else // defined(__SYMBIAN32__) || defined(__EMSCRIPTEN__)
  89. ioctl_arg_type arg = (value ? 1 : 0);
  90. int result = ::ioctl(d, FIONBIO, &arg);
  91. get_last_error(ec, result < 0);
  92. #endif // defined(__SYMBIAN32__) || defined(__EMSCRIPTEN__)
  93. if (result >= 0)
  94. {
  95. if (value)
  96. state |= user_set_non_blocking;
  97. else
  98. {
  99. // Clearing the user-set non-blocking mode always overrides any
  100. // internally-set non-blocking flag. Any subsequent asynchronous
  101. // operations will need to re-enable non-blocking I/O.
  102. state &= ~(user_set_non_blocking | internal_non_blocking);
  103. }
  104. return true;
  105. }
  106. return false;
  107. }
  108. bool set_internal_non_blocking(int d, state_type& state,
  109. bool value, asio::error_code& ec)
  110. {
  111. if (d == -1)
  112. {
  113. ec = asio::error::bad_descriptor;
  114. return false;
  115. }
  116. if (!value && (state & user_set_non_blocking))
  117. {
  118. // It does not make sense to clear the internal non-blocking flag if the
  119. // user still wants non-blocking behaviour. Return an error and let the
  120. // caller figure out whether to update the user-set non-blocking flag.
  121. ec = asio::error::invalid_argument;
  122. return false;
  123. }
  124. #if defined(__SYMBIAN32__) || defined(__EMSCRIPTEN__)
  125. int result = ::fcntl(d, F_GETFL, 0);
  126. get_last_error(ec, result < 0);
  127. if (result >= 0)
  128. {
  129. int flag = (value ? (result | O_NONBLOCK) : (result & ~O_NONBLOCK));
  130. result = ::fcntl(d, F_SETFL, flag);
  131. get_last_error(ec, result < 0);
  132. }
  133. #else // defined(__SYMBIAN32__) || defined(__EMSCRIPTEN__)
  134. ioctl_arg_type arg = (value ? 1 : 0);
  135. int result = ::ioctl(d, FIONBIO, &arg);
  136. get_last_error(ec, result < 0);
  137. #endif // defined(__SYMBIAN32__) || defined(__EMSCRIPTEN__)
  138. if (result >= 0)
  139. {
  140. if (value)
  141. state |= internal_non_blocking;
  142. else
  143. state &= ~internal_non_blocking;
  144. return true;
  145. }
  146. return false;
  147. }
  148. std::size_t sync_read(int d, state_type state, buf* bufs,
  149. std::size_t count, bool all_empty, asio::error_code& ec)
  150. {
  151. if (d == -1)
  152. {
  153. ec = asio::error::bad_descriptor;
  154. return 0;
  155. }
  156. // A request to read 0 bytes on a stream is a no-op.
  157. if (all_empty)
  158. {
  159. ec.assign(0, ec.category());
  160. return 0;
  161. }
  162. // Read some data.
  163. for (;;)
  164. {
  165. // Try to complete the operation without blocking.
  166. signed_size_type bytes = ::readv(d, bufs, static_cast<int>(count));
  167. get_last_error(ec, bytes < 0);
  168. // Check if operation succeeded.
  169. if (bytes > 0)
  170. return bytes;
  171. // Check for EOF.
  172. if (bytes == 0)
  173. {
  174. ec = asio::error::eof;
  175. return 0;
  176. }
  177. // Operation failed.
  178. if ((state & user_set_non_blocking)
  179. || (ec != asio::error::would_block
  180. && ec != asio::error::try_again))
  181. return 0;
  182. // Wait for descriptor to become ready.
  183. if (descriptor_ops::poll_read(d, 0, ec) < 0)
  184. return 0;
  185. }
  186. }
  187. std::size_t sync_read1(int d, state_type state, void* data,
  188. std::size_t size, asio::error_code& ec)
  189. {
  190. if (d == -1)
  191. {
  192. ec = asio::error::bad_descriptor;
  193. return 0;
  194. }
  195. // A request to read 0 bytes on a stream is a no-op.
  196. if (size == 0)
  197. {
  198. ec.assign(0, ec.category());
  199. return 0;
  200. }
  201. // Read some data.
  202. for (;;)
  203. {
  204. // Try to complete the operation without blocking.
  205. signed_size_type bytes = ::read(d, data, size);
  206. get_last_error(ec, bytes < 0);
  207. // Check if operation succeeded.
  208. if (bytes > 0)
  209. return bytes;
  210. // Check for EOF.
  211. if (bytes == 0)
  212. {
  213. ec = asio::error::eof;
  214. return 0;
  215. }
  216. // Operation failed.
  217. if ((state & user_set_non_blocking)
  218. || (ec != asio::error::would_block
  219. && ec != asio::error::try_again))
  220. return 0;
  221. // Wait for descriptor to become ready.
  222. if (descriptor_ops::poll_read(d, 0, ec) < 0)
  223. return 0;
  224. }
  225. }
  226. bool non_blocking_read(int d, buf* bufs, std::size_t count,
  227. asio::error_code& ec, std::size_t& bytes_transferred)
  228. {
  229. for (;;)
  230. {
  231. // Read some data.
  232. signed_size_type bytes = ::readv(d, bufs, static_cast<int>(count));
  233. get_last_error(ec, bytes < 0);
  234. // Check for end of stream.
  235. if (bytes == 0)
  236. {
  237. ec = asio::error::eof;
  238. return true;
  239. }
  240. // Check if operation succeeded.
  241. if (bytes > 0)
  242. {
  243. bytes_transferred = bytes;
  244. return true;
  245. }
  246. // Retry operation if interrupted by signal.
  247. if (ec == asio::error::interrupted)
  248. continue;
  249. // Check if we need to run the operation again.
  250. if (ec == asio::error::would_block
  251. || ec == asio::error::try_again)
  252. return false;
  253. // Operation failed.
  254. bytes_transferred = 0;
  255. return true;
  256. }
  257. }
  258. bool non_blocking_read1(int d, void* data, std::size_t size,
  259. asio::error_code& ec, std::size_t& bytes_transferred)
  260. {
  261. for (;;)
  262. {
  263. // Read some data.
  264. signed_size_type bytes = ::read(d, data, size);
  265. get_last_error(ec, bytes < 0);
  266. // Check for end of stream.
  267. if (bytes == 0)
  268. {
  269. ec = asio::error::eof;
  270. return true;
  271. }
  272. // Check if operation succeeded.
  273. if (bytes > 0)
  274. {
  275. bytes_transferred = bytes;
  276. return true;
  277. }
  278. // Retry operation if interrupted by signal.
  279. if (ec == asio::error::interrupted)
  280. continue;
  281. // Check if we need to run the operation again.
  282. if (ec == asio::error::would_block
  283. || ec == asio::error::try_again)
  284. return false;
  285. // Operation failed.
  286. bytes_transferred = 0;
  287. return true;
  288. }
  289. }
  290. std::size_t sync_write(int d, state_type state, const buf* bufs,
  291. std::size_t count, bool all_empty, asio::error_code& ec)
  292. {
  293. if (d == -1)
  294. {
  295. ec = asio::error::bad_descriptor;
  296. return 0;
  297. }
  298. // A request to write 0 bytes on a stream is a no-op.
  299. if (all_empty)
  300. {
  301. ec.assign(0, ec.category());
  302. return 0;
  303. }
  304. // Write some data.
  305. for (;;)
  306. {
  307. // Try to complete the operation without blocking.
  308. signed_size_type bytes = ::writev(d, bufs, static_cast<int>(count));
  309. get_last_error(ec, bytes < 0);
  310. // Check if operation succeeded.
  311. if (bytes > 0)
  312. return bytes;
  313. // Operation failed.
  314. if ((state & user_set_non_blocking)
  315. || (ec != asio::error::would_block
  316. && ec != asio::error::try_again))
  317. return 0;
  318. // Wait for descriptor to become ready.
  319. if (descriptor_ops::poll_write(d, 0, ec) < 0)
  320. return 0;
  321. }
  322. }
  323. std::size_t sync_write1(int d, state_type state, const void* data,
  324. std::size_t size, asio::error_code& ec)
  325. {
  326. if (d == -1)
  327. {
  328. ec = asio::error::bad_descriptor;
  329. return 0;
  330. }
  331. // A request to write 0 bytes on a stream is a no-op.
  332. if (size == 0)
  333. {
  334. ec.assign(0, ec.category());
  335. return 0;
  336. }
  337. // Write some data.
  338. for (;;)
  339. {
  340. // Try to complete the operation without blocking.
  341. signed_size_type bytes = ::write(d, data, size);
  342. get_last_error(ec, bytes < 0);
  343. // Check if operation succeeded.
  344. if (bytes > 0)
  345. return bytes;
  346. // Operation failed.
  347. if ((state & user_set_non_blocking)
  348. || (ec != asio::error::would_block
  349. && ec != asio::error::try_again))
  350. return 0;
  351. // Wait for descriptor to become ready.
  352. if (descriptor_ops::poll_write(d, 0, ec) < 0)
  353. return 0;
  354. }
  355. }
  356. bool non_blocking_write(int d, const buf* bufs, std::size_t count,
  357. asio::error_code& ec, std::size_t& bytes_transferred)
  358. {
  359. for (;;)
  360. {
  361. // Write some data.
  362. signed_size_type bytes = ::writev(d, bufs, static_cast<int>(count));
  363. get_last_error(ec, bytes < 0);
  364. // Check if operation succeeded.
  365. if (bytes >= 0)
  366. {
  367. bytes_transferred = bytes;
  368. return true;
  369. }
  370. // Retry operation if interrupted by signal.
  371. if (ec == asio::error::interrupted)
  372. continue;
  373. // Check if we need to run the operation again.
  374. if (ec == asio::error::would_block
  375. || ec == asio::error::try_again)
  376. return false;
  377. // Operation failed.
  378. bytes_transferred = 0;
  379. return true;
  380. }
  381. }
  382. bool non_blocking_write1(int d, const void* data, std::size_t size,
  383. asio::error_code& ec, std::size_t& bytes_transferred)
  384. {
  385. for (;;)
  386. {
  387. // Write some data.
  388. signed_size_type bytes = ::write(d, data, size);
  389. get_last_error(ec, bytes < 0);
  390. // Check if operation succeeded.
  391. if (bytes >= 0)
  392. {
  393. bytes_transferred = bytes;
  394. return true;
  395. }
  396. // Retry operation if interrupted by signal.
  397. if (ec == asio::error::interrupted)
  398. continue;
  399. // Check if we need to run the operation again.
  400. if (ec == asio::error::would_block
  401. || ec == asio::error::try_again)
  402. return false;
  403. // Operation failed.
  404. bytes_transferred = 0;
  405. return true;
  406. }
  407. }
  408. #if defined(ASIO_HAS_FILE)
  409. std::size_t sync_read_at(int d, state_type state, uint64_t offset,
  410. buf* bufs, std::size_t count, bool all_empty, asio::error_code& ec)
  411. {
  412. if (d == -1)
  413. {
  414. ec = asio::error::bad_descriptor;
  415. return 0;
  416. }
  417. // A request to read 0 bytes on a stream is a no-op.
  418. if (all_empty)
  419. {
  420. ec.assign(0, ec.category());
  421. return 0;
  422. }
  423. // Read some data.
  424. for (;;)
  425. {
  426. // Try to complete the operation without blocking.
  427. signed_size_type bytes = ::preadv(d, bufs, static_cast<int>(count), offset);
  428. get_last_error(ec, bytes < 0);
  429. // Check if operation succeeded.
  430. if (bytes > 0)
  431. return bytes;
  432. // Check for EOF.
  433. if (bytes == 0)
  434. {
  435. ec = asio::error::eof;
  436. return 0;
  437. }
  438. // Operation failed.
  439. if ((state & user_set_non_blocking)
  440. || (ec != asio::error::would_block
  441. && ec != asio::error::try_again))
  442. return 0;
  443. // Wait for descriptor to become ready.
  444. if (descriptor_ops::poll_read(d, 0, ec) < 0)
  445. return 0;
  446. }
  447. }
  448. std::size_t sync_read_at1(int d, state_type state, uint64_t offset,
  449. void* data, std::size_t size, asio::error_code& ec)
  450. {
  451. if (d == -1)
  452. {
  453. ec = asio::error::bad_descriptor;
  454. return 0;
  455. }
  456. // A request to read 0 bytes on a stream is a no-op.
  457. if (size == 0)
  458. {
  459. ec.assign(0, ec.category());
  460. return 0;
  461. }
  462. // Read some data.
  463. for (;;)
  464. {
  465. // Try to complete the operation without blocking.
  466. signed_size_type bytes = ::pread(d, data, size, offset);
  467. get_last_error(ec, bytes < 0);
  468. // Check if operation succeeded.
  469. if (bytes > 0)
  470. return bytes;
  471. // Check for EOF.
  472. if (bytes == 0)
  473. {
  474. ec = asio::error::eof;
  475. return 0;
  476. }
  477. // Operation failed.
  478. if ((state & user_set_non_blocking)
  479. || (ec != asio::error::would_block
  480. && ec != asio::error::try_again))
  481. return 0;
  482. // Wait for descriptor to become ready.
  483. if (descriptor_ops::poll_read(d, 0, ec) < 0)
  484. return 0;
  485. }
  486. }
  487. bool non_blocking_read_at(int d, uint64_t offset, buf* bufs, std::size_t count,
  488. asio::error_code& ec, std::size_t& bytes_transferred)
  489. {
  490. for (;;)
  491. {
  492. // Read some data.
  493. signed_size_type bytes = ::preadv(d, bufs, static_cast<int>(count), offset);
  494. get_last_error(ec, bytes < 0);
  495. // Check for EOF.
  496. if (bytes == 0)
  497. {
  498. ec = asio::error::eof;
  499. return true;
  500. }
  501. // Check if operation succeeded.
  502. if (bytes > 0)
  503. {
  504. bytes_transferred = bytes;
  505. return true;
  506. }
  507. // Retry operation if interrupted by signal.
  508. if (ec == asio::error::interrupted)
  509. continue;
  510. // Check if we need to run the operation again.
  511. if (ec == asio::error::would_block
  512. || ec == asio::error::try_again)
  513. return false;
  514. // Operation failed.
  515. bytes_transferred = 0;
  516. return true;
  517. }
  518. }
  519. bool non_blocking_read_at1(int d, uint64_t offset, void* data, std::size_t size,
  520. asio::error_code& ec, std::size_t& bytes_transferred)
  521. {
  522. for (;;)
  523. {
  524. // Read some data.
  525. signed_size_type bytes = ::pread(d, data, size, offset);
  526. get_last_error(ec, bytes < 0);
  527. // Check for EOF.
  528. if (bytes == 0)
  529. {
  530. ec = asio::error::eof;
  531. return true;
  532. }
  533. // Check if operation succeeded.
  534. if (bytes > 0)
  535. {
  536. bytes_transferred = bytes;
  537. return true;
  538. }
  539. // Retry operation if interrupted by signal.
  540. if (ec == asio::error::interrupted)
  541. continue;
  542. // Check if we need to run the operation again.
  543. if (ec == asio::error::would_block
  544. || ec == asio::error::try_again)
  545. return false;
  546. // Operation failed.
  547. bytes_transferred = 0;
  548. return true;
  549. }
  550. }
  551. std::size_t sync_write_at(int d, state_type state, uint64_t offset,
  552. const buf* bufs, std::size_t count, bool all_empty,
  553. asio::error_code& ec)
  554. {
  555. if (d == -1)
  556. {
  557. ec = asio::error::bad_descriptor;
  558. return 0;
  559. }
  560. // A request to write 0 bytes on a stream is a no-op.
  561. if (all_empty)
  562. {
  563. ec.assign(0, ec.category());
  564. return 0;
  565. }
  566. // Write some data.
  567. for (;;)
  568. {
  569. // Try to complete the operation without blocking.
  570. signed_size_type bytes = ::pwritev(d,
  571. bufs, static_cast<int>(count), offset);
  572. get_last_error(ec, bytes < 0);
  573. // Check if operation succeeded.
  574. if (bytes > 0)
  575. return bytes;
  576. // Operation failed.
  577. if ((state & user_set_non_blocking)
  578. || (ec != asio::error::would_block
  579. && ec != asio::error::try_again))
  580. return 0;
  581. // Wait for descriptor to become ready.
  582. if (descriptor_ops::poll_write(d, 0, ec) < 0)
  583. return 0;
  584. }
  585. }
  586. std::size_t sync_write_at1(int d, state_type state, uint64_t offset,
  587. const void* data, std::size_t size, asio::error_code& ec)
  588. {
  589. if (d == -1)
  590. {
  591. ec = asio::error::bad_descriptor;
  592. return 0;
  593. }
  594. // A request to write 0 bytes on a stream is a no-op.
  595. if (size == 0)
  596. {
  597. ec.assign(0, ec.category());
  598. return 0;
  599. }
  600. // Write some data.
  601. for (;;)
  602. {
  603. // Try to complete the operation without blocking.
  604. signed_size_type bytes = ::pwrite(d, data, size, offset);
  605. get_last_error(ec, bytes < 0);
  606. // Check if operation succeeded.
  607. if (bytes > 0)
  608. return bytes;
  609. // Operation failed.
  610. if ((state & user_set_non_blocking)
  611. || (ec != asio::error::would_block
  612. && ec != asio::error::try_again))
  613. return 0;
  614. // Wait for descriptor to become ready.
  615. if (descriptor_ops::poll_write(d, 0, ec) < 0)
  616. return 0;
  617. }
  618. }
  619. bool non_blocking_write_at(int d, uint64_t offset,
  620. const buf* bufs, std::size_t count,
  621. asio::error_code& ec, std::size_t& bytes_transferred)
  622. {
  623. for (;;)
  624. {
  625. // Write some data.
  626. signed_size_type bytes = ::pwritev(d,
  627. bufs, static_cast<int>(count), offset);
  628. get_last_error(ec, bytes < 0);
  629. // Check if operation succeeded.
  630. if (bytes >= 0)
  631. {
  632. bytes_transferred = bytes;
  633. return true;
  634. }
  635. // Retry operation if interrupted by signal.
  636. if (ec == asio::error::interrupted)
  637. continue;
  638. // Check if we need to run the operation again.
  639. if (ec == asio::error::would_block
  640. || ec == asio::error::try_again)
  641. return false;
  642. // Operation failed.
  643. bytes_transferred = 0;
  644. return true;
  645. }
  646. }
  647. bool non_blocking_write_at1(int d, uint64_t offset,
  648. const void* data, std::size_t size,
  649. asio::error_code& ec, std::size_t& bytes_transferred)
  650. {
  651. for (;;)
  652. {
  653. // Write some data.
  654. signed_size_type bytes = ::pwrite(d, data, size, offset);
  655. get_last_error(ec, bytes < 0);
  656. // Check if operation succeeded.
  657. if (bytes >= 0)
  658. {
  659. bytes_transferred = bytes;
  660. return true;
  661. }
  662. // Retry operation if interrupted by signal.
  663. if (ec == asio::error::interrupted)
  664. continue;
  665. // Check if we need to run the operation again.
  666. if (ec == asio::error::would_block
  667. || ec == asio::error::try_again)
  668. return false;
  669. // Operation failed.
  670. bytes_transferred = 0;
  671. return true;
  672. }
  673. }
  674. #endif // defined(ASIO_HAS_FILE)
  675. int ioctl(int d, state_type& state, long cmd,
  676. ioctl_arg_type* arg, asio::error_code& ec)
  677. {
  678. if (d == -1)
  679. {
  680. ec = asio::error::bad_descriptor;
  681. return -1;
  682. }
  683. int result = ::ioctl(d, cmd, arg);
  684. get_last_error(ec, result < 0);
  685. if (result >= 0)
  686. {
  687. // When updating the non-blocking mode we always perform the ioctl syscall,
  688. // even if the flags would otherwise indicate that the descriptor is
  689. // already in the correct state. This ensures that the underlying
  690. // descriptor is put into the state that has been requested by the user. If
  691. // the ioctl syscall was successful then we need to update the flags to
  692. // match.
  693. if (cmd == static_cast<long>(FIONBIO))
  694. {
  695. if (*arg)
  696. {
  697. state |= user_set_non_blocking;
  698. }
  699. else
  700. {
  701. // Clearing the non-blocking mode always overrides any internally-set
  702. // non-blocking flag. Any subsequent asynchronous operations will need
  703. // to re-enable non-blocking I/O.
  704. state &= ~(user_set_non_blocking | internal_non_blocking);
  705. }
  706. }
  707. }
  708. return result;
  709. }
  710. int fcntl(int d, int cmd, asio::error_code& ec)
  711. {
  712. if (d == -1)
  713. {
  714. ec = asio::error::bad_descriptor;
  715. return -1;
  716. }
  717. int result = ::fcntl(d, cmd);
  718. get_last_error(ec, result < 0);
  719. return result;
  720. }
  721. int fcntl(int d, int cmd, long arg, asio::error_code& ec)
  722. {
  723. if (d == -1)
  724. {
  725. ec = asio::error::bad_descriptor;
  726. return -1;
  727. }
  728. int result = ::fcntl(d, cmd, arg);
  729. get_last_error(ec, result < 0);
  730. return result;
  731. }
  732. int poll_read(int d, state_type state, asio::error_code& ec)
  733. {
  734. if (d == -1)
  735. {
  736. ec = asio::error::bad_descriptor;
  737. return -1;
  738. }
  739. pollfd fds;
  740. fds.fd = d;
  741. fds.events = POLLIN;
  742. fds.revents = 0;
  743. int timeout = (state & user_set_non_blocking) ? 0 : -1;
  744. int result = ::poll(&fds, 1, timeout);
  745. get_last_error(ec, result < 0);
  746. if (result == 0)
  747. if (state & user_set_non_blocking)
  748. ec = asio::error::would_block;
  749. return result;
  750. }
  751. int poll_write(int d, state_type state, asio::error_code& ec)
  752. {
  753. if (d == -1)
  754. {
  755. ec = asio::error::bad_descriptor;
  756. return -1;
  757. }
  758. pollfd fds;
  759. fds.fd = d;
  760. fds.events = POLLOUT;
  761. fds.revents = 0;
  762. int timeout = (state & user_set_non_blocking) ? 0 : -1;
  763. int result = ::poll(&fds, 1, timeout);
  764. get_last_error(ec, result < 0);
  765. if (result == 0)
  766. if (state & user_set_non_blocking)
  767. ec = asio::error::would_block;
  768. return result;
  769. }
  770. int poll_error(int d, state_type state, asio::error_code& ec)
  771. {
  772. if (d == -1)
  773. {
  774. ec = asio::error::bad_descriptor;
  775. return -1;
  776. }
  777. pollfd fds;
  778. fds.fd = d;
  779. fds.events = POLLPRI | POLLERR | POLLHUP;
  780. fds.revents = 0;
  781. int timeout = (state & user_set_non_blocking) ? 0 : -1;
  782. int result = ::poll(&fds, 1, timeout);
  783. get_last_error(ec, result < 0);
  784. if (result == 0)
  785. if (state & user_set_non_blocking)
  786. ec = asio::error::would_block;
  787. return result;
  788. }
  789. } // namespace descriptor_ops
  790. } // namespace detail
  791. } // namespace asio
  792. #include "asio/detail/pop_options.hpp"
  793. #endif // !defined(ASIO_WINDOWS)
  794. // && !defined(ASIO_WINDOWS_RUNTIME)
  795. // && !defined(__CYGWIN__)
  796. #endif // ASIO_DETAIL_IMPL_DESCRIPTOR_OPS_IPP