Skip to content

Commit 76467df

Browse files
committed
patch 7.4.1306
Problem: Job control doesn't work well on MS-Windows. Solution: Various fixes. (Ken Takata, Ozaki Kiichi , Yukihiro Nakadaira, Yasuhiro Matsumoto)
1 parent 7c29f38 commit 76467df

8 files changed

Lines changed: 105 additions & 24 deletions

File tree

src/Make_mvc.mak

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
# Processor Version: CPUNR=[i386, i486, i586, i686, pentium4] (default is
114114
# i386)
115115
#
116-
# Version Support: WINVER=[0x0400, 0x0500] (default is 0x0400)
116+
# Version Support: WINVER=[0x0400, 0x0500] (default is 0x0500)
117117
#
118118
# Debug version: DEBUG=yes
119119
# Mapfile: MAP=[no, yes or lines] (default is yes)
@@ -370,9 +370,8 @@ CON_LIB = $(CON_LIB) /DELAYLOAD:comdlg32.dll /DELAYLOAD:ole32.dll DelayImp.lib
370370
!endif
371371

372372
### Set the default $(WINVER) to make it work with VC++7.0 (VS.NET)
373-
# When set to 0x0500 ":browse" stops working.
374373
!ifndef WINVER
375-
WINVER = 0x0400
374+
WINVER = 0x0500
376375
!endif
377376

378377
# If you have a fixed directory for $VIM or $VIMRUNTIME, other than the normal

src/eval.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7720,8 +7720,7 @@ get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
77207720
static void
77217721
job_free(job_T *job)
77227722
{
7723-
/* TODO: free any handles */
7724-
7723+
mch_clear_job(job);
77257724
vim_free(job);
77267725
}
77277726

@@ -14369,9 +14368,11 @@ f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
1436914368
s = vim_strsave_shellescape(s, FALSE, TRUE);
1437014369
if (s == NULL)
1437114370
goto theend;
14371+
ga_concat(&ga, s);
14372+
vim_free(s);
1437214373
}
14373-
ga_concat(&ga, s);
14374-
vim_free(s);
14374+
else
14375+
ga_concat(&ga, s);
1437514376
if (li->li_next != NULL)
1437614377
ga_append(&ga, ' ');
1437714378
#endif
@@ -21623,7 +21624,8 @@ get_tv_string_buf_chk(typval_T *varp, char_u *buf)
2162321624
"process %ld %s", (long)job->jv_pid, status);
2162421625
# elif defined(WIN32)
2162521626
vim_snprintf((char *)buf, NUMBUFLEN,
21626-
"process %ld %s", (long)job->jv_pi.dwProcessId,
21627+
"process %ld %s",
21628+
(long)job->jv_proc_info.dwProcessId,
2162721629
status);
2162821630
# else
2162921631
/* fall-back */

src/os_unix.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5092,13 +5092,20 @@ mch_job_status(job_T *job)
50925092
job->jv_status = JOB_ENDED;
50935093
return "dead";
50945094
}
5095+
if (WIFSIGNALED(status))
5096+
{
5097+
job->jv_exitval = -1;
5098+
job->jv_status = JOB_ENDED;
5099+
return "dead";
5100+
}
50955101
return "run";
50965102
}
50975103

50985104
int
50995105
mch_stop_job(job_T *job, char_u *how)
51005106
{
51015107
int sig = -1;
5108+
pid_t job_pid;
51025109

51035110
if (STRCMP(how, "hup") == 0)
51045111
sig = SIGHUP;
@@ -5112,10 +5119,30 @@ mch_stop_job(job_T *job, char_u *how)
51125119
sig = atoi((char *)how);
51135120
else
51145121
return FAIL;
5122+
51155123
/* TODO: have an option to only kill the process, not the group? */
5116-
kill(-job->jv_pid, sig);
5124+
job_pid = job->jv_pid;
5125+
if (job_pid == getpgid(job_pid))
5126+
job_pid = -job_pid;
5127+
5128+
kill(job_pid, sig);
5129+
51175130
return OK;
51185131
}
5132+
5133+
/*
5134+
* Clear the data related to "job".
5135+
*/
5136+
void
5137+
mch_clear_job(job_T *job)
5138+
{
5139+
/* call waitpid because child process may become zombie */
5140+
# ifdef __NeXT__
5141+
wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
5142+
# else
5143+
waitpid(job->jv_pid, NULL, WNOHANG);
5144+
# endif
5145+
}
51195146
#endif
51205147

