Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 61 lines (50 sloc) 1.111 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
# Nothing exciting, just a couple of utility routines that are used in several
# test suites

use IO::Socket;

sub start_server
{
    my ($S, @args) = @_;

    my $pid;

    if (! defined($pid = fork))
    {
        die "fork() error: $!, stopped";
    }
    elsif ($pid)
    {
        return $pid;
    }
    else
    {
        $S->server_loop(@args);
        exit; # When the parent stops this server, we want to stop this child
    }
}

sub stop_server
{
    my ($pid, $no_sleep) = @_;

    # Per RT 27778, use 'KILL' instead of 'INT' as the stop-server signal for
    # MSWin platforms:
    my $SIGNAL = ($^O eq 'MSWin32') ? 'KILL' : 'INT';
    kill $SIGNAL, $pid;
    if (! $no_sleep)
    {
        sleep 2; # give the old sockets time to go away
    }

    return;
}

sub find_port
{
    my $start_at = shift;
    $start_at ||= 9000;

    for my $port ($start_at .. ($start_at + 2000))
    {
        my $sock = IO::Socket->new(
            Domain => AF_INET,
            PeerAddr => 'localhost',
            PeerPort => $port
        );
        return $port if (! ref $sock);
    }

    return -1;
}

1;
Something went wrong with that request. Please try again.