From patchwork Wed Aug 23 04:21:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergio Durigan Junior X-Patchwork-Id: 74525 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E8FC03857B98 for ; Wed, 23 Aug 2023 04:22:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E8FC03857B98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1692764532; bh=H+fLL2vs925SW5+9PSISVo1GWHDvsVMcYwAt9n3CdXE=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=o/5CRvwFaZAEY7g//ZIVOxmzQOIYacfkMGzC54xBYFoIaOGlXhQV51dG86TM9zA5v N+5r7Vteu7k0HCoh/832mJdC4YP8vGpwav89oRhQNeMIpDo3txiiUsFquucqjmszlM Sxop75gJr6BBL4GQxT0+iCLABqi8qWsBiERbhSzc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.sergiodj.net (unknown [IPv6:2a01:4f8:13a:6e8:160::1]) by sourceware.org (Postfix) with ESMTPS id 9213E3858C74 for ; Wed, 23 Aug 2023 04:21:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9213E3858C74 Received: from localhost (24-212-170-20.cable.teksavvy.com [24.212.170.20]) by mail.sergiodj.net (Postfix) with ESMTPSA id E3761A602AE; Wed, 23 Aug 2023 00:21:45 -0400 (EDT) To: libc-alpha@sourceware.org Cc: Sergio Durigan Junior Subject: [PATCH] sysdeps: Clear O_CREAT|O_ACCMODE when trying again on sem_open Date: Wed, 23 Aug 2023 00:21:29 -0400 Message-Id: <20230823042129.3955131-1-sergiodj@sergiodj.net> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergio Durigan Junior via Libc-alpha From: Sergio Durigan Junior Reply-To: Sergio Durigan Junior Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" When invoking sem_open with O_CREAT as one of its flags, we'll end up in the second part of sem_open's "if ((oflag & O_CREAT) == 0 || (oflag & O_EXCL) == 0)", which means that we don't expect the semaphore file to exist. In that part, open_flags is initialized as "O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC" and there's an attempt to open(2) the file, which will likely fail because it won't exist. After that first (expected) failure, some cleanup is done and we go back to the label "try_again", which lives in the first part of the aforementioned "if". The problem is that, in that part of the code, we expect the semaphore file to exist, and as such O_CREAT (this time the flag we pass to open(2)) needs to be cleaned from open_flags, otherwise we'll see another failure (this time unexpected) when trying to open the file, which will lead the call to sem_open to fail as well. This can cause very strange bugs, especially with OpenMPI, which makes extensive use of semaphores. The fix here is to actually make sure that the O_CREAT|O_ACCMODE flags are clear after we enter "try_again". See also: https://bugs.launchpad.net/ubuntu/+source/h5py/+bug/2031912 --- sysdeps/pthread/sem_open.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sysdeps/pthread/sem_open.c b/sysdeps/pthread/sem_open.c index e5db929d20..ba91f89d57 100644 --- a/sysdeps/pthread/sem_open.c +++ b/sysdeps/pthread/sem_open.c @@ -66,8 +66,8 @@ __sem_open (const char *name, int oflag, ...) if ((oflag & O_CREAT) == 0 || (oflag & O_EXCL) == 0) { open_flags = O_RDWR | O_NOFOLLOW | O_CLOEXEC; - open_flags |= (oflag & ~(O_CREAT|O_ACCMODE)); try_again: + open_flags |= (oflag & ~(O_CREAT|O_ACCMODE)); fd = __open (dirname.name, open_flags); if (fd == -1)