public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* 1.3.2: setsid() vs. signals
@ 2001-09-04  4:35 Martin Muenstermann
  2001-09-04 18:51 ` Kazuhiro Fujieda
  0 siblings, 1 reply; 2+ messages in thread
From: Martin Muenstermann @ 2001-09-04  4:35 UTC (permalink / raw)
  To: cygwin

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/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: 1.3.2: setsid() vs. signals
  2001-09-04  4:35 1.3.2: setsid() vs. signals Martin Muenstermann
@ 2001-09-04 18:51 ` Kazuhiro Fujieda
  0 siblings, 0 replies; 2+ messages in thread
From: Kazuhiro Fujieda @ 2001-09-04 18:51 UTC (permalink / raw)
  To: Martin Muenstermann; +Cc: cygwin

>>> On Tue, 04 Sep 2001 13:34:11 +0200
>>> Martin Muenstermann <muenstermann@trustcenter.de> said:

> 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).

This is a bug in Cygwin DLL 1.3.2 and also in the latest snapshot.
I will fix it in the next release.
____
  | AIST      Kazuhiro Fujieda <fujieda@jaist.ac.jp>
  | HOKURIKU  Center for Information Science
o_/ 1990      Japan Advanced Institute of Science and Technology

--
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/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-09-04 18:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-04  4:35 1.3.2: setsid() vs. signals Martin Muenstermann
2001-09-04 18:51 ` Kazuhiro Fujieda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).