通过XPC与应用程序进行通信,并以root用户身份运行启动守护程序 [英] Communicating over XPC with an app and launch daemon running as root

查看:262
本文介绍了通过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用户不会向其他登录用户发送消息./p>

您需要切换到 CFNotificationCenterGetDarwinNotifyCenter .

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

Is it possible to communicate with a launch daemon running as root and an application over XPC? When my daemon is running as my user I can communicate with it fine, when run as root it stops receiving my messages. Is this intended security inside Mac OS X? I need to use low level xpc (for running on Lion as well). I know I can create a priviliged and signed helper tool that is running as root for my app. Will I be able to communicate with it with another process as well over XPC or sockets?

Thanks!

Small extract from my daemon code:

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;
}

解决方案

The problem is that CFNotificationCenterGetDistributedCenter works only on the same user, root user will not send message to other logged in users..

You'll need to switch to CFNotificationCenterGetDarwinNotifyCenter.

Please note however, that you can't pass any data using this center.

这篇关于通过XPC与应用程序进行通信,并以root用户身份运行启动守护程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