michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include "readstrings.h" michael@0: michael@0: // Prefer the currently running architecture (this is the same as the michael@0: // architecture that launched the updater) and fallback to CPU_TYPE_ANY if it michael@0: // is no longer available after the update. michael@0: static cpu_type_t pref_cpu_types[2] = { michael@0: #if defined(__i386__) michael@0: CPU_TYPE_X86, michael@0: #elif defined(__x86_64__) michael@0: CPU_TYPE_X86_64, michael@0: #elif defined(__ppc__) michael@0: CPU_TYPE_POWERPC, michael@0: #endif michael@0: CPU_TYPE_ANY }; michael@0: michael@0: void LaunchChild(int argc, char **argv) michael@0: { michael@0: // Initialize spawn attributes. michael@0: posix_spawnattr_t spawnattr; michael@0: if (posix_spawnattr_init(&spawnattr) != 0) { michael@0: printf("Failed to init posix spawn attribute."); michael@0: return; michael@0: } michael@0: michael@0: // Set spawn attributes. michael@0: size_t attr_count = 2; michael@0: size_t attr_ocount = 0; michael@0: if (posix_spawnattr_setbinpref_np(&spawnattr, attr_count, pref_cpu_types, &attr_ocount) != 0 || michael@0: attr_ocount != attr_count) { michael@0: printf("Failed to set binary preference on posix spawn attribute."); michael@0: posix_spawnattr_destroy(&spawnattr); michael@0: return; michael@0: } michael@0: michael@0: // "posix_spawnp" uses null termination for arguments rather than a count. michael@0: // Note that we are not duplicating the argument strings themselves. michael@0: char** argv_copy = (char**)malloc((argc + 1) * sizeof(char*)); michael@0: if (!argv_copy) { michael@0: printf("Failed to allocate memory for arguments."); michael@0: posix_spawnattr_destroy(&spawnattr); michael@0: return; michael@0: } michael@0: for (int i = 0; i < argc; i++) { michael@0: argv_copy[i] = argv[i]; michael@0: } michael@0: argv_copy[argc] = NULL; michael@0: michael@0: // Pass along our environment. michael@0: char** envp = NULL; michael@0: char*** cocoaEnvironment = _NSGetEnviron(); michael@0: if (cocoaEnvironment) { michael@0: envp = *cocoaEnvironment; michael@0: } michael@0: michael@0: int result = posix_spawnp(NULL, argv_copy[0], NULL, &spawnattr, argv_copy, envp); michael@0: michael@0: free(argv_copy); michael@0: posix_spawnattr_destroy(&spawnattr); michael@0: michael@0: if (result != 0) { michael@0: printf("Process spawn failed with code %d!", result); michael@0: } michael@0: } michael@0: michael@0: void michael@0: LaunchMacPostProcess(const char* aAppExe) michael@0: { michael@0: // Launch helper to perform post processing for the update; this is the Mac michael@0: // analogue of LaunchWinPostProcess (PostUpdateWin). michael@0: NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; michael@0: michael@0: // Find the app bundle containing the executable path given michael@0: NSString *path = [NSString stringWithUTF8String:aAppExe]; michael@0: NSBundle *bundle; michael@0: do { michael@0: path = [path stringByDeletingLastPathComponent]; michael@0: bundle = [NSBundle bundleWithPath:path]; michael@0: } while ((!bundle || ![bundle bundleIdentifier]) && [path length] > 1); michael@0: if (!bundle) { michael@0: // No bundle found for the app being launched michael@0: [pool release]; michael@0: return; michael@0: } michael@0: michael@0: NSString *iniPath = [bundle pathForResource:@"updater" ofType:@"ini"]; michael@0: if (!iniPath) { michael@0: // the file does not exist; there is nothing to run michael@0: [pool release]; michael@0: return; michael@0: } michael@0: michael@0: int readResult; michael@0: char values[2][MAX_TEXT_LEN]; michael@0: readResult = ReadStrings([iniPath UTF8String], michael@0: "ExeArg\0ExeRelPath\0", michael@0: 2, michael@0: values, michael@0: "PostUpdateMac"); michael@0: if (readResult) { michael@0: [pool release]; michael@0: return; michael@0: } michael@0: michael@0: NSString *exeArg = [NSString stringWithUTF8String:values[0]]; michael@0: NSString *exeRelPath = [NSString stringWithUTF8String:values[1]]; michael@0: if (!exeArg || !exeRelPath) { michael@0: [pool release]; michael@0: return; michael@0: } michael@0: michael@0: NSString *resourcePath = [bundle resourcePath]; michael@0: NSString *exeFullPath = [resourcePath stringByAppendingPathComponent:exeRelPath]; michael@0: michael@0: NSTask *task = [[NSTask alloc] init]; michael@0: [task setLaunchPath:exeFullPath]; michael@0: [task setArguments:[NSArray arrayWithObject:exeArg]]; michael@0: [task launch]; michael@0: [task waitUntilExit]; michael@0: // ignore the return value of the task, there's nothing we can do with it michael@0: [task release]; michael@0: michael@0: [pool release]; michael@0: } michael@0: