1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include <Cocoa/Cocoa.h>
8 #include <CoreServices/CoreServices.h>
9 #include <crt_externs.h>
13 #include "readstrings.h"
15 // Prefer the currently running architecture (this is the same as the
16 // architecture that launched the updater) and fallback to CPU_TYPE_ANY if it
17 // is no longer available after the update.
18 static cpu_type_t pref_cpu_types[2] = {
21 #elif defined(__x86_64__)
23 #elif defined(__ppc__)
28 void LaunchChild(int argc, char **argv)
30 // Initialize spawn attributes.
31 posix_spawnattr_t spawnattr;
32 if (posix_spawnattr_init(&spawnattr) != 0) {
33 printf("Failed to init posix spawn attribute.");
37 // Set spawn attributes.
38 size_t attr_count = 2;
39 size_t attr_ocount = 0;
40 if (posix_spawnattr_setbinpref_np(&spawnattr, attr_count, pref_cpu_types, &attr_ocount) != 0 ||
41 attr_ocount != attr_count) {
42 printf("Failed to set binary preference on posix spawn attribute.");
43 posix_spawnattr_destroy(&spawnattr);
47 // "posix_spawnp" uses null termination for arguments rather than a count.
48 // Note that we are not duplicating the argument strings themselves.
49 char** argv_copy = (char**)malloc((argc + 1) * sizeof(char*));
51 printf("Failed to allocate memory for arguments.");
52 posix_spawnattr_destroy(&spawnattr);
55 for (int i = 0; i < argc; i++) {
56 argv_copy[i] = argv[i];
58 argv_copy[argc] = NULL;
60 // Pass along our environment.
62 char*** cocoaEnvironment = _NSGetEnviron();
63 if (cocoaEnvironment) {
64 envp = *cocoaEnvironment;
67 int result = posix_spawnp(NULL, argv_copy[0], NULL, &spawnattr, argv_copy, envp);
70 posix_spawnattr_destroy(&spawnattr);
73 printf("Process spawn failed with code %d!", result);
78 LaunchMacPostProcess(const char* aAppBundle)
80 // Launch helper to perform post processing for the update; this is the Mac
81 // analogue of LaunchWinPostProcess (PostUpdateWin).
82 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
84 NSString* iniPath = [NSString stringWithUTF8String:aAppBundle];
86 [iniPath stringByAppendingPathComponent:@"Contents/Resources/updater.ini"];
88 NSFileManager* fileManager = [NSFileManager defaultManager];
89 if (![fileManager fileExistsAtPath:iniPath]) {
90 // the file does not exist; there is nothing to run
96 char values[2][MAX_TEXT_LEN];
97 readResult = ReadStrings([iniPath UTF8String],
98 "ExeRelPath\0ExeArg\0",
107 NSString *exeRelPath = [NSString stringWithUTF8String:values[0]];
108 NSString *exeArg = [NSString stringWithUTF8String:values[1]];
109 if (!exeArg || !exeRelPath) {
114 NSString* exeFullPath = [NSString stringWithUTF8String:aAppBundle];
115 exeFullPath = [exeFullPath stringByAppendingPathComponent:exeRelPath];
117 char optVals[1][MAX_TEXT_LEN];
118 readResult = ReadStrings([iniPath UTF8String],
124 NSTask *task = [[NSTask alloc] init];
125 [task setLaunchPath:exeFullPath];
126 [task setArguments:[NSArray arrayWithObject:exeArg]];
129 NSString *exeAsync = [NSString stringWithUTF8String:optVals[0]];
130 if ([exeAsync isEqualToString:@"false"]) {
131 [task waitUntilExit];
134 // ignore the return value of the task, there's nothing we can do with it