~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/sftp.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-04-20 07:46:48 UTC
  • mfrom: (1666.1.14 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060420074648-a9cefa72a3a9a5e8
Change SFTP subprocess handling so that Ctrl-C does not cause stale locks in SFTP accessed repositories.

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
register_urlparse_netloc_protocol('sftp')
66
66
 
67
67
 
 
68
def os_specific_subprocess_params():
 
69
    """Get O/S specific subprocess parameters."""
 
70
    if sys.platform == 'win32':
 
71
        # setting the process group and closing fds is not supported on 
 
72
        # win32
 
73
        return {}
 
74
    else:
 
75
        # we close fds as the child process does not need them to be open.
 
76
        # we set the process group so that signals from the keyboard like
 
77
        # 'SIGINT' - KeyboardInterrupt - are not recieved in the child procecss
 
78
        # if we do not do this, then the sftp/ssh subprocesses will terminate 
 
79
        # when a user hits CTRL-C, and we are unable to use them to unlock the
 
80
        # remote branch/repository etc.
 
81
        return {'preexec_fn': os.setpgrp,
 
82
                'close_fds': True,
 
83
                }
 
84
 
 
85
 
68
86
# don't use prefetch unless paramiko version >= 1.5.2 (there were bugs earlier)
69
87
_default_do_prefetch = False
70
88
if getattr(paramiko, '__version_info__', (0, 0, 0)) >= (1, 5, 5):
71
89
    _default_do_prefetch = True
72
90
 
73
91
 
74
 
_close_fds = True
75
 
if sys.platform == 'win32':
76
 
    # close_fds not supported on win32
77
 
    _close_fds = False
78
 
 
79
92
_ssh_vendor = None
80
 
 
81
93
def _get_ssh_vendor():
82
94
    """Find out what version of SSH is on the system."""
83
95
    global _ssh_vendor
94
106
 
95
107
    try:
96
108
        p = subprocess.Popen(['ssh', '-V'],
97
 
                             close_fds=_close_fds,
98
109
                             stdin=subprocess.PIPE,
99
110
                             stdout=subprocess.PIPE,
100
 
                             stderr=subprocess.PIPE)
 
111
                             stderr=subprocess.PIPE,
 
112
                             **os_specific_subprocess_params())
101
113
        returncode = p.returncode
102
114
        stdout, stderr = p.communicate()
103
115
    except OSError:
142
154
                args.extend(['-l', user])
143
155
            args.extend(['-s', 'sftp', hostname])
144
156
 
145
 
        self.proc = subprocess.Popen(args, close_fds=_close_fds,
 
157
        self.proc = subprocess.Popen(args,
146
158
                                     stdin=subprocess.PIPE,
147
 
                                     stdout=subprocess.PIPE)
 
159
                                     stdout=subprocess.PIPE,
 
160
                                     **os_specific_subprocess_params())
148
161
 
149
162
    def send(self, data):
150
163
        return os.write(self.proc.stdin.fileno(), data)