|
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/. */ |
|
5 |
|
6 #include "MacLaunchHelper.h" |
|
7 |
|
8 #include "nsMemory.h" |
|
9 #include "nsAutoPtr.h" |
|
10 #include "nsIAppStartup.h" |
|
11 |
|
12 #include <stdio.h> |
|
13 #include <spawn.h> |
|
14 #include <crt_externs.h> |
|
15 |
|
16 using namespace mozilla; |
|
17 |
|
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 }; |
|
28 |
|
29 cpu_type_t cpu_i386_types[2] = { |
|
30 CPU_TYPE_X86, |
|
31 CPU_TYPE_ANY }; |
|
32 |
|
33 cpu_type_t cpu_x64_86_types[2] = { |
|
34 CPU_TYPE_X86_64, |
|
35 CPU_TYPE_ANY }; |
|
36 } |
|
37 |
|
38 void LaunchChildMac(int aArgc, char** aArgv, |
|
39 uint32_t aRestartType, pid_t *pid) |
|
40 { |
|
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; |
|
48 |
|
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 } |
|
55 |
|
56 cpu_type_t *wanted_type = pref_cpu_types; |
|
57 size_t attr_count = ArrayLength(pref_cpu_types); |
|
58 |
|
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 } |
|
66 |
|
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 } |
|
75 |
|
76 // Pass along our environment. |
|
77 char** envp = NULL; |
|
78 char*** cocoaEnvironment = _NSGetEnviron(); |
|
79 if (cocoaEnvironment) { |
|
80 envp = *cocoaEnvironment; |
|
81 } |
|
82 |
|
83 int result = posix_spawnp(pid, argv_copy[0], NULL, &spawnattr, argv_copy, envp); |
|
84 |
|
85 posix_spawnattr_destroy(&spawnattr); |
|
86 |
|
87 if (result != 0) { |
|
88 printf("Process spawn failed with code %d!", result); |
|
89 } |
|
90 } |