michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 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 "MacLaunchHelper.h" michael@0: michael@0: #include "nsMemory.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsIAppStartup.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: using namespace mozilla; michael@0: michael@0: namespace { michael@0: 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: cpu_type_t cpu_i386_types[2] = { michael@0: CPU_TYPE_X86, michael@0: CPU_TYPE_ANY }; michael@0: michael@0: cpu_type_t cpu_x64_86_types[2] = { michael@0: CPU_TYPE_X86_64, michael@0: CPU_TYPE_ANY }; michael@0: } michael@0: michael@0: void LaunchChildMac(int aArgc, char** aArgv, michael@0: uint32_t aRestartType, pid_t *pid) 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: nsAutoArrayPtr argv_copy(new char*[aArgc + 1]); michael@0: for (int i = 0; i < aArgc; i++) { michael@0: argv_copy[i] = aArgv[i]; michael@0: } michael@0: argv_copy[aArgc] = NULL; 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: cpu_type_t *wanted_type = pref_cpu_types; michael@0: size_t attr_count = ArrayLength(pref_cpu_types); michael@0: michael@0: if (aRestartType & nsIAppStartup::eRestarti386) { michael@0: wanted_type = cpu_i386_types; michael@0: attr_count = ArrayLength(cpu_i386_types); michael@0: } else if (aRestartType & nsIAppStartup::eRestartx86_64) { michael@0: wanted_type = cpu_x64_86_types; michael@0: attr_count = ArrayLength(cpu_x64_86_types); michael@0: } michael@0: michael@0: // Set spawn attributes. michael@0: size_t attr_ocount = 0; michael@0: if (posix_spawnattr_setbinpref_np(&spawnattr, attr_count, wanted_type, &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: // 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(pid, argv_copy[0], NULL, &spawnattr, argv_copy, envp); michael@0: 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: }