From 9b936f6a1de7d16924e6496bdeac9206f215d5f8 Mon Sep 17 00:00:00 2001 From: Oleg Krupnov Date: Thu, 9 May 2013 13:01:07 +0300 Subject: [PATCH] Updated relaunch script, disabled relaunch attempts for sandboxed mode --- DFApplication.m | 38 ++++++++++++++++++++++++-------------- DFRelaunch.sh | 34 +++++++++++++++++++++++----------- 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/DFApplication.m b/DFApplication.m index d469e46..8175eff 100644 --- a/DFApplication.m +++ b/DFApplication.m @@ -5,6 +5,7 @@ #import "DFApplication.h" #import "DFCrashReportWindowController.h" +#import "ApplicationSandboxInfo.h" //------------------------------------------------------------------------------------------------- static NSString* const kUserDefaultCrashSequenceCount = @"DFApplication_crashSequenceCount"; @@ -36,19 +37,28 @@ - (void)dealloc //------------------------------------------------------------------------------------------------- - (void)launchAnotherInstanceAndWaitForTermination { - _isRelaunching = YES; - - // launch a script that waits for the app to exit and then relaunches it - NSString* scriptPath = [[NSBundle mainBundle] pathForResource:@"DFRelaunch" ofType:@"sh"]; - NSString* bundlePath = [NSString stringWithFormat:@"%s", [NSBundle mainBundle].executablePath.fileSystemRepresentation]; - NSString* processIdentifier = [NSString stringWithFormat:@"%d", [NSProcessInfo processInfo].processIdentifier]; - NSArray* arguments = @[scriptPath, - bundlePath, - processIdentifier]; - NSTask* task = [[[NSTask alloc] init] autorelease]; - task.launchPath = @"/bin/bash"; - task.arguments = arguments; - [task launch]; + if (![ApplicationSandboxInfo isSandboxed]) + { + _isRelaunching = YES; + + // launch a script that waits for the app to exit and then relaunches it + NSString* scriptPath = [[NSBundle mainBundle] pathForResource:@"DFRelaunch" ofType:@"sh"]; + NSString* bundlePath = [NSString stringWithFormat:@"%s", [NSBundle mainBundle].executablePath.fileSystemRepresentation]; + NSString* processIdentifier = [NSString stringWithFormat:@"%d", [NSProcessInfo processInfo].processIdentifier]; + NSString* bundleId = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"]; + NSArray* arguments = @[scriptPath, + bundlePath, + processIdentifier, + bundleId]; + NSTask* task = [[[NSTask alloc] init] autorelease]; + task.launchPath = @"/bin/bash"; + task.arguments = arguments; + [task launch]; + } + else + { + NSLog(@"Cannot relaunch sandboxed application"); + } } //------------------------------------------------------------------------------------------------- @@ -56,7 +66,7 @@ - (void)relaunch { // prevent endless loop of relaunch and crash NSUInteger crashSequenceCount = [[NSUserDefaults standardUserDefaults] integerForKey:kUserDefaultCrashSequenceCount]; - if (crashSequenceCount < kCrashSequenceCountMax - 1) + if (crashSequenceCount < kCrashSequenceCountMax - 1) { [self launchAnotherInstanceAndWaitForTermination]; } diff --git a/DFRelaunch.sh b/DFRelaunch.sh index 59179be..9854a57 100755 --- a/DFRelaunch.sh +++ b/DFRelaunch.sh @@ -8,21 +8,33 @@ #------------------------------------------------------------------------------------------------- # Bash script to relaunch the app -# give app a chance to terminate within 10 seconds +processPath=$1 +processId=$2 +processBundleId=$3 + +# waiting cycle i="10" while [ $i -gt 0 ] do - sleep 1 - - is_running=$(osascript -e 'tell application "System Events" to exists (process 1 whose unix id is '$2')') - echo "Waiting for shutdown: " $is_running - if [ "$is_running" = "false" ]; then - i="0" - else - i=$(($i - 1)) - fi +# wait one second +sleep 0.5 + +# check if running +instanceCount=$(ps -p $processId | grep $processPath | wc -l) + +if [ $instanceCount -gt 0 ] +then +# wait again +echo Still running $instanceCount instances of application, $i attempts left; +i=$(($i - 1)) + +else +# exit cycle +i="0" +fi done # relaunch the app -osascript -e 'tell application "'"$1"'" to activate' +echo Relaunching application +open -n -b $processBundleId