toolkit/mozapps/update/updater/launchchild_osx.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 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include <Cocoa/Cocoa.h>
michael@0 8 #include <CoreServices/CoreServices.h>
michael@0 9 #include <crt_externs.h>
michael@0 10 #include <stdlib.h>
michael@0 11 #include <stdio.h>
michael@0 12 #include <spawn.h>
michael@0 13 #include "readstrings.h"
michael@0 14
michael@0 15 // Prefer the currently running architecture (this is the same as the
michael@0 16 // architecture that launched the updater) and fallback to CPU_TYPE_ANY if it
michael@0 17 // is no longer available after the update.
michael@0 18 static cpu_type_t pref_cpu_types[2] = {
michael@0 19 #if defined(__i386__)
michael@0 20 CPU_TYPE_X86,
michael@0 21 #elif defined(__x86_64__)
michael@0 22 CPU_TYPE_X86_64,
michael@0 23 #elif defined(__ppc__)
michael@0 24 CPU_TYPE_POWERPC,
michael@0 25 #endif
michael@0 26 CPU_TYPE_ANY };
michael@0 27
michael@0 28 void LaunchChild(int argc, char **argv)
michael@0 29 {
michael@0 30 // Initialize spawn attributes.
michael@0 31 posix_spawnattr_t spawnattr;
michael@0 32 if (posix_spawnattr_init(&spawnattr) != 0) {
michael@0 33 printf("Failed to init posix spawn attribute.");
michael@0 34 return;
michael@0 35 }
michael@0 36
michael@0 37 // Set spawn attributes.
michael@0 38 size_t attr_count = 2;
michael@0 39 size_t attr_ocount = 0;
michael@0 40 if (posix_spawnattr_setbinpref_np(&spawnattr, attr_count, pref_cpu_types, &attr_ocount) != 0 ||
michael@0 41 attr_ocount != attr_count) {
michael@0 42 printf("Failed to set binary preference on posix spawn attribute.");
michael@0 43 posix_spawnattr_destroy(&spawnattr);
michael@0 44 return;
michael@0 45 }
michael@0 46
michael@0 47 // "posix_spawnp" uses null termination for arguments rather than a count.
michael@0 48 // Note that we are not duplicating the argument strings themselves.
michael@0 49 char** argv_copy = (char**)malloc((argc + 1) * sizeof(char*));
michael@0 50 if (!argv_copy) {
michael@0 51 printf("Failed to allocate memory for arguments.");
michael@0 52 posix_spawnattr_destroy(&spawnattr);
michael@0 53 return;
michael@0 54 }
michael@0 55 for (int i = 0; i < argc; i++) {
michael@0 56 argv_copy[i] = argv[i];
michael@0 57 }
michael@0 58 argv_copy[argc] = NULL;
michael@0 59
michael@0 60 // Pass along our environment.
michael@0 61 char** envp = NULL;
michael@0 62 char*** cocoaEnvironment = _NSGetEnviron();
michael@0 63 if (cocoaEnvironment) {
michael@0 64 envp = *cocoaEnvironment;
michael@0 65 }
michael@0 66
michael@0 67 int result = posix_spawnp(NULL, argv_copy[0], NULL, &spawnattr, argv_copy, envp);
michael@0 68
michael@0 69 free(argv_copy);
michael@0 70 posix_spawnattr_destroy(&spawnattr);
michael@0 71
michael@0 72 if (result != 0) {
michael@0 73 printf("Process spawn failed with code %d!", result);
michael@0 74 }
michael@0 75 }
michael@0 76
michael@0 77 void
michael@0 78 LaunchMacPostProcess(const char* aAppExe)
michael@0 79 {
michael@0 80 // Launch helper to perform post processing for the update; this is the Mac
michael@0 81 // analogue of LaunchWinPostProcess (PostUpdateWin).
michael@0 82 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
michael@0 83
michael@0 84 // Find the app bundle containing the executable path given
michael@0 85 NSString *path = [NSString stringWithUTF8String:aAppExe];
michael@0 86 NSBundle *bundle;
michael@0 87 do {
michael@0 88 path = [path stringByDeletingLastPathComponent];
michael@0 89 bundle = [NSBundle bundleWithPath:path];
michael@0 90 } while ((!bundle || ![bundle bundleIdentifier]) && [path length] > 1);
michael@0 91 if (!bundle) {
michael@0 92 // No bundle found for the app being launched
michael@0 93 [pool release];
michael@0 94 return;
michael@0 95 }
michael@0 96
michael@0 97 NSString *iniPath = [bundle pathForResource:@"updater" ofType:@"ini"];
michael@0 98 if (!iniPath) {
michael@0 99 // the file does not exist; there is nothing to run
michael@0 100 [pool release];
michael@0 101 return;
michael@0 102 }
michael@0 103
michael@0 104 int readResult;
michael@0 105 char values[2][MAX_TEXT_LEN];
michael@0 106 readResult = ReadStrings([iniPath UTF8String],
michael@0 107 "ExeArg\0ExeRelPath\0",
michael@0 108 2,
michael@0 109 values,
michael@0 110 "PostUpdateMac");
michael@0 111 if (readResult) {
michael@0 112 [pool release];
michael@0 113 return;
michael@0 114 }
michael@0 115
michael@0 116 NSString *exeArg = [NSString stringWithUTF8String:values[0]];
michael@0 117 NSString *exeRelPath = [NSString stringWithUTF8String:values[1]];
michael@0 118 if (!exeArg || !exeRelPath) {
michael@0 119 [pool release];
michael@0 120 return;
michael@0 121 }
michael@0 122
michael@0 123 NSString *resourcePath = [bundle resourcePath];
michael@0 124 NSString *exeFullPath = [resourcePath stringByAppendingPathComponent:exeRelPath];
michael@0 125
michael@0 126 NSTask *task = [[NSTask alloc] init];
michael@0 127 [task setLaunchPath:exeFullPath];
michael@0 128 [task setArguments:[NSArray arrayWithObject:exeArg]];
michael@0 129 [task launch];
michael@0 130 [task waitUntilExit];
michael@0 131 // ignore the return value of the task, there's nothing we can do with it
michael@0 132 [task release];
michael@0 133
michael@0 134 [pool release];
michael@0 135 }
michael@0 136

mercurial