我相信NSDistributedNotificationCenter应该为此工作。请注意,使用NSDistributedNotificationCenter在不同用户帐户中的进程之间进行通信本身并不需要root权限。
为了帮助用户帐户之间的协调,它可能有助于区分GUI应用程序和守护程序当前处于活动状态和处于控制状态的哪个实例以及哪些实例是被动的。您可以使用NSWorkspace的通知(NSWorkspaceSessionDidBecomeActiveNotification,NSWorkspaceSessionDidResignActiveNotification)来确定用户何时在用户帐户之间切换,并让您的实例相应地进行设置。
假设您的GUI应用程序和守护程序在3个不同的用户帐户中运行实例。例如,如果在活动用户帐户中想要开始更新过程,则可以使用NSDistributedNotificationCenter轻松地告知所有其他实例立即关闭。要做到这一点,你需要定义如下的内容。
在.h文件中,声明你通知不同的名称:
extern NSString * const MDShouldTerminateImmediatelyNotification;
在(一)实现文件
,创建名称,并设置等级可达感兴趣由分布式通知名字,等:
NSString * const MDShouldTerminateImmediatelyNotification = @"MDShouldTerminateImmediately";
- (id)init {
if (self = [super init]) {
[[NSDistributedNotificationCenter defaultCenter]
addObserver:self
selector:@selector(shouldTerminateImmediately:)
name:MDShouldTerminateImmediatelyNotification
object:nil];
}
return self;
}
- (void)dealloc {
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (void)shouldTerminateImmediately:(NSNotification *)notification {
if (ourInstanceIsInControl == NO) {
[NSApp terminate:nil];
}
}
在将启动更新过程中,你会做这样的事情来发送通知类:
- (void)beginUpdate {
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName:MDShouldTerminateImmediatelyNotification
object:[self description] // or just nil
userInfo:nil
options:NSNotificationDeliverImmediately | NSNotificationPostToAllSessions];
// continue
}
这至少应该是一个开始一起工作,我想....
其实,如果你正在讨论的root身份运行,做的所有用户帐户都一个单一的守护进程的实例,您可能需要考虑将该部分分解为Launchd Agent类型进程(后台进程,以用户级别运行,每个用户帐户都有自己的实例)。
欲了解更多信息:
Technical Note TN2083 Daemons and Agents
Root and Login Sessions
Creating launchd Daemons and Agents
谢谢,我已经看过“授权服务”,正如你所提到的,但它似乎是用来提升根权限。我已经在后台以root用户身份运行,需要从我的根特权后台任务启动用户登录会话中的应用程序。 – Dave 2010-12-01 14:31:37