LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-15-2003, 06:09 AM   #1
yapp
Member
 
Registered: Apr 2003
Location: Netherlands
Distribution: SuSE (before: Gentoo, Slackware)
Posts: 613

Rep: Reputation: 30
server wrapper script


Hi..

I'm trying to write a Perl program for networking. It's a sort of dummy-inetd. I removed inetd from my system, because I doesn't really suits my needs. Now, I'm using hoywayd to download my e-mail, and it's fully based on inetd.

So I started writing a wrapper-script in Perl. (also as excercise) This is what I got so far: everything is working, but the program terminates when I close the first connection.

Is there anyone with a little UNIX networking experience able to help me?

I've seen some pieces of code with 'vec' and 'select', but I didn't understand those system calls. The secoond time, the accept() call seams to fail.

Code:
#!/usr/bin/perl -w

$0='hotwayd-server';
my $listen_address = '127.0.0.1';
my $listen_port    = '1100';

use strict;
use IO::Socket::INET;
use POSIX qw(:sys_wait_h setsid);

my $INET_EOL = '\015\012';




# -----------------------------------------------
# Start the server socket

my $server = new IO::Socket::INET(
                                   LocalAddr => $listen_address,
                                   LocalPort => $listen_port,
                                   Proto     => 'tcp',
	   			   Timeout   => undef,
				   Reuse     => 1,
				   Listen    => 5,
                                 )
             or
	     die "hotwayd-server: couldn't spawn server: $!\n";


# -----------------------------------------------
# Action to be taken on shutdown:


sub clean_up
{
  $server->close() if defined $server;
}



# -----------------------------------------------
# Daemonize

sub daemonize()
{
  open(STDIN, '/dev/null')                  or die "Can't read /dev/null: $!"; 
  open(STDOUT, '>/dev/null')                or die "Can't write to /dev/null: $!";
  open(STDERR, '>/tmp/hotwayd-server.log')  or die "Can't write to /tmp/hotwayd-server.log: $!";

  defined(my $pid = fork)   or die "Can't fork: $!";
  exit if $pid;
  setsid()                  or die "Can't start a new session: $!";
}


# -----------------------------------------------
# Create PID file, fork into background.

# Handle shutdown
#$SIG{'INT'}  = \&clean_up;
#$SIG{'TERM'} = \&clean_up;
#$SIG{'QUIT'} = \&clean_up;
#$SIG{'__DIE__'} = \&clean_up;




# -----------------------------------------------
# Reading exit status of child processes.

  # We need to read the exit status of terminating child processes,
  # so the kernel can clean-up this last piece of process-information
  # from the process-table.  (or the child will remain a zombie until we die)

sub REAPER
{
  # Perform a wait for every child.
  my $child_pid;
  do
  {
    my $child_pid = waitpid(-1, WNOHANG);
  }
  while(defined $child_pid && $child_pid > 0);

  $SIG{'CHLD'} = \&REAPER;  # set again (still loathe sysV)
}

$SIG{'CHLD'} = \&REAPER;





# -----------------------------------------------
# Wait for incoming connections

$server->autoflush(1);
$server->listen();

daemonize();

while(my $socket = $server->accept())
{

  # Fork the client
  my $child = fork();

  if(! defined $child)
  {
    # Still in parent, unable to fork
    print $socket "-ERR unable to open session: $!!$INET_EOL";
    $socket->close();

    die "hotwayd-server: unable to fork child: $!\n";
  }
  elsif($child == 0)
  {
    # Inside child process.

    # dup the socket handels to STDIN and STDOUT in child process
    local *SOCKET_HANDLE;
    *SOCKET_HANDLE = *$socket;
    open(STDIN,  "<&*SOCKET_HANDLE") or die "hotwayd-server: unable to dup socket to STDIN:  $!\n";
    open(STDOUT, ">&*SOCKET_HANDLE") or die "hotwayd-server: unable to dup socket to STDOUT: $!\n";

    # leave STDERR untouched

    # Run the hotwayd program
    exec('/opt/hotwayd/sbin/hotwayd');
    die "hotwayd-server: unable to exec: $!\n";

    # Never goes beyond this line...
  }
}


clean_up();
 
Old 08-01-2003, 01:22 PM   #2
MacKtheHacK
Member
 
Registered: Jul 2003
Location: Boston, MA, USA
Distribution: RedHat, SuSE, Gentoo, Slackware, Mandrake ...
Posts: 111

Rep: Reputation: 15
You should take a look at the Net:aemon package on CPAN. It does most (perhaps all) of what you're trying to do. I'm using it, and it works well for me.
 
Old 08-02-2003, 11:09 AM   #3
yapp
Member
 
Registered: Apr 2003
Location: Netherlands
Distribution: SuSE (before: Gentoo, Slackware)
Posts: 613

Original Poster
Rep: Reputation: 30
OK thanks :-) I'll check it out soon. It sounds much like what I need.

Oh and maybe I'll try to do this in C too, because I could use some exercise (just found out that one school book of mine explains some UNIX network calls)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help on shell script as a wrapper to launch default programs according to input file daveiro Programming 2 11-24-2005 04:20 PM
Java wrapper for CT Server ysudheer Linux - Software 1 07-05-2005 11:26 PM
Bash: how to write a wrapper script? J_Szucs Programming 0 01-29-2005 05:50 PM
Wrapper needed for BASH Script outspoken Programming 0 01-12-2005 09:04 AM
OS Wrapper ines Linux - Software 10 11-04-2004 06:57 AM


All times are GMT -5. The time now is 05:55 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration