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__
26 #include <os/availability.h>
28 #if __has_include(<mach/mach.h>)
29 #include <os/object.h>
30 #include <mach/mach.h>
32 #if __has_include(<bank/bank_types.h>)
33 #include <bank/bank_types.h>
35 #if __has_include(<sys/persona.h>)
36 #include <sys/persona.h>
39 #ifndef __DISPATCH_BUILDING_DISPATCH__
40 #include <dispatch/dispatch.h>
41 #endif /* !__DISPATCH_BUILDING_DISPATCH__ */
43 #define OS_VOUCHER_SPI_VERSION 20150630
45 #if OS_VOUCHER_WEAK_IMPORT
46 #define OS_VOUCHER_EXPORT OS_EXPORT OS_WEAK_IMPORT
48 #define OS_VOUCHER_EXPORT OS_EXPORT
51 DISPATCH_ASSUME_NONNULL_BEGIN
56 * @group Voucher Transport SPI
57 * SPI intended for clients that need to transport vouchers.
64 * Vouchers are immutable sets of key/value attributes that can be adopted on a
65 * thread in the current process or sent to another process.
68 * Voucher objects are os_objects (c.f. <os/object.h>). They are memory-managed
69 * with the os_retain()/os_release() functions or -[retain]/-[release] methods.
71 OS_OBJECT_DECL_CLASS(voucher
);
75 * Represents the empty base voucher with no attributes.
77 #define VOUCHER_NULL ((voucher_t)0)
79 * @const VOUCHER_INVALID
80 * Represents an invalid voucher
82 #define VOUCHER_INVALID ((voucher_t)-1)
85 * @function voucher_adopt
88 * Adopt the specified voucher on the current thread and return the voucher
89 * that had been adopted previously.
92 * Adopted vouchers are automatically carried forward by the system to other
93 * threads and processes (across IPC).
95 * Consumes a reference to the specified voucher.
96 * Returns a reference to the previous voucher.
99 * The voucher object to adopt on the current thread.
102 * The previously adopted voucher object.
104 API_AVAILABLE(macos(10.10), ios(8.0))
105 OS_VOUCHER_EXPORT OS_OBJECT_RETURNS_RETAINED OS_WARN_RESULT_NEEDS_RELEASE
108 voucher_adopt(voucher_t _Nullable voucher OS_OBJECT_CONSUMED
);
111 * @function voucher_copy
114 * Returns a reference to the voucher that had been adopted previously on the
115 * current thread (or carried forward by the system).
118 * The currently adopted voucher object.
120 API_AVAILABLE(macos(10.10), ios(8.0))
121 OS_VOUCHER_EXPORT OS_OBJECT_RETURNS_RETAINED OS_WARN_RESULT OS_NOTHROW
126 * @function voucher_copy_without_importance
129 * Returns a reference to a voucher object with all the properties of the
130 * voucher that had been adopted previously on the current thread, but
131 * without the importance properties that are frequently attached to vouchers
132 * carried with IPC requests. Importance properties may elevate the scheduling
133 * of threads that adopt or retain the voucher while they service the request.
134 * See xpc_transaction_begin(3) for further details on importance.
137 * A copy of the currently adopted voucher object, with importance removed.
139 API_AVAILABLE(macos(10.10), ios(8.0))
140 OS_VOUCHER_EXPORT OS_OBJECT_RETURNS_RETAINED OS_WARN_RESULT OS_NOTHROW
142 voucher_copy_without_importance(void);
145 * @function voucher_replace_default_voucher
148 * Replace process attributes of default voucher (used for IPC by this process
149 * when no voucher is adopted on the sending thread) with the process attributes
150 * of the voucher adopted on the current thread.
153 * This allows a daemon to indicate from the context of an incoming IPC request
154 * that all future outgoing IPC from the process should be marked as acting
155 * "on behalf of" the sending process of the current IPC request (as long as the
156 * thread sending that outgoing IPC is not itself in the direct context of an
157 * IPC request, i.e. no voucher is adopted).
159 * If no voucher is adopted on the current thread or the current voucher does
160 * not contain any process attributes, the default voucher is reset to the
161 * default process attributes for the current process.
163 * CAUTION: Do NOT use this SPI without contacting the Darwin Runtime team.
165 API_AVAILABLE(macos(10.10), ios(8.0))
166 OS_VOUCHER_EXPORT OS_NOTHROW
168 voucher_replace_default_voucher(void);
171 * @function voucher_decrement_importance_count4CF
174 * Decrement external importance count of the mach voucher in the specified
178 * This is only intended for use by CoreFoundation to explicitly manage the
179 * App Nap state of an application following reception of a de-nap IPC message.
181 * CAUTION: Do NOT use this SPI without contacting the Darwin Runtime team.
183 API_AVAILABLE(macos(10.10), ios(8.0))
184 OS_VOUCHER_EXPORT OS_NOTHROW
186 voucher_decrement_importance_count4CF(voucher_t _Nullable voucher
);
189 * @group Voucher dispatch block SPI
193 * @typedef dispatch_block_flags_t
194 * SPI Flags to pass to the dispatch_block_create* functions.
196 * @const DISPATCH_BLOCK_NO_VOUCHER
197 * Flag indicating that a dispatch block object should not be assigned a voucher
198 * object. If invoked directly, the block object will be executed with the
199 * voucher adopted on the calling thread. If the block object is submitted to a
200 * queue, this replaces the default behavior of associating the submitted block
201 * instance with the voucher adopted at the time of submission.
202 * This flag is ignored if a specific voucher object is assigned with the
203 * dispatch_block_create_with_voucher* functions, and is equivalent to passing
204 * the NULL voucher to these functions.
206 * @const DISPATCH_BLOCK_IF_LAST_RESET_QUEUE_QOS_OVERRIDE
207 * Flag indicating that this dispatch block object should try to reset the
208 * recorded maximum QoS of all currently enqueued items on a serial dispatch
209 * queue at the base of a queue hierarchy.
211 * This is only works if the queue becomes empty by dequeuing the block in
212 * question, and then allows that block to enqueue more work on this hierarchy
213 * without perpetuating QoS overrides resulting from items previously executed
216 * A dispatch block object created with this flag set cannot be used with
217 * dispatch_block_wait() or dispatch_block_cancel().
219 #define DISPATCH_BLOCK_NO_VOUCHER (0x40ul)
221 #define DISPATCH_BLOCK_IF_LAST_RESET_QUEUE_QOS_OVERRIDE (0x80ul)
224 * @function dispatch_block_create_with_voucher
227 * Create a new dispatch block object on the heap from an existing block and
228 * the given flags, and assign it the specified voucher object.
231 * The provided block is Block_copy'ed to the heap, it and the specified voucher
232 * object are retained by the newly created dispatch block object.
234 * The returned dispatch block object is intended to be submitted to a dispatch
235 * queue with dispatch_async() and related functions, but may also be invoked
236 * directly. Both operations can be performed an arbitrary number of times but
237 * only the first completed execution of a dispatch block object can be waited
238 * on with dispatch_block_wait() or observed with dispatch_block_notify().
240 * The returned dispatch block will be executed with the specified voucher
241 * adopted for the duration of the block body. If the NULL voucher is passed,
242 * the block will be executed with the voucher adopted on the calling thread, or
243 * with no voucher if the DISPATCH_BLOCK_DETACHED flag was also provided.
245 * If the returned dispatch block object is submitted to a dispatch queue, the
246 * submitted block instance will be associated with the QOS class current at the
247 * time of submission, unless one of the following flags assigned a specific QOS
248 * class (or no QOS class) at the time of block creation:
249 * - DISPATCH_BLOCK_ASSIGN_CURRENT
250 * - DISPATCH_BLOCK_NO_QOS_CLASS
251 * - DISPATCH_BLOCK_DETACHED
252 * The QOS class the block object will be executed with also depends on the QOS
253 * class assigned to the queue and which of the following flags was specified or
255 * - DISPATCH_BLOCK_INHERIT_QOS_CLASS (default for asynchronous execution)
256 * - DISPATCH_BLOCK_ENFORCE_QOS_CLASS (default for synchronous execution)
257 * See description of dispatch_block_flags_t for details.
259 * If the returned dispatch block object is submitted directly to a serial queue
260 * and is configured to execute with a specific QOS class, the system will make
261 * a best effort to apply the necessary QOS overrides to ensure that blocks
262 * submitted earlier to the serial queue are executed at that same QOS class or
266 * Configuration flags for the block object.
267 * Passing a value that is not a bitwise OR of flags from dispatch_block_flags_t
268 * results in NULL being returned.
271 * A voucher object or NULL. Passing NULL is equivalent to specifying the
272 * DISPATCH_BLOCK_NO_VOUCHER flag.
275 * The block to create the dispatch block object from.
278 * The newly created dispatch block object, or NULL.
279 * When not building with Objective-C ARC, must be released with a -[release]
280 * message or the Block_release() function.
282 API_AVAILABLE(macos(10.10), ios(8.0))
283 DISPATCH_EXPORT DISPATCH_NONNULL3 DISPATCH_RETURNS_RETAINED_BLOCK
284 DISPATCH_WARN_RESULT DISPATCH_NOTHROW
286 dispatch_block_create_with_voucher(dispatch_block_flags_t flags
,
287 voucher_t _Nullable voucher
, dispatch_block_t block
);
290 * @function dispatch_block_create_with_voucher_and_qos_class
293 * Create a new dispatch block object on the heap from an existing block and
294 * the given flags, and assign it the specified voucher object, QOS class and
298 * The provided block is Block_copy'ed to the heap, it and the specified voucher
299 * object are retained by the newly created dispatch block object.
301 * The returned dispatch block object is intended to be submitted to a dispatch
302 * queue with dispatch_async() and related functions, but may also be invoked
303 * directly. Both operations can be performed an arbitrary number of times but
304 * only the first completed execution of a dispatch block object can be waited
305 * on with dispatch_block_wait() or observed with dispatch_block_notify().
307 * The returned dispatch block will be executed with the specified voucher
308 * adopted for the duration of the block body. If the NULL voucher is passed,
309 * the block will be executed with the voucher adopted on the calling thread, or
310 * with no voucher if the DISPATCH_BLOCK_DETACHED flag was also provided.
312 * If invoked directly, the returned dispatch block object will be executed with
313 * the assigned QOS class as long as that does not result in a lower QOS class
314 * than what is current on the calling thread.
316 * If the returned dispatch block object is submitted to a dispatch queue, the
317 * QOS class it will be executed with depends on the QOS class assigned to the
318 * block, the QOS class assigned to the queue and which of the following flags
319 * was specified or defaulted to:
320 * - DISPATCH_BLOCK_INHERIT_QOS_CLASS: default for asynchronous execution
321 * - DISPATCH_BLOCK_ENFORCE_QOS_CLASS: default for synchronous execution
322 * See description of dispatch_block_flags_t for details.
324 * If the returned dispatch block object is submitted directly to a serial queue
325 * and is configured to execute with a specific QOS class, the system will make
326 * a best effort to apply the necessary QOS overrides to ensure that blocks
327 * submitted earlier to the serial queue are executed at that same QOS class or
331 * Configuration flags for the block object.
332 * Passing a value that is not a bitwise OR of flags from dispatch_block_flags_t
333 * results in NULL being returned.
336 * A voucher object or NULL. Passing NULL is equivalent to specifying the
337 * DISPATCH_BLOCK_NO_VOUCHER flag.
341 * - QOS_CLASS_USER_INTERACTIVE
342 * - QOS_CLASS_USER_INITIATED
343 * - QOS_CLASS_DEFAULT
344 * - QOS_CLASS_UTILITY
345 * - QOS_CLASS_BACKGROUND
346 * - QOS_CLASS_UNSPECIFIED
347 * Passing QOS_CLASS_UNSPECIFIED is equivalent to specifying the
348 * DISPATCH_BLOCK_NO_QOS_CLASS flag. Passing any other value results in NULL
351 * @param relative_priority
352 * A relative priority within the QOS class. This value is a negative
353 * offset from the maximum supported scheduler priority for the given class.
354 * Passing a value greater than zero or less than QOS_MIN_RELATIVE_PRIORITY
355 * results in NULL being returned.
358 * The block to create the dispatch block object from.
361 * The newly created dispatch block object, or NULL.
362 * When not building with Objective-C ARC, must be released with a -[release]
363 * message or the Block_release() function.
365 API_AVAILABLE(macos(10.10), ios(8.0))
366 DISPATCH_EXPORT DISPATCH_NONNULL5 DISPATCH_RETURNS_RETAINED_BLOCK
367 DISPATCH_WARN_RESULT DISPATCH_NOTHROW
369 dispatch_block_create_with_voucher_and_qos_class(dispatch_block_flags_t flags
,
370 voucher_t _Nullable voucher
, dispatch_qos_class_t qos_class
,
371 int relative_priority
, dispatch_block_t block
);
374 * @group Voucher dispatch queue SPI
378 * @function dispatch_queue_create_with_accounting_override_voucher
381 * Deprecated, do not use, will abort process if called.
383 API_DEPRECATED("removed SPI", \
384 macos(10.11,10.12), ios(9.0,10.0), watchos(2.0,3.0), tvos(9.0,10.0))
385 DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
388 dispatch_queue_create_with_accounting_override_voucher(
389 const char *_Nullable label
,
390 dispatch_queue_attr_t _Nullable attr
,
391 voucher_t _Nullable voucher
);
393 #if __has_include(<mach/mach.h>)
395 * @group Voucher Mach SPI
396 * SPI intended for clients that need to interact with mach messages or mach
397 * voucher ports directly.
401 * @function voucher_create_with_mach_msg
404 * Creates a new voucher object from a mach message carrying a mach voucher port
407 * Ownership of the mach voucher port in the message is transfered to the new
408 * voucher object and the message header mach voucher field is cleared.
411 * The mach message to query.
414 * The newly created voucher object or NULL if the message was not carrying a
417 API_AVAILABLE(macos(10.10), ios(8.0))
418 OS_VOUCHER_EXPORT OS_OBJECT_RETURNS_RETAINED OS_WARN_RESULT OS_NOTHROW
420 voucher_create_with_mach_msg(mach_msg_header_t
*msg
);
423 * @group Voucher Persona SPI
424 * SPI intended for clients that need to interact with personas.
427 struct proc_persona_info
;
430 * @function voucher_get_current_persona
433 * Retrieve the persona identifier of the 'originator' process for the current
437 * Retrieve the persona identifier of the ’originator’ process possibly stored
438 * in the PERSONA_TOKEN attribute of the currently adopted voucher.
440 * If the thread has not adopted a voucher, or the current voucher does not
441 * contain a PERSONA_TOKEN attribute, this function returns the persona
442 * identifier of the current process.
444 * If the process is not running under a persona, then this returns
448 * The persona identifier of the 'originator' process for the current voucher,
449 * or the persona identifier of the current process
452 API_AVAILABLE(ios(9.2))
453 OS_VOUCHER_EXPORT OS_WARN_RESULT OS_NOTHROW
455 voucher_get_current_persona(void);
458 * @function voucher_get_current_persona_originator_info
461 * Retrieve the ’originator’ process persona info for the currently adopted
465 * If there is no currently adopted voucher, or no PERSONA_TOKEN attribute
466 * in that voucher, this function fails.
468 * @param persona_info
469 * The proc_persona_info structure to fill in case of success
472 * 0 on success: currently adopted voucher has a PERSONA_TOKEN
473 * -1 on failure: persona_info is untouched/uninitialized
475 API_AVAILABLE(ios(9.2))
476 OS_VOUCHER_EXPORT OS_WARN_RESULT OS_NOTHROW OS_NONNULL1
478 voucher_get_current_persona_originator_info(
479 struct proc_persona_info
*persona_info
);
482 * @function voucher_get_current_persona_proximate_info
485 * Retrieve the ’proximate’ process persona info for the currently adopted
489 * If there is no currently adopted voucher, or no PERSONA_TOKEN attribute
490 * in that voucher, this function fails.
492 * @param persona_info
493 * The proc_persona_info structure to fill in case of success
496 * 0 on success: currently adopted voucher has a PERSONA_TOKEN
497 * -1 on failure: persona_info is untouched/uninitialized
499 API_AVAILABLE(ios(9.2))
500 OS_VOUCHER_EXPORT OS_WARN_RESULT OS_NOTHROW OS_NONNULL1
502 voucher_get_current_persona_proximate_info(
503 struct proc_persona_info
*persona_info
);
505 #endif // __has_include(<mach/mach.h>)
509 DISPATCH_ASSUME_NONNULL_END
511 #endif // __OS_VOUCHER_PRIVATE__
513 #if OS_VOUCHER_ACTIVITY_SPI
514 #include "voucher_activity_private.h"