delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2011/10/21/06:50:51

X-Recipient: archive-cygwin AT delorie DOT com
X-Spam-Check-By: sourceware.org
Date: Fri, 21 Oct 2011 12:50:03 +0200
From: Corinna Vinschen <corinna-cygwin AT cygwin DOT com>
To: cygwin AT cygwin DOT com
Subject: Re: 1.7.9: spawn brakes reopening of serial port
Message-ID: <20111021105003.GB2979@calimero.vinschen.de>
Reply-To: cygwin AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
References: <0C11C5BF0B29FD43A8D0250F711D497F89DEDBFEE2 AT ex01-ubitronix DOT ubitronix DOT local>
MIME-Version: 1.0
In-Reply-To: <0C11C5BF0B29FD43A8D0250F711D497F89DEDBFEE2@ex01-ubitronix.ubitronix.local>
User-Agent: Mutt/1.5.21 (2010-09-15)
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

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
process:

  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

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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019