Back to index

lightning-sunbird  0.9+nobinonly
MacLaunchHelper.m
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Ben Goodger <ben@mozilla.org>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "MacLaunchHelper.h"
00040 
00041 #include <Cocoa/Cocoa.h>
00042 #include <mach-o/dyld.h>
00043 #include <sys/utsname.h>
00044 
00045 #ifdef __ppc__
00046 #include <sys/types.h>
00047 #include <sys/sysctl.h>
00048 #include <mach/machine.h>
00049 #endif /* __ppc__ */
00050 
00051 @interface TaskMonitor : NSObject
00052 -(void)prebindFinished:(NSNotification *)aNotification;
00053 @end
00054 
00055 @implementation TaskMonitor
00056 -(void)prebindFinished:(NSNotification *)aNotification
00057 {
00058   /* Delete the task and the TaskMonitor */
00059   [[aNotification object] release];
00060   [self release];
00061 }
00062 @end
00063 
00064 void
00065 UpdatePrebinding()
00066 {
00067 #ifdef _BUILD_STATIC_BIN
00068   struct utsname u;
00069   uname(&u);
00070 
00071   // We run the redo-prebinding script in these cases:
00072   // 10.1.x (5.x): No auto-update of prebinding exists
00073   // On 10.3.x, prebinding fails to update automatically but this script
00074   // doesn't work either.  It doesn't matter though, because in 10.3.4 and
00075   // higher, the loader is improved so that prebinding is unnecessary.
00076   if (u.release[0] != '5')
00077     return;
00078 
00079   if (!_dyld_launched_prebound()) {
00080     NSTask *task;
00081     NSArray *args;
00082     TaskMonitor *monitor;
00083     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
00084 
00085     NSLog(@"Not prebound, launching update script");
00086     task = [[NSTask alloc] init];
00087     args = [NSArray arrayWithObject: @"redo-prebinding.sh"];
00088 
00089     [task setCurrentDirectoryPath:[[[NSBundle mainBundle] executablePath] stringByDeletingLastPathComponent]];
00090     [task setLaunchPath:@"/bin/sh"];
00091     [task setArguments:args];
00092 
00093     monitor = [[TaskMonitor alloc] init];
00094 
00095     [[NSNotificationCenter defaultCenter] addObserver:monitor
00096      selector:@selector(prebindFinished:)
00097      name:NSTaskDidTerminateNotification
00098      object:nil];
00099 
00100     [task launch];
00101     [pool release];
00102   }
00103 #endif
00104 }
00105 
00106 void LaunchChildMac(int aArgc, char** aArgv)
00107 {
00108   int i;
00109   NSTask* child = [[NSTask alloc] init];
00110   NSMutableArray* args = [[NSMutableArray alloc] init];
00111   NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
00112 
00113 #ifdef __ppc__
00114   // It's possible that the app is a universal binary running under Rosetta
00115   // translation because the user forced it to.  Relaunching via NSTask would
00116   // launch the app natively, which the user apparently doesn't want.
00117   // In that case, try to preserve translation.
00118 
00119   // If the sysctl doesn't exist, it's because Rosetta doesn't exist,
00120   // so don't try to force translation.  In case of other errors, just assume
00121   // that the app is native.
00122 
00123   int isNative = 0;
00124   size_t sz = sizeof(isNative);
00125 
00126   if (sysctlbyname("sysctl.proc_native", &isNative, &sz, NULL, 0) == 0 &&
00127       !isNative) {
00128     // Running translated on ppc.
00129 
00130     cpu_type_t preferredCPU = CPU_TYPE_POWERPC;
00131     sysctlbyname("sysctl.proc_exec_affinity", NULL, NULL,
00132                  &preferredCPU, sizeof(preferredCPU));
00133 
00134     // Nothing can be done to handle failure, relaunch anyway.
00135   }
00136 #endif /* __ppc__ */
00137 
00138   for (i = 1; i < aArgc; ++i) 
00139     [args addObject: [NSString stringWithCString: aArgv[i]]];
00140   
00141   [child setCurrentDirectoryPath:[[[NSBundle mainBundle] executablePath] stringByDeletingLastPathComponent]];
00142   [child setLaunchPath:[[NSBundle mainBundle] executablePath]];
00143   [child setArguments:args];
00144   [child launch];
00145   [pool release];
00146 }
00147