DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 48OAGlBU2559280 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=Ijima15P X-Recipient: archive-cygwin AT delorie DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 846CE385DDDF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; s=default; t=1727173005; bh=HHgMCp0nX5c1adaADOjRSYy1ahtFkrL2GsOkEtUj6k8=; 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=Ijima15PrxJYVjuOf54X2SBzLl6VooqzmoZdNM7m6hQzn8BU70Z+GxeOtKLjDUDep RNkxw6iM24/kicTlz803ZLO3j98Vco0BUiICwuVBG9l7lFtis45YMGpObYvMshHVAK jwh7HpCuzeNnILa4F9URvsZO2rTcZ3eOFzA76n0o= X-Original-To: cygwin AT cygwin DOT com Delivered-To: cygwin AT cygwin DOT com DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B34F33858424 ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B34F33858424 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727172946; cv=none; b=eLcTr2OYdxfHNGq6njML/OG7fRG4TxCtku5U1nKTZkVk4Oq7pkGaSi6dUh8+CKMQTiscFkSpNvF3W2RIjt9ucoLjWrKmGn/RwKKYXWzDMDJWSSEAGoU5SoqHaMPtOkk9ktF+0WGkNl4/WK7Fg7beiIvsIXCmrNcpu6HOfVaBM5k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727172946; c=relaxed/simple; bh=hZFxgy2PrU+VrwDDy4hd55ud5mU77riIqkDTh8C/64A=; h=Subject:To:From:Message-ID:Date:MIME-Version; b=g+hGV36A0j8LTBZ77/UYfi6iYGMnFeKTKSV+Y9JjnBdG5wMY7ZjVpFQsNJW3v8ppiodmPpuvL5wzdCUwbPoLCLsy3XPUYplykaBi8Th1kmHZ+fSvu28JkOlCTQHsdw/oOv+0EIbJfuXM8POXLm6G5bBqoIMDLHQDKzTwWn0FeDY= 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: Tue, 24 Sep 2024 12:15:37 +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: X-TOI-EXPURGATEID: 150726::1727172937-F3FFD5D9-D6AA5312/0/0 CLEAN NORMAL X-TOI-MSGID: c59fdea9-10d9-4893-a18e-ab5984e985c6 X-Spam-Status: No, score=-3.5 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 48OAGlBU2559280 Christian Franke via Cygwin wrote: > 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. Possible fix: https://sourceware.org/pipermail/cygwin-patches/2024q3/012793.html -- Regards, Christian -- 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