From patchwork Mon Mar 21 21:19:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 52191 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 DC5A8389942E for ; Mon, 21 Mar 2022 21:20:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DC5A8389942E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1647897625; 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=l314MtgMdiYdyVZi2aX7/DwRfH+wyi/OtnlOTnpQ3EOIoK1IhIG37v9BqvERdzQ0f vDEZMLSycy4z4GiwtHAvvc1neDPsDFoSIyCfITEPqh71r3YU/IQ8qiJnIgoN2BpbdC zW0lpCkegJ67ENZO5TMz7+bKExea+4v+lRSCnxXM= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x335.google.com (mail-ot1-x335.google.com [IPv6:2607:f8b0:4864:20::335]) by sourceware.org (Postfix) with ESMTPS id C57C23857419 for ; Mon, 21 Mar 2022 21:19:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C57C23857419 Received: by mail-ot1-x335.google.com with SMTP id o20-20020a9d7194000000b005cb20cf4f1bso11344356otj.7 for ; Mon, 21 Mar 2022 14:19:20 -0700 (PDT) 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=VCQZrPKABV62ysDah5+QAAuJPynCsh1tajlDylxcRkSqmfKaLhCFcoFpVZvgVXivcf krBG8ZG2WbiKbPQYaT2l898rqQYGUQyeBZYBxadqbRVdyyMqXzkD8AyZdnzjyqPp76IZ PfvVj3+jXqAhOq+2dSKlUVnh2X8qHQVWL8VI5z6wPyLbC26qUgbscriRM2VfBsX4k69j JkWv+C10tdOACZsOWsRdl+KKYDTcsAMCzNc/Mr045OpY8hb249VwpyCwKBWB6x7bTvFV VnvUPwab14lC0XpDCQQqYavjgTt1PJaAPjlyF+hahzmeEUmnGtBjB23sH00TzK7Fb3KX 2aDg== X-Gm-Message-State: AOAM531gorVpvHAi6ydLdNiMRrqppLU8PorgcAkjEA1RMAptnzWhvwus /OGvU1UriZ6Qvc0mWKg/xFLrxPiVNuclGQ== X-Google-Smtp-Source: ABdhPJx2Sv88QhFyhk2llm5CWzgtaYmg5Otb2q4/1VOMAzqIn6gVE3Zbj+cNqYKV2t2Cw5LsNBc3Mg== X-Received: by 2002:a05:6830:1098:b0:5af:a452:2f17 with SMTP id y24-20020a056830109800b005afa4522f17mr8597951oto.175.1647897559740; Mon, 21 Mar 2022 14:19:19 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:2d55:d5f9:2c96:1f30:1f58]) by smtp.gmail.com with ESMTPSA id c37-20020a9d27a8000000b005b23cf22e23sm7925256otb.42.2022.03.21.14.19.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 14:19:19 -0700 (PDT) To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v5 1/7] support: Add xmkfifo Date: Mon, 21 Mar 2022 18:19:07 -0300 Message-Id: <20220321211913.2347924-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220321211913.2347924-1-adhemerval.zanella@linaro.org> References: <20220321211913.2347924-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 Mon Mar 21 21:19:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 52192 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 5A3D1385E837 for ; Mon, 21 Mar 2022 21:21:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5A3D1385E837 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1647897668; 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=aB4YSSPbZAfTzl5REKQrb+rpzjECOXRFlgyRbc5ZO5vEIduJHJJLe1YepHkcT0oDF VS8rYHFQnQKczZ/jWwUxlhoDRZFTh2X0qlHx0P/9XfT6er5QjTe5CAdI/1nKInwc+j RfczHZwi6n1hRfHlCpdjMKfBmw4GVCcv2x/ZaH1Y= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x329.google.com (mail-ot1-x329.google.com [IPv6:2607:f8b0:4864:20::329]) by sourceware.org (Postfix) with ESMTPS id 963DD389A130 for ; Mon, 21 Mar 2022 21:19:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 963DD389A130 Received: by mail-ot1-x329.google.com with SMTP id e25-20020a0568301e5900b005b236d5d74fso11384589otj.0 for ; Mon, 21 Mar 2022 14:19:22 -0700 (PDT) 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=H1FNS4Nn3g3WpQt2gePU9zSiSi0ga1GiMgMZBCiqKx4wNIp0RIzQVokHdc453chYsf 7FqRJXBl20TT11GVLj5Z9b1bKuttrZjpZwycUfQNAoO5ZA1c7cFGObBfcO7xJhbTfiie JD/OEKincW+UOFQ81QUcmpPYSIRAxX4RZH7D4vdRG/OimpnRn6ume1nTnaflqB7kfqOX m2FC2OGvj2LdDDewOyizs4NlE7fkPuAXL/J2Ud2vfA6CvhMrArX4tuWRnMrTnqclZuKx Amygt9AQJiN3cVS0VC/KuSlR0e/j4vehoffFL200UKJRegt3i8okc20yfb/ShcyxozkN rY5w== X-Gm-Message-State: AOAM532fpUSs0iAicUdIcnxfDSYFAg3sgURn56DF2o6CjRR72jcqvzrx A37tsPIR/uIsVekZBIP0dX7jDf+bjjRogA== X-Google-Smtp-Source: ABdhPJwQh7V42gIwmW6i6LjFr8vX5HCcXJsdnaZqKpWDNt/RBn8lZa5jDylbq6uV6hSpWQogBF4NMQ== X-Received: by 2002:a05:6830:2b08:b0:5b2:72c6:232 with SMTP id l8-20020a0568302b0800b005b272c60232mr8797871otv.212.1647897561475; Mon, 21 Mar 2022 14:19:21 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:2d55:d5f9:2c96:1f30:1f58]) by smtp.gmail.com with ESMTPSA id c37-20020a9d27a8000000b005b23cf22e23sm7925256otb.42.2022.03.21.14.19.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 14:19:20 -0700 (PDT) To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v5 2/7] misc: Add syslog test Date: Mon, 21 Mar 2022 18:19:08 -0300 Message-Id: <20220321211913.2347924-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220321211913.2347924-1-adhemerval.zanella@linaro.org> References: <20220321211913.2347924-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 Mon Mar 21 21:19:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 52193 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 6ADA8386EC0D for ; Mon, 21 Mar 2022 21:21:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6ADA8386EC0D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1647897710; bh=gfZHR6fhSfQ8eByHaobSfA7fO2uw85xyHWo/EqNFFFM=; 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=XqTzeMK6hCrHhx1uKoNeUVdrKITHBabCy06d/FofSvp8b2Z3BuxKqPOvyaJ0otgvT dA2XH18i1i6IGDkJCv9jKfUgm+phc/0HvHy/B4mbTn7065dSGIC96UNPfC7IDaUshx swSVtnyaMKXBMS3ITqteEOBPhLh8bZ273igkAbLc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x30.google.com (mail-oa1-x30.google.com [IPv6:2001:4860:4864:20::30]) by sourceware.org (Postfix) with ESMTPS id 928C43840C2C for ; Mon, 21 Mar 2022 21:19:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 928C43840C2C Received: by mail-oa1-x30.google.com with SMTP id 586e51a60fabf-dda559a410so112632fac.3 for ; Mon, 21 Mar 2022 14:19:24 -0700 (PDT) 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=gfZHR6fhSfQ8eByHaobSfA7fO2uw85xyHWo/EqNFFFM=; b=k3aulv0c/Hp7TmWsU6vGGUx9nnlegdC4RYN/3x9fQsLJwU6yBgafm9ME09bSLQPXh+ G1/41AZwN8uSnvDd7hXKILP4x/DG/cXcAqe0UtuuPUMa1i4YdbLAf5+/lr2F5KClpIMP DBW4Jpwnau9sXPG0F/CwB0uEOyUxGIArHaGHX0MVYWYFeWRTgyMapbOvvdnjU2b7NCqB 7SABGRkrZ3PwC07yE17AeKnz+LW2k+hqiRW35QsuyH5lM+uxFK5KSpAyPu25D2t6a7rf 3xroXGn9GGcxQvKRVRHBHGU6cmf6BThzhLGkKinm2OJBJQlamNENKqQffE7bmIlSZ5P9 qQFg== X-Gm-Message-State: AOAM5322LTrUGuC9vKjncfhQmRsNNFAs9iUJ1WWtl+T0slx1G/osdT1y GAB4V/LGxvtQMEG/8z3uA3PQmNb43aiqLg== X-Google-Smtp-Source: ABdhPJwC9SU2E5ui4QgzYpFzmeeLfMvRkZS4KHdDApdH24fYqvFVxYiRgLLTM5vWq+r4h2knhdHK1A== X-Received: by 2002:a05:6870:2041:b0:db:1a12:6cb1 with SMTP id l1-20020a056870204100b000db1a126cb1mr411441oad.153.1647897563185; Mon, 21 Mar 2022 14:19:23 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:2d55:d5f9:2c96:1f30:1f58]) by smtp.gmail.com with ESMTPSA id c37-20020a9d27a8000000b005b23cf22e23sm7925256otb.42.2022.03.21.14.19.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 14:19:22 -0700 (PDT) To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v5 3/7] misc: syslog: Fix indentation and style Date: Mon, 21 Mar 2022 18:19:09 -0300 Message-Id: <20220321211913.2347924-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220321211913.2347924-1-adhemerval.zanella@linaro.org> References: <20220321211913.2347924-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, 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" And also clenaup the headers, no semantic changes. --- misc/syslog.c | 485 ++++++++++++++++++++++++-------------------------- 1 file changed, 234 insertions(+), 251 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index ee83b1bb76..3b3b757a9a 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -31,49 +31,32 @@ 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 +#include #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 +84,203 @@ 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_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) +__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); - } - } - } - - out: - /* End of critical section. */ - __libc_cleanup_pop (0); - __libc_lock_unlock (syslog_lock); - - if (buf != failbuf) - free (buf); + 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); + + if (buf != failbuf) + free (buf); } /* AF_UNIX address of local logger */ @@ -310,47 +291,49 @@ static const struct sockaddr_un SyslogAddr = }; static void -openlog_internal(const char *ident, int logstat, int logfac) +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 +378,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 Mon Mar 21 21:19:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 52194 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 0B5773840C2C for ; Mon, 21 Mar 2022 21:22:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0B5773840C2C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1647897757; bh=1eFJjA8eHiYgwmfR3QnlJyovmWLwefQR+OIGJFwfp+A=; 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=bJeSR0s39VQpZNlUA9uSDBlpLtr+JHrjhmrAdgRsSA7KBt/hSyHYLVHBUyMMfk7ar GQsyeJ4fv7qtklVUw/9PAJNilI+D0JuxGMeT9cyR846bolc/W7gxiXlN3dYj65AYd+ 8T5qJRS3VVTrmLaXWLgkGT7h6GBtDInoRLt7Ev3w= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x2e.google.com (mail-oa1-x2e.google.com [IPv6:2001:4860:4864:20::2e]) by sourceware.org (Postfix) with ESMTPS id CEE87389EC65 for ; Mon, 21 Mar 2022 21:19:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CEE87389EC65 Received: by mail-oa1-x2e.google.com with SMTP id 586e51a60fabf-dd9d3e7901so111926fac.8 for ; Mon, 21 Mar 2022 14:19:25 -0700 (PDT) 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=1eFJjA8eHiYgwmfR3QnlJyovmWLwefQR+OIGJFwfp+A=; b=VEg1OshKRxaJ4k1DRcgwFcEeyYrfArFHUIhbwTY9YiGPVzUpX4JwvZqH3D9ODLSTiC KeeCVlA5Dnd2kbhjl9kiAC3gR60Egj71O9qsQRdvb2Zil5JMzbKejmSLHzUxQfmEight P1CohxbfB0Uepof1ubUTBsydxKBL2RzZ1JTykhCqMe4FnuqPIKC4qKEC80DDdWGuf01c oXbj/SPDJke8rZmUBjvNaMc9+xFOCorgEEn4nNoTE3k4AeXcSNO18NCTLwB3GCu9EE2m SnNyQFhuD/iv0KZ6UnykFqUtIZLdFs1tNqWKEqRCta7Knwhj6sQWe4KD6RzpHTNhoiir TsxQ== X-Gm-Message-State: AOAM532jVDyh0T3haDUu8JYca4OFDcmupchRwMVQzY6V54swOp4gUDP1 BosCZWl138w4M4tP5eiZ9t6fS7bqzM02Gg== X-Google-Smtp-Source: ABdhPJza1IOlLO8qVeNYA0/6YdPfa8Bf8gfO2BZGEM2cXiCXiHemQcsRE4bhgEe/FzsU3GvKnwH/yw== X-Received: by 2002:a05:6870:4248:b0:dc:c433:8074 with SMTP id v8-20020a056870424800b000dcc4338074mr433596oac.96.1647897564863; Mon, 21 Mar 2022 14:19:24 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:2d55:d5f9:2c96:1f30:1f58]) by smtp.gmail.com with ESMTPSA id c37-20020a9d27a8000000b005b23cf22e23sm7925256otb.42.2022.03.21.14.19.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 14:19:24 -0700 (PDT) To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v5 4/7] misc: syslog: Simplify implementation Date: Mon, 21 Mar 2022 18:19:10 -0300 Message-Id: <20220321211913.2347924-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220321211913.2347924-1-adhemerval.zanella@linaro.org> References: <20220321211913.2347924-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, 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" Use a temporary buffer for strftime instead of using internal libio members, simplify fprintf call on the memstream and memory allocation, use %b instead of %h, use dprintf instead of writev for LOG_PERROR. Checked on x86_64-linux-gnu and i686-linux-gnu. --- misc/syslog.c | 95 ++++++++++++++------------------------------------- 1 file changed, 26 insertions(+), 69 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index 3b3b757a9a..c9db35b8c8 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -42,8 +42,6 @@ static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94"; #include #include -#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 */ @@ -122,13 +120,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 []"]; @@ -142,9 +137,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); @@ -158,51 +151,22 @@ __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); - } - + /* "%b %e %H:%M:%S" */ + char timebuf[sizeof "MMM DD hh:mm:ss "]; + time_t now = time_now (); + struct tm now_tm; + __localtime_r (&now, &now_tm); + __strftime_l (timebuf, sizeof (timebuf), "%b %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); @@ -216,26 +180,18 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, /* Tell the cancellation handler to free this buffer. */ clarg.buf = buf; } + else + { + /* Nothing much to do but emit an error message. */ + 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, + "\n" + (buf[bufsize - 1] == '\n')); /* Get connected, output the message to the local logger. */ if (!connected) @@ -264,6 +220,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)) >= 0) From patchwork Mon Mar 21 21:19:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 52195 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 E1CDE389A103 for ; Mon, 21 Mar 2022 21:23:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E1CDE389A103 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1647897820; bh=Iy5GdLuiLC3dEGkL7NFnCWRNfZSZ7w9NQdsvdR62e8I=; 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=EEQldE6Wgkp4ZL84rG3W1nHg1peuZz4DUGLdWI23ePasOdTEV7Zhti7KxIV6BHfpJ HctHhMp/6kEP9cAvKbL3BWO3OMdTy27OyvYnWbuJhPAYBWI53g73WRUYX95klcyv7J zzHcik4QO/0BvmMnYMDAbQT6rxkuujpZMYVvhoL8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc30.google.com (mail-oo1-xc30.google.com [IPv6:2607:f8b0:4864:20::c30]) by sourceware.org (Postfix) with ESMTPS id 71BFB389EC60 for ; Mon, 21 Mar 2022 21:19:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 71BFB389EC60 Received: by mail-oo1-xc30.google.com with SMTP id w3-20020a4ac183000000b0031d806bbd7eso20610517oop.13 for ; Mon, 21 Mar 2022 14:19:27 -0700 (PDT) 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=Iy5GdLuiLC3dEGkL7NFnCWRNfZSZ7w9NQdsvdR62e8I=; b=VgS+entixO8nOBxP6/VfVgxg8Q9BJNhGGXHgm9MuURjedtk+ChLaQLACrtgDBOHMD4 aERGv4deIBiR1cW0IK0AhQehviqjo7aTqjwnVdlnXAJikp/NlLjvtLaAC4owuTyPF3CO XpixTS2mrixmClvyOYsuM8e/rYlFC2Rov8IWHG2cyYNCjCzNhLJJb0qPPQilUitOBtgB xEU8JF7XsLG0oQIz4vntiu1YeodHq5A/wtcsQ53Z3xg2f4k1jVEswTv8MGJ1ZkdECRhS 7wZ5CSubAI4uJaEhwZzcdjwbDQtbUWtDDwZZlt752AAq5ETuac3FWzuymwfrptDy9vFu 58SA== X-Gm-Message-State: AOAM531/T/50+6ChHXFmYpOv9OReAyzKEVTKT1/7EmRrpR2etAr/4fn/ ItSUdyGI3VwE7RVHmc7gOp19yGa6BK2K9Q== X-Google-Smtp-Source: ABdhPJwLuEoXwpvINtnaEvEObQQCFunCrUQOZvj2BMDztlkdFMJGWAc5JLdGq0+oA0G3prEoHpQJEw== X-Received: by 2002:a05:6870:529:b0:da:b3f:3226 with SMTP id j41-20020a056870052900b000da0b3f3226mr446275oao.214.1647897566482; Mon, 21 Mar 2022 14:19:26 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:2d55:d5f9:2c96:1f30:1f58]) by smtp.gmail.com with ESMTPSA id c37-20020a9d27a8000000b005b23cf22e23sm7925256otb.42.2022.03.21.14.19.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 14:19:26 -0700 (PDT) To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v5 5/7] misc: syslog: Use fixed-sized buffer and remove memstream Date: Mon, 21 Mar 2022 18:19:11 -0300 Message-Id: <20220321211913.2347924-6-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220321211913.2347924-1-adhemerval.zanella@linaro.org> References: <20220321211913.2347924-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" A fixed-sized 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. Also, since the buffer size is know, memstream is replaced with a malloced buffer for larger messages. Checked on x86_64-linux-gnu. --- misc/syslog.c | 86 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 34 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index c9db35b8c8..1b18375c95 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -120,12 +120,12 @@ void __vsyslog_internal (int pri, const char *fmt, va_list ap, unsigned int mode_flags) { - FILE *f; - char *buf = 0; + /* Try to use a static buffer as an optimization. */ + char bufs[1024]; + char *buf = NULL; size_t bufsize = 0; 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. */ @@ -149,43 +149,61 @@ __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; + + /* "%b %e %H:%M:%S " */ + char timestamp[sizeof "MMM DD hh:mm:ss "]; + time_t now = time_now (); + struct tm now_tm; + __localtime_r (&now, &now_tm); + __strftime_l (timestamp, sizeof timestamp, "%b %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) + + int l = __snprintf (bufs, sizeof bufs, + SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + if (0 <= l && l < sizeof bufs) { - __fsetlocking (f, FSETLOCKING_BYCALLER); - /* "%b %e %H:%M:%S" */ - char timebuf[sizeof "MMM DD hh:mm:ss "]; - time_t now = time_now (); - struct tm now_tm; - __localtime_r (&now, &now_tm); - __strftime_l (timebuf, sizeof (timebuf), "%b %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); - - /* Close the memory stream; this will finalize the data into a malloc'd - buffer in BUF. */ - fclose (f); + int vl = __vsnprintf_internal (bufs + l, sizeof bufs - l, fmt, apc, + mode_flags); + if (0 <= vl && vl < sizeof bufs - l) + { + buf = bufs; + bufsize = l + vl; + } - /* Tell the cancellation handler to free this buffer. */ - clarg.buf = buf; + va_end (apc); } - else + + if (buf == NULL) { - /* Nothing much to do but emit an error message. */ - bufsize = __snprintf (failbuf, sizeof failbuf, "out of memory[%d]", - __getpid ()); - buf = failbuf; + buf = malloc (l * sizeof (char)); + if (buf != NULL) + { + /* Tell the cancellation handler to free this buffer. */ + clarg.buf = buf; + + __snprintf (buf, sizeof buf, + SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + } + else + { + /* Nothing much to do but emit an error message. */ + bufsize = __snprintf (bufs, sizeof bufs, + "out of memory[%d]", __getpid ()); + buf = bufs; + } } /* Output to stderr if requested. */ @@ -236,7 +254,7 @@ out: __libc_cleanup_pop (0); __libc_lock_unlock (syslog_lock); - if (buf != failbuf) + if (buf != bufs) free (buf); } From patchwork Mon Mar 21 21:19:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 52196 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 AC8D6385E83F for ; Mon, 21 Mar 2022 21:24:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AC8D6385E83F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1647897862; bh=6OIb8Zov1Gm7UqKBymxUdiEC8CmG2TRj2weOyPIihDk=; 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=l9HIs9seCA9B2ld/xH+rYEtpsxNx9Re3JhMbAw6C8EH4tOo+m8eazoYc3n8gyFXPj R6l5day8uppokn+GxRUGCjOuknqgdx5oTh4MdqgajTnGLhhn5qahcE4AehVtSvtIVt u2BZD1QSjtJfwtUKyC/q8+zSqBhkWsy/CnHiVzxA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x33.google.com (mail-oa1-x33.google.com [IPv6:2001:4860:4864:20::33]) by sourceware.org (Postfix) with ESMTPS id 51DDF3899427 for ; Mon, 21 Mar 2022 21:19:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 51DDF3899427 Received: by mail-oa1-x33.google.com with SMTP id 586e51a60fabf-dda559a410so112740fac.3 for ; Mon, 21 Mar 2022 14:19:29 -0700 (PDT) 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=6OIb8Zov1Gm7UqKBymxUdiEC8CmG2TRj2weOyPIihDk=; b=OXlkiHC5us5JMx64nucXk+JqiZGKOWUHMCD8bNIvo15mbH34niFs3jTbEvMTPFHh4V uKcLFYBktZq1LJIPukbztMkTbLQ9SvE3etrMqWGsxooYGtbmNDO86fjWJ7kQpYGtMLu4 slpi9je+ghnhkThHXcse8FiNt4VaYvh4OWRCbjnI5HSiycIL9W0EXK9zXZUPz4Jfgohe fjQiYdaaKZIMMPs+du4WaXb9baugB5w/lO5DE2tFI3S5ZJ25TOQ/a5p0zPOAgLFirSeG oDvAkmv/zb8zOHAz/8x8WnxqEI9Sn8rd7Xn/KXllsOcrmTOHQ8ADrjzZK+970lvmaHaZ tbTg== X-Gm-Message-State: AOAM533Mfp0g9lvP+do/0pwVgNa15UA++aiKPM8WyNZJhL/MRqUXUKFo u9A05NNUhta7RJDJy+yd5/uRsiNmFCWnVg== X-Google-Smtp-Source: ABdhPJyNq2ijiew3Wx87SMGNUwS2lLvK3JxiJazzFyBVuKkFXhaDUi4VEkFO0Oie9HuXnyJ/9n3fFA== X-Received: by 2002:a05:6870:d0c4:b0:dd:c7f6:2d62 with SMTP id k4-20020a056870d0c400b000ddc7f62d62mr424477oaa.99.1647897568089; Mon, 21 Mar 2022 14:19:28 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:2d55:d5f9:2c96:1f30:1f58]) by smtp.gmail.com with ESMTPSA id c37-20020a9d27a8000000b005b23cf22e23sm7925256otb.42.2022.03.21.14.19.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 14:19:27 -0700 (PDT) To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v5 6/7] misc: syslog: Move SYSLOG_NAME to USE_MISC (BZ #16355) Date: Mon, 21 Mar 2022 18:19:12 -0300 Message-Id: <20220321211913.2347924-7-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220321211913.2347924-1-adhemerval.zanella@linaro.org> References: <20220321211913.2347924-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, 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..d933fea104 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 Mon Mar 21 21:19:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 52197 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 4AEED3898C73 for ; Mon, 21 Mar 2022 21:25:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4AEED3898C73 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1647897904; bh=KUYhm0gc+Y2uof8ds0YJYNMonvdtWyZ4uI7g8EatBss=; 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=o9iIv6mPWet3e5eb6YRFg9MR/ZKm+W+fkwqMbgrFLto9mO59eAeGW4GwumpHauNqj 5syIifw4eSknHvyZ3LWOVtLWt4yLege5uAr/gx+EaTCt2ZRogLrYaQOCfvktcRgWbR 3hj8ssL3DP4M4eqO1K23EI0NagDgHQIfdPiZdMQY= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc2b.google.com (mail-oo1-xc2b.google.com [IPv6:2607:f8b0:4864:20::c2b]) by sourceware.org (Postfix) with ESMTPS id BC955393BC36 for ; Mon, 21 Mar 2022 21:19:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BC955393BC36 Received: by mail-oo1-xc2b.google.com with SMTP id u30-20020a4a6c5e000000b00320d8dc2438so20665913oof.12 for ; Mon, 21 Mar 2022 14:19:30 -0700 (PDT) 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=KUYhm0gc+Y2uof8ds0YJYNMonvdtWyZ4uI7g8EatBss=; b=yRq5fbgaDqeDOzDcKRKMBQWX4f6FuZ+MqinYX3/6NU/Zt8/6seXPapGdkOPvHCYWv8 zuJX2ohP1U1O8FbgdaOC4SSBdtm7Zul/Jngxv0Ut8Rp8Bx02gHmRXXuEeafiGw3I1/Q+ l9ALwEyw1WOBAt3N5kxOXTq8EhQHiXhu6sazH/RwarCoD8QZXR5RcvH0EfnLmLx7O6if U/NDK2qqJQSgVgT+bBugjQQbN1FeMi2H1lU4SaG4DfrwDwJ+tN53PFin+yStMU7LmvF0 xlq+pzedBunu/xPJ8V+CzdoZ62TRxu8HbG1YnbhNIROCoMl+3RTPdmUne9Rjaxbe6Xz9 vt5g== X-Gm-Message-State: AOAM5310aL/6aJd+99A12o/YvBVc7pNX+PCJ9+HknzBo4d7nye7b88tt IMzkE4Ksqm/Z82v+qtk5TkDUXFZ2o5OqZg== X-Google-Smtp-Source: ABdhPJx1TM+36PeQ9ZVGiskEt5PnvRiaTbAqjTGKapI6O7HRHqvFJhgr4azvVAVcgncx00j7QJ7/BQ== X-Received: by 2002:a05:6870:17a9:b0:dd:c79d:21ec with SMTP id r41-20020a05687017a900b000ddc79d21ecmr424575oae.24.1647897569726; Mon, 21 Mar 2022 14:19:29 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:2d55:d5f9:2c96:1f30:1f58]) by smtp.gmail.com with ESMTPSA id c37-20020a9d27a8000000b005b23cf22e23sm7925256otb.42.2022.03.21.14.19.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 14:19:29 -0700 (PDT) To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v5 7/7] misc: Use 64 bit time_t interfaces on syslog Date: Mon, 21 Mar 2022 18:19:13 -0300 Message-Id: <20220321211913.2347924-8-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220321211913.2347924-1-adhemerval.zanella@linaro.org> References: <20220321211913.2347924-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" It also handles the highly unlikely case where localtime might return NULL, in this case only the PRI is set to hopefully instruct the relay to get eh TIMESTAMP (as defined by the RFC). Checked on x86_64-linux-gnu and i686-linux-gnu. --- misc/syslog.c | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index 1b18375c95..554089bfc4 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -153,11 +153,18 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, /* "%b %e %H:%M:%S " */ char timestamp[sizeof "MMM DD hh:mm:ss "]; - time_t now = time_now (); + __time64_t now = time64_now (); struct tm now_tm; - __localtime_r (&now, &now_tm); - __strftime_l (timestamp, sizeof timestamp, "%b %e %T ", &now_tm, - _nl_C_locobj_ptr); + struct tm *now_tmp = __localtime64_r (&now, &now_tm); + bool has_ts = now_tmp != NULL; + + /* In the unlikely case of localtime_r failure (tm_year out of int range) + skip the hostname so the message is handled as valid PRI but without + TIMESTAMP or invalid TIMESTAMP (which should force the relay to add the + timestamp itself). */ + if (has_ts) + __strftime_l (timestamp, sizeof timestamp, "%h %e %T ", now_tmp, + _nl_C_locobj_ptr); #define SYSLOG_HEADER(__pri, __timestamp, __msgoff, pid) \ "<%d>%s %n%s%s%.0d%s: ", \ @@ -165,8 +172,16 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, LogTag == NULL ? __progname : LogTag, \ "[" + (pid == 0), pid, "]" + (pid == 0) - int l = __snprintf (bufs, sizeof bufs, - SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); +#define SYSLOG_HEADER_WITHOUT_TS(__pri, __msgoff) \ + "<%d>: %n", __pri, __msgoff + + int l; + if (has_ts) + l = __snprintf (bufs, sizeof bufs, + SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + else + l = __snprintf (bufs, sizeof bufs, + SYSLOG_HEADER_WITHOUT_TS (pri, &msgoff)); if (0 <= l && l < sizeof bufs) { va_list apc; @@ -194,8 +209,12 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, /* Tell the cancellation handler to free this buffer. */ clarg.buf = buf; - __snprintf (buf, sizeof buf, - SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + if (has_ts) + __snprintf (bufs, sizeof bufs, + SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + else + __snprintf (bufs, sizeof bufs, + SYSLOG_HEADER_WITHOUT_TS (pri, &msgoff)); } else {