From bfea0b1673908af49b47f6a42dd545f828c950ec Mon Sep 17 00:00:00 2001 From: Greg Neagle Date: Fri, 14 Feb 2014 12:02:19 -0800 Subject: [PATCH] Use filesystem flag instead of NSDistributedNotification to signal that managedsoftwareupdate should stop. Turns out you can't send NSDistributedNotifications from user-land apps to background daemons launched by launchd. --- .../MSUMainWindowController.py | 9 +-- .../Managed Software Update/msulib.py | 2 +- code/client/managedsoftwareupdate | 5 -- code/client/munkilib/munkicommon.py | 67 +++---------------- 4 files changed, 12 insertions(+), 71 deletions(-) diff --git a/code/Managed Software Center/Managed Software Update/MSUMainWindowController.py b/code/Managed Software Center/Managed Software Update/MSUMainWindowController.py index eb56b6d0..0d87a1e8 100644 --- a/code/Managed Software Center/Managed Software Update/MSUMainWindowController.py +++ b/code/Managed Software Center/Managed Software Update/MSUMainWindowController.py @@ -1222,12 +1222,9 @@ class MSUMainWindowController(NSWindowController): self.disableStopButton() self._status_stopBtnState = 1 # send a notification that stop button was clicked - notification_center = NSDistributedNotificationCenter.defaultCenter() - notification_center.postNotificationName_object_userInfo_options_( - 'com.googlecode.munki.MunkiStatus.stopButtonClicked', - None, - None, - NSNotificationDeliverImmediately + NSNotificationPostToAllSessions) + STOP_REQUEST_FLAG = '/private/tmp/com.googlecode.munki.managedsoftwareupdate.stop_requested' + if not os.path.exists(STOP_REQUEST_FLAG): + open(STOP_REQUEST_FLAG, 'w').close() elif self.getUpdateCount() == 0: # no updates, this button must say "Check Again" diff --git a/code/Managed Software Center/Managed Software Update/msulib.py b/code/Managed Software Center/Managed Software Update/msulib.py index 7f922d18..7acab699 100644 --- a/code/Managed Software Center/Managed Software Update/msulib.py +++ b/code/Managed Software Center/Managed Software Update/msulib.py @@ -116,7 +116,7 @@ def filtered_html(text): def convertIconToPNG(app_name, destination_path, desired_size): - '''Converts an application icns file to a png file, chosing the representation + '''Converts an application icns file to a png file, choosing the representation closest to (but >= than if possible) the desired_size. Returns True if successful, False otherwise''' app_path = os.path.join('/Applications', app_name + '.app') diff --git a/code/client/managedsoftwareupdate b/code/client/managedsoftwareupdate index c0887114..fbb661b9 100755 --- a/code/client/managedsoftwareupdate +++ b/code/client/managedsoftwareupdate @@ -657,9 +657,6 @@ def main(): if applesoftwareupdatesonly and options.verbose: print ('NOTE: managedsoftwareupdate is configured to process Apple ' 'Software Updates only.') - - # TESTING, TESTING - munkicommon.initNotificationDelegate() updatecheckresult = None if not options.installonly and not applesoftwareupdatesonly: @@ -894,8 +891,6 @@ def main(): 'SuppressUserNotification is true.') munkicommon.cleanUpTmpDir() - # TESTING TESTING - munkicommon.removeNotificationDelegate() if mustlogout: # not handling this currently pass diff --git a/code/client/munkilib/munkicommon.py b/code/client/munkilib/munkicommon.py index c35cb626..3c2288ee 100755 --- a/code/client/munkilib/munkicommon.py +++ b/code/client/munkilib/munkicommon.py @@ -690,63 +690,6 @@ def archive_report(): # misc functions -class NotificationReceiver(NSObject): - '''An object that can receive NSDistributedNotifications and act upon them''' - stop_requested = False - - @classmethod - def create(cls): - '''Creates a NotificationReceiver and registers for notifications''' - notification_receiver_instance = cls.alloc().init() - if notification_receiver_instance: - notification_receiver_instance.register() - return notification_receiver_instance - - def destroy(self): - '''Unregisters for notifications and deallocates the object''' - self.unregister() - del(self) - - def unregister(self): - '''Tell the DistributedNotificationCenter to stop sending us notifications''' - NSDistributedNotificationCenter.defaultCenter().removeObserver_(self) - - def register(self): - '''Register for our notifications''' - notification_center = NSDistributedNotificationCenter.defaultCenter() - notification_center.addObserver_selector_name_object_suspensionBehavior_( - self, - self.shouldStop, - 'com.googlecode.munki.MunkiStatus.stopButtonClicked', - None, - NSNotificationSuspensionBehaviorDeliverImmediately) - - def shouldStop(self): - '''Delegate method called when a notification arrives''' - self.stop_requested = True - - def stopRequested(self): - '''Process an NSRunLoop so that notifications can be delivered, then return - our property''' - NSRunLoop.currentRunLoop().runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(.1)) - return self.stop_requested - - -_notificationdelegate = None -def initNotificationDelegate(): - '''Create a NotificationReceiver if needed, or return the existing one''' - global _notificationdelegate - if not _notificationdelegate: - _notificationdelegate = NotificationReceiver.create() - - -def removeNotificationDelegate(): - '''Destroy the NotificationReceiver if it exists''' - global _notificationdelegate - if _notificationdelegate: - _notificationdelegate.destroy() - _notificationdelegate = None - def validPlist(path): """Uses plutil to determine if path contains a valid plist. @@ -761,9 +704,15 @@ def validPlist(path): def stopRequested(): """Allows user to cancel operations when MunkiStatus is being used""" - if _notificationdelegate: - if _notificationdelegate.stopRequested(): + STOP_REQUEST_FLAG = '/private/tmp/com.googlecode.munki.managedsoftwareupdate.stop_requested' + if munkistatusoutput: + if os.path.exists(STOP_REQUEST_FLAG): log('### User stopped session ###') + try: + os.unlink(STOP_REQUEST_FLAG) + except OSError, err: + display_error( + 'Could not remove %s: %s', STOP_REQUEST_FLAG, err) return True return False