Bumping manifests a=b2g-bump
[gecko.git] / toolkit / xre / MacLaunchHelper.mm
blobe11aa4e2af3835dc06dd33d048b3435b7108f4f3
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"
8 #include "nsMemory.h"
9 #include "nsAutoPtr.h"
10 #include "nsIAppStartup.h"
12 #include <stdio.h>
13 #include <spawn.h>
14 #include <crt_externs.h>
16 using namespace mozilla;
18 namespace {
19 cpu_type_t pref_cpu_types[2] = {
20 #if defined(__i386__)
21                                  CPU_TYPE_X86,
22 #elif defined(__x86_64__)
23                                  CPU_TYPE_X86_64,
24 #elif defined(__ppc__)
25                                  CPU_TYPE_POWERPC,
26 #endif
27                                  CPU_TYPE_ANY };
29 cpu_type_t cpu_i386_types[2] = {
30                                  CPU_TYPE_X86,
31                                  CPU_TYPE_ANY };
33 cpu_type_t cpu_x64_86_types[2] = {
34                                  CPU_TYPE_X86_64,
35                                  CPU_TYPE_ANY };
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];
46   }
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.");
53     return;
54   }
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);
65   }
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);
73     return;
74   }
76   // Pass along our environment.
77   char** envp = NULL;
78   char*** cocoaEnvironment = _NSGetEnviron();
79   if (cocoaEnvironment) {
80     envp = *cocoaEnvironment;
81   }
83   int result = posix_spawnp(pid, argv_copy[0], NULL, &spawnattr, argv_copy, envp);
85   posix_spawnattr_destroy(&spawnattr);
87   if (result != 0) {
88     printf("Process spawn failed with code %d!", result);
89   }