]>
git.saurik.com Git - apple/libutil.git/blob - reexec_to_match_kernel.c
a617c64378dba392c90d8b2a8340a905953c992a
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/cdefs.h>
28 #include <crt_externs.h>
29 #include <mach/mach.h>
30 #include <mach-o/loader.h>
31 #include <mach-o/dyld.h>
32 #include <sys/sysctl.h>
38 static cpu_type_t
current_program_arch(void);
39 static cpu_type_t
current_kernel_arch(void);
40 static int reexec(cpu_type_t cputype
);
42 #define kReExec "REEXEC_TO_MATCH_KERNEL"
44 int reexec_to_match_kernel(void)
46 cpu_type_t kernarch
, progarch
;
49 alreadyenv
= getenv(kReExec
);
51 /* we've done this at least once, assume
52 another try won't help */
56 kernarch
= current_kernel_arch();
57 progarch
= current_program_arch();
60 /* could not determine kernel arch */
65 if (kernarch
== progarch
) {
66 /* nothing to do here */
70 /* Now we need to re-exec */
71 return reexec(kernarch
);
74 static cpu_type_t
current_program_arch(void)
76 cpu_type_t current_arch
= (_NSGetMachExecuteHeader())->cputype
;
81 static cpu_type_t
current_kernel_arch(void)
83 struct host_basic_info hi
;
86 cpu_type_t current_arch
;
91 size
= sizeof(hi
)/sizeof(int);
92 kret
= host_info(mach_host_self(), HOST_BASIC_INFO
, (host_info_t
)&hi
, &size
);
93 if (kret
!= KERN_SUCCESS
) {
97 current_arch
= hi
.cpu_type
;
99 /* Now determine if the kernel is running in 64-bit mode */
102 mib
[2] = KERN_PROC_PID
;
103 mib
[3] = 0; /* kernproc, pid 0 */
105 ret
= sysctl(mib
, sizeof(mib
)/sizeof(mib
[0]), &kp
, &len
, NULL
, 0);
110 if (kp
.kp_proc
.p_flag
& P_LP64
) {
111 current_arch
|= CPU_ARCH_ABI64
;
117 static int reexec(cpu_type_t cputype
)
119 posix_spawnattr_t attr
;
122 char **argv
, **oldenvp
, **newenvp
;
123 char execpath
[MAXPATHLEN
+1];
126 argv
= *_NSGetArgv();
127 oldenvp
= *_NSGetEnviron();
128 for (envcount
= 0; oldenvp
[envcount
]; envcount
++);
129 // if there are 4 elements and a NULL, envcount will be 4
131 newenvp
= calloc(envcount
+2, sizeof(newenvp
[0]));
132 for (envcount
= 0; oldenvp
[envcount
]; envcount
++) {
133 newenvp
[envcount
] = oldenvp
[envcount
];
135 newenvp
[envcount
++] = kReExec
"=1";
136 newenvp
[envcount
] = NULL
;
138 execsize
= (uint32_t)sizeof(execpath
);
139 ret
= _NSGetExecutablePath(execpath
, &execsize
);
144 ret
= posix_spawnattr_init(&attr
);
148 ret
= posix_spawnattr_setflags(&attr
, POSIX_SPAWN_SETEXEC
);
152 ret
= posix_spawnattr_setbinpref_np(&attr
, 1, &cputype
, &copied
);
153 if (ret
!= 0 || copied
!= 1) {
158 fprintf(stderr, "reexec: %s\n", execpath);
159 for (envcount=0; newenvp[envcount]; envcount++) {
160 fprintf(stderr, "env[%d] = %s\n", envcount, newenvp[envcount]);
162 for (envcount=0; argv[envcount]; envcount++) {
163 fprintf(stderr, "argv[%d] = %s\n", envcount, argv[envcount]);
166 ret
= posix_spawn(NULL
, execpath
, NULL
, &attr
, argv
, newenvp
);
172 /* should not be reached */