From patchwork Mon Jan 8 13:50:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: chenguanxi11234@163.com X-Patchwork-Id: 1884355 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=fQ8yWkgC; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=patchwork.ozlabs.org) Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4T8Rpk3HsYz1yP7 for ; Tue, 9 Jan 2024 21:23:26 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 3CE823CE51F for ; Tue, 9 Jan 2024 11:23:24 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-7.smtp.seeweb.it (in-7.smtp.seeweb.it [IPv6:2001:4b78:1:20::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by picard.linux.it (Postfix) with ESMTPS id 7C0DE3CB8AB for ; Mon, 8 Jan 2024 14:51:01 +0100 (CET) Received: from m12.mail.163.com (m12.mail.163.com [220.181.12.217]) by in-7.smtp.seeweb.it (Postfix) with ESMTP id D4DCD200AD5 for ; Mon, 8 Jan 2024 14:50:59 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=HcP3N Wj9V4wkC8qpxcwGoUijSS0s37y+btjFBVFiSwk=; b=fQ8yWkgC3MHEIYDW2RoKy rOnYHk5dO6FfxzZdCnKJiNBASMesGU4yT+EhhkfmTeJ2Z6j8vCFpLLvbBIxnF+wO TciecqNujbMHjN40CV8NDtYFAeYj8pqng3IkZSv/iMNbo1XjKKNJul5t4FyVGHnl urAZMGQwUAYa8KSNZ2/Cuk= Received: from ubuntu22.localdomain (unknown [117.176.219.50]) by zwqz-smtp-mta-g5-1 (Coremail) with SMTP id _____wDXv2fA_ZtlD5BxAg--.912S2; Mon, 08 Jan 2024 21:50:56 +0800 (CST) From: chenguanxi11234@163.com To: ltp@lists.linux.it Date: Mon, 8 Jan 2024 21:50:54 +0800 Message-Id: X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-CM-TRANSID: _____wDXv2fA_ZtlD5BxAg--.912S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxJw15Jw1xJw15Gr4kuryrCrg_yoW5CF4DpF Z3Aayvyr95Jr1Sgry7AFsFvF1FkrZ3JFyUJry7uw1jy3sayr1Yyr4ktry5JF4xu392gFWr CrWrtFyfX390qr7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRvzuNUUUUU= X-Originating-IP: [117.176.219.50] X-CM-SenderInfo: xfkh0wxxdq5xirrsjki6rwjhhfrp/1tbiVAFf+mVOA6BPuQAAsO X-Virus-Scanned: clamav-milter 1.0.3 at in-7.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.3 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=disabled version=4.0.0 X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on in-7.smtp.seeweb.it X-Mailman-Approved-At: Tue, 09 Jan 2024 11:23:10 +0100 Subject: [LTP] [PATCH] posix/conformance/interfaces/sem_timedwait/2-1: Fix test X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yang.guang5@zte.com.cn, Chen Haonan Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" From: Chen Haonan Since the parent and child processes are not operating on the same semaphore, this code wasn't doing its job correctly before, so we mapped the semaphore to a piece of shared memory and changed some implementation details in the original code to make it work. Signed-off-by: Chen Haonan --- .../interfaces/sem_timedwait/2-1.c | 41 ++++++++++++++----- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/testcases/open_posix_testsuite/conformance/interfaces/sem_timedwait/2-1.c b/testcases/open_posix_testsuite/conformance/interfaces/sem_timedwait/2-1.c index 655e35108..3082140e3 100644 --- a/testcases/open_posix_testsuite/conformance/interfaces/sem_timedwait/2-1.c +++ b/testcases/open_posix_testsuite/conformance/interfaces/sem_timedwait/2-1.c @@ -10,13 +10,24 @@ * will unlock the semaphore from another process. */ +/* Modifications by: Chen Haonan + * Date: 2024-01-08 + * + * Since the parent and child processes are not operating on + * the same semaphore, this code wasn't doing its job correctly + * before, so we mapped the semaphore to a piece of shared + * memory and changed some implementation details in the + * original code to make it work. + */ +#include #include #include #include #include #include #include +#include #include #include #include @@ -28,12 +39,16 @@ int main(void) { - sem_t mysemp; + sem_t *mysemp; struct timespec ts; int pid; - /* Semaphore started out locked */ - if (sem_init(&mysemp, 0, 0) == -1) { + /* Map semaphore to shared memory */ + int shm_fd = shm_open("/myshm", O_CREAT | O_RDWR, 0666); + + ftruncate(shm_fd, sizeof(sem_t)); + mysemp = (sem_t *)mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); + + if (sem_init(mysemp, 1, 0) == -1) { perror(ERROR_PREFIX "sem_init"); return PTS_UNRESOLVED; } @@ -43,20 +58,22 @@ int main(void) { ts.tv_sec = time(NULL) + 2; ts.tv_nsec = 0; - - if (sem_timedwait(&mysemp, &ts) == -1) { + if (sem_timedwait(mysemp, &ts) == -1) { puts("TEST FAILED"); + sem_destroy(mysemp); + munmap(mysemp, sizeof(sem_t)); + close(shm_fd); + shm_unlink("/myshm"); return PTS_FAIL; } else { - puts("TEST PASSED"); - sem_destroy(&mysemp); + puts("TEST PASSED"); return PTS_PASS; } } else if (pid > 0) // parent to unlock semaphore { int i; sleep(1); - if (sem_post(&mysemp) == -1) { + if (sem_post(mysemp) == -1) { perror(ERROR_PREFIX "sem_post"); return PTS_FAIL; } @@ -64,12 +81,14 @@ int main(void) perror("Error waiting for child to exit"); return PTS_UNRESOLVED; } - - if (!WEXITSTATUS(i)) { + if (WEXITSTATUS(i)) { return PTS_FAIL; } puts("TEST PASSED"); - sem_destroy(&mysemp); + sem_destroy(mysemp); + munmap(mysemp, sizeof(sem_t)); + close(shm_fd); + shm_unlink("/myshm"); return PTS_PASS; } return PTS_UNRESOLVED;