From patchwork Mon Apr 12 21:11:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 42951 X-Patchwork-Delegate: carlos@redhat.com 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 4FF79394FC1B; Mon, 12 Apr 2021 21:11:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4FF79394FC1B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1618261884; bh=Ldg+W/dOmjZc6BBcE3BhNLzNp89hQoKwwkuItqvrJ+s=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=Qgaqqx3wu9N6DHtKYAbv285RgjdFc+KWLl3EwivJwLRqx25l2aGxUcHg/+QwiqC7N qldw/IaAs/QOtbOFtS7wsGf0wn5MvBVX7xBwz2+HGk81nx1qeUbPQY6N7GYuAArS/O fX0Ud1HX9DI2WfzGW+nuBRMSbvFagwUbOM7shUAY= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x82e.google.com (mail-qt1-x82e.google.com [IPv6:2607:f8b0:4864:20::82e]) by sourceware.org (Postfix) with ESMTPS id 148F4385480B for ; Mon, 12 Apr 2021 21:11:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 148F4385480B Received: by mail-qt1-x82e.google.com with SMTP id i6so1499677qti.10 for ; Mon, 12 Apr 2021 14:11:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ldg+W/dOmjZc6BBcE3BhNLzNp89hQoKwwkuItqvrJ+s=; b=n3H/NEuQIj2X/OuniYznXrCMdYHwMH/VNzgclMzan/ZE8NyLdiwDAEb6whR5MEAHm6 7q9rqXmKMkfVDTpzSyBnfy94DVkT9OYkp1XD+BP5GMyRufrFg8XDx3FIxllSUgGSoz/A z9hpguC1wIkAmTlWNqkA8VcS5Xl/v46UgQtsudFFWITDulxEqA4E97D61VMZygQQTt/m JwDek3YHPi92OILPB306PPaBbVmXFQpNB730cZTgh1NhUjuUt3P8ZirmoS7hL2rBa7qQ HogU+74p98B9JhgZLQjsSv5h6bh4PYm0ZWezbo+PbmZP36ryIQp/u6spdsWk6TMepR91 W0lA== X-Gm-Message-State: AOAM531Ezpi7V5VAwrb0ilRmtgvCZIxsEz9vXY36i1ZzOM0e0Bc0ruM+ q2bHAxvFZajOQ47VWpCri70mpZ3zKcT/J7GM X-Google-Smtp-Source: ABdhPJxZhmE/hMZPLQqJB8st1nKOm9Azgl23iUePr8GpY9sJBCUHBzTwWDu45SZyQrzEjQEFwKY7Bg== X-Received: by 2002:ac8:7dd2:: with SMTP id c18mr27673820qte.301.1618261879239; Mon, 12 Apr 2021 14:11:19 -0700 (PDT) Received: from localhost.localdomain ([177.194.41.149]) by smtp.googlemail.com with ESMTPSA id i22sm3073392qko.135.2021.04.12.14.11.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Apr 2021 14:11:19 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 02/11] misc: Add syslog test Date: Mon, 12 Apr 2021 18:11:04 -0300 Message-Id: <20210412211113.393120-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210412211113.393120-1-adhemerval.zanella@linaro.org> References: <20210412211113.393120-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" The test cover: - All possible priorities and facilities through TCP and UDP. - Same syslog tests for vsyslog. - Some openlog/syslog/close combinations. - openlog with LOG_CONS, LOG_PERROR, and LOG_PID. Internally is done with a test-container where the main process mimics the syslog server interface. The test does not cover multithread and async-signal usage. Checked on x86_64-linux-gnu. --- misc/Makefile | 2 + misc/tst-syslog.c | 483 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 485 insertions(+) create mode 100644 misc/tst-syslog.c diff --git a/misc/Makefile b/misc/Makefile index 38dad737f2..63812124f2 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -107,6 +107,8 @@ tests-special += $(objpfx)tst-error1-mem.out \ $(objpfx)tst-allocate_once-mem.out endif +tests-container := tst-syslog + CFLAGS-select.c += -fexceptions -fasynchronous-unwind-tables CFLAGS-tsearch.c += $(uses-callbacks) CFLAGS-lsearch.c += $(uses-callbacks) diff --git a/misc/tst-syslog.c b/misc/tst-syslog.c new file mode 100644 index 0000000000..ed8c528db4 --- /dev/null +++ b/misc/tst-syslog.c @@ -0,0 +1,483 @@ +/* Basic tests for syslog interfaces. + Copyright (C) 2021 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const int facilities[] = + { + LOG_KERN, + LOG_USER, + LOG_MAIL, + LOG_DAEMON, + LOG_AUTH, + LOG_SYSLOG, + LOG_LPR, + LOG_NEWS, + LOG_UUCP, + LOG_CRON, + LOG_AUTHPRIV, + LOG_FTP, + LOG_LOCAL0, + LOG_LOCAL1, + LOG_LOCAL2, + LOG_LOCAL3, + LOG_LOCAL4, + LOG_LOCAL5, + LOG_LOCAL6, + LOG_LOCAL7, + }; + +static const int priorities[] = + { + LOG_EMERG, + LOG_ALERT, + LOG_CRIT, + LOG_ERR, + LOG_WARNING, + LOG_NOTICE, + LOG_INFO, + LOG_DEBUG + }; + +enum + { + ident_length = 64, + msg_length = 64 + }; + +#define SYSLOG_MSG_BASE "syslog_message" +#define OPENLOG_IDENT "openlog_ident" + +struct msg_t + { + int priority; + int facility; + char ident[ident_length]; + char msg[msg_length]; + pid_t pid; + }; + +static void +call_vsyslog (int priority, const char *format, ...) +{ + va_list ap; + va_start (ap, format); + vsyslog (priority, format, ap); + va_end (ap); +} + +static void +send_vsyslog (int options) +{ + for (size_t i = 0; i < array_length (facilities); i++) + { + for (size_t j = 0; j < array_length (priorities); j++) + { + int facility = facilities[i]; + int priority = priorities[j]; + call_vsyslog (facility | priority, "%s %d %d", SYSLOG_MSG_BASE, + facility, priority); + } + } +} + +static void +send_syslog (int options) +{ + for (size_t i = 0; i < array_length (facilities); i++) + { + for (size_t j = 0; j < array_length (priorities); j++) + { + int facility = facilities[i]; + int priority = priorities[j]; + syslog (facility | priority, "%s %d %d", SYSLOG_MSG_BASE, facility, + priority); + } + } +} + +static bool +check_syslog_message (const struct msg_t *msg, int msgnum, int options, + pid_t pid) +{ + if (msgnum == array_length (facilities) * array_length (priorities) - 1) + return false; + + int i = msgnum / array_length (priorities); + int j = msgnum % array_length (priorities); + + int expected_facility = facilities[i]; + /* With no preceding openlog, syslog default to LOG_USER. */ + if (expected_facility == LOG_KERN) + expected_facility = LOG_USER; + int expected_priority = priorities[j]; + + TEST_COMPARE (msg->facility, expected_facility); + TEST_COMPARE (msg->priority, expected_priority); + + return true; +} + +static void +send_openlog (int options) +{ + /* Define a non-default IDENT and a not default facility. */ + openlog (OPENLOG_IDENT, options, LOG_LOCAL0); + for (size_t j = 0; j < array_length (priorities); j++) + { + int priority = priorities[j]; + syslog (priority, "%s %d %d", SYSLOG_MSG_BASE, LOG_LOCAL0, priority); + } + closelog (); + + /* Back to the default IDENT with a non default facility. */ + openlog (NULL, 0, LOG_LOCAL6); + for (size_t j = 0; j < array_length (priorities); j++) + { + int priority = priorities[j]; + syslog (LOG_LOCAL7 | priority, "%s %d %d", SYSLOG_MSG_BASE, LOG_LOCAL7, + priority); + } + closelog (); + + /* LOG_KERN does not change the internal default facility. */ + openlog (NULL, 0, LOG_KERN); + for (size_t j = 0; j < array_length (priorities); j++) + { + int priority = priorities[j]; + syslog (priority, "%s %d %d", SYSLOG_MSG_BASE, LOG_KERN, priority); + } + closelog (); +} + +static bool +check_openlog_message (const struct msg_t *msg, int msgnum, + int options, pid_t pid) +{ + if (msgnum == 3 * array_length (priorities) - 1) + return false; + + int expected_priority = priorities[msgnum % array_length (priorities)]; + TEST_COMPARE (msg->priority, expected_priority); + + char expected_ident[ident_length]; + snprintf (expected_ident, sizeof (expected_ident), "%s%s%.0d%s:", + OPENLOG_IDENT, + options & LOG_PID ? "[" : "", + options & LOG_PID ? pid : 0, + options & LOG_PID ? "]" : ""); + + if (msgnum < array_length (priorities)) + { + if (options & LOG_PID) + TEST_COMPARE (msg->pid, pid); + TEST_COMPARE_STRING (msg->ident, expected_ident); + TEST_COMPARE (msg->facility, LOG_LOCAL0); + } + else if (msgnum < 2 * array_length (priorities)) + { + TEST_COMPARE (msg->facility, LOG_LOCAL7); + } + /* Since openlog (LOG_KERN) does not change the facility, we need to check + against the previous valid openlog. */ + else if (msgnum < 3 * array_length (priorities)) + { + TEST_COMPARE (msg->facility, LOG_LOCAL6); + } + + return true; +} + +static struct msg_t +parse_syslog_msg (const char *msg) +{ + struct msg_t r = { .pid = -1 }; + int number; + + /* The message in the form: + <179>Apr 8 14:51:19 tst-syslog: syslog message 176 3 */ + int n = sscanf (msg, "<%3d>%*s %*d %*d:%*d:%*d %32s %64s %*d %*d", + &number, r.ident, r.msg); + TEST_COMPARE (n, 3); + + r.facility = number & LOG_FACMASK; + r.priority = number & LOG_PRIMASK; + + char *pid_start = strchr (r.ident, '['); + if (pid_start != NULL) + { + char *pid_end = strchr (r.ident, ']'); + if (pid_end != NULL) + r.pid = strtoul (pid_start + 1, NULL, 10); + } + + return r; +} + +static struct msg_t +parse_syslog_console (const char *msg) +{ + int priority; + int facility; + struct msg_t r; + + /* The message in the form: + openlog_ident: syslog_message 128 0 */ + int n = sscanf (msg, "%32s %64s %d %d", + r.ident, r.msg, &facility, &priority); + TEST_COMPARE (n, 4); + + r.facility = facility; + r.priority = priority; + + return r; +} + +static void +check_syslog_udp (void (*syslog_send)(int), int options, + bool (*syslog_check)(const struct msg_t *, int, int, + pid_t)) +{ + struct sockaddr_un addr = + { + .sun_family = AF_UNIX, + .sun_path = _PATH_LOG + }; + + socklen_t addrlen = sizeof (addr); + int server_udp = xsocket (AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0); + xbind (server_udp, (struct sockaddr *) &addr, addrlen); + + pid_t sender_pid = xfork (); + if (sender_pid == 0) + { + syslog_send (options); + _exit (0); + } + + int msgnum = 0; + while (1) + { + char buf[512]; + size_t l = xrecvfrom (server_udp, buf, sizeof (buf), 0, + (struct sockaddr *) &addr, &addrlen); + buf[l] = '\0'; + + struct msg_t msg = parse_syslog_msg (buf); + if (!syslog_check (&msg, msgnum++, options, sender_pid)) + break; + } + + xclose (server_udp); + + int status; + xwaitpid (sender_pid, &status, 0); + TEST_COMPARE (status, 0); + + unlink (_PATH_LOG); +} + +static void +check_syslog_tcp (void (*syslog_send)(int), int options, + bool (*syslog_check)(const struct msg_t *, int, int, + pid_t)) +{ + struct sockaddr_un addr = + { + .sun_family = AF_UNIX, + .sun_path = _PATH_LOG + }; + socklen_t addrlen = sizeof (addr); + + int server_tcp = xsocket (AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + xbind (server_tcp, (struct sockaddr *) &addr, addrlen); + xlisten (server_tcp, 5); + + pid_t sender_pid = xfork (); + if (sender_pid == 0) + { + syslog_send (options); + _exit (0); + } + + int client_tcp = xaccept (server_tcp, NULL, NULL); + + char buf[512], *rb = buf; + size_t rbl = sizeof (buf); + size_t prl = 0; /* Track the size of the partial record. */ + int msgnum = 0; + + while (1) + { + size_t rl = xrecvfrom (client_tcp, rb, rbl - prl, 0, NULL, NULL); + if (rl == 0) + break; + + /* Iterate over the buffer to find and check the record. */ + size_t l = rl + prl; + char *b = buf; + while (1) + { + /* With TCP each record ends with a '\0'. */ + char *e = memchr (b, '\0', l); + if (e != NULL) + { + struct msg_t msg = parse_syslog_msg (b); + if (!syslog_check (&msg, msgnum++, options, sender_pid)) + break; + + /* Advance to the next record. */ + ptrdiff_t diff = e + 1 - b; + b += diff; + l -= diff; + } + else + { + /* Move the partial record to the start of the buffer. */ + memmove (buf, b, l); + rb = buf + l; + prl = l; + break; + } + } + } + + xclose (client_tcp); + xclose (server_tcp); + + int status; + xwaitpid (sender_pid, &status, 0); + TEST_COMPARE (status, 0); + + unlink (_PATH_LOG); +} + +static void +check_syslog_console_read (FILE *fp) +{ + char buf[512]; + int msgnum = 0; + while (fgets (buf, sizeof (buf), fp) != NULL) + { + struct msg_t msg = parse_syslog_console (buf); + TEST_COMPARE_STRING (msg.ident, OPENLOG_IDENT ":"); + TEST_COMPARE (msg.priority, priorities[msgnum]); + TEST_COMPARE (msg.facility, LOG_LOCAL0); + + if (++msgnum == array_length (priorities)) + break; + } +} + +static void +check_syslog_console (void) +{ + xmkfifo (_PATH_CONSOLE, 0666); + + pid_t sender_pid = xfork (); + if (sender_pid == 0) + { + send_openlog (LOG_CONS); + _exit (0); + } + + { + FILE *fp = xfopen (_PATH_CONSOLE, "r+"); + check_syslog_console_read (fp); + xfclose (fp); + } + + int status; + xwaitpid (sender_pid, &status, 0); + TEST_COMPARE (status, 0); + + unlink (_PATH_CONSOLE); +} + +static void +send_openlog_callback (void *clousure) +{ + int options = *(int *) clousure; + send_openlog (options); +} + +static void +check_syslog_perror (void) +{ + struct support_capture_subprocess result; + result = support_capture_subprocess (send_openlog_callback, + &(int){LOG_PERROR}); + + FILE *mfp = fmemopen (result.err.buffer, result.err.length, "r"); + if (mfp == NULL) + FAIL_EXIT1 ("fmemopen: %m"); + check_syslog_console_read (mfp); + xfclose (mfp); + + support_capture_subprocess_check (&result, "tst-openlog-child", 0, + sc_allow_stderr); + support_capture_subprocess_free (&result); +} + +static int +do_test (void) +{ + add_temp_file (_PATH_LOG); + add_temp_file (_PATH_CONSOLE); + + /* Send every combination of facility/priority over UDP and TCP. */ + check_syslog_udp (send_syslog, 0, check_syslog_message); + check_syslog_tcp (send_syslog, 0, check_syslog_message); + + /* Also check vsyslog. */ + check_syslog_udp (send_vsyslog, 0, check_syslog_message); + check_syslog_tcp (send_vsyslog, 0, check_syslog_message); + + /* Run some openlog/syslog/closelog combinations. */ + check_syslog_udp (send_openlog, 0, check_openlog_message); + check_syslog_tcp (send_openlog, 0, check_openlog_message); + + /* Check the LOG_PID option. */ + check_syslog_udp (send_openlog, LOG_PID, check_openlog_message); + check_syslog_tcp (send_openlog, LOG_PID, check_openlog_message); + + /* Check the LOG_CONS option. */ + check_syslog_console (); + + /* Check the LOG_PERROR option. */ + check_syslog_perror (); + + return 0; +} + +#include