This is the mail archive of the cygwin 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]
Other format: [Raw text]

Re: Sending signals to a subprocess


On 20 October 2010 13:20, Andy Koppe wrote:
>> Corinna made tcgetpgrp return 0 instead of -1 in some circumstances (see
>> http://www.cygwin.com/ml/cygwin-patches/2009-q4/msg00045.html) because she
>> saw Linux doing that. ÂBut when I run Corinna's test on my Linux system, I
>> get -1 where she got 0. ÂSo not all Linuxes agree on what tcgetpgrp should
>> do.
>
> Hmm, Corinna's test calls tcgetpgrp(master) in the parent only before
> the child is forked and after it exited, so it's correct to report
> that there's no foreground process.
>
> I wonder which Linux it was that returned 0 in case of failure. I've
> tried it on a recent Opensuse, an old Redhat with a 2.6.9 kernel, and
> also a Debian with a 2.4 kernel, and got -1 on all of those.

Actually I'd only tried my test on all three systems, whereas I'd
tried Corinna's only on the old Redhat, where it did print -1 for
failure. On the 2.4 system it can't open /dev/ptmx, whereas on the
Opensuse with 2.6.34 I do get the results Corinna reported, including
0 on the master side of the pty when enquiring from the parent.
(Process 0 is the startup process, so I guess that makes some sense.)

To bring my ramblings to some sort of conclusion, here's a slightly
amended version of Corinna's test that checks the master side from the
parent process before, *during* and after the child process:

#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/fcntl.h>
#include <sys/wait.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>

int main ()
{
  int master, slave, status;
  char pts[256];

  printf ("parent pid: %d\n", getpid ());
  if ((master = open ("/dev/ptmx", O_RDWR | O_NOCTTY)) >= 0)
    {
      int ret;
      grantpt (master);
      unlockpt (master);
      printf ("parent tcgetpgrp master before child: %d\n", tcgetpgrp (master));
      strcpy (pts, ptsname (master));
      switch (fork ())
        {
        case -1:
          break;
        case 0: // child
          ret = setsid ();
          printf ("child pid: %d (setsid: %d)\n", getpid (), ret);
          printf ("child tcgetpgrp master before open: %d\n",
tcgetpgrp(master));
          if ((slave = open (pts, O_RDWR)) >= 0)
            {
              printf ("child tcgetpgrp master after open: %d\n",
tcgetpgrp (master));
              printf ("child tcgetpgrp slave: %d\n", tcgetpgrp (slave));
              close (slave);
            }
          sleep(2);
          break;
        default:// parent
          sleep(1);
          printf ("parent tcgetpgrp master during child: %d\n",
tcgetpgrp (master));
          wait (&status);
          printf ("parent tcgetpgrp master after child: %d\n",
tcgetpgrp (master));
          break;
        }
      close (master);
      return 0;
    }
  return 1;
}


On Cygwin 1.7:

parent pid: 5000
parent tcgetpgrp master before child: 0
child pid: 1572 (setsid: 1572)
child tcgetpgrp master before open: 0
child tcgetpgrp master after open: 1572
child tcgetpgrp slave: 1572
parent tcgetpgrp master during child: 0
parent tcgetpgrp master after child: 0

On Opensuse with 2.6.34 kernel:

parent pid: 13507
parent tcgetpgrp master before child: 0
child pid: 13508 (setsid: 13508)
child tcgetpgrp master before open: 0
child tcgetpgrp master after open: 13508
child tcgetpgrp slave: 13508
parent tcgetpgrp master during child: 13508
parent tcgetpgrp master after child: 0

Andy

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


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