Changeset 2293 in kBuild for trunk/src/kash/shfork-win.c
- Timestamp:
- Feb 28, 2009 7:25:12 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/shfork-win.c
r2292 r2293 7 7 #include <locale.h> 8 8 #include "shinstance.h" 9 #include <Windows.h> 10 11 /******************************************************************************* 12 * Defined Constants And Macros * 13 *******************************************************************************/ 14 /** The stack size. This is also defined in shforkA-win.asm. */ 15 #define SHFORK_STACK_SIZE (1*1024*1024) 9 16 10 17 … … 26 33 /* called by shforkA-win.asm: */ 27 34 void *shfork_maybe_forked(int argc, char **argv, char **envp); 28 extern int shfork_body( uintptr_tstack_ptr);35 extern int shfork_body(shinstance *psh, void *stack_ptr); 29 36 30 37 … … 56 63 || strcmp(argv[6], "--stack-limit")) 57 64 { 65 char *stack; 58 66 shheap_init(); 59 return (char *)sh_malloc(NULL, 1*1024*1024) + 1*1024*1024; 67 stack = (char *)sh_malloc(NULL, SHFORK_STACK_SIZE) + SHFORK_STACK_SIZE; 68 g_stack_base = stack + SHFORK_STACK_SIZE; 69 g_stack_limit = stack; 70 return stack; 60 71 } 61 72 … … 64 75 * fork() call. 65 76 */ 66 77 setlocale(LC_ALL, ""); 67 78 68 79 /* … … 121 132 ptr <<= 4; 122 133 ptr |= digit; 134 str++; 123 135 } 124 136 return (void *)ptr; … … 132 144 * 133 145 * @returns child pid on success, -1 and errno on failure. 146 * @param psh The shell that's forking. 134 147 * @param stack_ptr The stack address at which the guest is suppost to resume. 135 148 */ 136 int shfork_body( uintptr_tstack_ptr)149 int shfork_body(shinstance *psh, void *stack_ptr) 137 150 { 138 errno = ENOSYS; 139 return -1; 151 PROCESS_INFORMATION ProcInfo; 152 STARTUPINFO StrtInfo; 153 intptr_t hndls[3]; 154 char szExeName[1024]; 155 char szCmdLine[1024+256]; 156 DWORD cch; 157 int rc = 0; 158 159 /* 160 * Mark all handles inheritable and get the three standard handles. 161 */ 162 shfile_fork_win(&psh->fdtab, 1 /* set */, &hndls[0]); 163 164 /* 165 * Create the process. 166 */ 167 cch = GetModuleFileName(GetModuleHandle(NULL), szExeName, sizeof(szExeName)); 168 if (cch > 0) 169 { 170 #if 0 /* quoting the program name doesn't seems to be working :/ */ 171 szCmdLine[0] = '"'; 172 memcpy(&szCmdLine[1], szExeName, cch); 173 szCmdLine[++cch] = '"'; 174 #else 175 memcpy(&szCmdLine[0], szExeName, cch); 176 #endif 177 cch += sprintf(&szCmdLine[cch], " --!forked!-- --stack-address %p --stack-base %p --stack-limit %p", 178 stack_ptr, g_stack_base, g_stack_limit); 179 szCmdLine[cch+1] = '\0'; 180 181 memset(&StrtInfo, '\0', sizeof(StrtInfo)); /* just in case. */ 182 StrtInfo.cb = sizeof(StrtInfo); 183 StrtInfo.lpReserved = NULL; 184 StrtInfo.lpDesktop = NULL; 185 StrtInfo.lpTitle = NULL; 186 StrtInfo.dwX = 0; 187 StrtInfo.dwY = 0; 188 StrtInfo.dwXSize = 0; 189 StrtInfo.dwYSize = 0; 190 StrtInfo.dwXCountChars = 0; 191 StrtInfo.dwYCountChars = 0; 192 StrtInfo.dwFillAttribute = 0; 193 StrtInfo.dwFlags = 0; 194 StrtInfo.wShowWindow = 0; 195 StrtInfo.cbReserved2 = 0; 196 StrtInfo.lpReserved2 = NULL; 197 StrtInfo.hStdInput = (HANDLE)hndls[0]; 198 StrtInfo.hStdOutput = (HANDLE)hndls[1]; 199 StrtInfo.hStdError = (HANDLE)hndls[2]; 200 if (CreateProcess(szExeName, 201 szCmdLine, 202 NULL, /* pProcessAttributes */ 203 NULL, /* pThreadAttributes */ 204 TRUE, /* bInheritHandles */ 205 CREATE_SUSPENDED, 206 NULL, /* pEnvironment */ 207 NULL, /* pCurrentDirectory */ 208 &StrtInfo, 209 &ProcInfo)) 210 { 211 /* 212 * Copy the memory to the child. 213 */ 214 rc = shheap_fork_copy_to_child(ProcInfo.hProcess); 215 if (!rc) 216 { 217 if (ResumeThread(ProcInfo.hThread) != (DWORD)-1) 218 { 219 rc = sh_add_child(psh, ProcInfo.dwProcessId, ProcInfo.hProcess); 220 if (!rc) 221 rc = (int)ProcInfo.dwProcessId; 222 } 223 else 224 { 225 DWORD dwErr = GetLastError(); 226 fprintf(stderr, "shfork: ResumeThread() -> %d\n", dwErr); 227 errno = EINVAL; 228 rc = -1; 229 } 230 } 231 if (rc == -1) 232 { 233 TerminateProcess(ProcInfo.hProcess, 127); 234 /* needed?: ResumeThread(ProcInfo.hThread); */ 235 CloseHandle(ProcInfo.hProcess); 236 } 237 CloseHandle(ProcInfo.hThread); 238 } 239 else 240 { 241 DWORD dwErr = GetLastError(); 242 fprintf(stderr, "shfork: CreateProcess(%s) -> %d\n", szExeName, dwErr); 243 errno = EINVAL; 244 rc = -1; 245 } 246 } 247 else 248 { 249 DWORD dwErr = GetLastError(); 250 fprintf(stderr, "shfork: GetModuleFileName() -> %d\n", dwErr); 251 errno = EINVAL; 252 rc = -1; 253 } 254 255 /* 256 * Restore the handle inherit property. 257 */ 258 shfile_fork_win(&psh->fdtab, 0 /* restore */, NULL); 259 260 return rc; 140 261 }
Note:
See TracChangeset
for help on using the changeset viewer.