Running the application from the root account

I am developing a Cocoa GUI application that has an Objective-C daemon. The daemon is launched by LaunchDaemon, the graphical interface is launched using loginItems for each user.

When deploying the update, I need to update the daemon, which is simple, and update the GUI. I would like to be able to exit the GUI, replace the application and restart it for each user account in which it is running. I would like to do all this from a daemon, which of course works as root.

How can I: 1) as root, exit and then restart the application in a different user interface? 2) how to root, shut down, and then rerun the specific loginItem element for each user who has currently registered it?

I tried searching, and there are many discussions, including this similar question , but there seems to be no working solution.

Any help is greatly appreciated.

+3
source share
3 answers

So, I used a support request from Apple to get the best answer, combined with some research on the Internet.

The basic plan for the attack was to have each instance of the GUI restart when the daemon said this.

-, , GUI ( .app). , . Apple. , , .

daemon DistributedNotification GUI , . , restarter, pid bundle, script , pid, 10 , "open bundlepath.app", .

NSTask "in memory" script, @ "kill% @; sleep 10; open% @", pid, bundlePath....

!

!

+3

, NSDistributedNotificationCenter . , NSDistributedNotificationCenter root.

, , GUI- , - . NSWorkspace (NSWorkspaceSessionDidBecomeActiveNotification, NSWorkspaceSessionDidResignActiveNotification), , .., .

, GUI , 3 . , , , NSDistributedNotificationCenter, . - .

.h, :

extern NSString * const MDShouldTerminateImmediatelyNotification;

in (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 ( , , ).

:

TN2083

root Login

d

+3

" " Apple.

0
source

Source: https://habr.com/ru/post/1777792/


All Articles