misc: syslog: Use fixed-sized buffer and remove memstream

A fixed-sized 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.

Also, since the buffer size is know, memstream is replaced with a
malloced buffer for larger messages.

Checked on x86_64-linux-gnu.
This commit is contained in:
Adhemerval Zanella 2021-10-05 09:58:09 -03:00
parent f9f5c70e7f
commit a583b6add4

View File

@ -120,12 +120,12 @@ void
__vsyslog_internal (int pri, const char *fmt, va_list ap, __vsyslog_internal (int pri, const char *fmt, va_list ap,
unsigned int mode_flags) unsigned int mode_flags)
{ {
FILE *f; /* Try to use a static buffer as an optimization. */
char *buf = 0; char bufs[1024];
char *buf = NULL;
size_t bufsize = 0; size_t bufsize = 0;
int msgoff; int msgoff;
int saved_errno = errno; int saved_errno = errno;
char failbuf[3 * sizeof (pid_t) + sizeof "out of memory []"];
#define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
/* Check for invalid bits. */ /* Check for invalid bits. */
@ -149,43 +149,61 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap,
if ((pri & LOG_FACMASK) == 0) if ((pri & LOG_FACMASK) == 0)
pri |= LogFacility; pri |= LogFacility;
/* Build the message in a memory-buffer stream. */ pid_t pid = LogStat & LOG_PID ? __getpid () : 0;
f = __open_memstream (&buf, &bufsize);
if (f != NULL) /* "%b %e %H:%M:%S " */
{ char timestamp[sizeof "MMM DD hh:mm:ss "];
__fsetlocking (f, FSETLOCKING_BYCALLER);
/* "%b %e %H:%M:%S" */
char timebuf[sizeof "MMM DD hh:mm:ss "];
time_t now = time_now (); time_t now = time_now ();
struct tm now_tm; struct tm now_tm;
__localtime_r (&now, &now_tm); __localtime_r (&now, &now_tm);
__strftime_l (timebuf, sizeof (timebuf), "%b %e %T", &now_tm, __strftime_l (timestamp, sizeof timestamp, "%b %e %T ", &now_tm,
_nl_C_locobj_ptr); _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)
int l = __snprintf (bufs, sizeof bufs,
SYSLOG_HEADER (pri, timestamp, &msgoff, pid));
if (0 <= l && l < sizeof bufs)
{
va_list apc;
va_copy (apc, ap);
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. */ /* Restore errno for %m format. */
__set_errno (saved_errno); __set_errno (saved_errno);
/* We have the header. Print the user's format into the buffer. */ int vl = __vsnprintf_internal (bufs + l, sizeof bufs - l, fmt, apc,
__vfprintf_internal (f, fmt, ap, mode_flags); mode_flags);
if (0 <= vl && vl < sizeof bufs - l)
{
buf = bufs;
bufsize = l + vl;
}
/* Close the memory stream; this will finalize the data into a malloc'd va_end (apc);
buffer in BUF. */ }
fclose (f);
if (buf == NULL)
{
buf = malloc (l * sizeof (char));
if (buf != NULL)
{
/* Tell the cancellation handler to free this buffer. */ /* Tell the cancellation handler to free this buffer. */
clarg.buf = buf; clarg.buf = buf;
__snprintf (buf, sizeof buf,
SYSLOG_HEADER (pri, timestamp, &msgoff, pid));
} }
else else
{ {
/* Nothing much to do but emit an error message. */ /* Nothing much to do but emit an error message. */
bufsize = __snprintf (failbuf, sizeof failbuf, "out of memory[%d]", bufsize = __snprintf (bufs, sizeof bufs,
__getpid ()); "out of memory[%d]", __getpid ());
buf = failbuf; buf = bufs;
}
} }
/* Output to stderr if requested. */ /* Output to stderr if requested. */
@ -236,7 +254,7 @@ out:
__libc_cleanup_pop (0); __libc_cleanup_pop (0);
__libc_lock_unlock (syslog_lock); __libc_lock_unlock (syslog_lock);
if (buf != failbuf) if (buf != bufs)
free (buf); free (buf);
} }