Hi There,
I have to achieve following scenario
Track system event on macosx for shutdown and restart and update one plist with same event via launchAgent
I have tried following code on launchAgent
class MyAgent {
init() {
let notificationCenter = NSWorkspace.shared.notificationCenter
// Register for system shutdown notification
notificationCenter.addObserver(self,
selector: #selector(handleNotification(_:)),
name: NSWorkspace.willPowerOffNotification,
object: nil)
RunLoop.current.run()
}
@objc func handleNotification(_ notification: Notification) {
var logMessage = ""
switch notification.name {
case NSWorkspace.willPowerOffNotification:
os_log("System is going to shut down at", log: log, type: .default)
updatePlistFile(event: "shut down")
let fileName = "example.txt"
let content = "shut down"
createAndWriteFile(fileName: fileName, content: content)
logMessage = "System is going to shut down at \(Date())\n"
}
}
}
loaded the agent, and tried to restart device, I can't see as it is coming to handleNotification
Same code is working fine from sample application but not from launchAgent
Is there any restriction is there for NSWorkspace, if is that so, how to track shutdown/restart event from launchAgent or LaunchDaemon
Any help will be appreciate
(getting back to this a bit late)
My larger goal here is at the lock screen I want to detect what events occured to reach this lock screen,
To do what? Some of what you're talking about here sounds like you might be better off modifying the login process more "directly", for example by using SFAuthorizationView.
Looking at you cases here:
restart or shutdown
There are two different issues here:
-
Generally speaking, the larger "system" doesn't really differentiate between "restart" and "shutdown". Conceptually, a restart is basically just a shutdown which happens to be followed by an immediately scheduled startup. The system does have some "awareness" of it (basically for logging/diagnostic purposes), but we don't really want to create or encourage a situation where the startup sequence changes dramatically based on how the machine shutdown.
-
The first login screen you see post-boot can be very, very complicated. On Intel mac, that screen is NOT part of "the booting system" but is actually an EFI "application". That dialog collects the login data, uses it to decrypt the boot data, then continues the boot process normally. The initial credentials received by EFI are passed into the kernel and then (eventually) used to complete the login process. The ARM process is different, but I believe there is a similar dynamic where the initial login process doesn't actually occur in the normal system context.
lock,?
I mentioned SFAuthorizationView above and this is why. The unlock screen you see over an account is the same screen/state as the login window. You can configure SFAuthorizationView to handle a variety of different states, including the login and lock screens, but the states themselves are completely different.
is it logout,
The typical reason developers are trying to "detect" this sort of event is because they want to do "something" when the user is logged in, in which case the right answer it to use a LaunchAgent which communicates with your daemon. TN2083: Daemons and Agents covers this in more detail.
Is there any kernel level
No. The important thing to keep in mind here is that the kernel isn't really involved with this "level" of the system. As far as it's concerned, it's job is to manage a basically "arbitrary" collection of resources (processes, threads, memory, mach ports, etc...). It doesn't really track any higher level concept (like "login"), particularly not in any kind of systemic way.
file or any other changes I can track from daemon to get to know this
No, not that I'm aware of.
__
Kevin Elliott
DTS Engineer, CoreOS/Hardware