2 * Copyright (c) 2013-2014 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
21 #ifndef __OS_VOUCHER_PRIVATE__
22 #define __OS_VOUCHER_PRIVATE__
25 #include <os/object.h>
27 #define OS_VOUCHER_SPI_VERSION 20150630
29 #if OS_VOUCHER_WEAK_IMPORT
30 #define OS_VOUCHER_EXPORT OS_EXPORT OS_WEAK_IMPORT
32 #define OS_VOUCHER_EXPORT OS_EXPORT
38 * @group Voucher Transport SPI
39 * SPI intended for clients that need to transport vouchers.
46 * Vouchers are immutable sets of key/value attributes that can be adopted on a
47 * thread in the current process or sent to another process.
50 * Voucher objects are os_objects (c.f. <os/object.h>). They are memory-managed
51 * with the os_retain()/os_release() functions or -[retain]/-[release] methods.
53 #if OS_OBJECT_USE_OBJC
54 OS_OBJECT_DECL(voucher
);
56 typedef struct voucher_s
*voucher_t
;
60 * @function voucher_adopt
63 * Adopt the specified voucher on the current thread and return the voucher
64 * that had been adopted previously.
67 * Adopted vouchers are automatically carried forward by the system to other
68 * threads and processes (across IPC).
70 * Consumes a reference to the specified voucher.
71 * Returns a reference to the previous voucher.
74 * The voucher object to adopt on the current thread.
77 * The previously adopted voucher object.
79 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
80 OS_VOUCHER_EXPORT OS_OBJECT_RETURNS_RETAINED OS_WARN_RESULT_NEEDS_RELEASE
83 voucher_adopt(voucher_t voucher OS_OBJECT_CONSUMED
);
86 * @function voucher_copy
89 * Returns a reference to the voucher that had been adopted previously on the
90 * current thread (or carried forward by the system).
93 * The currently adopted voucher object.
95 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
96 OS_VOUCHER_EXPORT OS_OBJECT_RETURNS_RETAINED OS_WARN_RESULT OS_NOTHROW
101 * @function voucher_copy_without_importance
104 * Returns a reference to a voucher object with all the properties of the
105 * voucher that had been adopted previously on the current thread, but
106 * without the importance properties that are frequently attached to vouchers
107 * carried with IPC requests. Importance properties may elevate the scheduling
108 * of threads that adopt or retain the voucher while they service the request.
109 * See xpc_transaction_begin(3) for further details on importance.
112 * A copy of the currently adopted voucher object, with importance removed.
114 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
115 OS_VOUCHER_EXPORT OS_OBJECT_RETURNS_RETAINED OS_WARN_RESULT OS_NOTHROW
117 voucher_copy_without_importance(void);
120 * @function voucher_replace_default_voucher
123 * Replace process attributes of default voucher (used for IPC by this process
124 * when no voucher is adopted on the sending thread) with the process attributes
125 * of the voucher adopted on the current thread.
128 * This allows a daemon to indicate from the context of an incoming IPC request
129 * that all future outgoing IPC from the process should be marked as acting
130 * "on behalf of" the sending process of the current IPC request (as long as the
131 * thread sending that outgoing IPC is not itself in the direct context of an
132 * IPC request, i.e. no voucher is adopted).
134 * If no voucher is adopted on the current thread or the current voucher does
135 * not contain any process attributes, the default voucher is reset to the
136 * default process attributes for the current process.
138 * CAUTION: Do NOT use this SPI without contacting the Darwin Runtime team.
140 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
141 OS_VOUCHER_EXPORT OS_NOTHROW
143 voucher_replace_default_voucher(void);
146 * @function voucher_decrement_importance_count4CF
149 * Decrement external importance count of the mach voucher in the specified
153 * This is only intended for use by CoreFoundation to explicitly manage the
154 * App Nap state of an application following receiption of a de-nap IPC message.
156 * CAUTION: Do NOT use this SPI without contacting the Darwin Runtime team.
158 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
159 OS_VOUCHER_EXPORT OS_NOTHROW
161 voucher_decrement_importance_count4CF(voucher_t voucher
);
164 * @group Voucher dispatch block SPI
167 #ifndef __DISPATCH_BUILDING_DISPATCH__
168 #include <dispatch/dispatch.h>
169 #endif /* !__DISPATCH_BUILDING_DISPATCH__ */
172 * @typedef dispatch_block_flags_t
173 * SPI Flags to pass to the dispatch_block_create* functions.
175 * @const DISPATCH_BLOCK_NO_VOUCHER
176 * Flag indicating that a dispatch block object should not be assigned a voucher
177 * object. If invoked directly, the block object will be executed with the
178 * voucher adopted on the calling thread. If the block object is submitted to a
179 * queue, this replaces the default behavior of associating the submitted block
180 * instance with the voucher adopted at the time of submission.
181 * This flag is ignored if a specific voucher object is assigned with the
182 * dispatch_block_create_with_voucher* functions, and is equivalent to passing
183 * the NULL voucher to these functions.
185 #define DISPATCH_BLOCK_NO_VOUCHER (0x40)
188 * @function dispatch_block_create_with_voucher
191 * Create a new dispatch block object on the heap from an existing block and
192 * the given flags, and assign it the specified voucher object.
195 * The provided block is Block_copy'ed to the heap, it and the specified voucher
196 * object are retained by the newly created dispatch block object.
198 * The returned dispatch block object is intended to be submitted to a dispatch
199 * queue with dispatch_async() and related functions, but may also be invoked
200 * directly. Both operations can be performed an arbitrary number of times but
201 * only the first completed execution of a dispatch block object can be waited
202 * on with dispatch_block_wait() or observed with dispatch_block_notify().
204 * The returned dispatch block will be executed with the specified voucher
205 * adopted for the duration of the block body. If the NULL voucher is passed,
206 * the block will be executed with the voucher adopted on the calling thread, or
207 * with no voucher if the DISPATCH_BLOCK_DETACHED flag was also provided.
209 * If the returned dispatch block object is submitted to a dispatch queue, the
210 * submitted block instance will be associated with the QOS class current at the
211 * time of submission, unless one of the following flags assigned a specific QOS
212 * class (or no QOS class) at the time of block creation:
213 * - DISPATCH_BLOCK_ASSIGN_CURRENT
214 * - DISPATCH_BLOCK_NO_QOS_CLASS
215 * - DISPATCH_BLOCK_DETACHED
216 * The QOS class the block object will be executed with also depends on the QOS
217 * class assigned to the queue and which of the following flags was specified or
219 * - DISPATCH_BLOCK_INHERIT_QOS_CLASS (default for asynchronous execution)
220 * - DISPATCH_BLOCK_ENFORCE_QOS_CLASS (default for synchronous execution)
221 * See description of dispatch_block_flags_t for details.
223 * If the returned dispatch block object is submitted directly to a serial queue
224 * and is configured to execute with a specific QOS class, the system will make
225 * a best effort to apply the necessary QOS overrides to ensure that blocks
226 * submitted earlier to the serial queue are executed at that same QOS class or
230 * Configuration flags for the block object.
231 * Passing a value that is not a bitwise OR of flags from dispatch_block_flags_t
232 * results in NULL being returned.
235 * A voucher object or NULL. Passing NULL is equivalent to specifying the
236 * DISPATCH_BLOCK_NO_VOUCHER flag.
239 * The block to create the dispatch block object from.
242 * The newly created dispatch block object, or NULL.
243 * When not building with Objective-C ARC, must be released with a -[release]
244 * message or the Block_release() function.
246 __OSX_AVAILABLE_STARTING(__MAC_10_10
, __IPHONE_8_0
)
247 DISPATCH_EXPORT DISPATCH_NONNULL3 DISPATCH_RETURNS_RETAINED_BLOCK
248 DISPATCH_WARN_RESULT DISPATCH_NOTHROW
250 dispatch_block_create_with_voucher(dispatch_block_flags_t flags
,
251 voucher_t voucher
, dispatch_block_t block
);
254 * @function dispatch_block_create_with_voucher_and_qos_class
257 * Create a new dispatch block object on the heap from an existing block and
258 * the given flags, and assign it the specified voucher object, QOS class and
262 * The provided block is Block_copy'ed to the heap, it and the specified voucher
263 * object are retained by the newly created dispatch block object.
265 * The returned dispatch block object is intended to be submitted to a dispatch
266 * queue with dispatch_async() and related functions, but may also be invoked
267 * directly. Both operations can be performed an arbitrary number of times but
268 * only the first completed execution of a dispatch block object can be waited
269 * on with dispatch_block_wait() or observed with dispatch_block_notify().
271 * The returned dispatch block will be executed with the specified voucher
272 * adopted for the duration of the block body. If the NULL voucher is passed,
273 * the block will be executed with the voucher adopted on the calling thread, or
274 * with no voucher if the DISPATCH_BLOCK_DETACHED flag was also provided.
276 * If invoked directly, the returned dispatch block object will be executed with
277 * the assigned QOS class as long as that does not result in a lower QOS class
278 * than what is current on the calling thread.
280 * If the returned dispatch block object is submitted to a dispatch queue, the
281 * QOS class it will be executed with depends on the QOS class assigned to the
282 * block, the QOS class assigned to the queue and which of the following flags
283 * was specified or defaulted to:
284 * - DISPATCH_BLOCK_INHERIT_QOS_CLASS: default for asynchronous execution
285 * - DISPATCH_BLOCK_ENFORCE_QOS_CLASS: default for synchronous execution
286 * See description of dispatch_block_flags_t for details.
288 * If the returned dispatch block object is submitted directly to a serial queue
289 * and is configured to execute with a specific QOS class, the system will make
290 * a best effort to apply the necessary QOS overrides to ensure that blocks
291 * submitted earlier to the serial queue are executed at that same QOS class or
295 * Configuration flags for the block object.
296 * Passing a value that is not a bitwise OR of flags from dispatch_block_flags_t
297 * results in NULL being returned.
300 * A voucher object or NULL. Passing NULL is equivalent to specifying the
301 * DISPATCH_BLOCK_NO_VOUCHER flag.
305 * - QOS_CLASS_USER_INTERACTIVE
306 * - QOS_CLASS_USER_INITIATED
307 * - QOS_CLASS_DEFAULT
308 * - QOS_CLASS_UTILITY
309 * - QOS_CLASS_BACKGROUND
310 * - QOS_CLASS_UNSPECIFIED
311 * Passing QOS_CLASS_UNSPECIFIED is equivalent to specifying the
312 * DISPATCH_BLOCK_NO_QOS_CLASS flag. Passing any other value results in NULL
315 * @param relative_priority
316 * A relative priority within the QOS class. This value is a negative
317 * offset from the maximum supported scheduler priority for the given class.
318 * Passing a value greater than zero or less than QOS_MIN_RELATIVE_PRIORITY
319 * results in NULL being returned.
322 * The block to create the dispatch block object from.
325 * The newly created dispatch block object, or NULL.
326 * When not building with Objective-C ARC, must be released with a -[release]
327 * message or the Block_release() function.
329 __OSX_AVAILABLE_STARTING(__MAC_10_10
, __IPHONE_8_0
)
330 DISPATCH_EXPORT DISPATCH_NONNULL5 DISPATCH_RETURNS_RETAINED_BLOCK
331 DISPATCH_WARN_RESULT DISPATCH_NOTHROW
333 dispatch_block_create_with_voucher_and_qos_class(dispatch_block_flags_t flags
,
334 voucher_t voucher
, dispatch_qos_class_t qos_class
,
335 int relative_priority
, dispatch_block_t block
);
338 * @group Voucher dispatch queue SPI
342 * @function dispatch_queue_create_with_accounting_override_voucher
345 * Creates a new dispatch queue with an accounting override voucher created
346 * from the specified voucher.
349 * See dispatch_queue_create() headerdoc for generic details on queue creation.
351 * The resource accounting attributes of the specified voucher are extracted
352 * and used to create an accounting override voucher for the new queue.
354 * Every block executed on the returned queue will initially have this override
355 * voucher adopted, any voucher automatically associated with or explicitly
356 * assigned to the block will NOT be used and released immediately before block
359 * The accounting override voucher will be automatically propagated to any
360 * asynchronous work generated from the queue following standard voucher
363 * NOTE: this SPI should only be used in special circumstances when a subsystem
364 * has complete control over all workitems submitted to a queue (e.g. no client
365 * block is ever submitted to the queue) and if and only if such queues have a
366 * one-to-one mapping with resource accounting identities.
368 * CAUTION: use of this SPI represents a potential voucher propagation hole. It
369 * is the responsibility of the caller to ensure that any callbacks into client
370 * code from the queue have the correct client voucher applied (rather than the
371 * automatically propagated accounting override voucher), e.g. by use of the
372 * dispatch_block_create() API to capture client state at the time the callback
376 * A string label to attach to the queue.
377 * This parameter is optional and may be NULL.
380 * DISPATCH_QUEUE_SERIAL, DISPATCH_QUEUE_CONCURRENT, or the result of a call to
381 * the function dispatch_queue_attr_make_with_qos_class().
384 * A voucher whose resource accounting attributes are used to create the
385 * accounting override voucher attached to the queue.
388 * The newly created dispatch queue.
390 __OSX_AVAILABLE_STARTING(__MAC_10_11
,__IPHONE_9_0
)
391 DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
394 dispatch_queue_create_with_accounting_override_voucher(const char *label
,
395 dispatch_queue_attr_t attr
, voucher_t voucher
);
398 * @group Voucher Mach SPI
399 * SPI intended for clients that need to interact with mach messages or mach
400 * voucher ports directly.
403 #include <mach/mach.h>
406 * @function voucher_create_with_mach_msg
409 * Creates a new voucher object from a mach message carrying a mach voucher port
412 * Ownership of the mach voucher port in the message is transfered to the new
413 * voucher object and the message header mach voucher field is cleared.
416 * The mach message to query.
419 * The newly created voucher object or NULL if the message was not carrying a
422 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
423 OS_VOUCHER_EXPORT OS_OBJECT_RETURNS_RETAINED OS_WARN_RESULT OS_NOTHROW
425 voucher_create_with_mach_msg(mach_msg_header_t
*msg
);
428 * @group Voucher Persona SPI
429 * SPI intended for clients that need to interact with personas.
432 #if __has_include(<bank/bank_types.h>)
433 #include <bank/bank_types.h>
435 #if __has_include(<sys/persona.h>)
436 #include <sys/persona.h>
439 struct proc_persona_info
;
442 * @function voucher_get_current_persona
445 * Retrieve the persona identifier of the 'originator' process for the current
449 * Retrieve the persona identifier of the ’originator’ process possibly stored
450 * in the PERSONA_TOKEN attribute of the currently adopted voucher.
452 * If the thread has not adopted a voucher, or the current voucher does not
453 * contain a PERSONA_TOKEN attribute, this function returns the persona
454 * identifier of the current process.
456 * If the process is not running under a persona, then this returns
460 * The persona identifier of the 'originator' process for the current voucher,
461 * or the persona identifier of the current process
464 __OSX_AVAILABLE_STARTING(__MAC_NA
,__IPHONE_9_2
)
465 OS_VOUCHER_EXPORT OS_WARN_RESULT OS_NOTHROW
467 voucher_get_current_persona(void);
470 * @function voucher_get_current_persona_originator_info
473 * Retrieve the ’originator’ process persona info for the currently adopted
477 * If there is no currently adopted voucher, or no PERSONA_TOKEN attribute
478 * in that voucher, this function fails.
480 * @param persona_info
481 * The proc_persona_info structure to fill in case of success
484 * 0 on success: currently adopted voucher has a PERSONA_TOKEN
485 * -1 on failure: persona_info is untouched/uninitialized
487 __OSX_AVAILABLE_STARTING(__MAC_NA
,__IPHONE_9_2
)
488 OS_VOUCHER_EXPORT OS_WARN_RESULT OS_NOTHROW OS_NONNULL1
490 voucher_get_current_persona_originator_info(
491 struct proc_persona_info
*persona_info
);
494 * @function voucher_get_current_persona_proximate_info
497 * Retrieve the ’proximate’ process persona info for the currently adopted
501 * If there is no currently adopted voucher, or no PERSONA_TOKEN attribute
502 * in that voucher, this function fails.
504 * @param persona_info
505 * The proc_persona_info structure to fill in case of success
508 * 0 on success: currently adopted voucher has a PERSONA_TOKEN
509 * -1 on failure: persona_info is untouched/uninitialized
511 __OSX_AVAILABLE_STARTING(__MAC_NA
,__IPHONE_9_2
)
512 OS_VOUCHER_EXPORT OS_WARN_RESULT OS_NOTHROW OS_NONNULL1
514 voucher_get_current_persona_proximate_info(
515 struct proc_persona_info
*persona_info
);
519 #endif // __OS_VOUCHER_PRIVATE__
521 #if (OS_VOUCHER_ACTIVITY_SPI || OS_VOUCHER_ACTIVITY_BUFFER_SPI) && \
522 !defined(__DISPATCH_BUILDING_DISPATCH__) && \
523 !defined(__OS_VOUCHER_ACTIVITY_PRIVATE__)
524 #include <os/voucher_activity_private.h>