LibreOffice Module onlineupdate (master) 1
launchchild_osx.mm
Go to the documentation of this file.
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.
18static 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
28void 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
77void
78LaunchMacPostProcess(const char* aAppBundle)
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 NSString* iniPath = [NSString stringWithUTF8String:aAppBundle];
85 iniPath =
86 [iniPath stringByAppendingPathComponent:@"Contents/Resources/updater.ini"];
87
88 NSFileManager* fileManager = [NSFileManager defaultManager];
89 if (![fileManager fileExistsAtPath:iniPath]) {
90 // the file does not exist; there is nothing to run
91 [pool release];
92 return;
93 }
94
95 int readResult;
96 char values[2][MAX_TEXT_LEN];
97 readResult = ReadStrings([iniPath UTF8String],
98 "ExeRelPath\0ExeArg\0",
99 2,
100 values,
101 "PostUpdateMac");
102 if (readResult) {
103 [pool release];
104 return;
105 }
106
107 NSString *exeRelPath = [NSString stringWithUTF8String:values[0]];
108 NSString *exeArg = [NSString stringWithUTF8String:values[1]];
109 if (!exeArg || !exeRelPath) {
110 [pool release];
111 return;
112 }
113
114 NSString* exeFullPath = [NSString stringWithUTF8String:aAppBundle];
115 exeFullPath = [exeFullPath stringByAppendingPathComponent:exeRelPath];
116
117 char optVals[1][MAX_TEXT_LEN];
118 readResult = ReadStrings([iniPath UTF8String],
119 "ExeAsync\0",
120 1,
121 optVals,
122 "PostUpdateMac");
123
124 NSTask *task = [[NSTask alloc] init];
125 [task setLaunchPath:exeFullPath];
126 [task setArguments:[NSArray arrayWithObject:exeArg]];
127 [task launch];
128 if (!readResult) {
129 NSString *exeAsync = [NSString stringWithUTF8String:optVals[0]];
130 if ([exeAsync isEqualToString:@"false"]) {
131 [task waitUntilExit];
132 }
133 }
134 // ignore the return value of the task, there's nothing we can do with it
135 [task release];
136
137 [pool release];
138}
FILE * init(int, char **)
static cpu_type_t pref_cpu_types[2]
void LaunchChild(int argc, char **argv)
void LaunchMacPostProcess(const char *aAppBundle)
return NULL
int i
std::vector< char * > values
int ReadStrings(const NS_tchar *path, const char *keyList, unsigned int numStrings, char results[][MAX_TEXT_LEN], const char *section)
A very basic parser for updater.ini taken mostly from nsINIParser.cpp that can be used by standalone ...
#define MAX_TEXT_LEN
Definition: readstrings.h:10
Any result