DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 48MJBbfP1323499 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=vPLqiK8v X-Recipient: archive-cygwin AT delorie DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C37623858C48 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; s=default; t=1727032295; bh=gEgaYws3bcObj/+++QRrUXFtFPxCLCvIXLAHenjuhNQ=; h=Date:To:Subject:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=vPLqiK8vANDhFmiVZvUbKyBZKB1nR5FX2F506KAxn2z65m1dtuQ/NsA6ZrbUH3+Wf CpR2UgcdMCJz50261D4Pf1fB0aTuWP+8qWWLUOzxMGTNpAVFy6BYZIq23bGbWIzunU 6JLYFl1rNa2EoJXlG0NbCetW38cGcqkWuuxzJ+pU= X-Original-To: cygwin AT cygwin DOT com Delivered-To: cygwin AT cygwin DOT com DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DF51B3858D20 ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DF51B3858D20 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727032273; cv=none; b=xjkv5rPczQlTUM1f2xtyoO8jvsPk3T3ZG3UWGbhaOlYcr/QWEInx63+8yAgHEOAUzawAm3W6iAM1wW7TtvAyExpBf54YcV56rGF/2kwIW3I6WuirkAV0b5d8OU/cPjrNpZ4Q4Ezu9rLsyRCLGWfrriefuqTjGLOqBf9VU8RGphA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727032273; c=relaxed/simple; bh=lWNsZtWuZNdBhUsqW/fsFLv3SAIKcK4WpkkgaWaCHKw=; h=DKIM-Signature:Date:To:From:Subject:Message-ID:MIME-Version; b=C9EpEmKfO24aUHcRJ1lYOugg75KAX/9Fu3QcGJFKMC7jiJJXYzyK5XjvjX+NYIBaSHMErJk1KdjGSvzVTHBcHhrEA67aLGbq/Om1h+ScE/Hi0BMLtzFgirV20r7lhBAoC28evtjARbQwIhiknEwjMuOpXmy73x7YVYkJbcPh1cI= ARC-Authentication-Results: i=1; server2.sourceware.org Date: Sun, 22 Sep 2024 19:11:05 +0000 To: cygwin AT cygwin DOT com Subject: Re: pread()/pwrite() fail with EBADF in child process if already used before fork() Message-ID: In-Reply-To: <13e3c5b1-3c7b-acc4-469e-0542d50cb6f6@t-online.de> References: <13e3c5b1-3c7b-acc4-469e-0542d50cb6f6 AT t-online DOT de> Feedback-ID: 65268012:user:proton X-Pm-Message-ID: 8e2b3c2398e7468cfa436d45757cf1bbd0fa95fe MIME-Version: 1.0 X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_INFOUSMEBIZ, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=no 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: Jim McNamara via Cygwin Reply-To: Jim McNamara Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com Sender: "Cygwin" Hi Christian, I worked with AI to try something like this does it help at all? It is not exactly your code but got rid of file descriptor error. These things were outlined on chatgpt... thanks, jim A "bad file descriptor" error in your code can occur for several reasons. Here are the most common causes: Uninitialized or Invalid File Descriptor: If the file descriptor is not properly initialized or has been closed before it's used in pread or pwrite, you'll get an error. File Descriptor from fork: After a fork, both the parent and child processes have the same file descriptors. If the parent process closes the file descriptor before the child tries to read/write, the child will encounter a "bad file descriptor" error. File Not Opened Successfully: If open fails (e.g., due to permissions, invalid flags, or system limits), it returns -1. If you do not check for this and use the returned value directly, it can lead to a bad file descriptor error. Incorrectly Using fopen: If you mistakenly used fopen instead of open (as was in your original code), the resulting file pointer would not be compatible with low-level operations like pread and pwrite. File Descriptor Limit Reached: Each process has a limit on the number of file descriptors it can open simultaneously. If this limit is exceeded, open will fail, and subsequent attempts to use the invalid file descriptor will result in an error. Closing the File Descriptor: If close(fd) is called while the file descriptor is still being used by the child process, it will cause a "bad file descriptor" error when the child tries to use it. #include #include #include #include #include int main() { int fd = open("pwrite.tmp", O_RDWR | O_CREAT, 0666); int fd2 = open("pwrite.tmp", O_RDWR | O_CREAT, 0666); if (fd < 0) { perror("Error opening file"); return 1; } if (fd2 < 0) { perror("Error opening file"); return 1; } char c = 42; // Example character to write if (pwrite(fd, &c, sizeof(c), 0) < 0) { perror("pwrite"); close(fd); return EXIT_FAILURE; } // Forking a child process pid_t pid = fork(); if (pid < 0) { perror("fork"); if (!fd > -1) close(fd); return EXIT_FAILURE; } if (pid == 0) { // Child process if (pread(fd2, &c, sizeof(c), 0) < 0) { perror("pread"); } else { printf("Read character: %d\n", c); // Print the read character } _exit(EXIT_SUCCESS); } int status; if (wait(&status) < 0) { perror("wait"); } if (close(fd2) < 0) { perror("close"); return EXIT_FAILURE; } return EXIT_SUCCESS; } Sent with Proton Mail secure email. On Sunday, September 22nd, 2024 at 2:09 PM, 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. > > -- > 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 -- 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