これには NSDistributedNotificationCenter が機能するはずです。NSDistributedNotificationCenter を使用して異なるユーザー アカウントのプロセス間で通信すること自体は、ルート権限を必要としないことに注意してください。
ユーザー アカウント間の調整を支援するために、現在アクティブで制御されている GUI アプリとデーモンのインスタンスと、パッシブなインスタンスを区別すると役立つ場合があります。NSWorkspace の通知 (NSWorkspaceSessionDidBecomeActiveNotification、NSWorkspaceSessionDidResignActiveNotification) を使用して、ユーザーがいつユーザー アカウントを切り替えたかなどを判断し、それに応じてインスタンスを設定することができます。
GUI アプリとデーモンに、3 つの異なるユーザー アカウントで実行されているインスタンスがあるとします。アクティブなユーザー アカウントで更新プロセスを開始する場合は、NSDistributedNotificationCenter を使用して、たとえば、他のすべてのインスタンスをすぐにシャットダウンするように簡単に指示できます。そのためには、次のように定義します。
.h ファイルで、さまざまな通知の名前を宣言します。
extern NSString * const MDShouldTerminateImmediatelyNotification;
(an)実装ファイルで、名前を作成し、その名前による分散通知に関心を持つようにクラスを設定します。
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 タイプのプロセス (バックグラウンド プロセス、ユーザー レベルで実行、各ユーザー アカウントで実行) に分割することを検討する必要がある場合があります。独自のインスタンスがあります)。
詳細については:
テクニカル ノート TN2083 デーモンとエージェント
ルートおよびログイン セッション
launchd デーモンとエージェントの作成