|
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/. */ |
|
6 |
|
7 #include <Cocoa/Cocoa.h> |
|
8 #include <CoreServices/CoreServices.h> |
|
9 #include <crt_externs.h> |
|
10 #include <stdlib.h> |
|
11 #include <stdio.h> |
|
12 #include <spawn.h> |
|
13 #include "readstrings.h" |
|
14 |
|
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] = { |
|
19 #if defined(__i386__) |
|
20 CPU_TYPE_X86, |
|
21 #elif defined(__x86_64__) |
|
22 CPU_TYPE_X86_64, |
|
23 #elif defined(__ppc__) |
|
24 CPU_TYPE_POWERPC, |
|
25 #endif |
|
26 CPU_TYPE_ANY }; |
|
27 |
|
28 void LaunchChild(int argc, char **argv) |
|
29 { |
|
30 // Initialize spawn attributes. |
|
31 posix_spawnattr_t spawnattr; |
|
32 if (posix_spawnattr_init(&spawnattr) != 0) { |
|
33 printf("Failed to init posix spawn attribute."); |
|
34 return; |
|
35 } |
|
36 |
|
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); |
|
44 return; |
|
45 } |
|
46 |
|
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*)); |
|
50 if (!argv_copy) { |
|
51 printf("Failed to allocate memory for arguments."); |
|
52 posix_spawnattr_destroy(&spawnattr); |
|
53 return; |
|
54 } |
|
55 for (int i = 0; i < argc; i++) { |
|
56 argv_copy[i] = argv[i]; |
|
57 } |
|
58 argv_copy[argc] = NULL; |
|
59 |
|
60 // Pass along our environment. |
|
61 char** envp = NULL; |
|
62 char*** cocoaEnvironment = _NSGetEnviron(); |
|
63 if (cocoaEnvironment) { |
|
64 envp = *cocoaEnvironment; |
|
65 } |
|
66 |
|
67 int result = posix_spawnp(NULL, argv_copy[0], NULL, &spawnattr, argv_copy, envp); |
|
68 |
|
69 free(argv_copy); |
|
70 posix_spawnattr_destroy(&spawnattr); |
|
71 |
|
72 if (result != 0) { |
|
73 printf("Process spawn failed with code %d!", result); |
|
74 } |
|
75 } |
|
76 |
|
77 void |
|
78 LaunchMacPostProcess(const char* aAppExe) |
|
79 { |
|
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]; |
|
83 |
|
84 // Find the app bundle containing the executable path given |
|
85 NSString *path = [NSString stringWithUTF8String:aAppExe]; |
|
86 NSBundle *bundle; |
|
87 do { |
|
88 path = [path stringByDeletingLastPathComponent]; |
|
89 bundle = [NSBundle bundleWithPath:path]; |
|
90 } while ((!bundle || ![bundle bundleIdentifier]) && [path length] > 1); |
|
91 if (!bundle) { |
|
92 // No bundle found for the app being launched |
|
93 [pool release]; |
|
94 return; |
|
95 } |
|
96 |
|
97 NSString *iniPath = [bundle pathForResource:@"updater" ofType:@"ini"]; |
|
98 if (!iniPath) { |
|
99 // the file does not exist; there is nothing to run |
|
100 [pool release]; |
|
101 return; |
|
102 } |
|
103 |
|
104 int readResult; |
|
105 char values[2][MAX_TEXT_LEN]; |
|
106 readResult = ReadStrings([iniPath UTF8String], |
|
107 "ExeArg\0ExeRelPath\0", |
|
108 2, |
|
109 values, |
|
110 "PostUpdateMac"); |
|
111 if (readResult) { |
|
112 [pool release]; |
|
113 return; |
|
114 } |
|
115 |
|
116 NSString *exeArg = [NSString stringWithUTF8String:values[0]]; |
|
117 NSString *exeRelPath = [NSString stringWithUTF8String:values[1]]; |
|
118 if (!exeArg || !exeRelPath) { |
|
119 [pool release]; |
|
120 return; |
|
121 } |
|
122 |
|
123 NSString *resourcePath = [bundle resourcePath]; |
|
124 NSString *exeFullPath = [resourcePath stringByAppendingPathComponent:exeRelPath]; |
|
125 |
|
126 NSTask *task = [[NSTask alloc] init]; |
|
127 [task setLaunchPath:exeFullPath]; |
|
128 [task setArguments:[NSArray arrayWithObject:exeArg]]; |
|
129 [task launch]; |
|
130 [task waitUntilExit]; |
|
131 // ignore the return value of the task, there's nothing we can do with it |
|
132 [task release]; |
|
133 |
|
134 [pool release]; |
|
135 } |
|
136 |