Forking a process in a daemon started by inetd

Forking a process in a daemon started by inetd

Post by Gregory C. Lark » Thu, 18 Nov 1999 04:00:00



Hi,

I am writing a small program that is invoked by inetd.  It is intended
to be a "pass through" server.  It forks/execs another process, which
is normally started by inetd itself.  My "pass through" server will do
some additional logging before the real process is started.

I have the fork/exec working and am able to pass data originating from
the connecting client through my server into the target process.  I
then receive data back from the target process and want to pass it
back to the connecting client.

This part doesn't seem to work - the client hangs even though I have
written the data to stdout in my server code.  I am using select() to
determine when the data from the target process is ready to send back
to the client, and that seems to work, too.

Also, I have my select() call within a while(1) loop.  Under what
condition should I call break in order to exit the loop.  I would like
to know when the child exits so I can exit the parent process.
However, I can't call waitpid while I am processing data between the
client and the target process.

Thanks for any information,
Greg Larkin

 
 
 

Forking a process in a daemon started by inetd

Post by Tom » Thu, 18 Nov 1999 04:00:00


Quote:> condition should I call break in order to exit the loop.  I would like
> to know when the child exits so I can exit the parent process.
> However, I can't call waitpid while I am processing data between the
> client and the target process.

You should get SIGCHLD when your child dies/exists/terminates, so setting a
handler for this signal (man sigaction) should give you some clues (though
knowing which of the children has just exited could be a different issue).

Tom

 
 
 

Forking a process in a daemon started by inetd

Post by ad.. » Wed, 01 Dec 1999 04:00:00



>> condition should I call break in order to exit the loop.  I would like
>> to know when the child exits so I can exit the parent process.
>> However, I can't call waitpid while I am processing data between the
>> client and the target process.
> You should get SIGCHLD when your child dies/exists/terminates, so setting a
> handler for this signal (man sigaction) should give you some clues (though
> knowing which of the children has just exited could be a different issue).

after the target process writes all its data, you call _exit(0); (or
something similar) in it, right? The target process goes zombie.
Normally, you don't really care which child exited. Passing -1 to waitpid()
will clean up any children.
In the parent you catch SIGCHLD like he stated, with the signal handler being
something like the following (note, I use signal() instead of sigaction()..I
haven't learned sigaction() yet)

void zombie_killer(int sig)
{
int status, child_val;
pid_t child_pid=-1;
pid_t get_pid;

/* the argument type. */
while ( (get_pid = waitpid(
        -1,             /* Wait for any child */
        (void *) &status,
        WNOHANG         /* Don't block waiting */
       )) > 0) {
           if (get_pid < 0) break;
           else child_pid = get_pid;
          }

/* No child to clean up, or error */
if (child_pid <= 0) {
        if (child_pid == -1) {
        /* error */
        printf("waitpid() error %d (%s)\n",errno,strerror(errno));
        }
        else if (child_pid == 0) {
        /* no child */
        printf("waitpid() no child to clean up\n");
        }
        /* Reset handler                    */
        signal(SIGCHLD, zombie_killer);
        return;
        }
else {
        printf("waitpid() returned pid %u\n",(unsigned int)child_pid);
        }

/* Reset handler                    */
/* Doing this before the waitpid()  */
/* can lead to an infinite loop     */
signal(SIGCHLD, zombie_killer);

/* Negative child_val indicates some error    */
/* Zero child_val indicates no data available */
    /*
     * We now have the info in 'status' and can manipulate it using
     * the macros in wait.h.
     */
    if (WIFEXITED(status))                /* did child exit normally? */
    {
        child_val = WEXITSTATUS(status); /* get child's exit status */
        printf("Child exited normally with status %d\n",child_val);
    }

Quote:}

-Cygnus
.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-.
Anthony J. Biacco                       Network Administrator/Engineer

    "Dream as if you'll live forever, live as if you'll die today"
http://cygnus.ncohafmuta.com                http://www.intergrafix.net
.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-.

 
 
 

1. starting daemon from cgi/cgi process that is a daemon

Hi,

Has anyone tried starting a daemon from a cgi program. I wrote
a daemon and it loads fine from the command line. The parent
process exits as it should and the child keeps running. But
when I try calling the program as a cgi program, the cgi
program (that is the parent process) doesn't seem to exit
until the child also exits. Does anyone know what I need to
do or how I need to write my daemon which is also a cgi
program. Now my browser hangs until the daemon terminates
instead of returning really quickly - or at least that is
what I have expected.

I tried using nested forks and setsid() but didn't seem to
affect anything.

All help would be greatly appriciated.
--
------------------------------------------------------------

 Chief Technology Officer (CTO)  Fax:   415-547-7735
 NetJet Communications           Phone: 415-547-7730
 Web Publisher to the World      http://www.netjet.com/
 ------------------------------------------------------------

2. ANNOUNCE: Pidentd version 3.0 is now available

3. What daemons started in inetd.conf ? Any command ?

4. Where to get a real Xterm?

5. Daemons started by Inetd

6. uucico problem

7. inetd process randomly? forking Solaris 2.4

8. Enterprise Management Center

9. Starting as a daemon instead of inetd

10. Make inetd start daemon that uses a dynamic library

11. Could NFS daemons be started via inetd?

12. Q inetd/sockets: how to run my daemon from inetd???

13. need tty term i/o help on process started by inetd