aboutsummaryrefslogtreecommitdiff
path: root/shell
diff options
context:
space:
mode:
Diffstat (limited to 'shell')
-rw-r--r--shell/ash.c95
1 files changed, 47 insertions, 48 deletions
diff --git a/shell/ash.c b/shell/ash.c
index 57f87f0e1..dfdba9316 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -321,7 +321,6 @@ struct forkshell {
321 /* struct parsefile *g_parsefile; */ 321 /* struct parsefile *g_parsefile; */
322 HANDLE hMapFile; 322 HANDLE hMapFile;
323 void *old_base; 323 void *old_base;
324 int nodeptr_offset;
325 int size; 324 int size;
326 325
327 /* type of forkshell */ 326 /* type of forkshell */
@@ -334,6 +333,9 @@ struct forkshell {
334 char **argv; 333 char **argv;
335 char *string; 334 char *string;
336 struct strlist *strlist; 335 struct strlist *strlist;
336
337 /* start of data block */
338 char *nodeptr[1];
337}; 339};
338 340
339enum { 341enum {
@@ -9002,7 +9004,7 @@ static int funcstringsize; /* size of strings in node */
9002static void *funcblock; /* block to allocate function from */ 9004static void *funcblock; /* block to allocate function from */
9003static char *funcstring; /* block to allocate strings from */ 9005static char *funcstring; /* block to allocate strings from */
9004#if ENABLE_PLATFORM_MINGW32 9006#if ENABLE_PLATFORM_MINGW32
9005static int nodeptrsize; 9007static int nodeptrcount;
9006static char **nodeptr; 9008static char **nodeptr;
9007#endif 9009#endif
9008 9010
@@ -9045,7 +9047,7 @@ sizenodelist(struct nodelist *lp)
9045{ 9047{
9046 while (lp) { 9048 while (lp) {
9047 funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); 9049 funcblocksize += SHELL_ALIGN(sizeof(struct nodelist));
9048 IF_PLATFORM_MINGW32(nodeptrsize += 2); 9050 IF_PLATFORM_MINGW32(nodeptrcount += 2);
9049 calcsize(lp->n); 9051 calcsize(lp->n);
9050 lp = lp->next; 9052 lp = lp->next;
9051 } 9053 }
@@ -9062,18 +9064,18 @@ calcsize(union node *n)
9062 calcsize(n->ncmd.redirect); 9064 calcsize(n->ncmd.redirect);
9063 calcsize(n->ncmd.args); 9065 calcsize(n->ncmd.args);
9064 calcsize(n->ncmd.assign); 9066 calcsize(n->ncmd.assign);
9065 IF_PLATFORM_MINGW32(nodeptrsize += 3); 9067 IF_PLATFORM_MINGW32(nodeptrcount += 3);
9066 break; 9068 break;
9067 case NPIPE: 9069 case NPIPE:
9068 sizenodelist(n->npipe.cmdlist); 9070 sizenodelist(n->npipe.cmdlist);
9069 IF_PLATFORM_MINGW32(nodeptrsize++); 9071 IF_PLATFORM_MINGW32(nodeptrcount++);
9070 break; 9072 break;
9071 case NREDIR: 9073 case NREDIR:
9072 case NBACKGND: 9074 case NBACKGND:
9073 case NSUBSHELL: 9075 case NSUBSHELL:
9074 calcsize(n->nredir.redirect); 9076 calcsize(n->nredir.redirect);
9075 calcsize(n->nredir.n); 9077 calcsize(n->nredir.n);
9076 IF_PLATFORM_MINGW32(nodeptrsize += 2); 9078 IF_PLATFORM_MINGW32(nodeptrcount += 2);
9077 break; 9079 break;
9078 case NAND: 9080 case NAND:
9079 case NOR: 9081 case NOR:
@@ -9082,41 +9084,41 @@ calcsize(union node *n)
9082 case NUNTIL: 9084 case NUNTIL:
9083 calcsize(n->nbinary.ch2); 9085 calcsize(n->nbinary.ch2);
9084 calcsize(n->nbinary.ch1); 9086 calcsize(n->nbinary.ch1);
9085 IF_PLATFORM_MINGW32(nodeptrsize += 2); 9087 IF_PLATFORM_MINGW32(nodeptrcount += 2);
9086 break; 9088 break;
9087 case NIF: 9089 case NIF:
9088 calcsize(n->nif.elsepart); 9090 calcsize(n->nif.elsepart);
9089 calcsize(n->nif.ifpart); 9091 calcsize(n->nif.ifpart);
9090 calcsize(n->nif.test); 9092 calcsize(n->nif.test);
9091 IF_PLATFORM_MINGW32(nodeptrsize += 3); 9093 IF_PLATFORM_MINGW32(nodeptrcount += 3);
9092 break; 9094 break;
9093 case NFOR: 9095 case NFOR:
9094 funcstringsize += strlen(n->nfor.var) + 1; 9096 funcstringsize += strlen(n->nfor.var) + 1;
9095 calcsize(n->nfor.body); 9097 calcsize(n->nfor.body);
9096 calcsize(n->nfor.args); 9098 calcsize(n->nfor.args);
9097 IF_PLATFORM_MINGW32(nodeptrsize += 3); 9099 IF_PLATFORM_MINGW32(nodeptrcount += 3);
9098 break; 9100 break;
9099 case NCASE: 9101 case NCASE:
9100 calcsize(n->ncase.cases); 9102 calcsize(n->ncase.cases);
9101 calcsize(n->ncase.expr); 9103 calcsize(n->ncase.expr);
9102 IF_PLATFORM_MINGW32(nodeptrsize += 2); 9104 IF_PLATFORM_MINGW32(nodeptrcount += 2);
9103 break; 9105 break;
9104 case NCLIST: 9106 case NCLIST:
9105 calcsize(n->nclist.body); 9107 calcsize(n->nclist.body);
9106 calcsize(n->nclist.pattern); 9108 calcsize(n->nclist.pattern);
9107 calcsize(n->nclist.next); 9109 calcsize(n->nclist.next);
9108 IF_PLATFORM_MINGW32(nodeptrsize += 3); 9110 IF_PLATFORM_MINGW32(nodeptrcount += 3);
9109 break; 9111 break;
9110 case NDEFUN: 9112 case NDEFUN:
9111 calcsize(n->ndefun.body); 9113 calcsize(n->ndefun.body);
9112 funcstringsize += strlen(n->ndefun.text) + 1; 9114 funcstringsize += strlen(n->ndefun.text) + 1;
9113 IF_PLATFORM_MINGW32(nodeptrsize += 2); 9115 IF_PLATFORM_MINGW32(nodeptrcount += 2);
9114 break; 9116 break;
9115 case NARG: 9117 case NARG:
9116 sizenodelist(n->narg.backquote); 9118 sizenodelist(n->narg.backquote);
9117 funcstringsize += strlen(n->narg.text) + 1; 9119 funcstringsize += strlen(n->narg.text) + 1;
9118 calcsize(n->narg.next); 9120 calcsize(n->narg.next);
9119 IF_PLATFORM_MINGW32(nodeptrsize += 3); 9121 IF_PLATFORM_MINGW32(nodeptrcount += 3);
9120 break; 9122 break;
9121 case NTO: 9123 case NTO:
9122#if BASH_REDIR_OUTPUT 9124#if BASH_REDIR_OUTPUT
@@ -9128,23 +9130,23 @@ calcsize(union node *n)
9128 case NAPPEND: 9130 case NAPPEND:
9129 calcsize(n->nfile.fname); 9131 calcsize(n->nfile.fname);
9130 calcsize(n->nfile.next); 9132 calcsize(n->nfile.next);
9131 IF_PLATFORM_MINGW32(nodeptrsize += 2); 9133 IF_PLATFORM_MINGW32(nodeptrcount += 2);
9132 break; 9134 break;
9133 case NTOFD: 9135 case NTOFD:
9134 case NFROMFD: 9136 case NFROMFD:
9135 calcsize(n->ndup.vname); 9137 calcsize(n->ndup.vname);
9136 calcsize(n->ndup.next); 9138 calcsize(n->ndup.next);
9137 IF_PLATFORM_MINGW32(nodeptrsize += 2); 9139 IF_PLATFORM_MINGW32(nodeptrcount += 2);
9138 break; 9140 break;
9139 case NHERE: 9141 case NHERE:
9140 case NXHERE: 9142 case NXHERE:
9141 calcsize(n->nhere.doc); 9143 calcsize(n->nhere.doc);
9142 calcsize(n->nhere.next); 9144 calcsize(n->nhere.next);
9143 IF_PLATFORM_MINGW32(nodeptrsize += 2); 9145 IF_PLATFORM_MINGW32(nodeptrcount += 2);
9144 break; 9146 break;
9145 case NNOT: 9147 case NNOT:
9146 calcsize(n->nnot.com); 9148 calcsize(n->nnot.com);
9147 IF_PLATFORM_MINGW32(nodeptrsize++); 9149 IF_PLATFORM_MINGW32(nodeptrcount++);
9148 break; 9150 break;
9149 }; 9151 };
9150} 9152}
@@ -15000,7 +15002,7 @@ spawn_forkshell(struct job *jp, struct forkshell *fs, int mode)
15000 * forkshell_prepare() and friends 15002 * forkshell_prepare() and friends
15001 * 15003 *
15002 * The sequence is as follows: 15004 * The sequence is as follows:
15003 * - funcblocksize, funcstringsize, nodeptrsize are initialized 15005 * - funcblocksize, funcstringsize, nodeptrcount are initialized
15004 * - forkshell_size(fs) is called to calculate the exact memory needed 15006 * - forkshell_size(fs) is called to calculate the exact memory needed
15005 * - a new struct is allocated 15007 * - a new struct is allocated
15006 * - funcblock, funcstring, nodeptr are initialized from the new block 15008 * - funcblock, funcstring, nodeptr are initialized from the new block
@@ -15017,7 +15019,7 @@ name(type *p) \
15017 funcblocksize += sizeof(type); 15019 funcblocksize += sizeof(type);
15018 /* do something here with p */ 15020 /* do something here with p */
15019#define SLIST_SIZE_END() \ 15021#define SLIST_SIZE_END() \
15020 nodeptrsize++; \ 15022 nodeptrcount++; \
15021 p = p->next; \ 15023 p = p->next; \
15022 } \ 15024 } \
15023} 15025}
@@ -15047,7 +15049,7 @@ name(type *vp) \
15047 */ 15049 */
15048SLIST_SIZE_BEGIN(var_size,struct var) 15050SLIST_SIZE_BEGIN(var_size,struct var)
15049funcstringsize += strlen(p->var_text) + 1; 15051funcstringsize += strlen(p->var_text) + 1;
15050nodeptrsize++; /* p->text */ 15052nodeptrcount++; /* p->text */
15051SLIST_SIZE_END() 15053SLIST_SIZE_END()
15052 15054
15053SLIST_COPY_BEGIN(var_copy,struct var) 15055SLIST_COPY_BEGIN(var_copy,struct var)
@@ -15062,7 +15064,7 @@ SLIST_COPY_END()
15062 */ 15064 */
15063SLIST_SIZE_BEGIN(strlist_size,struct strlist) 15065SLIST_SIZE_BEGIN(strlist_size,struct strlist)
15064funcstringsize += strlen(p->text) + 1; 15066funcstringsize += strlen(p->text) + 1;
15065nodeptrsize++; /* p->text */ 15067nodeptrcount++; /* p->text */
15066SLIST_SIZE_END() 15068SLIST_SIZE_END()
15067 15069
15068SLIST_COPY_BEGIN(strlist_copy,struct strlist) 15070SLIST_COPY_BEGIN(strlist_copy,struct strlist)
@@ -15082,9 +15084,9 @@ tblentry_size(struct tblentry *tep)
15082 if (tep->cmdtype == CMDFUNCTION) { 15084 if (tep->cmdtype == CMDFUNCTION) {
15083 funcblocksize += offsetof(struct funcnode, n); 15085 funcblocksize += offsetof(struct funcnode, n);
15084 calcsize(&tep->param.func->n); 15086 calcsize(&tep->param.func->n);
15085 nodeptrsize++; /* tep->param.func */ 15087 nodeptrcount++; /* tep->param.func */
15086 } 15088 }
15087 nodeptrsize++; /* tep->next */ 15089 nodeptrcount++; /* tep->next */
15088 tep = tep->next; 15090 tep = tep->next;
15089 } 15091 }
15090} 15092}
@@ -15129,7 +15131,7 @@ static void
15129cmdtable_size(struct tblentry **cmdtablep) 15131cmdtable_size(struct tblentry **cmdtablep)
15130{ 15132{
15131 int i; 15133 int i;
15132 nodeptrsize += CMDTABLESIZE; 15134 nodeptrcount += CMDTABLESIZE;
15133 funcblocksize += sizeof(struct tblentry *)*CMDTABLESIZE; 15135 funcblocksize += sizeof(struct tblentry *)*CMDTABLESIZE;
15134 for (i = 0; i < CMDTABLESIZE; i++) 15136 for (i = 0; i < CMDTABLESIZE; i++)
15135 tblentry_size(cmdtablep[i]); 15137 tblentry_size(cmdtablep[i]);
@@ -15158,7 +15160,7 @@ argv_size(char **p)
15158 while (p && *p) { 15160 while (p && *p) {
15159 funcblocksize += sizeof(char *); 15161 funcblocksize += sizeof(char *);
15160 funcstringsize += strlen(*p)+1; 15162 funcstringsize += strlen(*p)+1;
15161 nodeptrsize++; 15163 nodeptrcount++;
15162 p++; 15164 p++;
15163 } 15165 }
15164 funcblocksize += sizeof(char *); 15166 funcblocksize += sizeof(char *);
@@ -15192,7 +15194,7 @@ redirtab_size(struct redirtab *rdtp)
15192 while (rdtp) { 15194 while (rdtp) {
15193 funcblocksize += sizeof(*rdtp)+sizeof(rdtp->two_fd[0])*rdtp->pair_count; 15195 funcblocksize += sizeof(*rdtp)+sizeof(rdtp->two_fd[0])*rdtp->pair_count;
15194 rdtp = rdtp->next; 15196 rdtp = rdtp->next;
15195 nodeptrsize++; /* rdtp->next */ 15197 nodeptrcount++; /* rdtp->next */
15196 } 15198 }
15197} 15199}
15198 15200
@@ -15230,7 +15232,8 @@ globals_var_size(struct globals_var *gvp)
15230 redirtab_size(gvp->redirlist); 15232 redirtab_size(gvp->redirlist);
15231 for (i = 0; i < VTABSIZE; i++) 15233 for (i = 0; i < VTABSIZE; i++)
15232 var_size(gvp->vartab[i]); 15234 var_size(gvp->vartab[i]);
15233 nodeptrsize += 2 + VTABSIZE; /* gvp->redirlist, gvp->shellparam.p, vartab */ 15235 /* gvp->redirlist, gvp->shellparam.p, vartab */
15236 nodeptrcount += 2 + VTABSIZE;
15234} 15237}
15235 15238
15236#undef preverrout_fd 15239#undef preverrout_fd
@@ -15280,7 +15283,7 @@ globals_misc_size(struct globals_misc *p)
15280 if (p->physdir != p->nullstr) 15283 if (p->physdir != p->nullstr)
15281 funcstringsize += strlen(p->physdir) + 1; 15284 funcstringsize += strlen(p->physdir) + 1;
15282 funcstringsize += strlen(p->arg0) + 1; 15285 funcstringsize += strlen(p->arg0) + 1;
15283 nodeptrsize += 4; /* minusc, curdir, physdir, arg0 */ 15286 nodeptrcount += 4; /* minusc, curdir, physdir, arg0 */
15284} 15287}
15285 15288
15286static struct globals_misc * 15289static struct globals_misc *
@@ -15302,7 +15305,6 @@ globals_misc_copy(struct globals_misc *p)
15302static void 15305static void
15303forkshell_size(struct forkshell *fs) 15306forkshell_size(struct forkshell *fs)
15304{ 15307{
15305 funcblocksize += sizeof(struct forkshell);
15306 globals_var_size(fs->gvp); 15308 globals_var_size(fs->gvp);
15307 globals_misc_size(fs->gmp); 15309 globals_misc_size(fs->gmp);
15308 cmdtable_size(fs->cmdtable); 15310 cmdtable_size(fs->cmdtable);
@@ -15314,17 +15316,12 @@ forkshell_size(struct forkshell *fs)
15314 funcstringsize += (fs->string ? strlen(fs->string) : 0) + 1; 15316 funcstringsize += (fs->string ? strlen(fs->string) : 0) + 1;
15315 strlist_size(fs->strlist); 15317 strlist_size(fs->strlist);
15316 15318
15317 nodeptrsize += 7; /* gvp, gmp, cmdtable, n, argv, string, strlist */ 15319 nodeptrcount += 7; /* gvp, gmp, cmdtable, n, argv, string, strlist */
15318} 15320}
15319 15321
15320static struct forkshell * 15322static struct forkshell *
15321forkshell_copy(struct forkshell *fs) 15323forkshell_copy(struct forkshell *fs, struct forkshell *new)
15322{ 15324{
15323 struct forkshell *new;
15324
15325 new = funcblock;
15326 funcblock = (char *) funcblock + sizeof(struct forkshell);
15327
15328 memcpy(new, fs, sizeof(struct forkshell)); /* non-pointer stuff */ 15325 memcpy(new, fs, sizeof(struct forkshell)); /* non-pointer stuff */
15329 new->gvp = globals_var_copy(fs->gvp); 15326 new->gvp = globals_var_copy(fs->gvp);
15330 new->gmp = globals_misc_copy(fs->gmp); 15327 new->gmp = globals_misc_copy(fs->gmp);
@@ -15343,7 +15340,7 @@ static struct forkshell *
15343forkshell_prepare(struct forkshell *fs) 15340forkshell_prepare(struct forkshell *fs)
15344{ 15341{
15345 struct forkshell *new; 15342 struct forkshell *new;
15346 int size, nodeptr_offset; 15343 int size;
15347 HANDLE h; 15344 HANDLE h;
15348 SECURITY_ATTRIBUTES sa; 15345 SECURITY_ATTRIBUTES sa;
15349 15346
@@ -15352,11 +15349,17 @@ forkshell_prepare(struct forkshell *fs)
15352 fs->gmp = ash_ptr_to_globals_misc; 15349 fs->gmp = ash_ptr_to_globals_misc;
15353 fs->cmdtable = cmdtable; 15350 fs->cmdtable = cmdtable;
15354 15351
15355 nodeptrsize = 1; /* NULL terminated */ 15352 /*
15353 * Careful: much scope for off-by-one errors. nodeptrcount is the
15354 * number of actual pointers. There's also a terminating NULL pointer.
15355 * The array in the forkshell structure gives us one element for free.
15356 */
15357 nodeptrcount = 0;
15356 funcblocksize = 0; 15358 funcblocksize = 0;
15357 funcstringsize = 0; 15359 funcstringsize = 0;
15358 forkshell_size(fs); 15360 forkshell_size(fs);
15359 size = funcblocksize + funcstringsize + nodeptrsize*sizeof(char *); 15361 size = sizeof(struct forkshell) + nodeptrcount*sizeof(char *) +
15362 funcblocksize + funcstringsize;
15360 15363
15361 /* Allocate, initialize pointers */ 15364 /* Allocate, initialize pointers */
15362 memset(&sa, 0, sizeof(sa)); 15365 memset(&sa, 0, sizeof(sa));
@@ -15365,19 +15368,16 @@ forkshell_prepare(struct forkshell *fs)
15365 sa.bInheritHandle = TRUE; 15368 sa.bInheritHandle = TRUE;
15366 h = CreateFileMapping(INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE, 0, size, NULL); 15369 h = CreateFileMapping(INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE, 0, size, NULL);
15367 new = (struct forkshell *)MapViewOfFile(h, FILE_MAP_WRITE, 0,0, 0); 15370 new = (struct forkshell *)MapViewOfFile(h, FILE_MAP_WRITE, 0,0, 0);
15368 /* new = ckmalloc(size); */ 15371 nodeptr = new->nodeptr;
15369 funcblock = new; 15372 funcblock = (char *)nodeptr + (nodeptrcount+1)*sizeof(char *);
15370 funcstring = (char *) funcblock + funcblocksize; 15373 funcstring = (char *) funcblock + funcblocksize;
15371 nodeptr = (char **)((char *)funcstring + funcstringsize);
15372 nodeptr_offset = (char *)nodeptr - (char *)new;
15373 15374
15374 /* Now pack them all */ 15375 /* Now pack them all */
15375 forkshell_copy(fs); 15376 forkshell_copy(fs, new);
15376 15377
15377 /* Finish it up */ 15378 /* Finish it up */
15378 *nodeptr = NULL; 15379 *nodeptr = NULL;
15379 new->size = size; 15380 new->size = size;
15380 new->nodeptr_offset = nodeptr_offset;
15381 new->old_base = new; 15381 new->old_base = new;
15382 new->hMapFile = h; 15382 new->hMapFile = h;
15383 return new; 15383 return new;
@@ -15411,9 +15411,8 @@ forkshell_init(const char *idstr)
15411 sticky_mem_end = (char *) fs + fs->size; 15411 sticky_mem_end = (char *) fs + fs->size;
15412 15412
15413 /* pointer fixup */ 15413 /* pointer fixup */
15414 nodeptr = (char **)((char *)fs + fs->nodeptr_offset); 15414 for ( i=0; fs->nodeptr[i]; ++i ) {
15415 for ( i=0; nodeptr[i]; ++i ) { 15415 ptr = (char **)((char *)fs + (fs->nodeptr[i] - (char *)fs->old_base));
15416 ptr = (char **)((char *)fs + (nodeptr[i] - (char *)fs->old_base));
15417 if (*ptr) 15416 if (*ptr)
15418 *ptr = (char *)fs + (*ptr - (char *)fs->old_base); 15417 *ptr = (char *)fs + (*ptr - (char *)fs->old_base);
15419 } 15418 }