From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oo1-xc36.google.com (mail-oo1-xc36.google.com [IPv6:2607:f8b0:4864:20::c36]) by sourceware.org (Postfix) with ESMTPS id 6FD573888C74 for ; Fri, 18 Mar 2022 16:52:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6FD573888C74 Received: by mail-oo1-xc36.google.com with SMTP id k13-20020a4a948d000000b003172f2f6bdfso10819315ooi.1 for ; Fri, 18 Mar 2022 09:52:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fNFOB4NVB/p3CO4xPyjZeWNuOOzulitN8ZNSoY3V2dI=; b=EGyjHtfNsj4+5IupFVy3rtLo/8cgTfW+tF6mqs8Izosr2ct8Ln8N6xibyAdDczj2YA wUHlZJfiSHBoeRAZ+nTlmWFXIM5ctOyMYYHGCzj2pWHW8z9uNf5igya5H5x6q0NMb5+a gTKTmt2VyHbnbZu8WlVlLV0t9pzL4xnQg8kFUIBGO6lcO/gg4XcdC5sPKYBAB60P9nyA z+fmSh4y2896gbfcf6rckcacKCmPi1qOCsnlavf/4Nhtz3iZdDP4+dXhKrUQ4dY930om THBaQKjF40fwg22d9UiP/lb7eYFwtZHaKU5wbhlhTFwZcBeJg2mTxT6w2Xas4Yril/BK CQGA== X-Gm-Message-State: AOAM532YSy/HFX6vCtyzo0AQv0C3Hk+Cn9GTpVMg8XwJjFtfCoWP4OPF yekTiEGLUevEKT6BvJF1n3JidMhqneYfhQ== X-Google-Smtp-Source: ABdhPJyl/bAT+eiHW4TeJnc4tcqnMvPMpQq33oAFauavpJkKccKQhF3KMV4kNiBcbAF9p7Lm56qnDQ== X-Received: by 2002:a05:6870:9a0f:b0:da:71ab:35fa with SMTP id fo15-20020a0568709a0f00b000da71ab35famr7503440oab.76.1647622346403; Fri, 18 Mar 2022 09:52:26 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:99a3:99e2:1060:da92:ae49]) by smtp.gmail.com with ESMTPSA id c12-20020a9d75cc000000b005b24b061940sm3986417otl.33.2022.03.18.09.52.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Mar 2022 09:52:25 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v3 4/7] misc: syslog: Simplify implementation Date: Fri, 18 Mar 2022 13:52:11 -0300 Message-Id: <20220318165214.2291065-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220318165214.2291065-1-adhemerval.zanella@linaro.org> References: <20220318165214.2291065-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 18 Mar 2022 16:52:28 -0000 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 and i686-linux-gnu. --- misc/syslog.c | 96 ++++++++++++++++----------------------------------- 1 file changed, 29 insertions(+), 67 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index 0736459e7b..e8b1dfe9b8 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -123,13 +123,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 []"]; @@ -143,9 +140,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); @@ -159,51 +154,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 /* "%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); @@ -217,26 +185,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) @@ -265,6 +226,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)) -- 2.32.0