• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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_PUBLIC_CPP_PLATFORM_SOCKET_UTILS_POSIX_H_
6 #define MOJO_PUBLIC_CPP_PLATFORM_SOCKET_UTILS_POSIX_H_
7 
8 #include <stddef.h>
9 #include <sys/types.h>
10 
11 #include "base/component_export.h"
12 #include "base/files/platform_file.h"
13 #include "base/files/scoped_file.h"
14 #include "base/logging.h"
15 #include "base/macros.h"
16 
17 struct iovec;  // Declared in <sys/uio.h>
18 
19 namespace mojo {
20 
21 // NOTE: Functions declared here really don't belong in Mojo, but they exist to
22 // support code which used to rely on internal parts of the Mojo implementation
23 // and there wasn't a much better home for them. Consider moving them to
24 // src/base or something.
25 
26 // Like |write()| but handles |EINTR| and never raises |SIGPIPE|.
27 COMPONENT_EXPORT(MOJO_CPP_PLATFORM)
28 ssize_t SocketWrite(base::PlatformFile socket,
29                     const void* bytes,
30                     size_t num_bytes);
31 
32 // Like |writev()| but handles |EINTR| and never raises |SIGPIPE|.
33 COMPONENT_EXPORT(MOJO_CPP_PLATFORM)
34 ssize_t SocketWritev(base::PlatformFile socket,
35                      struct iovec* iov,
36                      size_t num_iov);
37 
38 // Wrapper around |sendmsg()| which makes it convenient to send attached file
39 // descriptors. All entries in |descriptors| must be valid and |descriptors|
40 // must be non-empty.
41 //
42 // Returns the same value as |sendmsg()|, i.e. -1 on error and otherwise the
43 // number of bytes sent. Note that the number of bytes sent may be smaller
44 // than the total data in |iov|.
45 //
46 // Note that regardless of success or failure, descriptors in |descriptors| are
47 // not closed.
48 COMPONENT_EXPORT(MOJO_CPP_PLATFORM)
49 ssize_t SendmsgWithHandles(base::PlatformFile socket,
50                            struct iovec* iov,
51                            size_t num_iov,
52                            const std::vector<base::ScopedFD>& descriptors);
53 
54 // Like |recvmsg()|, but handles |EINTR|.
55 COMPONENT_EXPORT(MOJO_CPP_PLATFORM)
56 ssize_t SocketRecvmsg(base::PlatformFile socket,
57                       void* buf,
58                       size_t num_bytes,
59                       std::vector<base::ScopedFD>* descriptors,
60                       bool block = false);
61 
62 // Treats |server_fd| as a socket listening for new connections. Returns |false|
63 // if it encounters an unrecoverable error.
64 //
65 // If a connection wasn't established but the server is still OK, this returns
66 // |true| and leaves |*connection_fd| unchanged.
67 //
68 // If a connection was accepted, this returns |true| and |*connection_fd| is
69 // updated with a file descriptor for the new connection.
70 //
71 // Iff |check_peer_user| is |true|, connecting clients running as a different
72 // user from the server (i.e. the calling process) will be rejected.
73 COMPONENT_EXPORT(MOJO_CPP_PLATFORM)
74 bool AcceptSocketConnection(base::PlatformFile server_fd,
75                             base::ScopedFD* connection_fd,
76                             bool check_peer_user = true);
77 
78 }  // namespace mojo
79 
80 #endif  // MOJO_PUBLIC_CPP_PLATFORM_SOCKET_UTILS_POSIX_H_
81