Slide 1

Slide 1 text

XPC Connection

Slide 2

Slide 2 text

XPC currently the only non-deprecated inter-process communication API on macOS

Slide 3

Slide 3 text

XPC services XPC connection

Slide 4

Slide 4 text

We are NOT talking about XPC Services here. We are talking XPC as API fo inter-process communication

Slide 5

Slide 5 text

XPC benefits 1. Performance: lazy (on-demand) automatic launching XPC services 2. Architecture: one executable can represent several services 3. Security: making services with minimum privileges 4. Stability: crashing one process doesn't affect another

Slide 6

Slide 6 text

Adding an executable, that can be called via XPC 1. Copy configuration plist to appropriate system location* 2. Change plist owner to root 3. Add plist to lauchd (load or bootstrap) 4. Put service's executable to location, defined in configuration plist 5. Run client! *System locations — user agent for current user: ~/Library/LaunchAgents/ — user agent for all users: /Library/LaunchAgents/ — daemon: /Library/LaunchDaemons/

Slide 7

Slide 7 text

Daemon config plist example

Slide 8

Slide 8 text

libxpc API xpc.h and connection.h

Slide 9

Slide 9 text

libxpc API xpc.h and connection.h

Slide 10

Slide 10 text

NSXPCConnection 1. NSXPCConnection 2. NSXPCInterface 3. NSXPCListener 4. NSXPCListenerEndpoint

Slide 11

Slide 11 text

NSXPCConnection 1. NSXPCConnection 2. NSXPCInterface 3. NSXPCListener 4. NSXPCListenerEndpoint optional

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Establishing XPC connection Details: https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/ Chapters/CreatingXPCServices.html Client side call on the server side

Slide 14

Slide 14 text

Establishing XPC connection Details: https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/ Chapters/CreatingXPCServices.html Server side Instantiate your listener soon after launch: set it's delegate

Slide 15

Slide 15 text

Establishing XPC connection Server side In NSXPCListener's delegate method set up the connection:

Slide 16

Slide 16 text

invalidation handler interruption handler

Slide 17

Slide 17 text

invalidation handler interruption handler error →

Slide 18

Slide 18 text

invalidation handler interruption handler error → OK →

Slide 19

Slide 19 text

Error output goes to Console.app

Slide 20

Slide 20 text

Method signature restrictions for XPC calls Methods should return Void. Parameters could be: 1. Arithmetic types (int, char, float, double, uint64_t, NSUInteger, and so on) 2. BOOL 3. C strings 4. C structures and arrays containing only the types listed above 5. Objective-C classes, that implement the NSSecureCoding protocol (e.g. NSString or custom classes). 6. NSArray, NSDictionary and other collections with objects, conforming to NSSecureCoding. 7. Only one block (reply block), that returns Void and receives parameters of types, listed above.

Slide 21

Slide 21 text

Passing objects by copy and by proxy Passing by copy is a recommended and performant way. Passing by proxy decreases performance and should be avoided.

Slide 22

Slide 22 text

Thank you! Happy to help via [email protected]