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