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__
27 #if __has_include(<mach/mach.h>)
28 #include <os/object.h>
29 #include <mach/mach.h>
31 #if __has_include(<bank/bank_types.h>)
32 #include <bank/bank_types.h>
34 #if __has_include(<sys/persona.h>)
35 #include <sys/persona.h>
38 #ifndef __DISPATCH_BUILDING_DISPATCH__
39 #include <dispatch/dispatch.h>
40 #endif /* !__DISPATCH_BUILDING_DISPATCH__ */
42 #define OS_VOUCHER_SPI_VERSION 20150630
44 #if OS_VOUCHER_WEAK_IMPORT
45 #define OS_VOUCHER_EXPORT OS_EXPORT OS_WEAK_IMPORT
47 #define OS_VOUCHER_EXPORT OS_EXPORT
50 DISPATCH_ASSUME_NONNULL_BEGIN
55 * @group Voucher Transport SPI
56 * SPI intended for clients that need to transport vouchers.
63 * Vouchers are immutable sets of key/value attributes that can be adopted on a
64 * thread in the current process or sent to another process.
67 * Voucher objects are os_objects (c.f. <os/object.h>). They are memory-managed
68 * with the os_retain()/os_release() functions or -[retain]/-[release] methods.
70 OS_OBJECT_DECL_CLASS(voucher
);
74 * Represents the empty base voucher with no attributes.
76 #define VOUCHER_NULL ((voucher_t)0)
78 * @const VOUCHER_INVALID
79 * Represents an invalid voucher
81 #define VOUCHER_INVALID ((voucher_t)-1)
84 * @function voucher_adopt
87 * Adopt the specified voucher on the current thread and return the voucher
88 * that had been adopted previously.
91 * Adopted vouchers are automatically carried forward by the system to other
92 * threads and processes (across IPC).
94 * Consumes a reference to the specified voucher.
95 * Returns a reference to the previous voucher.
98 * The voucher object to adopt on the current thread.
101 * The previously adopted voucher object.
103 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
104 OS_VOUCHER_EXPORT OS_OBJECT_RETURNS_RETAINED OS_WARN_RESULT_NEEDS_RELEASE
107 voucher_adopt(voucher_t _Nullable voucher OS_OBJECT_CONSUMED
);
110 * @function voucher_copy
113 * Returns a reference to the voucher that had been adopted previously on the
114 * current thread (or carried forward by the system).
117 * The currently adopted voucher object.
119 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
120 OS_VOUCHER_EXPORT OS_OBJECT_RETURNS_RETAINED OS_WARN_RESULT OS_NOTHROW
125 * @function voucher_copy_without_importance
128 * Returns a reference to a voucher object with all the properties of the
129 * voucher that had been adopted previously on the current thread, but
130 * without the importance properties that are frequently attached to vouchers
131 * carried with IPC requests. Importance properties may elevate the scheduling
132 * of threads that adopt or retain the voucher while they service the request.
133 * See xpc_transaction_begin(3) for further details on importance.
136 * A copy of the currently adopted voucher object, with importance removed.
138 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
139 OS_VOUCHER_EXPORT OS_OBJECT_RETURNS_RETAINED OS_WARN_RESULT OS_NOTHROW
141 voucher_copy_without_importance(void);
144 * @function voucher_replace_default_voucher
147 * Replace process attributes of default voucher (used for IPC by this process
148 * when no voucher is adopted on the sending thread) with the process attributes
149 * of the voucher adopted on the current thread.
152 * This allows a daemon to indicate from the context of an incoming IPC request
153 * that all future outgoing IPC from the process should be marked as acting
154 * "on behalf of" the sending process of the current IPC request (as long as the
155 * thread sending that outgoing IPC is not itself in the direct context of an
156 * IPC request, i.e. no voucher is adopted).
158 * If no voucher is adopted on the current thread or the current voucher does
159 * not contain any process attributes, the default voucher is reset to the
160 * default process attributes for the current process.
162 * CAUTION: Do NOT use this SPI without contacting the Darwin Runtime team.
164 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
165 OS_VOUCHER_EXPORT OS_NOTHROW
167 voucher_replace_default_voucher(void);
170 * @function voucher_decrement_importance_count4CF
173 * Decrement external importance count of the mach voucher in the specified
177 * This is only intended for use by CoreFoundation to explicitly manage the
178 * App Nap state of an application following reception of a de-nap IPC message.
180 * CAUTION: Do NOT use this SPI without contacting the Darwin Runtime team.
182 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
183 OS_VOUCHER_EXPORT OS_NOTHROW
185 voucher_decrement_importance_count4CF(voucher_t _Nullable voucher
);
188 * @group Voucher dispatch block SPI
192 * @typedef dispatch_block_flags_t
193 * SPI Flags to pass to the dispatch_block_create* functions.
195 * @const DISPATCH_BLOCK_NO_VOUCHER
196 * Flag indicating that a dispatch block object should not be assigned a voucher
197 * object. If invoked directly, the block object will be executed with the
198 * voucher adopted on the calling thread. If the block object is submitted to a
199 * queue, this replaces the default behavior of associating the submitted block
200 * instance with the voucher adopted at the time of submission.
201 * This flag is ignored if a specific voucher object is assigned with the
202 * dispatch_block_create_with_voucher* functions, and is equivalent to passing
203 * the NULL voucher to these functions.
205 #define DISPATCH_BLOCK_NO_VOUCHER (0x40)
208 * @function dispatch_block_create_with_voucher
211 * Create a new dispatch block object on the heap from an existing block and
212 * the given flags, and assign it the specified voucher object.
215 * The provided block is Block_copy'ed to the heap, it and the specified voucher
216 * object are retained by the newly created dispatch block object.
218 * The returned dispatch block object is intended to be submitted to a dispatch
219 * queue with dispatch_async() and related functions, but may also be invoked
220 * directly. Both operations can be performed an arbitrary number of times but
221 * only the first completed execution of a dispatch block object can be waited
222 * on with dispatch_block_wait() or observed with dispatch_block_notify().
224 * The returned dispatch block will be executed with the specified voucher
225 * adopted for the duration of the block body. If the NULL voucher is passed,
226 * the block will be executed with the voucher adopted on the calling thread, or
227 * with no voucher if the DISPATCH_BLOCK_DETACHED flag was also provided.
229 * If the returned dispatch block object is submitted to a dispatch queue, the
230 * submitted block instance will be associated with the QOS class current at the
231 * time of submission, unless one of the following flags assigned a specific QOS
232 * class (or no QOS class) at the time of block creation:
233 * - DISPATCH_BLOCK_ASSIGN_CURRENT
234 * - DISPATCH_BLOCK_NO_QOS_CLASS
235 * - DISPATCH_BLOCK_DETACHED
236 * The QOS class the block object will be executed with also depends on the QOS
237 * class assigned to the queue and which of the following flags was specified or
239 * - DISPATCH_BLOCK_INHERIT_QOS_CLASS (default for asynchronous execution)
240 * - DISPATCH_BLOCK_ENFORCE_QOS_CLASS (default for synchronous execution)
241 * See description of dispatch_block_flags_t for details.
243 * If the returned dispatch block object is submitted directly to a serial queue
244 * and is configured to execute with a specific QOS class, the system will make
245 * a best effort to apply the necessary QOS overrides to ensure that blocks
246 * submitted earlier to the serial queue are executed at that same QOS class or
250 * Configuration flags for the block object.
251 * Passing a value that is not a bitwise OR of flags from dispatch_block_flags_t
252 * results in NULL being returned.
255 * A voucher object or NULL. Passing NULL is equivalent to specifying the
256 * DISPATCH_BLOCK_NO_VOUCHER flag.
259 * The block to create the dispatch block object from.
262 * The newly created dispatch block object, or NULL.
263 * When not building with Objective-C ARC, must be released with a -[release]
264 * message or the Block_release() function.
266 __OSX_AVAILABLE_STARTING(__MAC_10_10
, __IPHONE_8_0
)
267 DISPATCH_EXPORT DISPATCH_NONNULL3 DISPATCH_RETURNS_RETAINED_BLOCK
268 DISPATCH_WARN_RESULT DISPATCH_NOTHROW
270 dispatch_block_create_with_voucher(dispatch_block_flags_t flags
,
271 voucher_t _Nullable voucher
, dispatch_block_t block
);
274 * @function dispatch_block_create_with_voucher_and_qos_class
277 * Create a new dispatch block object on the heap from an existing block and
278 * the given flags, and assign it the specified voucher object, QOS class and
282 * The provided block is Block_copy'ed to the heap, it and the specified voucher
283 * object are retained by the newly created dispatch block object.
285 * The returned dispatch block object is intended to be submitted to a dispatch
286 * queue with dispatch_async() and related functions, but may also be invoked
287 * directly. Both operations can be performed an arbitrary number of times but
288 * only the first completed execution of a dispatch block object can be waited
289 * on with dispatch_block_wait() or observed with dispatch_block_notify().
291 * The returned dispatch block will be executed with the specified voucher
292 * adopted for the duration of the block body. If the NULL voucher is passed,
293 * the block will be executed with the voucher adopted on the calling thread, or
294 * with no voucher if the DISPATCH_BLOCK_DETACHED flag was also provided.
296 * If invoked directly, the returned dispatch block object will be executed with
297 * the assigned QOS class as long as that does not result in a lower QOS class
298 * than what is current on the calling thread.
300 * If the returned dispatch block object is submitted to a dispatch queue, the
301 * QOS class it will be executed with depends on the QOS class assigned to the
302 * block, the QOS class assigned to the queue and which of the following flags
303 * was specified or defaulted to:
304 * - DISPATCH_BLOCK_INHERIT_QOS_CLASS: default for asynchronous execution
305 * - DISPATCH_BLOCK_ENFORCE_QOS_CLASS: default for synchronous execution
306 * See description of dispatch_block_flags_t for details.
308 * If the returned dispatch block object is submitted directly to a serial queue
309 * and is configured to execute with a specific QOS class, the system will make
310 * a best effort to apply the necessary QOS overrides to ensure that blocks
311 * submitted earlier to the serial queue are executed at that same QOS class or
315 * Configuration flags for the block object.
316 * Passing a value that is not a bitwise OR of flags from dispatch_block_flags_t
317 * results in NULL being returned.
320 * A voucher object or NULL. Passing NULL is equivalent to specifying the
321 * DISPATCH_BLOCK_NO_VOUCHER flag.
325 * - QOS_CLASS_USER_INTERACTIVE
326 * - QOS_CLASS_USER_INITIATED
327 * - QOS_CLASS_DEFAULT
328 * - QOS_CLASS_UTILITY
329 * - QOS_CLASS_BACKGROUND
330 * - QOS_CLASS_UNSPECIFIED
331 * Passing QOS_CLASS_UNSPECIFIED is equivalent to specifying the
332 * DISPATCH_BLOCK_NO_QOS_CLASS flag. Passing any other value results in NULL
335 * @param relative_priority
336 * A relative priority within the QOS class. This value is a negative
337 * offset from the maximum supported scheduler priority for the given class.
338 * Passing a value greater than zero or less than QOS_MIN_RELATIVE_PRIORITY
339 * results in NULL being returned.
342 * The block to create the dispatch block object from.
345 * The newly created dispatch block object, or NULL.
346 * When not building with Objective-C ARC, must be released with a -[release]
347 * message or the Block_release() function.
349 __OSX_AVAILABLE_STARTING(__MAC_10_10
, __IPHONE_8_0
)
350 DISPATCH_EXPORT DISPATCH_NONNULL5 DISPATCH_RETURNS_RETAINED_BLOCK
351 DISPATCH_WARN_RESULT DISPATCH_NOTHROW
353 dispatch_block_create_with_voucher_and_qos_class(dispatch_block_flags_t flags
,
354 voucher_t _Nullable voucher
, dispatch_qos_class_t qos_class
,
355 int relative_priority
, dispatch_block_t block
);
358 * @group Voucher dispatch queue SPI
362 * @function dispatch_queue_create_with_accounting_override_voucher
365 * Creates a new dispatch queue with an accounting override voucher created
366 * from the specified voucher.
369 * See dispatch_queue_create() headerdoc for generic details on queue creation.
371 * The resource accounting attributes of the specified voucher are extracted
372 * and used to create an accounting override voucher for the new queue.
374 * Every block executed on the returned queue will initially have this override
375 * voucher adopted, any voucher automatically associated with or explicitly
376 * assigned to the block will NOT be used and released immediately before block
379 * The accounting override voucher will be automatically propagated to any
380 * asynchronous work generated from the queue following standard voucher
383 * NOTE: this SPI should only be used in special circumstances when a subsystem
384 * has complete control over all workitems submitted to a queue (e.g. no client
385 * block is ever submitted to the queue) and if and only if such queues have a
386 * one-to-one mapping with resource accounting identities.
388 * CAUTION: use of this SPI represents a potential voucher propagation hole. It
389 * is the responsibility of the caller to ensure that any callbacks into client
390 * code from the queue have the correct client voucher applied (rather than the
391 * automatically propagated accounting override voucher), e.g. by use of the
392 * dispatch_block_create() API to capture client state at the time the callback
396 * A string label to attach to the queue.
397 * This parameter is optional and may be NULL.
400 * DISPATCH_QUEUE_SERIAL, DISPATCH_QUEUE_CONCURRENT, or the result of a call to
401 * the function dispatch_queue_attr_make_with_qos_class().
404 * A voucher whose resource accounting attributes are used to create the
405 * accounting override voucher attached to the queue.
408 * The newly created dispatch queue.
410 __OSX_AVAILABLE_STARTING(__MAC_10_11
,__IPHONE_9_0
)
411 DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
414 dispatch_queue_create_with_accounting_override_voucher(
415 const char *_Nullable label
,
416 dispatch_queue_attr_t _Nullable attr
,
417 voucher_t _Nullable voucher
);
419 #if __has_include(<mach/mach.h>)
421 * @group Voucher Mach SPI
422 * SPI intended for clients that need to interact with mach messages or mach
423 * voucher ports directly.
427 * @function voucher_create_with_mach_msg
430 * Creates a new voucher object from a mach message carrying a mach voucher port
433 * Ownership of the mach voucher port in the message is transfered to the new
434 * voucher object and the message header mach voucher field is cleared.
437 * The mach message to query.
440 * The newly created voucher object or NULL if the message was not carrying a
443 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
444 OS_VOUCHER_EXPORT OS_OBJECT_RETURNS_RETAINED OS_WARN_RESULT OS_NOTHROW
446 voucher_create_with_mach_msg(mach_msg_header_t
*msg
);
449 * @group Voucher Persona SPI
450 * SPI intended for clients that need to interact with personas.
453 struct proc_persona_info
;
456 * @function voucher_get_current_persona
459 * Retrieve the persona identifier of the 'originator' process for the current
463 * Retrieve the persona identifier of the ’originator’ process possibly stored
464 * in the PERSONA_TOKEN attribute of the currently adopted voucher.
466 * If the thread has not adopted a voucher, or the current voucher does not
467 * contain a PERSONA_TOKEN attribute, this function returns the persona
468 * identifier of the current process.
470 * If the process is not running under a persona, then this returns
474 * The persona identifier of the 'originator' process for the current voucher,
475 * or the persona identifier of the current process
478 __OSX_AVAILABLE_STARTING(__MAC_NA
,__IPHONE_9_2
)
479 OS_VOUCHER_EXPORT OS_WARN_RESULT OS_NOTHROW
481 voucher_get_current_persona(void);
484 * @function voucher_get_current_persona_originator_info
487 * Retrieve the ’originator’ process persona info for the currently adopted
491 * If there is no currently adopted voucher, or no PERSONA_TOKEN attribute
492 * in that voucher, this function fails.
494 * @param persona_info
495 * The proc_persona_info structure to fill in case of success
498 * 0 on success: currently adopted voucher has a PERSONA_TOKEN
499 * -1 on failure: persona_info is untouched/uninitialized
501 __OSX_AVAILABLE_STARTING(__MAC_NA
,__IPHONE_9_2
)
502 OS_VOUCHER_EXPORT OS_WARN_RESULT OS_NOTHROW OS_NONNULL1
504 voucher_get_current_persona_originator_info(
505 struct proc_persona_info
*persona_info
);
508 * @function voucher_get_current_persona_proximate_info
511 * Retrieve the ’proximate’ process persona info for the currently adopted
515 * If there is no currently adopted voucher, or no PERSONA_TOKEN attribute
516 * in that voucher, this function fails.
518 * @param persona_info
519 * The proc_persona_info structure to fill in case of success
522 * 0 on success: currently adopted voucher has a PERSONA_TOKEN
523 * -1 on failure: persona_info is untouched/uninitialized
525 __OSX_AVAILABLE_STARTING(__MAC_NA
,__IPHONE_9_2
)
526 OS_VOUCHER_EXPORT OS_WARN_RESULT OS_NOTHROW OS_NONNULL1
528 voucher_get_current_persona_proximate_info(
529 struct proc_persona_info
*persona_info
);
531 #endif // __has_include(<mach/mach.h>)
535 DISPATCH_ASSUME_NONNULL_END
537 #endif // __OS_VOUCHER_PRIVATE__
539 #if OS_VOUCHER_ACTIVITY_SPI
540 #include "voucher_activity_private.h"