1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/xre/MacLaunchHelper.mm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,90 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "MacLaunchHelper.h" 1.10 + 1.11 +#include "nsMemory.h" 1.12 +#include "nsAutoPtr.h" 1.13 +#include "nsIAppStartup.h" 1.14 + 1.15 +#include <stdio.h> 1.16 +#include <spawn.h> 1.17 +#include <crt_externs.h> 1.18 + 1.19 +using namespace mozilla; 1.20 + 1.21 +namespace { 1.22 +cpu_type_t pref_cpu_types[2] = { 1.23 +#if defined(__i386__) 1.24 + CPU_TYPE_X86, 1.25 +#elif defined(__x86_64__) 1.26 + CPU_TYPE_X86_64, 1.27 +#elif defined(__ppc__) 1.28 + CPU_TYPE_POWERPC, 1.29 +#endif 1.30 + CPU_TYPE_ANY }; 1.31 + 1.32 +cpu_type_t cpu_i386_types[2] = { 1.33 + CPU_TYPE_X86, 1.34 + CPU_TYPE_ANY }; 1.35 + 1.36 +cpu_type_t cpu_x64_86_types[2] = { 1.37 + CPU_TYPE_X86_64, 1.38 + CPU_TYPE_ANY }; 1.39 +} 1.40 + 1.41 +void LaunchChildMac(int aArgc, char** aArgv, 1.42 + uint32_t aRestartType, pid_t *pid) 1.43 +{ 1.44 + // "posix_spawnp" uses null termination for arguments rather than a count. 1.45 + // Note that we are not duplicating the argument strings themselves. 1.46 + nsAutoArrayPtr<char*> argv_copy(new char*[aArgc + 1]); 1.47 + for (int i = 0; i < aArgc; i++) { 1.48 + argv_copy[i] = aArgv[i]; 1.49 + } 1.50 + argv_copy[aArgc] = NULL; 1.51 + 1.52 + // Initialize spawn attributes. 1.53 + posix_spawnattr_t spawnattr; 1.54 + if (posix_spawnattr_init(&spawnattr) != 0) { 1.55 + printf("Failed to init posix spawn attribute."); 1.56 + return; 1.57 + } 1.58 + 1.59 + cpu_type_t *wanted_type = pref_cpu_types; 1.60 + size_t attr_count = ArrayLength(pref_cpu_types); 1.61 + 1.62 + if (aRestartType & nsIAppStartup::eRestarti386) { 1.63 + wanted_type = cpu_i386_types; 1.64 + attr_count = ArrayLength(cpu_i386_types); 1.65 + } else if (aRestartType & nsIAppStartup::eRestartx86_64) { 1.66 + wanted_type = cpu_x64_86_types; 1.67 + attr_count = ArrayLength(cpu_x64_86_types); 1.68 + } 1.69 + 1.70 + // Set spawn attributes. 1.71 + size_t attr_ocount = 0; 1.72 + if (posix_spawnattr_setbinpref_np(&spawnattr, attr_count, wanted_type, &attr_ocount) != 0 || 1.73 + attr_ocount != attr_count) { 1.74 + printf("Failed to set binary preference on posix spawn attribute."); 1.75 + posix_spawnattr_destroy(&spawnattr); 1.76 + return; 1.77 + } 1.78 + 1.79 + // Pass along our environment. 1.80 + char** envp = NULL; 1.81 + char*** cocoaEnvironment = _NSGetEnviron(); 1.82 + if (cocoaEnvironment) { 1.83 + envp = *cocoaEnvironment; 1.84 + } 1.85 + 1.86 + int result = posix_spawnp(pid, argv_copy[0], NULL, &spawnattr, argv_copy, envp); 1.87 + 1.88 + posix_spawnattr_destroy(&spawnattr); 1.89 + 1.90 + if (result != 0) { 1.91 + printf("Process spawn failed with code %d!", result); 1.92 + } 1.93 +}