Running at Logout (like Technical Note TN2228, but for logout)
Running at Logout (like Technical Note TN2228, but for logout)
- Subject: Running at Logout (like Technical Note TN2228, but for logout)
- From: Markus Hanauska <email@hidden>
- Date: Mon, 20 Jun 2011 14:00:44 +0200
I would like to write a little tool that syncs my preference files of various apps between different Macs. I have a couple of applications that have plenty of complicated settings and I would like to keep these settings always in sync between several different Macs. The problem is that I cannot use network synchronization, since one of those Macs is not always connected to a network and I would like this sync to work even without an active network connection. The solution: I will use an USB stick for it. The idea is pretty simple:
Prior to logging in, I plug in the USB stick, then I log into the Mac using my user account. The pref files are copied from the USB stick to my home folder (~/Library/Preferences). When I log out, the pref files are copied from my home folder back to the USB stick. The USB stick thus always contains the latest versions of these files. There is no need to merge settings, since I will always only be logged in to one of those Macs at all times and the settings of this Mac are always the new desired default settings. Ideally the tool will also support situations, where the USB stick is currently not plugged in during login or logout and prompt me for plugging it in before continuing the login/logout process.
There is just one big problem with that idea: The applications that read/write those pref files are login items, that means they will start immediately after logging in and keep running until I log out again. To make sure the apps start with the right prefs, I must replace the prefs prior to starting these apps (after login, but prior to login items) and since the sync can take a couple of seconds, I'd need a way to temporarily suspend the login until my tool is done. The same problem applies to logout: Since pref files are written asynchronously and thus the app might modify the pref file again right before quitting, my tool has to wait till the app has really quit before syncing the pref files, otherwise it may not sync the most up-to-date version. Thus my tool must run after all apps were quit, but prior to the real logout and of course it has to suspend the logout until it is done with copying files.
My first idea was to write a launchd agent, but I'm afraid such an agent is started way too late on login and terminated way too early on logout, so I dropped this idea pretty soon. The next idea was to write a launchd daemon that runs as root (thus having access to all user home directories), but this approach will not work, because in case of a file vault or a remote home, the daemon won't have access to the user home when the user is not currently logged in. So for the login problem the real solution seems to be an **authorization plug-in**. I can place it after the user home has been mounted, so it will have access to the user home files, but it can also "delay" the login process for as long as necessary (waiting for USB stick to be plugged-in, waiting for sync to finish or waiting for user to cancel sync), thus the sync will have finished for sure once the login items are being started. From what I've read so far regarding these plugins, this approach really seems to be the way to go.
The remaining problem is how to sync back on logout? There seems to be no way to write a authorization plug-in that runs on logout. Basically I need code that runs after all user processes have been terminated, but before the user home is unmounted, and that also can delay the logout process until all operations have been finished. Any way to do this?
A rather "crude" approach would be to start a launchd agent right after the login has succeeded that has an ExitTimeOut of 0, so launchd will send it a SIGTERM on logout, but it will never send it a SIGKILL. The agent could now catch the SIGTERM, wait for all other user processes to terminate (except for launchd and itself of course) and then start the sync process. The main problem with that approach: If my tool has a bug and somehow never terminates (nor crashes), the whole logout blocks forever. Also I'm not sure if the agent would be allowed to still start other processes (e.g. maybe a helper tool to display UI, since I'm not sure if I would like this agent to be Cocoa app) and if the agent is not "canceling" the whole logout by waiting too long. Maybe this is not a good idea after all, but the only one I could come up with :-(
Apple seems to have a better way to run lougout tasks, e.g. remote home synchronization, file vault compaction, etc.
Any ideas are pretty much welcome. Thanks.
Regards,
Markus_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden