1.7.9: spawn brakes reopening of serial port

Corinna Vinschen corinna-cygwin@cygwin.com
Fri Oct 21 10:50:00 GMT 2011

On Oct 20 10:19, Manuel Wienand wrote:
> Hallo,
> it seems that spawning a process brakes the reopening of a serial port
> (when done during the execution of that process). The result of the
> open() function is "Permission denied (13)" in that case.

I had a look into your testcase.  What you see is expected behaviour.

On Windows, a serial port can only be opened once at a time, for
exclusive access.  You could have tried that without starting a second

  open ("/dev/ttyS0", O_RDWR);  // succeeds
  open ("/dev/ttyS0", O_RDWR);  // 2nd call fails with EACCES.

Now I hear you say that you close the descriptor before trying to open
it the second time, but you're missing the fact that on spawn/exec the
child process inherits the open file descriptors from the parent process.
So, even if you close the descriptor in the parent, it's still open in
the child.

Apparently you don't want the child process to inherit this descriptor,
so you must set its "close-on-exec" flag.  There are basically two ways
to do that

 - fd = open ("/dev/ttyS0", O_RDWR | O_CLOEXEC);

 - fd = open ("/dev/ttyS0", O_RDWR);
   fcntl (fd, F_SETFD, fcntl (fd, F_GETFD) | FD_CLOEXEC);

I guess it goes without saying that the close-on-exec flag has to be
set before calling spawn/exec.

And, btw., please use /dev/ttyS0 rather than /dev/com1, etc.  These are
the "official" names for the serial ports, while the "dev/comX" syntax
is only supported for backward compatibility.


Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat

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

More information about the Cygwin mailing list