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) {