Bug 241 - When I kill scp, the underlying ssh child process remains alive
Summary: When I kill scp, the underlying ssh child process remains alive
Status: CLOSED FIXED
Alias: None
Product: Portable OpenSSH
Classification: Unclassified
Component: scp (show other bugs)
Version: -current
Hardware: All Linux
: P3 minor
Assignee: OpenSSH Bugzilla mailing list
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-05-14 11:26 AEST by edo biagioni
Modified: 2004-04-14 12:24 AEST (History)
0 users

See Also:


Attachments
diff -u for the bug, as requested by djm (1.09 KB, patch)
2003-06-03 11:55 AEST, edo biagioni
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description edo biagioni 2002-05-14 11:26:56 AEST
I am trying to use scp in an automated script, and would like to be
able to kill it (automatically) if there is a timeout.  However, when
I kill the scp process, the ssh process that scp created continues to
run.  This causes problems with my automated system -- the ssh
processes take a long time to time out or complete, and sometimes
complete inappropriately.


Fix: I enclose some diffs that work on linux.  I have only caught
SIGTERM -- a more complete implementation might catch other signals as
well (maybe at least SIGINT).

The /*esb*/ comments are for my reference only and may be removed -- I
do not need to be credited.  The version openssh-3.1p1x is the one
with my changes.  An acknowledgement that this message has reached the
correct individual(s) would be appreciated (your listed address,
openssh@openssh.com, does not appear to accept mail).

Many thanks for your excellent software!

    edo  -- esb@hawaii.edu

edo@maru 36-> diff -c openssh-3.1p1*/scp.c
*** openssh-3.1p1/scp.c Tue Mar  5 08:59:45 2002
--- openssh-3.1p1x/scp.c        Sat May  4 15:42:50 2002
***************
*** 133,138 ****
--- 133,142 ----
  /* This is the program to execute for the secured connection. ("ssh" 
or -S) */
  char *ssh_program = _PATH_SSH_PROGRAM;
 
+ /* esb: added the child PID so we can kill it if we get killed */
+ pid_t childpid;
+ static void killchild(int signo);
+
  /*
   * This function executes the given command as the specified user on the
   * given host.  This returns < 0 if execution fails, and >= 0 otherwise. This
***************
*** 167,173 ****
        close(reserved[1]);
 
        /* For a child to execute the command on the remote host using ssh. */
!       if (fork() == 0)  {
                /* Child. */
                close(pin[1]);
                close(pout[0]);
--- 171,178 ----
        close(reserved[1]);
 
        /* For a child to execute the command on the remote host using ssh. */
!       /* esb */
!       if ((childpid = fork()) == 0)  {
                /* Child. */
                close(pin[1]);
                close(pout[0]);
***************
*** 191,196 ****
--- 196,203 ----
        *fdout = pin[1];
        close(pout[1]);
        *fdin = pout[0];
+       /* esb */
+       (void) signal(SIGTERM, killchild);
        return 0;
  }
 
***************
*** 1086,1091 ****
--- 1093,1105 ----
        signal(SIGALRM, updateprogressmeter);
        alarm(PROGRESSTIME);
        errno = save_errno;
+ }
+
+ static void
+ killchild(int signo)
+ {
+         kill (childpid, signo);
+       _exit(1);
  }
 
  static int
Comment 1 Damien Miller 2003-05-16 13:16:52 AEST
Could you please attach you patch in unified "diff -u" format to the bug, rather
than pasting it into the comments field? Bugs in comments fields usually have to
be manually reconstructed.
Comment 2 edo biagioni 2003-06-03 11:55:08 AEST
Created attachment 317 [details]
diff -u for the bug, as requested by djm
Comment 3 Damien Miller 2003-06-04 22:41:03 AEST
patch applied, thanks. 

(a similar one for sftp too)
Comment 4 Damien Miller 2004-04-14 12:24:18 AEST
Mass change of RESOLVED bugs to CLOSED