Changeset 3140 for trunk/src/kmk/w32
- Timestamp:
- Mar 14, 2018, 10:28:10 PM (7 years ago)
- Location:
- trunk/src/kmk
- Files:
-
- 1 deleted
- 14 edited
- 3 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk
-
Property svn:mergeinfo
set to
/vendor/gnumake/current merged eligible
-
Property svn:mergeinfo
set to
-
trunk/src/kmk/w32/Makefile.am
r2591 r3140 1 1 # Makefile.am to create libw32.a for mingw32 host. 2 # Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 3 # 2007, 2008, 2009, 2010 Free Software Foundation, Inc. 2 # Copyright (C) 1997-2016 Free Software Foundation, Inc. 4 3 # This file is part of GNU Make. 5 4 # … … 17 16 # this program. If not, see <http://www.gnu.org/licenses/>. 18 17 18 AUTOMAKE_OPTIONS = subdir-objects 19 19 20 noinst_LIBRARIES = libw32.a 20 21 21 22 libw32_a_SOURCES = subproc/misc.c subproc/sub_proc.c subproc/w32err.c \ 22 pathstuff.c23 compat/posixfcn.c pathstuff.c w32os.c 23 24 24 libw32_a_CPPFLAGS = -I$(srcdir)/include -I$(srcdir)/subproc -I$(top_srcdir) 25 libw32_a_CPPFLAGS = -I$(srcdir)/include -I$(srcdir)/subproc -I$(top_srcdir) \ 26 -I$(top_srcdir)/glob -
trunk/src/kmk/w32/compat/dirent.c
r2591 r3140 1 1 /* Directory entry code for Window platforms. 2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. 2 Copyright (C) 1996-2016 Free Software Foundation, Inc. 4 3 This file is part of GNU Make. 5 4 … … 32 31 opendir(const char* pDirName) 33 32 { 34 35 DIR*pDir;36 char*pEndDirName;37 intnBufferLen;38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 33 struct stat sb; 34 DIR* pDir; 35 char* pEndDirName; 36 int nBufferLen; 37 38 /* sanity checks */ 39 if (!pDirName) { 40 errno = EINVAL; 41 return NULL; 42 } 43 if (stat(pDirName, &sb) != 0) { 44 errno = ENOENT; 45 return NULL; 46 } 47 if ((sb.st_mode & S_IFMT) != S_IFDIR) { 48 errno = ENOTDIR; 49 return NULL; 50 } 51 52 /* allocate a DIR structure to return */ 53 pDir = (DIR *) malloc(sizeof (DIR)); 54 55 if (!pDir) 56 return NULL; 57 58 /* input directory name length */ 59 nBufferLen = strlen(pDirName); 60 61 /* copy input directory name to DIR buffer */ 62 strcpy(pDir->dir_pDirectoryName, pDirName); 63 64 /* point to end of the copied directory name */ 65 pEndDirName = &pDir->dir_pDirectoryName[nBufferLen - 1]; 66 67 /* if directory name did not end in '/' or '\', add '/' */ 68 if ((*pEndDirName != '/') && (*pEndDirName != '\\')) { 69 pEndDirName++; 70 *pEndDirName = '/'; 71 } 72 73 /* now append the wildcard character to the buffer */ 74 pEndDirName++; 75 *pEndDirName = '*'; 76 pEndDirName++; 77 *pEndDirName = '\0'; 78 79 /* other values defaulted */ 80 pDir->dir_nNumFiles = 0; 81 pDir->dir_hDirHandle = INVALID_HANDLE_VALUE; 82 pDir->dir_ulCookie = __DIRENT_COOKIE; 84 83 85 84 #ifdef KMK_PRF 86 85 fprintf(stderr, "opendir(%s) -> %p\n", pDirName, pDir); 87 86 #endif 88 87 return pDir; 89 88 } 90 89 … … 92 91 closedir(DIR *pDir) 93 92 { 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 93 /* got a valid pointer? */ 94 if (!pDir) { 95 errno = EINVAL; 96 return; 97 } 98 99 /* sanity check that this is a DIR pointer */ 100 if (pDir->dir_ulCookie != __DIRENT_COOKIE) { 101 errno = EINVAL; 102 return; 103 } 104 105 /* close the WINDOWS32 directory handle */ 106 if (pDir->dir_hDirHandle != INVALID_HANDLE_VALUE) 107 FindClose(pDir->dir_hDirHandle); 108 109 free(pDir); 110 111 return; 113 112 } 114 113 … … 116 115 readdir(DIR* pDir) 117 116 { 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 117 WIN32_FIND_DATA wfdFindData; 118 119 if (!pDir) { 120 errno = EINVAL; 121 return NULL; 122 } 123 124 /* sanity check that this is a DIR pointer */ 125 if (pDir->dir_ulCookie != __DIRENT_COOKIE) { 126 errno = EINVAL; 127 return NULL; 128 } 129 130 if (pDir->dir_nNumFiles == 0) { 131 pDir->dir_hDirHandle = FindFirstFile(pDir->dir_pDirectoryName, &wfdFindData); 132 if (pDir->dir_hDirHandle == INVALID_HANDLE_VALUE) 133 return NULL; 134 } else if (!FindNextFile(pDir->dir_hDirHandle, &wfdFindData)) 135 return NULL; 136 137 /* bump count for next call to readdir() or telldir() */ 138 pDir->dir_nNumFiles++; 139 140 /* fill in struct dirent values */ 141 pDir->dir_sdReturn.d_ino = (ino_t)-1; 142 strcpy(pDir->dir_sdReturn.d_name, wfdFindData.cFileName); 143 144 return &pDir->dir_sdReturn; 146 145 } 147 146 … … 149 148 rewinddir(DIR* pDir) 150 149 { 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 150 if (!pDir) { 151 errno = EINVAL; 152 return; 153 } 154 155 /* sanity check that this is a DIR pointer */ 156 if (pDir->dir_ulCookie != __DIRENT_COOKIE) { 157 errno = EINVAL; 158 return; 159 } 160 161 /* close the WINDOWS32 directory handle */ 162 if (pDir->dir_hDirHandle != INVALID_HANDLE_VALUE) 163 if (!FindClose(pDir->dir_hDirHandle)) 164 errno = EBADF; 165 166 /* reset members which control readdir() */ 167 pDir->dir_hDirHandle = INVALID_HANDLE_VALUE; 168 pDir->dir_nNumFiles = 0; 169 170 return; 172 171 } 173 172 … … 175 174 telldir(DIR* pDir) 176 175 { 177 178 179 180 181 182 183 184 185 186 187 188 189 176 if (!pDir) { 177 errno = EINVAL; 178 return -1; 179 } 180 181 /* sanity check that this is a DIR pointer */ 182 if (pDir->dir_ulCookie != __DIRENT_COOKIE) { 183 errno = EINVAL; 184 return -1; 185 } 186 187 /* return number of times readdir() called */ 188 return pDir->dir_nNumFiles; 190 189 } 191 190 … … 193 192 seekdir(DIR* pDir, long nPosition) 194 193 { 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 } 194 if (!pDir) 195 return; 196 197 /* sanity check that this is a DIR pointer */ 198 if (pDir->dir_ulCookie != __DIRENT_COOKIE) 199 return; 200 201 /* go back to beginning of directory */ 202 rewinddir(pDir); 203 204 /* loop until we have found position we care about */ 205 for (--nPosition; nPosition && readdir(pDir); nPosition--); 206 207 /* flag invalid nPosition value */ 208 if (nPosition) 209 errno = EINVAL; 210 211 return; 212 } -
trunk/src/kmk/w32/imagecache.c
r2640 r3140 29 29 * Header Files * 30 30 *******************************************************************************/ 31 #include "make .h"31 #include "makeint.h" 32 32 33 33 #include <Windows.h> -
trunk/src/kmk/w32/include/dirent.h
r2702 r3140 1 1 /* Windows version of dirent.h 2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 3 2007, 2008, 2009, 2010 Free Software Foundation, Inc. 2 Copyright (C) 1996-2016 Free Software Foundation, Inc. 4 3 This file is part of GNU Make. 5 4 … … 44 43 struct dirent 45 44 { 46 ino_t d_ino; 45 ino_t d_ino; /* unused - no equivalent on WINDOWS32 */ 47 46 char d_name[NAME_MAX+1]; 48 47 }; 49 48 50 49 typedef struct dir_struct { 51 ULONGdir_ulCookie;52 HANDLEdir_hDirHandle;53 DWORDdir_nNumFiles;54 chardir_pDirectoryName[NAME_MAX+1];55 50 ULONG dir_ulCookie; 51 HANDLE dir_hDirHandle; 52 DWORD dir_nNumFiles; 53 char dir_pDirectoryName[NAME_MAX+1]; 54 struct dirent dir_sdReturn; 56 55 } DIR; 57 56 -
trunk/src/kmk/w32/include/pathstuff.h
r2591 r3140 1 1 /* Definitions for Windows path manipulation. 2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 3 2007, 2008, 2009, 2010 Free Software Foundation, Inc. 2 Copyright (C) 1996-2016 Free Software Foundation, Inc. 4 3 This file is part of GNU Make. 5 4 -
trunk/src/kmk/w32/include/sub_proc.h
r2912 r3140 1 1 /* Definitions for Windows process invocation. 2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. 2 Copyright (C) 1996-2016 Free Software Foundation, Inc. 4 3 This file is part of GNU Make. 5 4 … … 22 21 * Component Name: 23 22 * 24 * $Date : 2010/07/13 01:20:43$23 * $Date$ 25 24 * 26 * $Source : /sources/make/make/w32/include/sub_proc.h,v$25 * $Source$ 27 26 * 28 * $Id : sub_proc.h,v 1.12 2010/07/13 01:20:43 psmith Exp$27 * $Id$ 29 28 */ 30 29 … … 34 33 EXTERN_DECL(HANDLE process_init, (VOID_DECL)); 35 34 EXTERN_DECL(HANDLE process_init_fd, (HANDLE stdinh, HANDLE stdouth, 36 35 HANDLE stderrh)); 37 36 EXTERN_DECL(long process_begin, (HANDLE proc, char **argv, char **envp, 38 37 char *exec_path, char *as_user)); 39 38 EXTERN_DECL(long process_pipe_io, (HANDLE proc, char *stdin_data, 40 39 int stdin_data_len)); 41 40 #ifndef KMK /* unused */ 42 41 EXTERN_DECL(long process_file_io, (HANDLE proc)); 43 42 #endif 44 43 EXTERN_DECL(void process_cleanup, (HANDLE proc)); 45 EXTERN_DECL(HANDLE process_wait_for_any, ( VOID_DECL));44 EXTERN_DECL(HANDLE process_wait_for_any, (int block, DWORD* pdwWaitStatus)); 46 45 EXTERN_DECL(void process_register, (HANDLE proc)); 47 EXTERN_DECL(HANDLE process_easy, (char** argv, char** env)); 46 EXTERN_DECL(HANDLE process_easy, (char** argv, char** env, 47 int outfd, int errfd)); 48 48 EXTERN_DECL(BOOL process_kill, (HANDLE proc, int signal)); 49 49 EXTERN_DECL(int process_used_slots, (VOID_DECL)); 50 EXTERN_DECL(DWORD process_set_handles, (HANDLE *handles)); 51 50 52 #ifdef KMK 51 53 EXTERN_DECL(int process_kmk_register_submit, (HANDLE hEvent, intptr_t clue, pid_t *pPid)); … … 63 65 EXTERN_DECL(int process_errcnt, (HANDLE proc)); 64 66 EXTERN_DECL(void process_pipes, (HANDLE proc, int pipes[3])); 67 EXTERN_DECL(void process_noinherit, (int fildes)); 65 68 66 69 #endif -
trunk/src/kmk/w32/include/w32err.h
r2591 r3140 1 1 /* Definitions for Windows error handling. 2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. 2 Copyright (C) 1996-2016 Free Software Foundation, Inc. 4 3 This file is part of GNU Make. 5 4 … … 23 22 #endif 24 23 25 EXTERN_DECL(c har * map_windows32_error_to_string, (DWORD error));24 EXTERN_DECL(const char * map_windows32_error_to_string, (DWORD error)); 26 25 27 26 #endif /* !_W32ERR_H */ -
trunk/src/kmk/w32/pathstuff.c
r3060 r3140 1 1 /* Path conversion for Windows pathnames. 2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 3 2007, 2008, 2009, 2010 Free Software Foundation, Inc. 2 Copyright (C) 1996-2016 Free Software Foundation, Inc. 4 3 This file is part of GNU Make. 5 4 … … 16 15 this program. If not, see <http://www.gnu.org/licenses/>. */ 17 16 18 #include "make .h"17 #include "makeint.h" 19 18 #include <string.h> 20 19 #include <stdlib.h> … … 32 31 char *etok; /* token separator for old Path */ 33 32 34 35 36 37 38 39 if (isblank((unsigned char) *etok))40 41 42 33 /* 34 * Convert all spaces to delimiters. Note that pathnames which 35 * contain blanks get trounced here. Use 8.3 format as a workaround. 36 */ 37 for (etok = Path; etok && *etok; etok++) 38 if (ISBLANK ((unsigned char) *etok)) 39 *etok = to_delim; 40 41 return (convert_Path_to_windows32(Path, to_delim)); 43 42 } 44 43 … … 84 83 *etok = to_delim; 85 84 p = ++etok; 86 85 } else 87 86 p += strlen(p); 88 87 } else { … … 128 127 getcwd_fs(char* buf, int len) 129 128 { 130 131 132 133 134 #if 1 /* bird */129 char *p = getcwd(buf, len); 130 131 if (p) { 132 char *q = w32ify(buf, 0); 133 #if 1 /* bird - UPSTREAM? */ 135 134 buf[0] = '\0'; 136 135 strncat(buf, q, len); 137 136 #else /* !bird */ 138 139 #endif /* !bird */140 141 142 137 strncpy(buf, q, len); 138 #endif 139 } 140 141 return p; 143 142 } 144 143 -
trunk/src/kmk/w32/subproc/NMakefile
r2591 r3140 1 # NOTE: If you have no `make' program at all to process this makefile, run2 # `build.bat' instead.1 # NOTE: If you have no 'make' program at all to process this makefile, run 2 # 'build.bat' instead. 3 3 # 4 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 5 # 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. 4 # Copyright (C) 1996-2016 Free Software Foundation, Inc. 6 5 # This file is part of GNU Make. 7 6 # -
trunk/src/kmk/w32/subproc/misc.c
r2591 r3140 1 1 /* Process handling for Windows 2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. 2 Copyright (C) 1996-2016 Free Software Foundation, Inc. 4 3 This file is part of GNU Make. 5 4 … … 26 25 /* 27 26 * Description: Convert a NULL string terminated UNIX environment block to 28 * 27 * an environment block suitable for a windows32 system call 29 28 * 30 29 * Returns: TRUE= success, FALSE=fail 31 30 * 32 31 * Notes/Dependencies: the environment block is sorted in case-insensitive 33 * 32 * order, is double-null terminated, and is a char *, not a char ** 34 33 */ 35 34 int _cdecl compare(const void *a1, const void *a2) 36 35 { 37 36 return _stricoll(*((char**)a1),*((char**)a2)); 38 37 } 39 38 bool_t 40 arr2envblk(char **arr, char **envblk_out )39 arr2envblk(char **arr, char **envblk_out, int *envsize_needed) 41 40 { 42 43 44 45 41 char **tmp; 42 int size_needed; 43 int arrcnt; 44 char *ptr; 46 45 47 48 49 50 46 arrcnt = 0; 47 while (arr[arrcnt]) { 48 arrcnt++; 49 } 51 50 52 53 54 55 51 tmp = (char**) calloc(arrcnt + 1, sizeof(char *)); 52 if (!tmp) { 53 return FALSE; 54 } 56 55 57 arrcnt = 0; 58 size_needed = 0; 59 while (arr[arrcnt]) { 60 tmp[arrcnt] = arr[arrcnt]; 61 size_needed += strlen(arr[arrcnt]) + 1; 62 arrcnt++; 63 } 64 size_needed++; 56 arrcnt = 0; 57 size_needed = *envsize_needed = 0; 58 while (arr[arrcnt]) { 59 tmp[arrcnt] = arr[arrcnt]; 60 size_needed += strlen(arr[arrcnt]) + 1; 61 arrcnt++; 62 } 63 size_needed++; 64 *envsize_needed = size_needed; 65 65 66 66 qsort((void *) tmp, (size_t) arrcnt, sizeof (char*), compare); 67 67 68 69 70 71 72 68 ptr = *envblk_out = calloc(size_needed, 1); 69 if (!ptr) { 70 free(tmp); 71 return FALSE; 72 } 73 73 74 75 76 77 78 79 74 arrcnt = 0; 75 while (tmp[arrcnt]) { 76 strcpy(ptr, tmp[arrcnt]); 77 ptr += strlen(tmp[arrcnt]) + 1; 78 arrcnt++; 79 } 80 80 81 82 81 free(tmp); 82 return TRUE; 83 83 } -
trunk/src/kmk/w32/subproc/proc.h
r2591 r3140 1 1 /* Definitions for Windows 2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. 2 Copyright (C) 1996-2016 Free Software Foundation, Inc. 4 3 This file is part of GNU Make. 5 4 … … 21 20 typedef int bool_t; 22 21 23 #define E_SCALL 24 #define E_IO 25 #define E_NO_MEM 26 #define E_FORK 22 #define E_SCALL 101 23 #define E_IO 102 24 #define E_NO_MEM 103 25 #define E_FORK 104 27 26 28 extern bool_t arr2envblk(char **arr, char **envblk_out );27 extern bool_t arr2envblk(char **arr, char **envblk_out, int *envsize_needed); 29 28 30 29 #endif -
trunk/src/kmk/w32/subproc/sub_proc.c
r3051 r3140 1 1 /* Process handling for Windows. 2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. 2 Copyright (C) 1996-2016 Free Software Foundation, Inc. 4 3 This file is part of GNU Make. 5 4 … … 19 18 #include <stdlib.h> 20 19 #include <stdio.h> 20 #include <io.h> /* for _get_osfhandle */ 21 21 #ifdef _MSC_VER 22 22 # include <stddef.h> /* for intptr_t */ … … 24 24 # include <stdint.h> 25 25 #endif 26 #include <string.h> 26 27 #include <process.h> /* for msvc _beginthreadex, _endthreadex */ 27 28 #include <signal.h> 28 29 #include <windows.h> 29 #ifdef KMK 30 # include <assert.h> 31 # include "make.h" 32 # include "kmkbuiltin.h" 33 #endif 34 35 30 31 #include "makeint.h" 32 #include "filedef.h" 33 #include "variable.h" 36 34 #include "sub_proc.h" 37 35 #include "proc.h" … … 39 37 #include "debug.h" 40 38 39 #ifdef KMK 40 # include <assert.h> 41 # include "kmkbuiltin.h" 42 extern void kmk_cache_exec_image(const char *); /* imagecache.c */ 43 #endif 44 41 45 static char *make_command_line(char *shell_name, char *exec_path, char **argv); 42 #ifndef KMK43 extern char *xmalloc (unsigned int);44 #else45 extern void kmk_cache_exec_image(const char *); /* imagecache.c */46 #endif47 46 48 47 typedef struct sub_process_t { 49 48 #ifdef KMK 50 51 52 #endif 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 49 enum { kRegular = 0, kSubmit, kSubProcFreed } enmType; 50 intptr_t clue; 51 #endif 52 intptr_t sv_stdin[2]; 53 intptr_t sv_stdout[2]; 54 intptr_t sv_stderr[2]; 55 int using_pipes; 56 char *inp; 57 DWORD incnt; 58 char * volatile outp; 59 volatile DWORD outcnt; 60 char * volatile errp; 61 volatile DWORD errcnt; 62 pid_t pid; 63 int exit_code; 64 int signal; 65 long last_err; 66 long lerrno; 68 67 } sub_process; 69 68 … … 74 73 static int proc_index = 0; 75 74 static int fake_exits_pending = 0; 75 76 77 /* 78 * Fill a HANDLE list with handles to wait for. 79 */ 80 DWORD 81 process_set_handles(HANDLE *handles) 82 { 83 DWORD count = 0; 84 int i; 85 86 /* Build array of handles to wait for */ 87 for (i = 0; i < proc_index; i++) { 88 /* Don't wait on child processes that have already finished */ 89 if (fake_exits_pending && proc_array[i]->exit_code) 90 continue; 91 92 handles[count++] = (HANDLE) proc_array[i]->pid; 93 } 94 95 return count; 96 } 76 97 77 98 #ifndef KMK /* Inefficient! */ … … 83 104 process_adjust_wait_state(sub_process* pproc) 84 105 { 85 int i; 86 87 if (!proc_index) 88 return; 89 90 for (i = 0; i < proc_index; i++) 91 if (proc_array[i]->pid == pproc->pid) 92 break; 93 94 if (i < proc_index) { 95 proc_index--; 96 if (i != proc_index) 97 memmove(&proc_array[i], &proc_array[i+1], 98 (proc_index-i) * sizeof(sub_process*)); 99 proc_array[proc_index] = NULL; 100 } 101 } 106 int i; 107 108 if (!proc_index) 109 return; 110 111 for (i = 0; i < proc_index; i++) 112 if (proc_array[i]->pid == pproc->pid) 113 break; 114 115 if (i < proc_index) { 116 proc_index--; 117 if (i != proc_index) 118 memmove(&proc_array[i], &proc_array[i+1], 119 (proc_index-i) * sizeof(sub_process*)); 120 proc_array[proc_index] = NULL; 121 } 122 } 123 102 124 #endif /* !KMK */ 103 125 … … 106 128 */ 107 129 static sub_process * 108 process_wait_for_any_private( void)109 { 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 130 process_wait_for_any_private(int block, DWORD* pdwWaitStatus) 131 { 132 HANDLE handles[MAXIMUM_WAIT_OBJECTS]; 133 DWORD retval, which; 134 int i; 135 136 if (!proc_index) 137 return NULL; 138 139 /* build array of handles to wait for */ 140 for (i = 0; i < proc_index; i++) { 141 handles[i] = (HANDLE) proc_array[i]->pid; 142 143 if (fake_exits_pending && proc_array[i]->exit_code) 144 break; 145 } 146 147 /* wait for someone to exit */ 148 if (!fake_exits_pending) { 127 149 #ifdef KMK 128 150 l_wait_again: 129 151 #endif 130 retval = WaitForMultipleObjects(proc_index, handles, FALSE, INFINITE); 131 which = retval - WAIT_OBJECT_0; 132 } else { 133 fake_exits_pending--; 134 retval = !WAIT_FAILED; 135 which = i; 136 } 137 138 /* return pointer to process */ 139 if (retval != WAIT_FAILED) { 140 sub_process* pproc = proc_array[which]; 141 #ifdef KMK 142 if (pproc->enmType == kSubmit) { 143 /* Try get the result from kSubmit.c. This may not succeed if the whole 144 result hasn't arrived yet, in which we just restart the wait. */ 145 if (kSubmitSubProcGetResult(pproc->clue, &pproc->exit_code, &pproc->signal) != 0) { 146 goto l_wait_again; 147 } 148 } 152 retval = WaitForMultipleObjects(proc_index, handles, FALSE, (block ? INFINITE : 0)); 153 which = retval - WAIT_OBJECT_0; 154 } else { 155 fake_exits_pending--; 156 retval = !WAIT_FAILED; 157 which = i; 158 } 159 160 /* If the pointer is not NULL, set the wait status result variable. */ 161 if (pdwWaitStatus) 162 *pdwWaitStatus = retval; 163 164 /* return pointer to process */ 165 if ((retval == WAIT_TIMEOUT) || (retval == WAIT_FAILED)) { 166 return NULL; 167 } 168 else { 169 sub_process* pproc = proc_array[which]; 170 #ifdef KMK 171 if (pproc->enmType == kSubmit) { 172 /* Try get the result from kSubmit.c. This may not succeed if the whole 173 result hasn't arrived yet, in which we just restart the wait. */ 174 if (kSubmitSubProcGetResult(pproc->clue, &pproc->exit_code, &pproc->signal) != 0) { 175 goto l_wait_again; 176 } 177 } 149 178 #endif 150 179 #ifndef KMK /* Inefficient! */ 151 180 process_adjust_wait_state(pproc); 152 181 #else 153 proc_index--; 154 if ((int)which < proc_index) 155 proc_array[which] = proc_array[proc_index]; 156 proc_array[proc_index] = NULL; 157 #endif 158 return pproc; 159 } else 160 return NULL; 182 proc_index--; 183 if ((int)which < proc_index) 184 proc_array[which] = proc_array[proc_index]; 185 proc_array[proc_index] = NULL; 186 #endif 187 return pproc; 188 } 161 189 } 162 190 … … 167 195 process_kill(HANDLE proc, int signal) 168 196 { 169 sub_process* pproc = (sub_process*) proc; 170 pproc->signal = signal; 171 #ifdef KMK 172 if (pproc->enmType == kRegular) { 173 #endif 174 return (TerminateProcess((HANDLE) pproc->pid, signal)); 175 #ifdef KMK 176 } else if (pproc->enmType == kSubmit) { 177 return kSubmitSubProcKill(pproc->clue, signal) == 0; 178 } 179 assert(0); 180 return FALSE; 197 sub_process* pproc = (sub_process*) proc; 198 #ifdef KMK 199 if (pproc->enmType == kRegular) { 200 #endif 201 pproc->signal = signal; 202 return (TerminateProcess((HANDLE) pproc->pid, signal)); 203 #ifdef KMK 204 } else if (pproc->enmType == kSubmit) 205 return kSubmitSubProcKill(pproc->clue, signal) == 0; 206 assert(0); 207 return FALSE; 181 208 #endif 182 209 } … … 192 219 { 193 220 #ifdef KMK 194 195 #endif 196 197 221 assert(((sub_process *)proc)->enmType == kRegular); 222 #endif 223 if (proc_index < MAXIMUM_WAIT_OBJECTS) 224 proc_array[proc_index++] = (sub_process *) proc; 198 225 } 199 226 … … 255 282 process_used_slots(void) 256 283 { 257 284 return proc_index; 258 285 } 259 286 … … 263 290 * you must do 1 of things: 264 291 * 265 * 292 * x = process_easy(...); 266 293 * 267 294 * or 268 295 * 269 * 270 * 296 * x = process_init_fd(); 297 * process_register(x); 271 298 * 272 299 * or 273 300 * 274 * 275 * 301 * x = process_init(); 302 * process_register(x); 276 303 * 277 304 * You must NOT then call process_pipe_io() because this function is … … 281 308 282 309 HANDLE 283 process_wait_for_any( void)284 { 285 sub_process* pproc = process_wait_for_any_private();286 287 288 289 290 291 292 293 294 #ifdef KMK 295 296 297 298 299 300 301 302 310 process_wait_for_any(int block, DWORD* pdwWaitStatus) 311 { 312 sub_process* pproc = process_wait_for_any_private(block, pdwWaitStatus); 313 314 if (!pproc) 315 return NULL; 316 else { 317 /* 318 * Ouch! can't tell caller if this fails directly. Caller 319 * will have to use process_last_err() 320 */ 321 #ifdef KMK 322 /* Invalidate negative directory cache entries now that a 323 job has completed and possibly created new files that 324 was missing earlier. */ 325 dir_cache_invalid_after_job (); 326 327 if (pproc->enmType == kRegular) { 328 (void)process_file_io_private(pproc, FALSE); 329 } 303 330 #else 304 305 #endif 306 307 331 (void) process_file_io(pproc); 332 #endif 333 return ((HANDLE) pproc); 334 } 308 335 } 309 336 … … 319 346 { 320 347 if (proc == INVALID_HANDLE_VALUE) return ERROR_INVALID_HANDLE; 321 348 return (((sub_process *)proc)->last_err); 322 349 } 323 350 … … 326 353 { 327 354 if (proc == INVALID_HANDLE_VALUE) return EXIT_FAILURE; 328 return (((sub_process *)proc)->exit_code); 355 return (((sub_process *)proc)->exit_code); 356 } 357 358 void 359 process_noinherit(int fd) 360 { 361 HANDLE fh = (HANDLE)_get_osfhandle(fd); 362 363 if (fh && fh != INVALID_HANDLE_VALUE) 364 SetHandleInformation(fh, HANDLE_FLAG_INHERIT, 0); 329 365 } 330 366 … … 339 375 process_outbuf(HANDLE proc) 340 376 { 341 377 return (((sub_process *)proc)->outp); 342 378 } 343 379 … … 345 381 process_errbuf(HANDLE proc) 346 382 { 347 383 return (((sub_process *)proc)->errp); 348 384 } 349 385 … … 351 387 process_outcnt(HANDLE proc) 352 388 { 353 389 return (((sub_process *)proc)->outcnt); 354 390 } 355 391 … … 357 393 process_errcnt(HANDLE proc) 358 394 { 359 395 return (((sub_process *)proc)->errcnt); 360 396 } 361 397 … … 363 399 process_pipes(HANDLE proc, int pipes[3]) 364 400 { 365 366 367 368 401 pipes[0] = ((sub_process *)proc)->sv_stdin[0]; 402 pipes[1] = ((sub_process *)proc)->sv_stdout[0]; 403 pipes[2] = ((sub_process *)proc)->sv_stderr[0]; 404 return; 369 405 } 370 406 */ 371 407 372 408 HANDLE 373 409 process_init() 374 410 { 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 } 443 444 445 411 sub_process *pproc; 412 /* 413 * open file descriptors for attaching stdin/stdout/sterr 414 */ 415 HANDLE stdin_pipes[2]; 416 HANDLE stdout_pipes[2]; 417 HANDLE stderr_pipes[2]; 418 SECURITY_ATTRIBUTES inherit; 419 BYTE sd[SECURITY_DESCRIPTOR_MIN_LENGTH]; 420 421 pproc = malloc(sizeof(*pproc)); 422 memset(pproc, 0, sizeof(*pproc)); 423 424 /* We can't use NULL for lpSecurityDescriptor because that 425 uses the default security descriptor of the calling process. 426 Instead we use a security descriptor with no DACL. This 427 allows nonrestricted access to the associated objects. */ 428 429 if (!InitializeSecurityDescriptor((PSECURITY_DESCRIPTOR)(&sd), 430 SECURITY_DESCRIPTOR_REVISION)) { 431 pproc->last_err = GetLastError(); 432 pproc->lerrno = E_SCALL; 433 return((HANDLE)pproc); 434 } 435 436 inherit.nLength = sizeof(inherit); 437 inherit.lpSecurityDescriptor = (PSECURITY_DESCRIPTOR)(&sd); 438 inherit.bInheritHandle = TRUE; 439 440 // By convention, parent gets pipe[0], and child gets pipe[1] 441 // This means the READ side of stdin pipe goes into pipe[1] 442 // and the WRITE side of the stdout and stderr pipes go into pipe[1] 443 if (CreatePipe( &stdin_pipes[1], &stdin_pipes[0], &inherit, 0) == FALSE || 444 CreatePipe( &stdout_pipes[0], &stdout_pipes[1], &inherit, 0) == FALSE || 445 CreatePipe( &stderr_pipes[0], &stderr_pipes[1], &inherit, 0) == FALSE) { 446 447 pproc->last_err = GetLastError(); 448 pproc->lerrno = E_SCALL; 449 return((HANDLE)pproc); 450 } 451 452 // 453 // Mark the parent sides of the pipes as non-inheritable 454 // 455 if (SetHandleInformation(stdin_pipes[0], 456 HANDLE_FLAG_INHERIT, 0) == FALSE || 457 SetHandleInformation(stdout_pipes[0], 458 HANDLE_FLAG_INHERIT, 0) == FALSE || 459 SetHandleInformation(stderr_pipes[0], 460 HANDLE_FLAG_INHERIT, 0) == FALSE) { 461 462 pproc->last_err = GetLastError(); 463 pproc->lerrno = E_SCALL; 464 return((HANDLE)pproc); 465 } 466 pproc->sv_stdin[0] = (intptr_t) stdin_pipes[0]; 467 pproc->sv_stdin[1] = (intptr_t) stdin_pipes[1]; 468 pproc->sv_stdout[0] = (intptr_t) stdout_pipes[0]; 469 pproc->sv_stdout[1] = (intptr_t) stdout_pipes[1]; 470 pproc->sv_stderr[0] = (intptr_t) stderr_pipes[0]; 471 pproc->sv_stderr[1] = (intptr_t) stderr_pipes[1]; 472 473 pproc->using_pipes = 1; 474 475 pproc->lerrno = 0; 476 477 return((HANDLE)pproc); 478 } 479 480 481 HANDLE 446 482 process_init_fd(HANDLE stdinh, HANDLE stdouth, HANDLE stderrh) 447 483 { 448 sub_process *pproc; 449 450 pproc = malloc(sizeof(*pproc)); 451 memset(pproc, 0, sizeof(*pproc)); 452 453 /* 454 * Just pass the provided file handles to the 'child side' of the 455 * pipe, bypassing pipes altogether. 456 */ 457 pproc->sv_stdin[1] = (intptr_t) stdinh; 458 pproc->sv_stdout[1] = (intptr_t) stdouth; 459 pproc->sv_stderr[1] = (intptr_t) stderrh; 460 461 pproc->last_err = pproc->lerrno = 0; 462 463 return((HANDLE)pproc); 484 sub_process *pproc; 485 486 pproc = malloc(sizeof(*pproc)); 487 if (pproc) { 488 memset(pproc, 0, sizeof(*pproc)); 489 490 /* 491 * Just pass the provided file handles to the 'child 492 * side' of the pipe, bypassing pipes altogether. 493 */ 494 pproc->sv_stdin[1] = (intptr_t) stdinh; 495 pproc->sv_stdout[1] = (intptr_t) stdouth; 496 pproc->sv_stderr[1] = (intptr_t) stderrh; 497 498 pproc->last_err = pproc->lerrno = 0; 499 } 500 501 return((HANDLE)pproc); 464 502 } 465 503 … … 467 505 static HANDLE 468 506 find_file(const char *exec_path, const char *path_var, 469 char *full_fname, DWORD full_len) 470 { 471 HANDLE exec_handle; 472 char *fname; 473 char *ext; 474 DWORD req_len; 475 int i; 476 static const char *extensions[] = 477 /* Should .com come before no-extension case? */ 478 { ".exe", ".cmd", ".bat", "", ".com", NULL }; 479 480 fname = xmalloc(strlen(exec_path) + 5); 481 strcpy(fname, exec_path); 482 ext = fname + strlen(fname); 483 484 for (i = 0; extensions[i]; i++) { 485 strcpy(ext, extensions[i]); 486 if (((req_len = SearchPath (path_var, fname, NULL, full_len, 487 full_fname, NULL)) > 0 488 /* For compatibility with previous code, which 489 used OpenFile, and with Windows operation in 490 general, also look in various default 491 locations, such as Windows directory and 492 Windows System directory. Warning: this also 493 searches PATH in the Make's environment, which 494 might not be what the Makefile wants, but it 495 seems to be OK as a fallback, after the 496 previous SearchPath failed to find on child's 497 PATH. */ 498 || (req_len = SearchPath (NULL, fname, NULL, full_len, 499 full_fname, NULL)) > 0) 500 && req_len <= full_len 501 && (exec_handle = 502 CreateFile(full_fname, 503 GENERIC_READ, 504 FILE_SHARE_READ | FILE_SHARE_WRITE, 505 NULL, 506 OPEN_EXISTING, 507 FILE_ATTRIBUTE_NORMAL, 508 NULL)) != INVALID_HANDLE_VALUE) { 509 free(fname); 510 return(exec_handle); 511 } 512 } 513 514 free(fname); 515 return INVALID_HANDLE_VALUE; 507 char *full_fname, DWORD full_len) 508 { 509 HANDLE exec_handle; 510 char *fname; 511 char *ext; 512 DWORD req_len; 513 int i; 514 static const char *extensions[] = 515 /* Should .com come before no-extension case? */ 516 { ".exe", ".cmd", ".bat", "", ".com", NULL }; 517 518 fname = xmalloc(strlen(exec_path) + 5); 519 strcpy(fname, exec_path); 520 ext = fname + strlen(fname); 521 522 for (i = 0; extensions[i]; i++) { 523 strcpy(ext, extensions[i]); 524 if (((req_len = SearchPath (path_var, fname, NULL, full_len, 525 full_fname, NULL)) > 0 526 /* For compatibility with previous code, which 527 used OpenFile, and with Windows operation in 528 general, also look in various default 529 locations, such as Windows directory and 530 Windows System directory. Warning: this also 531 searches PATH in the Make's environment, which 532 might not be what the Makefile wants, but it 533 seems to be OK as a fallback, after the 534 previous SearchPath failed to find on child's 535 PATH. */ 536 || (req_len = SearchPath (NULL, fname, NULL, full_len, 537 full_fname, NULL)) > 0) 538 && req_len <= full_len 539 && (exec_handle = 540 CreateFile(full_fname, 541 GENERIC_READ, 542 FILE_SHARE_READ | FILE_SHARE_WRITE, 543 NULL, 544 OPEN_EXISTING, 545 FILE_ATTRIBUTE_NORMAL, 546 NULL)) != INVALID_HANDLE_VALUE) { 547 free(fname); 548 return(exec_handle); 549 } 550 } 551 552 free(fname); 553 return INVALID_HANDLE_VALUE; 554 } 555 556 /* 557 * Return non-zero of FNAME specifies a batch file and its name 558 * includes embedded whitespace. 559 */ 560 561 static int 562 batch_file_with_spaces(const char *fname) 563 { 564 size_t fnlen = strlen(fname); 565 566 return (fnlen > 4 567 && (_strnicmp(fname + fnlen - 4, ".bat", 4) == 0 568 || _strnicmp(fname + fnlen - 4, ".cmd", 4) == 0) 569 /* The set of characters in the 2nd arg to strpbrk 570 should be the same one used by make_command_line 571 below to decide whether an argv[] element needs 572 quoting. */ 573 && strpbrk(fname, " \t") != NULL); 516 574 } 517 575 … … 526 584 long 527 585 process_begin( 528 HANDLE proc, 529 char **argv, 530 char **envp, 531 char *exec_path, 532 char *as_user) 533 { 534 sub_process *pproc = (sub_process *)proc; 535 char *shell_name = 0; 536 int file_not_found=0; 537 HANDLE exec_handle; 538 char exec_fname[MAX_PATH]; 539 const char *path_var = NULL; 540 char **ep; 541 char buf[256]; 542 DWORD bytes_returned; 543 DWORD flags; 544 char *command_line; 545 STARTUPINFO startInfo; 546 PROCESS_INFORMATION procInfo; 547 char *envblk=NULL; 586 HANDLE proc, 587 char **argv, 588 char **envp, 589 char *exec_path, 590 char *as_user) 591 { 592 sub_process *pproc = (sub_process *)proc; 593 char *shell_name = 0; 594 int file_not_found=0; 595 HANDLE exec_handle; 596 char exec_fname[MAX_PATH]; 597 const char *path_var = NULL; 598 char **ep; 599 char buf[MAX_PATH]; 600 DWORD bytes_returned; 601 DWORD flags; 602 char *command_line; 603 STARTUPINFO startInfo; 604 PROCESS_INFORMATION procInfo; 605 char *envblk=NULL; 606 int envsize_needed = 0; 607 int pass_null_exec_path = 0; 548 608 #ifdef KMK 549 609 size_t exec_path_len; … … 552 612 assert (pproc->enmType == kRegular); 553 613 #endif 554 555 614 556 615 /* … … 574 633 else { 575 634 #endif /* KMK */ 576 /* Use the Makefile's value of PATH to look for the program to 577 execute, because it could be different from Make's PATH 578 (e.g., if the target sets its own value. */ 579 if (envp) 580 for (ep = envp; *ep; ep++) { 581 if (strncmp (*ep, "PATH=", 5) == 0 582 || strncmp (*ep, "Path=", 5) == 0) { 583 path_var = *ep + 5; 584 break; 585 } 586 } 587 exec_handle = find_file(exec_path, path_var, 588 exec_fname, sizeof(exec_fname)); 589 #ifdef KMK 590 } 591 #endif 592 593 /* 594 * If we couldn't open the file, just assume that Windows will be 595 * somehow able to find and execute it. 596 */ 597 if (exec_handle == INVALID_HANDLE_VALUE) { 598 file_not_found++; 599 } 600 else { 601 /* Attempt to read the first line of the file */ 602 if (ReadFile( exec_handle, 603 buf, sizeof(buf) - 1, /* leave room for trailing NULL */ 604 &bytes_returned, 0) == FALSE || bytes_returned < 2) { 605 606 pproc->last_err = GetLastError(); 607 pproc->lerrno = E_IO; 608 CloseHandle(exec_handle); 609 return(-1); 610 } 611 if (buf[0] == '#' && buf[1] == '!') { 612 /* 613 * This is a shell script... Change the command line from 614 * exec_path args to shell_name exec_path args 615 */ 616 char *p; 617 618 /* Make sure buf is NULL terminated */ 619 buf[bytes_returned] = 0; 620 /* 621 * Depending on the file system type, etc. the first line 622 * of the shell script may end with newline or newline-carriage-return 623 * Whatever it ends with, cut it off. 624 */ 625 p= strchr(buf, '\n'); 626 if (p) 627 *p = 0; 628 p = strchr(buf, '\r'); 629 if (p) 630 *p = 0; 631 632 /* 633 * Find base name of shell 634 */ 635 shell_name = strrchr( buf, '/'); 636 if (shell_name) { 637 shell_name++; 638 } else { 639 shell_name = &buf[2];/* skipping "#!" */ 640 } 641 642 } 643 CloseHandle(exec_handle); 644 } 645 646 flags = 0; 647 648 if (file_not_found) 649 command_line = make_command_line( shell_name, exec_path, argv); 650 else 651 command_line = make_command_line( shell_name, exec_fname, argv); 652 653 if ( command_line == NULL ) { 654 pproc->last_err = 0; 655 pproc->lerrno = E_NO_MEM; 656 return(-1); 657 } 658 659 if (envp) { 660 if (arr2envblk(envp, &envblk) ==FALSE) { 661 pproc->last_err = 0; 662 pproc->lerrno = E_NO_MEM; 663 free( command_line ); 664 return(-1); 665 } 666 } 667 668 if ((shell_name) || (file_not_found)) { 669 exec_path = 0; /* Search for the program in %Path% */ 670 } else { 671 exec_path = exec_fname; 672 } 673 674 /* 675 * Set up inherited stdin, stdout, stderr for child 676 */ 677 GetStartupInfo(&startInfo); 635 636 /* Use the Makefile's value of PATH to look for the program to 637 execute, because it could be different from Make's PATH 638 (e.g., if the target sets its own value. */ 639 if (envp) 640 for (ep = envp; *ep; ep++) { 641 if (strncmp (*ep, "PATH=", 5) == 0 642 || strncmp (*ep, "Path=", 5) == 0) { 643 path_var = *ep + 5; 644 break; 645 } 646 } 647 exec_handle = find_file(exec_path, path_var, 648 exec_fname, sizeof(exec_fname)); 649 #ifdef KMK 650 } 651 #endif 652 653 /* 654 * If we couldn't open the file, just assume that Windows will be 655 * somehow able to find and execute it. If the first character 656 * of the command is '/', assume they set SHELL to a Unixy shell 657 * that have some magic mounts known only to it, and run the whole 658 * command via $SHELL -c "COMMAND" instead. 659 */ 660 if (exec_handle == INVALID_HANDLE_VALUE) { 661 if (exec_path[0] == '/') { 662 char *new_argv0; 663 char **argvi = argv; 664 int arglen = 0; 665 666 strcpy(buf, variable_expand ("$(SHELL)")); 667 shell_name = &buf[0]; 668 strcpy(exec_fname, "-c"); 669 /* Construct a single command string in argv[0]. */ 670 while (*argvi) { 671 arglen += strlen(*argvi) + 1; 672 argvi++; 673 } 674 new_argv0 = xmalloc(arglen + 1); 675 new_argv0[0] = '\0'; 676 for (argvi = argv; *argvi; argvi++) { 677 strcat(new_argv0, *argvi); 678 strcat(new_argv0, " "); 679 } 680 /* Remove the extra blank at the end. */ 681 new_argv0[arglen-1] = '\0'; 682 free(argv[0]); 683 argv[0] = new_argv0; 684 argv[1] = NULL; 685 } 686 else 687 file_not_found++; 688 } 689 else { 690 /* Attempt to read the first line of the file */ 691 if (ReadFile( exec_handle, 692 buf, sizeof(buf) - 1, /* leave room for trailing NULL */ 693 &bytes_returned, 0) == FALSE || bytes_returned < 2) { 694 695 pproc->last_err = GetLastError(); 696 pproc->lerrno = E_IO; 697 CloseHandle(exec_handle); 698 return(-1); 699 } 700 if (buf[0] == '#' && buf[1] == '!') { 701 /* 702 * This is a shell script... Change the command line from 703 * exec_path args to shell_name exec_path args 704 */ 705 char *p; 706 707 /* Make sure buf is NULL terminated */ 708 buf[bytes_returned] = 0; 709 /* 710 * Depending on the file system type, etc. the first line 711 * of the shell script may end with newline or newline-carriage-return 712 * Whatever it ends with, cut it off. 713 */ 714 p= strchr(buf, '\n'); 715 if (p) 716 *p = 0; 717 p = strchr(buf, '\r'); 718 if (p) 719 *p = 0; 720 721 /* 722 * Find base name of shell 723 */ 724 shell_name = strrchr( buf, '/'); 725 if (shell_name) { 726 shell_name++; 727 } else { 728 shell_name = &buf[2];/* skipping "#!" */ 729 } 730 731 } 732 CloseHandle(exec_handle); 733 } 734 735 flags = 0; 736 737 if (file_not_found) 738 command_line = make_command_line( shell_name, exec_path, argv); 739 else { 740 /* If exec_fname includes whitespace, CreateProcess 741 behaves erratically and unreliably, and often fails 742 if argv[0] also includes whitespace (and thus will 743 be quoted by make_command_line below). So in that 744 case, we don't pass exec_fname as the 1st arg to 745 CreateProcess, but instead replace argv[0] with 746 exec_fname (to keep its leading directories and 747 extension as found by find_file), and pass NULL to 748 CreateProcess as its 1st arg. This works around 749 the bugs in CreateProcess, which are probably 750 caused by its passing the command to cmd.exe with 751 some incorrect quoting. */ 752 if (!shell_name 753 && batch_file_with_spaces(exec_fname) 754 && _stricmp(exec_path, argv[0]) == 0) { 755 char *new_argv, *p; 756 char **argvi; 757 int arglen, i; 758 pass_null_exec_path = 1; 759 /* Rewrite argv[] replacing argv[0] with exec_fname. */ 760 for (argvi = argv + 1, arglen = strlen(exec_fname) + 1; 761 *argvi; 762 argvi++) { 763 arglen += strlen(*argvi) + 1; 764 } 765 new_argv = xmalloc(arglen); 766 p = strcpy(new_argv, exec_fname) + strlen(exec_fname) + 1; 767 for (argvi = argv + 1, i = 1; *argvi; argvi++, i++) { 768 strcpy(p, *argvi); 769 argv[i] = p; 770 p += strlen(*argvi) + 1; 771 } 772 argv[i] = NULL; 773 free (argv[0]); 774 argv[0] = new_argv; 775 } 776 command_line = make_command_line( shell_name, exec_fname, argv); 777 } 778 779 if ( command_line == NULL ) { 780 pproc->last_err = 0; 781 pproc->lerrno = E_NO_MEM; 782 return(-1); 783 } 784 785 if (envp) { 786 if (arr2envblk(envp, &envblk, &envsize_needed) == FALSE) { 787 pproc->lerrno = E_NO_MEM; 788 free( command_line ); 789 if ((pproc->last_err == ERROR_INVALID_PARAMETER 790 || pproc->last_err == ERROR_MORE_DATA) 791 && envsize_needed > 32*1024) { 792 fprintf (stderr, "CreateProcess failed, probably because environment is too large (%d bytes).\n", 793 envsize_needed); 794 } 795 pproc->last_err = 0; 796 return(-1); 797 } 798 } 799 800 if (shell_name || file_not_found || pass_null_exec_path) { 801 exec_path = 0; /* Search for the program in %Path% */ 802 } else { 803 exec_path = exec_fname; 804 } 805 806 /* 807 * Set up inherited stdin, stdout, stderr for child 808 */ 809 memset(&startInfo, '\0', sizeof(startInfo)); 810 GetStartupInfo(&startInfo); 678 811 #ifndef KMK 679 startInfo.dwFlags = STARTF_USESTDHANDLES; 680 #endif 681 startInfo.lpReserved = 0; 682 startInfo.cbReserved2 = 0; 683 startInfo.lpReserved2 = 0; 684 startInfo.lpTitle = shell_name ? shell_name : exec_path; 812 startInfo.dwFlags = STARTF_USESTDHANDLES; 813 #endif 814 startInfo.lpReserved = 0; 815 startInfo.cbReserved2 = 0; 816 startInfo.lpReserved2 = 0; 685 817 #ifndef KMK 686 687 688 818 startInfo.hStdInput = (HANDLE)pproc->sv_stdin[1]; 819 startInfo.hStdOutput = (HANDLE)pproc->sv_stdout[1]; 820 startInfo.hStdError = (HANDLE)pproc->sv_stderr[1]; 689 821 #else 690 if ( pproc->sv_stdin[1] 691 || pproc->sv_stdout[1] 692 || pproc->sv_stderr[1]) {693 694 695 696 697 698 699 700 701 702 703 #endif 704 705 706 if (envblk)free(envblk);707 708 709 710 711 822 if ( ((HANDLE)pproc->sv_stdin[1] != INVALID_HANDLE_VALUE && pproc->sv_stdin[1]) 823 || ((HANDLE)pproc->sv_stdout[1] != INVALID_HANDLE_VALUE && pproc->sv_stdout[1]) 824 || ((HANDLE)pproc->sv_stderr[1] != INVALID_HANDLE_VALUE && pproc->sv_stderr[1]) ) { 825 startInfo.dwFlags = STARTF_USESTDHANDLES; 826 startInfo.hStdInput = (HANDLE)pproc->sv_stdin[1]; 827 startInfo.hStdOutput = (HANDLE)pproc->sv_stdout[1]; 828 startInfo.hStdError = (HANDLE)pproc->sv_stderr[1]; 829 } else { 830 startInfo.dwFlags = 0; 831 startInfo.hStdInput = 0; 832 startInfo.hStdOutput = 0; 833 startInfo.hStdError = 0; 834 } 835 #endif 836 837 if (as_user) { 838 free(envblk); 839 return -1; 840 } else { 841 DB (DB_JOBS, ("CreateProcess(%s,%s,...)\n", 842 exec_path ? exec_path : "NULL", 843 command_line ? command_line : "NULL")); 712 844 #ifdef KMK 713 845 if (exec_fname[0]) … … 726 858 } 727 859 #endif 728 if (CreateProcess( 729 exec_path, 730 command_line, 731 NULL, 732 0, /* default security attributes for thread */ 733 TRUE, /* inherit handles (e.g. helper pipes, oserv socket) */ 734 flags, 735 envblk, 736 0, /* default starting directory */ 737 &startInfo, 738 &procInfo) == FALSE) { 739 740 pproc->last_err = GetLastError(); 741 pproc->lerrno = E_FORK; 742 #ifdef KMK 743 if (pproc->last_err == ERROR_FILE_NOT_FOUND) 744 pproc->exit_code = 127; /* see execve failure in job.c. */ 745 #endif 746 fprintf(stderr, "process_begin: CreateProcess(%s, %s, ...) failed.\n", 860 if (CreateProcess( 861 exec_path, 862 command_line, 863 NULL, 864 0, /* default security attributes for thread */ 865 TRUE, /* inherit handles (e.g. helper pipes, oserv socket) */ 866 flags, 867 envblk, 868 0, /* default starting directory */ 869 &startInfo, 870 &procInfo) == FALSE) { 871 872 pproc->last_err = GetLastError(); 873 pproc->lerrno = E_FORK; 874 fprintf(stderr, "process_begin: CreateProcess(%s, %s, ...) failed.\n", 747 875 exec_path ? exec_path : "NULL", command_line); 748 if (envblk) free(envblk); 749 free( command_line ); 750 return(-1); 751 } 752 #ifdef KMK 753 switch (process_priority) { 754 case 1: SetThreadPriority(procInfo.hThread, THREAD_PRIORITY_IDLE); break; 755 case 2: SetThreadPriority(procInfo.hThread, THREAD_PRIORITY_BELOW_NORMAL); break; 756 case 3: SetThreadPriority(procInfo.hThread, THREAD_PRIORITY_NORMAL); break; 757 case 4: SetThreadPriority(procInfo.hThread, THREAD_PRIORITY_HIGHEST); break; 758 case 5: SetThreadPriority(procInfo.hThread, THREAD_PRIORITY_TIME_CRITICAL); break; 759 } 760 ResumeThread(procInfo.hThread); 761 #endif 762 } 763 764 pproc->pid = (pid_t)procInfo.hProcess; 765 /* Close the thread handle -- we'll just watch the process */ 766 CloseHandle(procInfo.hThread); 767 768 /* Close the halves of the pipes we don't need */ 769 #ifndef KMK 770 CloseHandle((HANDLE)pproc->sv_stdin[1]); 771 CloseHandle((HANDLE)pproc->sv_stdout[1]); 772 CloseHandle((HANDLE)pproc->sv_stderr[1]); 773 pproc->sv_stdin[1] = 0; 774 pproc->sv_stdout[1] = 0; 775 pproc->sv_stderr[1] = 0; 776 #else 777 if ((HANDLE)pproc->sv_stdin[1]) { 778 CloseHandle((HANDLE)pproc->sv_stdin[1]); 779 pproc->sv_stdin[1] = 0; 780 } 781 if ((HANDLE)pproc->sv_stdout[1]) { 782 CloseHandle((HANDLE)pproc->sv_stdout[1]); 783 pproc->sv_stdout[1] = 0; 784 } 785 if ((HANDLE)pproc->sv_stderr[1]) { 786 CloseHandle((HANDLE)pproc->sv_stderr[1]); 787 pproc->sv_stderr[1] = 0; 788 } 789 #endif 790 791 free( command_line ); 792 if (envblk) free(envblk); 793 pproc->lerrno=0; 794 return 0; 795 } 796 797 798 876 free(envblk); 877 free( command_line ); 878 return(-1); 879 } 880 #ifdef KMK 881 switch (process_priority) { 882 case 1: SetThreadPriority(procInfo.hThread, THREAD_PRIORITY_IDLE); break; 883 case 2: SetThreadPriority(procInfo.hThread, THREAD_PRIORITY_BELOW_NORMAL); break; 884 case 3: SetThreadPriority(procInfo.hThread, THREAD_PRIORITY_NORMAL); break; 885 case 4: SetThreadPriority(procInfo.hThread, THREAD_PRIORITY_HIGHEST); break; 886 case 5: SetThreadPriority(procInfo.hThread, THREAD_PRIORITY_TIME_CRITICAL); break; 887 } 888 ResumeThread(procInfo.hThread); 889 #endif 890 } 891 892 pproc->pid = (pid_t)procInfo.hProcess; 893 /* Close the thread handle -- we'll just watch the process */ 894 CloseHandle(procInfo.hThread); 895 896 /* Close the halves of the pipes we don't need */ 897 if ((HANDLE)pproc->sv_stdin[1] != INVALID_HANDLE_VALUE && pproc->sv_stdin[1]) 898 CloseHandle((HANDLE)pproc->sv_stdin[1]); 899 if ((HANDLE)pproc->sv_stdout[1] != INVALID_HANDLE_VALUE && pproc->sv_stdout[1]) 900 CloseHandle((HANDLE)pproc->sv_stdout[1]); 901 if ((HANDLE)pproc->sv_stderr[1] != INVALID_HANDLE_VALUE && pproc->sv_stderr[1]) 902 CloseHandle((HANDLE)pproc->sv_stderr[1]); 903 pproc->sv_stdin[1] = 0; 904 pproc->sv_stdout[1] = 0; 905 pproc->sv_stderr[1] = 0; 906 907 free( command_line ); 908 free(envblk); 909 pproc->lerrno=0; 910 return 0; 911 } 912 913 914 915 #if 0 /* unused */ 799 916 static DWORD 800 917 proc_stdin_thread(sub_process *pproc) 801 918 { 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 919 DWORD in_done; 920 for (;;) { 921 if (WriteFile( (HANDLE) pproc->sv_stdin[0], pproc->inp, pproc->incnt, 922 &in_done, NULL) == FALSE) 923 _endthreadex(0); 924 // This if should never be true for anonymous pipes, but gives 925 // us a chance to change I/O mechanisms later 926 if (in_done < pproc->incnt) { 927 pproc->incnt -= in_done; 928 pproc->inp += in_done; 929 } else { 930 _endthreadex(0); 931 } 932 } 933 return 0; // for compiler warnings only.. not reached 817 934 } 818 935 … … 820 937 proc_stdout_thread(sub_process *pproc) 821 938 { 822 823 824 825 826 827 828 829 830 831 832 833 /* 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 939 DWORD bufsize = 1024; 940 char c; 941 DWORD nread; 942 pproc->outp = malloc(bufsize); 943 if (pproc->outp == NULL) 944 _endthreadex(0); 945 pproc->outcnt = 0; 946 947 for (;;) { 948 if (ReadFile( (HANDLE)pproc->sv_stdout[0], &c, 1, &nread, NULL) 949 == FALSE) { 950 /* map_windows32_error_to_string(GetLastError());*/ 951 _endthreadex(0); 952 } 953 if (nread == 0) 954 _endthreadex(0); 955 if (pproc->outcnt + nread > bufsize) { 956 bufsize += nread + 512; 957 pproc->outp = realloc(pproc->outp, bufsize); 958 if (pproc->outp == NULL) { 959 pproc->outcnt = 0; 960 _endthreadex(0); 961 } 962 } 963 pproc->outp[pproc->outcnt++] = c; 964 } 965 return 0; 849 966 } 850 967 … … 852 969 proc_stderr_thread(sub_process *pproc) 853 970 { 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 971 DWORD bufsize = 1024; 972 char c; 973 DWORD nread; 974 pproc->errp = malloc(bufsize); 975 if (pproc->errp == NULL) 976 _endthreadex(0); 977 pproc->errcnt = 0; 978 979 for (;;) { 980 if (ReadFile( (HANDLE)pproc->sv_stderr[0], &c, 1, &nread, NULL) == FALSE) { 981 map_windows32_error_to_string(GetLastError()); 982 _endthreadex(0); 983 } 984 if (nread == 0) 985 _endthreadex(0); 986 if (pproc->errcnt + nread > bufsize) { 987 bufsize += nread + 512; 988 pproc->errp = realloc(pproc->errp, bufsize); 989 if (pproc->errp == NULL) { 990 pproc->errcnt = 0; 991 _endthreadex(0); 992 } 993 } 994 pproc->errp[pproc->errcnt++] = c; 995 } 996 return 0; 880 997 } 881 998 … … 890 1007 * Notes/Dependencies: 891 1008 */ 892 1009 long 893 1010 process_pipe_io( 894 895 896 897 { 898 899 900 901 902 903 904 905 906 907 908 1011 HANDLE proc, 1012 char *stdin_data, 1013 int stdin_data_len) 1014 { 1015 sub_process *pproc = (sub_process *)proc; 1016 bool_t stdin_eof = FALSE, stdout_eof = FALSE, stderr_eof = FALSE; 1017 HANDLE childhand = (HANDLE) pproc->pid; 1018 HANDLE tStdin = NULL, tStdout = NULL, tStderr = NULL; 1019 unsigned int dwStdin, dwStdout, dwStderr; 1020 HANDLE wait_list[4]; 1021 DWORD wait_count; 1022 DWORD wait_return; 1023 HANDLE ready_hand; 1024 bool_t child_dead = FALSE; 1025 BOOL GetExitCodeResult; 909 1026 #ifdef KMK 910 1027 assert (pproc->enmType == kRegular); 911 1028 #endif 912 1029 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 1030 /* 1031 * Create stdin thread, if needed 1032 */ 1033 pproc->inp = stdin_data; 1034 pproc->incnt = stdin_data_len; 1035 if (!pproc->inp) { 1036 stdin_eof = TRUE; 1037 CloseHandle((HANDLE)pproc->sv_stdin[0]); 1038 pproc->sv_stdin[0] = 0; 1039 } else { 1040 tStdin = (HANDLE) _beginthreadex( 0, 1024, 1041 (unsigned (__stdcall *) (void *))proc_stdin_thread, 1042 pproc, 0, &dwStdin); 1043 if (tStdin == 0) { 1044 pproc->last_err = GetLastError(); 1045 pproc->lerrno = E_SCALL; 1046 goto done; 1047 } 1048 } 1049 1050 /* 1051 * Assume child will produce stdout and stderr 1052 */ 1053 tStdout = (HANDLE) _beginthreadex( 0, 1024, 1054 (unsigned (__stdcall *) (void *))proc_stdout_thread, pproc, 0, 1055 &dwStdout); 1056 tStderr = (HANDLE) _beginthreadex( 0, 1024, 1057 (unsigned (__stdcall *) (void *))proc_stderr_thread, pproc, 0, 1058 &dwStderr); 1059 1060 if (tStdout == 0 || tStderr == 0) { 1061 1062 pproc->last_err = GetLastError(); 1063 pproc->lerrno = E_SCALL; 1064 goto done; 1065 } 1066 1067 1068 /* 1069 * Wait for all I/O to finish and for the child process to exit 1070 */ 1071 1072 while (!stdin_eof || !stdout_eof || !stderr_eof || !child_dead) { 1073 wait_count = 0; 1074 if (!stdin_eof) { 1075 wait_list[wait_count++] = tStdin; 1076 } 1077 if (!stdout_eof) { 1078 wait_list[wait_count++] = tStdout; 1079 } 1080 if (!stderr_eof) { 1081 wait_list[wait_count++] = tStderr; 1082 } 1083 if (!child_dead) { 1084 wait_list[wait_count++] = childhand; 1085 } 1086 1087 wait_return = WaitForMultipleObjects(wait_count, wait_list, 1088 FALSE, /* don't wait for all: one ready will do */ 1089 child_dead? 1000 :INFINITE); /* after the child dies, subthreads have 1090 one second to collect all remaining output */ 974 1091 975 1092 if (wait_return == WAIT_FAILED) { 976 /* 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1093 /* map_windows32_error_to_string(GetLastError());*/ 1094 pproc->last_err = GetLastError(); 1095 pproc->lerrno = E_SCALL; 1096 goto done; 1097 } 1098 1099 ready_hand = wait_list[wait_return - WAIT_OBJECT_0]; 1100 1101 if (ready_hand == tStdin) { 1102 CloseHandle((HANDLE)pproc->sv_stdin[0]); 1103 pproc->sv_stdin[0] = 0; 1104 CloseHandle(tStdin); 1105 tStdin = 0; 1106 stdin_eof = TRUE; 1107 1108 } else if (ready_hand == tStdout) { 1109 1110 CloseHandle((HANDLE)pproc->sv_stdout[0]); 1111 pproc->sv_stdout[0] = 0; 1112 CloseHandle(tStdout); 1113 tStdout = 0; 1114 stdout_eof = TRUE; 1115 1116 } else if (ready_hand == tStderr) { 1117 1118 CloseHandle((HANDLE)pproc->sv_stderr[0]); 1119 pproc->sv_stderr[0] = 0; 1120 CloseHandle(tStderr); 1121 tStderr = 0; 1122 stderr_eof = TRUE; 1123 1124 } else if (ready_hand == childhand) { 1125 1126 DWORD ierr; 1127 GetExitCodeResult = GetExitCodeProcess(childhand, &ierr); 1128 if (ierr == CONTROL_C_EXIT) { 1129 pproc->signal = SIGINT; 1130 } else { 1131 pproc->exit_code = ierr; 1132 } 1133 if (GetExitCodeResult == FALSE) { 1134 pproc->last_err = GetLastError(); 1135 pproc->lerrno = E_SCALL; 1136 goto done; 1137 } 1138 child_dead = TRUE; 1139 1140 } else { 1141 1142 /* ?? Got back a handle we didn't query ?? */ 1143 pproc->last_err = 0; 1144 pproc->lerrno = E_FAIL; 1145 goto done; 1146 } 1147 } 1031 1148 1032 1149 done: 1033 if (tStdin != 0) 1034 CloseHandle(tStdin); 1035 if (tStdout != 0) 1036 CloseHandle(tStdout); 1037 if (tStderr != 0) 1038 CloseHandle(tStderr); 1039 1040 if (pproc->lerrno) 1041 return(-1); 1042 else 1043 return(0); 1044 1045 } 1150 if (tStdin != 0) 1151 CloseHandle(tStdin); 1152 if (tStdout != 0) 1153 CloseHandle(tStdout); 1154 if (tStderr != 0) 1155 CloseHandle(tStderr); 1156 1157 if (pproc->lerrno) 1158 return(-1); 1159 else 1160 return(0); 1161 1162 } 1163 #endif /* unused */ 1046 1164 1047 1165 #ifndef KMK /* unused */ … … 1055 1173 * Notes/Dependencies: 1056 1174 */ 1057 1175 long 1058 1176 process_file_io( 1059 HANDLE proc) 1060 { 1061 sub_process *pproc; 1062 if (proc == NULL) 1063 pproc = process_wait_for_any_private(); 1064 else 1065 pproc = (sub_process *)proc; 1066 1067 /* some sort of internal error */ 1068 if (!pproc) 1069 return -1; 1177 HANDLE proc) 1178 { 1179 sub_process *pproc; 1180 1181 if (proc == NULL) 1182 pproc = process_wait_for_any_private(1, 0); 1183 else 1184 pproc = (sub_process *)proc; 1185 1186 /* some sort of internal error */ 1187 if (!pproc) 1188 return -1; 1070 1189 1071 1190 return process_file_io_private(proc, TRUE); … … 1084 1203 DWORD ierr; 1085 1204 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1205 childhand = (HANDLE) pproc->pid; 1206 1207 /* 1208 * This function is poorly named, and could also be used just to wait 1209 * for child death if you're doing your own pipe I/O. If that is 1210 * the case, close the pipe handles here. 1211 */ 1212 if (pproc->sv_stdin[0]) { 1213 CloseHandle((HANDLE)pproc->sv_stdin[0]); 1214 pproc->sv_stdin[0] = 0; 1215 } 1216 if (pproc->sv_stdout[0]) { 1217 CloseHandle((HANDLE)pproc->sv_stdout[0]); 1218 pproc->sv_stdout[0] = 0; 1219 } 1220 if (pproc->sv_stderr[0]) { 1221 CloseHandle((HANDLE)pproc->sv_stderr[0]); 1222 pproc->sv_stderr[0] = 0; 1223 } 1105 1224 1106 1225 #ifdef KMK … … 1110 1229 #endif 1111 1230 1112 /* 1113 * Wait for the child process to exit it we didn't do that already. 1114 */ 1115 if (fNeedToWait) { 1116 wait_return = WaitForSingleObject(childhand, INFINITE); 1117 if (wait_return != WAIT_OBJECT_0) { 1118 /* map_windows32_error_to_string(GetLastError());*/ 1119 pproc->last_err = GetLastError(); 1120 pproc->lerrno = E_SCALL; 1121 goto done2; 1122 } 1123 } 1124 1125 GetExitCodeResult = GetExitCodeProcess(childhand, &ierr); 1126 if (ierr == CONTROL_C_EXIT) { 1127 pproc->signal = SIGINT; 1128 } else { 1129 pproc->exit_code = ierr; 1130 } 1131 if (GetExitCodeResult == FALSE) { 1132 pproc->last_err = GetLastError(); 1133 pproc->lerrno = E_SCALL; 1134 } 1231 /* 1232 * Wait for the child process to exit 1233 */ 1234 1235 if (fNeedToWait) { /* bird */ 1236 wait_return = WaitForSingleObject(childhand, INFINITE); 1237 1238 if (wait_return != WAIT_OBJECT_0) { 1239 /* map_windows32_error_to_string(GetLastError());*/ 1240 pproc->last_err = GetLastError(); 1241 pproc->lerrno = E_SCALL; 1242 goto done2; 1243 } 1244 } /* bird */ 1245 1246 GetExitCodeResult = GetExitCodeProcess(childhand, &ierr); 1247 if (ierr == CONTROL_C_EXIT) { 1248 pproc->signal = SIGINT; 1249 } else { 1250 pproc->exit_code = ierr; 1251 } 1252 if (GetExitCodeResult == FALSE) { 1253 pproc->last_err = GetLastError(); 1254 pproc->lerrno = E_SCALL; 1255 } 1135 1256 1136 1257 done2: 1137 1138 1139 1140 1258 if (pproc->lerrno) 1259 return(-1); 1260 else 1261 return(0); 1141 1262 1142 1263 } … … 1144 1265 /* 1145 1266 * Description: Clean up any leftover handles, etc. It is up to the 1146 * caller to manage and free the input, ou put, and stderr buffers.1267 * caller to manage and free the input, output, and stderr buffers. 1147 1268 */ 1148 1269 void 1149 1270 process_cleanup( 1150 HANDLE proc) 1151 { 1152 sub_process *pproc = (sub_process *)proc; 1153 int i; 1154 1155 #ifdef KMK 1156 if (pproc->enmType == kRegular) { 1157 #endif 1158 1159 if (pproc->using_pipes) { 1160 for (i= 0; i <= 1; i++) { 1161 if ((HANDLE)pproc->sv_stdin[i]) 1162 CloseHandle((HANDLE)pproc->sv_stdin[i]); 1163 if ((HANDLE)pproc->sv_stdout[i]) 1164 CloseHandle((HANDLE)pproc->sv_stdout[i]); 1165 if ((HANDLE)pproc->sv_stderr[i]) 1166 CloseHandle((HANDLE)pproc->sv_stderr[i]); 1167 } 1168 } 1169 if ((HANDLE)pproc->pid) 1170 CloseHandle((HANDLE)pproc->pid); 1171 #ifdef KMK 1172 } else if (pproc->enmType == kSubmit) { 1173 kSubmitSubProcCleanup(pproc->clue); 1174 } else { 1175 assert(0); 1176 return; 1177 } 1178 pproc->enmType = kSubProcFreed; 1179 #endif 1180 1181 free(pproc); 1271 HANDLE proc) 1272 { 1273 sub_process *pproc = (sub_process *)proc; 1274 int i; 1275 1276 #ifdef KMK 1277 if (pproc->enmType == kRegular) { 1278 #endif 1279 if (pproc->using_pipes) { 1280 for (i= 0; i <= 1; i++) { 1281 if ((HANDLE)pproc->sv_stdin[i] 1282 && (HANDLE)pproc->sv_stdin[i] != INVALID_HANDLE_VALUE) 1283 CloseHandle((HANDLE)pproc->sv_stdin[i]); 1284 if ((HANDLE)pproc->sv_stdout[i] 1285 && (HANDLE)pproc->sv_stdout[i] != INVALID_HANDLE_VALUE) 1286 CloseHandle((HANDLE)pproc->sv_stdout[i]); 1287 if ((HANDLE)pproc->sv_stderr[i] 1288 && (HANDLE)pproc->sv_stderr[i] != INVALID_HANDLE_VALUE) 1289 CloseHandle((HANDLE)pproc->sv_stderr[i]); 1290 } 1291 } 1292 if ((HANDLE)pproc->pid) 1293 CloseHandle((HANDLE)pproc->pid); 1294 #ifdef KMK 1295 } else if (pproc->enmType == kSubmit) { 1296 kSubmitSubProcCleanup(pproc->clue); 1297 } else { 1298 assert(0); 1299 return; 1300 } 1301 pproc->enmType = kSubProcFreed; 1302 #endif 1303 1304 free(pproc); 1182 1305 } 1183 1306 … … 1185 1308 /* 1186 1309 * Description: 1187 * 1310 * Create a command line buffer to pass to CreateProcess 1188 1311 * 1189 1312 * Returns: the buffer or NULL for failure 1190 * 1313 * Shell case: sh_name a:/full/path/to/script argv[1] argv[2] ... 1191 1314 * Otherwise: argv[0] argv[1] argv[2] ... 1192 1315 * … … 1199 1322 make_command_line( char *shell_name, char *full_exec_path, char **argv) 1200 1323 { 1201 intargc = 0;1202 char**argvi;1203 int*enclose_in_quotes = NULL;1204 int*enclose_in_quotes_i;1205 unsigned intbytes_required = 0;1206 char*command_line;1207 char*command_line_i;1208 1209 1210 #undef HAVE_CYGWIN_SHELL 1324 int argc = 0; 1325 char** argvi; 1326 int* enclose_in_quotes = NULL; 1327 int* enclose_in_quotes_i; 1328 unsigned int bytes_required = 0; 1329 char* command_line; 1330 char* command_line_i; 1331 int cygwin_mode = 0; /* HAVE_CYGWIN_SHELL */ 1332 int have_sh = 0; /* HAVE_CYGWIN_SHELL */ 1333 #undef HAVE_CYGWIN_SHELL /* bird: paranoia */ 1211 1334 #ifdef HAVE_CYGWIN_SHELL 1212 have_sh = (shell_name != NULL || strstr(full_exec_path, "sh.exe")); 1213 cygwin_mode = 1; 1214 #endif 1215 1216 if (shell_name && full_exec_path) { 1217 bytes_required 1218 = strlen(shell_name) + 1 + strlen(full_exec_path); 1219 /* 1220 * Skip argv[0] if any, when shell_name is given. 1221 */ 1222 if (*argv) argv++; 1223 /* 1224 * Add one for the intervening space. 1225 */ 1226 if (*argv) bytes_required++; 1227 } 1228 1229 argvi = argv; 1230 while (*(argvi++)) argc++; 1231 1232 if (argc) { 1233 enclose_in_quotes = (int*) calloc(1, argc * sizeof(int)); 1234 1235 if (!enclose_in_quotes) { 1236 return NULL; 1237 } 1238 } 1239 1240 /* We have to make one pass through each argv[i] to see if we need 1241 * to enclose it in ", so we might as well figure out how much 1242 * memory we'll need on the same pass. 1243 */ 1244 1245 argvi = argv; 1246 enclose_in_quotes_i = enclose_in_quotes; 1247 while(*argvi) { 1248 char* p = *argvi; 1249 unsigned int backslash_count = 0; 1250 1251 /* 1252 * We have to enclose empty arguments in ". 1253 */ 1254 if (!(*p)) *enclose_in_quotes_i = 1; 1255 1256 while(*p) { 1257 switch (*p) { 1258 case '\"': 1259 /* 1260 * We have to insert a backslash for each " 1261 * and each \ that precedes the ". 1262 */ 1263 bytes_required += (backslash_count + 1); 1264 backslash_count = 0; 1265 break; 1335 have_sh = (shell_name != NULL || strstr(full_exec_path, "sh.exe")); 1336 cygwin_mode = 1; 1337 #endif 1338 1339 if (shell_name && full_exec_path) { 1340 bytes_required 1341 = strlen(shell_name) + 1 + strlen(full_exec_path); 1342 /* 1343 * Skip argv[0] if any, when shell_name is given. 1344 * The special case of "-c" in full_exec_path means 1345 * argv[0] is not the shell name, but the command string 1346 * to pass to the shell. 1347 */ 1348 if (*argv && strcmp(full_exec_path, "-c")) argv++; 1349 /* 1350 * Add one for the intervening space. 1351 */ 1352 if (*argv) bytes_required++; 1353 } 1354 1355 argvi = argv; 1356 while (*(argvi++)) argc++; 1357 1358 if (argc) { 1359 enclose_in_quotes = (int*) calloc(1, argc * sizeof(int)); 1360 1361 if (!enclose_in_quotes) { 1362 return NULL; 1363 } 1364 } 1365 1366 /* We have to make one pass through each argv[i] to see if we need 1367 * to enclose it in ", so we might as well figure out how much 1368 * memory we'll need on the same pass. 1369 */ 1370 1371 argvi = argv; 1372 enclose_in_quotes_i = enclose_in_quotes; 1373 while(*argvi) { 1374 char* p = *argvi; 1375 unsigned int backslash_count = 0; 1376 1377 /* 1378 * We have to enclose empty arguments in ". 1379 */ 1380 if (!(*p)) *enclose_in_quotes_i = 1; 1381 1382 while(*p) { 1383 switch (*p) { 1384 case '\"': 1385 /* 1386 * We have to insert a backslash for each " 1387 * and each \ that precedes the ". 1388 */ 1389 bytes_required += (backslash_count + 1); 1390 backslash_count = 0; 1391 break; 1266 1392 1267 1393 #if !defined(HAVE_MKS_SHELL) && !defined(HAVE_CYGWIN_SHELL) 1268 1269 1270 1271 #endif 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1394 case '\\': 1395 backslash_count++; 1396 break; 1397 #endif 1398 /* 1399 * At one time we set *enclose_in_quotes_i for '*' or '?' to suppress 1400 * wildcard expansion in programs linked with MSVC's SETARGV.OBJ so 1401 * that argv in always equals argv out. This was removed. Say you have 1402 * such a program named glob.exe. You enter 1403 * glob '*' 1404 * at the sh command prompt. Obviously the intent is to make glob do the 1405 * wildcarding instead of sh. If we set *enclose_in_quotes_i for '*' or '?', 1406 * then the command line that glob would see would be 1407 * glob "*" 1408 * and the _setargv in SETARGV.OBJ would _not_ expand the *. 1409 */ 1410 case ' ': 1411 case '\t': 1412 *enclose_in_quotes_i = 1; 1413 /* fall through */ 1414 1415 default: 1416 backslash_count = 0; 1417 break; 1418 } 1419 1420 /* 1421 * Add one for each character in argv[i]. 1422 */ 1423 bytes_required++; 1424 1425 p++; 1426 } 1427 1428 if (*enclose_in_quotes_i) { 1429 /* 1430 * Add one for each enclosing ", 1431 * and one for each \ that precedes the 1432 * closing ". 1433 */ 1434 bytes_required += (backslash_count + 2); 1435 } 1436 1437 /* 1438 * Add one for the intervening space. 1439 */ 1440 if (*(++argvi)) bytes_required++; 1441 enclose_in_quotes_i++; 1442 } 1443 1444 /* 1445 * Add one for the terminating NULL. 1446 */ 1447 bytes_required++; 1322 1448 #ifdef KMK /* for the space before the final " in case we need it. */ 1323 1449 bytes_required++; 1324 1450 #endif 1325 1451 1326 1327 1328 1329 if (enclose_in_quotes)free(enclose_in_quotes);1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1452 command_line = (char*) malloc(bytes_required); 1453 1454 if (!command_line) { 1455 free(enclose_in_quotes); 1456 return NULL; 1457 } 1458 1459 command_line_i = command_line; 1460 1461 if (shell_name && full_exec_path) { 1462 while(*shell_name) { 1463 *(command_line_i++) = *(shell_name++); 1464 } 1465 1466 *(command_line_i++) = ' '; 1467 1468 while(*full_exec_path) { 1469 *(command_line_i++) = *(full_exec_path++); 1470 } 1471 1472 if (*argv) { 1473 *(command_line_i++) = ' '; 1474 } 1475 } 1476 1477 argvi = argv; 1478 enclose_in_quotes_i = enclose_in_quotes; 1479 1480 while(*argvi) { 1481 char* p = *argvi; 1482 unsigned int backslash_count = 0; 1483 1484 if (*enclose_in_quotes_i) { 1485 *(command_line_i++) = '\"'; 1486 } 1487 1488 while(*p) { 1489 if (*p == '\"') { 1490 if (cygwin_mode && have_sh) { /* HAVE_CYGWIN_SHELL */ 1491 /* instead of a \", cygwin likes "" */ 1492 *(command_line_i++) = '\"'; 1493 } else { 1494 1495 /* 1496 * We have to insert a backslash for the " 1497 * and each \ that precedes the ". 1498 */ 1499 backslash_count++; 1500 1501 while(backslash_count) { 1502 *(command_line_i++) = '\\'; 1503 backslash_count--; 1504 }; 1505 } 1380 1506 #if !defined(HAVE_MKS_SHELL) && !defined(HAVE_CYGWIN_SHELL) 1381 1382 1383 1384 1385 #endif 1386 1387 1388 1389 1390 1391 1392 1393 1394 1507 } else if (*p == '\\') { 1508 backslash_count++; 1509 } else { 1510 backslash_count = 0; 1511 #endif 1512 } 1513 1514 /* 1515 * Copy the character. 1516 */ 1517 *(command_line_i++) = *(p++); 1518 } 1519 1520 if (*enclose_in_quotes_i) { 1395 1521 #if !defined(HAVE_MKS_SHELL) && !defined(HAVE_CYGWIN_SHELL) 1396 1397 1398 1399 1400 1401 1402 1522 /* 1523 * Add one \ for each \ that precedes the 1524 * closing ". 1525 */ 1526 while(backslash_count--) { 1527 *(command_line_i++) = '\\'; 1528 }; 1403 1529 #endif 1404 1530 #ifdef KMK … … 1413 1539 } 1414 1540 #endif 1415 *(command_line_i++) = '\"'; 1416 } 1417 1418 /* 1419 * Append an intervening space. 1420 */ 1421 if (*(++argvi)) { 1422 *(command_line_i++) = ' '; 1423 } 1424 1425 enclose_in_quotes_i++; 1426 } 1427 1428 /* 1429 * Append the terminating NULL. 1430 */ 1431 *command_line_i = '\0'; 1432 1433 if (enclose_in_quotes) free(enclose_in_quotes); 1434 return command_line; 1541 1542 *(command_line_i++) = '\"'; 1543 } 1544 1545 /* 1546 * Append an intervening space. 1547 */ 1548 if (*(++argvi)) { 1549 *(command_line_i++) = ' '; 1550 } 1551 1552 enclose_in_quotes_i++; 1553 } 1554 1555 /* 1556 * Append the terminating NULL. 1557 */ 1558 *command_line_i = '\0'; 1559 1560 free(enclose_in_quotes); 1561 return command_line; 1435 1562 } 1436 1563 … … 1439 1566 * using the default stdin, stdout, and stderr handles. 1440 1567 * Also, register process so that process_wait_for_any_private() 1441 * 1442 * 1568 * can be used via process_file_io(NULL) or 1569 * process_wait_for_any(). 1443 1570 * 1444 1571 * Returns: … … 1448 1575 HANDLE 1449 1576 process_easy( 1450 char **argv, 1451 char **envp) 1452 { 1453 #ifndef KMK 1454 HANDLE hIn; 1455 HANDLE hOut; 1456 HANDLE hErr; 1457 #endif 1458 HANDLE hProcess; 1577 char **argv, 1578 char **envp, 1579 int outfd, 1580 int errfd) 1581 { 1582 HANDLE hIn = INVALID_HANDLE_VALUE; 1583 HANDLE hOut = INVALID_HANDLE_VALUE; 1584 HANDLE hErr = INVALID_HANDLE_VALUE; 1585 HANDLE hProcess, tmpIn, tmpOut, tmpErr; 1586 DWORD e; 1459 1587 1460 1588 if (proc_index >= MAXIMUM_WAIT_OBJECTS) { 1461 1462 1589 DB (DB_JOBS, ("process_easy: All process slots used up\n")); 1590 return INVALID_HANDLE_VALUE; 1463 1591 } 1464 #ifndef KMK 1592 #ifdef KMK /* We can effort here by lettering CreateProcess/kernel do the standard handle duplication. */ 1593 if (outfd != -1 || errfd != -1) { 1594 #endif 1595 /* Standard handles returned by GetStdHandle can be NULL or 1596 INVALID_HANDLE_VALUE if the parent process closed them. If that 1597 happens, we open the null device and pass its handle to 1598 CreateProcess as the corresponding handle to inherit. */ 1599 tmpIn = GetStdHandle(STD_INPUT_HANDLE); 1465 1600 if (DuplicateHandle(GetCurrentProcess(), 1466 GetStdHandle(STD_INPUT_HANDLE),1601 tmpIn, 1467 1602 GetCurrentProcess(), 1468 1603 &hIn, … … 1470 1605 TRUE, 1471 1606 DUPLICATE_SAME_ACCESS) == FALSE) { 1472 fprintf(stderr, 1473 "process_easy: DuplicateHandle(In) failed (e=%ld)\n", 1474 GetLastError()); 1475 return INVALID_HANDLE_VALUE; 1607 if ((e = GetLastError()) == ERROR_INVALID_HANDLE) { 1608 tmpIn = CreateFile("NUL", GENERIC_READ, 1609 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 1610 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 1611 if (tmpIn != INVALID_HANDLE_VALUE 1612 && DuplicateHandle(GetCurrentProcess(), 1613 tmpIn, 1614 GetCurrentProcess(), 1615 &hIn, 1616 0, 1617 TRUE, 1618 DUPLICATE_SAME_ACCESS) == FALSE) 1619 CloseHandle(tmpIn); 1620 } 1621 if (hIn == INVALID_HANDLE_VALUE) { 1622 fprintf(stderr, "process_easy: DuplicateHandle(In) failed (e=%ld)\n", e); 1623 return INVALID_HANDLE_VALUE; 1624 } 1476 1625 } 1626 if (outfd >= 0) 1627 tmpOut = (HANDLE)_get_osfhandle (outfd); 1628 else 1629 tmpOut = GetStdHandle (STD_OUTPUT_HANDLE); 1477 1630 if (DuplicateHandle(GetCurrentProcess(), 1478 GetStdHandle(STD_OUTPUT_HANDLE),1631 tmpOut, 1479 1632 GetCurrentProcess(), 1480 1633 &hOut, … … 1482 1635 TRUE, 1483 1636 DUPLICATE_SAME_ACCESS) == FALSE) { 1484 fprintf(stderr, 1485 "process_easy: DuplicateHandle(Out) failed (e=%ld)\n", 1486 GetLastError()); 1487 return INVALID_HANDLE_VALUE; 1637 if ((e = GetLastError()) == ERROR_INVALID_HANDLE) { 1638 tmpOut = CreateFile("NUL", GENERIC_WRITE, 1639 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 1640 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 1641 if (tmpOut != INVALID_HANDLE_VALUE 1642 && DuplicateHandle(GetCurrentProcess(), 1643 tmpOut, 1644 GetCurrentProcess(), 1645 &hOut, 1646 0, 1647 TRUE, 1648 DUPLICATE_SAME_ACCESS) == FALSE) 1649 CloseHandle(tmpOut); 1650 } 1651 if (hOut == INVALID_HANDLE_VALUE) { 1652 fprintf(stderr, "process_easy: DuplicateHandle(Out) failed (e=%ld)\n", e); 1653 return INVALID_HANDLE_VALUE; 1654 } 1488 1655 } 1656 if (errfd >= 0) 1657 tmpErr = (HANDLE)_get_osfhandle (errfd); 1658 else 1659 tmpErr = GetStdHandle(STD_ERROR_HANDLE); 1489 1660 if (DuplicateHandle(GetCurrentProcess(), 1490 GetStdHandle(STD_ERROR_HANDLE),1661 tmpErr, 1491 1662 GetCurrentProcess(), 1492 1663 &hErr, … … 1494 1665 TRUE, 1495 1666 DUPLICATE_SAME_ACCESS) == FALSE) { 1496 fprintf(stderr, 1497 "process_easy: DuplicateHandle(Err) failed (e=%ld)\n", 1498 GetLastError()); 1499 return INVALID_HANDLE_VALUE; 1667 if ((e = GetLastError()) == ERROR_INVALID_HANDLE) { 1668 tmpErr = CreateFile("NUL", GENERIC_WRITE, 1669 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 1670 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 1671 if (tmpErr != INVALID_HANDLE_VALUE 1672 && DuplicateHandle(GetCurrentProcess(), 1673 tmpErr, 1674 GetCurrentProcess(), 1675 &hErr, 1676 0, 1677 TRUE, 1678 DUPLICATE_SAME_ACCESS) == FALSE) 1679 CloseHandle(tmpErr); 1680 } 1681 if (hErr == INVALID_HANDLE_VALUE) { 1682 fprintf(stderr, "process_easy: DuplicateHandle(Err) failed (e=%ld)\n", e); 1683 return INVALID_HANDLE_VALUE; 1684 } 1500 1685 } 1686 #ifdef KMK /* saving effort */ 1687 } 1688 #endif 1501 1689 1502 1690 hProcess = process_init_fd(hIn, hOut, hErr); 1503 #else1504 hProcess = process_init_fd(0, 0, 0);1505 #endif /* !KMK */1506 1691 1507 1692 if (process_begin(hProcess, argv, envp, argv[0], NULL)) { … … 1515 1700 ((sub_process*) hProcess)->exit_code = process_last_err(hProcess); 1516 1701 1517 #ifndef KMK1518 1702 /* close up unused handles */ 1519 CloseHandle(hIn); 1520 CloseHandle(hOut); 1521 CloseHandle(hErr); 1522 #endif 1703 if (hIn != INVALID_HANDLE_VALUE) 1704 CloseHandle(hIn); 1705 if (hOut != INVALID_HANDLE_VALUE) 1706 CloseHandle(hOut); 1707 if (hErr != INVALID_HANDLE_VALUE) 1708 CloseHandle(hErr); 1523 1709 } 1524 1710 -
trunk/src/kmk/w32/subproc/w32err.c
r2591 r3140 1 1 /* Error handling for Windows 2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. 2 Copyright (C) 1996-2016 Free Software Foundation, Inc. 4 3 This file is part of GNU Make. 5 4 … … 16 15 this program. If not, see <http://www.gnu.org/licenses/>. */ 17 16 17 #include <stdlib.h> 18 18 #include <windows.h> 19 #include "makeint.h" 19 20 #include "w32err.h" 20 21 … … 27 28 * comp.os.ms-windows.programmer.win32 28 29 */ 29 c har *30 const char * 30 31 map_windows32_error_to_string (DWORD ercode) { 31 /* __declspec (thread) necessary if you will use multiple threads on MSVC */ 32 #ifdef _MSC_VER 33 __declspec (thread) static char szMessageBuffer[128]; 34 #else 35 static char szMessageBuffer[128]; 36 #endif 37 /* Fill message buffer with a default message in 38 * case FormatMessage fails 39 */ 32 /* 33 * We used to have an MSVC-specific '__declspec (thread)' qualifier 34 * here, with the following comment: 35 * 36 * __declspec (thread) necessary if you will use multiple threads on MSVC 37 * 38 * However, Make was never multithreaded on Windows (except when 39 * Ctrl-C is hit, in which case the main thread is stopped 40 * immediately, so it doesn't matter in this context). The functions 41 * on sub_proc.c that started and stopped additional threads were 42 * never used, and are now #ifdef'ed away. Until we need more than 43 * one thread, we have no problems with the following buffer being 44 * static. (If and when we do need it to be in thread-local storage, 45 * the corresponding GCC qualifier is '__thread'.) 46 */ 47 static char szMessageBuffer[128]; 48 /* Fill message buffer with a default message in 49 * case FormatMessage fails 50 */ 40 51 wsprintf (szMessageBuffer, "Error %ld\n", ercode); 41 52 42 /* 43 * Special code for winsock error handling. 44 */ 45 if (ercode > WSABASEERR) { 46 HMODULE hModule = GetModuleHandle("wsock32"); 47 if (hModule != NULL) { 48 FormatMessage(FORMAT_MESSAGE_FROM_HMODULE, 49 hModule, 50 ercode, 51 LANG_NEUTRAL, 52 szMessageBuffer, 53 sizeof(szMessageBuffer), 54 NULL); 55 FreeLibrary(hModule); 56 } 57 } else { 58 /* 59 * Default system message handling 60 */ 61 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 62 NULL, 63 ercode, 64 LANG_NEUTRAL, 65 szMessageBuffer, 66 sizeof(szMessageBuffer), 67 NULL); 68 } 53 /* 54 * Special code for winsock error handling. 55 */ 56 if (ercode > WSABASEERR) { 57 #if 0 58 HMODULE hModule = GetModuleHandle("wsock32"); 59 if (hModule != NULL) { 60 FormatMessage(FORMAT_MESSAGE_FROM_HMODULE, 61 hModule, 62 ercode, 63 LANG_NEUTRAL, 64 szMessageBuffer, 65 sizeof(szMessageBuffer), 66 NULL); 67 FreeLibrary(hModule); 68 } 69 #else 70 O (fatal, NILF, szMessageBuffer); 71 #endif 72 } else { 73 /* 74 * Default system message handling 75 */ 76 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 77 NULL, 78 ercode, 79 LANG_NEUTRAL, 80 szMessageBuffer, 81 sizeof(szMessageBuffer), 82 NULL); 83 } 69 84 return szMessageBuffer; 70 85 } -
trunk/src/kmk/w32/w32os.c
r3139 r3140 169 169 jobserver_acquire (int timeout) 170 170 { 171 HANDLE handles[MAXIMUM_WAIT_OBJECTS ];171 HANDLE handles[MAXIMUM_WAIT_OBJECTS + 1]; /* bird: + 1 to prevent trashing the stack. */ 172 172 DWORD dwHandleCount; 173 173 DWORD dwEvent;
Note:
See TracChangeset
for help on using the changeset viewer.