James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 | // This file contains types/constants and functions specific to message pipes. |
| 6 | // |
| 7 | // Note: This header should be compilable as C. |
| 8 | |
| 9 | #ifndef MOJO_PUBLIC_C_SYSTEM_MESSAGE_PIPE_H_ |
| 10 | #define MOJO_PUBLIC_C_SYSTEM_MESSAGE_PIPE_H_ |
| 11 | |
Viet-Trung Luu | f559b92 | 2016-03-10 17:11:02 -0800 | [diff] [blame] | 12 | #include "mojo/public/c/system/handle.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 13 | #include "mojo/public/c/system/macros.h" |
Viet-Trung Luu | f559b92 | 2016-03-10 17:11:02 -0800 | [diff] [blame] | 14 | #include "mojo/public/c/system/result.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 15 | |
| 16 | // |MojoCreateMessagePipeOptions|: Used to specify creation parameters for a |
| 17 | // message pipe to |MojoCreateMessagePipe()|. |
| 18 | // |uint32_t struct_size|: Set to the size of the |
| 19 | // |MojoCreateMessagePipeOptions| struct. (Used to allow for future |
| 20 | // extensions.) |
| 21 | // |MojoCreateMessagePipeOptionsFlags flags|: Reserved for future use. |
| 22 | // |MOJO_CREATE_MESSAGE_PIPE_OPTIONS_FLAG_NONE|: No flags; default mode. |
| 23 | |
| 24 | typedef uint32_t MojoCreateMessagePipeOptionsFlags; |
| 25 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 26 | #define MOJO_CREATE_MESSAGE_PIPE_OPTIONS_FLAG_NONE \ |
| 27 | ((MojoCreateMessagePipeOptionsFlags)0) |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 28 | |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 29 | MOJO_STATIC_ASSERT(MOJO_ALIGNOF(int64_t) == 8, "int64_t has weird alignment"); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 30 | struct MOJO_ALIGNAS(8) MojoCreateMessagePipeOptions { |
| 31 | uint32_t struct_size; |
| 32 | MojoCreateMessagePipeOptionsFlags flags; |
| 33 | }; |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 34 | MOJO_STATIC_ASSERT(sizeof(MojoCreateMessagePipeOptions) == 8, |
| 35 | "MojoCreateMessagePipeOptions has wrong size"); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 36 | |
| 37 | // |MojoWriteMessageFlags|: Used to specify different modes to |
| 38 | // |MojoWriteMessage()|. |
| 39 | // |MOJO_WRITE_MESSAGE_FLAG_NONE| - No flags; default mode. |
| 40 | |
| 41 | typedef uint32_t MojoWriteMessageFlags; |
| 42 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 43 | #define MOJO_WRITE_MESSAGE_FLAG_NONE ((MojoWriteMessageFlags)0) |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 44 | |
| 45 | // |MojoReadMessageFlags|: Used to specify different modes to |
| 46 | // |MojoReadMessage()|. |
| 47 | // |MOJO_READ_MESSAGE_FLAG_NONE| - No flags; default mode. |
| 48 | // |MOJO_READ_MESSAGE_FLAG_MAY_DISCARD| - If the message is unable to be read |
| 49 | // for whatever reason (e.g., the caller-supplied buffer is too small), |
| 50 | // discard the message (i.e., simply dequeue it). |
| 51 | |
| 52 | typedef uint32_t MojoReadMessageFlags; |
| 53 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 54 | #define MOJO_READ_MESSAGE_FLAG_NONE ((MojoReadMessageFlags)0) |
| 55 | #define MOJO_READ_MESSAGE_FLAG_MAY_DISCARD ((MojoReadMessageFlags)1 << 0) |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 56 | |
Viet-Trung Luu | 4a43a9d | 2016-03-15 12:37:15 -0700 | [diff] [blame] | 57 | MOJO_BEGIN_EXTERN_C |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 58 | |
Viet-Trung Luu | 51cb505 | 2016-03-18 13:48:00 -0700 | [diff] [blame] | 59 | // |MojoCreateMessagePipe()|: Creates a message pipe, which is a bidirectional |
| 60 | // communication channel for framed data (i.e., messages). Messages can contain |
| 61 | // plain data and/or Mojo handles. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 62 | // |
| 63 | // |options| may be set to null for a message pipe with the default options. |
| 64 | // |
| 65 | // On success, |*message_pipe_handle0| and |*message_pipe_handle1| are set to |
Viet-Trung Luu | 67c8e97 | 2016-05-10 12:29:59 -0700 | [diff] [blame] | 66 | // handles for the two endpoints (ports) for the message pipe. Both handles have |
| 67 | // (at least) the following rights: |MOJO_HANDLE_RIGHT_TRANSFER|, |
| 68 | // |MOJO_HANDLE_RIGHT_READ|, |MOJO_HANDLE_RIGHT_WRITE|, |
| 69 | // |MOJO_HANDLE_RIGHT_GET_OPTIONS|, and |MOJO_HANDLE_RIGHT_SET_OPTIONS|. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 70 | // |
| 71 | // Returns: |
| 72 | // |MOJO_RESULT_OK| on success. |
| 73 | // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g., |
| 74 | // |*options| is invalid). |
| 75 | // |MOJO_RESULT_RESOURCE_EXHAUSTED| if a process/system/quota/etc. limit has |
| 76 | // been reached. |
Viet-Trung Luu | faf1aaa | 2015-10-23 17:01:25 -0700 | [diff] [blame] | 77 | MojoResult MojoCreateMessagePipe( |
Viet-Trung Luu | 51cb505 | 2016-03-18 13:48:00 -0700 | [diff] [blame] | 78 | const struct MojoCreateMessagePipeOptions* MOJO_RESTRICT |
| 79 | options, // Optional in. |
| 80 | MojoHandle* MOJO_RESTRICT message_pipe_handle0, // Out. |
| 81 | MojoHandle* MOJO_RESTRICT message_pipe_handle1); // Out. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 82 | |
Viet-Trung Luu | 51cb505 | 2016-03-18 13:48:00 -0700 | [diff] [blame] | 83 | // |MojoWriteMessage()|: Writes a message to the message pipe endpoint given by |
Viet-Trung Luu | 67c8e97 | 2016-05-10 12:29:59 -0700 | [diff] [blame] | 84 | // |message_pipe_handle| (which must have the |MOJO_HANDLE_RIGHT_WRITE| right), |
| 85 | // with message data specified by |bytes| of size |num_bytes| and attached |
| 86 | // handles specified by |handles| of count |num_handles|, and options specified |
| 87 | // by |flags|. If there is no message data, |bytes| may be null, in which case |
| 88 | // |num_bytes| must be zero. If there are no attached handles, |handles| may be |
| 89 | // null, in which case |num_handles| must be zero. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 90 | // |
| 91 | // If handles are attached, on success the handles will no longer be valid (the |
| 92 | // receiver will receive equivalent, but logically different, handles). Handles |
Viet-Trung Luu | 67c8e97 | 2016-05-10 12:29:59 -0700 | [diff] [blame] | 93 | // to be sent should not be in simultaneous use (e.g., on another thread). On |
| 94 | // failure, any handles to be attached will remain valid. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 95 | // |
| 96 | // Returns: |
| 97 | // |MOJO_RESULT_OK| on success (i.e., the message was enqueued). |
| 98 | // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g., if |
| 99 | // |message_pipe_handle| is not a valid handle, or some of the |
| 100 | // requirements above are not satisfied). |
Viet-Trung Luu | 67c8e97 | 2016-05-10 12:29:59 -0700 | [diff] [blame] | 101 | // |MOJO_RESULT_PERMISSION_DENIED| if |message_pipe_handle| does not have the |
Viet-Trung Luu | 862d13b | 2016-05-25 09:32:43 -0700 | [diff] [blame^] | 102 | // |MOJO_HANDLE_RIGHT_WRITE| right or if one of the handles to be sent |
| 103 | // does not have the |MOJO_HANDLE_RIGHT_TRANSFER| right. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 104 | // |MOJO_RESULT_RESOURCE_EXHAUSTED| if some system limit has been reached, or |
| 105 | // the number of handles to send is too large (TODO(vtl): reconsider the |
| 106 | // latter case). |
| 107 | // |MOJO_RESULT_FAILED_PRECONDITION| if the other endpoint has been closed. |
| 108 | // Note that closing an endpoint is not necessarily synchronous (e.g., |
Geoffrey Gowan | d043808 | 2015-01-30 16:36:00 -0800 | [diff] [blame] | 109 | // across processes), so this function may succeed even if the other |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 110 | // endpoint has been closed (in which case the message would be dropped). |
| 111 | // |MOJO_RESULT_UNIMPLEMENTED| if an unsupported flag was set in |*options|. |
Viet-Trung Luu | ba1947c | 2016-03-21 16:56:27 -0700 | [diff] [blame] | 112 | // |MOJO_RESULT_BUSY| if |message_pipe_handle| is currently in use in some |
| 113 | // transaction (that, e.g., may result in it being invalidated, such as |
| 114 | // being sent in a message), or if some handle to be sent is currently in |
| 115 | // use. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 116 | // |
Viet-Trung Luu | 862d13b | 2016-05-25 09:32:43 -0700 | [diff] [blame^] | 117 | // Note: |MOJO_RESULT_BUSY| is generally "preferred" over |
| 118 | // |MOJO_RESULT_PERMISSION_DENIED|. E.g., if a handle to be sent both is busy |
| 119 | // and does not have the transfer right, then the result will be "busy". |
| 120 | // |
| 121 | // TODO(vtl): We'll also report |MOJO_RESULT_BUSY| if a (data pipe |
| 122 | // producer/consumer) handle to be sent is in a two-phase write/read). But |
| 123 | // should we? (For comparison, there's no such provision in |MojoClose()|.) |
| 124 | // https://github.com/domokit/mojo/issues/782 |
| 125 | // |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 126 | // TODO(vtl): Add a notion of capacity for message pipes, and return |
| 127 | // |MOJO_RESULT_SHOULD_WAIT| if the message pipe is full. |
Viet-Trung Luu | 51cb505 | 2016-03-18 13:48:00 -0700 | [diff] [blame] | 128 | MojoResult MojoWriteMessage(MojoHandle message_pipe_handle, // In. |
| 129 | const void* bytes, // Optional in. |
| 130 | uint32_t num_bytes, // In. |
| 131 | const MojoHandle* handles, // Optional in. |
| 132 | uint32_t num_handles, // In. |
| 133 | MojoWriteMessageFlags flags); // In. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 134 | |
Viet-Trung Luu | 67c8e97 | 2016-05-10 12:29:59 -0700 | [diff] [blame] | 135 | // |MojoReadMessage()|: Reads the next message from the message pipe endpoint |
| 136 | // given by |message_pipe_handle| (which must have the |MOJO_HANDLE_RIGHT_READ| |
| 137 | // right) or indicates the size of the message if it cannot fit in the provided |
| 138 | // buffers. The message will be read in its entirety or not at all; if it is |
| 139 | // not, it will remain enqueued unless the |MOJO_READ_MESSAGE_FLAG_MAY_DISCARD| |
| 140 | // flag was passed. At most one message will be consumed from the queue, and the |
| 141 | // return value will indicate whether a message was successfully read. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 142 | // |
Geoffrey Gowan | d043808 | 2015-01-30 16:36:00 -0800 | [diff] [blame] | 143 | // |num_bytes| and |num_handles| are optional in/out parameters that on input |
| 144 | // must be set to the sizes of the |bytes| and |handles| arrays, and on output |
| 145 | // will be set to the actual number of bytes or handles contained in the |
| 146 | // message (even if the message was not retrieved due to being too large). |
| 147 | // Either |num_bytes| or |num_handles| may be null if the message is not |
| 148 | // expected to contain the corresponding type of data, but such a call would |
| 149 | // fail with |MOJO_RESULT_RESOURCE_EXHAUSTED| if the message in fact did |
| 150 | // contain that type of data. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 151 | // |
Geoffrey Gowan | d043808 | 2015-01-30 16:36:00 -0800 | [diff] [blame] | 152 | // |bytes| and |handles| will receive the contents of the message, if it is |
| 153 | // retrieved. Either or both may be null, in which case the corresponding size |
| 154 | // parameter(s) must also be set to zero or passed as null. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 155 | // |
| 156 | // Returns: |
| 157 | // |MOJO_RESULT_OK| on success (i.e., a message was actually read). |
| 158 | // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid. |
| 159 | // |MOJO_RESULT_FAILED_PRECONDITION| if the other endpoint has been closed. |
Viet-Trung Luu | 67c8e97 | 2016-05-10 12:29:59 -0700 | [diff] [blame] | 160 | // |MOJO_RESULT_PERMISSION_DENIED| if |message_pipe_handle| does not have the |
| 161 | // |MOJO_HANDLE_RIGHT_READ| right. |
Geoffrey Gowan | d043808 | 2015-01-30 16:36:00 -0800 | [diff] [blame] | 162 | // |MOJO_RESULT_RESOURCE_EXHAUSTED| if the message was too large to fit in the |
| 163 | // provided buffer(s). The message will have been left in the queue or |
| 164 | // discarded, depending on flags. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 165 | // |MOJO_RESULT_SHOULD_WAIT| if no message was available to be read. |
Viet-Trung Luu | ba1947c | 2016-03-21 16:56:27 -0700 | [diff] [blame] | 166 | // |MOJO_RESULT_BUSY| if |message_pipe_handle| is currently in use in some |
| 167 | // transaction (that, e.g., may result in it being invalidated, such as |
| 168 | // being sent in a message). |
Geoffrey Gowan | d043808 | 2015-01-30 16:36:00 -0800 | [diff] [blame] | 169 | // |
| 170 | // TODO(vtl): Reconsider the |MOJO_RESULT_RESOURCE_EXHAUSTED| error code; should |
| 171 | // distinguish this from the hitting-system-limits case. |
Viet-Trung Luu | 51cb505 | 2016-03-18 13:48:00 -0700 | [diff] [blame] | 172 | MojoResult MojoReadMessage( |
| 173 | MojoHandle message_pipe_handle, // In. |
| 174 | void* MOJO_RESTRICT bytes, // Optional out. |
| 175 | uint32_t* MOJO_RESTRICT num_bytes, // Optional in/out. |
| 176 | MojoHandle* MOJO_RESTRICT handles, // Optional out. |
| 177 | uint32_t* MOJO_RESTRICT num_handles, // Optional in/out. |
| 178 | MojoReadMessageFlags flags); // In. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 179 | |
Viet-Trung Luu | 4a43a9d | 2016-03-15 12:37:15 -0700 | [diff] [blame] | 180 | MOJO_END_EXTERN_C |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 181 | |
| 182 | #endif // MOJO_PUBLIC_C_SYSTEM_MESSAGE_PIPE_H_ |