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