[2292] | 1 |
|
---|
| 2 |
|
---|
| 3 | /*******************************************************************************
|
---|
| 4 | * Header Files *
|
---|
| 5 | *******************************************************************************/
|
---|
| 6 | #include <string.h>
|
---|
| 7 | #include <locale.h>
|
---|
| 8 | #include "shinstance.h"
|
---|
[2293] | 9 | #include <Windows.h>
|
---|
[2292] | 10 |
|
---|
[2293] | 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)
|
---|
[2292] | 16 |
|
---|
[2293] | 17 |
|
---|
[2292] | 18 | /*******************************************************************************
|
---|
| 19 | * Global Variables *
|
---|
| 20 | *******************************************************************************/
|
---|
| 21 | static void *g_stack_base = 0;
|
---|
| 22 | static void *g_stack_limit = 0;
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 | /*******************************************************************************
|
---|
| 26 | * Internal Functions *
|
---|
| 27 | *******************************************************************************/
|
---|
| 28 | static void *shfork_string_to_ptr(const char *str, const char *argv0, const char *what);
|
---|
| 29 |
|
---|
| 30 | /* in shforkA-win.asm: */
|
---|
[2416] | 31 | extern pid_t shfork_do_it(shinstance *psh);
|
---|
[2292] | 32 | extern void shfork_resume(void *cur, void *base, void *limit);
|
---|
| 33 |
|
---|
| 34 | /* called by shforkA-win.asm: */
|
---|
| 35 | void *shfork_maybe_forked(int argc, char **argv, char **envp);
|
---|
[2293] | 36 | extern int shfork_body(shinstance *psh, void *stack_ptr);
|
---|
[2416] | 37 | extern void init_syntax(void);
|
---|
[2292] | 38 |
|
---|
| 39 |
|
---|
[2416] | 40 | /**
|
---|
[2292] | 41 | * Called by shforkA-win.asm to check whether we're a forked child
|
---|
| 42 | * process or not.
|
---|
| 43 | *
|
---|
| 44 | * In the former case we will resume execution at the fork resume
|
---|
| 45 | * point. In the latter we'll allocate a new stack of the forkable
|
---|
| 46 | * heap and return it to the caller so real_main() in main.c can be
|
---|
| 47 | * invoked on it.
|
---|
| 48 | *
|
---|
| 49 | * @returns Stack or not at all.
|
---|
| 50 | * @param argc Argument count.
|
---|
| 51 | * @param argv Argument vector.
|
---|
| 52 | * @param envp Environment vector.
|
---|
| 53 | */
|
---|
| 54 | void *shfork_maybe_forked(int argc, char **argv, char **envp)
|
---|
| 55 | {
|
---|
| 56 | void *stack_ptr;
|
---|
| 57 |
|
---|
| 58 | /*
|
---|
| 59 | * Are we actually forking?
|
---|
| 60 | */
|
---|
| 61 | if ( argc != 8
|
---|
| 62 | || strcmp(argv[1], "--!forked!--")
|
---|
| 63 | || strcmp(argv[2], "--stack-address")
|
---|
| 64 | || strcmp(argv[4], "--stack-base")
|
---|
| 65 | || strcmp(argv[6], "--stack-limit"))
|
---|
| 66 | {
|
---|
[2293] | 67 | char *stack;
|
---|
[2416] | 68 | shheap_init(NULL);
|
---|
| 69 | g_stack_limit = stack = (char *)sh_malloc(NULL, SHFORK_STACK_SIZE);
|
---|
| 70 | g_stack_base = stack += SHFORK_STACK_SIZE;
|
---|
[2293] | 71 | return stack;
|
---|
[2292] | 72 | }
|
---|
| 73 |
|
---|
| 74 | /*
|
---|
| 75 | * Do any init that needs to be done before resuming the
|
---|
| 76 | * fork() call.
|
---|
| 77 | */
|
---|
[2293] | 78 | setlocale(LC_ALL, "");
|
---|
[2292] | 79 |
|
---|
| 80 | /*
|
---|
| 81 | * Convert the stack addresses.
|
---|
| 82 | */
|
---|
| 83 | stack_ptr = shfork_string_to_ptr(argv[3], argv[0], "--stack-address");
|
---|
| 84 | g_stack_base = shfork_string_to_ptr(argv[5], argv[0], "--stack-base");
|
---|
| 85 | g_stack_limit = shfork_string_to_ptr(argv[7], argv[0], "--stack-limit");
|
---|
[3477] | 86 | kHlpAssert((uintptr_t)stack_ptr < (uintptr_t)g_stack_base);
|
---|
| 87 | kHlpAssert((uintptr_t)stack_ptr > (uintptr_t)g_stack_limit);
|
---|
[2292] | 88 |
|
---|
| 89 | /*
|
---|
| 90 | * Switch stack and jump to the fork resume point.
|
---|
| 91 | */
|
---|
| 92 | shfork_resume(stack_ptr, g_stack_base, g_stack_limit);
|
---|
| 93 | /* (won't get here) */
|
---|
| 94 | return NULL;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /***
|
---|
| 98 | * Converts a string into a pointer.
|
---|
| 99 | *
|
---|
| 100 | * @returns Pointer.
|
---|
| 101 | * @param argv0 The program name in case of error.
|
---|
| 102 | * @param str The string to convert.
|
---|
| 103 | */
|
---|
| 104 | static void *shfork_string_to_ptr(const char *str, const char *argv0, const char *what)
|
---|
| 105 | {
|
---|
| 106 | const char *start = str;
|
---|
| 107 | intptr_t ptr = 0;
|
---|
| 108 | if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
|
---|
| 109 | str += 2;
|
---|
| 110 | while (*str)
|
---|
| 111 | {
|
---|
| 112 | unsigned digit;
|
---|
| 113 | switch (*str)
|
---|
| 114 | {
|
---|
| 115 | case '0': digit = 0; break;
|
---|
| 116 | case '1': digit = 1; break;
|
---|
| 117 | case '2': digit = 2; break;
|
---|
| 118 | case '3': digit = 3; break;
|
---|
| 119 | case '4': digit = 4; break;
|
---|
| 120 | case '5': digit = 5; break;
|
---|
| 121 | case '6': digit = 6; break;
|
---|
| 122 | case '7': digit = 7; break;
|
---|
| 123 | case '8': digit = 8; break;
|
---|
| 124 | case '9': digit = 9; break;
|
---|
| 125 | case 'a': case 'A': digit = 0xa; break;
|
---|
| 126 | case 'b': case 'B': digit = 0xb; break;
|
---|
| 127 | case 'c': case 'C': digit = 0xc; break;
|
---|
| 128 | case 'd': case 'D': digit = 0xd; break;
|
---|
| 129 | case 'e': case 'E': digit = 0xe; break;
|
---|
| 130 | case 'f': case 'F': digit = 0xf; break;
|
---|
| 131 | default:
|
---|
| 132 | fprintf(stderr, "%s: fatal error: Invalid %s '%s'\n", argv0, what, start);
|
---|
| 133 | exit(2);
|
---|
| 134 | }
|
---|
| 135 | ptr <<= 4;
|
---|
| 136 | ptr |= digit;
|
---|
[2293] | 137 | str++;
|
---|
[2292] | 138 | }
|
---|
| 139 | return (void *)ptr;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[2416] | 142 | /**
|
---|
| 143 | * Do the fork.
|
---|
| 144 | * @returns same as fork().
|
---|
| 145 | * @param psh The shell that's forking.
|
---|
| 146 | */
|
---|
| 147 | int shfork_do(shinstance *psh)
|
---|
| 148 | {
|
---|
| 149 | /* save globals */
|
---|
| 150 | void *pheap_head = shheap_get_head();
|
---|
| 151 | pid_t pid = shfork_do_it(psh);
|
---|
| 152 | if (pid == 0)
|
---|
| 153 | {
|
---|
| 154 | /* reinit stuff, only the heap is copied! */
|
---|
| 155 | shthread_set_shell(psh);
|
---|
| 156 | shheap_init(pheap_head);
|
---|
| 157 | setlocale(LC_ALL, "");
|
---|
| 158 | init_syntax();
|
---|
[3468] | 159 | sh_init_globals();
|
---|
[2416] | 160 | }
|
---|
| 161 | return pid;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | /**
|
---|
[2292] | 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.
|
---|
[2293] | 171 | * @param psh The shell that's forking.
|
---|
[2292] | 172 | * @param stack_ptr The stack address at which the guest is suppost to resume.
|
---|
| 173 | */
|
---|
[2293] | 174 | int shfork_body(shinstance *psh, void *stack_ptr)
|
---|
[2292] | 175 | {
|
---|
[2293] | 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 |
|
---|
[3477] | 184 | kHlpAssert((uintptr_t)stack_ptr < (uintptr_t)g_stack_base);
|
---|
| 185 | kHlpAssert((uintptr_t)stack_ptr > (uintptr_t)g_stack_limit);
|
---|
[2416] | 186 |
|
---|
[2293] | 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';
|
---|
[2416] | 208 | TRACE2((NULL, "shfork_body: szCmdLine=%s\n", szCmdLine));
|
---|
[2293] | 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;
|
---|
[2377] | 222 | StrtInfo.dwFlags = STARTF_USESTDHANDLES;;
|
---|
[2293] | 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 | {
|
---|
[3460] | 248 | rc = sh_add_child(psh, ProcInfo.dwProcessId, ProcInfo.hProcess, NULL);
|
---|
[2293] | 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;
|
---|
[2292] | 290 | }
|
---|