我相信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