Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

Rep: Reputation: 30
server wrapper script


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.

#!/usr/bin/perl -w

my $listen_address = '';
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,
	     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)

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

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


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



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";

    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
    die "hotwayd-server: unable to exec: $!\n";

    # Never goes beyond this line...

Old 08-01-2003, 02:22 PM   #2
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, 12:09 PM   #3
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)


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 05:20 PM
Java wrapper for CT Server ysudheer Linux - Software 1 07-06-2005 12:26 AM
Bash: how to write a wrapper script? J_Szucs Programming 0 01-29-2005 06:50 PM
Wrapper needed for BASH Script outspoken Programming 0 01-12-2005 10:04 AM
OS Wrapper ines Linux - Software 10 11-04-2004 07:57 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:30 AM.

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