From patchwork Thu Jun 27 16:20:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Cermak X-Patchwork-Id: 1953425 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Aekf+nXy; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (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 4W93hx3713z20Xg for ; Fri, 28 Jun 2024 02:21:05 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 87838385DDCD for ; Thu, 27 Jun 2024 16:21:02 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id A65CE3858D26 for ; Thu, 27 Jun 2024 16:20:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A65CE3858D26 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A65CE3858D26 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719505245; cv=none; b=QxAYQkO+6AoeCSJrQaORz6IX7tpce8il5sTqgBSzRTVx6PuhxJK0vEbeHNJS2UaS8IlgP8kC8dPuHEcbpsbGYGqBxGAZ3i/jCzDJGmTU5mrr0oFDkgF5gC14zOrPSQQdg7leHvDcBzrvKWO2V99EwziXh3ggccmesgGYD3pTIeI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719505245; c=relaxed/simple; bh=DKwDBSrHjkyfMmW6qawYpeMt8S0OrVGILXAeHPnlA1I=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=gmjAaqmkPIHd3teiYDs5mx4isLX/kYTmdk6SjIXEO0QXJ9kcSJvKs6UJisnTQwN845Zhyho+QGqbHQzQsaeR5LxJSzIwUwPnvEpM2MG0p/b3yK8PKHtalsFlKcRlOqukWbwzjjUKNZiTdE3orVBeQPW4hsS3n3/nVefestuYPu0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719505243; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=NQH3GZpir43fv2QB8vgwyfIqH78m9/NtLA8hf5ZzTZk=; b=Aekf+nXy4groGRN2BzKBuCB5Q1zCk37jDvoKRtV0kroHjBScH8im4M2nB18E+OxGRz/2wq 0JvypV00Vu+s0TQx8remwLoKUTnQ7ifoCAYq7m8h74yNYsmUVkwQSyY0OviGDDrP+wTDfS IJzpwQ2ypW7roARtwm9QQXhb6yxA1Wo= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-389-a3FaGXd0PcmfwDIubg8nVg-1; Thu, 27 Jun 2024 12:20:41 -0400 X-MC-Unique: a3FaGXd0PcmfwDIubg8nVg-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0264E19560B5 for ; Thu, 27 Jun 2024 16:20:41 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.39.208.8]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B6BDE1945CB1; Thu, 27 Jun 2024 16:20:39 +0000 (UTC) From: Martin Cermak To: libc-alpha@sourceware.org Cc: arjun@redhat.com Subject: [PATCH] misc: Add new test for daemon Date: Thu, 27 Jun 2024 18:20:06 +0200 Message-Id: <20240627162005.407679-1-mcermak@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org This commit adds a new testcase for the daemon function. --- The dev_null_redir function may be too linux specific. Not sure about a better way to do it. --- misc/Makefile | 1 + misc/tst-daemon.c | 155 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 misc/tst-daemon.c diff --git a/misc/Makefile b/misc/Makefile index c273ec6974..7313f9122d 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -232,6 +232,7 @@ gpl2lgpl := \ tests := \ bug-hsearch1 \ bug18240 \ + tst-daemon \ tst-dbl-efgcvt \ tst-dirname \ tst-empty \ diff --git a/misc/tst-daemon.c b/misc/tst-daemon.c new file mode 100644 index 0000000000..9ab72781e1 --- /dev/null +++ b/misc/tst-daemon.c @@ -0,0 +1,155 @@ +/* Tests for daemon. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include + +/* Define pipe communication encoding. */ +#define STATUS_PASS 'p' +#define STATUS_FAIL 'f' + +/* Check if given file descriptor is redirected to /dev/null. */ +unsigned char +dev_null_redir (FILE * f) +{ + char b1[1024]; + char b2[1024]; + ssize_t len; + + int fd = fileno (f); + + len = snprintf (b1, sizeof (b1), "/proc/self/fd/%d", fd); + if (len < 0 || len >= sizeof (b1)) + return 1; + + len = readlink (b1, b2, sizeof (b2) - 1); + if (len < 0) + return 1; + + b2[len] = '\0'; + + if (strcmp (b2, "/dev/null") != 0) + return 1; + + return 0; +} + +/* Run one daemon test with specified parameters. */ +void +test_daemon (int nochdir, int noclose) +{ + pid_t pid; + int status; + int pipefd[2]; + char buf; + + if (pipe (pipefd) == -1) + error (EXIT_FAILURE, errno, "failed to create the pipe"); + + pid = fork (); + if (pid == 0) + { + /* This is the child. */ + char cwd[1024]; + char origcwd[1024]; + char retval = STATUS_PASS; + + getcwd (origcwd, sizeof (origcwd)); + + /* Daemonize the child. */ + if (daemon (nochdir, noclose) != 0) + retval = STATUS_FAIL; + + switch (nochdir) + { + case 0: + getcwd (cwd, sizeof (cwd)); + if (strcmp (cwd, "/") != 0) + retval = STATUS_FAIL; + break; + default: + getcwd (cwd, sizeof (cwd)); + if (strcmp (cwd, origcwd) != 0) + retval = STATUS_FAIL; + } + + switch (noclose) + { + case 0: + if (dev_null_redir (stdin) != 0) + retval = STATUS_FAIL; + if (dev_null_redir (stdout) != 0) + retval = STATUS_FAIL; + if (dev_null_redir (stderr) != 0) + retval = STATUS_FAIL; + break; + default: + if (dev_null_redir (stdin) != 1) + retval = STATUS_FAIL; + if (dev_null_redir (stdout) != 1) + retval = STATUS_FAIL; + if (dev_null_redir (stderr) != 1) + retval = STATUS_FAIL; + } + + /* Report test result to the parent. */ + close (pipefd[0]); + write (pipefd[1], &retval, 1); + close (pipefd[1]); + + _exit (0); + } + else if (pid < 0) + /* Something went wrong. */ + error (EXIT_FAILURE, errno, "cannot fork"); + + /* This is the parent. */ + if (waitpid (pid, &status, 0) != pid) + error (EXIT_FAILURE, 0, "oops, wrong test program terminated"); + + if (WTERMSIG (status) != 0) + error (EXIT_FAILURE, 0, "child terminated incorrectly"); + status = WEXITSTATUS (status); + + /* Read test result from the daemon and report it. */ + close (pipefd[1]); + if (read (pipefd[0], &buf, 1) != 1) + error (EXIT_FAILURE, 0, "failed reading daemon status"); + close (pipefd[0]); + TEST_VERIFY_EXIT (buf == STATUS_PASS); +} + + +int +do_test (void) +{ + /* Run daemon tests with various parameters. */ + test_daemon (0, 0); + test_daemon (1, 0); + test_daemon (0, 1); + test_daemon (1, 1); + + return 0; +} + +#include