]> git.saurik.com Git - apple/libutil.git/blob - reexec_to_match_kernel.c
a617c64378dba392c90d8b2a8340a905953c992a
[apple/libutil.git] / reexec_to_match_kernel.c
1 /*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <sys/cdefs.h>
25
26 #include <spawn.h>
27 #include <errno.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>
33 #include <stdlib.h>
34 #include <stdio.h>
35
36 #include "libutil.h"
37
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);
41
42 #define kReExec "REEXEC_TO_MATCH_KERNEL"
43
44 int reexec_to_match_kernel(void)
45 {
46 cpu_type_t kernarch, progarch;
47 char *alreadyenv;
48
49 alreadyenv = getenv(kReExec);
50 if (alreadyenv) {
51 /* we've done this at least once, assume
52 another try won't help */
53 return 0;
54 }
55
56 kernarch = current_kernel_arch();
57 progarch = current_program_arch();
58
59 if (kernarch == 0) {
60 /* could not determine kernel arch */
61 errno = EINVAL;
62 return -1;
63 }
64
65 if (kernarch == progarch) {
66 /* nothing to do here */
67 return 0;
68 }
69
70 /* Now we need to re-exec */
71 return reexec(kernarch);
72 }
73
74 static cpu_type_t current_program_arch(void)
75 {
76 cpu_type_t current_arch = (_NSGetMachExecuteHeader())->cputype;
77
78 return current_arch;
79 }
80
81 static cpu_type_t current_kernel_arch(void)
82 {
83 struct host_basic_info hi;
84 unsigned int size;
85 kern_return_t kret;
86 cpu_type_t current_arch;
87 int ret, mib[4];
88 size_t len;
89 struct kinfo_proc kp;
90
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) {
94 return 0;
95 }
96
97 current_arch = hi.cpu_type;
98
99 /* Now determine if the kernel is running in 64-bit mode */
100 mib[0] = CTL_KERN;
101 mib[1] = KERN_PROC;
102 mib[2] = KERN_PROC_PID;
103 mib[3] = 0; /* kernproc, pid 0 */
104 len = sizeof(kp);
105 ret = sysctl(mib, sizeof(mib)/sizeof(mib[0]), &kp, &len, NULL, 0);
106 if (ret == -1) {
107 return 0;
108 }
109
110 if (kp.kp_proc.p_flag & P_LP64) {
111 current_arch |= CPU_ARCH_ABI64;
112 }
113
114 return current_arch;
115 }
116
117 static int reexec(cpu_type_t cputype)
118 {
119 posix_spawnattr_t attr;
120 int ret, envcount;
121 size_t copied = 0;
122 char **argv, **oldenvp, **newenvp;
123 char execpath[MAXPATHLEN+1];
124 uint32_t execsize;
125
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
130
131 newenvp = calloc(envcount+2, sizeof(newenvp[0]));
132 for (envcount = 0; oldenvp[envcount]; envcount++) {
133 newenvp[envcount] = oldenvp[envcount];
134 }
135 newenvp[envcount++] = kReExec"=1";
136 newenvp[envcount] = NULL;
137
138 execsize = (uint32_t)sizeof(execpath);
139 ret = _NSGetExecutablePath(execpath, &execsize);
140 if (ret != 0) {
141 return -1;
142 }
143
144 ret = posix_spawnattr_init(&attr);
145 if (ret != 0) {
146 return -1;
147 }
148 ret = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETEXEC);
149 if (ret != 0) {
150 return -1;
151 }
152 ret = posix_spawnattr_setbinpref_np(&attr, 1, &cputype, &copied);
153 if (ret != 0 || copied != 1) {
154 return -1;
155 }
156
157 /*
158 fprintf(stderr, "reexec: %s\n", execpath);
159 for (envcount=0; newenvp[envcount]; envcount++) {
160 fprintf(stderr, "env[%d] = %s\n", envcount, newenvp[envcount]);
161 }
162 for (envcount=0; argv[envcount]; envcount++) {
163 fprintf(stderr, "argv[%d] = %s\n", envcount, argv[envcount]);
164 }
165 */
166 ret = posix_spawn(NULL, execpath, NULL, &attr, argv, newenvp);
167 if (ret != 0) {
168 errno = ret;
169 return -1;
170 }
171
172 /* should not be reached */
173 return 0;
174 }