Changeset 1213

Show
Ignore:
Timestamp:
10/16/09 03:17:43 (5 weeks ago)
Author:
vasi
Message:

No zombies

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • bin/sshjour

    r1211 r1213  
    11#!/usr/bin/ruby 
    22 
     3# Bonjour host + port; fallback host + port 
    34host, port, alternative, altport = ARGV 
    45host = host + '.local' 
    56 
     7# Default to fallback 
    68found, fport = alternative, altport 
    79 
     
    911IO.popen('-') do |pipe| 
    1012        if pipe # parent 
    11                 fork { sleep 1; Process.kill 'ALRM', pipe.pid rescue nil } # timeout 
     13                # Timeout if reading hangs 
     14                alarm = fork do 
     15                        begin 
     16                                sleep 1 
     17                                Process.kill 'ALRM', pipe.pid 
     18                        rescue Exception => e 
     19                                # That's ok! 
     20                        end 
     21                end 
    1222                key = pipe.read 
    1323                 
     24                # No zombie 
     25                Process.kill('INT', alarm) 
     26                Process.wait(alarm) 
     27                 
     28                # Did we succeed, or timeout, or what? 
    1429                Process.wait(pipe.pid) 
    1530                if $?.success? 
     31                        # Is it the key we're looking for? 
    1632                        known = IO.readlines(ENV['HOME'] + '/.ssh/known_hosts') 
    1733                        found, fport = host, port if known.include?(key) 
    1834                end 
    19         else 
     35        else # child 
    2036                $stderr.close 
    2137                exec 'ssh-keyscan', host 
     
    2844end 
    2945 
    30 #puts found 
     46# Make ourself into a connection 
    3147exec('nc', found, fport || '22')