xpc module won't get loaded

I have a xpc module (wrote in Swift using NSXPCConnection), it works well with test application (simple swift test app), my plan is to put this module under XPCService of a DAL plugIn, since the plugin written in C++, I created an Objective-C dylib which response for talking to the XPC, and this dylib using NSXConnection to activate the XPC module(as the Swift test App), it exports C API to the plugin.

Somehow when application using the plugin, the plugin call the API of the dylib to activate the XPC, but the XPC won't get loaded (from Activity Monitor).

Any idea to get this working? I cannot put XPCService folder inside the Obj-C dylib instead of inside the plugin bundle, and I don't want load the xpc as daemon, want it be loaded as plugin demanded.

Adding Obj-C into current C plugin is pretty headache, is there any C++ version NSXPConnection for this kind of situation?

=======this is the code in the dylib for activating the connection ======

- (int) initXPC
{
  connection = [[NSXPCConnection alloc] initWithServiceName:@"com....."];
  connection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(MyProtocol)];
   
  [connection resume];
  self->started = false;
  NSLog(@"😂 XPC connection setup, result:%@", ((connection == nil) ? @"Failed":@"Succeed!") );
  return connection != nil ;
}

Thanks Steven

Answered by DTS Engineer in 702826022

my plan is to put this module under XPCService of a DAL plugIn

IIRC a DAL plug-in is an old school plug-in that’s represented on disk as a bundle (rather than, say, an app extension). Is that right?

If so, you won’t be able to achieve this goal. Old school plug-ins like this are loaded into the host app’s process, and so their code is indistinguishable from the host app’s code. Thus your plug-in can only talk to the app’s XPC Services; the system won’t look for XPC Services in your bundle.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

my plan is to put this module under XPCService of a DAL plugIn

IIRC a DAL plug-in is an old school plug-in that’s represented on disk as a bundle (rather than, say, an app extension). Is that right?

If so, you won’t be able to achieve this goal. Old school plug-ins like this are loaded into the host app’s process, and so their code is indistinguishable from the host app’s code. Thus your plug-in can only talk to the app’s XPC Services; the system won’t look for XPC Services in your bundle.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks a lot, looks like I need find other way for it.

-Steven

Is it possible that I put the xpc module inside other application, use that application as the host and it launch the xpc as demand of plugin, could the DAL plugin talk to the XPC module?

No. XPC Services embedded in an app are scoped to just that app. To quote the xpcservice.plist man page:

Services embedded in an application bundle will only be visible to the containing application

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

One trick you can use here is to create an Service Management login item (installed via SMLoginItemSetEnabled). It can publish an XPC service that’s visible to all code running in that GUI login session. However, there is one major caveat…


Note I want to be clear about terminology here. In the context of plug-ins on Apple platforms:

  • The host app is the app using the plug-in

  • The container app is the app in which the plug-in resides

With that in mind…


Be aware that your plug-in is loaded in the host app’s process and is subject to that app’s restrictions. If the host app is sandboxed, the sandbox will block its outgoing connection to the Service Management login item’s XPC service.


For an example of how to cerate a Service Management login item that publishes an XPC service, see AppSandboxLoginItemXPCDemo.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

That's a better idea, BTW, one question might be simple to you. when I call xpc_release(connection) after xpc_connection_cancel, it will crash and system complains misuse like

libxpc.dylib                      0x00007fffd9152504 _xpc_api_misuse + 75
1  libxpc.dylib                      0x00007fffd9141b7f _xpc_connection_last_xref_cancel + 52
2  libxpc.dylib                      0x00007fffd9141b22 -[OS_xpc_connection _xref_dispose] + 17

If I remove xpc_release, it seems to be ok, and the app didn't enable ARC, does that mean if we called xpc_connection_cancel, even in non-ARC, we don't need call xpc_release? my env is Monterey 12.2

Thanks Steven

If you’re not using ARC then xpc_release is definitely required to balance the create in (presumably) xpc_connection_create. The specific API misused trap you’re hitting has a message Release of last reference on a suspended connection.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

still possible the connection was suspended?

Connections start suspended.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

xpc module won't get loaded
 
 
Q