]>
git.saurik.com Git - apple/libutil.git/blob - reexec_to_match_kernel.c
2 * Copyright (c) 2008-2010 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
, const char *guardenv
);
42 #define kReExecToMatchKernel "REEXEC_TO_MATCH_KERNEL"
43 #define kReExecToMatchLP64 "REEXEC_TO_MATCH_LP64NESS"
45 int reexec_to_match_kernel(void)
47 cpu_type_t kernarch
, progarch
;
50 alreadyenv
= getenv(kReExecToMatchKernel
);
52 /* we've done this at least once, assume
53 another try won't help */
57 kernarch
= current_kernel_arch();
58 progarch
= current_program_arch();
61 /* could not determine kernel arch */
66 if (kernarch
== progarch
) {
67 /* nothing to do here */
71 /* Now we need to re-exec */
72 return reexec(kernarch
, kReExecToMatchKernel
);
75 int reexec_to_match_lp64ness(bool isLP64
)
77 cpu_type_t kernarch
, progarch
, targetarch
;
80 alreadyenv
= getenv(kReExecToMatchLP64
);
82 /* we've done this at least once, assume
83 another try won't help */
87 kernarch
= current_kernel_arch();
88 progarch
= current_program_arch();
91 /* could not determine kernel arch */
97 targetarch
= kernarch
| CPU_ARCH_ABI64
;
99 targetarch
= kernarch
& ~CPU_ARCH_ABI64
;
102 if (targetarch
== progarch
) {
103 /* nothing to do here */
107 /* Now we need to re-exec */
108 return reexec(targetarch
, kReExecToMatchLP64
);
111 static cpu_type_t
current_program_arch(void)
113 cpu_type_t current_arch
= (_NSGetMachExecuteHeader())->cputype
;
118 static cpu_type_t
current_kernel_arch(void)
120 struct host_basic_info hi
;
123 cpu_type_t current_arch
;
126 struct kinfo_proc kp
;
128 size
= sizeof(hi
)/sizeof(int);
129 kret
= host_info(mach_host_self(), HOST_BASIC_INFO
, (host_info_t
)&hi
, &size
);
130 if (kret
!= KERN_SUCCESS
) {
134 current_arch
= hi
.cpu_type
;
136 /* Now determine if the kernel is running in 64-bit mode */
139 mib
[2] = KERN_PROC_PID
;
140 mib
[3] = 0; /* kernproc, pid 0 */
142 ret
= sysctl(mib
, sizeof(mib
)/sizeof(mib
[0]), &kp
, &len
, NULL
, 0);
147 if (kp
.kp_proc
.p_flag
& P_LP64
) {
148 current_arch
|= CPU_ARCH_ABI64
;
154 static int reexec(cpu_type_t cputype
, const char *guardenv
)
156 posix_spawnattr_t attr
;
159 char **argv
, **oldenvp
, **newenvp
;
160 char execpath
[MAXPATHLEN
+1];
164 argv
= *_NSGetArgv();
165 oldenvp
= *_NSGetEnviron();
166 for (envcount
= 0; oldenvp
[envcount
]; envcount
++);
167 // if there are 4 elements and a NULL, envcount will be 4
169 newenvp
= calloc(envcount
+2, sizeof(newenvp
[0]));
170 for (envcount
= 0; oldenvp
[envcount
]; envcount
++) {
171 newenvp
[envcount
] = oldenvp
[envcount
];
174 snprintf(guardstr
, sizeof(guardstr
), "%s=1", guardenv
);
175 newenvp
[envcount
++] = guardstr
;
176 newenvp
[envcount
] = NULL
;
178 execsize
= (uint32_t)sizeof(execpath
);
179 ret
= _NSGetExecutablePath(execpath
, &execsize
);
184 ret
= posix_spawnattr_init(&attr
);
188 ret
= posix_spawnattr_setflags(&attr
, POSIX_SPAWN_SETEXEC
);
192 ret
= posix_spawnattr_setbinpref_np(&attr
, 1, &cputype
, &copied
);
193 if (ret
!= 0 || copied
!= 1) {
198 fprintf(stderr
, "reexec: %s (arch=%d)\n", execpath
, cputype
);
199 for (envcount
=0; newenvp
[envcount
]; envcount
++) {
200 fprintf(stderr
, "env[%d] = %s\n", envcount
, newenvp
[envcount
]);
202 for (envcount
=0; argv
[envcount
]; envcount
++) {
203 fprintf(stderr
, "argv[%d] = %s\n", envcount
, argv
[envcount
]);
207 ret
= posix_spawn(NULL
, execpath
, NULL
, &attr
, argv
, newenvp
);
213 /* should not be reached */