This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

1.3.2: setsid() vs. signals


Hi, 

I have not found anything about this problem on the mailing list so I am
sending this report:

Running cygwin 1.3.2 on WinNT 4.0 SP 4 I have the following problem:

My app does a fork and daemonizes itself by calling setsid(). When I
press Ctrl-C in the shell where I started the app, the application
receives a SIGINT. 
IMHO, after calling setsid() a app should not receive any signals from
the shell any longer (at least this is the case on solaris).

Here is a test application 
NOTE: the daemon() function has been taken from openssh (in fact I
noticed the problem by calling "ssh -f myhost xterm"):

/* ---------------------------------- */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define _PATH_DEVNULL  "/dev/null"

char* prog;

int
daemon(nochdir, noclose)
        int nochdir, noclose;
{
  int fd;

  switch (fork()) {
  case -1:
    return (-1);
  case 0:
    break;
  default:
#ifdef HAVE_CYGWIN
    /*
     * This sleep avoids a race condition which kills the
     * child process if parent is started by a NT/W2K service.
     */
    sleep(1);
#endif
    _exit(0);
  }

/*   if (setpgid(0,0) < 0 )*/  /* cygwin: setpgid does catch signals */
  if (setsid() == -1)  /* cygwin: setsid does not catch signals */
    return (-1);

  if (!nochdir)
    (void)chdir("/");

  if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
    (void)dup2(fd, STDIN_FILENO);
    (void)dup2(fd, STDOUT_FILENO);
    (void)dup2(fd, STDERR_FILENO);
    if (fd > 2)
      (void)close (fd);
  }
  return (0);
}

void sighandler(int sig)
{
  printf( "%s: got signal %d, exiting\n", prog, sig );
  _exit(2);
}


int main(int argc, char *argv[])
{
  int i;

  prog = strdup(argv[0]);
  for( i = 1; i < 16; i++ )
    signal(i, sighandler);
  printf( "%s: signal handling activated\n", prog);

  printf("running as daemon...\n");
  if( daemon(1,1) < 0 )
    {
      printf("daemon() failed.\n");
      exit(3);
    }

  while(1)
    sleep(1);

  exit(0);
}

/* ---------------------------------- */

Run it, after the fork press Ctrl-C in the shell.
I receive the message " ./sigint.exe: got signal 2, exiting" and the
process is dead. 

BTW: When I call setpgid(0,0) instead of setsid(), signals of the shell
are correctly ignored.

In cygwin 1.1.2 this problem did not occur.

Thanks,
Martin

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]