1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "MacLaunchHelper.h"
10 #include "nsIAppStartup.h"
14 #include <crt_externs.h>
16 using namespace mozilla;
19 cpu_type_t pref_cpu_types[2] = {
22 #elif defined(__x86_64__)
24 #elif defined(__ppc__)
29 cpu_type_t cpu_i386_types[2] = {
33 cpu_type_t cpu_x64_86_types[2] = {
38 void LaunchChildMac(int aArgc, char** aArgv,
39 uint32_t aRestartType, pid_t *pid)
41 // "posix_spawnp" uses null termination for arguments rather than a count.
42 // Note that we are not duplicating the argument strings themselves.
43 nsAutoArrayPtr<char*> argv_copy(new char*[aArgc + 1]);
44 for (int i = 0; i < aArgc; i++) {
45 argv_copy[i] = aArgv[i];
47 argv_copy[aArgc] = NULL;
49 // Initialize spawn attributes.
50 posix_spawnattr_t spawnattr;
51 if (posix_spawnattr_init(&spawnattr) != 0) {
52 printf("Failed to init posix spawn attribute.");
56 cpu_type_t *wanted_type = pref_cpu_types;
57 size_t attr_count = ArrayLength(pref_cpu_types);
59 if (aRestartType & nsIAppStartup::eRestarti386) {
60 wanted_type = cpu_i386_types;
61 attr_count = ArrayLength(cpu_i386_types);
62 } else if (aRestartType & nsIAppStartup::eRestartx86_64) {
63 wanted_type = cpu_x64_86_types;
64 attr_count = ArrayLength(cpu_x64_86_types);
67 // Set spawn attributes.
68 size_t attr_ocount = 0;
69 if (posix_spawnattr_setbinpref_np(&spawnattr, attr_count, wanted_type, &attr_ocount) != 0 ||
70 attr_ocount != attr_count) {
71 printf("Failed to set binary preference on posix spawn attribute.");
72 posix_spawnattr_destroy(&spawnattr);
76 // Pass along our environment.
78 char*** cocoaEnvironment = _NSGetEnviron();
79 if (cocoaEnvironment) {
80 envp = *cocoaEnvironment;
83 int result = posix_spawnp(pid, argv_copy[0], NULL, &spawnattr, argv_copy, envp);
85 posix_spawnattr_destroy(&spawnattr);
88 printf("Process spawn failed with code %d!", result);