posix.qbk 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. [/
  2. / Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:posix POSIX-Specific Functionality]
  8. [link boost_asio.overview.posix.local UNIX Domain Sockets]
  9. [link boost_asio.overview.posix.stream_descriptor Stream-Oriented File Descriptors]
  10. [link boost_asio.overview.posix.fork Fork]
  11. [section:local UNIX Domain Sockets]
  12. Boost.Asio provides basic support for UNIX domain sockets (also known as local
  13. sockets). The simplest use involves creating a pair of connected sockets.
  14. The following code:
  15. local::stream_protocol::socket socket1(my_io_context);
  16. local::stream_protocol::socket socket2(my_io_context);
  17. local::connect_pair(socket1, socket2);
  18. will create a pair of stream-oriented sockets. To do the same for
  19. datagram-oriented sockets, use:
  20. local::datagram_protocol::socket socket1(my_io_context);
  21. local::datagram_protocol::socket socket2(my_io_context);
  22. local::connect_pair(socket1, socket2);
  23. A UNIX domain socket server may be created by binding an acceptor to an
  24. endpoint, in much the same way as one does for a TCP server:
  25. ::unlink("/tmp/foobar"); // Remove previous binding.
  26. local::stream_protocol::endpoint ep("/tmp/foobar");
  27. local::stream_protocol::acceptor acceptor(my_io_context, ep);
  28. local::stream_protocol::socket socket(my_io_context);
  29. acceptor.accept(socket);
  30. A client that connects to this server might look like:
  31. local::stream_protocol::endpoint ep("/tmp/foobar");
  32. local::stream_protocol::socket socket(my_io_context);
  33. socket.connect(ep);
  34. Transmission of file descriptors or credentials across UNIX domain sockets is
  35. not directly supported within Boost.Asio, but may be achieved by accessing the
  36. socket's underlying descriptor using the [link
  37. boost_asio.reference.basic_socket.native_handle native_handle()] member function.
  38. [heading See Also]
  39. [link boost_asio.reference.local__connect_pair local::connect_pair],
  40. [link boost_asio.reference.local__datagram_protocol local::datagram_protocol],
  41. [link boost_asio.reference.local__datagram_protocol.endpoint local::datagram_protocol::endpoint],
  42. [link boost_asio.reference.local__datagram_protocol.socket local::datagram_protocol::socket],
  43. [link boost_asio.reference.local__stream_protocol local::stream_protocol],
  44. [link boost_asio.reference.local__stream_protocol.acceptor local::stream_protocol::acceptor],
  45. [link boost_asio.reference.local__stream_protocol.endpoint local::stream_protocol::endpoint],
  46. [link boost_asio.reference.local__stream_protocol.iostream local::stream_protocol::iostream],
  47. [link boost_asio.reference.local__stream_protocol.socket local::stream_protocol::socket],
  48. [link boost_asio.examples.cpp03_examples.unix_domain_sockets UNIX domain sockets examples].
  49. [heading Notes]
  50. UNIX domain sockets are only available at compile time if supported by the
  51. target operating system. A program may test for the macro
  52. `BOOST_ASIO_HAS_LOCAL_SOCKETS` to determine whether they are supported.
  53. [endsect]
  54. [section:stream_descriptor Stream-Oriented File Descriptors]
  55. Boost.Asio includes classes added to permit synchronous and asynchronous read and
  56. write operations to be performed on POSIX file descriptors, such as pipes,
  57. standard input and output, and various devices.
  58. These classes also provide limited support for regular files. This support
  59. assumes that the underlying read and write operations provided by the operating
  60. system never fail with `EAGAIN` or `EWOULDBLOCK`. (This assumption normally
  61. holds for buffered file I/O.) Synchronous and asynchronous read and write
  62. operations on file descriptors will succeed but the I/O will always be
  63. performed immediately. Wait operations, and operations involving
  64. `boost::asio::null_buffers`, are not portably supported.
  65. For example, to perform read and write operations on standard input
  66. and output, the following objects may be created:
  67. posix::stream_descriptor in(my_io_context, ::dup(STDIN_FILENO));
  68. posix::stream_descriptor out(my_io_context, ::dup(STDOUT_FILENO));
  69. These are then used as synchronous or asynchronous read and write streams. This
  70. means the objects can be used with any of the [link boost_asio.reference.read
  71. read()], [link boost_asio.reference.async_read async_read()], [link
  72. boost_asio.reference.write write()], [link boost_asio.reference.async_write async_write()],
  73. [link boost_asio.reference.read_until read_until()] or [link
  74. boost_asio.reference.async_read_until async_read_until()] free functions.
  75. [heading See Also]
  76. [link boost_asio.reference.posix__stream_descriptor posix::stream_descriptor],
  77. [link boost_asio.examples.cpp03_examples.chat Chat example (C++03)],
  78. [link boost_asio.examples.cpp11_examples.chat Chat example (C++11)].
  79. [heading Notes]
  80. POSIX stream descriptors are only available at compile time if supported by the
  81. target operating system. A program may test for the macro
  82. `BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR` to determine whether they are supported.
  83. [endsect]
  84. [section:fork Fork]
  85. Boost.Asio supports programs that utilise the `fork()` system call. Provided the
  86. program calls `io_context.notify_fork()` at the appropriate times, Boost.Asio will
  87. recreate any internal file descriptors (such as the "self-pipe trick"
  88. descriptor used for waking up a reactor). The notification is usually performed
  89. as follows:
  90. io_context_.notify_fork(boost::asio::io_context::fork_prepare);
  91. if (fork() == 0)
  92. {
  93. io_context_.notify_fork(boost::asio::io_context::fork_child);
  94. ...
  95. }
  96. else
  97. {
  98. io_context_.notify_fork(boost::asio::io_context::fork_parent);
  99. ...
  100. }
  101. User-defined services can also be made fork-aware by overriding the
  102. `io_context::service::notify_fork()` virtual function.
  103. Note that any file descriptors accessible via Boost.Asio's public API (e.g. the
  104. descriptors underlying `basic_socket<>`, `posix::stream_descriptor`, etc.) are
  105. not altered during a fork. It is the program's responsibility to manage these
  106. as required.
  107. [heading See Also]
  108. [link boost_asio.reference.io_context.notify_fork io_context::notify_fork()],
  109. [link boost_asio.reference.io_context.fork_event io_context::fork_event],
  110. [link boost_asio.reference.execution_context__service.notify_fork io_context::service::notify_fork()],
  111. [link boost_asio.examples.cpp03_examples.fork Fork examples].
  112. [endsect]
  113. [endsect]