releasing/cbrtools/perl/Net/DummyInetd.pm
author jascui
Tue, 16 Nov 2010 15:54:07 +0800
changeset 682 2c32f186fa1f
parent 602 3145852acc89
permissions -rw-r--r--
Solving incorrect handling when processing ExportName=SymbolName@Ordinal syntax.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
602
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     1
# Net::DummyInetd.pm
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     2
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     3
# Copyright (c) 1995-1997 Graham Barr <gbarr@pobox.com>. All rights reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     4
# This program is free software; you can redistribute it and/or
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     5
# modify it under the same terms as Perl itself.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     6
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     7
package Net::DummyInetd;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     8
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     9
require 5.002;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    10
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    11
use IO::Handle;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    12
use IO::Socket;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    13
use strict;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    14
use vars qw($VERSION);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    15
use Carp;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    16
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    17
$VERSION = do { my @r=(q$Revision: 1.6 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    18
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    19
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    20
sub _process
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    21
{
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    22
 my $listen = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    23
 my @cmd = @_;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    24
 my $vec = '';
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    25
 my $r;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    26
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    27
 vec($vec,fileno($listen),1) = 1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    28
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    29
 while(select($r=$vec,undef,undef,undef))
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    30
  {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    31
   my $sock = $listen->accept;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    32
   my $pid;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    33
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    34
   if($pid = fork())
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    35
    {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    36
     sleep 1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    37
     close($sock);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    38
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    39
   elsif(defined $pid)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    40
    {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    41
     my $x =  IO::Handle->new_from_fd($sock,"r");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    42
     open(STDIN,"<&=".fileno($x)) || die "$! $@";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    43
     close($x);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    44
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    45
     my $y = IO::Handle->new_from_fd($sock,"w");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    46
     open(STDOUT,">&=".fileno($y)) || die "$! $@";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    47
     close($y);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    48
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    49
     close($sock);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    50
     exec(@cmd) || carp "$! $@";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    51
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    52
   else
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    53
    {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    54
     close($sock);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    55
     carp $!;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    56
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    57
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    58
 exit -1; 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    59
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    60
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    61
sub new
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    62
{
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    63
 my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    64
 my $type = ref($self) || $self;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    65
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    66
 my $listen = IO::Socket::INET->new(Listen => 5, Proto => 'tcp');
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    67
 my $pid;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    68
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    69
 return bless [ $listen->sockport, $pid ]
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    70
	if($pid = fork());
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    71
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    72
 _process($listen,@_);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    73
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    74
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    75
sub port
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    76
{
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    77
 my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    78
 $self->[0];
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    79
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    80
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    81
sub DESTROY
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    82
{
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    83
 my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    84
 kill 9, $self->[1];
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    85
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    86
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    87
1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    88
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    89
__END__
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    90
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    91
=head1 NAME
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    92
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    93
Net::DummyInetd - A dummy Inetd server
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    94
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    95
=head1 SYNOPSIS
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    96
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    97
    use Net::DummyInetd;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    98
    use Net::SMTP;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    99
    
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   100
    $inetd = new Net::DummyInetd qw(/usr/lib/sendmail -ba -bs);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   101
    
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   102
    $smtp  = Net::SMTP->new('localhost', Port => $inetd->port);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   103
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   104
=head1 DESCRIPTION
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   105
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   106
C<Net::DummyInetd> is just what it's name says, it is a dummy inetd server.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   107
Creation of a C<Net::DummyInetd> will cause a child process to be spawned off
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   108
which will listen to a socket. When a connection arrives on this socket
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   109
the specified command is fork'd and exec'd with STDIN and STDOUT file
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   110
descriptors duplicated to the new socket.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   111
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   112
This package was added as an example of how to use C<Net::SMTP> to connect
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   113
to a C<sendmail> process, which is not the default, via SIDIN and STDOUT.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   114
A C<Net::Inetd> package will be available in the next release of C<libnet>
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   115
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   116
=head1 CONSTRUCTOR
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   117
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   118
=over 4
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   119
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   120
=item new ( CMD )
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   121
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   122
Creates a new object and spawns a child process which listens to a socket.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   123
C<CMD> is a list, which will be passed to C<exec> when a new process needs
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   124
to be created.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   125
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   126
=back
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   127
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   128
=head1 METHODS
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   129
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   130
=over 4
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   131
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   132
=item port
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   133
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   134
Returns the port number on which the I<DummyInetd> object is listening
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   135
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   136
=back
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   137
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   138
=head1 AUTHOR
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   139
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   140
Graham Barr <gbarr@pobox.com>
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   141
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   142
=head1 COPYRIGHT
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   143
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   144
Copyright (c) 1995-1997 Graham Barr. All rights reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   145
This program is free software; you can redistribute it and/or modify
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   146
it under the same terms as Perl itself.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   147
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   148
=cut