OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 Mojo system handle-related declarations/definitions. | |
6 // | |
7 // Note: This header should be compilable as C. | |
8 | |
9 #ifndef MOJO_PUBLIC_C_INCLUDE_MOJO_SYSTEM_HANDLE_H_ | |
10 #define MOJO_PUBLIC_C_INCLUDE_MOJO_SYSTEM_HANDLE_H_ | |
11 | |
12 #include <mojo/macros.h> | |
13 #include <mojo/result.h> | |
14 #include <stdint.h> | |
15 | |
16 // |MojoHandle|: Handles to Mojo objects. | |
17 // |MOJO_HANDLE_INVALID| - A value that is never a valid handle. | |
18 | |
19 typedef uint32_t MojoHandle; | |
20 | |
21 #define MOJO_HANDLE_INVALID ((MojoHandle)0) | |
22 | |
23 // |MojoHandleRights|: Rights ("permissions") for handles. Each handle has a | |
24 // bitset of rights flags, which subsequently only be reduced. See each function | |
25 // for details about the right(s) required (some, like |MojoClose()|, require no | |
26 // rights). | |
27 // |MOJO_HANDLE_RIGHT_NONE| - No rights. | |
28 // |MOJO_HANDLE_RIGHT_DUPLICATE| - Right to duplicate a handle. | |
29 // |MOJO_HANDLE_RIGHT_TRANSFER| - Right to transfer a handle (e.g., include in | |
30 // a message). | |
31 // |MOJO_HANDLE_RIGHT_READ| - Right to "read" from the handle (e.g., read a | |
32 // message). | |
33 // |MOJO_HANDLE_RIGHT_WRITE| - Right to "write" to the handle (e.g., write a | |
34 // message). | |
35 // |MOJO_HANDLE_RIGHT_GET_OPTIONS| - Right to get a handle's options. | |
36 // |MOJO_HANDLE_RIGHT_SET_OPTIONS| - Right to set a handle's options. | |
37 // |MOJO_HANDLE_RIGHT_MAP_READABLE| - Right to "map" a (e.g., buffer) handle | |
38 // as readable memory. | |
39 // |MOJO_HANDLE_RIGHT_MAP_WRITABLE| - Right to "map" a (e.g., buffer) handle | |
40 // as writable memory. | |
41 // |MOJO_HANDLE_RIGHT_MAP_EXECUTABLE| - Right to "map" a (e.g., buffer) handle | |
42 // as executable memory. | |
43 // | |
44 // TODO(vtl): Add rights support/checking to existing handle types. | |
45 | |
46 typedef uint32_t MojoHandleRights; | |
47 | |
48 #define MOJO_HANDLE_RIGHT_NONE ((MojoHandleRights)0) | |
49 #define MOJO_HANDLE_RIGHT_DUPLICATE ((MojoHandleRights)1 << 0) | |
50 #define MOJO_HANDLE_RIGHT_TRANSFER ((MojoHandleRights)1 << 1) | |
51 #define MOJO_HANDLE_RIGHT_READ ((MojoHandleRights)1 << 2) | |
52 #define MOJO_HANDLE_RIGHT_WRITE ((MojoHandleRights)1 << 3) | |
53 #define MOJO_HANDLE_RIGHT_GET_OPTIONS ((MojoHandleRights)1 << 4) | |
54 #define MOJO_HANDLE_RIGHT_SET_OPTIONS ((MojoHandleRights)1 << 5) | |
55 #define MOJO_HANDLE_RIGHT_MAP_READABLE ((MojoHandleRights)1 << 6) | |
56 #define MOJO_HANDLE_RIGHT_MAP_WRITABLE ((MojoHandleRights)1 << 7) | |
57 #define MOJO_HANDLE_RIGHT_MAP_EXECUTABLE ((MojoHandleRights)1 << 8) | |
58 | |
59 // |MojoHandleSignals|: Used to specify signals that can be waited on for a | |
60 // handle (and which can be triggered), e.g., the ability to read or write to | |
61 // the handle. | |
62 // |MOJO_HANDLE_SIGNAL_NONE| - No flags. |MojoWait()|, etc. will return | |
63 // |MOJO_RESULT_FAILED_PRECONDITION| if you attempt to wait on this. | |
64 // |MOJO_HANDLE_SIGNAL_READABLE| - Can read (e.g., a message) from the handle. | |
65 // |MOJO_HANDLE_SIGNAL_WRITABLE| - Can write (e.g., a message) to the handle. | |
66 // |MOJO_HANDLE_SIGNAL_PEER_CLOSED| - The peer handle is closed. | |
67 // |MOJO_HANDLE_SIGNAL_READ_THRESHOLD| - Can read a certain amount of data | |
68 // from the handle (e.g., a data pipe consumer; see | |
69 // |MojoDataPipeConsumerOptions|). | |
70 | |
71 typedef uint32_t MojoHandleSignals; | |
72 | |
73 #define MOJO_HANDLE_SIGNAL_NONE ((MojoHandleSignals)0) | |
74 #define MOJO_HANDLE_SIGNAL_READABLE ((MojoHandleSignals)1 << 0) | |
75 #define MOJO_HANDLE_SIGNAL_WRITABLE ((MojoHandleSignals)1 << 1) | |
76 #define MOJO_HANDLE_SIGNAL_PEER_CLOSED ((MojoHandleSignals)1 << 2) | |
77 #define MOJO_HANDLE_SIGNAL_READ_THRESHOLD ((MojoHandleSignals)1 << 3) | |
78 #define MOJO_HANDLE_SIGNAL_WRITE_THRESHOLD ((MojoHandleSignals)1 << 4) | |
79 | |
80 // |MojoHandleSignalsState|: Returned by wait functions to indicate the | |
81 // signaling state of handles. Members are as follows: | |
82 // - |satisfied signals|: Bitmask of signals that were satisfied at some time | |
83 // before the call returned. | |
84 // - |satisfiable signals|: These are the signals that are possible to | |
85 // satisfy. For example, if the return value was | |
86 // |MOJO_RESULT_FAILED_PRECONDITION|, you can use this field to | |
87 // determine which, if any, of the signals can still be satisfied. | |
88 // Note: This struct is not extensible (and only has 32-bit quantities), so it's | |
89 // 32-bit-aligned. | |
90 | |
91 MOJO_STATIC_ASSERT(MOJO_ALIGNOF(uint32_t) == 4, "uint32_t has weird alignment"); | |
92 struct MOJO_ALIGNAS(4) MojoHandleSignalsState { | |
93 MojoHandleSignals satisfied_signals; | |
94 MojoHandleSignals satisfiable_signals; | |
95 }; | |
96 MOJO_STATIC_ASSERT(sizeof(struct MojoHandleSignalsState) == 8, | |
97 "MojoHandleSignalsState has wrong size"); | |
98 | |
99 MOJO_BEGIN_EXTERN_C | |
100 | |
101 // |MojoClose()|: Closes the given |handle|. | |
102 // | |
103 // Returns: | |
104 // |MOJO_RESULT_OK| on success. | |
105 // |MOJO_RESULT_INVALID_ARGUMENT| if |handle| is not a valid handle. | |
106 // |MOJO_RESULT_BUSY| if |handle| is currently in use in some transaction | |
107 // (that, e.g., may result in it being invalidated, such as being sent in | |
108 // a message). | |
109 // | |
110 // Concurrent operations on |handle| may succeed (or fail as usual) if they | |
111 // happen before the close, be cancelled with result |MOJO_RESULT_CANCELLED| if | |
112 // they properly overlap (this is likely the case with |MojoWait()|, etc.), or | |
113 // fail with |MOJO_RESULT_INVALID_ARGUMENT| if they happen after. | |
114 MojoResult MojoClose(MojoHandle handle); // In. | |
115 | |
116 // |MojoGetRights()|: Gets the given |handle|'s rights. | |
117 // | |
118 // On success, |*rights| (|rights| must be non-null) will be set to |handle|'s | |
119 // rights. | |
120 // | |
121 // Returns: | |
122 // |MOJO_RESULT_OK| on success. | |
123 // |MOJO_RESULT_INVALID_ARGUMENT| if |handle| is not a valid handle. | |
124 // |MOJO_RESULT_BUSY| if |handle| is currently in use in some transaction | |
125 // (that, e.g., may result in it being invalidated, such as being sent in | |
126 // a message). | |
127 MojoResult MojoGetRights(MojoHandle handle, MojoHandleRights* rights); // Out. | |
128 | |
129 // |MojoReplaceHandleWithReducedRights()|: Replaces |handle| with an equivalent | |
130 // one with reduced rights. | |
131 // | |
132 // On success, |*replacement_handle| will be a handle that is equivalent to | |
133 // |handle| (before the call), but with: | |
134 // | |
135 // replacement handle rights = current rights & ~rights_to_remove. | |
136 // | |
137 // |handle| will be invalidated and any ongoing two-phase operations (e.g., for | |
138 // data pipes) on |handle| will be aborted. | |
139 // | |
140 // On failure, |handle| will remain valid and unchanged (with any ongoing | |
141 // two-phase operations undisturbed) and |*replacement_handle| will not be set. | |
142 // | |
143 // Note that it is not an error to "remove" rights that the handle does not | |
144 // (currently) possess. | |
145 // | |
146 // Returns: | |
147 // |MOJO_RESULT_OK| on success. | |
148 // |MOJO_RESULT_INVALID_ARGUMENT| if |handle| is not a valid handle. | |
149 // |MOJO_RESULT_RESOURCE_EXHAUSTED| if a process/system/quota/etc. limit has | |
150 // been reached. | |
151 // |MOJO_RESULT_BUSY| if |handle| is currently in use in some transaction | |
152 // (that, e.g., may result in it being invalidated, such as being sent in | |
153 // a message). | |
154 MojoResult MojoReplaceHandleWithReducedRights( | |
155 MojoHandle handle, | |
156 MojoHandleRights rights_to_remove, | |
157 MojoHandle* replacement_handle); // Out. | |
158 | |
159 // |MojoDuplicateHandleWithReducedRights()|: Duplicates |handle| to a new handle | |
160 // with reduced rights. This requires |handle| to have the | |
161 // |MOJO_HANDLE_RIGHT_DUPLICATE| (note that some handle types may never have | |
162 // this right). | |
163 // | |
164 // The rights for the new handle are determined as in | |
165 // |MojoReplaceHandleWithReducedRights()|. That is, on success: | |
166 // | |
167 // new handle rights = original handle rights & ~rights_to_remove. | |
168 // | |
169 // Returns: | |
170 // |MOJO_RESULT_OK| on success. | |
171 // |MOJO_RESULT_INVALID_ARGUMENT| if |handle| is not a valid handle. | |
172 // |MOJO_RESULT_PERMISSION_DENIED| if |handle| does not have the | |
173 // |MOJO_HANDLE_RIGHT_DUPLICATE| right. | |
174 // |MOJO_RESULT_RESOURCE_EXHAUSTED| if a process/system/quota/etc. limit has | |
175 // been reached. | |
176 // |MOJO_RESULT_BUSY| if |handle| is currently in use in some transaction | |
177 // (that, e.g., may result in it being invalidated, such as being sent in | |
178 // a message). | |
179 MojoResult MojoDuplicateHandleWithReducedRights( | |
180 MojoHandle handle, | |
181 MojoHandleRights rights_to_remove, | |
182 MojoHandle* new_handle); // Out. | |
183 | |
184 // |MojoDuplicateHandle()|: Duplicates |handle| to a new handle with equivalent | |
185 // rights. This requires |handle| to have the |MOJO_HANDLE_RIGHT_DUPLICATE|. | |
186 // This is equivalent to |MojoDuplicateHandleWithReducedRights(handle, | |
187 // MOJO_HANDLE_RIGHT_NONE, new_handle)|. | |
188 // | |
189 // Returns: | |
190 // |MOJO_RESULT_OK| on success. | |
191 // |MOJO_RESULT_INVALID_ARGUMENT| if |handle| is not a valid handle. | |
192 // |MOJO_RESULT_PERMISSION_DENIED| if |handle| does not have the | |
193 // |MOJO_HANDLE_RIGHT_DUPLICATE| right. | |
194 // |MOJO_RESULT_RESOURCE_EXHAUSTED| if a process/system/quota/etc. limit has | |
195 // been reached. | |
196 // |MOJO_RESULT_BUSY| if |handle| is currently in use in some transaction | |
197 // (that, e.g., may result in it being invalidated, such as being sent in | |
198 // a message). | |
199 MojoResult MojoDuplicateHandle(MojoHandle handle, | |
200 MojoHandle* new_handle); // Out. | |
201 | |
202 MOJO_END_EXTERN_C | |
203 | |
204 #endif // MOJO_PUBLIC_C_INCLUDE_MOJO_SYSTEM_HANDLE_H_ | |
OLD | NEW |