connection.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. #ifndef __XPC_CONNECTION_H__
  2. #define __XPC_CONNECTION_H__
  3. #ifndef __XPC_INDIRECT__
  4. #error "Please #include <xpc/xpc.h> instead of this file directly."
  5. // For HeaderDoc.
  6. #include <xpc/base.h>
  7. #endif // __XPC_INDIRECT__
  8. #ifndef __BLOCKS__
  9. #error "XPC connections require Blocks support."
  10. #endif // __BLOCKS__
  11. __BEGIN_DECLS
  12. /*!
  13. * @constant XPC_ERROR_CONNECTION_INTERRUPTED
  14. * Will be delivered to the connection's event handler if the remote service
  15. * exited. The connection is still live even in this case, and resending a
  16. * message will cause the service to be launched on-demand. This error serves
  17. * as a client's indication that it should resynchronize any state that it had
  18. * given the service.
  19. *
  20. * Any messages in the queue to be sent will be unwound and canceled when this
  21. * error occurs. In the case where a message waiting to be sent has a reply
  22. * handler, that handler will be invoked with this error. In the context of the
  23. * reply handler, this error indicates that a reply to the message will never
  24. * arrive.
  25. *
  26. * Messages that do not have reply handlers associated with them will be
  27. * silently disposed of. This error will only be given to peer connections.
  28. */
  29. #define XPC_ERROR_CONNECTION_INTERRUPTED \
  30. XPC_GLOBAL_OBJECT(_xpc_error_connection_interrupted)
  31. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  32. XPC_EXPORT
  33. const struct _xpc_dictionary_s _xpc_error_connection_interrupted;
  34. /*!
  35. * @constant XPC_ERROR_CONNECTION_INVALID
  36. * Will be delivered to the connection's event handler if the named service
  37. * provided to xpc_connection_create() could not be found in the XPC service
  38. * namespace. The connection is useless and should be disposed of.
  39. *
  40. * Any messages in the queue to be sent will be unwound and canceled when this
  41. * error occurs, similarly to the behavior when XPC_ERROR_CONNECTION_INTERRUPTED
  42. * occurs. The only difference is that the XPC_ERROR_CONNECTION_INVALID will be
  43. * given to outstanding reply handlers and the connection's event handler.
  44. *
  45. * This error may be given to any type of connection.
  46. */
  47. #define XPC_ERROR_CONNECTION_INVALID \
  48. XPC_GLOBAL_OBJECT(_xpc_error_connection_invalid)
  49. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  50. XPC_EXPORT
  51. const struct _xpc_dictionary_s _xpc_error_connection_invalid;
  52. /*!
  53. * @constant XPC_ERROR_TERMINATION_IMMINENT
  54. * This error will be delivered to a peer connection's event handler when the
  55. * XPC runtime has determined that the program should exit and that all
  56. * outstanding transactions must be wound down, and no new transactions can be
  57. * opened.
  58. *
  59. * After this error has been delivered to the event handler, no more messages
  60. * will be received by the connection. The runtime will still attempt to deliver
  61. * outgoing messages, but this error should be treated as an indication that
  62. * the program will exit very soon, and any outstanding business over the
  63. * connection should be wrapped up as quickly as possible and the connection
  64. * canceled shortly thereafter.
  65. *
  66. * This error will only be delivered to peer connections received through a
  67. * listener or the xpc_main() event handler.
  68. */
  69. #define XPC_ERROR_TERMINATION_IMMINENT \
  70. XPC_GLOBAL_OBJECT(_xpc_error_termination_imminent)
  71. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  72. XPC_EXPORT
  73. const struct _xpc_dictionary_s _xpc_error_termination_imminent;
  74. /*!
  75. * @constant XPC_CONNECTION_MACH_SERVICE_LISTENER
  76. * Passed to xpc_connection_create_mach_service(). This flag indicates that the
  77. * caller is the listener for the named service. This flag may only be passed
  78. * for services which are advertised in the process' launchd.plist(5). You may
  79. * not use this flag to dynamically add services to the Mach bootstrap
  80. * namespace.
  81. */
  82. #define XPC_CONNECTION_MACH_SERVICE_LISTENER (1 << 0)
  83. /*!
  84. * @constant XPC_CONNECTION_MACH_SERVICE_PRIVILEGED
  85. * Passed to xpc_connection_create_mach_service(). This flag indicates that the
  86. * job advertising the service name in its launchd.plist(5) should be in the
  87. * privileged Mach bootstrap. This is typically accomplished by placing your
  88. * launchd.plist(5) in /Library/LaunchDaemons. If specified alongside the
  89. * XPC_CONNECTION_MACH_SERVICE_LISTENER flag, this flag is a no-op.
  90. */
  91. #define XPC_CONNECTION_MACH_SERVICE_PRIVILEGED (1 << 1)
  92. /*!
  93. * @typedef xpc_finalizer_f
  94. * A function that is invoked when a connection is being torn down and its
  95. * context needs to be freed. It is not safe to reference the connection from
  96. * within this function.
  97. *
  98. * @param value
  99. * The context object that is to be disposed of.
  100. */
  101. typedef void (*xpc_finalizer_t)(void *value);
  102. /*!
  103. * @function xpc_connection_create
  104. * Creates a new connection object.
  105. *
  106. * @param name
  107. * If non-NULL, the name of the service with which to connect. The returned
  108. * connection will be a peer.
  109. *
  110. * If NULL, an anonymous listener connection will be created. You can embed the
  111. * ability to create new peer connections in an endpoint, which can be inserted
  112. * into a message and sent to another process .
  113. *
  114. * @param targetq
  115. * The GCD queue to which the event handler block will be submitted. This
  116. * parameter may be NULL, in which case the connection's target queue will be
  117. * libdispatch's default target queue, defined as DISPATCH_TARGET_QUEUE_DEFAULT.
  118. * The target queue may be changed later with a call to
  119. * xpc_connection_set_target_queue().
  120. *
  121. * @result
  122. * A new connection object. The caller is responsible for disposing of the
  123. * returned object with {@link xpc_release} when it is no longer needed.
  124. *
  125. * @discussion
  126. * This method will succeed even if the named service does not exist. This is
  127. * because the XPC namespace is not queried for the service name until
  128. * the first call to xpc_connection_resume().
  129. *
  130. * XPC connections, like dispatch sources, are returned in a suspended state, so
  131. * you must call {@link xpc_connection_resume()} in order to begin receiving
  132. * events from the connection. Also like dispatch sources, connections must be
  133. * resumed in order to be safely released. It is a programming error to release
  134. * a suspended connection.
  135. */
  136. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  137. XPC_EXPORT XPC_MALLOC XPC_RETURNS_RETAINED XPC_WARN_RESULT
  138. xpc_connection_t
  139. xpc_connection_create(const char *name, dispatch_queue_t targetq);
  140. /*!
  141. * @function xpc_connection_create_mach_service
  142. * Creates a new connection object representing a Mach service.
  143. *
  144. * @param name
  145. * The name of the remote service with which to connect. The service name must
  146. * exist in a Mach bootstrap that is accessible to the process and be advertised
  147. * in a launchd.plist.
  148. *
  149. * @param targetq
  150. * The GCD queue to which the event handler block will be submitted. This
  151. * parameter may be NULL, in which case the connection's target queue will be
  152. * libdispatch's default target queue, defined as DISPATCH_TARGET_QUEUE_DEFAULT.
  153. * The target queue may be changed later with a call to
  154. * xpc_connection_set_target_queue().
  155. *
  156. * @param flags
  157. * Additional attributes with which to create the connection.
  158. *
  159. * @result
  160. * A new connection object.
  161. *
  162. * @discussion
  163. * If the XPC_CONNECTION_MACH_SERVICE_LISTENER flag is given to this method,
  164. * then the connection returned will be a listener connection. Otherwise, a peer
  165. * connection will be returned. See the documentation for
  166. * {@link xpc_connection_set_event_handler()} for the semantics of listener
  167. * connections versus peer connections.
  168. *
  169. * This method will succeed even if the named service does not exist. This is
  170. * because the Mach namespace is not queried for the service name until the
  171. * first call to {@link xpc_connection_resume()}.
  172. */
  173. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  174. XPC_EXPORT XPC_MALLOC XPC_RETURNS_RETAINED XPC_WARN_RESULT XPC_NONNULL1
  175. xpc_connection_t
  176. xpc_connection_create_mach_service(const char *name, dispatch_queue_t targetq,
  177. uint64_t flags);
  178. /*!
  179. * @function xpc_connection_create_from_endpoint
  180. * Creates a new connection from the given endpoint.
  181. *
  182. * @param endpoint
  183. * The endpoint from which to create the new connection.
  184. *
  185. * @result
  186. * A new peer connection to the listener represented by the given endpoint.
  187. *
  188. * The same responsibilities of setting an event handler and resuming the
  189. * connection after calling xpc_connection_create() apply to the connection
  190. * returned by this API. Since the connection yielded by this API is not
  191. * associated with a name (and therefore is not rediscoverable), this connection
  192. * will receive XPC_ERROR_CONNECTION_INVALID if the listening side crashes,
  193. * exits or cancels the listener connection.
  194. */
  195. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  196. XPC_EXPORT XPC_MALLOC XPC_RETURNS_RETAINED XPC_WARN_RESULT XPC_NONNULL_ALL
  197. xpc_connection_t
  198. xpc_connection_create_from_endpoint(xpc_endpoint_t endpoint);
  199. /*!
  200. * @function xpc_connection_set_target_queue
  201. * Sets the target queue of the given connection.
  202. *
  203. * @param connection
  204. * The connection object which is to be manipulated.
  205. *
  206. * @param targetq
  207. * The GCD queue to which the event handler block will be submitted. This
  208. * parameter may be NULL, in which case the connection's target queue will be
  209. * libdispatch's default target queue, defined as DISPATCH_TARGET_QUEUE_DEFAULT.
  210. *
  211. * @discussion
  212. * Setting the target queue is asynchronous and non-preemptive and therefore
  213. * this method will not interrupt the execution of an already-running event
  214. * handler block. Setting the target queue may be likened to issuing a barrier
  215. * to the connection which does the actual work of changing the target queue.
  216. *
  217. * The XPC runtime guarantees this non-preemptiveness even for concurrent target
  218. * queues. If the target queue is a concurrent queue, then XPC still guarantees
  219. * that there will never be more than one invocation of the connection's event
  220. * handler block executing concurrently. If you wish to process events
  221. * concurrently, you can dispatch_async(3) to a concurrent queue from within
  222. * the event handler.
  223. *
  224. * IMPORTANT: When called from within the event handler block,
  225. * dispatch_get_current_queue(3) is NOT guaranteed to return a pointer to the
  226. * queue set with this method.
  227. *
  228. * Despite this seeming inconsistency, the XPC runtime guarantees that, when the
  229. * target queue is a serial queue, the event handler block will execute
  230. * synchonously with respect to other blocks submitted to that same queue. When
  231. * the target queue is a concurrent queue, the event handler block may run
  232. * concurrently with other blocks submitted to that queue, but it will never run
  233. * concurrently with other invocations of itself for the same connection, as
  234. * discussed previously.
  235. */
  236. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  237. XPC_EXPORT XPC_NONNULL1
  238. void
  239. xpc_connection_set_target_queue(xpc_connection_t connection,
  240. dispatch_queue_t targetq);
  241. /*!
  242. * @function xpc_connection_set_event_handler
  243. * Sets the event handler block for the connection.
  244. *
  245. * @param connection
  246. * The connection object which is to be manipulated.
  247. *
  248. * @param handler
  249. * The event handler block.
  250. *
  251. * @discussion
  252. * Setting the event handler is asynchronous and non-preemptive, and therefore
  253. * this method will not interrupt the execution of an already-running event
  254. * handler block. If the event handler is executing at the time of this call, it
  255. * will finish, and then the connection's event handler will be changed before
  256. * the next invocation of the event handler. The XPC runtime guarantees this
  257. * non-preemptiveness even for concurrent target queues.
  258. *
  259. * Connection event handlers are non-reentrant, so it is safe to call
  260. * xpc_connection_set_event_handler() from within the event handler block.
  261. *
  262. * The event handler's execution should be treated as a barrier to all
  263. * connection activity. When it is executing, the connection will not attempt to
  264. * send or receive messages, including reply messages. Thus, it is not safe to
  265. * call xpc_connection_send_message_with_reply_sync() on the connection from
  266. * within the event handler.
  267. *
  268. * You do not hold a reference on the object received as the event handler's
  269. * only argument. Regardless of the type of object received, it is safe to call
  270. * xpc_retain() on the object to obtain a reference to it.
  271. *
  272. * A connection may receive different events depending upon whether it is a
  273. * listener or not. Any connection may receive an error in its event handler.
  274. * But while normal connections may receive messages in addition to errors,
  275. * listener connections will receive connections and and not messages.
  276. *
  277. * Connections received by listeners are equivalent to those returned by
  278. * xpc_connection_create() with a non-NULL name argument and a NULL targetq
  279. * argument with the exception that you do not hold a reference on them.
  280. * You must set an event handler and resume the connection. If you do not wish
  281. * to accept the connection, you may simply call xpc_connection_cancel() on it
  282. * and return. The runtime will dispose of it for you.
  283. *
  284. * If there is an error in the connection, this handler will be invoked with the
  285. * error dictionary as its argument. This dictionary will be one of the well-
  286. * known XPC_ERROR_* dictionaries.
  287. *
  288. * Regardless of the type of event, ownership of the event object is NOT
  289. * implicitly transferred. Thus, the object will be released and deallocated at
  290. * some point in the future after the event handler returns. If you wish the
  291. * event's lifetime to persist, you must retain it with xpc_retain().
  292. *
  293. * Connections received through the event handler will be released and
  294. * deallocated after the connection has gone invalid and delivered that event to
  295. * its event handler.
  296. */
  297. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  298. XPC_EXPORT XPC_NONNULL_ALL
  299. void
  300. xpc_connection_set_event_handler(xpc_connection_t connection,
  301. xpc_handler_t handler);
  302. /*!
  303. * @function xpc_connection_suspend
  304. * Suspends the connection so that the event handler block will not fire and
  305. * that the connection will not attempt to send any messages it has in its
  306. * queue. All calls to xpc_connection_suspend() must be balanced with calls to
  307. * xpc_connection_resume() before releasing the last reference to the
  308. * connection.
  309. *
  310. * @param connection
  311. * The connection object which is to be manipulated.
  312. *
  313. * @discussion
  314. * Suspension is asynchronous and non-preemptive, and therefore this method will
  315. * not interrupt the execution of an already-running event handler block. If
  316. * the event handler is executing at the time of this call, it will finish, and
  317. * then the connection will be suspended before the next scheduled invocation
  318. * of the event handler. The XPC runtime guarantees this non-preemptiveness even
  319. * for concurrent target queues.
  320. *
  321. * Connection event handlers are non-reentrant, so it is safe to call
  322. * xpc_connection_suspend() from within the event handler block.
  323. */
  324. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  325. XPC_EXPORT XPC_NONNULL_ALL
  326. void
  327. xpc_connection_suspend(xpc_connection_t connection);
  328. /*!
  329. * @function xpc_connection_resume
  330. * Resumes the connection. Connections start in a suspended state, so you must
  331. * call xpc_connection_resume() on a connection before it will send or receive
  332. * any messages.
  333. *
  334. * @param connection
  335. * The connection object which is to be manipulated.
  336. *
  337. * @discussion
  338. * In order for a connection to become live, every call to
  339. * xpc_connection_suspend() must be balanced with a call to
  340. * xpc_connection_resume() after the initial call to xpc_connection_resume().
  341. * After the initial resume of the connection, calling xpc_connection_resume()
  342. * more times than xpc_connection_suspend() has been called is considered an
  343. * error.
  344. */
  345. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  346. XPC_EXPORT XPC_NONNULL_ALL
  347. void
  348. xpc_connection_resume(xpc_connection_t connection);
  349. /*!
  350. * @function xpc_connection_send_message
  351. * Sends a message over the connection to the destination service.
  352. *
  353. * @param connection
  354. * The connection over which the message shall be sent.
  355. *
  356. * @param message
  357. * The message to send. This must be a dictionary object. This dictionary is
  358. * logically copied by the connection, so it is safe to modify the dictionary
  359. * after this call.
  360. *
  361. * @discussion
  362. * Messages are delivered in FIFO order. This API is safe to call from multiple
  363. * GCD queues. There is no indication that a message was delivered successfully.
  364. * This is because even once the message has been successfully enqueued on the
  365. * remote end, there are no guarantees about when the runtime will dequeue the
  366. * message and invoke the other connection's event handler block.
  367. *
  368. * If this API is used to send a message that is in reply to another message,
  369. * there is no guarantee of ordering between the invocations of the connection's
  370. * event handler and the reply handler for that message, even if they are
  371. * targeted to the same queue.
  372. *
  373. * After extensive study, we have found that clients who are interested in
  374. * the state of the message on the server end are typically holding open
  375. * transactions related to that message. And the only reliable way to track the
  376. * lifetime of that transaction is at the protocol layer. So the server should
  377. * send a reply message, which upon receiving, will cause the client to close
  378. * its transaction.
  379. */
  380. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  381. XPC_EXPORT XPC_NONNULL_ALL
  382. void
  383. xpc_connection_send_message(xpc_connection_t connection, xpc_object_t message);
  384. /*!
  385. * @function xpc_connection_send_barrier
  386. * Issues a barrier against the connection's message-send activity.
  387. *
  388. * @param connection
  389. * The connection against which the barrier is to be issued.
  390. *
  391. * @param barrier
  392. * The barrier block to issue. This barrier prevents concurrent message-send
  393. * activity on the connection. No messages will be sent while the barrier block
  394. * is executing.
  395. *
  396. * @discussion
  397. * XPC guarantees that, even if the connection's target queue is a concurrent
  398. * queue, there are no other messages being sent concurrently while the barrier
  399. * block is executing. XPC does not guarantee that the reciept of messages
  400. * (either through the connection's event handler or through reply handlers)
  401. * will be suspended while the barrier is executing.
  402. *
  403. * A barrier is issued relative to the message-send queue. Thus, if you call
  404. * xpc_connection_send_message() five times and then call
  405. * xpc_connection_send_barrier(), the barrier will be invoked after the fifth
  406. * message has been sent and its memory disposed of. You may safely cancel a
  407. * connection from within a barrier block.
  408. *
  409. * If a barrier is issued after sending a message which expects a reply, the
  410. * behavior is the same as described above. The receipt of a reply message will
  411. * not influence when the barrier runs.
  412. *
  413. * A barrier block can be useful for throttling resource consumption on the
  414. * connected side of a connection. For example, if your connection sends many
  415. * large messages, you can use a barrier to limit the number of messages that
  416. * are inflight at any given time. This can be particularly useful for messages
  417. * that contain kernel resources (like file descriptors) which have a system-
  418. * wide limit.
  419. *
  420. * If a barrier is issued on a canceled connection, it will be invoked
  421. * immediately. If a connection has been canceled and still has outstanding
  422. * barriers, those barriers will be invoked as part of the connection's
  423. * unwinding process.
  424. *
  425. * It is important to note that a barrier block's execution order is not
  426. * guaranteed with respect to other blocks that have been scheduled on the
  427. * target queue of the connection. Or said differently,
  428. * xpc_connection_send_barrier(3) is not equivalent to dispatch_async(3).
  429. */
  430. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  431. XPC_EXPORT XPC_NONNULL_ALL
  432. void
  433. xpc_connection_send_barrier(xpc_connection_t connection,
  434. dispatch_block_t barrier);
  435. /*!
  436. * @function xpc_connection_send_message_with_reply
  437. * Sends a message over the connection to the destination service and associates
  438. * a handler to be invoked when the remote service sends a reply message.
  439. *
  440. * @param connection
  441. * The connection over which the message shall be sent.
  442. *
  443. * @param message
  444. * The message to send. This must be a dictionary object.
  445. *
  446. * @param replyq
  447. * The GCD queue to which the reply handler will be submitted. This may be a
  448. * concurrent queue.
  449. *
  450. * @param handler
  451. * The handler block to invoke when a reply to the message is received from
  452. * the connection. If the remote service exits prematurely before the reply was
  453. * received, the XPC_ERROR_CONNECTION_INTERRUPTED error will be returned.
  454. * If the connection went invalid before the message could be sent, the
  455. * XPC_ERROR_CONNECTION_INVALID error will be returned.
  456. *
  457. * @discussion
  458. * If the given GCD queue is a concurrent queue, XPC cannot guarantee that there
  459. * will not be multiple reply handlers being invoked concurrently. XPC does not
  460. * guarantee any ordering for the invocation of reply handers. So if multiple
  461. * messages are waiting for replies and the connection goes invalid, there is no
  462. * guarantee that the reply handlers will be invoked in FIFO order. Similarly,
  463. * XPC does not guarantee that reply handlers will not run concurrently with
  464. * the connection's event handler in the case that the reply queue and the
  465. * connection's target queue are the same concurrent queue.
  466. */
  467. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  468. XPC_EXPORT XPC_NONNULL1 XPC_NONNULL2 XPC_NONNULL4
  469. void
  470. xpc_connection_send_message_with_reply(xpc_connection_t connection,
  471. xpc_object_t message, dispatch_queue_t replyq, xpc_handler_t handler);
  472. /*!
  473. * @function xpc_connection_send_message_with_reply_sync
  474. * Sends a message over the connection and blocks the caller until a reply is
  475. * received.
  476. *
  477. * @param connection
  478. * The connection over which the message shall be sent.
  479. *
  480. * @param message
  481. * The message to send. This must be a dictionary object.
  482. *
  483. * @result
  484. * The message that the remote service sent in reply to the original message.
  485. * If the remote service exits prematurely before the reply was received, the
  486. * XPC_ERROR_CONNECTION_INTERRUPTED error will be returned. If the connection
  487. * went invalid before the message could be sent, the
  488. * XPC_ERROR_CONNECTION_INVALID error will be returned.
  489. *
  490. * You are responsible for releasing the returned object.
  491. *
  492. * @discussion
  493. * This API is primarily for transitional purposes. Its implementation is
  494. * conceptually equivalent to calling xpc_connection_send_message_with_reply()
  495. * and then immediately blocking the calling thread on a semaphore and
  496. * signaling the semaphore from the reply block.
  497. *
  498. * Be judicious about your use of this API. It can block indefinitely, so if you
  499. * are using it to implement an API that can be called from the main thread, you
  500. * may wish to consider allowing the API to take a queue and callback block so
  501. * that results may be delivered asynchrously if possible.
  502. */
  503. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  504. XPC_EXPORT XPC_NONNULL_ALL XPC_WARN_RESULT XPC_RETURNS_RETAINED
  505. xpc_object_t
  506. xpc_connection_send_message_with_reply_sync(xpc_connection_t connection,
  507. xpc_object_t message);
  508. /*!
  509. * @function xpc_connection_cancel
  510. * Cancels the connection and ensures that its event handler will not fire
  511. * again. After this call, any messages that have not yet been sent will be
  512. * discarded, and the connection will be unwound. If there are messages that are
  513. * awaiting replies, they will have their reply handlers invoked with the
  514. * XPC_ERROR_CONNECTION_INVALID error.
  515. *
  516. * @param connection
  517. * The connection object which is to be manipulated.
  518. *
  519. * @discussion
  520. * Cancellation is asynchronous and non-preemptive and therefore this method
  521. * will not interrupt the execution of an already-running event handler block.
  522. * If the event handler is executing at the time of this call, it will finish,
  523. * and then the connection will be canceled, causing a final invocation of the
  524. * event handler to be scheduled with the XPC_ERROR_CONNECTION_INVALID error.
  525. * After that invocation, there will be no further invocations of the event
  526. * handler.
  527. *
  528. * The XPC runtime guarantees this non-preemptiveness even for concurrent target
  529. * queues.
  530. */
  531. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  532. XPC_EXPORT XPC_NONNULL_ALL
  533. void
  534. xpc_connection_cancel(xpc_connection_t connection);
  535. /*!
  536. * @function xpc_connection_get_name
  537. * Returns the name of the service with which the connections was created.
  538. *
  539. * @param connection
  540. * The connection object which is to be examined.
  541. *
  542. * @result
  543. * The name of the remote service. If you obtained the connection through an
  544. * invocation of another connection's event handler, NULL is returned.
  545. */
  546. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  547. XPC_EXPORT XPC_NONNULL_ALL XPC_WARN_RESULT
  548. const char *
  549. xpc_connection_get_name(xpc_connection_t connection);
  550. /*!
  551. * @function xpc_connection_get_euid
  552. * Returns the EUID of the remote peer.
  553. *
  554. * @param connection
  555. * The connection object which is to be examined.
  556. *
  557. * @result
  558. * The EUID of the remote peer at the time the connection was made.
  559. */
  560. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  561. XPC_EXPORT XPC_NONNULL_ALL XPC_WARN_RESULT
  562. uid_t
  563. xpc_connection_get_euid(xpc_connection_t connection);
  564. /*!
  565. * @function xpc_connection_get_egid
  566. * Returns the EGID of the remote peer.
  567. *
  568. * @param connection
  569. * The connection object which is to be examined.
  570. *
  571. * @result
  572. * The EGID of the remote peer at the time the connection was made.
  573. */
  574. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  575. XPC_EXPORT XPC_NONNULL_ALL XPC_WARN_RESULT
  576. gid_t
  577. xpc_connection_get_egid(xpc_connection_t connection);
  578. /*!
  579. * @function xpc_connection_get_pid
  580. * Returns the PID of the remote peer.
  581. *
  582. * @param connection
  583. * The connection object which is to be examined.
  584. *
  585. * @result
  586. * The PID of the remote peer.
  587. *
  588. * @discussion
  589. * A given PID is not guaranteed to be unique across an entire boot cycle.
  590. * Great care should be taken when dealing with this information, as it can go
  591. * stale after the connection is established. Mac OS X recycles PIDs, and
  592. * therefore another process could spawn and claim the PID before a message is
  593. * actually received from the connection.
  594. *
  595. * XPC will deliver an error to your event handler if the remote process goes
  596. * away, but there are no guarantees as to the timing of this notification's
  597. * delivery either at the kernel layer or at the XPC layer.
  598. */
  599. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  600. XPC_EXPORT XPC_NONNULL_ALL XPC_WARN_RESULT
  601. pid_t
  602. xpc_connection_get_pid(xpc_connection_t connection);
  603. /*!
  604. * @function xpc_connection_get_asid
  605. * Returns the audit session identifier of the remote peer.
  606. *
  607. * @param connection
  608. * The connection object which is to be examined.
  609. *
  610. * @result
  611. * The audit session ID of the remote peer at the time the connection was made.
  612. */
  613. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  614. XPC_EXPORT XPC_NONNULL_ALL XPC_WARN_RESULT
  615. au_asid_t
  616. xpc_connection_get_asid(xpc_connection_t connection);
  617. /*!
  618. * @function xpc_connection_set_context
  619. * Sets context on an connection.
  620. *
  621. * @param connection
  622. * The connection which is to be manipulated.
  623. *
  624. * @param context
  625. * The context to associate with the connection.
  626. *
  627. * @discussion
  628. * If you must manage the memory of the context object, you must set a finalizer
  629. * to dispose of it. If this method is called on a connection which already has
  630. * context associated with it, the finalizer will NOT be invoked. The finalizer
  631. * is only invoked when the connection is being deallocated.
  632. *
  633. * It is recommended that, instead of changing the actual context pointer
  634. * associated with the object, you instead change the state of the context
  635. * object itself.
  636. */
  637. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  638. XPC_EXPORT XPC_NONNULL1
  639. void
  640. xpc_connection_set_context(xpc_connection_t connection, void *context);
  641. /*!
  642. * @function xpc_connection_get_context
  643. * Returns the context associated with the connection.
  644. *
  645. * @param connection
  646. * The connection which is to be examined.
  647. *
  648. * @result
  649. * The context associated with the connection. NULL if there has been no context
  650. * associated with the object.
  651. */
  652. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  653. XPC_EXPORT XPC_NONNULL_ALL XPC_WARN_RESULT
  654. void *
  655. xpc_connection_get_context(xpc_connection_t connection);
  656. /*!
  657. * @function xpc_connection_set_finalizer_f
  658. * Sets the finalizer for the given connection.
  659. *
  660. * @param connection
  661. * The connection on which to set the finalizer.
  662. *
  663. * @param finalizer
  664. * The function that will be invoked when the connection's retain count has
  665. * dropped to zero and is being torn down.
  666. *
  667. * @discussion
  668. * For many uses of context objects, this API allows for a convenient shorthand
  669. * for freeing them. For example, for a context object allocated with malloc(3):
  670. *
  671. * xpc_connection_set_finalizer_f(object, free);
  672. */
  673. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0)
  674. XPC_EXPORT XPC_NONNULL1
  675. void
  676. xpc_connection_set_finalizer_f(xpc_connection_t connection,
  677. xpc_finalizer_t finalizer);
  678. __END_DECLS
  679. #endif // __XPC_CONNECTION_H__