descriptor_ops.ipp 24 KB

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