From patchwork Mon Mar 21 21:19:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 52194 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0B5773840C2C for ; Mon, 21 Mar 2022 21:22:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0B5773840C2C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1647897757; bh=1eFJjA8eHiYgwmfR3QnlJyovmWLwefQR+OIGJFwfp+A=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=bJeSR0s39VQpZNlUA9uSDBlpLtr+JHrjhmrAdgRsSA7KBt/hSyHYLVHBUyMMfk7ar GQsyeJ4fv7qtklVUw/9PAJNilI+D0JuxGMeT9cyR846bolc/W7gxiXlN3dYj65AYd+ 8T5qJRS3VVTrmLaXWLgkGT7h6GBtDInoRLt7Ev3w= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x2e.google.com (mail-oa1-x2e.google.com [IPv6:2001:4860:4864:20::2e]) by sourceware.org (Postfix) with ESMTPS id CEE87389EC65 for ; Mon, 21 Mar 2022 21:19:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CEE87389EC65 Received: by mail-oa1-x2e.google.com with SMTP id 586e51a60fabf-dd9d3e7901so111926fac.8 for ; Mon, 21 Mar 2022 14:19:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1eFJjA8eHiYgwmfR3QnlJyovmWLwefQR+OIGJFwfp+A=; b=VEg1OshKRxaJ4k1DRcgwFcEeyYrfArFHUIhbwTY9YiGPVzUpX4JwvZqH3D9ODLSTiC KeeCVlA5Dnd2kbhjl9kiAC3gR60Egj71O9qsQRdvb2Zil5JMzbKejmSLHzUxQfmEight P1CohxbfB0Uepof1ubUTBsydxKBL2RzZ1JTykhCqMe4FnuqPIKC4qKEC80DDdWGuf01c oXbj/SPDJke8rZmUBjvNaMc9+xFOCorgEEn4nNoTE3k4AeXcSNO18NCTLwB3GCu9EE2m SnNyQFhuD/iv0KZ6UnykFqUtIZLdFs1tNqWKEqRCta7Knwhj6sQWe4KD6RzpHTNhoiir TsxQ== X-Gm-Message-State: AOAM532jVDyh0T3haDUu8JYca4OFDcmupchRwMVQzY6V54swOp4gUDP1 BosCZWl138w4M4tP5eiZ9t6fS7bqzM02Gg== X-Google-Smtp-Source: ABdhPJza1IOlLO8qVeNYA0/6YdPfa8Bf8gfO2BZGEM2cXiCXiHemQcsRE4bhgEe/FzsU3GvKnwH/yw== X-Received: by 2002:a05:6870:4248:b0:dc:c433:8074 with SMTP id v8-20020a056870424800b000dcc4338074mr433596oac.96.1647897564863; Mon, 21 Mar 2022 14:19:24 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:2d55:d5f9:2c96:1f30:1f58]) by smtp.gmail.com with ESMTPSA id c37-20020a9d27a8000000b005b23cf22e23sm7925256otb.42.2022.03.21.14.19.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 14:19:24 -0700 (PDT) To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v5 4/7] misc: syslog: Simplify implementation Date: Mon, 21 Mar 2022 18:19:10 -0300 Message-Id: <20220321211913.2347924-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220321211913.2347924-1-adhemerval.zanella@linaro.org> References: <20220321211913.2347924-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Use a temporary buffer for strftime instead of using internal libio members, simplify fprintf call on the memstream and memory allocation, use %b instead of %h, use dprintf instead of writev for LOG_PERROR. Checked on x86_64-linux-gnu and i686-linux-gnu. --- misc/syslog.c | 95 ++++++++++++++------------------------------------- 1 file changed, 26 insertions(+), 69 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index 3b3b757a9a..c9db35b8c8 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -42,8 +42,6 @@ static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94"; #include #include -#define ftell(s) _IO_ftell (s) - static int LogType = SOCK_DGRAM; /* type of socket connection */ static int LogFile = -1; /* fd for log */ static bool connected; /* have done connect */ @@ -122,13 +120,10 @@ void __vsyslog_internal (int pri, const char *fmt, va_list ap, unsigned int mode_flags) { - struct tm now_tm; - time_t now; - int fd; FILE *f; char *buf = 0; size_t bufsize = 0; - size_t msgoff; + int msgoff; int saved_errno = errno; char failbuf[3 * sizeof (pid_t) + sizeof "out of memory []"]; @@ -142,9 +137,7 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, /* Prepare for multiple users. We have to take care: most syscalls we are using are cancellation points. */ - struct cleanup_arg clarg; - clarg.buf = NULL; - clarg.oldaction = NULL; + struct cleanup_arg clarg = { NULL, NULL }; __libc_cleanup_push (cancel_handler, &clarg); __libc_lock_lock (syslog_lock); @@ -158,51 +151,22 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, /* Build the message in a memory-buffer stream. */ f = __open_memstream (&buf, &bufsize); - if (f == NULL) - { - /* We cannot get a stream. There is not much we can do but emitting an - error messages. */ - char numbuf[3 * sizeof (pid_t)]; - char *nump; - char *endp = __stpcpy (failbuf, "out of memory ["); - pid_t pid = __getpid (); - - nump = numbuf + sizeof (numbuf); - /* The PID can never be zero. */ - do - *--nump = '0' + pid % 10; - while ((pid /= 10) != 0); - - endp = __mempcpy (endp, nump, (numbuf + sizeof (numbuf)) - nump); - *endp++ = ']'; - *endp = '\0'; - buf = failbuf; - bufsize = endp - failbuf; - msgoff = 0; - } - else + if (f != NULL) { __fsetlocking (f, FSETLOCKING_BYCALLER); - fprintf (f, "<%d>", pri); - now = time_now (); - f->_IO_write_ptr += __strftime_l (f->_IO_write_ptr, - f->_IO_write_end - f->_IO_write_ptr, - "%h %e %T ", - __localtime_r (&now, &now_tm), - _nl_C_locobj_ptr); - msgoff = ftell (f); - if (LogTag == NULL) - LogTag = __progname; - if (LogTag != NULL) - __fputs_unlocked (LogTag, f); - if (LogStat & LOG_PID) - fprintf (f, "[%d]", (int) __getpid ()); - if (LogTag != NULL) - { - __putc_unlocked (':', f); - __putc_unlocked (' ', f); - } - + /* "%b %e %H:%M:%S" */ + char timebuf[sizeof "MMM DD hh:mm:ss "]; + time_t now = time_now (); + struct tm now_tm; + __localtime_r (&now, &now_tm); + __strftime_l (timebuf, sizeof (timebuf), "%b %e %T", &now_tm, + _nl_C_locobj_ptr); + + pid_t pid = LogStat & LOG_PID ? __getpid () : 0; + + fprintf (f, "<%d>%s %n%s%s%.0d%s: ", pri, timebuf, &msgoff, + LogTag == NULL ? __progname : LogTag, + pid != 0 ? "[" : "", pid, pid != 0 ? "]" : ""); /* Restore errno for %m format. */ __set_errno (saved_errno); @@ -216,26 +180,18 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, /* Tell the cancellation handler to free this buffer. */ clarg.buf = buf; } + else + { + /* Nothing much to do but emit an error message. */ + bufsize = __snprintf (failbuf, sizeof failbuf, "out of memory[%d]", + __getpid ()); + buf = failbuf; + } /* Output to stderr if requested. */ if (LogStat & LOG_PERROR) - { - struct iovec iov[2]; - struct iovec *v = iov; - - v->iov_base = buf + msgoff; - v->iov_len = bufsize - msgoff; - /* Append a newline if necessary. */ - if (buf[bufsize - 1] != '\n') - { - ++v; - v->iov_base = (char *) "\n"; - v->iov_len = 1; - } - - /* writev is a cancellation point. */ - __writev (STDERR_FILENO, iov, v - iov + 1); - } + __dprintf (STDERR_FILENO, "%s%s", buf + msgoff, + "\n" + (buf[bufsize - 1] == '\n')); /* Get connected, output the message to the local logger. */ if (!connected) @@ -264,6 +220,7 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, * Make sure the error reported is the one from the * syslogd failure. */ + int fd; if (LogStat & LOG_CONS && (fd = __open (_PATH_CONSOLE, O_WRONLY | O_NOCTTY | O_CLOEXEC, 0)) >= 0)