unix_domain_socket.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_POSIX_UNIX_DOMAIN_SOCKET_H_
  5. #define BASE_POSIX_UNIX_DOMAIN_SOCKET_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <sys/types.h>
  9. #include <vector>
  10. #include "base/base_export.h"
  11. #include "base/files/scoped_file.h"
  12. #include "base/process/process_handle.h"
  13. #include "build/build_config.h"
  14. namespace base {
  15. class Pickle;
  16. #if !defined(OS_NACL_NONSFI)
  17. // Creates a connected pair of UNIX-domain SOCK_SEQPACKET sockets, and passes
  18. // ownership of the newly allocated file descriptors to |one| and |two|.
  19. // Returns true on success.
  20. bool BASE_EXPORT CreateSocketPair(ScopedFD* one, ScopedFD* two);
  21. #endif
  22. class BASE_EXPORT UnixDomainSocket {
  23. public:
  24. // Maximum number of file descriptors that can be read by RecvMsg().
  25. static const size_t kMaxFileDescriptors;
  26. #if !defined(OS_NACL_NONSFI)
  27. // Use to enable receiving process IDs in RecvMsgWithPid. Should be called on
  28. // the receiving socket (i.e., the socket passed to RecvMsgWithPid). Returns
  29. // true if successful.
  30. static bool EnableReceiveProcessId(int fd);
  31. #endif // !defined(OS_NACL_NONSFI)
  32. // Use sendmsg to write the given msg and include a vector of file
  33. // descriptors. Returns true if successful.
  34. static bool SendMsg(int fd,
  35. const void* msg,
  36. size_t length,
  37. const std::vector<int>& fds);
  38. // Use recvmsg to read a message and an array of file descriptors. Returns
  39. // -1 on failure. Note: will read, at most, |kMaxFileDescriptors| descriptors.
  40. static ssize_t RecvMsg(int fd,
  41. void* msg,
  42. size_t length,
  43. std::vector<ScopedFD>* fds);
  44. // Same as RecvMsg above, but also returns the sender's process ID (as seen
  45. // from the caller's namespace). However, before using this function to
  46. // receive process IDs, EnableReceiveProcessId() should be called on the
  47. // receiving socket.
  48. static ssize_t RecvMsgWithPid(int fd,
  49. void* msg,
  50. size_t length,
  51. std::vector<ScopedFD>* fds,
  52. ProcessId* pid);
  53. #if !defined(OS_NACL_NONSFI)
  54. // Perform a sendmsg/recvmsg pair.
  55. // 1. This process creates a UNIX SEQPACKET socketpair. Using
  56. // connection-oriented sockets (SEQPACKET or STREAM) is critical here,
  57. // because if one of the ends closes the other one must be notified.
  58. // 2. This process writes a request to |fd| with an SCM_RIGHTS control
  59. // message containing on end of the fresh socket pair.
  60. // 3. This process blocks reading from the other end of the fresh
  61. // socketpair.
  62. // 4. The target process receives the request, processes it and writes the
  63. // reply to the end of the socketpair contained in the request.
  64. // 5. This process wakes up and continues.
  65. //
  66. // fd: descriptor to send the request on
  67. // reply: buffer for the reply
  68. // reply_len: size of |reply|
  69. // result_fd: (may be NULL) the file descriptor returned in the reply
  70. // (if any)
  71. // request: the bytes to send in the request
  72. static ssize_t SendRecvMsg(int fd,
  73. uint8_t* reply,
  74. unsigned reply_len,
  75. int* result_fd,
  76. const Pickle& request);
  77. // Similar to SendRecvMsg(), but |recvmsg_flags| allows to control the flags
  78. // of the recvmsg(2) call.
  79. static ssize_t SendRecvMsgWithFlags(int fd,
  80. uint8_t* reply,
  81. unsigned reply_len,
  82. int recvmsg_flags,
  83. int* result_fd,
  84. const Pickle& request);
  85. #endif // !defined(OS_NACL_NONSFI)
  86. private:
  87. // Similar to RecvMsg, but allows to specify |flags| for recvmsg(2).
  88. static ssize_t RecvMsgWithFlags(int fd,
  89. void* msg,
  90. size_t length,
  91. int flags,
  92. std::vector<ScopedFD>* fds,
  93. ProcessId* pid);
  94. };
  95. } // namespace base
  96. #endif // BASE_POSIX_UNIX_DOMAIN_SOCKET_H_