From patchwork Mon Apr 12 21:11:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 42950 X-Patchwork-Delegate: carlos@redhat.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C24EA3939C19; Mon, 12 Apr 2021 21:11:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C24EA3939C19 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1618261880; bh=A0d+0O8DLfRIFZQuMtGhZDbK135or2anEorsGZLxmrc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=SMYyQXWCKPxENOVelOJqn+093utKvpF4drOVC0B5kS5GkKNIbVMmwlUZNrUPhx1a5 tsK062HQl91jg4eSSUTbfujqQj1Khxf1ag23Pg6plYPHyJWjWcc1m6YyhT9W6Len70 AKBK2GrZwGjsJF5ngr9KWrvWJjpfhz2nBSfRlvlw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qv1-xf32.google.com (mail-qv1-xf32.google.com [IPv6:2607:f8b0:4864:20::f32]) by sourceware.org (Postfix) with ESMTPS id AC294385480B for ; Mon, 12 Apr 2021 21:11:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org AC294385480B Received: by mail-qv1-xf32.google.com with SMTP id 30so7045192qva.9 for ; Mon, 12 Apr 2021 14:11:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=A0d+0O8DLfRIFZQuMtGhZDbK135or2anEorsGZLxmrc=; b=EUuCoMkW5omN8t61CYWFPrI4ektjJgehU2GF2Uwykogk2bXAe4g3ZeFVxgyRVscB0U Iw8HIElDpjn7Bh4YJfJEni1yEVlDAFFfascvE3JsAM4GWX0cjtIu7DBysSvxiuSRs/+q KyMB8N2MnBTLJsdSLc+qJS2wRgj84hbFlmxvjFg36iZ6qQtd5oUronjUqAliSgdrBWSl hmh/JccJ3SjHXLzXN23EOWSAHfiwP+bReKbgJGe/zw/gumir5e4RBoy4PxAlPlQzKVsu GCFGXdwoKv+rrXYWmOiZaKzAp7uNwcEM/1XKX5vORVrSWISWsKxmFWrbvCPs2p+EChT3 5Qfg== X-Gm-Message-State: AOAM531QepxPnop2NrkCJabjJ+CgyLaXDtN+QL6xXP8yq3NLEoaYPMbX e1Mti/kOj6Vp6t0RkPMEzQEQlHriITaW2kmA X-Google-Smtp-Source: ABdhPJxEcEaDjyc7ZcMHcinfXaIRKB5yJX8PMmyRWS7e3lEhPL4Hg1IXeQmeP9sPLeKByu/FYHbYmg== X-Received: by 2002:a0c:ed50:: with SMTP id v16mr29621366qvq.54.1618261878136; Mon, 12 Apr 2021 14:11:18 -0700 (PDT) Received: from localhost.localdomain ([177.194.41.149]) by smtp.googlemail.com with ESMTPSA id i22sm3073392qko.135.2021.04.12.14.11.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Apr 2021 14:11:17 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 01/11] support: Add xmkfifo Date: Mon, 12 Apr 2021 18:11:03 -0300 Message-Id: <20210412211113.393120-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@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 900e17f94f..cbf2efdf5f 100644 --- a/support/Makefile +++ b/support/Makefile @@ -114,6 +114,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..d9f3e9fde7 --- /dev/null +++ b/support/xmkfifo.c @@ -0,0 +1,29 @@ +/* mkfifo with error checking. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +void +xmkfifo (const char *pathname, mode_t mode) +{ + int r = mkfifo (pathname, mode); + if (r < 0) + FAIL_EXIT1 ("mkfifo (%s, %d): %m", pathname, mode); +} diff --git a/support/xunistd.h b/support/xunistd.h index c642588c29..86fee87b7a 100644 --- a/support/xunistd.h +++ b/support/xunistd.h @@ -48,6 +48,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 *pathtname, mode_t mode); /* Equivalent of "mkdir -p". */ void xmkdirp (const char *, mode_t); From patchwork Mon Apr 12 21:11:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 42951 X-Patchwork-Delegate: carlos@redhat.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4FF79394FC1B; Mon, 12 Apr 2021 21:11:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4FF79394FC1B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1618261884; bh=Ldg+W/dOmjZc6BBcE3BhNLzNp89hQoKwwkuItqvrJ+s=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=Qgaqqx3wu9N6DHtKYAbv285RgjdFc+KWLl3EwivJwLRqx25l2aGxUcHg/+QwiqC7N qldw/IaAs/QOtbOFtS7wsGf0wn5MvBVX7xBwz2+HGk81nx1qeUbPQY6N7GYuAArS/O fX0Ud1HX9DI2WfzGW+nuBRMSbvFagwUbOM7shUAY= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x82e.google.com (mail-qt1-x82e.google.com [IPv6:2607:f8b0:4864:20::82e]) by sourceware.org (Postfix) with ESMTPS id 148F4385480B for ; Mon, 12 Apr 2021 21:11:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 148F4385480B Received: by mail-qt1-x82e.google.com with SMTP id i6so1499677qti.10 for ; Mon, 12 Apr 2021 14:11:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ldg+W/dOmjZc6BBcE3BhNLzNp89hQoKwwkuItqvrJ+s=; b=n3H/NEuQIj2X/OuniYznXrCMdYHwMH/VNzgclMzan/ZE8NyLdiwDAEb6whR5MEAHm6 7q9rqXmKMkfVDTpzSyBnfy94DVkT9OYkp1XD+BP5GMyRufrFg8XDx3FIxllSUgGSoz/A z9hpguC1wIkAmTlWNqkA8VcS5Xl/v46UgQtsudFFWITDulxEqA4E97D61VMZygQQTt/m JwDek3YHPi92OILPB306PPaBbVmXFQpNB730cZTgh1NhUjuUt3P8ZirmoS7hL2rBa7qQ HogU+74p98B9JhgZLQjsSv5h6bh4PYm0ZWezbo+PbmZP36ryIQp/u6spdsWk6TMepR91 W0lA== X-Gm-Message-State: AOAM531Ezpi7V5VAwrb0ilRmtgvCZIxsEz9vXY36i1ZzOM0e0Bc0ruM+ q2bHAxvFZajOQ47VWpCri70mpZ3zKcT/J7GM X-Google-Smtp-Source: ABdhPJxZhmE/hMZPLQqJB8st1nKOm9Azgl23iUePr8GpY9sJBCUHBzTwWDu45SZyQrzEjQEFwKY7Bg== X-Received: by 2002:ac8:7dd2:: with SMTP id c18mr27673820qte.301.1618261879239; Mon, 12 Apr 2021 14:11:19 -0700 (PDT) Received: from localhost.localdomain ([177.194.41.149]) by smtp.googlemail.com with ESMTPSA id i22sm3073392qko.135.2021.04.12.14.11.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Apr 2021 14:11:19 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 02/11] misc: Add syslog test Date: Mon, 12 Apr 2021 18:11:04 -0300 Message-Id: <20210412211113.393120-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210412211113.393120-1-adhemerval.zanella@linaro.org> References: <20210412211113.393120-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" The test cover: - All possible priorities and facilities through TCP and UDP. - Same syslog tests for vsyslog. - Some openlog/syslog/close combinations. - openlog with LOG_CONS, LOG_PERROR, and LOG_PID. Internally is done with a test-container where the main process mimics the syslog server interface. The test does not cover multithread and async-signal usage. Checked on x86_64-linux-gnu. --- misc/Makefile | 2 + misc/tst-syslog.c | 483 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 485 insertions(+) create mode 100644 misc/tst-syslog.c diff --git a/misc/Makefile b/misc/Makefile index 38dad737f2..63812124f2 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -107,6 +107,8 @@ tests-special += $(objpfx)tst-error1-mem.out \ $(objpfx)tst-allocate_once-mem.out endif +tests-container := tst-syslog + CFLAGS-select.c += -fexceptions -fasynchronous-unwind-tables CFLAGS-tsearch.c += $(uses-callbacks) CFLAGS-lsearch.c += $(uses-callbacks) diff --git a/misc/tst-syslog.c b/misc/tst-syslog.c new file mode 100644 index 0000000000..ed8c528db4 --- /dev/null +++ b/misc/tst-syslog.c @@ -0,0 +1,483 @@ +/* Basic tests for syslog interfaces. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const int facilities[] = + { + LOG_KERN, + LOG_USER, + LOG_MAIL, + LOG_DAEMON, + LOG_AUTH, + LOG_SYSLOG, + LOG_LPR, + LOG_NEWS, + LOG_UUCP, + LOG_CRON, + LOG_AUTHPRIV, + LOG_FTP, + LOG_LOCAL0, + LOG_LOCAL1, + LOG_LOCAL2, + LOG_LOCAL3, + LOG_LOCAL4, + LOG_LOCAL5, + LOG_LOCAL6, + LOG_LOCAL7, + }; + +static const int priorities[] = + { + LOG_EMERG, + LOG_ALERT, + LOG_CRIT, + LOG_ERR, + LOG_WARNING, + LOG_NOTICE, + LOG_INFO, + LOG_DEBUG + }; + +enum + { + ident_length = 64, + msg_length = 64 + }; + +#define SYSLOG_MSG_BASE "syslog_message" +#define OPENLOG_IDENT "openlog_ident" + +struct msg_t + { + int priority; + int facility; + char ident[ident_length]; + char msg[msg_length]; + pid_t pid; + }; + +static void +call_vsyslog (int priority, const char *format, ...) +{ + va_list ap; + va_start (ap, format); + vsyslog (priority, format, ap); + va_end (ap); +} + +static void +send_vsyslog (int options) +{ + for (size_t i = 0; i < array_length (facilities); i++) + { + for (size_t j = 0; j < array_length (priorities); j++) + { + int facility = facilities[i]; + int priority = priorities[j]; + call_vsyslog (facility | priority, "%s %d %d", SYSLOG_MSG_BASE, + facility, priority); + } + } +} + +static void +send_syslog (int options) +{ + for (size_t i = 0; i < array_length (facilities); i++) + { + for (size_t j = 0; j < array_length (priorities); j++) + { + int facility = facilities[i]; + int priority = priorities[j]; + syslog (facility | priority, "%s %d %d", SYSLOG_MSG_BASE, facility, + priority); + } + } +} + +static bool +check_syslog_message (const struct msg_t *msg, int msgnum, int options, + pid_t pid) +{ + if (msgnum == array_length (facilities) * array_length (priorities) - 1) + return false; + + int i = msgnum / array_length (priorities); + int j = msgnum % array_length (priorities); + + int expected_facility = facilities[i]; + /* With no preceding openlog, syslog default to LOG_USER. */ + if (expected_facility == LOG_KERN) + expected_facility = LOG_USER; + int expected_priority = priorities[j]; + + TEST_COMPARE (msg->facility, expected_facility); + TEST_COMPARE (msg->priority, expected_priority); + + return true; +} + +static void +send_openlog (int options) +{ + /* Define a non-default IDENT and a not default facility. */ + openlog (OPENLOG_IDENT, options, LOG_LOCAL0); + for (size_t j = 0; j < array_length (priorities); j++) + { + int priority = priorities[j]; + syslog (priority, "%s %d %d", SYSLOG_MSG_BASE, LOG_LOCAL0, priority); + } + closelog (); + + /* Back to the default IDENT with a non default facility. */ + openlog (NULL, 0, LOG_LOCAL6); + for (size_t j = 0; j < array_length (priorities); j++) + { + int priority = priorities[j]; + syslog (LOG_LOCAL7 | priority, "%s %d %d", SYSLOG_MSG_BASE, LOG_LOCAL7, + priority); + } + closelog (); + + /* LOG_KERN does not change the internal default facility. */ + openlog (NULL, 0, LOG_KERN); + for (size_t j = 0; j < array_length (priorities); j++) + { + int priority = priorities[j]; + syslog (priority, "%s %d %d", SYSLOG_MSG_BASE, LOG_KERN, priority); + } + closelog (); +} + +static bool +check_openlog_message (const struct msg_t *msg, int msgnum, + int options, pid_t pid) +{ + if (msgnum == 3 * array_length (priorities) - 1) + return false; + + int expected_priority = priorities[msgnum % array_length (priorities)]; + TEST_COMPARE (msg->priority, expected_priority); + + char expected_ident[ident_length]; + snprintf (expected_ident, sizeof (expected_ident), "%s%s%.0d%s:", + OPENLOG_IDENT, + options & LOG_PID ? "[" : "", + options & LOG_PID ? pid : 0, + options & LOG_PID ? "]" : ""); + + if (msgnum < array_length (priorities)) + { + if (options & LOG_PID) + TEST_COMPARE (msg->pid, pid); + TEST_COMPARE_STRING (msg->ident, expected_ident); + TEST_COMPARE (msg->facility, LOG_LOCAL0); + } + else if (msgnum < 2 * array_length (priorities)) + { + TEST_COMPARE (msg->facility, LOG_LOCAL7); + } + /* Since openlog (LOG_KERN) does not change the facility, we need to check + against the previous valid openlog. */ + else if (msgnum < 3 * array_length (priorities)) + { + TEST_COMPARE (msg->facility, LOG_LOCAL6); + } + + return true; +} + +static struct msg_t +parse_syslog_msg (const char *msg) +{ + struct msg_t r = { .pid = -1 }; + int number; + + /* The message in the form: + <179>Apr 8 14:51:19 tst-syslog: syslog message 176 3 */ + int n = sscanf (msg, "<%3d>%*s %*d %*d:%*d:%*d %32s %64s %*d %*d", + &number, r.ident, r.msg); + TEST_COMPARE (n, 3); + + r.facility = number & LOG_FACMASK; + r.priority = number & LOG_PRIMASK; + + char *pid_start = strchr (r.ident, '['); + if (pid_start != NULL) + { + char *pid_end = strchr (r.ident, ']'); + if (pid_end != NULL) + r.pid = strtoul (pid_start + 1, NULL, 10); + } + + return r; +} + +static struct msg_t +parse_syslog_console (const char *msg) +{ + int priority; + int facility; + struct msg_t r; + + /* The message in the form: + openlog_ident: syslog_message 128 0 */ + int n = sscanf (msg, "%32s %64s %d %d", + r.ident, r.msg, &facility, &priority); + TEST_COMPARE (n, 4); + + r.facility = facility; + r.priority = priority; + + return r; +} + +static void +check_syslog_udp (void (*syslog_send)(int), int options, + bool (*syslog_check)(const struct msg_t *, int, int, + pid_t)) +{ + struct sockaddr_un addr = + { + .sun_family = AF_UNIX, + .sun_path = _PATH_LOG + }; + + socklen_t addrlen = sizeof (addr); + int server_udp = xsocket (AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0); + xbind (server_udp, (struct sockaddr *) &addr, addrlen); + + pid_t sender_pid = xfork (); + if (sender_pid == 0) + { + syslog_send (options); + _exit (0); + } + + int msgnum = 0; + while (1) + { + char buf[512]; + size_t l = xrecvfrom (server_udp, buf, sizeof (buf), 0, + (struct sockaddr *) &addr, &addrlen); + buf[l] = '\0'; + + struct msg_t msg = parse_syslog_msg (buf); + if (!syslog_check (&msg, msgnum++, options, sender_pid)) + break; + } + + xclose (server_udp); + + int status; + xwaitpid (sender_pid, &status, 0); + TEST_COMPARE (status, 0); + + unlink (_PATH_LOG); +} + +static void +check_syslog_tcp (void (*syslog_send)(int), int options, + bool (*syslog_check)(const struct msg_t *, int, int, + pid_t)) +{ + struct sockaddr_un addr = + { + .sun_family = AF_UNIX, + .sun_path = _PATH_LOG + }; + socklen_t addrlen = sizeof (addr); + + int server_tcp = xsocket (AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + xbind (server_tcp, (struct sockaddr *) &addr, addrlen); + xlisten (server_tcp, 5); + + pid_t sender_pid = xfork (); + if (sender_pid == 0) + { + syslog_send (options); + _exit (0); + } + + int client_tcp = xaccept (server_tcp, NULL, NULL); + + char buf[512], *rb = buf; + size_t rbl = sizeof (buf); + size_t prl = 0; /* Track the size of the partial record. */ + int msgnum = 0; + + while (1) + { + size_t rl = xrecvfrom (client_tcp, rb, rbl - prl, 0, NULL, NULL); + if (rl == 0) + break; + + /* Iterate over the buffer to find and check the record. */ + size_t l = rl + prl; + char *b = buf; + while (1) + { + /* With TCP each record ends with a '\0'. */ + char *e = memchr (b, '\0', l); + if (e != NULL) + { + struct msg_t msg = parse_syslog_msg (b); + if (!syslog_check (&msg, msgnum++, options, sender_pid)) + break; + + /* Advance to the next record. */ + ptrdiff_t diff = e + 1 - b; + b += diff; + l -= diff; + } + else + { + /* Move the partial record to the start of the buffer. */ + memmove (buf, b, l); + rb = buf + l; + prl = l; + break; + } + } + } + + xclose (client_tcp); + xclose (server_tcp); + + int status; + xwaitpid (sender_pid, &status, 0); + TEST_COMPARE (status, 0); + + unlink (_PATH_LOG); +} + +static void +check_syslog_console_read (FILE *fp) +{ + char buf[512]; + int msgnum = 0; + while (fgets (buf, sizeof (buf), fp) != NULL) + { + struct msg_t msg = parse_syslog_console (buf); + TEST_COMPARE_STRING (msg.ident, OPENLOG_IDENT ":"); + TEST_COMPARE (msg.priority, priorities[msgnum]); + TEST_COMPARE (msg.facility, LOG_LOCAL0); + + if (++msgnum == array_length (priorities)) + break; + } +} + +static void +check_syslog_console (void) +{ + xmkfifo (_PATH_CONSOLE, 0666); + + pid_t sender_pid = xfork (); + if (sender_pid == 0) + { + send_openlog (LOG_CONS); + _exit (0); + } + + { + FILE *fp = xfopen (_PATH_CONSOLE, "r+"); + check_syslog_console_read (fp); + xfclose (fp); + } + + int status; + xwaitpid (sender_pid, &status, 0); + TEST_COMPARE (status, 0); + + unlink (_PATH_CONSOLE); +} + +static void +send_openlog_callback (void *clousure) +{ + int options = *(int *) clousure; + send_openlog (options); +} + +static void +check_syslog_perror (void) +{ + struct support_capture_subprocess result; + result = support_capture_subprocess (send_openlog_callback, + &(int){LOG_PERROR}); + + FILE *mfp = fmemopen (result.err.buffer, result.err.length, "r"); + if (mfp == NULL) + FAIL_EXIT1 ("fmemopen: %m"); + check_syslog_console_read (mfp); + xfclose (mfp); + + support_capture_subprocess_check (&result, "tst-openlog-child", 0, + sc_allow_stderr); + support_capture_subprocess_free (&result); +} + +static int +do_test (void) +{ + add_temp_file (_PATH_LOG); + add_temp_file (_PATH_CONSOLE); + + /* Send every combination of facility/priority over UDP and TCP. */ + check_syslog_udp (send_syslog, 0, check_syslog_message); + check_syslog_tcp (send_syslog, 0, check_syslog_message); + + /* Also check vsyslog. */ + check_syslog_udp (send_vsyslog, 0, check_syslog_message); + check_syslog_tcp (send_vsyslog, 0, check_syslog_message); + + /* Run some openlog/syslog/closelog combinations. */ + check_syslog_udp (send_openlog, 0, check_openlog_message); + check_syslog_tcp (send_openlog, 0, check_openlog_message); + + /* Check the LOG_PID option. */ + check_syslog_udp (send_openlog, LOG_PID, check_openlog_message); + check_syslog_tcp (send_openlog, LOG_PID, check_openlog_message); + + /* Check the LOG_CONS option. */ + check_syslog_console (); + + /* Check the LOG_PERROR option. */ + check_syslog_perror (); + + return 0; +} + +#include From patchwork Mon Apr 12 21:11:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 42953 X-Patchwork-Delegate: carlos@redhat.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E3D67394FC18; Mon, 12 Apr 2021 21:11:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E3D67394FC18 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1618261886; bh=TpHwy3TDVXOds5CUWXmjeodKtZPETTuqT/A1Cj8Md1E=; 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=hH2byuT8JUYzEy256toSuySjCfje6DmtYxP+C7R84tvPqpUsRUaManq+e3HP8z2qQ O/+ugIy6vCV3Iijzid/T7ReAYPd1DILb5mBKH6S7wbZXm+1LJfwliJ/ForjceKi/d9 w3tZH9aGvVyBYVGKls9QLkXh0ugiZGJzPfxoEAoE= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x735.google.com (mail-qk1-x735.google.com [IPv6:2607:f8b0:4864:20::735]) by sourceware.org (Postfix) with ESMTPS id 7A6363938C35 for ; Mon, 12 Apr 2021 21:11:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 7A6363938C35 Received: by mail-qk1-x735.google.com with SMTP id x11so15764340qkp.11 for ; Mon, 12 Apr 2021 14:11:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TpHwy3TDVXOds5CUWXmjeodKtZPETTuqT/A1Cj8Md1E=; b=F+8GAlxqTP605LmleQn9kUerZB/YaWrXVN3SBhOf4mOtctdZTLyvL5VjzA9+HK9z7X lI6ZoDzDMJMpchxzecSu+JVKtCQBfm5bZjQid1WLUA9Jfla/PPba3bmeuL4nUwmsTTlp dcupWHnWjerN+6kgZptZbjYXpvu8Ati+pMmMcq9U/fMxk7N1HesmFuPaK3+qQ9UX18Ab kkisGc6yEthf5pfMyKOxwO5N3x2WnknNGQfKrSnZ/QR++yQd3Td7khwb/zc1Wmc+H0oV WFTY7NIQOZrIXm144VFstEXNG8iIK4i0Px3fNtJMCKKjiPRnrn+237YN+F6M3w+0UNeo 3Jkg== X-Gm-Message-State: AOAM530HX1/Egv3gB5yxm1BI8gfeeLr25n/pTWisa8cgALp1aFhrY20D juQ1eFUCdrF8IzWthhj06fJBNFTrAWNPSBZ3 X-Google-Smtp-Source: ABdhPJwAeSKWSQBU+4jYmpGYU6ucr7vqemXHPFDnW4c0/lnBpPKAzb/gHw5fkq0AHZEHgIug7FW/ZA== X-Received: by 2002:ae9:e513:: with SMTP id w19mr30725914qkf.231.1618261880446; Mon, 12 Apr 2021 14:11:20 -0700 (PDT) Received: from localhost.localdomain ([177.194.41.149]) by smtp.googlemail.com with ESMTPSA id i22sm3073392qko.135.2021.04.12.14.11.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Apr 2021 14:11:20 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 03/11] misc: syslog: Fix indentation and style Date: Mon, 12 Apr 2021 18:11:05 -0300 Message-Id: <20210412211113.393120-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210412211113.393120-1-adhemerval.zanella@linaro.org> References: <20210412211113.393120-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" No semantic changes. --- misc/syslog.c | 510 +++++++++++++++++++++++++------------------------- 1 file changed, 253 insertions(+), 257 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index 2cc63ef287..2ec986811d 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -27,10 +27,6 @@ * SUCH DAMAGE. */ -#if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94"; -#endif /* LIBC_SCCS and not lint */ - #include #include #include @@ -60,20 +56,19 @@ 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 int 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 LogType = SOCK_DGRAM; /* Type of socket connection */ +static int LogFile = -1; /* fd for log */ +static int 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. */ /* 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); #ifndef NO_SIGPIPE static void sigpipe_handler (int); #endif @@ -88,8 +83,7 @@ struct cleanup_arg struct sigaction *oldaction; }; -static void -cancel_handler (void *ptr) +static void cancel_handler (void *ptr) { /* Restore the old signal handler. */ struct cleanup_arg *clarg = (struct cleanup_arg *) ptr; @@ -98,7 +92,7 @@ cancel_handler (void *ptr) { #ifndef NO_SIGPIPE if (clarg->oldaction != NULL) - __sigaction (SIGPIPE, clarg->oldaction, NULL); + __sigaction (SIGPIPE, clarg->oldaction, NULL); #endif /* Free the memstream buffer, */ @@ -112,276 +106,278 @@ cancel_handler (void *ptr) /* * syslog, vsyslog -- - * print message on log file; output is intended for syslogd(8). + * 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; + struct tm now_tm; + time_t now; + int fd; + FILE *f; + char *buf = 0; + size_t bufsize = 0; + size_t msgoff; #ifndef NO_SIGPIPE - struct sigaction action, oldaction; - int sigpipe; + struct sigaction action, oldaction; + int sigpipe; #endif - 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; - } - - /* writev is a cancellation point. */ - (void)__writev(STDERR_FILENO, iov, v - iov + 1); - } + 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; + } + + /* writev is a cancellation point. */ + (void) __writev (STDERR_FILENO, iov, v - iov + 1); + } #ifndef NO_SIGPIPE - /* Prepare for a broken connection. */ - memset (&action, 0, sizeof (action)); - action.sa_handler = sigpipe_handler; - sigemptyset (&action.sa_mask); - sigpipe = __sigaction (SIGPIPE, &action, &oldaction); - if (sigpipe == 0) - clarg.oldaction = &oldaction; + /* Prepare for a broken connection. */ + memset (&action, 0, sizeof (action)); + action.sa_handler = sigpipe_handler; + sigemptyset (&action.sa_mask); + sigpipe = __sigaction (SIGPIPE, &action, &oldaction); + if (sigpipe == 0) + clarg.oldaction = &oldaction; #endif - /* Get connected, output the message to the local logger. */ - if (!connected) - openlog_internal(LogTag, LogStat | LOG_NDELAY, 0); - - /* 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, send_flags) < 0) - { - if (connected) - { - /* Try to reopen the syslog connection. Maybe it went - down. */ - closelog_internal (); - openlog_internal(LogTag, LogStat | LOG_NDELAY, 0); - } - - if (!connected || __send(LogFile, buf, bufsize, send_flags) < 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, 0)) >= 0) - { - __dprintf (fd, "%s\r\n", buf + msgoff); - (void)__close(fd); - } - } - } + /* Get connected, output the message to the local logger. */ + if (!connected) + openlog_internal (LogTag, LogStat | LOG_NDELAY, 0); + + /* 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, send_flags) < 0) + { + if (connected) + { + /* Try to reopen the syslog connection. Maybe it went down. */ + closelog_internal (); + openlog_internal (LogTag, LogStat | LOG_NDELAY, 0); + } + + if (!connected || __send (LogFile, buf, bufsize, send_flags) < 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, 0)) >= 0) + { + __dprintf (fd, "%s\r\n", buf + msgoff); + (void) __close (fd); + } + } + } #ifndef NO_SIGPIPE - if (sigpipe == 0) - __sigaction (SIGPIPE, &oldaction, (struct sigaction *) NULL); + if (sigpipe == 0) + __sigaction (SIGPIPE, &oldaction, (struct sigaction *) NULL); #endif - out: - /* End of critical section. */ - __libc_cleanup_pop (0); - __libc_lock_unlock (syslog_lock); +out: + /* End of critical section. */ + __libc_cleanup_pop (0); + __libc_lock_unlock (syslog_lock); - if (buf != failbuf) - free (buf); + if (buf != failbuf) + free (buf); } -static struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */ - +static struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */ 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 != 0 && (logfac &~ LOG_FACMASK) == 0) - LogFacility = logfac; - - int retry = 0; - while (retry < 2) { - if (LogFile == -1) { - SyslogAddr.sun_family = AF_UNIX; - (void)strncpy(SyslogAddr.sun_path, _PATH_LOG, - sizeof(SyslogAddr.sun_path)); - 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; - (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 = 1; - } - break; - } + if (ident != NULL) + LogTag = ident; + LogStat = logstat; + if (logfac != 0 && (logfac & ~LOG_FACMASK) == 0) + LogFacility = logfac; + + int retry = 0; + while (retry < 2) + { + if (LogFile == -1) + { + SyslogAddr.sun_family = AF_UNIX; + (void) strncpy (SyslogAddr.sun_path, _PATH_LOG, + sizeof (SyslogAddr.sun_path)); + 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; + (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 = 1; + } + break; + } } void @@ -430,20 +426,20 @@ closelog (void) __libc_cleanup_pop (1); } -/* setlogmask -- set the log mask level */ +/* setlogmask -- set the log mask level */ 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 Apr 12 21:11:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 42952 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 55F353982069; Mon, 12 Apr 2021 21:11:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 55F353982069 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1618261885; bh=kGVjouPQgRzhamIWPBkY09HJMMVL+bBskLBdnWDdU9o=; 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=nqB6PEs4OEuZbcFyCP8yDV9fW8mqZpcdTpYFmFxxGL9Ag2d7k0TIpE/xbOhJ4W4N0 XjoH1CxeOIpud638sfTMFzsUw0x27861xTli6KTOJMjoBQUSa1/790MBI8BxlB3yz7 yx5JZIMYdqNQTCZCpzqU7UUthghp2/xo5i+Q67wk= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x72b.google.com (mail-qk1-x72b.google.com [IPv6:2607:f8b0:4864:20::72b]) by sourceware.org (Postfix) with ESMTPS id 18B8A3939C06 for ; Mon, 12 Apr 2021 21:11:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 18B8A3939C06 Received: by mail-qk1-x72b.google.com with SMTP id o5so15858078qkb.0 for ; Mon, 12 Apr 2021 14:11:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=kGVjouPQgRzhamIWPBkY09HJMMVL+bBskLBdnWDdU9o=; b=WRQZQj62apbZxfZ8gXHhoxRuIN5swuJ16XOUcv8RyOy3koyZr/+d9jmyNIANpz0JLw Yhx9jXFNWR9Qk98t8mPWdcIQoo8xYGcDoS63rVI8UBz93QZNASvM2lSBJSbz4X42u/DE ZSbuVWtWnsMpKy6Vr2u9UQfEtIqaZsGJ1ADAUFA2kdL9DOskKPSrBm8ytQhVvfZutskv 8wf0vgU0bd3s5s0qOXjJspzmOn2qMjrEbvdTrLDxAwtwRlOJOsH2Pwh01mrj8lHfXuwz /f5qPJp6AymXJod4FX3yJMzjp+FhSm41KkeMwNj5wviSRIlegTCJigqbSpoh/43zbP7h eFcA== X-Gm-Message-State: AOAM530rnrL6/WOoiSpptZjYFq4+EFS31NJSIpVhodfAfQNEqtH57Tua 38upjAKOIJLoogfcTrnN1iSogbFR6QCca1I6 X-Google-Smtp-Source: ABdhPJya63SSgPXyZ3DX4HuBZxUWKB4MCd3mFltdJtZt1gGCyXkDE0J19sXwe6AzHPjEPYhIx3cObA== X-Received: by 2002:a05:620a:ec4:: with SMTP id x4mr7489735qkm.372.1618261881537; Mon, 12 Apr 2021 14:11:21 -0700 (PDT) Received: from localhost.localdomain ([177.194.41.149]) by smtp.googlemail.com with ESMTPSA id i22sm3073392qko.135.2021.04.12.14.11.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Apr 2021 14:11:21 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 04/11] misc: syslog: Use bool for connected Date: Mon, 12 Apr 2021 18:11:06 -0300 Message-Id: <20210412211113.393120-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210412211113.393120-1-adhemerval.zanella@linaro.org> References: <20210412211113.393120-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Checked on x86_64-linux-gnu. --- misc/syslog.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index 2ec986811d..668b4c852b 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -58,7 +58,7 @@ static int LogType = SOCK_DGRAM; /* Type of socket connection */ static int LogFile = -1; /* fd for log */ -static int connected; /* Have done connect */ +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 */ @@ -374,7 +374,7 @@ openlog_internal (const char *ident, int logstat, int logfac) } } else - connected = 1; + connected = true; } break; } @@ -408,7 +408,7 @@ closelog_internal (void) __close (LogFile); LogFile = -1; - connected = 0; + connected = false; } void From patchwork Mon Apr 12 21:11:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 42954 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 99A3C3982074; Mon, 12 Apr 2021 21:11:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 99A3C3982074 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1618261887; bh=yw5FDOmuRuD+XjRDcZYDzjXThdDqXEwO9nrTNWH1DkA=; 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=Y3d+gWO0t8U72XIFfVH5QUqmoSNmjXnYlgCiVVny74VdK9lO9kBID1OBTKQ9DLzgA AchyDeju7iNIg7AZ6tE8srhLlJVway23rtmxQkQkyO/LSVrjKuEP7OgkpdwSd7ewNV Etx/53OoyqTP91pwricYesaFq3A/wd0SlYDJGfoo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qv1-xf2c.google.com (mail-qv1-xf2c.google.com [IPv6:2607:f8b0:4864:20::f2c]) by sourceware.org (Postfix) with ESMTPS id 38110385480B for ; Mon, 12 Apr 2021 21:11:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 38110385480B Received: by mail-qv1-xf2c.google.com with SMTP id 30so7045289qva.9 for ; Mon, 12 Apr 2021 14:11:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yw5FDOmuRuD+XjRDcZYDzjXThdDqXEwO9nrTNWH1DkA=; b=PYqhgCJrrh/PoptwZHhBftJ40A9+NcXfVz5Msu9Gd1piksnKBrzIXynmZ+UDN19xG6 ZmVuUyMWo/De0P2EP8tKkqFTiGT+sclNbvu3T1fo3yEa2X+cSAAJpAUDLn3SGru7TUMt /25pdDwpduPmnCxF07QvG6IyNXqTpH4ygSFl0ArNURGO6HYaN46O4Vf5k2GC9JfsYZ/K lwUuOf4P58fMfq5ROfjfARpRDGL7al6fUEARyqFyMvU6Q+qfT6alhx0OFRdKFyyVZwd/ yG4fv0SMweyoz4RKxgG52ruOWBpTFOOkODwhf2iV4ztiJxGRwbnLEYNG886PqmHRbsJm zMlA== X-Gm-Message-State: AOAM530rVmgkRbSPW4waxOAv45w04oBCoBTAaM0fKtadiyIyPjrarWeZ QFyW11UynZdiBc0OreOOAnWt2lWk+SKtDM3J X-Google-Smtp-Source: ABdhPJzXSRt7wpXxpTSiRZ/XnS7+HUJZkjQC2TyIXeHkUSRKoCvoET9Mt3vGaLBDnVe+74/+mt1c6A== X-Received: by 2002:a05:6214:485:: with SMTP id ay5mr29879563qvb.41.1618261882634; Mon, 12 Apr 2021 14:11:22 -0700 (PDT) Received: from localhost.localdomain ([177.194.41.149]) by smtp.googlemail.com with ESMTPSA id i22sm3073392qko.135.2021.04.12.14.11.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Apr 2021 14:11:22 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 05/11] misc: syslog: Assume MSG_NOSIGNAL support (BZ #17144) Date: Mon, 12 Apr 2021 18:11:07 -0300 Message-Id: <20210412211113.393120-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210412211113.393120-1-adhemerval.zanella@linaro.org> References: <20210412211113.393120-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" MSG_NOSIGNAL was added on POSIX 2008 and Hurd seems to support it. The SIGPIPE handling also makes the implementation not thread-safe (due the sigaction usage). Checked on x86_64-linux-gnu. --- misc/syslog.c | 49 +++----------------------------- sysdeps/unix/sysv/linux/syslog.c | 4 --- 2 files changed, 4 insertions(+), 49 deletions(-) delete mode 100644 sysdeps/unix/sysv/linux/syslog.c diff --git a/misc/syslog.c b/misc/syslog.c index 668b4c852b..a3e458f54a 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -69,13 +69,6 @@ extern char *__progname; /* Program name, from crt0. */ __libc_lock_define_initialized (static, syslog_lock) static void openlog_internal (const char *, int, int); static void closelog_internal (void); -#ifndef NO_SIGPIPE -static void sigpipe_handler (int); -#endif - -#ifndef send_flags -# define send_flags 0 -#endif struct cleanup_arg { @@ -89,15 +82,8 @@ static void cancel_handler (void *ptr) struct cleanup_arg *clarg = (struct cleanup_arg *) ptr; if (clarg != NULL) - { -#ifndef NO_SIGPIPE - if (clarg->oldaction != NULL) - __sigaction (SIGPIPE, clarg->oldaction, NULL); -#endif - - /* Free the memstream buffer, */ - free (clarg->buf); - } + /* Free the memstream buffer, */ + free (clarg->buf); /* Free the lock. */ __libc_lock_unlock (syslog_lock); @@ -154,10 +140,6 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, char *buf = 0; size_t bufsize = 0; size_t msgoff; -#ifndef NO_SIGPIPE - struct sigaction action, oldaction; - int sigpipe; -#endif int saved_errno = errno; char failbuf[3 * sizeof (pid_t) + sizeof "out of memory []"]; @@ -268,16 +250,6 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, (void) __writev (STDERR_FILENO, iov, v - iov + 1); } -#ifndef NO_SIGPIPE - /* Prepare for a broken connection. */ - memset (&action, 0, sizeof (action)); - action.sa_handler = sigpipe_handler; - sigemptyset (&action.sa_mask); - sigpipe = __sigaction (SIGPIPE, &action, &oldaction); - if (sigpipe == 0) - clarg.oldaction = &oldaction; -#endif - /* Get connected, output the message to the local logger. */ if (!connected) openlog_internal (LogTag, LogStat | LOG_NDELAY, 0); @@ -287,7 +259,7 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, if (LogType == SOCK_STREAM) ++bufsize; - if (!connected || __send (LogFile, buf, bufsize, send_flags) < 0) + if (!connected || __send (LogFile, buf, bufsize, MSG_NOSIGNAL) < 0) { if (connected) { @@ -296,7 +268,7 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, openlog_internal (LogTag, LogStat | LOG_NDELAY, 0); } - if (!connected || __send (LogFile, buf, bufsize, send_flags) < 0) + if (!connected || __send (LogFile, buf, bufsize, MSG_NOSIGNAL) < 0) { closelog_internal (); /* attempt re-open next time */ /* @@ -314,11 +286,6 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, } } -#ifndef NO_SIGPIPE - if (sigpipe == 0) - __sigaction (SIGPIPE, &oldaction, (struct sigaction *) NULL); -#endif - out: /* End of critical section. */ __libc_cleanup_pop (0); @@ -392,14 +359,6 @@ openlog (const char *ident, int logstat, int logfac) __libc_cleanup_pop (1); } -#ifndef NO_SIGPIPE -static void -sigpipe_handler (int signo) -{ - closelog_internal (); -} -#endif - static void closelog_internal (void) { diff --git a/sysdeps/unix/sysv/linux/syslog.c b/sysdeps/unix/sysv/linux/syslog.c deleted file mode 100644 index cf8ce23385..0000000000 --- a/sysdeps/unix/sysv/linux/syslog.c +++ /dev/null @@ -1,4 +0,0 @@ -#define NO_SIGPIPE -#define send_flags MSG_NOSIGNAL - -#include From patchwork Mon Apr 12 21:11:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 42955 X-Patchwork-Delegate: carlos@redhat.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3F68E3982075; Mon, 12 Apr 2021 21:11:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3F68E3982075 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1618261888; bh=WousP/OKZXOEPfm3hDGNiQRsptrnyfg4vnD2f0+5VXw=; 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=Py0nERwyxx8+acmPfoGElrUYKsKR35hLANIO9UXTyLdTN1b+Ve7IM6AWCYL4MzwGW 3mYK4P28qmbvKP1pSjv/54gghmGTOHq6mRZi3V4In+YptZEMQ1oMfZH08jvlntJi3/ Wjm6/o1wApuXggDnDEVsRtwJKh2gvh0rmmz5J6s0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qv1-xf31.google.com (mail-qv1-xf31.google.com [IPv6:2607:f8b0:4864:20::f31]) by sourceware.org (Postfix) with ESMTPS id 702463982051 for ; Mon, 12 Apr 2021 21:11:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 702463982051 Received: by mail-qv1-xf31.google.com with SMTP id i3so1518987qvj.7 for ; Mon, 12 Apr 2021 14:11:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WousP/OKZXOEPfm3hDGNiQRsptrnyfg4vnD2f0+5VXw=; b=COj+zh48zBHlAHwUWhFnK29nlvHFXg36dsXThxz0+0YTz4wIwO0jfgGKqpN7aSHepz BiJfw9YFHYxvKjt9mwzXHDu4QmoHNQJUfh2u/RSn9fncD4Xgt6I4QKpdLc51WbQCm4f1 bj+DaLeklR65axlJ4I68FvgVNhMmU7wltwq2JUOGhjTxL+Km+z1tik9kNi9/vBEQd9QX rq4w1xFPgL0r8tt29sMMe8NJC/GcAq6rDExso67TNt6tHP70yYbiApyhgyDtGGQhofJJ wGJ8ec1OgO2aG2mRAJ0lGBP71k6O6TaW5P8vXn8gDARLIgEYkcnkyfO/6XfvVN6S/E0C ghSw== X-Gm-Message-State: AOAM533dKqsZtt2ZMqei0clXFk9vGYhIEAG7+xzQZh+8LQxD3ugWoQED JwHitvwT+9lDJu0i4zz6lCjG/Cy95c8SM2WQ X-Google-Smtp-Source: ABdhPJzqhH3ssnELTshlVY3GrzSvmMNMZkdsfU8Elc1JY2zZqC92Lj1UWj6QGEnNyalmTGEF6/9WCw== X-Received: by 2002:a0c:ef09:: with SMTP id t9mr29849148qvr.21.1618261883747; Mon, 12 Apr 2021 14:11:23 -0700 (PDT) Received: from localhost.localdomain ([177.194.41.149]) by smtp.googlemail.com with ESMTPSA id i22sm3073392qko.135.2021.04.12.14.11.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Apr 2021 14:11:23 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 06/11] misc: syslog: Simplify implementation Date: Mon, 12 Apr 2021 18:11:08 -0300 Message-Id: <20210412211113.393120-6-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210412211113.393120-1-adhemerval.zanella@linaro.org> References: <20210412211113.393120-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@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 | 138 ++++++++++++++------------------------------------ 1 file changed, 38 insertions(+), 100 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index a3e458f54a..cb608766cb 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -27,34 +27,16 @@ * SUCH DAMAGE. */ -#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 */ @@ -133,13 +115,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 []"]; @@ -153,9 +132,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); @@ -169,86 +146,46 @@ __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), + + /* "%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); - 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); - } + 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); - - /* 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; } + 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. */ - (void) __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) @@ -277,11 +214,12 @@ __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, 0)) >= 0) { __dprintf (fd, "%s\r\n", buf + msgoff); - (void) __close (fd); + __close (fd); } } } @@ -312,8 +250,8 @@ openlog_internal (const char *ident, int logstat, int logfac) if (LogFile == -1) { SyslogAddr.sun_family = AF_UNIX; - (void) strncpy (SyslogAddr.sun_path, _PATH_LOG, - sizeof (SyslogAddr.sun_path)); + strncpy (SyslogAddr.sun_path, _PATH_LOG, + sizeof (SyslogAddr.sun_path)); if (LogStat & LOG_NDELAY) { LogFile = __socket (AF_UNIX, LogType | SOCK_CLOEXEC, 0); @@ -329,7 +267,7 @@ openlog_internal (const char *ident, int logstat, int logfac) int saved_errno = errno; int fd = LogFile; LogFile = -1; - (void) __close (fd); + __close (fd); __set_errno (old_errno); if (saved_errno == EPROTOTYPE) { From patchwork Mon Apr 12 21:11:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 42956 X-Patchwork-Delegate: carlos@redhat.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C7C643982071; Mon, 12 Apr 2021 21:11:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C7C643982071 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1618261888; bh=SiKv0rwct10uA7A0aHblOgYcigPZjTc/4xTxpuJD9PU=; 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=Px9CHdACv8dT4+/sOUL47xtEpbPtdZ330/VCxAKK58vHyAuj6a7Bj+pT2PJQWA03H WobBMudlgjmtusQ7Bj6Yxhc5zuLLmM+nU0rzd0x+39X220Oohww35esPWz57lXI3uL hfVcRs3DQ2e0uDlYjuDJSgv1XR13BxRox4ge4CF0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qv1-xf35.google.com (mail-qv1-xf35.google.com [IPv6:2607:f8b0:4864:20::f35]) by sourceware.org (Postfix) with ESMTPS id 7C73C398206C for ; Mon, 12 Apr 2021 21:11:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 7C73C398206C Received: by mail-qv1-xf35.google.com with SMTP id iu14so7062417qvb.4 for ; Mon, 12 Apr 2021 14:11:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SiKv0rwct10uA7A0aHblOgYcigPZjTc/4xTxpuJD9PU=; b=IHrzD308yVSaSwv8GjJT0nYggDKWcalGZSoU2Miu7mHjXSmNC4yTaJhVKSTHUDIrHe gLZegD2c2NK+hbYROW3NT64k1bLrjUz9Hpba+x4yFULBGr9e7FbkvQ32byLSLyvv0DgM Ish668lfQACos3tH5jx8oNdDoF00mhxPZuWd68nPQCW9nGHDZMXMvLmMnQHgSwO3f/sS iAcEQmxQ2LR04JvGfeNmRycQxmJXlpYPFP8Cuf4NX5RO8BpNyXGkHNDeu22+K3oDa+Pt Ve9u3ZPHKKzUbKDaCmP7bzhcS70xJpy57ygD6eeX9CtOsKEQv/KyYT35HxPaql2z0j9H 2Qlw== X-Gm-Message-State: AOAM533LXoyCF/fkfC8+HkKTaG+xZZdBdyckD/P2pVRN6XLygx92THWM 9pOYilju02WZ11gT9wPFy05oeMbYgk4FnHRB X-Google-Smtp-Source: ABdhPJy9HlwZUC9Lv/LzisPfhFb278nI53opnTmlHHEJmjWXktcEM2Yc1FzTc11rBuQK3422C0DucA== X-Received: by 2002:a05:6214:8c4:: with SMTP id da4mr30259780qvb.57.1618261884845; Mon, 12 Apr 2021 14:11:24 -0700 (PDT) Received: from localhost.localdomain ([177.194.41.149]) by smtp.googlemail.com with ESMTPSA id i22sm3073392qko.135.2021.04.12.14.11.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Apr 2021 14:11:24 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 07/11] misc: syslog: Use static buffer Date: Mon, 12 Apr 2021 18:11:09 -0300 Message-Id: <20210412211113.393120-7-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210412211113.393120-1-adhemerval.zanella@linaro.org> References: <20210412211113.393120-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@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 | 102 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 68 insertions(+), 34 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index cb608766cb..a310eba476 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -115,12 +115,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_to_free = 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. */ @@ -144,42 +143,77 @@ __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); - __vfprintf_internal (f, fmt, ap, mode_flags); - fclose (f); + int vl = __vsnprintf_internal (bufs + l, sizeof (bufs) - l, fmt, apc, + mode_flags); + if (l + vl < sizeof (bufs)) + { + buf = bufs; + bufsize = l + vl; + } - /* 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_to_free = true; + clarg.buf = buf; + } + else + { + bufsize = __snprintf (bufs, sizeof bufs, + "out of memory[%d]", __getpid ()); + buf = bufs; + } } /* Output to stderr if requested. */ @@ -229,7 +263,7 @@ out: __libc_cleanup_pop (0); __libc_lock_unlock (syslog_lock); - if (buf != failbuf) + if (buf_to_free) free (buf); } From patchwork Mon Apr 12 21:11:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 42957 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 C3C3A3982070; Mon, 12 Apr 2021 21:11:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C3C3A3982070 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1618261889; bh=l7QPyDUtKNP+6vKbXjXkZSpRQKxutYSrS7+rqd3l8ow=; 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=KqbNpT4l547xI2SlYfVfd62UT4KfwX8MWDgMA5dK5yobojcObvJUamrTTC1SYCQaf NvBAXcgGObHSvPOLsXaoBYM/Q8E5OnUQ50fXi/M5AdgbR4XvoHq/7syY+8c7t11vM7 sFPQsBqqKXePVZBcU4e+zAeiNsFaPKjfm7Ts+mHE= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x732.google.com (mail-qk1-x732.google.com [IPv6:2607:f8b0:4864:20::732]) by sourceware.org (Postfix) with ESMTPS id 7A404398206F for ; Mon, 12 Apr 2021 21:11:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 7A404398206F Received: by mail-qk1-x732.google.com with SMTP id o5so15858271qkb.0 for ; Mon, 12 Apr 2021 14:11:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=l7QPyDUtKNP+6vKbXjXkZSpRQKxutYSrS7+rqd3l8ow=; b=VZnekSrqx3Yz8d13v3hvn/iRmwCm5/1HpFpTSJadOYq4aL1NlJkq3iUIhkQRNDrKX+ ZI/3z7DF+uBd0urN3ijD87Uux7pOKEz99x5yoOnN7jM6LmwiuD//xyxWIKM5UqH8eNdv a+QgCsn8NZIQNUPLDQqmouBUvpSdYxul4OyNmqJhsQBpEe6BQWZVnoohblGNm9xQ9+ga sr8EhhPC/Fz+6gZEuPLbS1E7L8JdkJtZxi98cKnouObuXL/YR8euKLIv08ymLYAh0/FW cKts6uKbfM8UqGaxSi9LiaGrMap/0eU5FwZFGQLPvWYNgV4tsGKl+RF8u7XlIGjF34XL TbDw== X-Gm-Message-State: AOAM5320plT42bpwePtF9r8ukHrzsyVGvygbAXODec0d0ZULLjNxjquv ShSrokN4j8Paxp8i7mnEE/JT6VLADjVp1vNI X-Google-Smtp-Source: ABdhPJw3LzojSAZJ1NDZ9ExDwRazm16vfIZ7uE1ZYJA9wLJOnoC7kZryuxsEsxqU7e5sOHdrmIWoTg== X-Received: by 2002:a37:6149:: with SMTP id v70mr6877293qkb.303.1618261885912; Mon, 12 Apr 2021 14:11:25 -0700 (PDT) Received: from localhost.localdomain ([177.194.41.149]) by smtp.googlemail.com with ESMTPSA id i22sm3073392qko.135.2021.04.12.14.11.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Apr 2021 14:11:25 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 08/11] misc: syslog: Use CLOC_EXEC with _PATH_CONSOLE (BZ #17145) Date: Mon, 12 Apr 2021 18:11:10 -0300 Message-Id: <20210412211113.393120-8-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210412211113.393120-1-adhemerval.zanella@linaro.org> References: <20210412211113.393120-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" The syslog open the '/dev/console' for LOG_CONS without O_CLOEXEC, which might leak in multithread programs that call fork. Checked on x86_64-linux-gnu. --- misc/syslog.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/misc/syslog.c b/misc/syslog.c index a310eba476..b595889e1c 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -250,7 +250,8 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, */ int fd; if (LogStat & LOG_CONS && - (fd = __open (_PATH_CONSOLE, O_WRONLY | O_NOCTTY, 0)) >= 0) + (fd = __open (_PATH_CONSOLE, O_WRONLY | O_NOCTTY | O_CLOEXEC, + 0)) >= 0) { __dprintf (fd, "%s\r\n", buf + msgoff); __close (fd); From patchwork Mon Apr 12 21:11:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 42958 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 584DE39DC4D2; Mon, 12 Apr 2021 21:11:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 584DE39DC4D2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1618261890; bh=4QqBw38AcgCQ7cmsBDPrfrgrBeEseJrx7WElTLV34Bg=; 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=LwIguTnneJ/IskSrSwPTkh/3t6HY9lOq0oQj+6AyzYsamTtETlKwC8pboWbeTLS6g TvJnkcoMnsTERx21JR9k3zhfh9r4QcneHzxyRG+Fw4amqtj5xEDw2kGba/HQBkkJki bTCdzuFouO5Yw01rKzFcvMfvcJoHi1ba4JrY0Rzc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x72e.google.com (mail-qk1-x72e.google.com [IPv6:2607:f8b0:4864:20::72e]) by sourceware.org (Postfix) with ESMTPS id 834163982073 for ; Mon, 12 Apr 2021 21:11:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 834163982073 Received: by mail-qk1-x72e.google.com with SMTP id c123so10975774qke.1 for ; Mon, 12 Apr 2021 14:11:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4QqBw38AcgCQ7cmsBDPrfrgrBeEseJrx7WElTLV34Bg=; b=X9Wstt+gA2a6e/QLtRiSNekVBf0yxAa/a7ph7566zSug/4ITz/k29LUFAbGft56W8Y f0WZpzq8baIb/2esiUS76qlMg6HZt1d8Ul2vq6po2kpIeHTSMcnFkLQje+nbT2HH3P3o QqMTYy1HHAaORQjpyH6hi5xPlxzeJs5rHViY1Ou+QOnh4c7bwCLQHiQc949m/1cSyIWa hiDgTp2trUI1fGt6JJlumNwPun5ha6ibCCEzUIIRbxu6uRuVjJe81HZxKLDe4fp2Ipha jbo75cy87Q6kSffQVOCiTyarXSvdf2TCGzekQ+o1jeIBHzKArFys3Oh8b6XF9OrV1kNI zDCA== X-Gm-Message-State: AOAM531lJ8sxS+ETPlURitlReQpmMmT9IJH9p4cbHflRNetVvqi3DvV/ JO9jRObzsu9BqDJur8B52OjIzLADt6JJVpj2 X-Google-Smtp-Source: ABdhPJye91GXWwtmDvrU4OKTg4Rjy8Bx7NLOTHjfsjXAYcaU1EgmyHGVwdu9SIJcCKr2EGyd4AufYQ== X-Received: by 2002:a37:8bc5:: with SMTP id n188mr28988908qkd.305.1618261887013; Mon, 12 Apr 2021 14:11:27 -0700 (PDT) Received: from localhost.localdomain ([177.194.41.149]) by smtp.googlemail.com with ESMTPSA id i22sm3073392qko.135.2021.04.12.14.11.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Apr 2021 14:11:26 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 09/11] misc: syslog: Use static const for AF_UNIX address Date: Mon, 12 Apr 2021 18:11:11 -0300 Message-Id: <20210412211113.393120-9-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210412211113.393120-1-adhemerval.zanella@linaro.org> References: <20210412211113.393120-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Checked on x86_64-linux-gnu. --- misc/syslog.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index b595889e1c..be12bcc91d 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -268,7 +268,12 @@ out: free (buf); } -static struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */ +/* AF_UNIX address of local logger */ +static const struct sockaddr_un SyslogAddr = + { + .sun_family = AF_UNIX, + .sun_path = _PATH_LOG + }; static void openlog_internal (const char *ident, int logstat, int logfac) @@ -284,9 +289,6 @@ openlog_internal (const char *ident, int logstat, int logfac) { if (LogFile == -1) { - SyslogAddr.sun_family = AF_UNIX; - strncpy (SyslogAddr.sun_path, _PATH_LOG, - sizeof (SyslogAddr.sun_path)); if (LogStat & LOG_NDELAY) { LogFile = __socket (AF_UNIX, LogType | SOCK_CLOEXEC, 0); From patchwork Mon Apr 12 21:11:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 42959 X-Patchwork-Delegate: carlos@redhat.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E20E73A07014; Mon, 12 Apr 2021 21:11:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E20E73A07014 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1618261891; bh=xG/BwDLS0vr/gjRM6mfHPTM8qGoJ0qZGRQRQvUTpUOc=; 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=gGoocEhWZh/Sy0XsIp/EiyUFAjxLJCZFtRwUel7YMZi53iVdxDYXkOCfZswAQNoVf 5syGR7/rbf+X6oF63Ac7BH2WWY9WCZUqtDSlhVSWi5Tqu1ZXVVwFLZEX6oWuSPQhWa EC/rRL2btttqo8vedYs5/rYAcYElnjs8OlwuKz98= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x82f.google.com (mail-qt1-x82f.google.com [IPv6:2607:f8b0:4864:20::82f]) by sourceware.org (Postfix) with ESMTPS id 9B7083982070 for ; Mon, 12 Apr 2021 21:11:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 9B7083982070 Received: by mail-qt1-x82f.google.com with SMTP id c6so11119014qtc.1 for ; Mon, 12 Apr 2021 14:11:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xG/BwDLS0vr/gjRM6mfHPTM8qGoJ0qZGRQRQvUTpUOc=; b=YlwA4Gxn/kz6IJYmhpS9yJRK3hVemoyhHe2csmVpT5JK0WiSIORrq0kpZp5Yv8cn4n Q+V4ivH2FSBOAMINinnWLfIlDuWGP+JCx6AZzIAuEK/WBc9xTE2YfOzkHkgEXYDYCblm E5ksixr9/IoLi+woewc42iFWIai/Rp+OJl9Iwovudamx4FIkAezYmtb2kymazdvqw3T9 Hux3E2VfqYWF5y1YOE/BFVvX+5ga4wm1kp2oH40KLfd3BHQcS2aLdjcDJhtSCkkUjSC7 zujHOcOt6vK+DUVpLJ2XdHVDflWfHnmrTeENzhZEd9ja8Um09Fdt5X4oEyTR15pxCLbS C3FQ== X-Gm-Message-State: AOAM533i+FV80sPijXr1jA00om6L+bvTntrR8I8STXfwA5gAR9QVb9Ap Ua7Zoru4SKixF7f+2tlApjXAz4ArwCEbOzhX X-Google-Smtp-Source: ABdhPJx6iI1WN3yQksDGOki3rpSXyyWMG1TCMOcUGAQNGQotVikWqO9ZouyJevtqttr4ayQrz+KHEQ== X-Received: by 2002:ac8:7747:: with SMTP id g7mr27165310qtu.144.1618261888092; Mon, 12 Apr 2021 14:11:28 -0700 (PDT) Received: from localhost.localdomain ([177.194.41.149]) by smtp.googlemail.com with ESMTPSA id i22sm3073392qko.135.2021.04.12.14.11.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Apr 2021 14:11:27 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 10/11] misc: syslog: Move SYSLOG_NAME to USE_MISC (BZ #16355) Date: Mon, 12 Apr 2021 18:11:12 -0300 Message-Id: <20210412211113.393120-10-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210412211113.393120-1-adhemerval.zanella@linaro.org> References: <20210412211113.393120-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@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 Mon Apr 12 21:11:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 42960 X-Patchwork-Delegate: carlos@redhat.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 02A6C39DC4C6; Mon, 12 Apr 2021 21:11:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 02A6C39DC4C6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1618261893; bh=GTyTdClYGgoYL4dUhJYcSn7zDsigjrLX+KxINQufVZM=; 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=UnTWOJP63lCqRunHXqev6b8nQLFzTZfW0jvzYz+0sq7AtvwGmkbFY06Py1EZXrvJP aDW+Jih2y9J15rtP7B80CcsvdhgSDMVSM8NVGvo1p6BQ+z0BNgOF3XJILahxjPxyEY NkRhb5lEliNyexloszabKLe90mgyLdETOQJQUTWs= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x72f.google.com (mail-qk1-x72f.google.com [IPv6:2607:f8b0:4864:20::72f]) by sourceware.org (Postfix) with ESMTPS id D0BD6398207F for ; Mon, 12 Apr 2021 21:11:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D0BD6398207F Received: by mail-qk1-x72f.google.com with SMTP id c4so15823360qkg.3 for ; Mon, 12 Apr 2021 14:11:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=GTyTdClYGgoYL4dUhJYcSn7zDsigjrLX+KxINQufVZM=; b=e3LMSRUwSoHMIQBsUHJAVqZYt/6MF5hJGqvhhrHw9FwYTTjS+EwZi1gpua9idvDTPL nShoJjSmXv+27kn4jlly7fLdzwvGdJ/iAU2SRKaltfD9MLCwrEVKsNJvwMS6uXMGISyd yR3fFYwgRqeOtYTbpiZXu/HLPhz3dGEl1SWWKJZK6unOonUXFIvkcDKGhVnDQBlYmOYZ haRCR89EPoEBJCmG9d+iltBCwAN1FpHBp4+cveSdyB2FgyBHXwbsbXzl/4cGffxDWUEQ uCLjEMVbUE8legJZDJmSbI9CSo/ziNiW38WamtLUjfJ+i5L2/N841Ruyw6fsK/smrTg8 xd7w== X-Gm-Message-State: AOAM531WMDNQyy4XftfCYSpOH5Ox2DcqDGoG1INPRCanMqZilONL0Bv1 5vkF1Fl7lu7tjJCSOfLbKBk9Aj4Z3ROk6Oc/ X-Google-Smtp-Source: ABdhPJzCM3gKP0q6wJTdqGfKccH69TdjmTntRdZfwv0fzRVFXOpJY0bVqKst3eKn+jFvLZNWY7ZPsw== X-Received: by 2002:a37:7985:: with SMTP id u127mr29080031qkc.333.1618261889161; Mon, 12 Apr 2021 14:11:29 -0700 (PDT) Received: from localhost.localdomain ([177.194.41.149]) by smtp.googlemail.com with ESMTPSA id i22sm3073392qko.135.2021.04.12.14.11.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Apr 2021 14:11:28 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 11/11] misc: syslog: Use RFC5424 Date: Mon, 12 Apr 2021 18:11:13 -0300 Message-Id: <20210412211113.393120-11-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210412211113.393120-1-adhemerval.zanella@linaro.org> References: <20210412211113.393120-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 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 autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@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 --- include/time.h | 8 ++++++ misc/syslog.c | 69 +++++++++++++++++++++++++++++++---------------- misc/tst-syslog.c | 30 ++++++++++----------- 3 files changed, 68 insertions(+), 39 deletions(-) diff --git a/include/time.h b/include/time.h index caf2af5e74..3defec6c20 100644 --- a/include/time.h +++ b/include/time.h @@ -502,6 +502,14 @@ time_now (void) __clock_gettime (TIME_CLOCK_GETTIME_CLOCKID, &ts); return ts.tv_sec; } + +static inline struct __timespec64 +timespec64_now (void) +{ + struct __timespec64 ts64; + __clock_gettime64 (TIME_CLOCK_GETTIME_CLOCKID, &ts64); + return ts64; +} #endif #endif diff --git a/misc/syslog.c b/misc/syslog.c index be12bcc91d..36cdf037f8 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -38,6 +38,10 @@ #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 */ @@ -111,6 +115,30 @@ __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_t +{ + char b[sizeof ("YYYY-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_t *timebuf) +{ + struct __timespec64 ts = timespec64_now (); + struct tm now_tm; + __gmtime64_r (&ts.tv_sec, &now_tm); + + __snprintf (timebuf->b, sizeof (timebuf->b), + "%04d-%02d-%02dT%02d:%02d:%02d.%06dZ", + now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, + now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec, + (int32_t) ts.tv_nsec / 1000); +} + void __vsyslog_internal (int pri, const char *fmt, va_list ap, unsigned int mode_flags) @@ -143,35 +171,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; - - enum - { - timebuf_size = 3+1 /* "%h " */ - + 2+1 /* "%e " */ - + 2+1 + 2+1 + 2+1 + 1, /* "%T " */ + struct timebuf_t timestamp; + syslog_rfc3339_timestamp (×tamp); - 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; @@ -198,7 +221,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 ed8c528db4..5b24927afe 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)) @@ -218,28 +219,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); + <34>1 2003-10-11T22:14:15.003Z mymachine openlog_ident PID - MSG */ + 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; } @@ -252,8 +250,8 @@ 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", + 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); @@ -390,7 +388,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);