X-Recipient: archive-cygwin AT delorie DOT com X-SWARE-Spam-Status: No, hits=-0.3 required=5.0 tests=AWL,BAYES_00,RP_MATCHES_RCVD,TW_FC X-Spam-Check-By: sourceware.org From: Manuel Wienand To: "cygwin AT cygwin DOT com" Date: Mon, 24 Oct 2011 12:02:42 +0200 Subject: RE: 1.7.9: spawn brakes reopening of serial port Message-ID: <0C11C5BF0B29FD43A8D0250F711D497F89DEDBFF17@ex01-ubitronix.ubitronix.local> References: <0C11C5BF0B29FD43A8D0250F711D497F89DEDBFEE2 AT ex01-ubitronix DOT ubitronix DOT local> <20111021105003 DOT GB2979 AT calimero DOT vinschen DOT de> In-Reply-To: <20111021105003.GB2979@calimero.vinschen.de> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 X-IsSubscribed: yes Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from base64 to 8bit by delorie.com id p9OA52Lq009820 Hallo Corinna, thanks for the response. Yes, you are right, I missed the fact that the child process inherits the file descriptors. I took a look at the documentation of exec and just to be clear on the descriptor/handler issue: - file descriptor are passed down (basically always when calling the 'open' function) - socket descriptors are *not* passed down (not sure, wasn't in the documentation) - handles of semaphores, mutexes, memory maps, message queues are *not* passed down. - signal mask, scheduling policies (at leat when using SCHED_FIFO, SCHED_RR or SCHED_SPORADIC) and directory streams are passed down Is that correct? Thanks Manuel > -----Original Message----- > From: *** On Behalf Of Corinna Vinschen > Sent: Friday, October 21, 2011 12:50 PM > To: *** > Subject: Re: 1.7.9: spawn brakes reopening of serial port > > 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 > > > -- > This message has been scanned for viruses and > dangerous content by MailScanner, and is > believed to be clean.