From patchwork Fri Feb 18 14:23:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 51207 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 C459A385DC18 for ; Fri, 18 Feb 2022 14:24:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C459A385DC18 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1645194271; bh=yLtoGvS6xckQHPl0gjw6Ytmpjx0eAYqmfl2aBNnEpXg=; 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=JawrNuAjnpde0vq3NICicu9MoLoOrHFt28u2IY+zz8gUUksXQJAzt4h6pWZzITU8V r8SgtYonHepmoECjl077w2NRKctlCXh0iOSOfqXpBP26/NHvrzyIJy24bH0V3UZWxV A+IGli7vfxyfF4IewLLA+lwjvMGHnf3rzYFB0qG8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc33.google.com (mail-oo1-xc33.google.com [IPv6:2607:f8b0:4864:20::c33]) by sourceware.org (Postfix) with ESMTPS id 259E6385DC18 for ; Fri, 18 Feb 2022 14:23:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 259E6385DC18 Received: by mail-oo1-xc33.google.com with SMTP id w10-20020a4ae08a000000b0031bdf7a6d76so3637881oos.10 for ; Fri, 18 Feb 2022 06:23:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yLtoGvS6xckQHPl0gjw6Ytmpjx0eAYqmfl2aBNnEpXg=; b=ZdNJ+CRrbUTddpz98/Gc56cSteZuYLGEijp8zERB3jgWIDmxp+H+4zHLBNm5d2tAL9 qrhtB1Dm2oOK3F6YKoo72+m7sDnUBQCxoGe2r6RlgdtYH3mKLCk5TA+/UM/m+G3LK2Je Pt5uqv2NxmdLVKUoZiF139r4Y9OFtBYnBMh/5w21ez9T+aTHcKxbAp+Kaub6AEZMNWfs HURoUgZCQvmng8pQe1HvAjqFkjzndaTz3XX1T8kmuMdkIQQHjqJqOGO4JGIBdfF5kqCd 1imxVDxagrg2mjvVkA0UmMai+2UKDjTrQbBt43NTuk+gb/2fVt2jngd5NVeaPQMoFqiN hkkg== X-Gm-Message-State: AOAM533HcevfjPlY6yDM2Ahvn89cPwJt44YVsy4jkSiCqwMwnd2E03+K R/5QOML84Dv7XpdJisBVv8iz8l1ZbhTjNQ== X-Google-Smtp-Source: ABdhPJysg8skiFRqONLYVyMBKdlrJMYjYbDg1HmfiWgL5Jl35SU0Z7Md5684U+bnU9aFqaUx58LM9Q== X-Received: by 2002:a05:6870:538e:b0:c4:7dc0:d72e with SMTP id h14-20020a056870538e00b000c47dc0d72emr2954025oan.257.1645194208142; Fri, 18 Feb 2022 06:23:28 -0800 (PST) Received: from birita.. ([2804:431:c7cb:6747:90e1:2029:f1e0:f47a]) by smtp.gmail.com with ESMTPSA id bp5sm702993oib.25.2022.02.18.06.23.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Feb 2022 06:23:27 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v2 1/7] support: Add xmkfifo Date: Fri, 18 Feb 2022 11:23:16 -0300 Message-Id: <20220218142322.432304-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220218142322.432304-1-adhemerval.zanella@linaro.org> References: <20220218142322.432304-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.2 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, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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 Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Wrapper support mkfifo. --- support/Makefile | 1 + support/xmkfifo.c | 29 +++++++++++++++++++++++++++++ support/xunistd.h | 1 + 3 files changed, 31 insertions(+) create mode 100644 support/xmkfifo.c diff --git a/support/Makefile b/support/Makefile index 5ddcb8d158..c3609e211b 100644 --- a/support/Makefile +++ b/support/Makefile @@ -133,6 +133,7 @@ libsupport-routines = \ xmemstream \ xmkdir \ xmkdirp \ + xmkfifo \ xmmap \ xmprotect \ xmunmap \ diff --git a/support/xmkfifo.c b/support/xmkfifo.c new file mode 100644 index 0000000000..a8e196dbc2 --- /dev/null +++ b/support/xmkfifo.c @@ -0,0 +1,29 @@ +/* mkfifo with error checking. + Copyright (C) 2022 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 + +void +xmkfifo (const char *pathname, mode_t mode) +{ + int r = mkfifo (pathname, mode); + if (r < 0) + FAIL_EXIT1 ("mkfifo (%s, 0%o): %m", pathname, mode); +} diff --git a/support/xunistd.h b/support/xunistd.h index 0454d83cf1..960a62d412 100644 --- a/support/xunistd.h +++ b/support/xunistd.h @@ -61,6 +61,7 @@ void xsymlink (const char *target, const char *linkpath); void xchdir (const char *path); void xfchmod (int fd, mode_t mode); void xchmod (const char *pathname, mode_t mode); +void xmkfifo (const char *pathname, mode_t mode); /* Equivalent of "mkdir -p". */ void xmkdirp (const char *, mode_t); From patchwork Fri Feb 18 14:23:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 51208 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 05E49385380C for ; Fri, 18 Feb 2022 14:25:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 05E49385380C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1645194314; bh=vvb7l/clATDspoO1ZOcDsHcB5X2PrV5lk1FiJfE/Q6g=; 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=rZ+bKXQzapuzhs5ZPjChSjt+/c5K8X1QyDgGbAnkK1FtZZkhmHEMLjbB0kUahyV4b 262NPgM5g+799YqQIoQyo80WGjh2BPhQtNrf3ALkmMCXiXjkAOliHY1DjLV6QY+PfD uAyNlDozUWkSauf4kk7N/pdGGm1v6GXi1ebGvw8E= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x22a.google.com (mail-oi1-x22a.google.com [IPv6:2607:f8b0:4864:20::22a]) by sourceware.org (Postfix) with ESMTPS id D83A6385BF93 for ; Fri, 18 Feb 2022 14:23:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D83A6385BF93 Received: by mail-oi1-x22a.google.com with SMTP id i5so3248051oih.1 for ; Fri, 18 Feb 2022 06:23:30 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=vvb7l/clATDspoO1ZOcDsHcB5X2PrV5lk1FiJfE/Q6g=; b=ojbWLfyt66SKWyBZwnx43Gf97VrYsrywD11WZ+gekf5xSizuJgIMOKqSXXlS+pogm0 Yj5smEburUVA5CUFRuTUx24hGGEyzllBLTL/jsh1EwIap+3HXjhvVkSkP6Hr3yunAynY ObYyTc424zzt32sQ4sywEVKVRlJ1Kggjme4wYMMGUoUNugeC+oj31qQhm6deL4C4yI3d y87U+jfMgsCoIggumwITonyh7lPkaepqyUniHBSa1WTIiRwnOpwBYRZBVIuu1V6b5mnc CwF8aNDUmNJhyC/dgMslOoEBfFpgGF2b+062UgAd7W+bS5xU7jm4vPQL061/zoA2RvKD TUNQ== X-Gm-Message-State: AOAM530lY5BYvaPbCej+YikFVJqp7M/F1ogXx9VSnJeeDv9i1rYU8+51 t37Cx6iM6ZjE0RTcjo7z9gpt67Iwk5n9ZQ== X-Google-Smtp-Source: ABdhPJwFBEzP6ETRoAGLEBHT0q0ciUWBH2BDFpX+Ehvgclmhz9kcYOSpBw7gHaKhtUc9QKTDH7zyMg== X-Received: by 2002:aca:3685:0:b0:2cc:cf2a:1dc7 with SMTP id d127-20020aca3685000000b002cccf2a1dc7mr5009018oia.124.1645194209785; Fri, 18 Feb 2022 06:23:29 -0800 (PST) Received: from birita.. ([2804:431:c7cb:6747:90e1:2029:f1e0:f47a]) by smtp.gmail.com with ESMTPSA id bp5sm702993oib.25.2022.02.18.06.23.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Feb 2022 06:23:29 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v2 2/7] misc: Add syslog test Date: Fri, 18 Feb 2022 11:23:17 -0300 Message-Id: <20220218142322.432304-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220218142322.432304-1-adhemerval.zanella@linaro.org> References: <20220218142322.432304-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 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, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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 Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@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 | 477 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 479 insertions(+) create mode 100644 misc/tst-syslog.c diff --git a/misc/Makefile b/misc/Makefile index 3d8a569d06..ba8232a0e9 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -115,6 +115,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..8505178616 --- /dev/null +++ b/misc/tst-syslog.c @@ -0,0 +1,477 @@ +/* Basic tests for syslog interfaces. + Copyright (C) 2022 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); + else if (msgnum < 3 * array_length (priorities)) + TEST_COMPARE (msg->facility, LOG_KERN); + + 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 From patchwork Fri Feb 18 14:23:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 51209 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 CCF083853812 for ; Fri, 18 Feb 2022 14:26:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CCF083853812 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1645194360; bh=AKmp2wR1eiLVWaixnsY2FS6izFXn2sPTtkDSK3etVhA=; 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=NnBu59y0Ct50fM+fRD+ZhwkOimM9n9OONwXfwOb6uURPgBDye/b2S6k/xakILPYnF EjO+3a9Lz2eucyux2ogGWrld8Y043CDH460E7EBHwjveEas9We7oJ6D6kd+dE9Lbyw c/TQlxONndLLUfC+hWuduyfFYne6WA4kO0uA6wuY= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) by sourceware.org (Postfix) with ESMTPS id 83B733853809 for ; Fri, 18 Feb 2022 14:23:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 83B733853809 Received: by mail-oi1-x235.google.com with SMTP id s8so3200458oij.13 for ; Fri, 18 Feb 2022 06:23:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AKmp2wR1eiLVWaixnsY2FS6izFXn2sPTtkDSK3etVhA=; b=Q12FWezIRN4QhrSREgOuDU9zlX2MYpIVFziSRWU+bxzLxc057laiI0ZQg2UhsgzXeC wDVpe6hWVjFD9f7HCbtIdK/+/YasS7+ShKJxIKwy7E1CKbrAfdeM0S0eUK5Pf1S2kuBv uPINUCmJ7EysClVFtT4YfVrIpJGrH9TJkv4xXaA/1YsI4JdLrKdpVzxBQerw9BP4iNBv 5wTSeNFsNk4t/ZwwmIQFtMdRe9RSved246Gvj8pMGN7sUE5cLvrdDzmTnZ6KVI0sv6JJ NpqzX5qZe1e6dsSplhZRHI7NTSxsyi1NoDxwk5q6wzsTidJ0FxzblDnnJio3wCbOWPTx PzFg== X-Gm-Message-State: AOAM530f/hfooW1/K0tKXiTnopefLUaET+qf9feZmbYctnZ5bUBfZA3J LxIDGAfjw452dGWlBreDUu58TswqlX0djA== X-Google-Smtp-Source: ABdhPJwWOWNKPUJDH7qT7L6amfk8AFU13ZjtSwlvDPgKOcvoU09X8skmhgrpqlVj50uNO+6pF3Wk/w== X-Received: by 2002:a05:6808:300d:b0:2cf:b34:615b with SMTP id ay13-20020a056808300d00b002cf0b34615bmr4880669oib.329.1645194211277; Fri, 18 Feb 2022 06:23:31 -0800 (PST) Received: from birita.. ([2804:431:c7cb:6747:90e1:2029:f1e0:f47a]) by smtp.gmail.com with ESMTPSA id bp5sm702993oib.25.2022.02.18.06.23.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Feb 2022 06:23:30 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v2 3/7] misc: syslog: Fix indentation and style Date: Fri, 18 Feb 2022 11:23:18 -0300 Message-Id: <20220218142322.432304-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220218142322.432304-1-adhemerval.zanella@linaro.org> References: <20220218142322.432304-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, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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 Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" No semantic changes. --- misc/syslog.c | 441 +++++++++++++++++++++++++------------------------- 1 file changed, 221 insertions(+), 220 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index ee83b1bb76..07a6938bff 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -60,20 +60,20 @@ static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94"; #define ftell(s) _IO_ftell (s) -static int LogType = SOCK_DGRAM; /* type of socket connection */ -static int LogFile = -1; /* fd for log */ -static bool connected; /* have done connect */ -static int LogStat; /* status bits, set by openlog() */ +static int LogType = SOCK_DGRAM; /* type of socket connection */ +static int LogFile = -1; /* fd for log */ +static bool connected; /* have done connect */ +static int LogStat; /* status bits, set by openlog() */ static const char *LogTag; /* string to tag the entry with */ -static int LogFacility = LOG_USER; /* default facility code */ -static int LogMask = 0xff; /* mask of priorities to be logged */ -extern char *__progname; /* Program name, from crt0. */ +static int LogFacility = LOG_USER; /* default facility code */ +static int LogMask = 0xff; /* mask of priorities to be logged */ +extern char *__progname; /* Program name, from crt0. */ /* Define the lock. */ __libc_lock_define_initialized (static, syslog_lock) -static void openlog_internal(const char *, int, int); -static void closelog_internal(void); +static void openlog_internal (const char *, int, int); +static void closelog_internal (void); struct cleanup_arg { @@ -101,205 +101,204 @@ cancel_handler (void *ptr) * print message on log file; output is intended for syslogd(8). */ void -__syslog(int pri, const char *fmt, ...) +__syslog (int pri, const char *fmt, ...) { - va_list ap; + va_list ap; - va_start(ap, fmt); - __vsyslog_internal(pri, fmt, ap, 0); - va_end(ap); + va_start (ap, fmt); + __vsyslog_internal (pri, fmt, ap, 0); + va_end (ap); } ldbl_hidden_def (__syslog, syslog) ldbl_strong_alias (__syslog, syslog) void -__vsyslog(int pri, const char *fmt, va_list ap) +__vsyslog (int pri, const char *fmt, va_list ap) { - __vsyslog_internal(pri, fmt, ap, 0); + __vsyslog_internal (pri, fmt, ap, 0); } ldbl_weak_alias (__vsyslog, vsyslog) void -__syslog_chk(int pri, int flag, const char *fmt, ...) +__syslog_chk (int pri, int flag, const char *fmt, ...) { - va_list ap; + va_list ap; - va_start(ap, fmt); - __vsyslog_internal(pri, fmt, ap, (flag > 0) ? PRINTF_FORTIFY : 0); - va_end(ap); + va_start (ap, fmt); + __vsyslog_internal (pri, fmt, ap, (flag > 0) ? PRINTF_FORTIFY : 0); + va_end (ap); } void __vsyslog_chk(int pri, int flag, const char *fmt, va_list ap) { - __vsyslog_internal(pri, fmt, ap, (flag > 0) ? PRINTF_FORTIFY : 0); + __vsyslog_internal (pri, fmt, ap, (flag > 0) ? PRINTF_FORTIFY : 0); } void __vsyslog_internal(int pri, const char *fmt, va_list ap, unsigned int mode_flags) { - struct tm now_tm; - time_t now; - int fd; - FILE *f; - char *buf = 0; - size_t bufsize = 0; - size_t msgoff; - int saved_errno = errno; - char failbuf[3 * sizeof (pid_t) + sizeof "out of memory []"]; - -#define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID - /* Check for invalid bits. */ - if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { - syslog(INTERNALLOG, - "syslog: unknown facility/priority: %x", pri); - pri &= LOG_PRIMASK|LOG_FACMASK; + struct tm now_tm; + time_t now; + int fd; + FILE *f; + char *buf = 0; + size_t bufsize = 0; + size_t msgoff; + int saved_errno = errno; + char failbuf[3 * sizeof (pid_t) + sizeof "out of memory []"]; + +#define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID + /* Check for invalid bits. */ + if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) + { + syslog (INTERNALLOG, "syslog: unknown facility/priority: %x", pri); + pri &= LOG_PRIMASK|LOG_FACMASK; + } + + /* Prepare for multiple users. We have to take care: most syscalls we are + using are cancellation points. */ + struct cleanup_arg clarg; + clarg.buf = NULL; + clarg.oldaction = NULL; + __libc_cleanup_push (cancel_handler, &clarg); + __libc_lock_lock (syslog_lock); + + /* Check priority against setlogmask values. */ + if ((LOG_MASK (LOG_PRI (pri)) & LogMask) == 0) + goto out; + + /* Set default facility if none specified. */ + if ((pri & LOG_FACMASK) == 0) + pri |= LogFacility; + + /* Build the message in a memory-buffer stream. */ + f = __open_memstream (&buf, &bufsize); + if (f == NULL) + { + /* We cannot get a stream. There is not much we can do but emitting an + error messages. */ + char numbuf[3 * sizeof (pid_t)]; + char *nump; + char *endp = __stpcpy (failbuf, "out of memory ["); + pid_t pid = __getpid (); + + nump = numbuf + sizeof (numbuf); + /* The PID can never be zero. */ + do + *--nump = '0' + pid % 10; + while ((pid /= 10) != 0); + + endp = __mempcpy (endp, nump, (numbuf + sizeof (numbuf)) - nump); + *endp++ = ']'; + *endp = '\0'; + buf = failbuf; + bufsize = endp - failbuf; + msgoff = 0; + } + else + { + __fsetlocking (f, FSETLOCKING_BYCALLER); + fprintf (f, "<%d>", pri); + now = time_now (); + f->_IO_write_ptr += __strftime_l (f->_IO_write_ptr, + f->_IO_write_end - f->_IO_write_ptr, + "%h %e %T ", + __localtime_r (&now, &now_tm), + _nl_C_locobj_ptr); + msgoff = ftell (f); + if (LogTag == NULL) + LogTag = __progname; + if (LogTag != NULL) + __fputs_unlocked (LogTag, f); + if (LogStat & LOG_PID) + fprintf (f, "[%d]", (int) __getpid ()); + if (LogTag != NULL) + { + __putc_unlocked (':', f); + __putc_unlocked (' ', f); + } + + /* Restore errno for %m format. */ + __set_errno (saved_errno); + + /* We have the header. Print the user's format into the buffer. */ + __vfprintf_internal (f, fmt, ap, mode_flags); + + /* Close the memory stream; this will finalize the data into a malloc'd + buffer in BUF. */ + fclose (f); + + /* Tell the cancellation handler to free this buffer. */ + clarg.buf = buf; + } + + /* Output to stderr if requested. */ + if (LogStat & LOG_PERROR) + { + struct iovec iov[2]; + struct iovec *v = iov; + + v->iov_base = buf + msgoff; + v->iov_len = bufsize - msgoff; + /* Append a newline if necessary. */ + if (buf[bufsize - 1] != '\n') + { + ++v; + v->iov_base = (char *) "\n"; + v->iov_len = 1; } - /* Prepare for multiple users. We have to take care: most - syscalls we are using are cancellation points. */ - struct cleanup_arg clarg; - clarg.buf = NULL; - clarg.oldaction = NULL; - __libc_cleanup_push (cancel_handler, &clarg); - __libc_lock_lock (syslog_lock); - - /* Check priority against setlogmask values. */ - if ((LOG_MASK (LOG_PRI (pri)) & LogMask) == 0) - goto out; - - /* Set default facility if none specified. */ - if ((pri & LOG_FACMASK) == 0) - pri |= LogFacility; - - /* Build the message in a memory-buffer stream. */ - f = __open_memstream (&buf, &bufsize); - if (f == NULL) - { - /* We cannot get a stream. There is not much we can do but - emitting an error messages. */ - char numbuf[3 * sizeof (pid_t)]; - char *nump; - char *endp = __stpcpy (failbuf, "out of memory ["); - pid_t pid = __getpid (); - - nump = numbuf + sizeof (numbuf); - /* The PID can never be zero. */ - do - *--nump = '0' + pid % 10; - while ((pid /= 10) != 0); - - endp = __mempcpy (endp, nump, (numbuf + sizeof (numbuf)) - nump); - *endp++ = ']'; - *endp = '\0'; - buf = failbuf; - bufsize = endp - failbuf; - msgoff = 0; - } - else - { - __fsetlocking (f, FSETLOCKING_BYCALLER); - fprintf (f, "<%d>", pri); - now = time_now (); - f->_IO_write_ptr += __strftime_l (f->_IO_write_ptr, - f->_IO_write_end - - f->_IO_write_ptr, - "%h %e %T ", - __localtime_r (&now, &now_tm), - _nl_C_locobj_ptr); - msgoff = ftell (f); - if (LogTag == NULL) - LogTag = __progname; - if (LogTag != NULL) - __fputs_unlocked (LogTag, f); - if (LogStat & LOG_PID) - fprintf (f, "[%d]", (int) __getpid ()); - if (LogTag != NULL) - { - __putc_unlocked (':', f); - __putc_unlocked (' ', f); - } - - /* Restore errno for %m format. */ - __set_errno (saved_errno); - - /* We have the header. Print the user's format into the - buffer. */ - __vfprintf_internal (f, fmt, ap, mode_flags); - - /* Close the memory stream; this will finalize the data - into a malloc'd buffer in BUF. */ - fclose (f); - - /* Tell the cancellation handler to free this buffer. */ - clarg.buf = buf; - } - - /* Output to stderr if requested. */ - if (LogStat & LOG_PERROR) { - struct iovec iov[2]; - struct iovec *v = iov; - - v->iov_base = buf + msgoff; - v->iov_len = bufsize - msgoff; - /* Append a newline if necessary. */ - if (buf[bufsize - 1] != '\n') - { - ++v; - v->iov_base = (char *) "\n"; - v->iov_len = 1; - } - - /* writev is a cancellation point. */ - (void)__writev(STDERR_FILENO, iov, v - iov + 1); + /* writev is a cancellation point. */ + __writev (STDERR_FILENO, iov, v - iov + 1); + } + + /* Get connected, output the message to the local logger. */ + if (!connected) + openlog_internal (NULL, LogStat | LOG_NDELAY, LogFacility); + + /* If we have a SOCK_STREAM connection, also send ASCII NUL as a record + terminator. */ + if (LogType == SOCK_STREAM) + ++bufsize; + + if (!connected || __send(LogFile, buf, bufsize, MSG_NOSIGNAL) < 0) + { + if (connected) + { + /* Try to reopen the syslog connection. Maybe it went down. */ + closelog_internal (); + openlog_internal (NULL, LogStat | LOG_NDELAY, LogFacility); } - /* Get connected, output the message to the local logger. */ - if (!connected) - openlog_internal(NULL, LogStat | LOG_NDELAY, LogFacility); - - /* If we have a SOCK_STREAM connection, also send ASCII NUL as - a record terminator. */ - if (LogType == SOCK_STREAM) - ++bufsize; - - if (!connected || __send(LogFile, buf, bufsize, MSG_NOSIGNAL) < 0) - { - if (connected) - { - /* Try to reopen the syslog connection. Maybe it went - down. */ - closelog_internal (); - openlog_internal(NULL, LogStat | LOG_NDELAY, LogFacility); - } - - if (!connected || __send(LogFile, buf, bufsize, MSG_NOSIGNAL) < 0) - { - closelog_internal (); /* attempt re-open next time */ - /* - * Output the message to the console; don't worry - * about blocking, if console blocks everything will. - * Make sure the error reported is the one from the - * syslogd failure. - */ - if (LogStat & LOG_CONS && - (fd = __open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_CLOEXEC, - 0)) >= 0) - { - __dprintf (fd, "%s\r\n", buf + msgoff); - (void)__close(fd); - } - } - } + if (!connected || __send(LogFile, buf, bufsize, MSG_NOSIGNAL) < 0) + { + closelog_internal (); /* attempt re-open next time */ + /* + * Output the message to the console; don't worry + * about blocking, if console blocks everything will. + * Make sure the error reported is the one from the + * syslogd failure. + */ + if (LogStat & LOG_CONS && + (fd = __open (_PATH_CONSOLE, O_WRONLY | O_NOCTTY + | O_CLOEXEC, 0)) + >= 0) + { + __dprintf (fd, "%s\r\n", buf + msgoff); + __close(fd); + } + } + } out: - /* End of critical section. */ - __libc_cleanup_pop (0); - __libc_lock_unlock (syslog_lock); + /* End of critical section. */ + __libc_cleanup_pop (0); + __libc_lock_unlock (syslog_lock); - if (buf != failbuf) - free (buf); + if (buf != failbuf) + free (buf); } /* AF_UNIX address of local logger */ @@ -312,45 +311,47 @@ static const struct sockaddr_un SyslogAddr = static void openlog_internal(const char *ident, int logstat, int logfac) { - if (ident != NULL) - LogTag = ident; - LogStat = logstat; - if ((logfac &~ LOG_FACMASK) == 0) - LogFacility = logfac; - - int retry = 0; - while (retry < 2) { - if (LogFile == -1) { - if (LogStat & LOG_NDELAY) { - LogFile = __socket(AF_UNIX, LogType | SOCK_CLOEXEC, 0); - if (LogFile == -1) - return; - } - } - if (LogFile != -1 && !connected) + if (ident != NULL) + LogTag = ident; + LogStat = logstat; + if ((logfac &~ LOG_FACMASK) == 0) + LogFacility = logfac; + + int retry = 0; + while (retry < 2) + { + if (LogFile == -1) + { + if (LogStat & LOG_NDELAY) + { + LogFile = __socket (AF_UNIX, LogType | SOCK_CLOEXEC, 0); + if (LogFile == -1) + return; + } + } + if (LogFile != -1 && !connected) + { + int old_errno = errno; + if (__connect (LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) + { + int saved_errno = errno; + int fd = LogFile; + LogFile = -1; + __close(fd); + __set_errno (old_errno); + if (saved_errno == EPROTOTYPE) { - int old_errno = errno; - if (__connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) - == -1) - { - int saved_errno = errno; - int fd = LogFile; - LogFile = -1; - (void)__close(fd); - __set_errno (old_errno); - if (saved_errno == EPROTOTYPE) - { - /* retry with the other type: */ - LogType = (LogType == SOCK_DGRAM - ? SOCK_STREAM : SOCK_DGRAM); - ++retry; - continue; - } - } else - connected = true; + /* retry with the other type: */ + LogType = LogType == SOCK_DGRAM ? SOCK_STREAM : SOCK_DGRAM; + ++retry; + continue; } - break; + } + else + connected = true; } + break; + } } void @@ -395,16 +396,16 @@ closelog (void) int setlogmask (int pmask) { - int omask; + int omask; - /* Protect against multiple users. */ - __libc_lock_lock (syslog_lock); + /* Protect against multiple users. */ + __libc_lock_lock (syslog_lock); - omask = LogMask; - if (pmask != 0) - LogMask = pmask; + omask = LogMask; + if (pmask != 0) + LogMask = pmask; - __libc_lock_unlock (syslog_lock); + __libc_lock_unlock (syslog_lock); - return (omask); + return (omask); } From patchwork Fri Feb 18 14:23:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 51210 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 E97B0385DC39 for ; Fri, 18 Feb 2022 14:26:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E97B0385DC39 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1645194402; bh=HCwfMJFT9uA+l1mnmEZcWvENXxdh7el3nzO5ApvV8Rg=; 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=ihHaI/aD4B5Oou0tGcaR5A3ICm4Ls/1bk61pfcErem4DJWZtYj68RwagXXWoLxTXg 9az8DwqmqrSRuHHkSCPgwlWImcEWEgLODNvjhGJgnfpHZNNfn49B3zrqm2C/DCHiUC zSWRr9soDwvw0KTth0jHk3+AMEBYRT3yUUUHjoWM= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x229.google.com (mail-oi1-x229.google.com [IPv6:2607:f8b0:4864:20::229]) by sourceware.org (Postfix) with ESMTPS id C0D13385E440 for ; Fri, 18 Feb 2022 14:23:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C0D13385E440 Received: by mail-oi1-x229.google.com with SMTP id q5so3225598oij.6 for ; Fri, 18 Feb 2022 06:23:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HCwfMJFT9uA+l1mnmEZcWvENXxdh7el3nzO5ApvV8Rg=; b=v64OhelhP76T5sOWs2aND13/C4/qwQhyDYccH9leLx7+zUrjrlyCSVy7J2X+kVKAS3 DX7Goyr98MI6EvM8+Cduw9xwuyo3lq9Aq4+qktuPkYf65HpEklXgeupNzxh5CUr3F0qT XIVgo5sYzf7mZ8vl5Qj42kyVO5+Jie7+zUV8WuJh5D6Rn8xf0lCE+BRHABZbiiRu9UqL lH4upRmJfCsgsrsEFW3oHluSGAvv35DA1aor8rfCa1DsbThvAnsfjv8vKE6eE4j1YmJn pSXPlUlOerWXL8SQ4ZFZvQ6odZp0CCzVNNDMTEWAXF3/TKx94uR9oFHrrHUrHN0YkXHH 0pEA== X-Gm-Message-State: AOAM531c57Lc4+87gTmijcp4pDaKK8vMJRZOZ3/7qm8hUIdXMwBaRyrq VYjOIZ9SSLIIURf0UxWAksD0hJujzTyPhw== X-Google-Smtp-Source: ABdhPJzOQsQDQdJl69j8BqcN6/bDAgNzNFdQNJt3CmuKznKwDatQ0sTCJ9ZxgyFvvazSE3v/ahs9Lg== X-Received: by 2002:a05:6808:1828:b0:2ce:6a75:b810 with SMTP id bh40-20020a056808182800b002ce6a75b810mr3358353oib.215.1645194212747; Fri, 18 Feb 2022 06:23:32 -0800 (PST) Received: from birita.. ([2804:431:c7cb:6747:90e1:2029:f1e0:f47a]) by smtp.gmail.com with ESMTPSA id bp5sm702993oib.25.2022.02.18.06.23.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Feb 2022 06:23:32 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v2 4/7] misc: syslog: Simplify implementation Date: Fri, 18 Feb 2022 11:23:19 -0300 Message-Id: <20220218142322.432304-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220218142322.432304-1-adhemerval.zanella@linaro.org> References: <20220218142322.432304-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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 Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Remove unrequired includes, use a temporary buffer for strftime instead of using internal libio members, simplify fprintf call on the memstream and memory allocation, use dprintf instead of writev for LOG_PERROR. Checked on x86_64-linux-gnu. --- misc/syslog.c | 126 ++++++++++++++------------------------------------ 1 file changed, 35 insertions(+), 91 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index 07a6938bff..e46cf1103b 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -31,34 +31,16 @@ static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94"; #endif /* LIBC_SCCS and not lint */ -#include +#include +#include +#include +#include +#include #include -#include +#include #include #include -#include - -#include -#include -#include -#include #include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include -#include - -#include - -#define ftell(s) _IO_ftell (s) static int LogType = SOCK_DGRAM; /* type of socket connection */ static int LogFile = -1; /* fd for log */ @@ -139,13 +121,10 @@ void __vsyslog_internal(int pri, const char *fmt, va_list ap, unsigned int mode_flags) { - struct tm now_tm; - time_t now; - int fd; FILE *f; char *buf = 0; size_t bufsize = 0; - size_t msgoff; + int msgoff; int saved_errno = errno; char failbuf[3 * sizeof (pid_t) + sizeof "out of memory []"]; @@ -159,9 +138,7 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, /* Prepare for multiple users. We have to take care: most syscalls we are using are cancellation points. */ - struct cleanup_arg clarg; - clarg.buf = NULL; - clarg.oldaction = NULL; + struct cleanup_arg clarg = { NULL, NULL }; __libc_cleanup_push (cancel_handler, &clarg); __libc_lock_lock (syslog_lock); @@ -175,51 +152,24 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, /* Build the message in a memory-buffer stream. */ f = __open_memstream (&buf, &bufsize); - if (f == NULL) - { - /* We cannot get a stream. There is not much we can do but emitting an - error messages. */ - char numbuf[3 * sizeof (pid_t)]; - char *nump; - char *endp = __stpcpy (failbuf, "out of memory ["); - pid_t pid = __getpid (); - - nump = numbuf + sizeof (numbuf); - /* The PID can never be zero. */ - do - *--nump = '0' + pid % 10; - while ((pid /= 10) != 0); - - endp = __mempcpy (endp, nump, (numbuf + sizeof (numbuf)) - nump); - *endp++ = ']'; - *endp = '\0'; - buf = failbuf; - bufsize = endp - failbuf; - msgoff = 0; - } - else + if (f != NULL) { __fsetlocking (f, FSETLOCKING_BYCALLER); - fprintf (f, "<%d>", pri); - now = time_now (); - f->_IO_write_ptr += __strftime_l (f->_IO_write_ptr, - f->_IO_write_end - f->_IO_write_ptr, - "%h %e %T ", - __localtime_r (&now, &now_tm), - _nl_C_locobj_ptr); - msgoff = ftell (f); - if (LogTag == NULL) - LogTag = __progname; - if (LogTag != NULL) - __fputs_unlocked (LogTag, f); - if (LogStat & LOG_PID) - fprintf (f, "[%d]", (int) __getpid ()); - if (LogTag != NULL) - { - __putc_unlocked (':', f); - __putc_unlocked (' ', f); - } - + /* "%h %e %H:%M:%S " */ + char timebuf[3+1 /* "%h " */ + + 2+1 /* "%e " */ + + 2+1 + 2+1 + 2+1 + 1 /* "%T " */]; + time_t now = time_now (); + struct tm now_tm; + __localtime_r (&now, &now_tm); + __strftime_l (timebuf, sizeof (timebuf), "%h %e %T ", &now_tm, + _nl_C_locobj_ptr); + + pid_t pid = LogStat & LOG_PID ? __getpid () : 0; + + fprintf (f, "<%d>%s %n%s%s%.0d%s: ", pri, timebuf, &msgoff, + LogTag == NULL ? __progname : LogTag, + pid != 0 ? "[" : "", pid, pid != 0 ? "]" : ""); /* Restore errno for %m format. */ __set_errno (saved_errno); @@ -233,26 +183,19 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, /* Tell the cancellation handler to free this buffer. */ clarg.buf = buf; } + else + { + /* We cannot get a stream. There is not much we can do but emitting an + error messages. */ + bufsize = __snprintf (failbuf, sizeof failbuf, "out of memory[%d]", + __getpid ()); + buf = failbuf; + } /* Output to stderr if requested. */ if (LogStat & LOG_PERROR) - { - struct iovec iov[2]; - struct iovec *v = iov; - - v->iov_base = buf + msgoff; - v->iov_len = bufsize - msgoff; - /* Append a newline if necessary. */ - if (buf[bufsize - 1] != '\n') - { - ++v; - v->iov_base = (char *) "\n"; - v->iov_len = 1; - } - - /* writev is a cancellation point. */ - __writev (STDERR_FILENO, iov, v - iov + 1); - } + __dprintf (STDERR_FILENO, "%s%s", buf + msgoff, + buf[bufsize - 1] != '\n' ? "\n" : ""); /* Get connected, output the message to the local logger. */ if (!connected) @@ -281,6 +224,7 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, * Make sure the error reported is the one from the * syslogd failure. */ + int fd; if (LogStat & LOG_CONS && (fd = __open (_PATH_CONSOLE, O_WRONLY | O_NOCTTY | O_CLOEXEC, 0)) From patchwork Fri Feb 18 14:23:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 51211 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 F1EA6385E449 for ; Fri, 18 Feb 2022 14:27:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F1EA6385E449 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1645194445; bh=t0Jld9dy2adjbv+rougsrzyuEvQmzk1+fyLcHSKUo50=; 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=NpJJ1St+yYHFx7NsF9bNfEKsi4mBh3zqzYv02hoM3HKuETmNiTrPzoSahXoaHIs0N FYoRjGszlHXxsiBbMjxpXLt+VPJpFB57hqwuUxv0UW2Tvtp3YGqRpli6fRqLdmV+gb 1xGW8He+NjTZpLZEsPum9XtECMzwuqHiPT2U7MIo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc32.google.com (mail-oo1-xc32.google.com [IPv6:2607:f8b0:4864:20::c32]) by sourceware.org (Postfix) with ESMTPS id 2DADF385BF93 for ; Fri, 18 Feb 2022 14:23:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2DADF385BF93 Received: by mail-oo1-xc32.google.com with SMTP id w10-20020a4ae08a000000b0031bdf7a6d76so3638188oos.10 for ; Fri, 18 Feb 2022 06:23:35 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=t0Jld9dy2adjbv+rougsrzyuEvQmzk1+fyLcHSKUo50=; b=znnZA28wiqXim1mKy6rn3Haj1sCeUokJYyd1KZd2oZdr/gA2k+m0uGiU0Yraf75of3 EYimmA1eGbBnowmoTrUYjd7sDZg5X1EOKApeQUHrsee2L7MIBg60rcnuizpfRXvxoLsB NLoMwGL3iV9PdghmJcjWzqriamNfZfhm7AjPhdpfILJaoFsnKVKjEgTtF+uXqW9JHkjJ /Jaa+QhNuElgfBK7iOeQYmO6Z8kUVGkL1L5xIY5BFNEQZdEFK45uUa3Y3UJGLbjfeL05 o3bjyedFovOrrbdnz35uQqRzvT42Y23JzVsJzDunx+Hm52ilMX1HElupTe2i6B9iBChS mTeA== X-Gm-Message-State: AOAM533moRuSaiUQVvuOgm1rcWdt7nOAsHjoBH5tvWgL81j/IPS2PraD /40X2u4NoTbsBB4oZZwoyhWVkC9hXOPy6w== X-Google-Smtp-Source: ABdhPJwrS5Wd4O+e1ev66JMipxMGRghUA9CCSbTo4e8A823QVzXhAP4uH9VyuXu4moe2Sr5XAflevg== X-Received: by 2002:a05:6871:5d0:b0:c4:7dc0:d727 with SMTP id v16-20020a05687105d000b000c47dc0d727mr2887528oan.250.1645194214201; Fri, 18 Feb 2022 06:23:34 -0800 (PST) Received: from birita.. ([2804:431:c7cb:6747:90e1:2029:f1e0:f47a]) by smtp.gmail.com with ESMTPSA id bp5sm702993oib.25.2022.02.18.06.23.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Feb 2022 06:23:33 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v2 5/7] misc: syslog: Use static buffer Date: Fri, 18 Feb 2022 11:23:20 -0300 Message-Id: <20220218142322.432304-6-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220218142322.432304-1-adhemerval.zanella@linaro.org> References: <20220218142322.432304-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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 Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" The static buffer is used instead of memstream for messages up to 1024 bytes to avoid the potential BUFSIZ (8K) malloc and free for each syslog call. The memstream is still used as fallback for larger messages. Checked on x86_64-linux-gnu. --- misc/syslog.c | 105 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 68 insertions(+), 37 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index e46cf1103b..088e3b5664 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -121,12 +121,11 @@ void __vsyslog_internal(int pri, const char *fmt, va_list ap, unsigned int mode_flags) { - FILE *f; - char *buf = 0; + char *buf = NULL; size_t bufsize = 0; + bool buf_malloced = false; int msgoff; int saved_errno = errno; - char failbuf[3 * sizeof (pid_t) + sizeof "out of memory []"]; #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID /* Check for invalid bits. */ @@ -150,46 +149,78 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, if ((pri & LOG_FACMASK) == 0) pri |= LogFacility; - /* Build the message in a memory-buffer stream. */ - f = __open_memstream (&buf, &bufsize); - if (f != NULL) + pid_t pid = LogStat & LOG_PID ? __getpid () : 0; + + enum + { + timebuf_size = 3+1 /* "%h " */ + + 2+1 /* "%e " */ + + 2+1 + 2+1 + 2+1 + 1, /* "%T " */ + + bufs_size = 1024 + }; + + /* "%h %e %H:%M:%S " */ + char timestamp[timebuf_size]; + time_t now = time_now (); + struct tm now_tm; + __localtime_r (&now, &now_tm); + __strftime_l (timestamp, sizeof (timestamp), "%h %e %T ", &now_tm, + _nl_C_locobj_ptr); + +#define SYSLOG_HEADER(__pri, __timestamp, __msgoff, pid) \ + "<%d>%s %n%s%s%.0d%s: ", \ + __pri, __timestamp, __msgoff, \ + LogTag == NULL ? __progname : LogTag, \ + pid != 0 ? "[" : "", pid, pid != 0 ? "]" : "" + + /* Try to use a static buffer as an optimization. */ + char bufs[bufs_size]; + int l = __snprintf (bufs, sizeof bufs, + SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + if (l < sizeof (bufs)) { - __fsetlocking (f, FSETLOCKING_BYCALLER); - /* "%h %e %H:%M:%S " */ - char timebuf[3+1 /* "%h " */ - + 2+1 /* "%e " */ - + 2+1 + 2+1 + 2+1 + 1 /* "%T " */]; - time_t now = time_now (); - struct tm now_tm; - __localtime_r (&now, &now_tm); - __strftime_l (timebuf, sizeof (timebuf), "%h %e %T ", &now_tm, - _nl_C_locobj_ptr); - - pid_t pid = LogStat & LOG_PID ? __getpid () : 0; - - fprintf (f, "<%d>%s %n%s%s%.0d%s: ", pri, timebuf, &msgoff, - LogTag == NULL ? __progname : LogTag, - pid != 0 ? "[" : "", pid, pid != 0 ? "]" : ""); + va_list apc; + va_copy (apc, ap); + /* Restore errno for %m format. */ __set_errno (saved_errno); - /* We have the header. Print the user's format into the buffer. */ - __vfprintf_internal (f, fmt, ap, mode_flags); + int vl = __vsnprintf_internal (bufs + l, sizeof (bufs) - l, fmt, apc, + mode_flags); + if (l + vl < sizeof (bufs)) + { + buf = bufs; + bufsize = l + vl; + } - /* Close the memory stream; this will finalize the data into a malloc'd - buffer in BUF. */ - fclose (f); - - /* Tell the cancellation handler to free this buffer. */ - clarg.buf = buf; + va_end (apc); } - else + + /* If the required size is larger than buffer size fallbacks to + open_memstream. */ + if (buf == NULL) { - /* We cannot get a stream. There is not much we can do but emitting an - error messages. */ - bufsize = __snprintf (failbuf, sizeof failbuf, "out of memory[%d]", - __getpid ()); - buf = failbuf; + FILE *f = __open_memstream (&buf, &bufsize); + if (f != NULL) + { + __fsetlocking (f, FSETLOCKING_BYCALLER); + fprintf (f, SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + /* Restore errno for %m format. */ + __set_errno (saved_errno); + __vfprintf_internal (f, fmt, ap, mode_flags); + fclose (f); + + /* Tell the cancellation handler to free this buffer. */ + buf_malloced = true; + clarg.buf = buf; + } + else + { + bufsize = __snprintf (bufs, sizeof bufs, + "out of memory[%d]", __getpid ()); + buf = bufs; + } } /* Output to stderr if requested. */ @@ -241,7 +272,7 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, __libc_cleanup_pop (0); __libc_lock_unlock (syslog_lock); - if (buf != failbuf) + if (buf_malloced) free (buf); } From patchwork Fri Feb 18 14:23:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 51212 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 68BBF385EC58 for ; Fri, 18 Feb 2022 14:28:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 68BBF385EC58 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1645194491; bh=Qzptpn06tltdiUUMWV9PasUxOg0MRhG2VvKWywUHIJU=; 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=c5voI9E4d3NnAKBZREB+xRFHl5KY6B8kdeOR/DFzfEqWXXEBRo6iqrX6qN3MZUyRm W3wLtV7pc8eW7aV306geajF9vruMI2/BMFKl4uzry6xpxI07CkDsUYkiRVyyW7gGeX eywbn19bDBORzRtAicT6i9KfeKb54cTFv/78XPII= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x236.google.com (mail-oi1-x236.google.com [IPv6:2607:f8b0:4864:20::236]) by sourceware.org (Postfix) with ESMTPS id 7D1F9385E440 for ; Fri, 18 Feb 2022 14:23:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7D1F9385E440 Received: by mail-oi1-x236.google.com with SMTP id z7so220100oid.4 for ; Fri, 18 Feb 2022 06:23:36 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Qzptpn06tltdiUUMWV9PasUxOg0MRhG2VvKWywUHIJU=; b=ImGOOoalrOh+i4xLfhGw1Owie9qRC2Lnkiv4JmMCMP3FWaoQ40IOO8EjRuii6N1GF/ ka3HjWQpCszt+FDwn0mGh0DL/h6AiDx0yXa9IMnJU6Pgau/dBctWcO3LII5BzMOOLWao pR09RBpRXf5C4vWTtGTV2PU9KAKEVb34iPYxHUgPMIzM7/eERo8QSqm4H5lwIyJvg39j FMGcpPYiR99ool9ZXnW2HHTSdLBJhIUQWN8SZ/vdsudd5WeskWzp599Is4jd9mQgpW6N Jr8cNKJ+f6YS/4sTVlgvRoqNDJE9q2lAfZjWIVkYiNFHvVMm+qZGh1GzdUIIMO4yJI4C rtpw== X-Gm-Message-State: AOAM532toENUUkOaRxckJVsb/TXziZg5YjlC1QAHz9EYFtNWAIW84bST kMVbJ41OmoQ7mTyf3UlRE10FYnoAibiTJA== X-Google-Smtp-Source: ABdhPJwPNj2JIDcUDqaEYgGxQlHzKmSwr8HXBak4WmXPsRhF0AE/+iYDKYdfBYDAXF34SpwoJV6DSg== X-Received: by 2002:a05:6808:bc6:b0:2d3:6cab:9106 with SMTP id o6-20020a0568080bc600b002d36cab9106mr3446101oik.78.1645194215578; Fri, 18 Feb 2022 06:23:35 -0800 (PST) Received: from birita.. ([2804:431:c7cb:6747:90e1:2029:f1e0:f47a]) by smtp.gmail.com with ESMTPSA id bp5sm702993oib.25.2022.02.18.06.23.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Feb 2022 06:23:35 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v2 6/7] misc: syslog: Move SYSLOG_NAME to USE_MISC (BZ #16355) Date: Fri, 18 Feb 2022 11:23:21 -0300 Message-Id: <20220218142322.432304-7-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220218142322.432304-1-adhemerval.zanella@linaro.org> References: <20220218142322.432304-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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 Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" There is no easy solution as described on first comment in bug report, and some code (like busybox) assumes facilitynames existance when SYSLOG_NAMES is defined (so we can't just remove it as suggested in comment #2). So use the easier solution and guard it with __USE_MISC. --- misc/sys/syslog.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/misc/sys/syslog.h b/misc/sys/syslog.h index dc3b0e7ef8..bf368d1b8d 100644 --- a/misc/sys/syslog.h +++ b/misc/sys/syslog.h @@ -62,7 +62,7 @@ #define LOG_PRI(p) ((p) & LOG_PRIMASK) #define LOG_MAKEPRI(fac, pri) ((fac) | (pri)) -#ifdef SYSLOG_NAMES +#if defined(SYSLOG_NAMES) && defined(__USE_MISC) #define INTERNAL_NOPRI 0x10 /* the "no priority" priority */ /* mark "facility" */ #define INTERNAL_MARK LOG_MAKEPRI(LOG_NFACILITIES << 3, 0) @@ -118,7 +118,7 @@ CODE prioritynames[] = /* facility of pri */ #define LOG_FAC(p) (((p) & LOG_FACMASK) >> 3) -#ifdef SYSLOG_NAMES +#if defined(SYSLOG_NAMES) && defined(__USE_MISC) CODE facilitynames[] = { { "auth", LOG_AUTH }, From patchwork Fri Feb 18 14:23:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 51213 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 7CD41385E445 for ; Fri, 18 Feb 2022 14:28:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7CD41385E445 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1645194533; bh=EH5NyPiN4m9akW3nj+26qdQazOTL/9t+XhoQcZ+9j+Y=; 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=kXRChPRlPZJUq1JMDsBP5lBZDOCNMf3ylJcsgDoZOGiCY7+p2l05ASiNN2d+LXJfO 9bSd7s0ikNWUknCe48sL+qd1lEXEByWrdAm5bUmTX7yTVt+AI9fbjjC93Jj+gO8k8W ZmUwvhU0U4kmXjVrq0ri2Iex3pjqhwnLvBYm324g= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc2d.google.com (mail-oo1-xc2d.google.com [IPv6:2607:f8b0:4864:20::c2d]) by sourceware.org (Postfix) with ESMTPS id 17AC9385E020 for ; Fri, 18 Feb 2022 14:23:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 17AC9385E020 Received: by mail-oo1-xc2d.google.com with SMTP id 189-20020a4a03c6000000b003179d7b30d8so3682861ooi.2 for ; Fri, 18 Feb 2022 06:23:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EH5NyPiN4m9akW3nj+26qdQazOTL/9t+XhoQcZ+9j+Y=; b=4CRkt4Igi8h1hiGYMbsn91J5Rg4F8cVKpzZsUGTAJpRIl9csCXOw+gQgkXAKFKTd2s aDfJ2HZhGC1Nfz9fzp5mu3pEV0JmX0n9yI3zvirpOnQvH+N3i54iIGvGrydcUuoP4/N5 OTU81jDflUIRTGP3odJ/P7Ub7hueLmxz8KlmbTGo2+0U3HCO0xQhVkNk7ch0d+GUEIie QvY/0FsNPfFzInBXWq6ZKM1upVobiCixjq/PZBwDE0Um4O13etNxZpcUUj4LZzxA8izz 61uGBgtAizXGGwDHI8okAsVmU2+8t5e1Aq5T5QiiFdqeiZcuJro2i46BAHvhrkHpDy5+ ruyA== X-Gm-Message-State: AOAM533YsqLOocQI7BmtgoPSRMBV+pJwf85Gi4o6ojkGQU9ua48BPcJS difxKvlns6zDgN6WR4w/g3wxqZDHY0smrQ== X-Google-Smtp-Source: ABdhPJyyc9SqEY4fOaJ1vR0eqFgW70Fv7kljPApD/rl8eBG4StOwbF6A4zAsUk2PFaPJzwf+WE0nAQ== X-Received: by 2002:a05:6870:d3cc:b0:c4:7dc0:d72f with SMTP id l12-20020a056870d3cc00b000c47dc0d72fmr3129809oag.258.1645194217014; Fri, 18 Feb 2022 06:23:37 -0800 (PST) Received: from birita.. ([2804:431:c7cb:6747:90e1:2029:f1e0:f47a]) by smtp.gmail.com with ESMTPSA id bp5sm702993oib.25.2022.02.18.06.23.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Feb 2022 06:23:36 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v2 7/7] misc: syslog: Use RFC5424 Date: Fri, 18 Feb 2022 11:23:22 -0300 Message-Id: <20220218142322.432304-8-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220218142322.432304-1-adhemerval.zanella@linaro.org> References: <20220218142322.432304-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_NUMSUBJECT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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 Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Current syslog format provides limited timestamp information (not full year information nor microsecond precision) and the timezone information might incur in confusion [1] since there is no metadata information on how it was generated. So instead of using another ad-hoc syslog format, this patch adapts it to follow RFC5424: - VERSION is set as 1. - TIMESTAMP is logged as described by RFC 3339 using UTC ('Z') with maximum allowed TIME-SECFRAC precision (microseconds). - HOSTNAME is obtained with gethostname (as allowed by the RFC). - PROCID is only set if LOG_PID is previously set by openlog. - MSGID and STRUCTURED-DATA are both set to NILVALUE. Checked on x86_64-linux-gnu. [1] https://sourceware.org/pipermail/libc-alpha/2021-March/123583.html --- misc/syslog.c | 79 +++++++++++++++++++++++++++++++++-------------- misc/tst-syslog.c | 28 ++++++++--------- 2 files changed, 69 insertions(+), 38 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index 088e3b5664..f15a536dd7 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -31,6 +31,7 @@ static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94"; #endif /* LIBC_SCCS and not lint */ +#include #include #include #include @@ -42,6 +43,10 @@ static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94"; #include #include +#ifndef HOST_NAME_MAX +# define HOST_NAME_MAX 64 +#endif + static int LogType = SOCK_DGRAM; /* type of socket connection */ static int LogFile = -1; /* fd for log */ static bool connected; /* have done connect */ @@ -117,6 +122,39 @@ __vsyslog_chk(int pri, int flag, const char *fmt, va_list ap) __vsyslog_internal (pri, fmt, ap, (flag > 0) ? PRINTF_FORTIFY : 0); } +/* Defined by RFC5424. */ +#define NILVALUE "-" + +struct timebuf +{ + /* Leave room for outlandish but possible years. + The "+ 1" is for strftime's adding 1900 to tm_year. */ + char b[INT_STRLEN_BOUND (int) + 1 + + sizeof "-MM-DDThh:mm:ss.nnnnnnZ"]; +}; + + +/* Fill TIMEBUF with a RFC3339 timestamp. Use UTC time and maximum + TIME-SECFRAC accurancy allowed (6 digits for microseconds). */ +static void +syslog_rfc3339_timestamp (struct timebuf *timebuf) +{ + struct __timespec64 ts; + __clock_gettime64 (TIME_CLOCK_GETTIME_CLOCKID, &ts); + struct tm now_tm; + struct tm *gmtm = __gmtime64_r (&ts.tv_sec, &now_tm); + if (gmtm == NULL) + strcpy (timebuf->b, NILVALUE); + else + { + size_t datebytes = __strftime_l (timebuf->b, sizeof timebuf->b, + "%FT%T", gmtm, _nl_C_locobj_ptr); + int usec = ts.tv_nsec / 1000; + __snprintf (timebuf->b + datebytes, sizeof timebuf->b - datebytes, + ".%06dZ", usec); + } +} + void __vsyslog_internal(int pri, const char *fmt, va_list ap, unsigned int mode_flags) @@ -149,35 +187,30 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, if ((pri & LOG_FACMASK) == 0) pri |= LogFacility; - pid_t pid = LogStat & LOG_PID ? __getpid () : 0; + struct timebuf timestamp; + syslog_rfc3339_timestamp (×tamp); - enum - { - timebuf_size = 3+1 /* "%h " */ - + 2+1 /* "%e " */ - + 2+1 + 2+1 + 2+1 + 1, /* "%T " */ - - bufs_size = 1024 - }; + char hostname[HOST_NAME_MAX]; + if (__gethostname (hostname, sizeof (hostname)) == -1) + strcpy (hostname, NILVALUE); - /* "%h %e %H:%M:%S " */ - char timestamp[timebuf_size]; - time_t now = time_now (); - struct tm now_tm; - __localtime_r (&now, &now_tm); - __strftime_l (timestamp, sizeof (timestamp), "%h %e %T ", &now_tm, - _nl_C_locobj_ptr); + pid_t pid = LogStat & LOG_PID ? __getpid () : 0; -#define SYSLOG_HEADER(__pri, __timestamp, __msgoff, pid) \ - "<%d>%s %n%s%s%.0d%s: ", \ - __pri, __timestamp, __msgoff, \ - LogTag == NULL ? __progname : LogTag, \ - pid != 0 ? "[" : "", pid, pid != 0 ? "]" : "" +#define SYSLOG_HEADER(__pri, __timestamp, __msgoff, __hostname, __pid) \ + "<%d>1 %s %n%s %s %s%.0d %s %s ", \ + __pri, /* PRI */ \ + __timestamp.b, /* TIMESTAMP */ \ + __msgoff, __hostname, /* HOSTNAME */ \ + LogTag == NULL ? __progname : LogTag, /* APP-NAME */ \ + NILVALUE + !!__pid, __pid, /* PROCID */ \ + NILVALUE, /* MSGID */ \ + NILVALUE /* STRUCT-DATA */ /* Try to use a static buffer as an optimization. */ + enum { bufs_size = 1024 }; char bufs[bufs_size]; int l = __snprintf (bufs, sizeof bufs, - SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + SYSLOG_HEADER (pri, timestamp, &msgoff, hostname, pid)); if (l < sizeof (bufs)) { va_list apc; @@ -205,7 +238,7 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, if (f != NULL) { __fsetlocking (f, FSETLOCKING_BYCALLER); - fprintf (f, SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + fprintf (f, SYSLOG_HEADER (pri, timestamp, &msgoff, hostname, pid)); /* Restore errno for %m format. */ __set_errno (saved_errno); __vfprintf_internal (f, fmt, ap, mode_flags); diff --git a/misc/tst-syslog.c b/misc/tst-syslog.c index 8505178616..7b78b0c619 100644 --- a/misc/tst-syslog.c +++ b/misc/tst-syslog.c @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -201,7 +202,7 @@ check_openlog_message (const struct msg_t *msg, int msgnum, { if (options & LOG_PID) TEST_COMPARE (msg->pid, pid); - TEST_COMPARE_STRING (msg->ident, expected_ident); + TEST_COMPARE_STRING (msg->ident, OPENLOG_IDENT); TEST_COMPARE (msg->facility, LOG_LOCAL0); } else if (msgnum < 2 * array_length (priorities)) @@ -212,28 +213,25 @@ check_openlog_message (const struct msg_t *msg, int msgnum, return true; } +#define NILVALUE "-" + static struct msg_t parse_syslog_msg (const char *msg) { struct msg_t r = { .pid = -1 }; int number; + char procid[INT_BUFSIZE_BOUND (pid_t)]; /* 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); + int n = sscanf (msg, "<%6d>1 %*s %*s %32s %s - - %s", + &number, r.ident, procid, r.msg); + TEST_COMPARE (n, 4); 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); - } + r.pid = strcmp (procid, NILVALUE) == 0 ? 0 : strtoul (procid, NULL, 10); return r; } @@ -246,9 +244,9 @@ parse_syslog_console (const char *msg) 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); + mymachine openlog_ident PID - syslog_message 128 0 */ + int n = sscanf (msg, "%*s %32s %*s - - %s %d %d", + r.ident, r.msg, &facility, &priority); TEST_COMPARE (n, 4); r.facility = facility; @@ -384,7 +382,7 @@ check_syslog_console_read (FILE *fp) while (fgets (buf, sizeof (buf), fp) != NULL) { struct msg_t msg = parse_syslog_console (buf); - TEST_COMPARE_STRING (msg.ident, OPENLOG_IDENT ":"); + TEST_COMPARE_STRING (msg.ident, OPENLOG_IDENT); TEST_COMPARE (msg.priority, priorities[msgnum]); TEST_COMPARE (msg.facility, LOG_LOCAL0);