Home | History | Annotate | Download | only in embedder
      1 // Copyright 2014 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 MOJO_EMBEDDER_PLATFORM_CHANNEL_UTILS_POSIX_H_
      6 #define MOJO_EMBEDDER_PLATFORM_CHANNEL_UTILS_POSIX_H_
      7 
      8 #include <stddef.h>
      9 #include <sys/types.h>  // For |ssize_t|.
     10 
     11 #include <deque>
     12 
     13 #include "base/memory/scoped_ptr.h"
     14 #include "mojo/embedder/platform_handle.h"
     15 #include "mojo/system/system_impl_export.h"
     16 
     17 struct iovec;  // Declared in <sys/uio.h>.
     18 
     19 namespace mojo {
     20 namespace embedder {
     21 
     22 // The maximum number of handles that can be sent "at once" using
     23 // |PlatformChannelSendmsgWithHandles()|.
     24 // TODO(vtl): This number is taken from ipc/file_descriptor_set_posix.h:
     25 // |FileDescriptorSet::kMaxDescriptorsPerMessage|. Where does it come from?
     26 const size_t kPlatformChannelMaxNumHandles = 7;
     27 
     28 // Use these to write to a socket created using |PlatformChannelPair| (or
     29 // equivalent). These are like |write()| and |writev()|, but handle |EINTR| and
     30 // never raise |SIGPIPE|. (Note: On Mac, the suppression of |SIGPIPE| is set up
     31 // by |PlatformChannelPair|.)
     32 MOJO_SYSTEM_IMPL_EXPORT ssize_t PlatformChannelWrite(PlatformHandle h,
     33                                                      const void* bytes,
     34                                                      size_t num_bytes);
     35 MOJO_SYSTEM_IMPL_EXPORT ssize_t PlatformChannelWritev(PlatformHandle h,
     36                                                       struct iovec* iov,
     37                                                       size_t num_iov);
     38 
     39 // Writes data, and the given set of |PlatformHandle|s (i.e., file descriptors)
     40 // over the Unix domain socket given by |h| (e.g., created using
     41 // |PlatformChannelPair()|). All the handles must be valid, and there must be at
     42 // least one and at most |kPlatformChannelMaxNumHandles| handles. The return
     43 // value is as for |sendmsg()|, namely -1 on failure and otherwise the number of
     44 // bytes of data sent on success (note that this may not be all the data
     45 // specified by |iov|). (The handles are not closed, regardless of success or
     46 // failure.)
     47 MOJO_SYSTEM_IMPL_EXPORT ssize_t PlatformChannelSendmsgWithHandles(
     48     PlatformHandle h,
     49     struct iovec* iov,
     50     size_t num_iov,
     51     PlatformHandle* platform_handles,
     52     size_t num_platform_handles);
     53 
     54 // TODO(vtl): Remove this once I've switched things over to
     55 // |PlatformChannelSendmsgWithHandles()|.
     56 // Sends |PlatformHandle|s (i.e., file descriptors) over the Unix domain socket
     57 // (e.g., created using PlatformChannelPair|). (These will be sent in a single
     58 // message having one null byte of data and one control message header with all
     59 // the file descriptors.) All of the handles must be valid, and there must be at
     60 // most |kPlatformChannelMaxNumHandles| (and at least one handle). Returns true
     61 // on success, in which case it closes all the handles.
     62 MOJO_SYSTEM_IMPL_EXPORT bool PlatformChannelSendHandles(PlatformHandle h,
     63                                                         PlatformHandle* handles,
     64                                                         size_t num_handles);
     65 
     66 // Wrapper around |recvmsg()|, which will extract any attached file descriptors
     67 // (in the control message) to |PlatformHandle|s (and append them to
     68 // |platform_handles|). (This also handles |EINTR|.)
     69 MOJO_SYSTEM_IMPL_EXPORT ssize_t PlatformChannelRecvmsg(
     70     PlatformHandle h,
     71     void* buf,
     72     size_t num_bytes,
     73     std::deque<PlatformHandle>* platform_handles);
     74 
     75 }  // namespace embedder
     76 }  // namespace mojo
     77 
     78 #endif  // MOJO_EMBEDDER_PLATFORM_CHANNEL_UTILS_POSIX_H_
     79