1 |
|
---|
2 |
|
---|
3 | /*******************************************************************************
|
---|
4 | * Header Files *
|
---|
5 | *******************************************************************************/
|
---|
6 | #include <string.h>
|
---|
7 | #include <locale.h>
|
---|
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)
|
---|
16 |
|
---|
17 |
|
---|
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: */
|
---|
31 | extern void shfork_resume(void *cur, void *base, void *limit);
|
---|
32 |
|
---|
33 | /* called by shforkA-win.asm: */
|
---|
34 | void *shfork_maybe_forked(int argc, char **argv, char **envp);
|
---|
35 | extern int shfork_body(shinstance *psh, void *stack_ptr);
|
---|
36 |
|
---|
37 |
|
---|
38 | /***
|
---|
39 | * Called by shforkA-win.asm to check whether we're a forked child
|
---|
40 | * process or not.
|
---|
41 | *
|
---|
42 | * In the former case we will resume execution at the fork resume
|
---|
43 | * point. In the latter we'll allocate a new stack of the forkable
|
---|
44 | * heap and return it to the caller so real_main() in main.c can be
|
---|
45 | * invoked on it.
|
---|
46 | *
|
---|
47 | * @returns Stack or not at all.
|
---|
48 | * @param argc Argument count.
|
---|
49 | * @param argv Argument vector.
|
---|
50 | * @param envp Environment vector.
|
---|
51 | */
|
---|
52 | void *shfork_maybe_forked(int argc, char **argv, char **envp)
|
---|
53 | {
|
---|
54 | void *stack_ptr;
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * Are we actually forking?
|
---|
58 | */
|
---|
59 | if ( argc != 8
|
---|
60 | || strcmp(argv[1], "--!forked!--")
|
---|
61 | || strcmp(argv[2], "--stack-address")
|
---|
62 | || strcmp(argv[4], "--stack-base")
|
---|
63 | || strcmp(argv[6], "--stack-limit"))
|
---|
64 | {
|
---|
65 | char *stack;
|
---|
66 | shheap_init();
|
---|
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;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * Do any init that needs to be done before resuming the
|
---|
75 | * fork() call.
|
---|
76 | */
|
---|
77 | setlocale(LC_ALL, "");
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * Convert the stack addresses.
|
---|
81 | */
|
---|
82 | stack_ptr = shfork_string_to_ptr(argv[3], argv[0], "--stack-address");
|
---|
83 | g_stack_base = shfork_string_to_ptr(argv[5], argv[0], "--stack-base");
|
---|
84 | g_stack_limit = shfork_string_to_ptr(argv[7], argv[0], "--stack-limit");
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Switch stack and jump to the fork resume point.
|
---|
88 | */
|
---|
89 | shfork_resume(stack_ptr, g_stack_base, g_stack_limit);
|
---|
90 | /* (won't get here) */
|
---|
91 | return NULL;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /***
|
---|
95 | * Converts a string into a pointer.
|
---|
96 | *
|
---|
97 | * @returns Pointer.
|
---|
98 | * @param argv0 The program name in case of error.
|
---|
99 | * @param str The string to convert.
|
---|
100 | */
|
---|
101 | static void *shfork_string_to_ptr(const char *str, const char *argv0, const char *what)
|
---|
102 | {
|
---|
103 | const char *start = str;
|
---|
104 | intptr_t ptr = 0;
|
---|
105 | if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
|
---|
106 | str += 2;
|
---|
107 | while (*str)
|
---|
108 | {
|
---|
109 | unsigned digit;
|
---|
110 | switch (*str)
|
---|
111 | {
|
---|
112 | case '0': digit = 0; break;
|
---|
113 | case '1': digit = 1; break;
|
---|
114 | case '2': digit = 2; break;
|
---|
115 | case '3': digit = 3; break;
|
---|
116 | case '4': digit = 4; break;
|
---|
117 | case '5': digit = 5; break;
|
---|
118 | case '6': digit = 6; break;
|
---|
119 | case '7': digit = 7; break;
|
---|
120 | case '8': digit = 8; break;
|
---|
121 | case '9': digit = 9; break;
|
---|
122 | case 'a': case 'A': digit = 0xa; break;
|
---|
123 | case 'b': case 'B': digit = 0xb; break;
|
---|
124 | case 'c': case 'C': digit = 0xc; break;
|
---|
125 | case 'd': case 'D': digit = 0xd; break;
|
---|
126 | case 'e': case 'E': digit = 0xe; break;
|
---|
127 | case 'f': case 'F': digit = 0xf; break;
|
---|
128 | default:
|
---|
129 | fprintf(stderr, "%s: fatal error: Invalid %s '%s'\n", argv0, what, start);
|
---|
130 | exit(2);
|
---|
131 | }
|
---|
132 | ptr <<= 4;
|
---|
133 | ptr |= digit;
|
---|
134 | str++;
|
---|
135 | }
|
---|
136 | return (void *)ptr;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /***
|
---|
140 | * Create the child process making sure it inherits all our handles,
|
---|
141 | * copy of the forkable heap and kick it off.
|
---|
142 | *
|
---|
143 | * Called by shfork_do_it() in shforkA-win.asm.
|
---|
144 | *
|
---|
145 | * @returns child pid on success, -1 and errno on failure.
|
---|
146 | * @param psh The shell that's forking.
|
---|
147 | * @param stack_ptr The stack address at which the guest is suppost to resume.
|
---|
148 | */
|
---|
149 | int shfork_body(shinstance *psh, void *stack_ptr)
|
---|
150 | {
|
---|
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 = STARTF_USESTDHANDLES;;
|
---|
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;
|
---|
261 | }
|
---|