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

是否可以通过 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

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

https://stackoverflow.com/questions/23552805/

相关文章:

python - 具有多个 worker 的 gunicorn 中的变量访问

sorting - lucene.net,文档提升不起作用

unix - 2个unix文件怎么会有相同的inode,但是inode的引用计数是1?

r - 警告 : replacing previous import ‘head’ when loa

ruby-on-rails - Rails - routes.rb - 滚动到根页面的 anchor

.net - 每个文件只执行一个类

sql - 在 NLTK 中将英语转换为 SQL

ios - 解码Code128条码?

jquery - Select2 无限滚动组

php - Laravel:模型内验证。多重验证规则