toolkit/xre/MacLaunchHelper.mm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "MacLaunchHelper.h"
michael@0 7
michael@0 8 #include "nsMemory.h"
michael@0 9 #include "nsAutoPtr.h"
michael@0 10 #include "nsIAppStartup.h"
michael@0 11
michael@0 12 #include <stdio.h>
michael@0 13 #include <spawn.h>
michael@0 14 #include <crt_externs.h>
michael@0 15
michael@0 16 using namespace mozilla;
michael@0 17
michael@0 18 namespace {
michael@0 19 cpu_type_t pref_cpu_types[2] = {
michael@0 20 #if defined(__i386__)
michael@0 21 CPU_TYPE_X86,
michael@0 22 #elif defined(__x86_64__)
michael@0 23 CPU_TYPE_X86_64,
michael@0 24 #elif defined(__ppc__)
michael@0 25 CPU_TYPE_POWERPC,
michael@0 26 #endif
michael@0 27 CPU_TYPE_ANY };
michael@0 28
michael@0 29 cpu_type_t cpu_i386_types[2] = {
michael@0 30 CPU_TYPE_X86,
michael@0 31 CPU_TYPE_ANY };
michael@0 32
michael@0 33 cpu_type_t cpu_x64_86_types[2] = {
michael@0 34 CPU_TYPE_X86_64,
michael@0 35 CPU_TYPE_ANY };
michael@0 36 }
michael@0 37
michael@0 38 void LaunchChildMac(int aArgc, char** aArgv,
michael@0 39 uint32_t aRestartType, pid_t *pid)
michael@0 40 {
michael@0 41 // "posix_spawnp" uses null termination for arguments rather than a count.
michael@0 42 // Note that we are not duplicating the argument strings themselves.
michael@0 43 nsAutoArrayPtr<char*> argv_copy(new char*[aArgc + 1]);
michael@0 44 for (int i = 0; i < aArgc; i++) {
michael@0 45 argv_copy[i] = aArgv[i];
michael@0 46 }
michael@0 47 argv_copy[aArgc] = NULL;
michael@0 48
michael@0 49 // Initialize spawn attributes.
michael@0 50 posix_spawnattr_t spawnattr;
michael@0 51 if (posix_spawnattr_init(&spawnattr) != 0) {
michael@0 52 printf("Failed to init posix spawn attribute.");
michael@0 53 return;
michael@0 54 }
michael@0 55
michael@0 56 cpu_type_t *wanted_type = pref_cpu_types;
michael@0 57 size_t attr_count = ArrayLength(pref_cpu_types);
michael@0 58
michael@0 59 if (aRestartType & nsIAppStartup::eRestarti386) {
michael@0 60 wanted_type = cpu_i386_types;
michael@0 61 attr_count = ArrayLength(cpu_i386_types);
michael@0 62 } else if (aRestartType & nsIAppStartup::eRestartx86_64) {
michael@0 63 wanted_type = cpu_x64_86_types;
michael@0 64 attr_count = ArrayLength(cpu_x64_86_types);
michael@0 65 }
michael@0 66
michael@0 67 // Set spawn attributes.
michael@0 68 size_t attr_ocount = 0;
michael@0 69 if (posix_spawnattr_setbinpref_np(&spawnattr, attr_count, wanted_type, &attr_ocount) != 0 ||
michael@0 70 attr_ocount != attr_count) {
michael@0 71 printf("Failed to set binary preference on posix spawn attribute.");
michael@0 72 posix_spawnattr_destroy(&spawnattr);
michael@0 73 return;
michael@0 74 }
michael@0 75
michael@0 76 // Pass along our environment.
michael@0 77 char** envp = NULL;
michael@0 78 char*** cocoaEnvironment = _NSGetEnviron();
michael@0 79 if (cocoaEnvironment) {
michael@0 80 envp = *cocoaEnvironment;
michael@0 81 }
michael@0 82
michael@0 83 int result = posix_spawnp(pid, argv_copy[0], NULL, &spawnattr, argv_copy, envp);
michael@0 84
michael@0 85 posix_spawnattr_destroy(&spawnattr);
michael@0 86
michael@0 87 if (result != 0) {
michael@0 88 printf("Process spawn failed with code %d!", result);
michael@0 89 }
michael@0 90 }

mercurial