DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 494EwiGK1272952 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=cxgkJRyt X-Recipient: archive-cygwin AT delorie DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2B935386F47A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; s=default; t=1728053922; bh=MgfpJouprhwgnlr061T5JIQBvzVSX9MrQTsbXtzBxZw=; h=References:In-Reply-To:Date:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=cxgkJRytB8KkR6+txGNdar17Wh/MAAIRyH9y89WCa4FckLOAir07q5t34c8WL5EK9 qyxnYrXQ/O80B89u4docoTHaPSP9eDBmtpU+u2U+RZfY8HjEriU1Hh2ibqIXxvVwUA QfQ54TYctZsyQ8mTQAozF4xrB0zDfXk/Irt2YQnw= X-Original-To: cygwin AT cygwin DOT com Delivered-To: cygwin AT cygwin DOT com DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AA9C1386F463 ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AA9C1386F463 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728053847; cv=none; b=S90HCW8FNJNMpGTGT4VK1VeXPWyHePSrYzqYw+yqWCIfLBmKX9FwmNbvlKBVz2L3O28lx8JWY69Dsg2HC18LjmlK2at5K5vF7l+x2cH33vhvZF0YuINPotHY3koly/hhLkiPnmuFIKOiyveDrCwU+xvAXnb9YImzqtpSgnt8KhM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728053847; c=relaxed/simple; bh=x3h9zEdftSq1mhbn63TfewE/rUfbFPZlfqg90mhBC+U=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=GxxadG85QW9AyKADXwO6uDmofTgqQasgaFss0WLGHk/0Pjv0mwk4CsC/zy6suzh6ikHlueYW4IqTjZDgZaBcitlcwFIKUmHdvOevLgKUMty6zR/hU0CHG4De9mPsrGise/Ulkg+y66IDBIOoRT7yQ2kZ6vfr92WX6h7vj/Zeki0= ARC-Authentication-Results: i=1; server2.sourceware.org X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728053841; x=1728658641; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=HsZ3ge/CmPgCuo4RciOAgIWiEwnvabZHilEWfR7nxL4=; b=gg6BTSeVKGv0TH9HPy4ya7mk8zZ3nZt/zw8ztE8XEMDnCDwE5kmW3KCPEOHs9WZ+pu Q825gBx9FPSN9DoNESWjstz5lZZDm4RfeFMnPCn8UIyVfLu5IAHi4qL2WITx2rUkMXwt Mvd0ROLpBkGBb0S+mI0qeD5sL2lPkf3wZ2ZNeXcCDim+iQeOcMtOwVWPqH6wFsIo6k9y 7sNI3b5no2mm2tZllemO1WSNFK3vbhOpIbNxJ7ERz3gj/NTpY5hBLczljgDs3vTBhgea AUI75s4ryAjRHeaFJtbFZdEE223HwMXJ2xhGyOknL9TGGPu6pnABskNi3Kwi0CShD2Mz DZWA== X-Gm-Message-State: AOJu0Yz2mt2jGDhLL1nyXKexA1EONF+m4/7MOSHS7FTu7I0lbXuITwAk D3xFPEqetQyPcSwqI7eIH8k5taPKmYoZ4DYx6QSm7A6GqN5oFk9CChBPK5Qmq9UEuD6qRj7fhzV shYET+xs51IKVtsOpHe7LMmquJN+H3lOY X-Google-Smtp-Source: AGHT+IF1B02ALw6u8TKtU1StZTmEmmKX4nMpVKEEh526iJf+LaRmSlvUXHL5ocXfGc0q3uXXiwjbbf1H539xQYVNW9U= X-Received: by 2002:a05:651c:b14:b0:2f7:544a:28af with SMTP id 38308e7fff4ca-2faf3c159eemr16817711fa.14.1728053840364; Fri, 04 Oct 2024 07:57:20 -0700 (PDT) MIME-Version: 1.0 References: <13e3c5b1-3c7b-acc4-469e-0542d50cb6f6 AT t-online DOT de> In-Reply-To: Date: Fri, 4 Oct 2024 16:56:44 +0200 Message-ID: Subject: Re: pread()/pwrite() fail with EBADF in child process if already used before fork() To: cygwin AT cygwin DOT com X-Spam-Status: No, score=0.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, 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 List-Id: General Cygwin discussions and problem reports List-Archive: List-Post: List-Help: List-Subscribe: , From: Cedric Blancher via Cygwin Reply-To: Cedric Blancher Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "Cygwin" On Tue, 24 Sept 2024 at 12:16, Christian Franke via Cygwin wrote: > > 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 > Is this fix going to be committed to Cygwin 3.6 anytime soon? Ced -- Cedric Blancher [https://plus.google.com/u/0/+CedricBlancher/] Institute Pasteur -- 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