diff options
author | Ron Yorston <rmy@pobox.com> | 2014-04-30 20:11:59 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2014-04-30 20:11:59 +0100 |
commit | 9b44b5e6c0f31057dd95982b1c65a89a90f1ca5c (patch) | |
tree | 39f1bfcf92bfe639e4df21710a06bf516a9a39b6 /shell | |
parent | d323477244a4d32eb1297d7a4329beab3e8c5e80 (diff) | |
download | busybox-w32-9b44b5e6c0f31057dd95982b1c65a89a90f1ca5c.tar.gz busybox-w32-9b44b5e6c0f31057dd95982b1c65a89a90f1ca5c.tar.bz2 busybox-w32-9b44b5e6c0f31057dd95982b1c65a89a90f1ca5c.zip |
ash: further tidying up of MinGW forkshell
The forkpoint_fn in the forkshell structure was redundant as
the fpid element indicated which function to call in the child.
Explicitly set fpid before calling spawn_forkshell and replace
the forkpoint_fn array with a function, forkshell_child.
Diffstat (limited to 'shell')
-rw-r--r-- | shell/ash.c | 88 |
1 files changed, 47 insertions, 41 deletions
diff --git a/shell/ash.c b/shell/ash.c index 0d88d4108..9a7147d72 100644 --- a/shell/ash.c +++ b/shell/ash.c | |||
@@ -212,12 +212,10 @@ | |||
212 | //kbuild:lib-$(CONFIG_ASH_RANDOM_SUPPORT) += random.o | 212 | //kbuild:lib-$(CONFIG_ASH_RANDOM_SUPPORT) += random.o |
213 | 213 | ||
214 | #if ENABLE_PLATFORM_MINGW32 | 214 | #if ENABLE_PLATFORM_MINGW32 |
215 | struct forkshell; | ||
216 | union node; | 215 | union node; |
217 | struct strlist; | 216 | struct strlist; |
218 | struct job; | 217 | struct job; |
219 | 218 | ||
220 | typedef void (*forkpoint_fn)(struct forkshell *fs); | ||
221 | struct forkshell { | 219 | struct forkshell { |
222 | /* filled by forkshell_copy() */ | 220 | /* filled by forkshell_copy() */ |
223 | struct globals_var *gvp; | 221 | struct globals_var *gvp; |
@@ -225,40 +223,34 @@ struct forkshell { | |||
225 | struct tblentry **cmdtable; | 223 | struct tblentry **cmdtable; |
226 | /* struct alias **atab; */ | 224 | /* struct alias **atab; */ |
227 | /* struct parsefile *g_parsefile; */ | 225 | /* struct parsefile *g_parsefile; */ |
228 | int fpid; | ||
229 | HANDLE hMapFile; | 226 | HANDLE hMapFile; |
230 | void *old_base; | 227 | void *old_base; |
231 | int nodeptr_offset; | 228 | int nodeptr_offset; |
232 | int size; | 229 | int size; |
233 | 230 | ||
234 | forkpoint_fn fp; | 231 | /* type of forkshell */ |
235 | /* optional data, used by forkpoint_fn */ | 232 | int fpid; |
233 | |||
234 | /* optional data, used by forkshell_child */ | ||
236 | int flags; | 235 | int flags; |
237 | int fd[10]; | 236 | int fd[10]; |
238 | union node *n; | 237 | union node *n; |
239 | char **argv; | 238 | char **argv; |
240 | char *string; | 239 | char *string; |
241 | struct strlist *strlist; | 240 | struct strlist *strlist; |
242 | pid_t pid; | ||
243 | }; | 241 | }; |
244 | 242 | ||
245 | static void forkshell_openhere(struct forkshell *fs); | 243 | enum { |
246 | static void forkshell_evalbackcmd(struct forkshell *fs); | 244 | FS_OPENHERE, |
247 | static void forkshell_evalsubshell(struct forkshell *fs); | 245 | FS_EVALBACKCMD, |
248 | static void forkshell_evalpipe(struct forkshell *fs); | 246 | FS_EVALSUBSHELL, |
249 | static void forkshell_shellexec(struct forkshell *fs); | 247 | FS_EVALPIPE, |
250 | 248 | FS_SHELLEXEC | |
251 | static const forkpoint_fn forkpoints[] = { | ||
252 | forkshell_openhere, | ||
253 | forkshell_evalbackcmd, | ||
254 | forkshell_evalsubshell, | ||
255 | forkshell_evalpipe, | ||
256 | forkshell_shellexec, | ||
257 | NULL | ||
258 | }; | 249 | }; |
259 | 250 | ||
260 | static struct forkshell* forkshell_prepare(struct forkshell *fs); | 251 | static struct forkshell* forkshell_prepare(struct forkshell *fs); |
261 | static void forkshell_init(const char *idstr); | 252 | static void forkshell_init(const char *idstr); |
253 | static void forkshell_child(struct forkshell *fs); | ||
262 | static void sticky_free(void *p); | 254 | static void sticky_free(void *p); |
263 | #define free(p) sticky_free(p) | 255 | #define free(p) sticky_free(p) |
264 | static int spawn_forkshell(struct job *jp, struct forkshell *fs, int mode); | 256 | static int spawn_forkshell(struct job *jp, struct forkshell *fs, int mode); |
@@ -5353,8 +5345,7 @@ openhere(union node *redir) | |||
5353 | } | 5345 | } |
5354 | #if ENABLE_PLATFORM_MINGW32 | 5346 | #if ENABLE_PLATFORM_MINGW32 |
5355 | memset(&fs, 0, sizeof(fs)); | 5347 | memset(&fs, 0, sizeof(fs)); |
5356 | fs.fp = forkshell_openhere; | 5348 | fs.fpid = FS_OPENHERE; |
5357 | fs.flags = 0; | ||
5358 | fs.n = redir; | 5349 | fs.n = redir; |
5359 | fs.fd[0] = pip[0]; | 5350 | fs.fd[0] = pip[0]; |
5360 | fs.fd[1] = pip[1]; | 5351 | fs.fd[1] = pip[1]; |
@@ -6170,7 +6161,7 @@ evalbackcmd(union node *n, struct backcmd *result) | |||
6170 | ash_msg_and_raise_error("pipe call failed"); | 6161 | ash_msg_and_raise_error("pipe call failed"); |
6171 | jp = makejob(/*n,*/ 1); | 6162 | jp = makejob(/*n,*/ 1); |
6172 | #if ENABLE_PLATFORM_MINGW32 | 6163 | #if ENABLE_PLATFORM_MINGW32 |
6173 | result->fs.fp = forkshell_evalbackcmd; | 6164 | result->fs.fpid = FS_EVALBACKCMD; |
6174 | result->fs.n = n; | 6165 | result->fs.n = n; |
6175 | result->fs.fd[0] = pip[0]; | 6166 | result->fs.fd[0] = pip[0]; |
6176 | result->fs.fd[1] = pip[1]; | 6167 | result->fs.fd[1] = pip[1]; |
@@ -8994,7 +8985,7 @@ evalsubshell(union node *n, int flags) | |||
8994 | jp = makejob(/*n,*/ 1); | 8985 | jp = makejob(/*n,*/ 1); |
8995 | #if ENABLE_PLATFORM_MINGW32 | 8986 | #if ENABLE_PLATFORM_MINGW32 |
8996 | memset(&fs, 0, sizeof(fs)); | 8987 | memset(&fs, 0, sizeof(fs)); |
8997 | fs.fp = forkshell_evalsubshell; | 8988 | fs.fpid = FS_EVALSUBSHELL; |
8998 | fs.n = n; | 8989 | fs.n = n; |
8999 | fs.flags = flags; | 8990 | fs.flags = flags; |
9000 | if (spawn_forkshell(jp, &fs, backgnd) < 0) | 8991 | if (spawn_forkshell(jp, &fs, backgnd) < 0) |
@@ -9119,7 +9110,7 @@ evalpipe(union node *n, int flags) | |||
9119 | } | 9110 | } |
9120 | #if ENABLE_PLATFORM_MINGW32 | 9111 | #if ENABLE_PLATFORM_MINGW32 |
9121 | memset(&fs, 0, sizeof(fs)); | 9112 | memset(&fs, 0, sizeof(fs)); |
9122 | fs.fp = forkshell_evalpipe; | 9113 | fs.fpid = FS_EVALPIPE; |
9123 | fs.flags = flags; | 9114 | fs.flags = flags; |
9124 | fs.n = lp->n; | 9115 | fs.n = lp->n; |
9125 | fs.fd[0] = pip[0]; | 9116 | fs.fd[0] = pip[0]; |
@@ -9787,7 +9778,7 @@ evalcommand(union node *cmd, int flags) | |||
9787 | struct forkshell fs; | 9778 | struct forkshell fs; |
9788 | 9779 | ||
9789 | memset(&fs, 0, sizeof(fs)); | 9780 | memset(&fs, 0, sizeof(fs)); |
9790 | fs.fp = forkshell_shellexec; | 9781 | fs.fpid = FS_SHELLEXEC; |
9791 | fs.argv = argv; | 9782 | fs.argv = argv; |
9792 | fs.string = (char*)path; | 9783 | fs.string = (char*)path; |
9793 | fs.fd[0] = cmdentry.u.index; | 9784 | fs.fd[0] = cmdentry.u.index; |
@@ -13935,6 +13926,28 @@ forkshell_shellexec(struct forkshell *fs) | |||
13935 | shellexec(argv, path, idx); | 13926 | shellexec(argv, path, idx); |
13936 | } | 13927 | } |
13937 | 13928 | ||
13929 | static void | ||
13930 | forkshell_child(struct forkshell *fs) | ||
13931 | { | ||
13932 | switch ( fs->fpid ) { | ||
13933 | case FS_OPENHERE: | ||
13934 | forkshell_openhere(fs); | ||
13935 | break; | ||
13936 | case FS_EVALBACKCMD: | ||
13937 | forkshell_evalbackcmd(fs); | ||
13938 | break; | ||
13939 | case FS_EVALSUBSHELL: | ||
13940 | forkshell_evalsubshell(fs); | ||
13941 | break; | ||
13942 | case FS_EVALPIPE: | ||
13943 | forkshell_evalpipe(fs); | ||
13944 | break; | ||
13945 | case FS_SHELLEXEC: | ||
13946 | forkshell_shellexec(fs); | ||
13947 | break; | ||
13948 | } | ||
13949 | } | ||
13950 | |||
13938 | /* | 13951 | /* |
13939 | * Reset the pointers to the builtin environment variables in the hash | 13952 | * Reset the pointers to the builtin environment variables in the hash |
13940 | * table to point to varinit rather than the bogus copy created during | 13953 | * table to point to varinit rather than the bogus copy created during |
@@ -13963,23 +13976,24 @@ reinitvar(void) | |||
13963 | static int | 13976 | static int |
13964 | spawn_forkshell(struct job *jp, struct forkshell *fs, int mode) | 13977 | spawn_forkshell(struct job *jp, struct forkshell *fs, int mode) |
13965 | { | 13978 | { |
13966 | const char *argv[] = { "sh", "--forkshell", NULL, NULL }; | 13979 | struct forkshell *new; |
13967 | char buf[16]; | 13980 | char buf[16]; |
13981 | const char *argv[] = { "sh", "--forkshell", NULL, NULL }; | ||
13982 | pid_t pid; | ||
13968 | 13983 | ||
13969 | struct forkshell *new; | ||
13970 | new = forkshell_prepare(fs); | 13984 | new = forkshell_prepare(fs); |
13971 | sprintf(buf, "%x", (unsigned int)new->hMapFile); | 13985 | sprintf(buf, "%x", (unsigned int)new->hMapFile); |
13972 | argv[2] = buf; | 13986 | argv[2] = buf; |
13973 | fs->pid = mingw_spawn_applet(P_NOWAIT, "sh", argv, | 13987 | pid = mingw_spawn_applet(P_NOWAIT, "sh", argv, |
13974 | (const char *const *)environ); | 13988 | (const char *const *)environ); |
13975 | CloseHandle(new->hMapFile); | 13989 | CloseHandle(new->hMapFile); |
13976 | UnmapViewOfFile(new); | 13990 | UnmapViewOfFile(new); |
13977 | if (fs->pid == -1) { | 13991 | if (pid == -1) { |
13978 | free(jp); | 13992 | free(jp); |
13979 | return -1; | 13993 | return -1; |
13980 | } | 13994 | } |
13981 | forkparent(jp, fs->node, mode, fs->pid); | 13995 | forkparent(jp, fs->node, mode, pid); |
13982 | return fs->pid; | 13996 | return pid; |
13983 | } | 13997 | } |
13984 | 13998 | ||
13985 | /* | 13999 | /* |
@@ -14342,17 +14356,10 @@ static struct forkshell * | |||
14342 | forkshell_prepare(struct forkshell *fs) | 14356 | forkshell_prepare(struct forkshell *fs) |
14343 | { | 14357 | { |
14344 | struct forkshell *new; | 14358 | struct forkshell *new; |
14345 | int size, fp, nodeptr_offset; | 14359 | int size, nodeptr_offset; |
14346 | HANDLE h; | 14360 | HANDLE h; |
14347 | SECURITY_ATTRIBUTES sa; | 14361 | SECURITY_ATTRIBUTES sa; |
14348 | 14362 | ||
14349 | for (fp = 0; forkpoints[fp] && forkpoints[fp] != fs->fp; fp++) | ||
14350 | ; | ||
14351 | |||
14352 | if (!forkpoints[fp]) | ||
14353 | bb_error_msg_and_die("invalid forkpoint %08x", (int)fs->fp); | ||
14354 | fs->fpid = fp; | ||
14355 | |||
14356 | /* Calculate size of "new" */ | 14363 | /* Calculate size of "new" */ |
14357 | fs->gvp = ash_ptr_to_globals_var; | 14364 | fs->gvp = ash_ptr_to_globals_var; |
14358 | fs->gmp = ash_ptr_to_globals_misc; | 14365 | fs->gmp = ash_ptr_to_globals_misc; |
@@ -14423,7 +14430,6 @@ forkshell_init(const char *idstr) | |||
14423 | nodeptr++; | 14430 | nodeptr++; |
14424 | } | 14431 | } |
14425 | /* Now fix up stuff that can't be transferred */ | 14432 | /* Now fix up stuff that can't be transferred */ |
14426 | fs->fp = forkpoints[fs->fpid]; | ||
14427 | for (i = 0; i < ARRAY_SIZE(varinit_data); i++) | 14433 | for (i = 0; i < ARRAY_SIZE(varinit_data); i++) |
14428 | fs->gvp->varinit[i].var_func = varinit_data[i].var_func; | 14434 | fs->gvp->varinit[i].var_func = varinit_data[i].var_func; |
14429 | for (i = 0; i < CMDTABLESIZE; i++) { | 14435 | for (i = 0; i < CMDTABLESIZE; i++) { |
@@ -14450,7 +14456,7 @@ forkshell_init(const char *idstr) | |||
14450 | 14456 | ||
14451 | reinitvar(); | 14457 | reinitvar(); |
14452 | 14458 | ||
14453 | fs->fp(fs); | 14459 | forkshell_child(fs); |
14454 | } | 14460 | } |
14455 | 14461 | ||
14456 | #undef free | 14462 | #undef free |