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 12:21, Ken Brown wrote:
> On 10/20/2010 1:09 AM, Andy Koppe wrote:
>>> Emacs creates a subprocess that runs an interactive bash shell. ÂEmacs
>>> wants
>>> to get the PGID of the foreground process group associated to the tty of
>>> this shell, and it does this on Linux via TIOCGPGRP (or equally well
>>> tcgetpgrp). ÂI think it uses the file descriptor of the master of the pty
>>> for this purpose. ÂIf you (or some other programmer reading this) could
>>> give
>>> me the code for setting all this up, I could play with it and try to
>>> figure
>>> out why I'm seeing a difference between Linux and Cygwin here. ÂI just
>>> don't
>>> know how to create a subprocess, give it a terminal, etc.
>> Here's a test along those lines that does show a difference between
>> Linux and Cygwin:
>> #include<stdio.h>
>> #include<pty.h>
>> int main(void)
>> {
>> Â int pid, fd;
>> Â pid = forkpty(&fd, 0, 0, 0);
>> Â if (!pid)
>> Â Â sleep(2);
>> Â else {
>> Â Â sleep(1);
>> Â Â printf("pid=%i fd=%i pgrp=%i\n", pid, fd, tcgetpgrp(fd));
>> Â }
>> }
> Thanks, Andy. ÂI had no idea how to do this.

D'oh, I'd actually written a very similar test before, except there
I'd looked at the slave side of the pty only:

So here's a test trying tcgetpgrp on both sides:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pty.h>

int main(void) {
  int pid, master_fd, slave_fd;
  openpty(&master_fd, &slave_fd, 0, 0, 0);
  pid = fork();
  if (!pid) {
  else {
    printf("pid=%i tcgetpgrp(master_fd)=%i tcgetpgrp(slave_fd)=%i\n",
           pid, tcgetpgrp(master_fd), tcgetpgrp(slave_fd));

Cygwin 1.5:
pid=1072 tcgetpgrp(master_fd)=1072 tcgetpgrp(slave_fd)=1072

Cygwin 1.7:
pid=2440 tcgetpgrp(master_fd)=0 tcgetpgrp(slave_fd)=-1

pid=23238 tcgetpgrp(master_fd)=23238 tcgetpgrp(slave_fd)=-1

I think the luit/tcsh problem that triggered the change from 1.5 to
1.7 only concerned the slave side.

>> On Linux, where it requires -lutil to link, this gives:
>> pid=13308 fd=3 tcgetpgrp(fd)=13308
> I can confirm this on my Linux system. ÂI mention this because apparently
> tcgetpgrp isn't the same on all Linux systems. ÂSee below.
>> On Cygwin:
>> pid=268 fd=3 tcgetpgrp(fd)=0
> Corinna made tcgetpgrp return 0 instead of -1 in some circumstances (see
> 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.


Problem reports:
Unsubscribe info:

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