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