blob: 166e08f7d58686ddf3bad26020fa85d1a6af14f0 [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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 Luuf559b922016-03-10 17:11:02 -080012#include "mojo/public/c/system/handle.h"
James Robinson646469d2014-10-03 15:33:28 -070013#include "mojo/public/c/system/macros.h"
Viet-Trung Luuf559b922016-03-10 17:11:02 -080014#include "mojo/public/c/system/result.h"
James Robinson646469d2014-10-03 15:33:28 -070015
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
24typedef uint32_t MojoCreateMessagePipeOptionsFlags;
25
James Robinson646469d2014-10-03 15:33:28 -070026#define MOJO_CREATE_MESSAGE_PIPE_OPTIONS_FLAG_NONE \
27 ((MojoCreateMessagePipeOptionsFlags)0)
James Robinson646469d2014-10-03 15:33:28 -070028
James Robinsonbaf71d32014-10-08 13:00:20 -070029MOJO_STATIC_ASSERT(MOJO_ALIGNOF(int64_t) == 8, "int64_t has weird alignment");
James Robinson646469d2014-10-03 15:33:28 -070030struct MOJO_ALIGNAS(8) MojoCreateMessagePipeOptions {
31 uint32_t struct_size;
32 MojoCreateMessagePipeOptionsFlags flags;
33};
James Robinsonbaf71d32014-10-08 13:00:20 -070034MOJO_STATIC_ASSERT(sizeof(MojoCreateMessagePipeOptions) == 8,
35 "MojoCreateMessagePipeOptions has wrong size");
James Robinson646469d2014-10-03 15:33:28 -070036
37// |MojoWriteMessageFlags|: Used to specify different modes to
38// |MojoWriteMessage()|.
39// |MOJO_WRITE_MESSAGE_FLAG_NONE| - No flags; default mode.
40
41typedef uint32_t MojoWriteMessageFlags;
42
James Robinson646469d2014-10-03 15:33:28 -070043#define MOJO_WRITE_MESSAGE_FLAG_NONE ((MojoWriteMessageFlags)0)
James Robinson646469d2014-10-03 15:33:28 -070044
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
52typedef uint32_t MojoReadMessageFlags;
53
James Robinson646469d2014-10-03 15:33:28 -070054#define MOJO_READ_MESSAGE_FLAG_NONE ((MojoReadMessageFlags)0)
55#define MOJO_READ_MESSAGE_FLAG_MAY_DISCARD ((MojoReadMessageFlags)1 << 0)
James Robinson646469d2014-10-03 15:33:28 -070056
Viet-Trung Luu4a43a9d2016-03-15 12:37:15 -070057MOJO_BEGIN_EXTERN_C
James Robinson646469d2014-10-03 15:33:28 -070058
Viet-Trung Luu51cb5052016-03-18 13:48:00 -070059// |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 Robinson646469d2014-10-03 15:33:28 -070062//
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 Luu67c8e972016-05-10 12:29:59 -070066// 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 Robinson646469d2014-10-03 15:33:28 -070070//
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 Luufaf1aaa2015-10-23 17:01:25 -070077MojoResult MojoCreateMessagePipe(
Viet-Trung Luu51cb5052016-03-18 13:48:00 -070078 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 Robinson646469d2014-10-03 15:33:28 -070082
Viet-Trung Luu51cb5052016-03-18 13:48:00 -070083// |MojoWriteMessage()|: Writes a message to the message pipe endpoint given by
Viet-Trung Luu67c8e972016-05-10 12:29:59 -070084// |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 Robinson646469d2014-10-03 15:33:28 -070090//
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 Luu67c8e972016-05-10 12:29:59 -070093// 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 Robinson646469d2014-10-03 15:33:28 -070095//
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 Luu67c8e972016-05-10 12:29:59 -0700101// |MOJO_RESULT_PERMISSION_DENIED| if |message_pipe_handle| does not have the
Viet-Trung Luu862d13b2016-05-25 09:32:43 -0700102// |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 Robinson646469d2014-10-03 15:33:28 -0700104// |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 Gowand0438082015-01-30 16:36:00 -0800109// across processes), so this function may succeed even if the other
James Robinson646469d2014-10-03 15:33:28 -0700110// 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 Luuba1947c2016-03-21 16:56:27 -0700112// |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 Robinson646469d2014-10-03 15:33:28 -0700116//
Viet-Trung Luu862d13b2016-05-25 09:32:43 -0700117// 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 Robinson646469d2014-10-03 15:33:28 -0700126// 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 Luu51cb5052016-03-18 13:48:00 -0700128MojoResult 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 Robinson646469d2014-10-03 15:33:28 -0700134
Viet-Trung Luu67c8e972016-05-10 12:29:59 -0700135// |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 Robinson646469d2014-10-03 15:33:28 -0700142//
Geoffrey Gowand0438082015-01-30 16:36:00 -0800143// |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 Robinson646469d2014-10-03 15:33:28 -0700151//
Geoffrey Gowand0438082015-01-30 16:36:00 -0800152// |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 Robinson646469d2014-10-03 15:33:28 -0700155//
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 Luu67c8e972016-05-10 12:29:59 -0700160// |MOJO_RESULT_PERMISSION_DENIED| if |message_pipe_handle| does not have the
161// |MOJO_HANDLE_RIGHT_READ| right.
Geoffrey Gowand0438082015-01-30 16:36:00 -0800162// |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 Robinson646469d2014-10-03 15:33:28 -0700165// |MOJO_RESULT_SHOULD_WAIT| if no message was available to be read.
Viet-Trung Luuba1947c2016-03-21 16:56:27 -0700166// |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 Gowand0438082015-01-30 16:36:00 -0800169//
170// TODO(vtl): Reconsider the |MOJO_RESULT_RESOURCE_EXHAUSTED| error code; should
171// distinguish this from the hitting-system-limits case.
Viet-Trung Luu51cb5052016-03-18 13:48:00 -0700172MojoResult 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 Robinson646469d2014-10-03 15:33:28 -0700179
Viet-Trung Luu4a43a9d2016-03-15 12:37:15 -0700180MOJO_END_EXTERN_C
James Robinson646469d2014-10-03 15:33:28 -0700181
182#endif // MOJO_PUBLIC_C_SYSTEM_MESSAGE_PIPE_H_