⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spawn.cc

📁 cygwin, 著名的在win32下模拟unix操作系统的东东
💻 CC
📖 第 1 页 / 共 2 页
字号:
/* spawn.cc   Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.This file is part of Cygwin.This software is a copyrighted work licensed under the terms of theCygwin license.  Please consult the file "CYGWIN_LICENSE" fordetails. */#include "winsup.h"#include <stdlib.h>#include <stdarg.h>#include <unistd.h>#include <process.h>#include <sys/wait.h>#include <errno.h>#include <limits.h>#include <wingdi.h>#include <winuser.h>#include <ctype.h>#include "cygerrno.h"#include <sys/cygwin.h>#include "security.h"#include "fhandler.h"#include "path.h"#include "dtable.h"#include "sigproc.h"#include "cygheap.h"#include "child_info.h"#include "shared_info.h"#include "pinfo.h"#define NEED_VFORK#include "perthread.h"#include "registry.h"#include "environ.h"#include "cygthread.h"#define LINE_BUF_CHUNK (MAX_PATH * 2)static suffix_info std_suffixes[] ={  suffix_info (".exe", 1), suffix_info ("", 1),  suffix_info (".com"), suffix_info (".cmd"),  suffix_info (".bat"), suffix_info (".dll"),  suffix_info (NULL)};HANDLE hExeced;DWORD dwExeced;/* Add .exe to PROG if not already present and see if that exists.   If not, return PROG (converted from posix to win32 rules if necessary).   The result is always BUF.   Returns (possibly NULL) suffix */static const char *perhaps_suffix (const char *prog, path_conv& buf){  char *ext;  debug_printf ("prog '%s'", prog);  buf.check (prog, PC_SYM_FOLLOW | PC_FULL, std_suffixes);  if (!buf.exists () || buf.isdir ())    ext = NULL;  else if (buf.known_suffix)    ext = (char *) buf + (buf.known_suffix - buf.get_win32 ());  else    ext = strchr (buf, '\0');  debug_printf ("buf %s, suffix found '%s'", (char *) buf, ext);  return ext;}/* Find an executable name, possibly by appending known executable   suffixes to it.  The win32-translated name is placed in 'buf'.   Any found suffix is returned in known_suffix.   If the file is not found and !null_if_not_found then the win32 version   of name is placed in buf and returned.  Otherwise the contents of buf   is undefined and NULL is returned.  */const char * __stdcallfind_exec (const char *name, path_conv& buf, const char *mywinenv,	   unsigned opt, const char **known_suffix){  const char *suffix = "";  debug_printf ("find_exec (%s)", name);  const char *retval = buf;  char tmp[MAX_PATH];  const char *posix = (opt & FE_NATIVE) ? NULL : name;  bool has_slash = strchr (name, '/');  /* Check to see if file can be opened as is first.     Win32 systems always check . first, but PATH may not be set up to     do this. */  if ((has_slash || opt & FE_CWD)      && (suffix = perhaps_suffix (name, buf)) != NULL)    {      if (posix && !has_slash)	{	  tmp[0] = '.';	  tmp[1] = '/';	  strcpy (tmp + 2, name);	  posix = tmp;	}      goto out;    }  win_env *winpath;  const char *path;  const char *posix_path;  /* Return the error condition if this is an absolute path or if there     is no PATH to search. */  if (strchr (name, '/') || strchr (name, '\\') ||      isdrive (name) ||      !(winpath = getwinenv (mywinenv)) ||      !(path = winpath->get_native ()) ||      *path == '\0')    goto errout;  debug_printf ("%s%s", mywinenv, path);  posix = (opt & FE_NATIVE) ? NULL : tmp;  posix_path = winpath->get_posix () - 1;  /* Iterate over the specified path, looking for the file with and     without executable extensions. */  do    {      posix_path++;      char *eotmp = strccpy (tmp, &path, ';');      /* An empty path or '.' means the current directory, but we've	 already tried that.  */      if (opt & FE_CWD && (tmp[0] == '\0' || (tmp[0] == '.' && tmp[1] == '\0')))	continue;      *eotmp++ = '\\';      strcpy (eotmp, name);      debug_printf ("trying %s", tmp);      if ((suffix = perhaps_suffix (tmp, buf)) != NULL)	{	  if (posix == tmp)	    {	      eotmp = strccpy (tmp, &posix_path, ':');	      if (eotmp == tmp)		*eotmp++ = '.';	      *eotmp++ = '/';	      strcpy (eotmp, name);	    }	  goto out;	}    }  while (*path && *++path && (posix_path = strchr (posix_path, ':'))); errout:  posix = NULL;  /* Couldn't find anything in the given path.     Take the appropriate action based on null_if_not_found. */  if (opt & FE_NNF)    retval = NULL;  else if (opt & FE_NATIVE)    buf.check (name);  else    retval = name; out:  if (posix)    buf.set_path (posix);  debug_printf ("%s = find_exec (%s)", (char *) buf, name);  if (known_suffix)    *known_suffix = suffix ?: strchr (buf, '\0');  return retval;}/* Utility for spawn_guts.  */static HANDLEhandle (int n, int direction){  fhandler_base *fh = cygheap->fdtab[n];  if (!fh)    return INVALID_HANDLE_VALUE;  if (fh->get_close_on_exec ())    return INVALID_HANDLE_VALUE;  if (direction == 0)    return fh->get_handle ();  return fh->get_output_handle ();}intiscmd (const char *argv0, const char *what){  int n;  n = strlen (argv0) - strlen (what);  if (n >= 2 && argv0[1] != ':')    return 0;  return n >= 0 && strcasematch (argv0 + n, what) &&	 (n == 0 || isdirsep (argv0[n - 1]));}class linebuf{ public:  size_t ix;  char *buf;  size_t alloced;  linebuf () : ix (0), buf (NULL), alloced (0) {}  ~linebuf () {/* if (buf) free (buf);*/}  void add (const char *what, int len);  void add (const char *what) {add (what, strlen (what));}  void prepend (const char *what, int len);};voidlinebuf::add (const char *what, int len){  size_t newix;  if ((newix = ix + len) >= alloced || !buf)    {      alloced += LINE_BUF_CHUNK + newix;      buf = (char *) realloc (buf, alloced + 1);    }  memcpy (buf + ix, what, len);  ix = newix;  buf[ix] = '\0';}voidlinebuf::prepend (const char *what, int len){  int buflen;  size_t newix;  if ((newix = ix + len) >= alloced)    {      alloced += LINE_BUF_CHUNK + newix;      buf = (char *) realloc (buf, alloced + 1);      buf[ix] = '\0';    }  if ((buflen = strlen (buf)))      memmove (buf + len, buf, buflen + 1);  else      buf[newix] = '\0';  memcpy (buf, what, len);  ix = newix;}class av{  char **argv;  int calloced; public:  int error;  int argc;  av (int ac, const char * const *av) : calloced (0), error (false), argc (ac)  {    argv = (char **) cmalloc (HEAP_1_ARGV, (argc + 5) * sizeof (char *));    memcpy (argv, av, (argc + 1) * sizeof (char *));  }  ~av ()  {    if (argv)      {	for (int i = 0; i < calloced; i++)	  if (argv[i])	    cfree (argv[i]);	cfree (argv);      }  }  int unshift (const char *what, int conv = 0);  operator char **() {return argv;}  void all_calloced () {calloced = argc;}  void replace0_maybe (const char *arg0)  {    /* Note: Assumes that argv array has not yet been "unshifted" */    if (!calloced	&& (argv[0] = cstrdup1 (arg0)))      calloced = true;    else      error = errno;  }  void dup_maybe (int i)  {    if (i >= calloced	&& !(argv[i] = cstrdup1 (argv[i])))      error = errno;  }  void dup_all ()  {    for (int i = calloced; i < argc; i++)      if (!(argv[i] = cstrdup1 (argv[i])))	error = errno;  }};intav::unshift (const char *what, int conv){  char **av;  av = (char **) crealloc (argv, (argc + 2) * sizeof (char *));  if (!av)    return 0;  argv = av;  memmove (argv + 1, argv, (argc + 1) * sizeof (char *));  char buf[MAX_PATH + 1];  if (conv)    {      cygwin_conv_to_posix_path (what, buf);      char *p = strchr (buf, '\0') - 4;      if (p > buf && strcasematch (p, ".exe"))	*p = '\0';      what = buf;    }  if (!(*argv = cstrdup1 (what)))    error = errno;  argc++;  calloced++;  return 1;}static int __stdcallspawn_guts (const char * prog_arg, const char *const *argv,	    const char *const envp[], int mode){  BOOL rc;  pid_t cygpid;  sigframe thisframe (mainthread);  MALLOC_CHECK;  if (prog_arg == NULL)    {      syscall_printf ("prog_arg is NULL");      set_errno (EINVAL);      return -1;    }  syscall_printf ("spawn_guts (%d, %.132s)", mode, prog_arg);  if (argv == NULL)    {      syscall_printf ("argv is NULL");      set_errno (EINVAL);      return -1;    }  path_conv real_path;  linebuf one_line;  STARTUPINFO si = {0, NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL};  child_info_spawn ciresrv;  si.lpReserved2 = (LPBYTE) &ciresrv;  si.cbReserved2 = sizeof (ciresrv);  DWORD chtype;  if (mode != _P_OVERLAY)    chtype = PROC_SPAWN;  else    chtype = PROC_EXEC;  HANDLE subproc_ready;  if (chtype != PROC_EXEC)    subproc_ready = NULL;  else    {      subproc_ready = CreateEvent (&sec_all, TRUE, FALSE, NULL);      ProtectHandleINH (subproc_ready);    }  init_child_info (chtype, &ciresrv, (mode == _P_OVERLAY) ? myself->pid : 1,		   subproc_ready);  if (!DuplicateHandle (hMainProc, hMainProc, hMainProc, &ciresrv.parent, 0, 1,			DUPLICATE_SAME_ACCESS))     {       system_printf ("couldn't create handle to myself for child, %E");       return -1;     }  ciresrv.moreinfo = (cygheap_exec_info *) ccalloc (HEAP_1_EXEC, 1, sizeof (cygheap_exec_info));  ciresrv.moreinfo->old_title = NULL;  /* CreateProcess takes one long string that is the command line (sigh).     We need to quote any argument that has whitespace or embedded "'s.  */  int ac;  for (ac = 0; argv[ac]; ac++)    /* nothing */;  av newargv (ac, argv);  int null_app_name = 0;  if (ac == 3 && argv[1][0] == '/' && argv[1][1] == 'c' &&      (iscmd (argv[0], "command.com") || iscmd (argv[0], "cmd.exe")))    {      real_path.check (prog_arg);      one_line.add ("\"");      if (!real_path.error)	one_line.add (real_path);      else	one_line.add (argv[0]);      one_line.add ("\"");      one_line.add (" ");      one_line.add (argv[1]);      one_line.add (" ");      one_line.add (argv[2]);      strcpy (real_path, argv[0]);      null_app_name = 1;      goto skip_arg_parsing;    }  const char *ext;  if ((ext = perhaps_suffix (prog_arg, real_path)) == NULL)    {      set_errno (ENOENT);      return -1;    }  MALLOC_CHECK;  /* If the file name ends in either .exe, .com, .bat, or .cmd we assume     that it is NOT a script file */  while (*ext == '\0')    {      HANDLE hnd = CreateFile (real_path, GENERIC_READ,			       FILE_SHARE_READ | FILE_SHARE_WRITE,			       &sec_none_nih, OPEN_EXISTING,			       FILE_ATTRIBUTE_NORMAL, 0);      if (hnd == INVALID_HANDLE_VALUE)	{	  __seterrno ();	  return -1;	}      DWORD done;      char buf[2 * MAX_PATH + 1];      buf[0] = buf[1] = buf[2] = buf[sizeof (buf) - 1] = '\0';      if (!ReadFile (hnd, buf, sizeof (buf) - 1, &done, 0))	{	  CloseHandle (hnd);	  __seterrno ();	  return -1;	}      CloseHandle (hnd);      if (buf[0] == 'M' && buf[1] == 'Z')	break;      debug_printf ("%s is a script", (char *) real_path);      char *pgm, *arg1;      if (buf[0] != '#' || buf[1] != '!')	{	  pgm = (char *) "/bin/sh";	  arg1 = NULL;	}      else	{	  char *ptr;	  pgm = buf + 2;	  pgm += strspn (pgm, " \t");	  for (ptr = pgm, arg1 = NULL;	       *ptr && *ptr != '\r' && *ptr != '\n';	       ptr++)	    if (!arg1 && (*ptr == ' ' || *ptr == '\t'))	      {		/* Null terminate the initial command and step over		   any additional white space.  If we've hit the		   end of the line, exit the loop.  Otherwise, we've		   found the first argument. Position the current		   pointer on the last known white space. */		*ptr = '\0';		char *newptr = ptr + 1;		newptr += strspn (newptr, " \t");		if (!*newptr || *newptr == '\r' || *newptr == '\n')		  break;		arg1 = newptr;		ptr = newptr - 1;	      }	  *ptr = '\0';	}      /* Replace argv[0] with the full path to the script if this is the	 first time through the loop. */      newargv.replace0_maybe (prog_arg);      /* pointers:       * pgm	interpreter name       * arg1	optional string       */      if (arg1)	newargv.unshift (arg1);      /* FIXME: This should not be using FE_NATIVE.  It should be putting	 the posix path on the argv list. */      find_exec (pgm, real_path, "PATH=", FE_NATIVE, &ext);      newargv.unshift (real_path, 1);    }  if (real_path.iscygexec ())    newargv.dup_all ();  else    {      for (int i = 0; i < newargv.argc; i++)	{	  char *p = NULL;	  const char *a;	  newargv.dup_maybe (i);	  a = i ? newargv[i] : (char *) real_path;	  int len = strlen (a);	  if (len != 0 && !strpbrk (a, " \t\n\r\""))	    one_line.add (a, len);	  else	    {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -