From patchwork Tue Oct 5 13:56:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 45891 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 42C6C385AC29 for ; Tue, 5 Oct 2021 13:59:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 42C6C385AC29 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1633442393; bh=obu86TjOScFgiJW3Y3pDbfcPLb9YZnfisQx/qy0Yo3k=; 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=maDpm5ZV0pU0mIx4BNLNrp8j3z399UoX2ykHkWMjTqUfP5dwAKaDI0fyWcNlMCf1B liGjPdAD2Lrh2crz1tBbhF+R74t96HRp+30tJg9WiFQBfGEtlYOsfKC4neFJpPGFH7 ZEuI1nyRgmCXyerfhTSIU/c1PEePNtiYXwM+so9Y= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-vs1-xe35.google.com (mail-vs1-xe35.google.com [IPv6:2607:f8b0:4864:20::e35]) by sourceware.org (Postfix) with ESMTPS id C16C33857017 for ; Tue, 5 Oct 2021 13:56:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C16C33857017 Received: by mail-vs1-xe35.google.com with SMTP id o124so23767937vsc.6 for ; Tue, 05 Oct 2021 06:56:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=obu86TjOScFgiJW3Y3pDbfcPLb9YZnfisQx/qy0Yo3k=; b=hymRcRN80Oc4EVBM2MljXgKeaCK6+rFqYPantko4IChm7fPx3Yh7AnRoqkWgvl7iLG 5Nfx/XhXtOZfcRza5SKQdpus1xJlQ7qnDraVrKaXSAZ4c8CVlAR9rZdFszlWU30U3j+P GLAzpBKYs8/ildu/xENZlT2t1AyxmKM1VLC5rmHjZq1bImIZ7JzrO42xmQGbmYAZ95d9 E0CHrOMsn2Um2+NGdN2Sg8NyXdutT7rJ/qrQ50g/HKuNzuEmEIbGIGv8nR/afDKwWiUr 777CT7dB46umuVZyOPrB3rLhNTtb7cJ2vpC8QTAov8SlPU8WXM7OkBREB3MQ3L5VHiar kC3Q== X-Gm-Message-State: AOAM532Ba6n3Vc/oqbZCoXCQFzKDq05wnJdVXaoTP7NJoajf0q6Nk5pu yp58ufxSHWd14KKu53z92LOTFfzTUJXapA== X-Google-Smtp-Source: ABdhPJxQN3h8Xn1p+ynzB6QffIXsF17mKIofgKI9KByGeEahj1bacDDZL3oo9FbIh+6LEQTSkhyshQ== X-Received: by 2002:a67:f48c:: with SMTP id o12mr10118280vsn.22.1633442199170; Tue, 05 Oct 2021 06:56:39 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:807a:9735:fcf:f170:1c20]) by smtp.gmail.com with ESMTPSA id g204sm3204902vkg.1.2021.10.05.06.56.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Oct 2021 06:56:38 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 4/7] misc: syslog: Simplify implementation Date: Tue, 5 Oct 2021 10:56:28 -0300 Message-Id: <20211005135631.3209020-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211005135631.3209020-1-adhemerval.zanella@linaro.org> References: <20211005135631.3209020-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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Remove unrequired includes, use a temporary buffer for strftime instead of using internal libio members, simplify fprintf call on the memstream and memory allocation, use dprintf instead of writev for LOG_PERROR. Checked on x86_64-linux-gnu. --- misc/syslog.c | 126 ++++++++++++++------------------------------------ 1 file changed, 35 insertions(+), 91 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index 34ee5dd655..8365e16128 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -31,34 +31,16 @@ static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94"; #endif /* LIBC_SCCS and not lint */ -#include +#include +#include +#include +#include +#include #include -#include +#include #include #include -#include - -#include -#include -#include -#include #include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include -#include - -#include - -#define ftell(s) _IO_ftell (s) static int LogType = SOCK_DGRAM; /* type of socket connection */ static int LogFile = -1; /* fd for log */ @@ -139,13 +121,10 @@ void __vsyslog_internal(int pri, const char *fmt, va_list ap, unsigned int mode_flags) { - struct tm now_tm; - time_t now; - int fd; FILE *f; char *buf = 0; size_t bufsize = 0; - size_t msgoff; + int msgoff; int saved_errno = errno; char failbuf[3 * sizeof (pid_t) + sizeof "out of memory []"]; @@ -159,9 +138,7 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, /* Prepare for multiple users. We have to take care: most syscalls we are using are cancellation points. */ - struct cleanup_arg clarg; - clarg.buf = NULL; - clarg.oldaction = NULL; + struct cleanup_arg clarg = { NULL, NULL }; __libc_cleanup_push (cancel_handler, &clarg); __libc_lock_lock (syslog_lock); @@ -175,51 +152,24 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, /* Build the message in a memory-buffer stream. */ f = __open_memstream (&buf, &bufsize); - if (f == NULL) - { - /* We cannot get a stream. There is not much we can do but emitting an - error messages. */ - char numbuf[3 * sizeof (pid_t)]; - char *nump; - char *endp = __stpcpy (failbuf, "out of memory ["); - pid_t pid = __getpid (); - - nump = numbuf + sizeof (numbuf); - /* The PID can never be zero. */ - do - *--nump = '0' + pid % 10; - while ((pid /= 10) != 0); - - endp = __mempcpy (endp, nump, (numbuf + sizeof (numbuf)) - nump); - *endp++ = ']'; - *endp = '\0'; - buf = failbuf; - bufsize = endp - failbuf; - msgoff = 0; - } - else + if (f != NULL) { __fsetlocking (f, FSETLOCKING_BYCALLER); - fprintf (f, "<%d>", pri); - now = time_now (); - f->_IO_write_ptr += __strftime_l (f->_IO_write_ptr, - f->_IO_write_end - f->_IO_write_ptr, - "%h %e %T ", - __localtime_r (&now, &now_tm), - _nl_C_locobj_ptr); - msgoff = ftell (f); - if (LogTag == NULL) - LogTag = __progname; - if (LogTag != NULL) - __fputs_unlocked (LogTag, f); - if (LogStat & LOG_PID) - fprintf (f, "[%d]", (int) __getpid ()); - if (LogTag != NULL) - { - __putc_unlocked (':', f); - __putc_unlocked (' ', f); - } - + /* "%h %e %H:%M:%S " */ + char timebuf[3+1 /* "%h " */ + + 2+1 /* "%e " */ + + 2+1 + 2+1 + 2+1 + 1 /* "%T " */]; + time_t now = time_now (); + struct tm now_tm; + __localtime_r (&now, &now_tm); + __strftime_l (timebuf, sizeof (timebuf), "%h %e %T ", &now_tm, + _nl_C_locobj_ptr); + + pid_t pid = LogStat & LOG_PID ? __getpid () : 0; + + fprintf (f, "<%d>%s %n%s%s%.0d%s: ", pri, timebuf, &msgoff, + LogTag == NULL ? __progname : LogTag, + pid != 0 ? "[" : "", pid, pid != 0 ? "]" : ""); /* Restore errno for %m format. */ __set_errno (saved_errno); @@ -233,26 +183,19 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, /* Tell the cancellation handler to free this buffer. */ clarg.buf = buf; } + else + { + /* We cannot get a stream. There is not much we can do but emitting an + error messages. */ + bufsize = __snprintf (failbuf, sizeof failbuf, "out of memory[%d]", + __getpid ()); + buf = failbuf; + } /* Output to stderr if requested. */ if (LogStat & LOG_PERROR) - { - struct iovec iov[2]; - struct iovec *v = iov; - - v->iov_base = buf + msgoff; - v->iov_len = bufsize - msgoff; - /* Append a newline if necessary. */ - if (buf[bufsize - 1] != '\n') - { - ++v; - v->iov_base = (char *) "\n"; - v->iov_len = 1; - } - - /* writev is a cancellation point. */ - __writev (STDERR_FILENO, iov, v - iov + 1); - } + __dprintf (STDERR_FILENO, "%s%s", buf + msgoff, + buf[bufsize - 1] != '\n' ? "\n" : ""); /* Get connected, output the message to the local logger. */ if (!connected) @@ -281,6 +224,7 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, * Make sure the error reported is the one from the * syslogd failure. */ + int fd; if (LogStat & LOG_CONS && (fd = __open (_PATH_CONSOLE, O_WRONLY | O_NOCTTY | O_CLOEXEC, 0))