<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "XPC",
  "identifier" : "/documentation/XPC/xpc_session_create_mach_service",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "XPC"
    ],
    "preciseIdentifier" : "c:@F@xpc_session_create_mach_service"
  },
  "title" : "xpc_session_create_mach_service"
}
-->

# xpc_session_create_mach_service

Establishes a connection to a launch agent or launch daemon with the name you specify.

```
extern xpc_session_txpc_session_create_mach_service(const char *mach_service, dispatch_queue_t target_queue, xpc_session_create_flags_t flags, xpc_rich_error_t*error_out);
```

## Parameters

`mach_service`

The name of the Mach service to connect to. The service name must exist in the Mach bootstrap accessible to the process and advertised in a `launchd.plist`.

`target_queue`

The dispatch queue to use for session events. You can specify a concurrent dispatch queue. If you specify <doc://com.apple.documentation/documentation/ObjectiveC/nil-227m0>, the session uses `DISPATCH_TARGET_QUEUE_DEFAULT`.

`flags`

Attributes the session uses when establishing the connection.

`error_out`

If provided, a pointer that receives an error if the connection fails.

## Return Value

A new session object if the connection succeeds; otherwise <doc://com.apple.documentation/documentation/ObjectiveC/nil-227m0> if the service can’t be found or is unavailable. If the connection fails, this method populates the `error_out` parameter, if set, with an error that describes the failure.

## Discussion

By default, the session object this method returns is activated and is ready to accept messages. To create an inactive session, specify [`XPC_SESSION_CREATE_INACTIVE`](/documentation/XPC/xpc_session_create_flags_t-c.enum/XPC_SESSION_CREATE_INACTIVE) in the `flags` parameter.

> Important:
> You’re responsible for calling ``doc://com.apple.xpc/documentation/XPC/xpc_release`` to dispose of the session when it’s no longer needed.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)