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