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