comparison src/os_win32.c @ 8483:7376d36395f0 v7.4.1532

commit https://github.com/vim/vim/commit/7bffaa9f9b477969d85cef41adeadc4506373708 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Mar 10 21:46:03 2016 +0100 patch 7.4.1532 Problem: MS-Windows channel leaks file descriptor. Solution: Use CreateFile with the right options. (Yasuhiro Matsumoto)
author Christian Brabandt <cb@256bit.org>
date Thu, 10 Mar 2016 22:00:05 +0100
parents 9f63e4506c40
children daebcbd87bd3
comparison
equal deleted inserted replaced
8482:b0e1295c1edf 8483:7376d36395f0
4990 4990
4991 return x; 4991 return x;
4992 } 4992 }
4993 4993
4994 #if defined(FEAT_JOB) || defined(PROTO) 4994 #if defined(FEAT_JOB) || defined(PROTO)
4995 static HANDLE
4996 job_io_file_open(
4997 char_u *fname,
4998 DWORD dwDesiredAccess,
4999 DWORD dwShareMode,
5000 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
5001 DWORD dwCreationDisposition,
5002 DWORD dwFlagsAndAttributes)
5003 {
5004 HANDLE h;
5005 # ifdef FEAT_MBYTE
5006 WCHAR *wn = NULL;
5007 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
5008 {
5009 wn = enc_to_utf16(fname, NULL);
5010 if (wn != NULL)
5011 {
5012 h = CreateFileW(wn, dwDesiredAccess, dwShareMode,
5013 lpSecurityAttributes, dwCreationDisposition,
5014 dwFlagsAndAttributes, NULL);
5015 vim_free(wn);
5016 if (h == INVALID_HANDLE_VALUE
5017 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
5018 wn = NULL;
5019 }
5020 }
5021 if (wn == NULL)
5022 # endif
5023
5024 h = CreateFile((LPCSTR)fname, dwDesiredAccess, dwShareMode,
5025 lpSecurityAttributes, dwCreationDisposition,
5026 dwFlagsAndAttributes, NULL);
5027 return h;
5028 }
5029
4995 void 5030 void
4996 mch_start_job(char *cmd, job_T *job, jobopt_T *options) 5031 mch_start_job(char *cmd, job_T *job, jobopt_T *options)
4997 { 5032 {
4998 STARTUPINFO si; 5033 STARTUPINFO si;
4999 PROCESS_INFORMATION pi; 5034 PROCESS_INFORMATION pi;
5044 5079
5045 if (use_file_for_in) 5080 if (use_file_for_in)
5046 { 5081 {
5047 char_u *fname = options->jo_io_name[PART_IN]; 5082 char_u *fname = options->jo_io_name[PART_IN];
5048 5083
5049 int fd = mch_open((char *)fname, O_RDONLY, 0); 5084 ifd[0] = job_io_file_open(fname, GENERIC_READ,
5050 if (fd < 0) 5085 FILE_SHARE_READ | FILE_SHARE_WRITE,
5086 &saAttr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
5087 if (ifd[0] == INVALID_HANDLE_VALUE)
5051 { 5088 {
5052 EMSG2(_(e_notopen), fname); 5089 EMSG2(_(e_notopen), fname);
5053 goto failed; 5090 goto failed;
5054 } 5091 }
5055 ifd[0] = (HANDLE)_get_osfhandle(fd);
5056 } 5092 }
5057 else if (!use_null_for_in && 5093 else if (!use_null_for_in &&
5058 (!CreatePipe(&ifd[0], &ifd[1], &saAttr, 0) 5094 (!CreatePipe(&ifd[0], &ifd[1], &saAttr, 0)
5059 || !pSetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0))) 5095 || !pSetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0)))
5060 goto failed; 5096 goto failed;
5061 5097
5062 if (use_file_for_out) 5098 if (use_file_for_out)
5063 { 5099 {
5064 char_u *fname = options->jo_io_name[PART_OUT]; 5100 char_u *fname = options->jo_io_name[PART_OUT];
5065 5101
5066 int fd = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644); 5102 ofd[1] = job_io_file_open(fname, GENERIC_WRITE,
5067 if (fd < 0) 5103 FILE_SHARE_READ | FILE_SHARE_WRITE,
5104 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5105 if (ofd[1] == INVALID_HANDLE_VALUE)
5068 { 5106 {
5069 EMSG2(_(e_notopen), fname); 5107 EMSG2(_(e_notopen), fname);
5070 goto failed; 5108 goto failed;
5071 } 5109 }
5072 ofd[1] = (HANDLE)_get_osfhandle(fd);
5073 } 5110 }
5074 else if (!use_null_for_out && 5111 else if (!use_null_for_out &&
5075 (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0) 5112 (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0)
5076 || !pSetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0))) 5113 || !pSetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0)))
5077 goto failed; 5114 goto failed;
5078 5115
5079 if (use_file_for_err) 5116 if (use_file_for_err)
5080 { 5117 {
5081 char_u *fname = options->jo_io_name[PART_ERR]; 5118 char_u *fname = options->jo_io_name[PART_ERR];
5082 5119
5083 int fd = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600); 5120 efd[1] = job_io_file_open(fname, GENERIC_WRITE,
5084 if (fd < 0) 5121 FILE_SHARE_READ | FILE_SHARE_WRITE,
5122 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5123 if (efd[1] == INVALID_HANDLE_VALUE)
5085 { 5124 {
5086 EMSG2(_(e_notopen), fname); 5125 EMSG2(_(e_notopen), fname);
5087 goto failed; 5126 goto failed;
5088 } 5127 }
5089 efd[1] = (HANDLE)_get_osfhandle(fd);
5090 } 5128 }
5091 else if (!use_out_for_err && !use_null_for_err && 5129 else if (!use_out_for_err && !use_null_for_err &&
5092 (!CreatePipe(&efd[0], &efd[1], &saAttr, 0) 5130 (!CreatePipe(&efd[0], &efd[1], &saAttr, 0)
5093 || !pSetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0))) 5131 || !pSetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0)))
5094 goto failed; 5132 goto failed;