DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 48NCs80F1881093 Authentication-Results: delorie.com; dkim=pass (1024-bit key, unprotected) header.d=cygwin.com header.i=@cygwin.com header.a=rsa-sha256 header.s=default header.b=qy+DFjdc X-Recipient: archive-cygwin AT delorie DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2864C3858C53 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; s=default; t=1727096046; bh=Boy9M+dWQqpmN5+ZXYNLFX5b2DVzk2KQb0XaRXw3lHM=; h=Subject:To:References:Date:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=qy+DFjdcw9aGp1cJy7txPkv9Xg9xUzRsz+cvnZBVI3frBWjYww466WtrcKF2FV8Fr +lPIagREqIeyvL3FTlC53IlA4wVjdQOSfHiFpZh4Dvfv8t/7CeJ0gKUpH5386bP3m+ ed6E6QlGOFLzTIyZOfwiHebH7XndLmnXMDwgLlpQ= X-Original-To: cygwin AT cygwin DOT com Delivered-To: cygwin AT cygwin DOT com DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 533773858D39 ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 533773858D39 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727095993; cv=none; b=VfY37xpfycrX7FVCZbKlM9dbQXm0xpfW1tsIIiWZpap13nJjrs5a3T9C0QXWYDT7qIoCsXAG7FsnELVeVYBpuwOG4d43Igr68rCKLCeBFSJKpRqhICqwGC6SSvc9HxKvPYS7pbi/SN9RmaZ/nAfj0cKiB23rSLnFGonyh1V2k/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727095993; c=relaxed/simple; bh=zJujzBlGc8v9gUEf9A480+ManGUQNy8xll0+hjNIKwk=; h=Subject:To:From:Message-ID:Date:MIME-Version; b=IfMwXsr29QTc5kXi5s0fTNDnrJi9vEHTXI8xjMZdskyxVqgj6L/yibDtfCN5StyklAMeCHXGh+4niOFAMw11fY4LUknGY/rl+KHa/dFwUi8MG0ia9iZyvopBNLLmZohNdwPmsGINPbQU1+Lon45G0UHvrIJdwf2qRCF7ncg8lQk= ARC-Authentication-Results: i=1; server2.sourceware.org Subject: Re: pread()/pwrite() fail with EBADF in child process if already used before fork() To: cygwin AT cygwin DOT com References: <13e3c5b1-3c7b-acc4-469e-0542d50cb6f6 AT t-online DOT de> Message-ID: Date: Mon, 23 Sep 2024 14:52:52 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 SeaMonkey/2.53.18.2 MIME-Version: 1.0 In-Reply-To: <13e3c5b1-3c7b-acc4-469e-0542d50cb6f6@t-online.de> X-TOI-EXPURGATEID: 150726::1727095973-2FFFD5E0-42F9F952/0/0 CLEAN NORMAL X-TOI-MSGID: a88a529e-b943-47ef-97b4-cec36e9d82fa X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00, BODY_8BITS, FREEMAIL_FROM, KAM_DMARC_STATUS, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: cygwin AT cygwin DOT com X-Mailman-Version: 2.1.30 Precedence: list List-Id: General Cygwin discussions and problem reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Christian Franke via Cygwin Reply-To: cygwin AT cygwin DOT com Cc: Christian Franke Content-Type: text/plain; charset="utf-8"; Format="flowed" Errors-To: cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com Sender: "Cygwin" Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from base64 to 8bit by delorie.com id 48NCs80F1881093 Christian Franke via Cygwin wrote: > Found during test of 'stress-ng --pseek ...' from current upstream > stress-ng git HEAD: > > Testcase: > > $ uname -r > 3.5.4-1.x86_64 > > $ cat pfail.c > #include > #include > #include > #include > > int main() > { >   int fd = open("pwrite.tmp", O_RDWR|O_CREAT|O_BINARY, 0666); >   if (fd < 0) { >     perror("open"); return 1; >   } > >   char c = 42; >   if (pwrite(fd, &c, 1, 0) < 0) >     perror("pwrite"); > >   if (fork() == 0) { >     if (pread(fd, &c, 1, 0) < 0) >       perror("pread"); >     _exit(0); >   } > >   int status; >   wait(&status); >   return 0; > } > > $ make pfail > cc     pfail.c   -o pfail > > $ ./pfail > pread: Bad file descriptor > > $ strace ./pfail > ... >   617   75356 [main] pfail 10826 dofork: 10827 = fork() >    82   11289 [main] pfail 10827 seterrno_from_nt_status: > /usr/src/debug/cygwin-3.5.4-1/winsup/cygwin/fhandler/disk_file.cc:1883 > status 0xC0000008 -> windows error 6 >    80   75436 [main] pfail 10826 wait4: calling proc_subproc, pid -1, > options 0 >    76   11365 [main] pfail 10827 geterrno_from_win_error: windows > error 6 == errno 9 >    78   75514 [main] pfail 10826 proc_subproc: args: 5, -7728 >    64   11429 [main] pfail 10827 pread: -1 = pread(3, 0x7FFFFCC0B, 1, > 0), errno 9 > ... > > > The problem does not occur if there is no pread()/pwrite() before the > fork(). This suggests that the child process inherits the extra handle > value used to keep the original seek position, but not the actual handle. > The mentioned extra handle 'prw_handle' is set to null after fork here: https://cygwin.com/git/?p=newlib-cygwin.git;a=blob;f=winsup/cygwin/fhandler/disk_file.cc;h=f4c21d3#l1698 But a test suggests that fhandler_disk_file::fixup_after_fork() is never called or debug_printf() does not work if called in this function. Thoughts? -- Problem reports: https://cygwin.com/problems.html FAQ: https://cygwin.com/faq/ Documentation: https://cygwin.com/docs.html Unsubscribe info: https://cygwin.com/ml/#unsubscribe-simple