How to terminate a child process that is waiting for input??

How to terminate a child process that is waiting for input??

Post by A. Sytn » Fri, 06 Sep 2002 06:57:36



I have a C server program which issues a system subroutine call (
system() ) to delete a file, but the file has security restrictions
and the child is sitting waiting for a response to  rm: Remove
/tmp/save/abcde?

I have tried to implement a SIGALRM timeout of 10 seconds and I want
to kill the child in the signal handler but the child some how is
taking precedence and the signal handler for the timeout is not
getting serviced - Only when I kill the process does the SIGALRM get
serviced.

Any clues?

AS

 
 
 

How to terminate a child process that is waiting for input??

Post by Nicholas Drone » Fri, 06 Sep 2002 13:11:51



> I have a C server program which issues a system subroutine call (
> system() ) to delete a file, but the file has security restrictions
> and the child is sitting waiting for a response to  rm: Remove
> /tmp/save/abcde?
> I have tried to implement a SIGALRM timeout of 10 seconds and I want
> to kill the child in the signal handler but the child some how is
> taking precedence and the signal handler for the timeout is not
> getting serviced - Only when I kill the process does the SIGALRM get
> serviced.
> Any clues?

Yes, use unlink() instead of system("rm file");

Regards,

Nicholas

 
 
 

How to terminate a child process that is waiting for input??

Post by Joerg Brueh » Fri, 06 Sep 2002 19:37:11


Hi!


> I have a C server program which issues a system subroutine call (
> system() ) to delete a file, but the file has security restrictions
> and the child is sitting waiting for a response to  rm: Remove
> /tmp/save/abcde?

AFAIK, the command line you give to "system()" is analyzed by
the shell, so you might add I/O redirection:
   system ( "rm /tmp/save/abcde < /dev/null" );

Quote:

> I have tried to implement a SIGALRM timeout of 10 seconds and I want
> to kill the child in the signal handler but the child some how is
> taking precedence and the signal handler for the timeout is not
> getting serviced - Only when I kill the process does the SIGALRM get
> serviced.

When you call "system()", execution of your process is blocked
until the child process (you are just starting) terminates.
I am not totally surprised that an alarm signal is blocked also.

If you want to receive it, you might have to use "fork()" and
"exec()" explicitly.

HTH,
Joerg Bruehe

--
Joerg Bruehe, SQL Datenbanksysteme GmbH, Berlin, Germany
     (speaking only for himself)

 
 
 

How to terminate a child process that is waiting for input??

Post by A. Sytn » Fri, 06 Sep 2002 21:36:17




> > I have a C server program which issues a system subroutine call (
> > system() ) to delete a file, ...

> > Any clues?

> Yes, use unlink() instead of system("rm file");

> Regards,

> Nicholas

The system () subroutine is used for other commands which are passed
to the server from another node...

Most of the commands execute immediately and exit - Except for this
case with the remove which is awaiting terminal user input.

Here is *my* system function which tries to do a timeout in the child
and it does not SIGALRM - Its as if the execl command has blocked
SIGALRM...
-----------------------------------------------------------------------------
int sig_timout = 0;
int server_flag = FALSE;
struct itimerval itv;
pid_t   pid;

int system_call (command_string)
const char *command_string;
{
   int          status;
   struct sigaction  ignore,saveintr,savequit;
   sigset_t     chldmask, savemask;

   if (command_string == NULL)
      return(1);

   if ( (pid = fork() ) < 0)
   {
      status = -1;
   }
   else
      if (pid == 0)  /* {child} */
      {          
        if ( signal(SIGALRM, sig_alrm) == SIG_ERR)
          fprintf (stderr, "%s child SIGNAL ALARM failed\n",
time_stamp());
    /*
     * Set up a timeout of 10 seconds.
     */
        itv.it_interval.tv_usec = 0;
        itv.it_interval.tv_sec = 0;
        itv.it_value.tv_usec = 0;
        itv.it_value.tv_sec = 10;

        setitimer(ITIMER_REAL, &itv, (struct itimerval *) 0);

        execl ("/bin/sh", "sh", "-c", command_string, (char *) 0);
    /*
     * Turn off the alarm, they typed something.
     */
        itv.it_value.tv_usec = 0;
        itv.it_value.tv_sec = 0;

        setitimer(ITIMER_REAL, &itv, (struct itimerval *) 0);
        server_flag = FALSE;

        if (sig_timout == 1)
          _exit(1024);
        else
          _exit(127);
      }
      else
      {                
         printf ("child pid = %d\n", (int) pid );

         waitpid(pid, &status, 0);
         printf ("status = %d, errno = %d\n", status, (int) errno );
      }
   return (status);

Quote:}