Skip to content

Commit

Permalink
Updated relaunch script, disabled relaunch attempts for sandboxed mode
Browse files Browse the repository at this point in the history
  • Loading branch information
AJet committed May 9, 2013
1 parent 1df1f11 commit 9b936f6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
38 changes: 24 additions & 14 deletions DFApplication.m
Expand Up @@ -5,6 +5,7 @@

#import "DFApplication.h"
#import "DFCrashReportWindowController.h"
#import "ApplicationSandboxInfo.h"

//-------------------------------------------------------------------------------------------------
static NSString* const kUserDefaultCrashSequenceCount = @"DFApplication_crashSequenceCount";
Expand Down Expand Up @@ -36,27 +37,36 @@ - (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");
}
}

//-------------------------------------------------------------------------------------------------
- (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];
}
Expand Down
34 changes: 23 additions & 11 deletions DFRelaunch.sh
Expand Up @@ -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

0 comments on commit 9b936f6

Please sign in to comment.