void dotitle(Char **vc, struct command * c) { int k; char titlebuf[512]; char errbuf[128],err2[128]; char **v; Char **nvc; UNREFERENCED_PARAMETER(c); vc++; nvc = glob_all_or_error(vc); if (nvc == NULL) return; if (nvc != vc) cleanup_push(nvc, blk_cleanup); if ((k = GetConsoleTitle(titlebuf, sizeof(titlebuf))) != 0) { setcopy(STRoldtitle,str2short(titlebuf), VAR_READWRITE|VAR_NOGLOB); } titlebuf[0] = '\0'; v = short2blk(nvc); if (nvc != vc) cleanup_until(nvc); cleanup_push((Char **)v, blk_cleanup); for (k = 0; v[k] != NULL ; k++){ __try { StringCbCat(titlebuf,sizeof(titlebuf),v[k]); StringCbCat(titlebuf,sizeof(titlebuf)," "); } __except(GetExceptionCode()) { stderror(ERR_TOOMANY); } } if (!SetConsoleTitle(titlebuf) ) { make_err_str(GetLastError(),errbuf,128); (void)StringCbPrintf(err2,sizeof(err2),"%s",v[k]); stderror(ERR_SYSTEM,err2,errbuf); } cleanup_until((Char **)v); return; }
void dops(Char ** vc, struct command *c) { DWORD nump; unsigned int i,k; char **v; if (!ProcessListFunc) return; gflag = 0; tglob(vc); if (gflag) { vc = globall(vc); if (vc == 0) stderror(ERR_NAME | ERR_NOMATCH); } else vc = gargv = saveblk(vc); trim(vc); v = short2blk(vc); for (k = 0; v[k] != NULL ; k++){ if ( v[k][0] == '-' ) { if( (v[k][1] == 'W') || (v[k][1] == 'w')) g_dowindows = 1; } } nump = ProcessListFunc(); for(i=0; i< nump; i++) { if (gdwPlatform == VER_PLATFORM_WIN32_NT) xprintf("%6u %-20s %-30s\n",processlist[i].pid, processlist[i].exename, g_dowindows?processlist[i].title:""); else xprintf("0x%08x %-20s %-30s\n",processlist[i].pid, processlist[i].exename, g_dowindows?processlist[i].title:""); } g_dowindows =0; if (processlist) heap_free(processlist); }
/* * Execute command f, arg list t. * Record error message if not found. * Also do shell scripts here. */ static void texec(Char *sf, Char **st) { struct varent *v; Char *lastsh[2], **vp, *st0, **ost; char *f, **t; int fd; unsigned char c = '\0'; /* The order for the conversions is significant */ t = short2blk(st); f = short2str(sf); Vt = t; errno = 0; /* don't use a previous error */ (void)execve(f, t, environ); Vt = 0; blkfree((Char **)t); switch (errno) { case ENOEXEC: /* * From: [email protected] (Casper H.S. Dik) If we could not execute * it, don't feed it to the shell if it looks like a binary! */ if ((fd = open(f, O_RDONLY)) != -1) { if (read(fd, (char *)&c, 1) == 1) { if (!Isprint(c) && (c != '\n' && c != '\t')) { (void)close(fd); /* * We *know* what ENOEXEC means. */ stderror(ERR_ARCH, f, strerror(errno)); } } #ifdef _PATH_BSHELL else c = '#'; #endif (void)close(fd); } /* * If there is an alias for shell, then put the words of the alias in * front of the argument list replacing the command name. Note no * interpretation of the words at this point. */ v = adrof1(STRshell, &aliases); if (v == 0) { vp = lastsh; vp[0] = adrof(STRshell) ? value(STRshell) : STR_SHELLPATH; vp[1] = NULL; #ifdef _PATH_BSHELL if (fd != -1 && c != '#') vp[0] = STR_BSHELL; #endif } else vp = v->vec; st0 = st[0]; st[0] = sf; ost = st; st = blkspl(vp, st); /* Splice up the new arglst */ ost[0] = st0; sf = *st; /* The order for the conversions is significant */ t = short2blk(st); f = short2str(sf); free(st); Vt = t; (void)execve(f, t, environ); Vt = 0; blkfree((Char **)t); /* FALLTHROUGH */ case ENOMEM: stderror(ERR_SYSTEM, f, strerror(errno)); /* NOTREACHED */ case ENOENT: break; default: if (exerr == 0) { exerr = strerror(errno); if (expath) free(expath); expath = Strsave(sf); Vexpath = expath; } } }
int main(int argc, char *argv[]) { struct sigaction oact; Char *cp; char *tcp, **tempv; const char *ecp; sigset_t nsigset; int f; cshin = stdin; cshout = stdout; csherr = stderr; setprogname(argv[0]); settimes(); /* Immed. estab. timing base */ /* * Initialize non constant strings */ #ifdef _PATH_BSHELL STR_BSHELL = SAVE(_PATH_BSHELL); #endif #ifdef _PATH_CSHELL STR_SHELLPATH = SAVE(_PATH_CSHELL); #endif STR_environ = blk2short(environ); environ = short2blk(STR_environ); /* So that we can free it */ STR_WORD_CHARS = SAVE(WORD_CHARS); HIST = '!'; HISTSUB = '^'; word_chars = STR_WORD_CHARS; tempv = argv; if (eq(str2short(tempv[0]), STRaout)) /* A.out's are quittable */ quitit = 1; uid = getuid(); gid = getgid(); euid = geteuid(); egid = getegid(); /* * We are a login shell if: 1. we were invoked as -<something> and we had * no arguments 2. or we were invoked only with the -l flag */ loginsh = (**tempv == '-' && argc == 1) || (argc == 2 && tempv[1][0] == '-' && tempv[1][1] == 'l' && tempv[1][2] == '\0'); if (loginsh && **tempv != '-') { /* * Mangle the argv space */ tempv[1][0] = '\0'; tempv[1][1] = '\0'; tempv[1] = NULL; for (tcp = *tempv; *tcp++;) continue; for (tcp--; tcp >= *tempv; tcp--) tcp[1] = tcp[0]; *++tcp = '-'; argc--; } if (loginsh) (void)time(&chktim); AsciiOnly = 1; #ifdef NLS (void)setlocale(LC_ALL, ""); { int k; for (k = 0200; k <= 0377 && !Isprint(k); k++) continue; AsciiOnly = k > 0377; } #else AsciiOnly = getenv("LANG") == NULL && getenv("LC_CTYPE") == NULL; #endif /* NLS */ /* * Move the descriptors to safe places. The variable didfds is 0 while we * have only FSH* to work with. When didfds is true, we have 0,1,2 and * prefer to use these. */ initdesc(); /* * XXX: This is to keep programs that use stdio happy. * what we really want is freunopen() .... * Closing cshin cshout and csherr (which are really stdin stdout * and stderr at this point and then reopening them in the same order * gives us again stdin == cshin stdout == cshout and stderr == csherr. * If that was not the case builtins like printf that use stdio * would break. But in any case we could fix that with memcpy and * a bit of pointer manipulation... * Fortunately this is not needed under the current implementation * of stdio. */ (void)fclose(cshin); (void)fclose(cshout); (void)fclose(csherr); if (!(cshin = funopen2((void *) &SHIN, readf, writef, seekf, NULL, closef))) exit(1); if (!(cshout = funopen2((void *) &SHOUT, readf, writef, seekf, NULL, closef))) exit(1); if (!(csherr = funopen2((void *) &SHERR, readf, writef, seekf, NULL, closef))) exit(1); (void)setvbuf(cshin, NULL, _IOLBF, 0); (void)setvbuf(cshout, NULL, _IOLBF, 0); (void)setvbuf(csherr, NULL, _IOLBF, 0); /* * Initialize the shell variables. ARGV and PROMPT are initialized later. * STATUS is also munged in several places. CHILD is munged when * forking/waiting */ set(STRstatus, Strsave(STR0)); if ((ecp = getenv("HOME")) != NULL) cp = quote(SAVE(ecp)); else cp = NULL; if (cp == NULL) fast = 1; /* No home -> can't read scripts */ else set(STRhome, cp); dinit(cp); /* dinit thinks that HOME == cwd in a login * shell */ /* * Grab other useful things from the environment. Should we grab * everything?? */ if ((ecp = getenv("LOGNAME")) != NULL || (ecp = getenv("USER")) != NULL) set(STRuser, quote(SAVE(ecp))); if ((ecp = getenv("TERM")) != NULL) set(STRterm, quote(SAVE(ecp))); /* * Re-initialize path if set in environment */ if ((ecp = getenv("PATH")) == NULL) { #ifdef _PATH_DEFPATH importpath(str2short(_PATH_DEFPATH)); #else setq(STRpath, defaultpath(), &shvhed); #endif } else { importpath(str2short(ecp)); } set(STRshell, Strsave(STR_SHELLPATH)); doldol = putn((int) getpid()); /* For $$ */ shtemp = Strspl(STRtmpsh, doldol); /* For << */ /* * Record the interrupt states from the parent process. If the parent is * non-interruptible our hand must be forced or we (and our children) won't * be either. Our children inherit termination from our parent. We catch it * only if we are the login shell. */ /* parents interruptibility */ (void)sigaction(SIGINT, NULL, &oact); parintr = oact.sa_handler; (void)sigaction(SIGTERM, NULL, &oact); parterm = oact.sa_handler; /* catch these all, login shell or not */ (void)signal(SIGHUP, phup); /* exit processing on HUP */ (void)signal(SIGXCPU, phup); /* ...and on XCPU */ (void)signal(SIGXFSZ, phup); /* ...and on XFSZ */ /* * Process the arguments. * * Note that processing of -v/-x is actually delayed till after script * processing. * * We set the first character of our name to be '-' if we are a shell * running interruptible commands. Many programs which examine ps'es * use this to filter such shells out. */ argc--, tempv++; while (argc > 0 && (tcp = tempv[0])[0] == '-' && *++tcp != '\0' && !batch) { do switch (*tcp++) { case 0: /* - Interruptible, no prompt */ prompt = 0; setintr = 1; nofile = 1; break; case 'b': /* -b Next arg is input file */ batch = 1; break; case 'c': /* -c Command input from arg */ if (argc == 1) xexit(0); argc--, tempv++; arginp = SAVE(tempv[0]); prompt = 0; nofile = 1; break; case 'e': /* -e Exit on any error */ exiterr = 1; break; case 'f': /* -f Fast start */ fast = 1; break; case 'i': /* -i Interactive, even if !intty */ intact = 1; nofile = 1; break; case 'm': /* -m read .cshrc (from su) */ mflag = 1; break; case 'n': /* -n Don't execute */ noexec = 1; break; case 'q': /* -q (Undoc'd) ... die on quit */ quitit = 1; break; case 's': /* -s Read from std input */ nofile = 1; break; case 't': /* -t Read one line from input */ onelflg = 2; prompt = 0; nofile = 1; break; case 'v': /* -v Echo hist expanded input */ nverbose = 1; /* ... later */ break; case 'x': /* -x Echo just before execution */ nexececho = 1; /* ... later */ break; case 'V': /* -V Echo hist expanded input */ setNS(STRverbose); /* NOW! */ break; case 'X': /* -X Echo just before execution */ setNS(STRecho); /* NOW! */ break; } while (*tcp); tempv++, argc--; } if (quitit) /* With all due haste, for debugging */ (void)signal(SIGQUIT, SIG_DFL); /* * Unless prevented by -, -c, -i, -s, or -t, if there are remaining * arguments the first of them is the name of a shell file from which to * read commands. */ if (nofile == 0 && argc > 0) { nofile = open(tempv[0], O_RDONLY); if (nofile < 0) { child = 1; /* So this doesn't return */ stderror(ERR_SYSTEM, tempv[0], strerror(errno)); } ffile = SAVE(tempv[0]); /* * Replace FSHIN. Handle /dev/std{in,out,err} specially * since once they are closed we cannot open them again. * In that case we use our own saved descriptors */ if ((SHIN = dmove(nofile, FSHIN)) < 0) switch(nofile) { case 0: SHIN = FSHIN; break; case 1: SHIN = FSHOUT; break; case 2: SHIN = FSHERR; break; default: stderror(ERR_SYSTEM, tempv[0], strerror(errno)); /* NOTREACHED */ } (void)ioctl(SHIN, FIOCLEX, NULL); prompt = 0; /* argc not used any more */ tempv++; } intty = isatty(SHIN); intty |= intact; if (intty || (intact && isatty(SHOUT))) { if (!batch && (uid != euid || gid != egid)) { errno = EACCES; child = 1; /* So this doesn't return */ stderror(ERR_SYSTEM, "csh", strerror(errno)); } } /* * Decide whether we should play with signals or not. If we are explicitly * told (via -i, or -) or we are a login shell (arg0 starts with -) or the * input and output are both the ttys("csh", or "csh</dev/ttyx>/dev/ttyx") * Note that in only the login shell is it likely that parent may have set * signals to be ignored */ if (loginsh || intact || (intty && isatty(SHOUT))) setintr = 1; settell(); /* * Save the remaining arguments in argv. */ setq(STRargv, blk2short(tempv), &shvhed); /* * Set up the prompt. */ if (prompt) { set(STRprompt, Strsave(uid == 0 ? STRsymhash : STRsymcent)); /* that's a meta-questionmark */ set(STRprompt2, Strsave(STRmquestion)); } /* * If we are an interactive shell, then start fiddling with the signals; * this is a tricky game. */ shpgrp = getpgrp(); opgrp = tpgrp = -1; if (setintr) { **argv = '-'; if (!quitit) /* Wary! */ (void)signal(SIGQUIT, SIG_IGN); (void)signal(SIGINT, pintr); sigemptyset(&nsigset); (void)sigaddset(&nsigset, SIGINT); (void)sigprocmask(SIG_BLOCK, &nsigset, NULL); (void)signal(SIGTERM, SIG_IGN); if (quitit == 0 && arginp == 0) { (void)signal(SIGTSTP, SIG_IGN); (void)signal(SIGTTIN, SIG_IGN); (void)signal(SIGTTOU, SIG_IGN); /* * Wait till in foreground, in case someone stupidly runs csh & * dont want to try to grab away the tty. */ if (isatty(FSHERR)) f = FSHERR; else if (isatty(FSHOUT)) f = FSHOUT; else if (isatty(OLDSTD)) f = OLDSTD; else f = -1; retry: if ((tpgrp = tcgetpgrp(f)) != -1) { if (tpgrp != shpgrp) { sig_t old = signal(SIGTTIN, SIG_DFL); (void)kill(0, SIGTTIN); (void)signal(SIGTTIN, old); goto retry; } opgrp = shpgrp; shpgrp = getpid(); tpgrp = shpgrp; /* * Setpgid will fail if we are a session leader and * mypid == mypgrp (POSIX 4.3.3) */ if (opgrp != shpgrp) if (setpgid(0, shpgrp) == -1) goto notty; /* * We do that after we set our process group, to make sure * that the process group belongs to a process in the same * session as the tty (our process and our group) (POSIX 7.2.4) */ if (tcsetpgrp(f, shpgrp) == -1) goto notty; (void)ioctl(dcopy(f, FSHTTY), FIOCLEX, NULL); } if (tpgrp == -1) { notty: (void)fprintf(csherr, "Warning: no access to tty (%s).\n", strerror(errno)); (void)fprintf(csherr, "Thus no job control in this shell.\n"); } } } if ((setintr == 0) && (parintr == SIG_DFL)) setintr = 1; (void)signal(SIGCHLD, pchild); /* while signals not ready */ /* * Set an exit here in case of an interrupt or error reading the shell * start-up scripts. */ reenter = setexit(); /* PWP */ haderr = 0; /* In case second time through */ if (!fast && reenter == 0) { /* Will have value(STRhome) here because set fast if don't */ { sig_t oparintr; sigset_t osigset; int osetintr; oparintr = parintr; osetintr = setintr; sigemptyset(&nsigset); (void)sigaddset(&nsigset, SIGINT); (void)sigprocmask(SIG_BLOCK, &nsigset, &osigset); setintr = 0; parintr = SIG_IGN; /* Disable onintr */ #ifdef _PATH_DOTCSHRC (void)srcfile(_PATH_DOTCSHRC, 0, 0); #endif if (!fast && !arginp && !onelflg) dohash(NULL, NULL); #ifdef _PATH_DOTLOGIN if (loginsh) (void)srcfile(_PATH_DOTLOGIN, 0, 0); #endif (void)sigprocmask(SIG_SETMASK, &osigset, NULL); setintr = osetintr; parintr = oparintr; } (void)srccat(value(STRhome), STRsldotcshrc); if (!fast && !arginp && !onelflg && !havhash) dohash(NULL, NULL); /* * Source history before .login so that it is available in .login */ if ((cp = value(STRhistfile)) != STRNULL) tildehist[2] = cp; dosource(tildehist, NULL); if (loginsh) (void)srccat(value(STRhome), STRsldotlogin); } /* * Now are ready for the -v and -x flags */ if (nverbose) setNS(STRverbose); if (nexececho) setNS(STRecho); /* * All the rest of the world is inside this call. The argument to process * indicates whether it should catch "error unwinds". Thus if we are a * interactive shell our call here will never return by being blown past on * an error. */ process(setintr); /* * Mop-up. */ if (intty) { if (loginsh) { (void)fprintf(cshout, "logout\n"); (void)close(SHIN); child = 1; goodbye(); } else { (void)fprintf(cshout, "exit\n"); } } rechist(); exitstat(); /* NOTREACHED */ }
The default action is to shutdown without a reboot.\n\"now\" must be \ specified to actually shutdown or reboot\n"}; void doshutdown(Char **vc, struct command *c) { unsigned int flags = 0; unsigned char reboot,shutdown,logoff,shutdown_ok; char **v; char *ptr; char errbuf[128]; int k; HANDLE hToken; TOKEN_PRIVILEGES tp,tpPrevious; LUID luid; DWORD cbPrevious = sizeof(TOKEN_PRIVILEGES); if (gdwPlatform != VER_PLATFORM_WIN32_NT) { stderror(ERR_SYSTEM,"shutdown","Sorry,not supported on win95"); } shutdown_ok = reboot = shutdown = logoff = 0; gflag = 0; tglob(vc); if (gflag) { vc = globall(vc); if (vc == 0) stderror(ERR_NAME | ERR_NOMATCH); } else vc = gargv = saveblk(vc); trim(vc); v = short2blk(vc); for (k = 0; v[k] != NULL ; k++){ if ( v[k][0] == '-' ) { ptr = v[k]; ptr++; while( ptr && *ptr) { if (*ptr == 'f') flags |= EWX_FORCE; if (*ptr == 'r') reboot =1; else if (*ptr == 'l') logoff =1; else { blkfree((Char **)v); stderror(ERR_SYSTEM,"Usage",shutdown_usage); } ptr++; } } else if (!lstrcmpi(v[k],"now")) { shutdown_ok = 1; } } if (k == 0) { blkfree((Char**)v); stderror(ERR_SYSTEM,"Usage",shutdown_usage); } if (!reboot && !logoff){ flags |= EWX_SHUTDOWN; shutdown = 1; } if (reboot && logoff ) { blkfree((Char **)v); stderror(ERR_SYSTEM,"Usage",shutdown_usage); } if (reboot) flags |= EWX_REBOOT; if (logoff) flags |= EWX_LOGOFF; if ((reboot || shutdown) && (!shutdown_ok) ) { blkfree((Char **)v); stderror(ERR_SYSTEM,"shutdown","Specify \"now\" to really shutdown"); } if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES| TOKEN_QUERY, &hToken) ){ make_err_str(GetLastError(),errbuf,128); blkfree((Char **)v); stderror(ERR_SYSTEM,"shutdown failed",errbuf); } if (!LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid)) { make_err_str(GetLastError(),errbuf,128); blkfree((Char **)v); stderror(ERR_SYSTEM,"shutdown failed",errbuf); } tp.PrivilegeCount = 1; tp.Privileges[0].Luid = luid; tp.Privileges[0].Attributes = 0; if (!AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(tp),&tpPrevious, &cbPrevious)){ make_err_str(GetLastError(),errbuf,128); blkfree((Char **)v); stderror(ERR_SYSTEM,"shutdown failed",errbuf); } tpPrevious.PrivilegeCount = 1; tpPrevious.Privileges[0].Luid = luid; tpPrevious.Privileges[0].Attributes |= SE_PRIVILEGE_ENABLED; if (!AdjustTokenPrivileges(hToken,FALSE,&tpPrevious,cbPrevious,NULL, NULL)){ make_err_str(GetLastError(),errbuf,128); blkfree((Char **)v); stderror(ERR_SYSTEM,"shutdown failed",errbuf); } if ( !ExitWindowsEx(flags,0) ) { make_err_str(GetLastError(),errbuf,128); blkfree((Char **)v); stderror(ERR_SYSTEM,"shutdown failed",errbuf); } }
int nt_try_fast_exec(struct command *t) { register Char **pv, **av; register Char *dp,*sav; register char **tt; register char *f; register struct varent *v; register int hashval,i; register int slash; int rc = 0, gflag; Char *vp; Char *blk[2]; vp = varval(STRNTslowexec); if (vp != STRNULL) return 1; blk[0] = t->t_dcom[0]; blk[1] = 0; // don't do backtick if(Strchr(t->t_dcom[0],'`') ) return 1; gflag = tglob(blk); if (gflag) { pv = globall(blk, gflag); if (pv == 0) { return 1; } } else pv = saveblk(blk); trim(pv); epath = Strsave(pv[0]); v = adrof(STRpath); if (v == 0 && epath[0] != '/' && epath[0] != '.') { blkfree(pv); return 1; } slash = any(short2str(epath),'/'); /* * Glob the argument list, if necessary. Otherwise trim off the quote bits. */ av = &t->t_dcom[1]; gflag = tglob(av); if (gflag) { av = globall(av, gflag);/*FIXRESET*/ if (av == 0) { blkfree(pv); return 1; } } else av = saveblk(av); blkfree(t->t_dcom); t->t_dcom = blkspl(pv, av); xfree((ptr_t) pv); xfree((ptr_t) av); av = t->t_dcom; //trim(av); if (*av == NULL || **av == '\0') return 1; xechoit(av);/*FIXRESET*/ /* Echo command if -x */ if (v == 0 || v->vec[0] == 0 || slash) pv = abspath; else pv = v->vec; sav = Strspl(STRslash,*av); hashval = hashval_extern(*av); i = 0; do { #pragma warning(disable:4310) if (!slash && ABSOLUTEP(pv[0]) && havhash) { #pragma warning(default:4310) if (!bit_extern(hashval,i)){ pv++;i++; continue; } } if (pv[0][0] == 0 || eq(pv[0],STRdot)) { tt = short2blk(av); f = short2str(*av); rc = nt_texec(f, tt); blkfree((Char**)tt); if (!rc) break; } else { dp = Strspl(*pv,sav); tt = short2blk(av); f = short2str(dp); rc = nt_texec(f, tt); blkfree((Char**)tt); xfree((ptr_t)dp); if (!rc) break; } pv++; i++; }while(*pv); return rc; }
void dostart(Char ** vc, struct command *c) { char *cmdstr,*cmdend,*ptr; char argv0[256];/*FIXBUF*/ DWORD cmdsize; char *currdir=NULL; char *savepath; char **v = NULL; STARTUPINFO si; PROCESS_INFORMATION pi; DWORD dwCreationFlags=CREATE_NEW_CONSOLE; DWORD k,cmdlen,j,jj,ret; UNREFERENCED_PARAMETER(c); vc++; cmdsize = 512; cmdstr = heap_alloc(cmdsize); cmdend = cmdstr; cmdlen = 0; memset(&si,0,sizeof(si)); si.cb = sizeof(si); vc = glob_all_or_error(vc); v = short2blk(vc); if(v == NULL) { stderror(ERR_NOMEM); return; } blkfree(vc); for (k = 0; v[k] != NULL ; k++){ if ( v[k][0] == '-' ) { /* various options */ if( (v[k][1] == 'T') || (v[k][1] == 't')) si.lpTitle =&( v[k][2]); else if ( (v[k][1] == 'D') || (v[k][1] == 'd')) currdir =&( v[k][2]); else if (!_stricmp(&v[k][1],"MIN") ) si.wShowWindow = SW_SHOWMINIMIZED; else if (!_stricmp(&v[k][1],"MAX") ) si.wShowWindow = SW_SHOWMAXIMIZED; else if (!_stricmp(&v[k][1],"SEPARATE") ) dwCreationFlags |= CREATE_SEPARATE_WOW_VDM; else if (!_stricmp(&v[k][1],"SHARED") ) dwCreationFlags |= CREATE_SHARED_WOW_VDM; else if (!_stricmp(&v[k][1],"LOW") ) dwCreationFlags |= IDLE_PRIORITY_CLASS; else if (!_stricmp(&v[k][1],"NORMAL") ) dwCreationFlags |= NORMAL_PRIORITY_CLASS; else if (!_stricmp(&v[k][1],"HIGH") ) dwCreationFlags |= HIGH_PRIORITY_CLASS; else if (!_stricmp(&v[k][1],"REALTIME") ) dwCreationFlags |= REALTIME_PRIORITY_CLASS; else{ blkfree((Char **)v); stderror(ERR_SYSTEM,start_usage,"See CMD.EXE for more info");/*FIXRESET*/ } } else{ // non-option arg break; } } /* * Stop the insanity of requiring start "tcsh -l" * Option processing now stops at first non-option arg * -amol 5/30/96 */ for (jj=k;v[jj] != NULL; jj++) { j=(lstrlen(v[jj]) + 2); if (j + cmdlen > cmdsize) { ptr = cmdstr; cmdstr = heap_realloc(cmdstr, max(cmdsize << 1, j+cmdlen) ); if(!cmdstr) { heap_free(ptr); stderror(ERR_NOMEM,"start");/*FIXRESET*/ } cmdend = cmdstr + (cmdend - ptr); cmdsize <<= 1; } ptr = v[jj]; while (*ptr) { *cmdend++ = *ptr++; cmdlen++; } *cmdend++ = ' '; cmdlen++; } if (jj == k) { blkfree((Char **)v); stderror(ERR_SYSTEM,start_usage,"See CMD.EXE for more info");/*FIXRESET*/ return; } *cmdend = 0; StringCbCopy(argv0,sizeof(argv0),v[k]); /* * strictly speaking, it should do no harm to set the path * back to '\'-delimited even in the parent, but in the * interest of consistency, we save the old value and restore it * later */ savepath = fix_path_for_child(); if (! CreateProcess(NULL, cmdstr, NULL, NULL, FALSE, dwCreationFlags, NULL, currdir, &si, &pi) ) { restore_path(savepath); ret = GetLastError(); if (ret == ERROR_BAD_EXE_FORMAT || ret == ERROR_ACCESS_DENIED || (ret == ERROR_FILE_NOT_FOUND && (is_url(v[k]) || is_directory(v[k])) ) ) { char erbuf[MAX_PATH]; errno = ENOEXEC; try_shell_ex(&v[k],0,FALSE); heap_free(cmdstr); /* free !! */ if (errno) { strerror_s(erbuf,sizeof(erbuf),errno); stderror(ERR_ARCH,argv0,erbuf);/*FIXRESET*/ } } else if (ret == ERROR_INVALID_PARAMETER) { errno = ENAMETOOLONG; heap_free(cmdstr); /* free !! */ stderror(ERR_TOOLARGE,argv0);/*FIXRESET*/ } else { errno = ENOENT; if ( ( (v[k][0] == '\\') ||(v[k][0] == '/') ) && ( (v[k][1] == '\\') ||(v[k][1] == '/') ) && (!v[k+1]) ) try_shell_ex(&v[k],0,FALSE); heap_free(cmdstr); /* free !! */ if (errno) { stderror(ERR_NOTFOUND,argv0);/*FIXRESET*/ } } } else { CloseHandle(pi.hProcess); CloseHandle(pi.hThread); heap_free(cmdstr); restore_path(savepath); } blkfree((Char **)v); return; }
int fork(void) { size_t rc; size_t stacksize; char modname[512];/*FIXBUF*/ HANDLE hProc,hThread, hArray[2]; STARTUPINFO si; PROCESS_INFORMATION pi; SECURITY_ATTRIBUTES sa; DWORD dwCreationflags; unsigned int priority; HANDLE h64Parent,h64Child; #ifndef _M_ALPHA unsigned long fork_stack_end; #endif _M_ALPHA __fork_stack_begin =GETSTACKBASE(); #ifndef _M_ALPHA __fork_stack_end = &fork_stack_end; #else __fork_stack_end = (unsigned long *)__asm("mov $sp, $0"); #endif /*_M_ALPHA*/ h64Parent = h64Child = NULL; // // Create two inheritable events // sa.nLength = sizeof(sa); sa.lpSecurityDescriptor =0; sa.bInheritHandle = TRUE; if (!__hforkchild) __hforkchild = CreateEvent(&sa,TRUE,FALSE,NULL); if (!__hforkparent) __hforkparent = CreateEvent(&sa,TRUE,FALSE,NULL); rc = setjmp(__fork_context); if (rc) { // child #ifdef _M_IX86 // // Restore old registration // -amol 2/2/97 GETEXCEPTIONREGIST() = (struct _EXCEPTION_REGISTRATION_RECORD*)_old_exr; #endif // _M_ALPHA SetEvent(__hforkchild); dprintf("Child ready to rumble\n"); if(WaitForSingleObject(__hforkparent,FORK_TIMEOUT) != WAIT_OBJECT_0) ExitProcess(0xFFFF); CloseHandle(__hforkchild); CloseHandle(__hforkparent); __hforkchild = __hforkparent=0; //__asm { int 3}; restore_fds(); STR_environ = blk2short(environ); environ = short2blk(STR_environ); /* So that we can free it */ return 0; } copy_fds(); memset(&si,0,sizeof(si)); si.cb= sizeof(si); /* * This f!@#!@% function returns the old value even if the std handles * have been closed. * Skip this step, since we know tcsh will do the right thing later. * si.hStdInput= GetStdHandle(STD_INPUT_HANDLE); si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); si.hStdError = GetStdHandle(STD_ERROR_HANDLE); */ if (!GetModuleFileName(GetModuleHandle(NULL),modname,512) ) { rc = GetLastError(); return -1; } dwCreationflags = GetPriorityClass(GetCurrentProcess()); priority = GetThreadPriority(GetCurrentThread()); rc = CreateProcess(NULL, modname, NULL, NULL, TRUE, CREATE_SUSPENDED | dwCreationflags, NULL, NULL, &si, &pi); if (!rc) { rc = GetLastError(); return -1; } ResetEvent(__hforkchild); ResetEvent(__hforkparent); hProc = pi.hProcess; hThread = pi.hThread; __forked=1; /* * Usage of events in the wow64 case: * * h64Parent : initially non-signalled * h64Child : initially non-signalled * * 1. Create the events, resume the child thread. * 2. Child opens h64Parent to see if it is a child process in wow64 * 3. Child opens and sets h64Child to tell parent it's running. (This * step is needed because we can't copy to a process created in the * suspended state on wow64.) * 4. Copy gForkData and then set h64Parent. This tells the child * that the parameters in the structure are trustworthy. * 5. Wait for h64Child so that we know the child has created the stack * in dynamic memory. * * The rest of the fork hack should now proceed as in x86 * */ if (bIsWow64Process) { // allocate the heap for the child. this can be done even when // the child is suspended. // avoids inexplicable allocation failures in the child. if (VirtualAllocEx(hProc, __heap_base, __heap_size, MEM_RESERVE, PAGE_READWRITE) == NULL) { dprintf("virtual allocex failed %d\n",GetLastError()); goto error; } if (VirtualAllocEx(hProc, __heap_base, __heap_size, MEM_COMMIT, PAGE_READWRITE) == NULL) { dprintf("virtual allocex2 failed %d\n",GetLastError()); goto error; } // Do NOT expect existing events if (!CreateWow64Events(pi.dwProcessId,&h64Parent,&h64Child,FALSE)) { goto error; } ResumeThread(hThread); // wait for the child to tell us it is running //if (WaitForSingleObject(h64Child,FORK_TIMEOUT) != WAIT_OBJECT_0) { // rc = GetLastError(); // goto error; //} hArray[0] = h64Child; hArray[1] = hProc; if (WaitForMultipleObjects(2,hArray,FALSE,FORK_TIMEOUT) != WAIT_OBJECT_0){ rc = GetLastError(); goto error; } } // // Copy all the shared data // if (!WriteProcessMemory(hProc,&gForkData,&gForkData, sizeof(ForkData),&rc)) { goto error; } if (rc != sizeof(ForkData)) goto error; if (!bIsWow64Process) { rc = ResumeThread(hThread); } // in the wow64 case, the child will be waiting on h64parent again. // set it, and then wait for h64child. This will mean the child has // a stack set up at the right location. else { SetEvent(h64Parent); hArray[0] = h64Child; hArray[1] = hProc; if (WaitForMultipleObjects(2,hArray,FALSE,FORK_TIMEOUT) != WAIT_OBJECT_0){ rc = GetLastError(); goto error; } CloseHandle(h64Parent); CloseHandle(h64Child); h64Parent = h64Child = NULL; } // // Wait for the child to start and init itself. // The timeout is so that we don't wait too long // hArray[0] = __hforkchild; hArray[1] = hProc; if (WaitForMultipleObjects(2,hArray,FALSE,FORK_TIMEOUT) != WAIT_OBJECT_0){ int err = GetLastError(); // For debugging purposes dprintf("wait failed err %d\n",err); goto error; } // Stop the child again and copy the stack and heap // SuspendThread(hThread); if (!SetThreadPriority(hThread,priority) ) { priority =GetLastError(); } // stack stacksize = (char*)__fork_stack_begin - (char*)__fork_stack_end; if (!WriteProcessMemory(hProc,(char *)__fork_stack_end, (char *)__fork_stack_end, (u_long)stacksize, &rc)){ goto error; } // // copy heap itself if (!WriteProcessMemory(hProc, (void*)__heap_base,(void*)__heap_base, (DWORD)((char*)__heap_top-(char*)__heap_base), &rc)){ goto error; } rc = fork_copy_user_mem(hProc); if(rc) { goto error; } // Release the child. SetEvent(__hforkparent); rc = ResumeThread(hThread); __forked=0; dprintf("forked process %d\n",pi.dwProcessId); start_sigchild_thread(hProc,pi.dwProcessId); close_copied_fds(); CloseHandle(hThread); // // return process id to parent. return pi.dwProcessId; error: __forked=0; SetEvent(__hforkparent); ResumeThread(hThread); CloseHandle(hProc); CloseHandle(hThread); if (h64Parent) { SetEvent(h64Parent); // don't let child block forever CloseHandle(h64Parent); } if (h64Child) CloseHandle(h64Child); return -1; }