xref: /openbsd/regress/sys/kern/sosplice/Remote.pm (revision 09467b48)
1#	$OpenBSD: Remote.pm,v 1.4 2016/05/03 19:13:04 bluhm Exp $
2
3# Copyright (c) 2010-2014 Alexander Bluhm <bluhm@openbsd.org>
4#
5# Permission to use, copy, modify, and distribute this software for any
6# purpose with or without fee is hereby granted, provided that the above
7# copyright notice and this permission notice appear in all copies.
8#
9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17use strict;
18use warnings;
19
20package Remote;
21use parent 'Proc';
22use Carp;
23use Cwd;
24use File::Basename;
25
26sub new {
27	my $class = shift;
28	my %args = @_;
29	$args{logfile} ||= "remote.log";
30	$args{up} ||= "Started";
31	$args{func} = sub { Carp::confess "$class func may not be called" };
32	$args{remotessh}
33	    or croak "$class remote ssh host not given";
34	$args{forward}
35	    or croak "$class forward not given";
36	my $self = Proc::new($class, %args);
37	$self->{listenaddr}
38	    or croak "$class listen addr not given";
39	$self->{connectaddr}
40	    or croak "$class connect addr not given";
41	$self->{connectport}
42	    or croak "$class connect port not given";
43	return $self;
44}
45
46sub up {
47	my $self = Proc::up(shift, @_);
48	my $timeout = shift || 10;
49	my $lsock = $self->loggrep(qr/^listen sock: /, $timeout)
50	    or croak ref($self), " no listen sock in $self->{logfile} ".
51		"after $timeout seconds";
52	my($addr, $port) = $lsock =~ /: (\S+) (\S+)$/
53	    or croak ref($self), " no listen addr and port in $self->{logfile}";
54	$self->{listenaddr} = $addr;
55	$self->{listenport} = $port;
56	return $self;
57}
58
59sub child {
60	my $self = shift;
61
62	print STDERR $self->{up}, "\n";
63	my @opts = $ENV{SSH_OPTIONS} ? split(' ', $ENV{SSH_OPTIONS}) : ();
64	my $dir = dirname($0);
65	$dir = getcwd() if ! $dir || $dir eq ".";
66	my @cmd = ("ssh", "-n", @opts, $self->{remotessh}, "perl",
67	    "-I", "$dir/..", "$dir/".basename($0), $self->{forward},
68	    $self->{listenaddr}, $self->{connectaddr}, $self->{connectport},
69	    ($self->{testfile} ? "$dir/".basename($self->{testfile}) : ()));
70	print STDERR "execute: @cmd\n";
71	exec @cmd;
72	die ref($self), " exec '@cmd' failed: $!";
73}
74
751;
76