macos - 通过 XPC 与应用程序通信并启动以 root 身份运行的守护进程

标签 macos daemon xpc

是否可以通过 XPC 与以 root 身份运行的启动守护进程和应用程序进行通信?当我的守护程序以我的用户身份运行时,我可以很好地与其通信,当以 root 身份运行时,它停止接收我的消息。这是 Mac OS X 内部的预期安全性吗? 我需要使用低级别的 xpc(也可以在 Lion 上运行)。我知道我可以为我的应用程序创建一个以 root 身份运行的特权和签名帮助工具。我是否能够通过 XPC 或套接字与另一个进程进行通信?

谢谢!

我的守护程序代码的一小段摘录:

int main()
{
    Logger::Start(Poco::Path::expand("/Users/Shared/Me/Service.log"));
    Logger::LogInfo("Starting xpc_main...");

    void* observer = nullptr;
    CFStringRef observedObject = CFSTR("com.me.service.close");
    CFNotificationCenterRef center = CFNotificationCenterGetDistributedCenter();
    CFNotificationCenterAddObserver(center, observer, notificationCallback, CFSTR("ClientClosing"), observedObject, CFNotificationSuspensionBehaviorDeliverImmediately);

    xpc_connection_t listener = xpc_connection_create_mach_service("com.me.service", NULL, XPC_CONNECTION_MACH_SERVICE_LISTENER);
    xpc_connection_set_event_handler(listener, ^(xpc_object_t event)
    {
        // New connections arrive here. You may safely cast to
        // xpc_connection_t. You will never receive messages here.
        // The semantics of this handler are similar to those of
        // of the one given to xpc_main().
        Logger::LogInfo("Event Handler on listener is called");

        eventHandler((xpc_connection_t)event);
    }); 

    Logger::LogInfo("call xpc_connection_resume...");

    xpc_connection_resume(listener);

    CFRunLoopRun();

    Logger::LogInfo("Main Program is Exiting...");

    return 0;
}

最佳答案

问题是 CFNotificationCenterGetDistributedCenter 仅对同一用户有效,root 用户不会向其他登录用户发送消息。

您需要切换到 CFNotificationCenterGetDarwinNotifyCenter

但是请注意,您不能使用该中心传递任何数据。

关于macos - 通过 XPC 与应用程序通信并启动以 root 身份运行的守护进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23552805/

相关文章:

objective-c - XPC服务: Raise Stack Size on Worker Thread

macos - mac上/private/etc/apache2和/etc/apache2有什么区别

java - 在 OSX 10.9.2 上安装和卸载了 Java 8,安装的 Java 7 和 Java 控制面板仍然引用 Java 8,无法更改 JNLP 安全设置

swift - 在 xcode 13 中的构建之间保留可访问性权限

macos - 如何在 Mac 上访问 Java 9 Jshell

java - 如何创建java守护进程以及如何交互?

php - 如何让 Laravel 5 命令像恶魔一样运行

objective-c - 有没有办法通过 XPC 传递任意结构?

python - 使用 python-daemon 守护 python 脚本-套接字问题

objective-c - 使用 NSXPCConnection 时如何同步等待回复 block