VirtualBox

source: kBuild/trunk/src/kash/shfork-win.c@ 2629

Last change on this file since 2629 was 2416, checked in by bird, 14 years ago

kash: implemented opendir/readdir/closedir for windows (NT). Fixed windows forking.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.4 KB
Line 
1
2
3/*******************************************************************************
4* Header Files *
5*******************************************************************************/
6#include <string.h>
7#include <locale.h>
8#include <assert.h>
9#include "shinstance.h"
10#include <Windows.h>
11
12/*******************************************************************************
13* Defined Constants And Macros *
14*******************************************************************************/
15/** The stack size. This is also defined in shforkA-win.asm. */
16#define SHFORK_STACK_SIZE (1*1024*1024)
17
18
19/*******************************************************************************
20* Global Variables *
21*******************************************************************************/
22static void *g_stack_base = 0;
23static void *g_stack_limit = 0;
24
25
26/*******************************************************************************
27* Internal Functions *
28*******************************************************************************/
29static void *shfork_string_to_ptr(const char *str, const char *argv0, const char *what);
30
31/* in shforkA-win.asm: */
32extern pid_t shfork_do_it(shinstance *psh);
33extern void shfork_resume(void *cur, void *base, void *limit);
34
35/* called by shforkA-win.asm: */
36void *shfork_maybe_forked(int argc, char **argv, char **envp);
37extern int shfork_body(shinstance *psh, void *stack_ptr);
38extern void init_syntax(void);
39
40
41/**
42 * Called by shforkA-win.asm to check whether we're a forked child
43 * process or not.
44 *
45 * In the former case we will resume execution at the fork resume
46 * point. In the latter we'll allocate a new stack of the forkable
47 * heap and return it to the caller so real_main() in main.c can be
48 * invoked on it.
49 *
50 * @returns Stack or not at all.
51 * @param argc Argument count.
52 * @param argv Argument vector.
53 * @param envp Environment vector.
54 */
55void *shfork_maybe_forked(int argc, char **argv, char **envp)
56{
57 void *stack_ptr;
58
59 /*
60 * Are we actually forking?
61 */
62 if ( argc != 8
63 || strcmp(argv[1], "--!forked!--")
64 || strcmp(argv[2], "--stack-address")
65 || strcmp(argv[4], "--stack-base")
66 || strcmp(argv[6], "--stack-limit"))
67 {
68 char *stack;
69 shheap_init(NULL);
70 g_stack_limit = stack = (char *)sh_malloc(NULL, SHFORK_STACK_SIZE);
71 g_stack_base = stack += SHFORK_STACK_SIZE;
72 return stack;
73 }
74
75 /*
76 * Do any init that needs to be done before resuming the
77 * fork() call.
78 */
79 setlocale(LC_ALL, "");
80
81 /*
82 * Convert the stack addresses.
83 */
84 stack_ptr = shfork_string_to_ptr(argv[3], argv[0], "--stack-address");
85 g_stack_base = shfork_string_to_ptr(argv[5], argv[0], "--stack-base");
86 g_stack_limit = shfork_string_to_ptr(argv[7], argv[0], "--stack-limit");
87 assert((uintptr_t)stack_ptr < (uintptr_t)g_stack_base);
88 assert((uintptr_t)stack_ptr > (uintptr_t)g_stack_limit);
89
90 /*
91 * Switch stack and jump to the fork resume point.
92 */
93 shfork_resume(stack_ptr, g_stack_base, g_stack_limit);
94 /* (won't get here) */
95 return NULL;
96}
97
98/***
99 * Converts a string into a pointer.
100 *
101 * @returns Pointer.
102 * @param argv0 The program name in case of error.
103 * @param str The string to convert.
104 */
105static void *shfork_string_to_ptr(const char *str, const char *argv0, const char *what)
106{
107 const char *start = str;
108 intptr_t ptr = 0;
109 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
110 str += 2;
111 while (*str)
112 {
113 unsigned digit;
114 switch (*str)
115 {
116 case '0': digit = 0; break;
117 case '1': digit = 1; break;
118 case '2': digit = 2; break;
119 case '3': digit = 3; break;
120 case '4': digit = 4; break;
121 case '5': digit = 5; break;
122 case '6': digit = 6; break;
123 case '7': digit = 7; break;
124 case '8': digit = 8; break;
125 case '9': digit = 9; break;
126 case 'a': case 'A': digit = 0xa; break;
127 case 'b': case 'B': digit = 0xb; break;
128 case 'c': case 'C': digit = 0xc; break;
129 case 'd': case 'D': digit = 0xd; break;
130 case 'e': case 'E': digit = 0xe; break;
131 case 'f': case 'F': digit = 0xf; break;
132 default:
133 fprintf(stderr, "%s: fatal error: Invalid %s '%s'\n", argv0, what, start);
134 exit(2);
135 }
136 ptr <<= 4;
137 ptr |= digit;
138 str++;
139 }
140 return (void *)ptr;
141}
142
143/**
144 * Do the fork.
145 * @returns same as fork().
146 * @param psh The shell that's forking.
147 */
148int shfork_do(shinstance *psh)
149{
150 /* save globals */
151 void *pheap_head = shheap_get_head();
152 pid_t pid = shfork_do_it(psh);
153 if (pid == 0)
154 {
155 /* reinit stuff, only the heap is copied! */
156 shthread_set_shell(psh);
157 shheap_init(pheap_head);
158 setlocale(LC_ALL, "");
159 init_syntax();
160 }
161 return pid;
162}
163
164/**
165 * Create the child process making sure it inherits all our handles,
166 * copy of the forkable heap and kick it off.
167 *
168 * Called by shfork_do_it() in shforkA-win.asm.
169 *
170 * @returns child pid on success, -1 and errno on failure.
171 * @param psh The shell that's forking.
172 * @param stack_ptr The stack address at which the guest is suppost to resume.
173 */
174int shfork_body(shinstance *psh, void *stack_ptr)
175{
176 PROCESS_INFORMATION ProcInfo;
177 STARTUPINFO StrtInfo;
178 intptr_t hndls[3];
179 char szExeName[1024];
180 char szCmdLine[1024+256];
181 DWORD cch;
182 int rc = 0;
183
184 assert((uintptr_t)stack_ptr < (uintptr_t)g_stack_base);
185 assert((uintptr_t)stack_ptr > (uintptr_t)g_stack_limit);
186
187 /*
188 * Mark all handles inheritable and get the three standard handles.
189 */
190 shfile_fork_win(&psh->fdtab, 1 /* set */, &hndls[0]);
191
192 /*
193 * Create the process.
194 */
195 cch = GetModuleFileName(GetModuleHandle(NULL), szExeName, sizeof(szExeName));
196 if (cch > 0)
197 {
198#if 0 /* quoting the program name doesn't seems to be working :/ */
199 szCmdLine[0] = '"';
200 memcpy(&szCmdLine[1], szExeName, cch);
201 szCmdLine[++cch] = '"';
202#else
203 memcpy(&szCmdLine[0], szExeName, cch);
204#endif
205 cch += sprintf(&szCmdLine[cch], " --!forked!-- --stack-address %p --stack-base %p --stack-limit %p",
206 stack_ptr, g_stack_base, g_stack_limit);
207 szCmdLine[cch+1] = '\0';
208 TRACE2((NULL, "shfork_body: szCmdLine=%s\n", szCmdLine));
209
210 memset(&StrtInfo, '\0', sizeof(StrtInfo)); /* just in case. */
211 StrtInfo.cb = sizeof(StrtInfo);
212 StrtInfo.lpReserved = NULL;
213 StrtInfo.lpDesktop = NULL;
214 StrtInfo.lpTitle = NULL;
215 StrtInfo.dwX = 0;
216 StrtInfo.dwY = 0;
217 StrtInfo.dwXSize = 0;
218 StrtInfo.dwYSize = 0;
219 StrtInfo.dwXCountChars = 0;
220 StrtInfo.dwYCountChars = 0;
221 StrtInfo.dwFillAttribute = 0;
222 StrtInfo.dwFlags = STARTF_USESTDHANDLES;;
223 StrtInfo.wShowWindow = 0;
224 StrtInfo.cbReserved2 = 0;
225 StrtInfo.lpReserved2 = NULL;
226 StrtInfo.hStdInput = (HANDLE)hndls[0];
227 StrtInfo.hStdOutput = (HANDLE)hndls[1];
228 StrtInfo.hStdError = (HANDLE)hndls[2];
229 if (CreateProcess(szExeName,
230 szCmdLine,
231 NULL, /* pProcessAttributes */
232 NULL, /* pThreadAttributes */
233 TRUE, /* bInheritHandles */
234 CREATE_SUSPENDED,
235 NULL, /* pEnvironment */
236 NULL, /* pCurrentDirectory */
237 &StrtInfo,
238 &ProcInfo))
239 {
240 /*
241 * Copy the memory to the child.
242 */
243 rc = shheap_fork_copy_to_child(ProcInfo.hProcess);
244 if (!rc)
245 {
246 if (ResumeThread(ProcInfo.hThread) != (DWORD)-1)
247 {
248 rc = sh_add_child(psh, ProcInfo.dwProcessId, ProcInfo.hProcess);
249 if (!rc)
250 rc = (int)ProcInfo.dwProcessId;
251 }
252 else
253 {
254 DWORD dwErr = GetLastError();
255 fprintf(stderr, "shfork: ResumeThread() -> %d\n", dwErr);
256 errno = EINVAL;
257 rc = -1;
258 }
259 }
260 if (rc == -1)
261 {
262 TerminateProcess(ProcInfo.hProcess, 127);
263 /* needed?: ResumeThread(ProcInfo.hThread); */
264 CloseHandle(ProcInfo.hProcess);
265 }
266 CloseHandle(ProcInfo.hThread);
267 }
268 else
269 {
270 DWORD dwErr = GetLastError();
271 fprintf(stderr, "shfork: CreateProcess(%s) -> %d\n", szExeName, dwErr);
272 errno = EINVAL;
273 rc = -1;
274 }
275 }
276 else
277 {
278 DWORD dwErr = GetLastError();
279 fprintf(stderr, "shfork: GetModuleFileName() -> %d\n", dwErr);
280 errno = EINVAL;
281 rc = -1;
282 }
283
284 /*
285 * Restore the handle inherit property.
286 */
287 shfile_fork_win(&psh->fdtab, 0 /* restore */, NULL);
288
289 return rc;
290}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette