Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
2.9 KiB

  1. # Net::DummyInetd.pm
  2. #
  3. # Copyright (c) 1995-1997 Graham Barr <[email protected]>. All rights reserved.
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the same terms as Perl itself.
  6. package Net::DummyInetd;
  7. require 5.002;
  8. use IO::Handle;
  9. use IO::Socket;
  10. use strict;
  11. use vars qw($VERSION);
  12. use Carp;
  13. $VERSION = do { my @r=(q$Revision: 1.6 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r};
  14. sub _process
  15. {
  16. my $listen = shift;
  17. my @cmd = @_;
  18. my $vec = '';
  19. my $r;
  20. vec($vec,fileno($listen),1) = 1;
  21. while(select($r=$vec,undef,undef,undef))
  22. {
  23. my $sock = $listen->accept;
  24. my $pid;
  25. if($pid = fork())
  26. {
  27. sleep 1;
  28. close($sock);
  29. }
  30. elsif(defined $pid)
  31. {
  32. my $x = IO::Handle->new_from_fd($sock,"r");
  33. open(STDIN,"<&=".fileno($x)) || die "$! $@";
  34. close($x);
  35. my $y = IO::Handle->new_from_fd($sock,"w");
  36. open(STDOUT,">&=".fileno($y)) || die "$! $@";
  37. close($y);
  38. close($sock);
  39. exec(@cmd) || carp "$! $@";
  40. }
  41. else
  42. {
  43. close($sock);
  44. carp $!;
  45. }
  46. }
  47. exit -1;
  48. }
  49. sub new
  50. {
  51. my $self = shift;
  52. my $type = ref($self) || $self;
  53. my $listen = IO::Socket::INET->new(Listen => 5, Proto => 'tcp');
  54. my $pid;
  55. return bless [ $listen->sockport, $pid ]
  56. if($pid = fork());
  57. _process($listen,@_);
  58. }
  59. sub port
  60. {
  61. my $self = shift;
  62. $self->[0];
  63. }
  64. sub DESTROY
  65. {
  66. my $self = shift;
  67. kill 9, $self->[1];
  68. }
  69. 1;
  70. __END__
  71. =head1 NAME
  72. Net::DummyInetd - A dummy Inetd server
  73. =head1 SYNOPSIS
  74. use Net::DummyInetd;
  75. use Net::SMTP;
  76. $inetd = new Net::DummyInetd qw(/usr/lib/sendmail -ba -bs);
  77. $smtp = Net::SMTP->new('localhost', Port => $inetd->port);
  78. =head1 DESCRIPTION
  79. C<Net::DummyInetd> is just what it's name says, it is a dummy inetd server.
  80. Creation of a C<Net::DummyInetd> will cause a child process to be spawned off
  81. which will listen to a socket. When a connection arrives on this socket
  82. the specified command is fork'd and exec'd with STDIN and STDOUT file
  83. descriptors duplicated to the new socket.
  84. This package was added as an example of how to use C<Net::SMTP> to connect
  85. to a C<sendmail> process, which is not the default, via SIDIN and STDOUT.
  86. A C<Net::Inetd> package will be available in the next release of C<libnet>
  87. =head1 CONSTRUCTOR
  88. =over 4
  89. =item new ( CMD )
  90. Creates a new object and spawns a child process which listens to a socket.
  91. C<CMD> is a list, which will be passed to C<exec> when a new process needs
  92. to be created.
  93. =back
  94. =head1 METHODS
  95. =over 4
  96. =item port
  97. Returns the port number on which the I<DummyInetd> object is listening
  98. =back
  99. =head1 AUTHOR
  100. Graham Barr <[email protected]>
  101. =head1 COPYRIGHT
  102. Copyright (c) 1995-1997 Graham Barr. All rights reserved.
  103. This program is free software; you can redistribute it and/or modify
  104. it under the same terms as Perl itself.
  105. =cut