51215148
/*

src/os_win32.c

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5038,19 +5038,44 @@ mch_start_job(char *cmd, job_T *job)
50385038
{
50395039
STARTUPINFO si;
50405040
PROCESS_INFORMATION pi;
5041+
HANDLE jo;
50415042

5043+
jo = CreateJobObject(NULL, NULL);
5044+
if (jo == NULL)
5045+
{
5046+
job->jv_status = JOB_FAILED;
5047+
return;
5048+
}
5049+
5050+
ZeroMemory(&pi, sizeof(pi));
50425051
ZeroMemory(&si, sizeof(si));
50435052
si.cb = sizeof(si);
5053+
si.dwFlags = STARTF_USESHOWWINDOW;
5054+
si.wShowWindow = SW_HIDE;
50445055

50455056
if (!vim_create_process(cmd, FALSE,
5057+
CREATE_SUSPENDED |
50465058
CREATE_DEFAULT_ERROR_MODE |
50475059
CREATE_NEW_PROCESS_GROUP |
5048-
CREATE_NO_WINDOW,
5060+
CREATE_NEW_CONSOLE,
50495061
&si, &pi))
5062+
{
5063+
CloseHandle(jo);
50505064
job->jv_status = JOB_FAILED;
5065+
}
50515066
else
50525067
{
5053-
job->jv_pi = pi;
5068+
if (!AssignProcessToJobObject(jo, pi.hProcess))
5069+
{
5070+
/* if failing, switch the way to terminate
5071+
* process with TerminateProcess. */
5072+
CloseHandle(jo);
5073+
jo = NULL;
5074+
}
5075+
ResumeThread(pi.hThread);
5076+
CloseHandle(job->jv_proc_info.hThread);
5077+
job->jv_proc_info = pi;
5078+
job->jv_job_object = jo;
50545079
job->jv_status = JOB_STARTED;
50555080
}
50565081
}
@@ -5060,12 +5085,10 @@ mch_job_status(job_T *job)
50605085
{
50615086
DWORD dwExitCode = 0;
50625087

5063-
if (!GetExitCodeProcess(job->jv_pi.hProcess, &dwExitCode))
5064-
return "dead";
5065-
if (dwExitCode != STILL_ACTIVE)
5088+
if (!GetExitCodeProcess(job->jv_proc_info.hProcess, &dwExitCode)
5089+
|| dwExitCode != STILL_ACTIVE)
50665090
{
5067-
CloseHandle(job->jv_pi.hProcess);
5068-
CloseHandle(job->jv_pi.hThread);
5091+
job->jv_status = JOB_ENDED;
50695092
return "dead";
50705093
}
50715094
return "run";
@@ -5074,14 +5097,39 @@ mch_job_status(job_T *job)
50745097
int
50755098
mch_stop_job(job_T *job, char_u *how)
50765099
{
5100+
int ret = 0;
5101+
int ctrl_c = STRCMP(how, "int") == 0;
5102+
50775103
if (STRCMP(how, "kill") == 0)
5078-
TerminateProcess(job->jv_pi.hProcess, 0);
5079-
else
5080-
return GenerateConsoleCtrlEvent(
5081-
STRCMP(how, "hup") == 0 ?
5082-
CTRL_BREAK_EVENT : CTRL_C_EVENT,
5083-
job->jv_pi.dwProcessId) ? OK : FAIL;
5084-
return OK;
5104+
{
5105+
if (job->jv_job_object != NULL)
5106+
return TerminateJobObject(job->jv_job_object, 0) ? OK : FAIL;
5107+
else
5108+
return TerminateProcess(job->jv_proc_info.hProcess, 0) ? OK : FAIL;
5109+
}
5110+
5111+
if (!AttachConsole(job->jv_proc_info.dwProcessId))
5112+
return FAIL;
5113+
ret = GenerateConsoleCtrlEvent(
5114+
ctrl_c ? CTRL_C_EVENT : CTRL_BREAK_EVENT,
5115+
job->jv_proc_info.dwProcessId)
5116+
? OK : FAIL;
5117+
FreeConsole();
5118+
return ret;
5119+
}
5120+
5121+
/*
5122+
* Clear the data related to "job".
5123+
*/
5124+
void
5125+
mch_clear_job(job_T *job)
5126+
{
5127+
if (job->jv_status != JOB_FAILED)
5128+
{
5129+
if (job->jv_job_object != NULL)
5130+
CloseHandle(job->jv_job_object);
5131+
CloseHandle(job->jv_proc_info.hProcess);
5132+
}
50855133
}
50865134
#endif
50875135

src/proto/os_unix.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ int mch_call_shell(char_u *cmd, int options);
6060
void mch_start_job(char **argv, job_T *job);
6161
char *mch_job_status(job_T *job);
6262
int mch_stop_job(job_T *job, char_u *how);
63+
void mch_clear_job(job_T *job);
6364
void mch_breakcheck(void);
6465
int mch_expandpath(garray_T *gap, char_u *path, int flags);
6566
int mch_expand_wildcards(int num_pat, char_u **pat, int *num_file, char_u ***file, int flags);

src/proto/os_win32.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ int mch_call_shell(char_u *cmd, int options);
4343
void mch_start_job(char *cmd, job_T *job);
4444
char *mch_job_status(job_T *job);
4545
int mch_stop_job(job_T *job, char_u *how);
46+
void mch_clear_job(job_T *job);
4647
void mch_set_normal_colors(void);
4748
void mch_write(char_u *s, int len);
4849
void mch_delay(long msec, int ignoreinput);

src/structs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,8 @@ struct jobvar_S
12491249
int jv_exitval;
12501250
#endif
12511251
#ifdef WIN32
1252-
PROCESS_INFORMATION jv_pi;
1252+
PROCESS_INFORMATION jv_proc_info;
1253+
HANDLE jv_job_object;
12531254
#endif
12541255
jobstatus_T jv_status;
12551256

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,8 @@ static char *(features[]) =
747747

748748
static int included_patches[] =
749749
{ /* Add new patch number below this line */
750+
/**/
751+
1306,
750752
/**/
751753
1305,
752754
/**/

0 commit comments

Comments
 (0)