Skip to content

Commit fae4283

Browse files
committed
patch 8.0.0839: cannot kill a job in a terminal with CTRL-C
Problem: Cannot kill a job in a terminal with CTRL-C. Solution: Set the controlling tty and send SIGINT. (closes #1910)
1 parent 94053a5 commit fae4283

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

src/os_unix.c

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4194,6 +4194,26 @@ open_pty(int *pty_master_fd, int *pty_slave_fd)
41944194
}
41954195
#endif
41964196

4197+
/*
4198+
* Send SIGINT to a child process if "c" is an interrupt character.
4199+
*/
4200+
void
4201+
may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4202+
{
4203+
# ifdef SIGINT
4204+
if (c == Ctrl_C || c == intr_char)
4205+
{
4206+
# ifdef HAVE_SETSID
4207+
kill(-pid, SIGINT);
4208+
# else
4209+
kill(0, SIGINT);
4210+
# endif
4211+
if (wpid > 0)
4212+
kill(wpid, SIGINT);
4213+
}
4214+
# endif
4215+
}
4216+
41974217
int
41984218
mch_call_shell(
41994219
char_u *cmd,
@@ -4765,23 +4785,12 @@ mch_call_shell(
47654785
*/
47664786
if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
47674787
{
4768-
# ifdef SIGINT
47694788
/*
47704789
* Send SIGINT to the child's group or all
47714790
* processes in our group.
47724791
*/
4773-
if (ta_buf[ta_len] == Ctrl_C
4774-
|| ta_buf[ta_len] == intr_char)
4775-
{
4776-
# ifdef HAVE_SETSID
4777-
kill(-pid, SIGINT);
4778-
# else
4779-
kill(0, SIGINT);
4780-
# endif
4781-
if (wpid > 0)
4782-
kill(wpid, SIGINT);
4783-
}
4784-
# endif
4792+
may_send_sigint(ta_buf[ta_len], pid, wpid);
4793+
47854794
if (pty_master_fd < 0 && toshell_fd >= 0
47864795
&& ta_buf[ta_len] == Ctrl_D)
47874796
{
@@ -5360,15 +5369,26 @@ mch_job_start(char **argv, job_T *job, jobopt_T *options)
53605369
if (null_fd >= 0)
53615370
close(null_fd);
53625371

5372+
if (pty_slave_fd >= 0)
5373+
{
5374+
/* push stream discipline modules */
5375+
SetupSlavePTY(pty_slave_fd);
5376+
# ifdef TIOCSCTTY
5377+
/* Try to become controlling tty (probably doesn't work,
5378+
* unless run by root) */
5379+
ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5380+
# endif
5381+
}
5382+
53635383
/* See above for type of argv. */
53645384
execvp(argv[0], argv);
53655385

53665386
if (stderr_works)
53675387
perror("executing job failed");
5368-
#ifdef EXITFREE
5388+
# ifdef EXITFREE
53695389
/* calling free_all_mem() here causes problems. Ignore valgrind
53705390
* reporting possibly leaked memory. */
5371-
#endif
5391+
# endif
53725392
_exit(EXEC_FAILED); /* exec failed, return failure code */
53735393
}
53745394

src/proto/os_unix.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ int mch_report_winsize(int fd, int rows, int cols);
5757
void mch_set_shellsize(void);
5858
void mch_new_shellsize(void);
5959
int mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc);
60+
void may_send_sigint(int c, pid_t pid, pid_t wpid);
6061
int mch_call_shell(char_u *cmd, int options);
6162
void mch_job_start(char **argv, job_T *job, jobopt_T *options);
6263
char *mch_job_status(job_T *job);

src/terminal.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
* - don't allow exiting Vim when a terminal is still running a job
4040
* - in bash mouse clicks are inserting characters.
4141
* - mouse scroll: when over other window, scroll that window.
42-
* - typing CTRL-C is not sent to the terminal. need to setup controlling tty?
43-
* #1910
4442
* - For the scrollback buffer store lines in the buffer, only attributes in
4543
* tl_scrollback.
4644
* - When the job ends:
@@ -962,6 +960,17 @@ terminal_loop(void)
962960
/* job finished while waiting for a character */
963961
break;
964962

963+
#ifdef UNIX
964+
may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
965+
#endif
966+
#ifdef WIN3264
967+
if (c == Ctrl_C)
968+
/* We don't know if the job can handle CTRL-C itself or not, this
969+
* may kill the shell instead of killing the command running in the
970+
* shell. */
971+
mch_stop_job(curbuf->b_term->tl_job, "quit")
972+
#endif
973+
965974
if (c == (termkey == 0 ? Ctrl_W : termkey))
966975
{
967976
int prev_c = c;

src/version.c

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

770770
static int included_patches[] =
771771
{ /* Add new patch number below this line */
772+
/**/
773+
839,
772774
/**/
773775
838,
774776
/**/

0 commit comments

Comments
 (0)