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