1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/update/updater/launchchild_osx.mm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,136 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include <Cocoa/Cocoa.h> 1.11 +#include <CoreServices/CoreServices.h> 1.12 +#include <crt_externs.h> 1.13 +#include <stdlib.h> 1.14 +#include <stdio.h> 1.15 +#include <spawn.h> 1.16 +#include "readstrings.h" 1.17 + 1.18 +// Prefer the currently running architecture (this is the same as the 1.19 +// architecture that launched the updater) and fallback to CPU_TYPE_ANY if it 1.20 +// is no longer available after the update. 1.21 +static cpu_type_t pref_cpu_types[2] = { 1.22 +#if defined(__i386__) 1.23 + CPU_TYPE_X86, 1.24 +#elif defined(__x86_64__) 1.25 + CPU_TYPE_X86_64, 1.26 +#elif defined(__ppc__) 1.27 + CPU_TYPE_POWERPC, 1.28 +#endif 1.29 + CPU_TYPE_ANY }; 1.30 + 1.31 +void LaunchChild(int argc, char **argv) 1.32 +{ 1.33 + // Initialize spawn attributes. 1.34 + posix_spawnattr_t spawnattr; 1.35 + if (posix_spawnattr_init(&spawnattr) != 0) { 1.36 + printf("Failed to init posix spawn attribute."); 1.37 + return; 1.38 + } 1.39 + 1.40 + // Set spawn attributes. 1.41 + size_t attr_count = 2; 1.42 + size_t attr_ocount = 0; 1.43 + if (posix_spawnattr_setbinpref_np(&spawnattr, attr_count, pref_cpu_types, &attr_ocount) != 0 || 1.44 + attr_ocount != attr_count) { 1.45 + printf("Failed to set binary preference on posix spawn attribute."); 1.46 + posix_spawnattr_destroy(&spawnattr); 1.47 + return; 1.48 + } 1.49 + 1.50 + // "posix_spawnp" uses null termination for arguments rather than a count. 1.51 + // Note that we are not duplicating the argument strings themselves. 1.52 + char** argv_copy = (char**)malloc((argc + 1) * sizeof(char*)); 1.53 + if (!argv_copy) { 1.54 + printf("Failed to allocate memory for arguments."); 1.55 + posix_spawnattr_destroy(&spawnattr); 1.56 + return; 1.57 + } 1.58 + for (int i = 0; i < argc; i++) { 1.59 + argv_copy[i] = argv[i]; 1.60 + } 1.61 + argv_copy[argc] = NULL; 1.62 + 1.63 + // Pass along our environment. 1.64 + char** envp = NULL; 1.65 + char*** cocoaEnvironment = _NSGetEnviron(); 1.66 + if (cocoaEnvironment) { 1.67 + envp = *cocoaEnvironment; 1.68 + } 1.69 + 1.70 + int result = posix_spawnp(NULL, argv_copy[0], NULL, &spawnattr, argv_copy, envp); 1.71 + 1.72 + free(argv_copy); 1.73 + posix_spawnattr_destroy(&spawnattr); 1.74 + 1.75 + if (result != 0) { 1.76 + printf("Process spawn failed with code %d!", result); 1.77 + } 1.78 +} 1.79 + 1.80 +void 1.81 +LaunchMacPostProcess(const char* aAppExe) 1.82 +{ 1.83 + // Launch helper to perform post processing for the update; this is the Mac 1.84 + // analogue of LaunchWinPostProcess (PostUpdateWin). 1.85 + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 1.86 + 1.87 + // Find the app bundle containing the executable path given 1.88 + NSString *path = [NSString stringWithUTF8String:aAppExe]; 1.89 + NSBundle *bundle; 1.90 + do { 1.91 + path = [path stringByDeletingLastPathComponent]; 1.92 + bundle = [NSBundle bundleWithPath:path]; 1.93 + } while ((!bundle || ![bundle bundleIdentifier]) && [path length] > 1); 1.94 + if (!bundle) { 1.95 + // No bundle found for the app being launched 1.96 + [pool release]; 1.97 + return; 1.98 + } 1.99 + 1.100 + NSString *iniPath = [bundle pathForResource:@"updater" ofType:@"ini"]; 1.101 + if (!iniPath) { 1.102 + // the file does not exist; there is nothing to run 1.103 + [pool release]; 1.104 + return; 1.105 + } 1.106 + 1.107 + int readResult; 1.108 + char values[2][MAX_TEXT_LEN]; 1.109 + readResult = ReadStrings([iniPath UTF8String], 1.110 + "ExeArg\0ExeRelPath\0", 1.111 + 2, 1.112 + values, 1.113 + "PostUpdateMac"); 1.114 + if (readResult) { 1.115 + [pool release]; 1.116 + return; 1.117 + } 1.118 + 1.119 + NSString *exeArg = [NSString stringWithUTF8String:values[0]]; 1.120 + NSString *exeRelPath = [NSString stringWithUTF8String:values[1]]; 1.121 + if (!exeArg || !exeRelPath) { 1.122 + [pool release]; 1.123 + return; 1.124 + } 1.125 + 1.126 + NSString *resourcePath = [bundle resourcePath]; 1.127 + NSString *exeFullPath = [resourcePath stringByAppendingPathComponent:exeRelPath]; 1.128 + 1.129 + NSTask *task = [[NSTask alloc] init]; 1.130 + [task setLaunchPath:exeFullPath]; 1.131 + [task setArguments:[NSArray arrayWithObject:exeArg]]; 1.132 + [task launch]; 1.133 + [task waitUntilExit]; 1.134 + // ignore the return value of the task, there's nothing we can do with it 1.135 + [task release]; 1.136 + 1.137 + [pool release]; 1.138 +} 1.139 +